// 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 popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=800,left = 100,top = 102');");
}

function textover() { 
 var g,b,k,f,args=textover.arguments;a=parseInt(args[0]);if(isNaN(a))a=0;
 if(!document.p7setc){p7c=new Array();document.p7setc=true;for(var u=0;u<10;u++){
 p7c[u]=new Array();}}for(k=0;k<p7c[a].length;k++){if((g=MM_findObj(p7c[a][k]))!=null){
 b=(document.layers)?g:g.style;b.visibility="hidden";}}for(k=1;k<args.length;k++){
 if((g=MM_findObj(args[k]))!=null){b=(document.layers)?g:g.style;b.visibility="visible";f=false;
 for(var j=0;j<p7c[a].length;j++){if(args[k]==p7c[a][j]) {f=true;}}
 if(!f){p7c[a][p7c[a].length++]=args[k];}}}
}

function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/* NLP commented the function below as it was causing problems with the editable pages, 
removed the return null; at the bottom of the function 
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;

initEditable();
}*/

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";
				}
			}
		}
	}

initEditable();
}

function initEditable(){
	if(document.getElementById("editorwindow")){
		document.getElementById("editorwindow").contentWindow.document.designMode="On";
	}
}
function format($style){
	if($style!=="left" && $style!=="right" && $style!=="centre"){
		document.getElementById("editorwindow").contentWindow.document.execCommand($style, null, false);
	} else {
		$dir=justify+$style;
		document.getElementById("editorwindow").contentWindow.document.execCommand($dir, null, false);
	}
}
function clear(){
		document.getElementById("editorwindow").contentWindow.document.execCommand('removeformat', null, false);
}

function submitpageform(){
	document.getElementById("pagedata").value=document.getElementById("editorwindow").contentWindow.document.body.innerHTML;
	document.getElementById("newname").value=document.getElementById("oldname").value;
	document.getElementById("updatepageform").submit();
}
function deletepageform(){
	if(confirm("Are you sure you want to delete this page?\n\nYou will NOT be able to undo this action.")){
		document.getElementById("action").value="delete";
		document.getElementById("updatepageform").submit();
	}
}

function submitsearchform(){
	//document.getElementById("pagedata").value=document.getElementById("editorwindow").contentWindow.document.body.innerHTML;
	//document.getElementById("newname").value=document.getElementById("oldname").value;
	//document.getElementById("updatepageform").submit();
	var length = document.getElementById("shopsearchinput").value.length
	if(length<5){
		alert("Please enter at least 5 characters as your search information.");
		//return false;
	} else {
	//alert(length);
	submit_form("searchform");
	}
}


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+130;
		//document.getElementById("editor").style.top=document.getElementById(theDiv).offsetTop+119;
		document.getElementById("editor").style.top=document.getElementById(theDiv).offsetTop+240;
		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);

}


/*NLP Copied from LG for validating the registration of trade users*/
function swap_menu_img(dom_obj_id,image_url)	//swap an image's src url
{
	if(document.getElementById)
		the_obj = document.getElementById(dom_obj_id);
	else
	if(document[dom_obj_id])
		the_obj = document[dom_obj_id];
	
	if(the_obj)
		the_obj.src = image_url;
}


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;
}


function EditHomepageAdvert(id)
{
	location.href="homepage.php?advert_id="+id;
}

function EditPrivacyInfo(id)
{

	location.href="privacy.php?page_id="+id;
}

function EditTermsInfo(id)
{

	location.href="terms.php?page_id="+id;
}

function EditFAQInfo(id)
{

	location.href="faqs.php?page_id="+id;
}

function EditDownloadableDocument(id)
{

	location.href="documents.php?document_id="+id;
}


function EditShippingInfo(id)
{

	location.href="shipping.php?page_id="+id;
}



function EditImageAnnotationInfo(id,fileis,position,tableposition)
{
	//ImageMap/image.php?&file=../../productimages/{$image_details->pic1}.jpg&amp;loc={$loc}&amp;table={$table}
	//location.href="ImageMap/image.php?&file=images/brain.jpg";
	//alert(file);
	var somestring;
	somestring="ImageMap/image.php?id="+id+"&file="+fileis+".jpg&loc="+position+"&table="+tableposition;
	location.href=somestring;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function P7_autoLayers() { //v1.4 by PVII

 var g,b,k,f,args=P7_autoLayers.arguments;a=parseInt(args[0]);if(isNaN(a))a=0;
 if(!document.p7setc){p7c=new Array();document.p7setc=true;for(var u=0;u<10;u++){
 p7c[u]=new Array();}}for(k=0;k<p7c[a].length;k++){if((g=MM_findObj(p7c[a][k]))!=null){
 b=(document.layers)?g:g.style;b.visibility="hidden";}}for(k=1;k<args.length;k++){
 if((g=MM_findObj(args[k]))!=null){b=(document.layers)?g:g.style;b.visibility="visible";f=false;
 for(var j=0;j<p7c[a].length;j++){if(args[k]==p7c[a][j]) {f=true;}}
 if(!f){p7c[a][p7c[a].length++]=args[k];}}}
}



