/*===============*/
/* general_js.js */
/*------------------------------*/
/* General JavaScript Functions */
/* V1.1 21 June 2004            */
/*==============================*/

function CopyBillingAddressToDeliveryAddress(formName) {
 document.forms[formName].txtDeliveryAddressLine1.value = document.forms[formName].txtBillingAddressLine1.value;
 document.forms[formName].txtDeliveryAddressLine2.value = document.forms[formName].txtBillingAddressLine2.value;
 document.forms[formName].txtDeliveryAddressLine3.value = document.forms[formName].txtBillingAddressLine3.value;
 document.forms[formName].txtDeliveryTown.value = document.forms[formName].txtBillingTown.value;
 document.forms[formName].txtDeliveryCounty.value = document.forms[formName].txtBillingCounty.value;
 document.forms[formName].txtDeliveryPostcode.value = document.forms[formName].txtBillingPostcode.value;
}


function RobsReplace(sStr, sStrToFilterOut) {
    var s = '';
    for (var i = 0; i < sStr.length; i++) {
        if (sStr.charAt(i) != sStrToFilterOut) s += sStr.charAt(i);
    } return s;
}


function ClrForm() {
	document.deliveryaddress.txtDeliveryAddressLine1.value='';
	document.deliveryaddress.txtDeliveryAddressLine2.value='';
	document.deliveryaddress.txtDeliveryAddressLine3.value='';
	document.deliveryaddress.txtDeliveryTown.value='';
	document.deliveryaddress.txtDeliveryCounty.value='';
	document.deliveryaddress.txtDeliveryPostcode.value='';
}


function ChangePostcode(obj) {
 if (obj.value.length<5)
  {
   alert("Invalid Postcode. Please enter postcode correctly, i.e. AB12 3CD")
  } 
 else
  {
   if (obj.value.indexOf(' ')==-1)
    {
     var postcodeset1=obj.value
     var postcodeset2=obj.value
     postcodeset1=obj.value.substring(0,obj.value.length-3);
     postcodeset2=obj.value.substring(obj.value.length-3,99);
     temppostcodestr=postcodeset1+" "+postcodeset2;
     obj.value=temppostcodestr.toUpperCase()
    }
   else
    {
     obj.value=obj.value.toUpperCase()
    }  
  }
}


function ToUpperCase(obj)
 { obj.value=obj.value.toUpperCase() }
 

function ToProperCase(obj)
{ var first, the_rest; first = obj.value.charAt(0); the_rest = obj.value.substring(1); obj.value = first.toUpperCase() + the_rest.toLowerCase(); } 


function ToCapitalAfterSpace(obj)
{
 obj.value = obj.value.toLowerCase()
 obj.value = obj.value.substr(0,1).toUpperCase() + obj.value.substr(1,obj.value.length-1)
 for (i=0; i<obj.value.length; i++) 
  {
   if (obj.value.substr(i,1)==" ") 
    { obj.value = obj.value.substr(0,i+1) + obj.value.substr(i+1,1).toUpperCase() + obj.value.substr(i+2,obj.value.length-i) }
  }
 obj.value = RobsReplace(obj.value, '/'); 
}


function ToCapitalAfterSpaceOrHyphen(obj)
{
 obj.value = obj.value.toLowerCase()
 obj.value = obj.value.substr(0,1).toUpperCase() + obj.value.substr(1,obj.value.length-1)
 for (i=0; i<obj.value.length; i++) 
  {
   if (obj.value.substr(i,1)==" "||obj.value.substr(i,1)=="-") 
    { obj.value = obj.value.substr(0,i+1) + obj.value.substr(i+1,1).toUpperCase() + obj.value.substr(i+2,obj.value.length-i) }
  }
 obj.value = RobsReplace(obj.value, '/'); 
}


function isDigit(c) { return ((c >= '0') && (c <= '9')) }


function isValidXMLChar(c) {
  validxml=true;
  testchar = (c < '0') || (c > '9') || (c < 'a') || (c > 'z') || (c < 'A') || (c > 'Z');
  if (testchar==true)
   {
    if ((c == '!') || (c == '/') || (c == '\\') || (c == '^') || (c == '%') || (c == '$') || (c == '&') || (c == '*') || (c == '{') || (c == '}') || (c == '[') || (c == ']') || (c == '+') || (c == '=') || (c == ';') || (c == ':') || (c == '~') || (c == '`') || (c == '#') || (c == '|') || (c == '?') || (c == '<') || (c == '>'))
     { validxml=false; }
   }
 return validxml;  
}


function ValidateTelephoneNumber(obj) {
 for (i=0; i<obj.value.length; i++) 
  {
   if (obj.value.substr(i,1)!=" "&&isDigit(obj.value.substr(i,1))!=true) 
    { alert("Telephone Number must consist of digits 0 through 9, and spaces only."); }
  }
}


function ValidateTelephoneNumber2(obj, type) {
 retvalue=0;
 for (i=0; i<obj.value.length; i++) 
  {
   if(!type) { type='Telephone'; }
   if (obj.value.substr(i,1)!=" "&&isDigit(obj.value.substr(i,1))!=true) 
    { 
     retvalue = 1;
     alert(type+" Number must consist of digits 0 through 9, and spaces only."); 
     break;
    }
   if(type=='Home') 
    {
     if (obj.value.substring(0,2) == '07')
	  {
	   retvalue = 1;
	   alert("Please do not enter Mobile number as Home Telephone Number.");
	   break;
	  }
    } 
  }
 return retvalue;
}


function ValidateEmailAddress(obj) {
 if (obj.value.indexOf('@')==-1) 
  { alert("Invalid Email Address entered."); }
}


function ValidateStandardFields(obj) {
 ValidStandardField=true;
 for (i=0; i<obj.value.length; i++) 
  {
   if (obj.value.substr(i,1)!=" "&&isValidXMLChar(obj.value.substr(i,1))!=true) 
    { ValidStandardField = false; }
  }
 return ValidStandardField; 
}


function submitForm(theForm,theSubmitButton)
{
 SubmitBtn_PleaseWait(theSubmitButton,'Please wait..');
 test_personal = Validator_PersonalDetails(theForm);
 test_billing = Validator_BillingAddress(theForm);
 test_delivery = Validator_DeliveryAddress(theForm);
 if (test_personal==true && test_billing==true && test_delivery==true)
 	{ theForm.submit(); }
 else
    { SubmitBtn_Reset(theSubmitButton,'Next >'); }	
}


function submitForm2(theForm,theSubmitButton,value,DisallowMobileNumberAsHomeNumber,ValidatePaymentDetails)
{
 SubmitBtn_PleaseWait(theSubmitButton,'Please wait..');
 test_personal = Validator_PersonalDetails(theForm, DisallowMobileNumberAsHomeNumber);
 test_billing = Validator_BillingAddress(theForm);
 test_delivery = Validator_DeliveryAddress(theForm);
 if (ValidatePaymentDetails!=null){
 //_PAYMENT_DETAILS_VALIDATION
 test_payment = Validator_PaymentDetails(theForm);
 //END
 }
 else{
   test_payment=true;
 }
 if (test_personal==true && test_billing==true && test_delivery==true && test_payment==true)
 	{ 
		theForm.submit();
		return true;
	}
 else
    { 
			SubmitBtn_Reset(theSubmitButton,value); 
			return false;
		}	
}

function Validator_PaymentDetails(theForm) {
	errorflag = false;
	errormsg = "The following details were not supplied or were incorrect.\n";
	errormsg += "Please enter: \n\n";
  
  //card type
  if(theForm.cardType.value.length == 0){
    errorflag = true; 
    errormsg += "Card Type not entered.\n";
  }
  //if cardtype is switch or maestro
  if(theForm.cardType.value == "Switch" || theForm.cardType.value == "Maestro"){
    //check for issue number
    if(theForm.txtIssue.value.length > 3){
      errorflag = true;  
      errormsg += "Invalid Issue number.\n";
    }
  }
  //card holder name
  if(theForm.txtCardName.value.length == 0){
    errorflag = true; 
    errormsg += "Card Holder Name not entered.\n";
  }
  //card number
  if(theForm.txtCardNumber.value.length < 5){
    errorflag = true; 
    errormsg += "Card Number Invalid.\n";
  }
  //security code
  if(theForm.txtCV2.value.length < 3){
    errorflag = true; 
    errormsg += "Card Security Code.\n";
  }
  //exp date: month && year
  if(isNaN(theForm.exmonth.value) || (theForm.exmonth.value.length != 2)){
    errorflag = true; 
    errormsg += "Card Expiry Month.\n";
  }
  if(isNaN(theForm.exyear.value) || (theForm.exyear.value.length != 2)){
    errorflag = true; 
    errormsg += "Card Expiry Year.\n";
  }
	if (errorflag){ 
    alert(errormsg); 
    return false; 
  }
	else{ 
    return true; 
  }
}

function Validator_PersonalDetails(theForm, DisallowMobileNumberAsHomeNumber)
	{
	errorflag = false;
	errormsg = "The following details were not supplied or were incorrect.\n";
	errormsg = errormsg + "Please enter: \n\n";
	if (theForm.txtTitle)
	 {
		if (theForm.txtTitle.value.length == 0) 
	 		{ errorflag = true; errormsg = errormsg + "Title not entered.\n"; }
	 }
	if (theForm.txtForenames) 
	 {
	 	if (theForm.txtForenames.value.length == 0) 
	 		{ errorflag = true; errormsg = errormsg + "Forename(s) not entered.\n"; }
	 }
	if (theForm.txtSurname) 
	 { 
		if (theForm.txtSurname.value.length == 0) 
		 { errorflag = true; errormsg = errormsg + "Surname not entered.\n"; }
	 }
	if (theForm.txtTelephoneHome) 
	 {
		if (theForm.txtTelephoneHome.value.length == 0) 
	 	 { errorflag = true; errormsg = errormsg + "Home Telephone Number not entered.\n"; }
	 	if (DisallowMobileNumberAsHomeNumber==true)
	 	 {
	      if (ValidateTelephoneNumber2(theForm.txtTelephoneHome, 'Home')==1) { errorflag = true; errormsg = errormsg + "Mobile number is used as a home number. Please use a non mobile number\n"; }
	     }
	 }
	/*if (theForm.txtTelephoneWork) 
	 {
		if (theForm.txtTelephoneWork.value.length == 0) 
		 { errorflag = true; errormsg = errormsg + "Work Telephone Number not entered.\n"; } 
	 }*/
	if (theForm.txtEmailAddress) 
	 {
		if (theForm.txtEmailAddress.value.indexOf('@') == -1)
		 { errorflag = true; errormsg = errormsg + "Email Address invalid.\n"; }
	 }
	if (theForm.txtEmailAddress) 
	 {
		if (theForm.txtEmailAddress.value.length == 0) 
		 { errorflag = true; errormsg = errormsg + "Email Address not entered.\n"; }
	 }
	if (theForm.txtEmailAddress_reenter) 
	 {
		if (theForm.txtEmailAddress_reenter.value.length == 0) 
		 { errorflag = true; errormsg = errormsg + "Email Address (Re-enter) not entered.\n"; }
	 } 
	if (theForm.txtPassword) 
	 {
		if (theForm.txtPassword.value.length < 4||theForm.txtPassword.value.length > 12) 
		 { errorflag = true; errormsg = errormsg + "Please enter a password of between 4 and 12 characters.\n";	}
	 }
	if (theForm.txtPassword_reenter) 
	 {
		if (theForm.txtPassword_reenter.value.length < 4||theForm.txtPassword_reenter.value.length > 12) 
		 { errorflag = true; errormsg = errormsg + "Please enter a password of between 4 and 12 characters.\n";	}
	 } 
	if (theForm.txtPassword_reenter) 
	 {
		if (theForm.txtPassword.value != theForm.txtPassword_reenter.value) 
		 { errorflag = true; errormsg = errormsg + "Passwords do not match.\n";	}
	 } 
	if (theForm.txtEmailAddress_reenter) 
	 {
	 	if (theForm.txtEmailAddress.value != theForm.txtEmailAddress_reenter.value) 
		 { errorflag = true; errormsg = errormsg + "Email Addresses do not match.\n";	}
     } 
	 validatestandardfieldsflag=false;
	 validatestandardfieldsproblemfields="";
	 if (theForm.txtTitle) 
	 {
	 	if (ValidateStandardFields(theForm.txtTitle)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Title, "; }
	 }
	 if (theForm.txtForenames) 
	 {
	 	if (ValidateStandardFields(theForm.txtForenames)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Forename(s), "; }
	 }
	 if (theForm.txtSurname) 
	 {
	 	if (ValidateStandardFields(theForm.txtSurname)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Surname, "; }
	 }
	 if (theForm.txtTelephoneWork) 
	 {
	 	if (ValidateStandardFields(theForm.txtTelephoneWork)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Work Telephone, "; }
	 }
	 if (theForm.txtTelephoneHome) 
	 {
	 	if (ValidateStandardFields(theForm.txtTelephoneHome)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Home Telephone, "; }
	 }
	 if (theForm.txtTelephoneMobile) 
	 {
	 	if (ValidateStandardFields(theForm.txtTelephoneMobile)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Mobile Telephone, "; }
	 }
	 if (theForm.txtTelephoneFax) 
	 {
	 	if (ValidateStandardFields(theForm.txtTelephoneFax)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Fax Telephone, "; }
	 }
	 if (theForm.txtEmailAddress) 
	 {
	 	if (ValidateStandardFields(theForm.txtEmailAddress)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Email Address, "; }
	 }
	 if (theForm.txtEmailAddress_reenter) 
	 {
	 	if (ValidateStandardFields(theForm.txtEmailAddress_reenter)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Email (re-entry) Address, "; }
	 }
	 if (theForm.txtPassword) 
	 {
		 if (ValidateStandardFields(theForm.txtPassword)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Password, "; }
	 }
	 if (theForm.txtPassword_reenter) 
	 {
		 if (ValidateStandardFields(theForm.txtPassword_reenter)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Password (re-entry), "; }
	 }
     if (validatestandardfieldsflag==true)
      { errorflag = true; errormsg = errormsg + "Sorry but you must not use any exclamation marks, ampersands, slashes, or other such keyboard characters.\r\nPlease check you have entered information using standard characters i.e. a-z,minus,comma and/or brackets.\r\nFields to alter: - "+validatestandardfieldsproblemfields; } 
	if (errorflag)
	  	{ alert(errormsg); return false; }
	else
		{ return true; }
}


function Validator_BillingAddress(theForm)
	{
	errorflag = false;
	errormsg = "The following details were not supplied or were incorrect.\n";
	errormsg = errormsg + "Please enter: \n\n";

	if (theForm.txtBillingAddressLine1) 
	 {
		if (theForm.txtBillingAddressLine1.value.length == 0) 
		 {
		  errorflag = true;
		  errormsg = errormsg + "Billing Address Line 1 not entered.\n";
		 }
	 }
	if (theForm.txtBillingTown) 
	 {
		if (theForm.txtBillingTown.value.length == 0) 
		 {
		  errorflag = true;
		  errormsg = errormsg + "Billing Address Town not entered.\n";
		 }
	 }
	if (theForm.txtBillingPostcode) 
	 { 
		if (theForm.txtBillingPostcode.value.length == 0) 
		 {
		  errorflag = true;
		  errormsg = errormsg + "Billing Address Postcode not entered.\n";
		 }
	 }

	 validatestandardfieldsflag=false;
	 validatestandardfieldsproblemfields="";
	 if (theForm.txtBillingAddressLine1) 
	 {
		 if (ValidateStandardFields(theForm.txtBillingAddressLine1)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Billing Address Line 1, "; }
	 }
	 if (theForm.txtBillingAddressLine2) 
	 {
		 if (ValidateStandardFields(theForm.txtBillingAddressLine2)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Billing Address Line 2, "; }
	 }
	 if (theForm.txtBillingAddressLine3) 
	 {
		 if (ValidateStandardFields(theForm.txtBillingAddressLine3)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Billing Address Line 3, "; }
	 }
	 if (theForm.txtBillingTown) 
	 {
		 if (ValidateStandardFields(theForm.txtBillingTown)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Billing Address Town/City, "; }
	 }
	 if (theForm.txtBillingCounty) 
	 {
		 if (ValidateStandardFields(theForm.txtBillingCounty)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Billing Address County, "; }
	 }
	 if (theForm.txtBillingPostcode) 
	 {
		 if (ValidateStandardFields(theForm.txtBillingPostcode)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Billing Address Postcode, "; }
	 }
	 if (validatestandardfieldsflag==true)
      { errorflag = true; errormsg = errormsg + "Sorry but you must not use any exclamation marks, ampersands, slashes, or other such keyboard characters.\r\nPlease check you have entered information using standard characters i.e. a-z,minus,comma and/or brackets.\r\nFields to alter: - "+validatestandardfieldsproblemfields; } 
	if (errorflag)
	  	{ alert(errormsg); return false; }
	else
		{ return true; }
}


function Validator_DeliveryAddress(theForm)
	{
	errorflag = false;
	errormsg = "The following details were not supplied or were incorrect.\n";
	errormsg = errormsg + "Please enter: \n\n";

	if (theForm.txtDeliveryAddressLine1) 
	 {
		if (theForm.txtDeliveryAddressLine1.value.length == 0) 
		 {
		  errorflag = true;
		  errormsg = errormsg + "Delivery Address Line 1 not entered.\n";
		 }
	 }
	if (theForm.txtDeliveryTown) 
	 {
	 	if (theForm.txtDeliveryTown.value.length == 0) 
		 {
		  errorflag = true;
		  errormsg = errormsg + "Delivery Address Town not entered.\n";
		 }
	 }
	if (theForm.txtDeliveryPostcode) 
	 {
		if (theForm.txtDeliveryPostcode.value.length == 0) 
		 {
		  errorflag = true;
		  errormsg = errormsg + "Delivery Address Postcode not entered.\n";
		 }
	 }
	 validatestandardfieldsflag=false;
	 validatestandardfieldsproblemfields="";
	 if (theForm.txtDeliveryAddressLine1) 
	 {
		 if (ValidateStandardFields(theForm.txtDeliveryAddressLine1)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Delivery Address Line 1, "; }
	 }
	 if (theForm.txtDeliveryAddressLine2) 
	 {
		 if (ValidateStandardFields(theForm.txtDeliveryAddressLine2)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Delivery Address Line 2, "; }
	 }
	 if (theForm.txtDeliveryAddressLine3) 
	 {
		 if (ValidateStandardFields(theForm.txtDeliveryAddressLine3)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Delivery Address Line 3, "; }
	 }
	 if (theForm.txtDeliveryTown) 
	 {
		 if (ValidateStandardFields(theForm.txtDeliveryTown)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Delivery Address Town/City, "; }
	 }
	 if (theForm.txtDeliveryCounty) 
	 {
		 if (ValidateStandardFields(theForm.txtDeliveryCounty)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Delivery Address County, "; }
	 }
	 if (theForm.txtDeliveryPostcode) 
	 {
		 if (ValidateStandardFields(theForm.txtDeliveryPostcode)==false) { validatestandardfieldsflag = true; validatestandardfieldsproblemfields = validatestandardfieldsproblemfields+"Delivery Address Postcode, "; }
	 }
	 if (validatestandardfieldsflag==true)
      { errorflag = true; errormsg = errormsg + "Sorry but you must not use any exclamation marks, ampersands, slashes, or other such keyboard characters.\r\nPlease check you have entered information using standard characters i.e. a-z,minus,comma and/or brackets.\r\nFields to alter: - "+validatestandardfieldsproblemfields; } 
	if (errorflag)
	  	{ alert(errormsg); return false; }
	else
		{ return true; }
}


function sel_text()
{ window.event.srcElement.select(); }

function SubmitBtn_PleaseWait(obj,ProcessingText)
{ obj.value=ProcessingText; obj.disabled=true; } 

function SubmitBtn_Reset(obj,ProcessingText)
{ obj.value=ProcessingText; obj.disabled=false; } 

function submitAdvSrchForm(theForm,theSubmitButton)
{
 SubmitBtn_PleaseWait(theSubmitButton,'Please wait..');
 test_advsrch = Validator_AdvancedSearch(theForm);
 if (test_advsrch==true)
 	{ theForm.submit(); }
 else
    { SubmitBtn_Reset(theSubmitButton,'Search'); }	
}


function Validator_AdvancedSearch(theForm)
	{
	errorflag = false;
	errormsg = "Please select from at least one of the Type, Manufacturer or Price drop-down choices to search for something.\n";
	if (theForm.feature.value == ''&&theForm.manufacturer.value == ''&&theForm.pricerangemin.value == ''&&theForm.pricerangemax.value == '') 
	 { errorflag = true; }
	if (errorflag)
	 { alert(errormsg); return false; }
	else
	 { return true; }
}


function ValidateKeywordSearch(theForm) {
 if (theForm.search.value.length == 0) 
  { alert("Please enter keyword(s) or product code first."); }
 else
  { theForm.submit(); } 
}


function UpdateAndSubmit(theForm,theField,theValue) {
 theForm[theField].value = theValue;
 theForm.submit();
}


function PopupThisWindow(location,width,height,left,top) {
 window.open(location, "", "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width="+width+",height="+height+",left="+left+",top="+top);
}


function checkCheckBox(chkCheckbox,msg){
if (chkCheckbox.checked == false )
{
alert(msg);
return false;
}else
return true;
}


function popUp(URL){
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=400,left = 200,top = 100');");
}


