// JavaScript Document
var addresseslinked=0;
var editorShown=0;
var killEditor=0;
var varItem;
startTimer();
function showDiv(theDiv){
	if(document.getElementById(theDiv).style.display!="block"){
		document.getElementById(theDiv).style.display="block";
		var date= new Date();
		date.setTime(date.getTime()+(7*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = theDiv+"="+"1"+expires+"; path=/";
	} else {
		document.getElementById(theDiv).style.display="none";
		var date= new Date();
		date.setTime(date.getTime()+(7*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = theDiv+"="+"0"+expires+"; path=/";
	}
}
function doMenuOpener(){
	var ca = document.cookie.split('; ');
	for(var i=0; i < ca.length; i++)
	{
		var c = ca[i];
		openOrClosed=c.split('=');
		if(IsNumeric(openOrClosed[0].substr(1,3))){
			if(document.getElementById(openOrClosed[0])){
				if(openOrClosed[1]=='1'){
					document.getElementById(openOrClosed[0]).style.display="block";
				} else {
					document.getElementById(openOrClosed[0]).style.display="none";
				}
			}
		}
	}
	return null;

}

function doMenuOpener(){
	var ca = document.cookie.split('; ');
	for(var i=0; i < ca.length; i++)
	{
		var c = ca[i];
		openOrClosed=c.split('=');
		if(IsNumeric(openOrClosed[0].substr(1,3))){
			if(document.getElementById(openOrClosed[0])){
				if(openOrClosed[1]=='1'){
					document.getElementById(openOrClosed[0]).style.display="block";
				} else {
					document.getElementById(openOrClosed[0]).style.display="none";
				}
			}
		}
	}
	return null;

}

function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}  

function changeimage(newPic){
	document.getElementById("shownpic").src="productimages/"+newPic+".jpg";
}function changeimagecms(newPic){
	document.getElementById("shownpic").src="../productimages/"+newPic+".jpg";
}

function dosameas(){
	if(document.getElementById('checkbox').status){
		addresseslinked=1;
		document.getElementById('shipaddress').value=document.getElementById('billaddress').value;
		document.getElementById('shiptown').value=document.getElementById('billtown').value;
		document.getElementById('shipcounty').value=document.getElementById('billcounty').value;
		document.getElementById('shippostcode').value=document.getElementById('billpostcode').value;
		
		document.getElementById('shipaddress').disabled=true;
		document.getElementById('shiptown').disabled=true;
		document.getElementById('shipcounty').disabled=true;
		document.getElementById('shippostcode').disabled=true;
	} else {
		addresseslinked=0;
		document.getElementById('shipaddress').disabled=false;
		document.getElementById('shiptown').disabled=false;
		document.getElementById('shipcounty').disabled=false;
		document.getElementById('shippostcode').disabled=false;
	}
}
function keypressed(){
	if(addresseslinked){
		document.getElementById('shipaddress').value=document.getElementById('billaddress').value;
		document.getElementById('shiptown').value=document.getElementById('billtown').value;
		document.getElementById('shipcounty').value=document.getElementById('billcounty').value;
		document.getElementById('shippostcode').value=document.getElementById('billpostcode').value;
	}
}
function pipon(theDiv){
	document.getElementById(theDiv).style.backgroundImage="url(images/pip.gif)";
}
function pipoff(theDiv){
	document.getElementById(theDiv).style.backgroundImage="";
}


function showEditor(theDiv, theItem, theElement, theType){
	if(document.getElementById("editor")){
		varItem=theItem;
		editorShown=0;
		document.getElementById("addcat").title="Add subcategory to "+theItem;
		document.getElementById("addprod").title="Add product type to "+theItem;
		document.getElementById("delcat").title="Delete "+theItem;
		document.getElementById("rename").title="Rename "+theItem;
		document.getElementById("up").title="Move "+theItem+" up the menu";
		document.getElementById("down").title="Move "+theItem+" down the menu";
		document.getElementById("editor").style.display="block";
		document.getElementById("editor").style.left=document.getElementById("shopcontainer").offsetLeft+90;
		document.getElementById("editor").style.top=document.getElementById(theDiv).offsetTop+213;
		if(theType){
			document.getElementById("addcat").style.display="none";
			document.getElementById("addprod").style.display="none";
			document.getElementById("rename").style.display="none";
		} else {
			document.getElementById("addcat").style.display="block";
			document.getElementById("addprod").style.display="block";
			document.getElementById("rename").style.display="block";
		}
		document.getElementById("addcat").style.display="none";
		document.getElementById("menuelementtoalter").value=theElement;
	}
}

function startTimer(){
	if (killEditor==1){
		editorShown+=1;
	}
	if(editorShown>5){
		document.getElementById("editor").style.display='none';
	}
	setTimeout(startTimer, 100);

}


function ValidateRegistration()
{
	var error_title = "There is a problem with the information you have entered.\n\n";
	
	var error_msg = "";
	
	var error_last = "\nPlease enter the above information correctly to register.";
	
	if(is_empty("in_businessname"))
		error_msg += "Please enter your business name\n";
	
	if(is_empty("in_forename"))
		error_msg += "Please enter your forename\n";
		
	if(is_empty("in_surname"))
		error_msg += "Please enter your surname\n";
		
	if(is_empty("in_businessadd1"))
		error_msg += "Please enter your business address line 1\n";
		
	if(is_empty("in_businesscity"))
		error_msg += "Please enter your town / city\n";
		
	if(is_empty("in_businesspostcode"))
		error_msg += "Please enter your postcode\n";
	else
		if(!valid_postcode("in_businesspostcode"))
			error_msg += "Please enter a valid postcode\n";
			
	/*if(is_empty("in_nodaysstock"))
	{
		var the_obj = document.getElementById("in_nodaysstock");
		the_obj.value = 1;
	}
	else
		if(!valid_number("in_nodaysstock"))
			error_msg += "Please enter a valid number of days for stock delivery\n";
	*/	
	if(is_empty("in_telephone"))
		error_msg += "Please enter your telephone number\n";
	else
		if(!valid_phone("in_telephone"))
			error_msg += "Please enter a valid telephone number\n";
			
	if(!is_empty("in_mobile"))
		if(!valid_phone("in_mobile"))
			error_msg += "Please enter a valid mobile number\n";
			
	if(is_empty("in_email"))
		error_msg += "Please enter your email address\n";
	else
		if(!valid_email("in_email"))
			error_msg += "Please enter a valid email address\n";
		
	if(is_empty("in_username"))
		error_msg += "Please enter a username\n";
	else
		if(!valid_username("in_username"))
			error_msg += "Username can only contain alphanumeric characters, full-stops and underscores (_)\n";
			
	if(is_empty("in_password1"))
		error_msg += "Please enter a password (at least 5 characters)\n";
	else
	if(!valid_password("in_password1"))
		error_msg += "Password must be 5 characters or more\n";
		
	if(is_empty("in_password2"))
		error_msg += "Please confirm password\n";
	else
	if(!equal_values("in_password1","in_password2"))
		error_msg += "Passwords do not match. Please re-enter and confirm password\n";
		
	
		
	if(error_msg.length>0)
	{
		alert(error_title+error_msg+error_last);
	}
	else
	{
		submit_form("register_form");
	}
}

function is_empty(field)
{
	the_field = document.getElementById(field);
	
	if(the_field.value=="")
		return true;
	else
		return false;
}

function valid_number(field_id)
{
	var valid_number_exp = new Array();
	valid_number_exp[0] = "^[0-9]+$";

	var the_obj = document.getElementById(field_id);
	
	if(the_obj)
		if(matches_expressions(valid_number_exp,the_obj.value))
			return true;

	return false;
}

function matches_expressions(ex_ar,check_against)
{
	for(i=0;i<ex_ar.length;i++)
	{
		var re = new RegExp(ex_ar[i]);

		if(check_against.match(re))
			return true;
	}
	return false;
}

function valid_postcode(field_id)
{
	var valid_codes_regex = new Array();
	
	var alpha = "[A-Z]";
	var num = "[0-9]";
	
	valid_codes_regex[0] = "^" + alpha + num + " ?" + num + alpha + alpha + "$";
	valid_codes_regex[1] = "^" + alpha + num + num + " ?" + num + alpha + alpha + "$";
	valid_codes_regex[2] = "^" + alpha + alpha + num + " ?" + num + alpha + alpha + "$";
	valid_codes_regex[3] = "^" + alpha + alpha + num + num + " ?" + num + alpha + alpha + "$";
	valid_codes_regex[4] = "^" + alpha + num + alpha + " ?" + num + alpha + alpha + "$";
	valid_codes_regex[5] = "^" + alpha + alpha + num + alpha + " ?" + num + alpha + alpha + "$";
	
	var the_obj = document.getElementById(field_id);
	if(the_obj)
	{
		the_obj.value = the_obj.value.toUpperCase();
		
		if(matches_expressions(valid_codes_regex,the_obj.value))
			return true;
	}
	
	return false;
}


function valid_phone(field_id)
{
	var valid_phone_exp = new Array();
	valid_phone_exp[0] = "^[0-9][0-9 ]*$";
	
	var the_obj = document.getElementById(field_id);

	if(the_obj)
		if(matches_expressions(valid_phone_exp,the_obj.value) && the_obj.value.length>6)
			return true;
			
	return false;
}

function valid_email(field_id)
{
	var valid_email_exp = new Array();
	valid_email_exp[0] = "^[a-zA-z.0-9\-\_]+@{1}[a-zA-z0-9\-\_]+[.]{1}[a-zA-z.0-9\-\_]+$";
		
	var the_obj = document.getElementById(field_id);
		if(the_obj)
			if(matches_expressions(valid_email_exp,the_obj.value))
				return true;
				
	return false;
}

function valid_username(field_id)
{
	var valid_username_exp = new Array();
	valid_username_exp[0] = "^[A-Za-z0-9._]+$";
		
	var the_obj = document.getElementById(field_id);
		if(the_obj)
			if(matches_expressions(valid_username_exp,the_obj.value))
				return true;
				
	return false;
}

function valid_password(field_id)
{
	var the_obj = document.getElementById(field_id);
	if(the_obj)
		if(the_obj.value.length<5)
			return false;
				
	return true;
}

function valid_number(field_id)
{
	var valid_number_exp = new Array();
	valid_number_exp[0] = "^[0-9]+$";

	var the_obj = document.getElementById(field_id);
	
	if(the_obj)
		if(matches_expressions(valid_number_exp,the_obj.value))
			return true;

	return false;
}

function equal_values(field_1,field_2)
{
	var the_obj1 = document.getElementById(field_1);
	var the_obj2 = document.getElementById(field_2);
		
	if(the_obj1 && the_obj2)
		if(the_obj1.value == the_obj2.value)
			return true;
			
	return false;
}

function submit_form(form_id)
{
	var the_form=document.getElementById(form_id);
	
	if(the_form)
		the_form.submit();
}

function field_exists(field_id)
{
	var the_obj = document.getElementById(field_id);
	
	if(the_obj)
		return true;
	else
		return false;
}


function ValidateUpdateCustomer()
{
	var error_title = "There is a problem with the information you have entered\n\n";
			
	var error_msg = "";

	var error_last = "\nPlease enter the above information correctly to update this customer";


	if(is_empty("in_contactforename"))
		error_msg += "Please enter the trade users Forename\n";

	if(is_empty("in_contactsurname"))
		error_msg += "Please enter the trade users Surname\n";

	if(is_empty("in_businessname"))
		error_msg += "Please enter the trade users Business Name\n";
		
	if(is_empty("in_businessadd1"))
		error_msg += "Please enter the trade users Address Line 1\n";

	if(is_empty("in_businesscity"))
		error_msg += "Please enter the trade users Town / City\n";

	if(is_empty("in_businesspostcode"))
		error_msg += "Please enter the trade users Postcode\n";
	else
	if(!valid_postcode("in_businesspostcode"))
		error_msg += "Please enter a valid postcode\n";

	if(is_empty("in_telephone"))
		error_msg += "Please enter a telephone number\n";
	else
	if(!valid_phone("in_telephone"))
		error_msg += "Please enter a valid telephone number\n";

	if(is_empty("in_email"))
		error_msg += "Please enter an email address\n";
	else
	if(!valid_email("in_email"))
		error_msg += "Please enter a valid email address\n";

	if(!is_empty("in_password1"))
	{
		if(!valid_password("in_password1"))
			error_msg += "Password must be 5 characters or more\n";
			
		if(is_empty("in_password2"))
			error_msg += "Please confirm password\n";
		else
		if(!equal_values("in_password1","in_password2"))
			error_msg += "Passwords do not match. Please re-enter and confirm password\n";
	}

	if(error_msg.length>0)
	{
		alert(error_title+error_msg+error_last);
	}
	else
	{
		submit_form("add_customer");
	}
}

function ValidateAddCustomer()
{
	var error_title = "There is a problem with the information you have entered\n\n";
		
	var error_msg = "";
		
	var error_last = "\nPlease enter the above information correctly to register";
	
	
		
	if(is_empty("in_contactforename"))
		error_msg += "Please enter the trade users Forename\n";
		
	if(is_empty("in_contactsurname"))
		error_msg += "Please enter the trade users Surname\n";
		
	if(is_empty("in_businessname"))
		error_msg += "Please enter the trade users Business Name\n";
		
	if(is_empty("in_businessadd1"))
		error_msg += "Please enter the trade users Address Line 1\n";
		
	if(is_empty("in_businesscity"))
		error_msg += "Please enter the trade users Town / City\n";
		
	if(is_empty("in_businesspostcode"))
		error_msg += "Please enter the trade users Postcode\n";
	else
	if(!valid_postcode("in_businesspostcode"))
		error_msg += "Please enter a valid postcode\n";
		
	if(is_empty("in_telephone"))
		error_msg += "Please enter a telephone number\n";
	else
	if(!valid_phone("in_telephone"))
		error_msg += "Please enter a valid telephone number\n";
		
	if(is_empty("in_email"))
		error_msg += "Please enter an email address\n";
	else
	if(!valid_email("in_email"))
		error_msg += "Please enter a valid email address\n";
		
	if(is_empty("in_username"))
		error_msg += "Please enter a username for the customer\n";
	else
	if(!valid_username("in_username"))
		error_msg += "Username can only contain alphanumeric characters, full-stops and underscores (_)\n";
		
	if(is_empty("in_password1"))
		error_msg += "Please enter a password (at least 5 characters)\n";
	else
	if(!valid_password("in_password1"))
		error_msg += "Password must be 5 characters or more\n";

	if(is_empty("in_password2"))
		error_msg += "Please confirm password\n";
	else
	if(!equal_values("in_password1","in_password2"))
		error_msg += "Passwords do not match. Please re-enter and confirm password\n";
	
	if(error_msg.length>0)
	{
		alert(error_title+error_msg+error_last);
	}
	else
	{
		submit_form("add_customer");
	}
}


function AddCustomer()
{
	location.href="users.php?action=add";
}

function HighlightRow(row)
{
	row.style.backgroundColor="#cccccc";
}

function UnHighlightRow(row)
{
	row.style.backgroundColor="";
}

function EditPriceBand(id)
{
	location.href="pricing.php?price_band="+id;
}

function EditMetaInfo(id)
{
	location.href="meta.php?page_id="+id;
}

function EditSlideshowInfo(id)
{
	location.href="slideshow.php?image_id="+id;
}


