function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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 MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

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();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0 
window.open(theURL,winName,features); 
} 

function highlightBrandLink(bid) {
	oSpan = document.getElementById("brand" + bid);
	oSpan.style.visibility = "visible";
}

function resetBrandLink(bid) {
	oSpan = document.getElementById("brand" + bid);
	oSpan.style.visibility = "hidden";
}

// Refresh the page to display number of items selected
function displayItemsPerPage(sURL, iItemsPage){ 

    var sLocation;

    sLocation = sURL + 'pageItems='+ iItemsPage;
    location.href = sLocation;

}

function HideShowTable(iVal, sTableID)
{
    if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf["MSIE 5"] != -1)
    {    	
		if(iVal == "1"){
			//visible
			document.getElementById(sTableID).style.display="";
		}
		else if(iVal == "0"){
			//invisible
			document.getElementById(sTableID).style.display="none";
		}				
    }
}

function emailValid(sEmail){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(sEmail)) return true;
	else return false;
}

function formcheckCustomer(){

	var str = "";
	var str2 = "";
	var oForm = document.frmCustomer;

	if(oForm.title.value.length < 2) str += " - Title\n";
	if(oForm.forename.value.length < 2) str+=" - Forename\n";
	if(oForm.surname.value.length < 2) str+=" - Surname\n";
	if(oForm.addressHouseNameOrNumber.value.length == 0) str+=" - House name or number\n";
	if(oForm.address1.value.length < 3) str+=" - Road / Street\n";
	if(oForm.address3.value.length < 3) str+=" - Town or City\n";
	if(oForm.postcode.value.length < 4) {
		str+=" - Post Code\n";
	} else {
		var sPostCode = oForm.postcode.value;
		sPostCode = sPostCode.toUpperCase();
		oForm.postcode.value = sPostCode;
		if (! isValidUKPostcode(sPostCode)) {
			str+=" - a valid UK post code, including the middle space\n";
		}
	};
		
	if(oForm.email.value.length < 6)	{
		str+=" - Email address\n";
	}
	else {
		if (! emailValid(oForm.email.value)) {
			str2+=" - Your email address is not valid\n";
		}
	}
	
	if(str != "" || str2 !=""){
		var strError = "";
		if (str != "") strError = "Please enter the following details:    \n----------------------\n" + str + "----------------------\n"
		if (str2 != "") strError += "\nPlease correct the following errors:    \n----------------------\n" + str2 + "----------------------\n"
		alert(strError);
		return false;
	}
	else{
		return true;
	}
}

function formcheckPassword(){
	var str = "";
	var str2 = "";	
	var oForm = document.frmPassword;
	if(oForm.password.value.length < 2) str+=" - Password\n";
	if(oForm.password2.value.length < 2) {
		str+=" - Password confirmation\n";
	}
	else {
		if (oForm.password.value != oForm.password2.value) {
			str2+=" - Your password and re-typed password do not match\n";
		}
		else if (oForm.password.value.length < 5) {
			str2+=" - Your password must be at least 5 characters long\n";
		}		
	}
	if(str != "" || str2 !=""){
		var strError = "";
		if (str != "") strError = "Please enter the following details:    \n----------------------\n" + str + "----------------------\n"
		if (str2 != "") strError += "\nPlease correct the following errors:    \n----------------------\n" + str2 + "----------------------\n"
		alert(strError);
		return false;
	}
	else {
		return true;
	}	
}

function formcheckLogin(){
	var str = "";
	var oForm = document.frmLogin;
	if(oForm.username.value == "")	str+=" - Email address\n";
	if(oForm.password.value == "") str+=" - Password\n";
	
	if(str != ""){
		var strError = "Please enter the following details:    \n----------------------\n" + str + "----------------------\n"
		alert(strError);
		return false;
	}
	else{
		return true;
	}
}

//Advanced Email Check credit-
//By JavaScript Kit (http://www.javascriptkit.com)
//Over 200+ free scripts here!
var testresults2;
function checkemail2(){
	var str=document.frmUser.customer_email.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		testresults2=true
	else{alert("Please input a valid email address");testresults2=false}
	return (testresults2)
}

function checkLogon() {
	if (document.logon.email.value == '' || document.logon.password.value == '') {
		alert('Please enter your email address and password.');
		return false;
	}
	else return true;
}

function checkPassword(){
	var pw=document.frmUser.customer_password.value;
	var pw2=document.frmUser.customer_password2.value;
	if (pw != '' && pw.length < 6) {
		alert("Please enter a password at least 6 characters long");
		return false;
	}
	if (pw != pw2) {
		alert("Your password and confirmation password do not match");
		return false;
	}
	else return true;
}

	function checkForm() {
		var oForm = document.frmUser;
		var msg = '';
		if(oForm.customer_forename.value == ''){msg += '\n- your forename'};
		if(oForm.customer_surname.value == ''){msg += '\n- your surname'};
		if(oForm.customer_HouseNameOrNumber.value == ''){msg += '\n- your house name or number'};
		if(oForm.customer_address1.value == ''){msg += '\n- your street address'};
		if(oForm.customer_address3.value == ''){msg += '\n- your post town'};
		if(oForm.customer_postcode.value == ''){
			msg += '\n- your post code';
		} else {
			var sPostCode = oForm.customer_postcode.value;
			sPostCode = sPostCode.toUpperCase();
			oForm.customer_postcode.value = sPostCode;
			if (! isValidUKPostcode(sPostCode)) {
				msg += '\n- a valid UK post code, including the middle space';
			}
		};
		if(oForm.customer_email.value == ''){msg += '\n- your email address'};

		// check delivery fields if the delivery address is different from the billing address
		if(oForm.customer_usedelivery[0].checked) {
			if(oForm.customer_delvforename.value == ''){msg += '\n- the delivery forename '};
			if(oForm.customer_delvsurname.value == ''){msg += '\n- the delivery surname'};
			if(oForm.customer_delvaddress1.value == ''){msg += '\n- the delivery address'};
			if(oForm.customer_delvaddress3.value == ''){msg += '\n- the delivery post town'};
			if(oForm.customer_delvpostcode.value == ''){
				msg += '\n- the delivery post code';
			} else {
				var sPostCode = oForm.customer_delvpostcode.value;
				sPostCode = sPostCode.toUpperCase();
				oForm.customer_delvpostcode.value = sPostCode;
				if (! isValidUKPostcode(sPostCode)) {
					msg += '\n- a valid UK post code for the delivery address, including the middle space';
				}
			};
		}
		
		if(msg.length == 0)	{
			if (document.layers||document.getElementById||document.all) {
				var bOK = checkemail2();
				if (bOK) return checkPassword();
				else return false;
			}
			else {
				return true;
			}
		}			
		else
		{
			alert('Please enter:\n' + msg);
			return false;
		}
	}		

	// UK Post code validation
	function isValidUKPostcode(sPostCode) {
	  var oRegExp = new RegExp("^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$");
	  if (sPostCode.match(oRegExp)) return true;
	  else return false;
	}
	
	//Credit Card Validation
	function toNumber(sInput)
	{
		var sOutput = "";
		count = 0;
		for (i = 0; i < sInput.length; i++) {
			sChar = sInput.substring(i, i+1);
			if (sChar >= "0" && sChar <= "9"){sOutput += sChar;}
		}
		return sOutput;
	}

	function IsValidCCNumber(sInput)
	{
		//Remove padding spaces
		sInput = toNumber(sInput);
		//Verify length of card number
		if (sInput.length > 18 || sInput.length < 1)
		{ alert("Please enter a valid credit card number.");return (false);}
		//Verify with Mod 10 LUN algorithm
		iSum = 0;
		iMuliplier = 1;
		iLen = sInput.length;
		for (i = 0; i < iLen; i++)
		{
		idigit = sInput.substring(iLen-i-1,iLen-i);
		iCheck = parseInt(idigit ,10)*iMuliplier;
		if (iCheck >= 10){iSum += (iCheck % 10) + 1;}
		else{iSum += iCheck;}
		if (iMuliplier == 1){iMuliplier++;}
		else{iMuliplier--;}
		}
		if ((iSum % 10) == 0){return (true);}
		else{alert("Please enter a valid credit card number.");return (false);}
	}

	function CheckCreditCardFrm(){
		var mssg;
		var oMssg;
		mssg ='';				
		if(document.frmPay.cardname.value==''){mssg = mssg+'\n- Card Name'}
		if(document.frmPay.cardnumber.value==''){mssg = mssg+'\n- Card Number'}
		if (IsValidCCNumber(document.frmPay.cardnumber.value)==false){return false;}
		oMssg = new String(mssg)
		if(oMssg.length > 0){alert('Please supply a value for:\n---------------------------------'+ mssg);return false}
		else{alert("Processing your card may take between 3 and 20 seconds, please be patient and do not submit twice.");return true;}
		return false
	}
