function testCookies(level, msgOn, msgOff, lang)
{
	if (level == undefined)
	{
		var level = '.';
	}
	if (document.cookie)
	{
		 document.open()
		 document.write(msgOn);
		 document.close()
		 }else{
		 document.open()
		 document.write('<span class="errorMsg"><a href="'+level+'/login/cookie-help.php?lang='+lang+'" target="_blank" >'+msgOff+'</a></span>');
		 document.close() 
	}
}

/***** IF USER ENTERS AN INCORRECT PASSWORD
****** THIS DISPLAYS THE ERROR MESSAGE
****** ON THE LOGIN PAGE
******************************/
function regCheck()
{

 	var txtBox = document.getElementById('box');
	txtBox.style.color = '#FF0';
	txtBox.style.fontSize = '0.9em';
	txtBox.style.fontWeight = 'bold';
	if (txtBox.style.display != 'none')
	{
	 	txtBox.style.display = 'block';
		txtBox.innerHTML = "";
	}
	else
	{
	 	txtBox.style.display = 'block';

	}
}



function showHideElement(id, extra)
{
	var element = document.getElementById(id);

	if(element.style.display == 'block')
	{
     	element.style.display = 'none';
    }
    else
    {
    	element.style.display = 'block';
    }
    if(extra == 'true')
    {
    	document.getElementById("darkBackgroundLayer").style.display = "none";
    	document.getElementById("indexCloseBtn").style.display = "none";
    }
    
}

/****************** bits for my details and register page ************************/


function checkFields(formName, email)
{
	var emailChk = email;
	var mail = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{ > 1,3})|([a-zA-Z]{2,7})|(aero|coop|info|museum|name))$/

	if (emailChk.search(mail)==-1)
	{
		alert('Oops!\nPlease enter a correctly formatted email address and click submit');
		return false;
	}

	if (emailChk == '')
	{
		alert('Oops!\nPlease enter your email address and press submit at the bottom of the page');
		return false;
	}
	if (document.forms[formName].elements['country'].value == "0")
	{
		alert('Oops!\nPlease select a country');
		return false;
	}
	if (document.forms[formName].elements['passhint'].value == "")
	{
		alert('Oops!\nPlease enter a password hint');
		return false;
	}
}

function same_as_address(formName)
{
	var checkbox_status = document.forms[formName].elements['same'].checked

	if ( !checkbox_status )
	{

		document.forms[formName].elements['same'].checked = checkbox_status
		document.forms[formName].elements['baddress1'].value = ''
		document.forms[formName].elements['baddress2'].value = ''
		document.forms[formName].elements['btown'].value = ''
		document.forms[formName].elements['bcounty'].value = ''
		document.forms[formName].elements['bpostcode'].value = ''
		document.forms[formName].elements['bcountry'].value = ''

	}
	else
	{

		document.forms[formName].elements['same'].checked = checkbox_status
		document.forms[formName].elements['baddress1'].value = document.forms[formName].elements['address1'].value
		document.forms[formName].elements['baddress2'].value = document.forms[formName].elements['address2'].value
		document.forms[formName].elements['btown'].value = document.forms[formName].elements['town'].value
		document.forms[formName].elements['bcounty'].value = document.forms[formName].elements['county'].value
		document.forms[formName].elements['bpostcode'].value = document.forms[formName].elements['postcode'].value
		document.forms[formName].elements['bcountry'].value = document.forms[formName].elements['country'].value
	}

}

function sameAddressPostal(formName)
{
	var checkbox_status = document.forms[formName].elements['same'].checked

	if ( !checkbox_status )
	{

		document.forms[formName].elements['same'].checked = checkbox_status
		document.forms[formName].elements['address1'].value = ''
		document.forms[formName].elements['address2'].value = ''
		document.forms[formName].elements['town'].value = ''
		document.forms[formName].elements['county'].value = ''
		document.forms[formName].elements['postcode'].value = ''
		document.forms[formName].elements['country'].value = ''

	}
	else
	{

		document.forms[formName].elements['same'].checked = checkbox_status
		document.forms[formName].elements['address1'].value = document.forms[formName].elements['baddress1'].value
		document.forms[formName].elements['address2'].value = document.forms[formName].elements['baddress2'].value
		document.forms[formName].elements['town'].value = document.forms[formName].elements['btown'].value
		document.forms[formName].elements['county'].value = document.forms[formName].elements['bcounty'].value
		document.forms[formName].elements['postcode'].value = document.forms[formName].elements['bpostcode'].value
		document.forms[formName].elements['country'].value = document.forms[formName].elements['bcountry'].value
	}

}

function forgot(){
	document.form1.submit();
}

function hint(){
	
	if (document.form1.email.value == "")
	{
		alert("Please enter an email address");
	}
	else
	{
		document.forms[0].action="hint.php";
		document.forms[0].submit();
	}
}

function confirmShut()
{
	var agree=confirm('Are you sure you wish to close the site?');
	if (agree)
		return true;
	else
		return false;
}

/********* ADMIN REFUNDS PAGE VALIDATION ********************/
function ValidateFormDate(form)
{
	
	var regdate = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/
	
	if (form.from_date.value =="")
	{
 	 	alert ('Please enter a date in the from field')
 	 	return false;
 	}
 	if (form.to_date.value =="")
	{
 	 	alert ('Please enter a date in the to field')
 	 	return false;
 	}
 	if (form.from_date.value.search(regdate) == -1)
 	{
 		alert ('The date should be entered dd-mm-yyy')
 		return false;
 	}
 	if (form.to_date.value.search(regdate) == -1)
 	{
 		alert ('The date should be entered dd-mm-yyy')
 		return false;
 	}
 	
 	if (( document.datesearch.type[0].checked == false ) 
	  && ( document.datesearch.type[1].checked == false ))
    {
        alert ( "Please select something to search for" );
        return false;
    }
}

function ValidateForm(form)
{
	var regnum = /(^-?\d\d*$)/
	
	if (form.inv_from.value =="")
	{
 	 	alert ('Please enter a number in the from field')
 	 	return false;
 	}
 	if (form.inv_to.value =="")
	{
 	 	alert ('Please enter a number in the to field')
 	 	return false;
 	}
 	if (form.inv_from.value.search(regnum) == -1)
 	{
 		alert ('Please only enter numbers')
 		return false;
 	}
 	if (form.inv_to.value.search(regnum) == -1)
 	{
 		alert ('Please only enter numbers')
 		return false;
 	}
 	
 	if (( document.invsearch.what[0].checked == false ) 
	  && ( document.invsearch.what[1].checked == false ))
    {
        alert ( "Please select something to search for" );
        return false;
    }
}

//validate invoice list form
function ValidateFormList(form)
{
	
	var regdate = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/
	
	if (form.inv_from.value =="")
	{
 	 	alert ('Please enter a date in the from field')
 	 	return false;
 	}
 	if (form.inv_to.value =="")
	{
 	 	alert ('Please enter a date in the to field')
 	 	return false;
 	}
 	if (form.inv_from.value.search(regdate) == -1)
 	{
 		alert ('The date should be entered dd-mm-yyy')
 		return false;
 	}
 	if (form.inv_to.value.search(regdate) == -1)
 	{
 		alert ('The date should be entered dd-mm-yyy')
 		return false;
 	}	
}

var thisCount = 0;
function selectMbox()
{
	if (thisCount == 0)
	{
		document.getElementById('selectBox').style.display = 'block';
		thisCount += 1; 
	}
	else
	{
		document.getElementById('selectBox').style.display = 'none';
		thisCount =0;
	}
}

//MAIL CLIENT:
function clearForm()
	{
		window.location = "./require/refresh_search.php";
	}

	function formValidateMail()
	{
		var regdate = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/
		
		if (document.search1.elements['sub'].value == 3)
		{
			alert("You must select a user subscription status");
			return false;
		}
		
		if (document.search1.elements['country'].value == 0)
		{
			alert("You must select a country group");
			return false;
		}
		if (document.search1.elements['route'].value == 0)
		{
			alert("You must select an option from the 'ROUTE' field");
			return false;
		}
		
		if (document.search1.elements['country'].value == '1' && document.search1.elements['countrySelect'].value == '0')
		{
			alert("You must select a country from the list");
			return false;
		}
		
		if (document.search1.elements['route'].value == '1' && document.search1.elements['purchHist'].value == '0')
		{
			alert("You must select an option from the 'PURCHASE HISTORY' list");
			return false;
		}
		
			
		if (document.search1.elements['purchHist'].value == 'purchases')
		{
			if ((document.search1.elements['startDate1'].value.search(regdate) == -1) || (document.search1.elements['endDate1'].value.search(regdate) == -1))
			{
				alert ('The date should be entered dd-mm-yyyy or dd/mm/yyyy')
				return false;
			}
		}
		
		if (document.search1.elements['purchHist'].value == 'purchases_pending')
		{
			if ((document.search1.elements['startDate1'].value.search(regdate) == -1) || (document.search1.elements['endDate1'].value.search(regdate) == -1))
			{
				alert ('The date should be entered dd-mm-yyyy or dd/mm/yyyy')
				return false;
			}
		}
		
		if (document.search1.elements['purchHist'].value == 'item' && document.search1.elements['media_id'].value == '0')
		{
			alert ('Please select a product to search for')
			return false;
		}
		
		if (document.search1.elements['purchHist'].value == 'itemNot' && document.search1.elements['media_id'].value == '0')
		{
			alert ('Please select a product to search for')
			return false;
		}
		
		if (document.search1.elements['route'].value == '2' && document.search1.elements['discCode'].value == '0')
		{
			alert("You must select an option from the 'DISCOUNT CODES ISSUED' list");
			return false;
		}
		
		if (document.search1.elements['route'].value == '3' && document.search1.elements['refund'].value == '0')
		{
			alert("You must select an option from the 'REFUND TABLE' list");
			return false;
		}
	}

/********************** MAIL BOX FUNCS *************************************************/
function checkHTMLEmpty()
	{
		if (document.fileUpload.elements['emailBody'].value == "" || document.fileUpload.elements['subject'].value == "")
		{
			alert('Please enter both a subject and select a file to upload for the HTML email.');
			return false
		}
	}
	function checkTextEmpty()
	{
		if (document.textemail.elements['composedMail'].value == "" || document.textemail.elements['subject'].value == "")
		{
			alert('Please enter both a subject and a message to send for the text email.');
			return false
		}
	}

	function showHTMLTimez(theValue)
	{
		var radioVal = theValue.value;
		
		if (radioVal == 't-yes')
		{
			document.getElementById('htmlTimezone').style.display = 'block';
		}
		else if (radioVal == 't-no')
		{
			document.getElementById('htmlTimezone').style.display = 'none';
		}
	}


	function showTextTimez(theValue)
	{
		var radioVal = theValue.value;
		
		if (radioVal == 't-yes')
		{
			document.getElementById('textTimezone').style.display = 'block';
		}
		else if (radioVal == 't-no')
		{
			document.getElementById('textTimezone').style.display = 'none';
		}
	}

	function showImgs(iValue)
	{
		var imgVal = iValue.value;
		
		if (imgVal == 'yes')
		{
			document.getElementById('images').style.display = 'block';
		}
		else if (imgVal == 'no')
		{
			document.getElementById('images').style.display = 'none';
		}
	}


function confirmCancel(job)
	{
		if (confirm('This will cancel mail job number '+job+'.\nAre you sure you want to do this?'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	function confirmRemove(job)
	{
		if (confirm('This will delete mail job number '+job+' from the database.\nAre you sure you want to do this?'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	function confirmJob(job)
	{
		if (confirm('This will activate and start sending job number '+job+'\nAre you sure you want to do this?'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	function testJob(job)
	{
		var emailAd = '';
		
		if (emailAd = prompt('To send one email to yourself for job number '+job+'\nPlease enter your email address below', 'kit@pro-create.co.uk'))
		{
			var reg1str = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
			var reg2str = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			var reg1 = new RegExp(reg1str);
			var reg2 = new RegExp(reg2str);

			if (!reg1.test(emailAd) && reg2.test(emailAd))
			{
				mailTestRequest(job, emailAd);
			}
			else
			{
				alert('Email address incorrect');
			}

		}
		else
		{
			//do nothing
		}
	}
	function previewMail(filename)
	{

		window.open(filename,'mywin');
		return false;

	}
	
function confirmEdit()
{
	if (confirm('Are you sure you want to accept this email?'))
	{
		return true;
	}
	else 
	{
		return false;
	}
}

	function checkedAll (id, checked)
	{
		var el = document.getElementById(id);
		for (var i = 0; i < el.elements.length; i++)
		{
		  el.elements[i].checked = checked;
		}
	}


	function submitDelete ()
	{
		if (confirm('Are you sure you want to delete all the selected emails?'))
		{
			document.delMail.submit();
		}
		else
		{
			return false;
		}
	}

/************************** SITE STATS FUNCS ********************************************/
	function testDate (sDate, eDate, nowDate)
	{
		var firstIndex = sDate.indexOf ("/");
		var lastIndex = sDate.lastIndexOf ("/");
		day1 = sDate.substring (0, firstIndex);
		month1 = sDate.substring (firstIndex+1, lastIndex);
		year1 = sDate.substring (lastIndex+1, sDate.length);
	
		date1 = year1+month1+day1;
		
		day2 = eDate.substring (0, firstIndex);
		month2 = eDate.substring (firstIndex+1, lastIndex);
		year2 = eDate.substring (lastIndex+1, eDate.length);
		
		date2 = year2+month2+day2;
		
		dayNow = nowDate.substring (0, firstIndex);
		monthNow = nowDate.substring (firstIndex+1, lastIndex);
		yearNow = nowDate.substring (lastIndex+1, nowDate.length);
		
		dateNow = yearNow+monthNow+dayNow;
		
		if(date1 > date2)
		{
			return -1;
		}
		else if(date1 > dateNow || date2 > dateNow)
		{
			return 1;
		}
	}
		
	function formValidate()
	{
		var regdate = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/
		
		var sDate = document.search1.elements['startDate'].value;
		var eDate = document.search1.elements['endDate'].value;
		var nowDate = document.search1.elements['now'].value;
		var dropDown = document.search1.elements['selectMonth'].value;
		var dateTest;
		
		if(sDate > "" || eDate > "")
		{
			if ((sDate.search(regdate) == -1) || (eDate.search(regdate) == -1))
			{
				alert ('The date should be entered dd/mm/yyyy')
				return false;
			}
			
			dateTest = testDate(sDate, eDate, nowDate);
			
			if (dateTest == -1)
			{
				alert("Your end date is before to your start date!\nPlease try another.");
				return false;
			}
			else if (dateTest == 1)
			{
				alert("This date range is invalid!\nPlease try another.");
				return false;
			}
		}

		if(sDate > "" && eDate > "" && dropDown!="false")
		{
			alert("Please select either a date range OR a drop down period.");
			return false;
		}
		
	}

// FINISH ABOVE
var numb = /^([0-9]{1,3})?((.|,|\'|\s)([0-9]{2,2}))$/

	
		function validateFormEditBundle(form) 
		{ 
			var numbTwo = /^([0-9]{1,3})$/
		 	if (form.title.value == "")
		 	{
		 	 	alert ('Please enter a title for the refund')
		 	 	return false;
		 	}
			
		
			var keyword = document.getElementById('sms_keyword').value;
			var origKeyword = document.getElementById('orig_keyword').value;
			
			if (keyword != origKeyword)
			{ 
				if(keyword > "")
				{
			 		for (myInt=0; myInt<document.getElementById('numKeywords').value; myInt++)
			 	 	{
			 	 		if (document.getElementById('sms_keyword_'+myInt).value == document.getElementById('sms_keyword').value)
			 	 		{
			 	 			alert("Your sms keyword is already being used\nPlease choose another");
			 	 			return false;
			 	 		}
			 	 	}	
		 		}
	 		}
			
			
			/*if (form.stock_level.value.search(numbTwo)==-1)
		 	{
		 		alert ('You must enter a number for the stock amount')
		 		return false;
		 	}*/
		
			return true;
			
			
		}
		
/******** POLL FUNCS ********************/
function confirmSubmitPoll()
	{

		amount = box.options[box.selectedIndex].value;
				
		for (var i=0; i < amount; i++)
		{
			if (document.getElementById('ans'+i).value == "")
			{
				alert("You have not filled in all the answers.\nPlease check and try again.\nOr remove unwanted answers be pressing 'REMOVE'.");
				return false;
			}
		}
		if(document.getElementById('question').value == "")
		{
			alert("You have not filled in a question.\nPlease check and try again.");
			return false;
		}

		var agree=confirm('Are you sure you wish to add this poll?');
		if (agree)
			return true;
		else
			return false;
	}

	function addBox()
	{
		box = document.getElementById('noQ');
		amount = box.options[box.selectedIndex].value;
		
		var textBoxes = document.getElementById('textBoxes');
		textBoxes.innerHTML = "";
		var n = 1;
		
		for (var i=0; i < amount; i++)
		{
			textBoxes.innerHTML = textBoxes.innerHTML +"<div id='a"+i+"'><span class='textFieldReg'>ANSWER "+n+":</span><input type='text' name='a"+i+"' id='ans"+i+"' class='myInputReg' /><span class='details'><a href='#' onclick='removeBox(\"a"+i+"\")';>REMOVE</a></div>"
			n++;
		}
		
		textBoxes.innerHTML = textBoxes.innerHTML +"<br /><div class='fullRighty'><input type='submit' class='mySubmit' value='SUBMIT' onclick='return confirmSubmitPoll()'/></div>";
	}
	
	function removeBox(id)
	{
		var d = document.getElementById('textBoxes');
		var olddiv = document.getElementById(id);	
		d.removeChild(olddiv);
	}
	function confirmDelete()
	{
		var agree = confirm('Are you sure you wish to delete this poll?');
		if (agree)
			return true;
		else
			return false;
	}
	function confirmInactive()
	{
		var agree = confirm('Are you sure you wish to inactivate this poll?');
		if (agree)
			return true;
		else
			return false;
	}
	
	function confirmDeletePollEdit()
	{
		var agree = confirm('Are you sure you wish to delete this answer?');
		if (agree)
			return true;
		else
			return false;
	}
	

/************************ new for gallery ****************************/
function confirmNews(id, area, page, link)
{		
	if(page == "process_remove")
	{
		var msg = 'Are you sure you wish to delete this item?';
		var agree=confirm(msg);
		if (agree){
			ajaxFunction(id, area, page, link);
		}
		else
			return false;
	}
	else if (page == "process_edit")
	{
		ajaxFunction(id, area, page, link);
	}	
}

function confirmDeletePhoto(id, gallid, pic, gallArea)
{

	var agree=confirm('Are you sure you wish to delete this photo?');
	if (agree)
		window.location = "./include/delete_photo.php?id="+id+"&gall="+gallid+"&pic="+pic+"&gall_area="+gallArea;
	else
		return false;
}
	
function confirmDeleteGallery(id, pic, gall)
{

	var agree=confirm('Are you sure you wish to delete this gallery?\nThis will delete all photos in this gallery.');
	if (agree)
		window.location = "./include/delete_gallery.php?id="+id+"&pic="+pic+"&gall="+gall;
	else
		return false;
}	

/*function confirmDeleteItem(info, gallid, pic, gallArea)
{

	var agree=confirm('Are you sure you wish to delete this photo?');
	if (agree)
		window.location = "./include/delete_photo.php?id="+id+"&gall="+gallid+"&pic="+pic+"&gall_area="+gallArea;
	else
		return false;
}*/
	
/**************** scroller functions ********************/
function scrollLefty(id)
{
	document.getElementById('scroller').scrollLeft -= 10;
	scrollDelay = setTimeout("scrollLefty()",100)
}
function scrollRighty()
{
	document.getElementById('scroller').scrollLeft += 10;
	scrollDelay = setTimeout("scrollRighty()",100)
}
function scrollUp(id)
{
	document.getElementById('newsCont').scrollTop -= 10;
	scrollDelay = setTimeout("scrollUp()",100)
}
function scrollDown()
{
	document.getElementById('newsCont').scrollTop += 10;
	scrollDelay = setTimeout("scrollDown()",100)
}
function stopScroll()
{
	clearTimeout(scrollDelay);
}
/******************************************************/


function copyit(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")

/*var holdtext = document.all['holdtext'];

holdtext.innerText = tdObj.innerText;

Copied = holdtext.createTextRange();

Copied.execCommand("Copy");*/
}


/******************* NEW GENERIC CODE BELOW *********************/

function genericDelete(msg, url, useFunction, whatFunction, var1, var2, var3, var4)
{
	var agree=confirm(msg);
		
	if (agree)
	{
		if (useFunction == true)
		{
			window[whatFunction](var1, var2, var3, var4);
		}
		else if (useFunction == false)
		{
			window.location = url;
		}
	}
	else
		return false;
}

function displayMsg(msg)
{
	alert(msg);
}

function genericShowHide(divToShow, divGetValue, condition)
{
	var divValue = document.getElementById(divGetValue).value;
	var divShow = document.getElementById(divToShow);
	
	if (divValue == condition)
	{
		if(divShow.style.display == 'block')
		{
			divShow.style.display = 'none';
		}
		else if (divShow.style.display == 'none')
		{
			divShow.style.display = 'block';
		}
	}
	else if (divValue != condition)
	{
		divShow.style.display = 'none';
	}
}

function confirmSubmit(msg)
{
	var agree=confirm(msg);
	if (agree)
		return true;
	else
		return false;
}	

function submitLogin()
{
	document.login.submit();
}

function submitPurchase()
{
	document.address.submit();
}

function logoutSubmit()
{
	document.logoutSubmit.submit();
}

function submitMyDetails()
{
	document.myDeets.submit();
}

function submitContact()
{
	document.contForm.submit();
}


var popupStatus = 0;
   
function loadPopup()
{

  if(popupStatus==0)
  {
     $("#popupBackground").css(
     {
        "opacity": "0.5"
     });
     $("#popupBackground").fadeIn("slow");
     $("#viewNews").fadeIn("slow");
     $("#indexCloseBtn").fadeIn("slow");
     popupStatus = 1;
  }
  
}

function disablePopup()
{
  if(popupStatus == 1)
  {
     $("#popupBackground").fadeOut("slow");
     $("#viewNews").fadeOut("slow")
      //$("#indexCloseBtn").fadeOut("slow");
     popupStatus = 0;
  }
}

function centrePopup()
{
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  
  var popupHeight = $("#viewNews").height();
  var popupWidth = $("#viewNews").width();
  
  //var closeHeight = $("#indexCloseBtn").height();
  //var closeWidth = $("#indexCloseBtn").width();

  $("#viewNews").css(
  {
     "position": "fixed",
     "top": windowHeight/2-popupHeight/2,
     "left": windowWidth/2-popupWidth/2
  });
  
  /*$("#indexCloseBtn").css(
  {
     "position": "fixed",
     "top": windowHeight/8-closeHeight/2,
     "left": windowWidth/2-closeWidth/2
  });*/
  
  $("#popupBackground").css({
     "height": windowHeight
  });
}
   
   
   
  
   	
