// Validation rounites for Advansys eCommerce with CMS
function popImage(imageURL,imageTitle){
	openPopup(imageURL,imageTitle);
}

function openPopup (imageURL, caption) {

/*
This routine creates a pop-up window, and ensures that it takes focus. It is 
intended to be called from an anchor tag. The new window will resize itself to 
the optimum size, so we make it as large as the largest required window to
overcome bugs in various manifestations of various browsers.
  
Author:   John Gardner
Written:  8th November 2003
Updated:  27th January 2004

Calling sequence: <a href="a.jpg" onClick="return openLargeImage('a.jpg','Caption');">

The first parameter is the URL of the image to be opened, and the second 
parameter is the caption for the image which is displayed in the window title
and in the alt property of the image tag.

Note that the calling sequence will simply open the image in the main window if
JavaScript isn't enabled.
  
*/

  // Constants - change these to suit your requirements Note that the defaultWidth
  // and defaultHeight variables should be set to more than your largest image to
  // overcome a bug in Mozilla (at least up to Firefox 0.9).

  var windowTop = 50;                // Top position of popup
  var windowLeft = 100                // Left position of popup
  var defaultWidth = 730;             // Default width (for browsers that cannot resize)
  var defaultHeight = 532;            // Default height (for browsers that cannot resize)
  var onLoseFocusExit = true;         // Set if window to exit when it loses focus
  var undefined;

  // Set up the window open options
  var Options = "width=" + defaultWidth + ",height=" + defaultHeight + ",top=" + windowTop + ",left=" + windowLeft + ",scrollbars=yes,resizable"
  
  // Now write the HTML markup to the new window, ensuring that we insert the 
  // parameter URL of the image and the parameter description of the image in 
  // the right place.
  var myScript = "<html>\n" + 
    "<head>\n" + 
	"<title>" + caption + "\</title>\n" +
	"<link href=/css/style.css rel=StyleSheet type=text/css>\n" +     
    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n" +
    "<meta http-equiv=\"Content-Language\" content=\"en-gb\">\n" +
    "<script language=\"JavaScript\" type=\"text/javascript\">\n" +
    "function resizewindow () {\n" +
    "  var width = document.myimage.width;\n" + 
    "  var height = document.myimage.height;\n";
  
  // Netscape  
  if (navigator.appName.indexOf("Netscape") != -1) {  
    myScript = myScript +  "  window.innerHeight = height+30;\n  window.innerWidth = width+20;\n"
  }
   
  // Opera 
  else if (navigator.appName.indexOf("Opera") != -1) {
    myScript = myScript +  "  window.resizeTo (width+20, height+60);\n"
  }
  
  // Microsoft 
  else if (navigator.appName.indexOf("Microsoft") != -1) { 
    //alert(imageURL);
	var img = new Image();
	
	img.src = imageURL;
	//alert(img.src);
	var height = img.height;
	height = img.height;
	//alert(height);
	
	if (height<=500) {
		 myScript = myScript + "  window.resizeTo (width+50, height+120);\n" 
	 } else {
		 myScript = myScript + "  window.resizeTo (width+50, height+120);\n" 
	}
	
  }
  
  // Assume a frig factor for any other browsers
  else {
    myScript = myScript + "  window.resizeTo (width+20, height+30);\n"
  }
      
  myScript = myScript + "}\n" + "window.onload = resizewindow;\n" +
    "</script>\n</head>\n" + "<body ";
    
  // If the window is required to close when it loses focus.
  if (onLoseFocusExit) {myScript = myScript + "onblur=\"self.close()\" ";}
    
  myScript = myScript + "style=\"margin: 5px; padding-right: 5px; padding-bottom: 5px; margin-bottom: 5px;\">\n" +  
	"<p align=center>\n" + 
    "<img src=\"" + imageURL + "\" alt=\"" + caption + "\" title=\"" + caption + "\" name=\"myimage\" style=\"border: solid 1px #3f3f3f\">\n" + 
    "</p></body>\n" +  "</html>\n";
        
  // Diagnostic - uncomment the next line if you wish to see the script generated.   
  //alert (myScript);
  
  // Create the popup window
  var imageWindow = window.open ("","",Options);
  //alert(myScript);
  imageWindow.document.write (myScript)
  imageWindow.document.close ();
  if (window.focus) imageWindow.focus();
  return false;
}//
function CheckSearch(form){
	if (form.search.value.length <=1){alert('Please enter one or more keywords'); return false;}
	return true;
}

function CheckAdvertiserLogin(form){
	if (form.a_email.value=='') {alert('Please enter your email address');form.a_email.focus(); return false;}
	if (!CheckEmail(form.a_email.value)) {alert('Please enter a valid email address');form.a_email.focus(); return false;}
	if (form.a_password.value=='') {alert('Please enter your password'); form.a_password.focus(); return false;}
	return true;
}

function CheckMemberLogin(form){
	if (form.s_email.value=='') {alert('Please enter your email address');form.s_email.focus(); return false;}
	if (!CheckEmail(form.s_email.value)) {alert('Please enter a valid email address');form.s_email.focus(); return false;}
	if (form.s_password.value=='') {alert('Please enter your password'); form.s_password.focus(); return false;}
	return true;
}

function CheckDefaultEmail(form){
	if (form.email.value=='') {alert('Please enter your email address');form.email.focus(); return false;}
	if (!CheckEmail(form.email.value)) {alert('Please enter a valid email address');form.email.focus(); return false;}
	return true;
}

function CheckContactForm(form){
	if (form.name.value=='') {alert('Please fill in your name'); form.name.focus(); return false}
	if (form.email.value=='') {alert('Please fill in your email address'); form.email.focus(); return false}
	if (!CheckEmail(form.email.value)) {alert('Invalid email address'); form.email.focus(); return false}
	if (form.telephone.value=='') {alert('Please fill in your contact telephone number'); form.telephone.focus(); return false}
	if (form.wherefound.value=='') {alert('Please tell us how you found our website'); form.wherefound.focus(); return false}
	if (form.message.value=='') {alert('Please enter your message'); form.message.focus(); return false}
}

function postit(postcode){ //check postcode format is valid
 test = postcode.value; size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
  postcode.value = test; //write back to form field
 if (size < 6 || size > 8){ //Code length rule
  alert(test + " is not a valid postcode - wrong length");
   postcode.focus();
  return false;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   alert(test + " is not a valid postcode - cannot start with a number");
    postcode.focus();
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   alert(test + " is not a valid postcode - alpha character in wrong position");
    postcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
    postcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
    postcode.focus();
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   alert(test + " is not a valid postcode - no space or space in wrong position");
    postcode.focus();
   return false;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   alert(test + " is not a valid postcode - only one space allowed");
    postcode.focus();
   return false;
  }
return true;
}


function CheckCustomerData(form,regform){

	if (form.title.value=='') {alert('Please select a title'); form.title.focus(); return false;}
	if (form.firstname.value=='') {alert('Please enter your first name'); form.firstname.focus(); return false;}
	if (form.lastname.value=='') {alert('Please enter your last name'); form.lastname.focus(); return false;}
	if(form.address1.value=='' && form.address2.value=='') {alert('Please enter your address'); form.address1.focus(); return false;}
	if (form.birthday_month.value=='') {alert('Please enter your month of birth'); form.birthday_month.focus(); return false;}
	if (form.birthday_day.value=='') {alert('Please enter your day of birth'); form.birthday_day.focus(); return false;}
	if (form.birthday_year.value=='') {alert('Please enter your year of birth'); form.birthday_year.focus(); return false;}
	if (form.preffname.value=='') {alert('Please enter your preffered name'); form.preffname.focus(); return false;}
	if (form.HouseNumber.value=='') {alert('Please enter your house number'); form.HouseNumber.focus(); return false;}
	if (form.city.value=='') {alert('Please enter your town or city'); form.city.focus(); return false;}

	if (form.postcode.value=='') {
		alert('Please enter your postcode'); form.postcode.focus(); return false;
	}else if(form.country.value=='GB'){
		if(!postit(form.postcode)){
			document.getElementById("pscodevalidation").value ="0";
			return false;
		}else{
			document.getElementById("pscodevalidation").value ="1";
		}
	}else{
		document.getElementById("pscodevalidation").value ="0";
	}
	
	if (form.country.value=='') {alert('Please select your country of residence'); form.country.focus(); return false;}
	
	if (form.mobile.value=='') 
	{
	  if (form.telephone.value=='') 
	  {
	    alert('Please enter either home phone or mobile phone '); 
	    form.telephone.focus(); 
	    return false;
	  }
	}
	
	if (form.email.value=='') {alert('Please enter your email address');form.email.focus(); return false;}
	if (!CheckEmail(form.email.value)) {alert('Please enter a valid email address');form.email.focus(); return false;}
	if (form.confemail != null){
	if (form.confemail.value=='') {alert('Please confirm your email address');form.confemail.focus(); return false;}
	if (!CheckEmail(form.confemail.value)) {alert('Please enter a valid email address');form.confemail.focus(); return false;}
	if ((form.email.value != form.confemail.value) && (form.email.value!='' && form.confemail.value!='')){alert('Email do not match');form.confemail.focus(); return false;}
	}
	if (form.bcemail){
	if (form.bcemail.value != form.email.value){
		window.open('/EmailChange.asp?e='+form.email.value, 'mywin', 'width=800,height=250, top=150, left=150')
	}
	}
	if (form.password.value=='') {alert('Please enter a password');form.password.focus(); return false;}
	if (form.cpassword != null){
	if (form.cpassword.value=='') {alert('Please confirm password');form.cpassword.focus(); return false;}
	if ((form.password.value != form.cpassword.value) && (form.password.value!='' && form.cpassword.value!='')){alert('The passwords entered do not match');form.cpassword.focus(); return false;}
	}
	if (form.wherefound){
		if (form.wherefound.selectedIndex==0) {alert('Please select where did you hear about us');form.wherefound.focus(); return false;}
			if (form.termsAndConditions != null){
		if (form.termsAndConditions.checked==false) {alert('You have to agree to the sites terms and privacy policy');form.termsAndConditions.focus(); return false;}
		}
	}
	if (regform){
		if (!form.specOffers.checked) {alert('Please consent to receiving our emails, you will not receive spam');form.specOffers.focus(); return false;}
	}
return true;
}

function CheckContactUsData(form) {
	if (form.name.value == '') {alert('Please enter your name');form.name.focus(); return false;}
	if (form.email.value == '') {alert('Please enter your email');form.email.focus(); return false;}
	if (form.country.value == '') {alert('Please enter your country');form.country.focus(); return false;}
	if (form.product.value == '') {alert('Please enter form subject');form.product.focus(); return false;}
	if (form.message.value == '') {alert('Please enter your message');form.message.focus(); return false;}
	return true;
}

function CheckAdvertiserDataM(form){

	if (form.title.value=='') {alert('Please select a title'); form.title.focus(); return false;}
	if (form.firstname.value=='') {alert('Please enter your first name'); form.firstname.focus(); return false;}
	if (form.lastname.value=='') {alert('Please enter your last name'); form.lastname.focus(); return false;}
	if (form.city.value=='') {alert('Please enter your town or city'); form.city.focus(); return false;}
	
	if (form.postcode.value=='') {
		alert('Please enter your postcode'); form.postcode.focus(); return false;
	}else if(form.country.value=='GB'){
		if(!postit(form.postcode)){
			document.getElementById("pscodevalidation").value ="0";
			return false;
		}else{
			document.getElementById("pscodevalidation").value ="1";
		}
	}else{
		document.getElementById("pscodevalidation").value ="0";
	}
	
	if (form.country.value=='') {alert('Please select your country of residence'); form.country.focus(); return false;}
	if (form.telephone.value=='') {alert('Please enter a contact telephone number'); form.telephone.focus(); return false;}
	//if (form.fax.value=='') {alert('Please enter a fax number'); form.fax.focus(); return false;}
	if (form.email.value=='') {alert('Please enter your email address');form.email.focus(); return false;}
	if (form.bcemail.value != form.email.value){
		window.open('/EmailChange.asp?e='+form.email.value, 'mywin', 'width=800,height=250, top=150, left=150')
	}
	if (!CheckEmail(form.email.value)) {alert('Please enter a valid email address');form.email.focus(); return false;}
	if (form.password.value=='') {alert('Please enter a password');form.password.focus(); return false;}
	//if (form.cpassword.value=='') {alert('Please confirm your password');form.cpassword.focus(); return false;}
	//if ((form.password.value != form.cpassword.value) && (form.password.value!='' && form.cpassword.value!='')){alert('Password do not match');form.cpassword.focus(); return false;}
	//if (form.termsAndConditions.checked==false) {alert('You have to agree to the sites terms and privacy policy');form.termsAndConditions.focus(); return false;}
	if (form.advTerms.checked==false) {alert('You have to agree to the sites terms and privacy policy');form.advTerms.focus(); return false;}
	
	return true;
}

function CheckAdvertiserDataM2(form){

	if (form.title.value=='') {alert('Please select a title'); form.title.focus(); return false;}
	if (form.firstname.value=='') {alert('Please enter your first name'); form.firstname.focus(); return false;}
	if (form.lastname.value=='') {alert('Please enter your last name'); form.lastname.focus(); return false;}
	if (form.city.value=='') {alert('Please enter your town or city'); form.city.focus(); return false;}
	
	if (form.postcode.value=='') {
		alert('Please enter your postcode'); form.postcode.focus(); return false;
	}else if(form.country.value=='GB'){
		if(!postit(form.postcode)){
			document.getElementById("pscodevalidation").value ="0";
			return false;
		}else{
			document.getElementById("pscodevalidation").value ="1";
		}
	}else{
		document.getElementById("pscodevalidation").value ="0";
	}

	if (form.HouseNumber.value == '') { alert('Please enter your house number'); form.HouseNumber.focus(); return false; }
	if(form.address1.value=='' && form.address2.value=='') {alert('Please enter your address'); form.address1.focus(); return false;}
	if (form.country.value=='') {alert('Please select your country of residence'); form.country.focus(); return false;}
	if (form.telephone.value=='') {alert('Please enter a contact telephone number'); form.telephone.focus(); return false;}
	//if (form.fax.value=='') {alert('Please enter a fax number'); form.fax.focus(); return false;}
	if (form.email.value=='') {alert('Please enter your email address');form.email.focus(); return false;}
	if (!CheckEmail(form.email.value)) {alert('Please enter a valid email address');form.email.focus(); return false;}
	if (form.password.value=='') {alert('Please enter a password');form.password.focus(); return false;}
	if (form.cpassword.value=='') {alert('Please confirm your password');form.cpassword.focus(); return false;}
	if ((form.password.value != form.cpassword.value) && (form.password.value!='' && form.cpassword.value!='')){alert('Password do not match');form.cpassword.focus(); return false;}
	if (form.wherefound.selectedIndex==0) {alert('Please select where did you hear about us');form.wherefound.focus(); return false;}
	if (form.termsAndConditions.checked==false) {alert('You have to agree to the sites terms and privacy policy');form.termsAndConditions.focus(); return false;}
	if (form.specOffers.checked==false) {alert('Please consent to receiving our emails, you will not receive spam');form.specOffers.focus(); return false;}
	
	return true;
}

function CheckAdvertiserData(form){
	if (form.title.value=='') {alert('Please select a title'); form.title.focus(); return false;}
	if (form.firstname.value=='') {alert('Please enter your first name'); form.firstname.focus(); return false;}
	if (form.lastname.value=='') {alert('Please enter your last name'); form.lastname.focus(); return false;}
	if (form.city.value=='') {alert('Please enter your town or city'); form.city.focus(); return false;}
	if (form.postcode.value=='') {alert('Please enter your postcode'); form.postcode.focus(); return false;}
	if (form.country.value=='') {alert('Please select your country of residence'); form.country.focus(); return false;}
	if (form.telephone.value=='') {alert('Please enter a contact telephone number'); form.telephone.focus(); return false;}
	//if (form.fax.value=='') {alert('Please enter a fax number'); form.fax.focus(); return false;}
	if (form.Btype.value=='') {alert('Please enter a your Business type'); form.Btype.focus(); return false;}
	if (form.email.value=='') {alert('Please enter your email address');form.email.focus(); return false;}
	if (!CheckEmail(form.email.value)) {alert('Please enter a valid email address');form.email.focus(); return false;}
	if (form.password.value=='') {alert('Please enter a password');form.password.focus(); return false;}
	if (form.cpassword.value=='') {alert('Please confirm your password');form.cpassword.focus(); return false;}
	if ((form.password.value != form.cpassword.value) && (form.password.value!='' && form.cpassword.value!='')){alert('Password do not match');form.cpassword.focus(); return false;}
	if (form.termsAndConditions.checked==false) {alert('You have to agree to the sites terms and privacy policy');form.termsAndConditions.focus(); return false;}
	return true;
}

function CheckCardData(form, gateway){

		if (form.houseno.value=="") {alert('Please enter your House no/ Name'); form.houseno.focus(); return false;}
		if (form.address1.value=="") {alert('Please enter your Address 1'); form.address1.focus(); return false;}

		if (form.City.value=="") {alert('Please enter your town or city'); form.City.focus(); return false;}
		if (form.PostCode.value=="") {alert('Please enter your postcode'); form.PostCode.focus(); return false;}	
		if (form.Email.value=="") {alert('Please enter your email address');form.Email.focus(); return false;}
		if (!CheckEmail(form.Email.value)) {alert('Please enter a valid email address');form.Email.focus(); return false;}
		if (form.PhoneNumber.value=="") {alert('Please enter a contact telephone number'); form.PhoneNumber.focus(); return false;}	
		if (form.CardType.value==""){alert('Please select the Card Type');form.CardType.focus();  return false;}
		if (form.CardNumber.value==""){alert('Please specify the Card Number');form.CardNumber.focus();  return false;}
		if (form.CardName.value==""){alert('Please specify the Card Name'); form.CardName.focus(); return false;}
		if (form.MCExpiryDateMM.value==""){alert('Please specify the Expiry Date');form.MCExpiryDateMM.focus();  return false;}
		if (form.MCExpiryDateYY.value==""){alert('Please specify the Expiry Date'); form.MCExpiryDateYY.focus(); return false;}
		if (form.CV2.value==""){alert('Please specify the CVV (last 3 digits on the reverse of you card)');form.CV2.focus();  return false;}
		return true;
}

	function crlinputs(){

	thisForm = document.getElementById('frmPayment');
		thisForm.houseno.value = "";
		thisForm.address1.value = "";
		thisForm.address2.value = "";
		thisform.City.value = "";
		thisform.State.value = "";
		thisForm.country.selectedIndex = 0;
		thisform.PostCode.value = "";
		thisform.Email.value = "";
		thisform.PhoneNumber.value = "";
		thisform.CV2.value = "";
		thisform.IssueNumber.value = "";
		thisform.ExpiryDateYY.selectedIndex = 0;
		thisform.ExpiryDateMM.selectedIndex = 0;
		thisform.StartDateYY.selectedIndex = 0;
		thisform.StartDateMM.selectedIndex = 0;
		thisform.CardNumber.value = "";
		thisform.CardName.value = "";
		thisform.CardType.selectedIndex = 0;
	}

function CheckDeliveryData(form){
	if (form.delname.value==''){alert('Please specify the Delivery Name'); return false;}
	if (form.deladdress1.value==''){alert('Please specify the Delivery Address'); return false;}
	if (form.delcity.value==''){alert('Please specify the Delivery City'); return false;}
	if (form.delcountrycode.value==''){alert('Please specify the Delivery Country'); return false;}
	return true;
}

function CheckOrder(form){
	if (!form.termsagreed.checked){ alert('You must agree to the terms and conditions before placing an order'); return false;}
	return true;
}
	
function CheckEmail(address) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(address)){
	return (true)
	}
	return (false)
}


function CheckIntegerNow(e){
    IsValidate(e,'0123456789',false);
}

function IsValidate(e,pattern,bspacebar){
 var keynum;
 if(window.event){
  keynum = e.keyCode;
 }else{
  if(e.which){
   keynum = e.which;
  }
 }
 var key=String.fromCharCode(keynum);
 if (keynum == 13){
  e.returnValue=keynum;
  return true;
 }else if (keynum == 32){
     if (bspacebar == false){
      if(e.stopPropagation){
       e.stopPropagation();
      }
      e.cancelBubble=true;
      if(e.preventDefault){
       e.preventDefault();
      }
      e.returnValue=false;
      return false;
  }else{
            e.returnValue=keynum;
   return true;
  }
    }else if((e.keyCode > 7 && e.keyCode < 10) || (e.keyCode > 27 && e.keyCode < 30) || (e.keyCode == 46) || e.keyCode > 47 && e.keyCode < 58) {
  window.status = '';
  return true;
 }else{
  if(pattern.indexOf(key)== -1){
   if(e.stopPropagation){
    e.stopPropagation();
   }
   e.cancelBubble=true;
   if(e.preventDefault){
    e.preventDefault();
   }
   e.returnValue=false;
   return false;
  }else{
   e.returnValue=keynum;
   return true;
  }
 }
}
var oldcolorid="depdesc1"
function changecolor(colorid){
	document.getElementById(oldcolorid).className="depdesc";
	document.getElementById(colorid).className="depdesc_sel";
	oldcolorid=colorid;
}

function checkInvitationEmails(form, counter) {
	if (counter > 1) {
		var i;
		for (i in form.email) {
			if (form.email[i].value != "") {
				if (!CheckEmail(form.email[i].value) && (checkinteger(i))) {
					alert('Please enter a valid email address: ' +i);
					form.email[i].focus();
					return false;
				}
			}
		}
	} else if (counter == 1) {
		if (form.email.value != "") {
			if (!CheckEmail(form.email.value)) {
				alert('Please enter a valid email address');
				form.email.focus();
				return false;
			}
		}
	} else {
		return false;
	}
	return true;
}

function checkRewardBankOrder(form) {
	if (form.rewBankAccount.value=="") {alert('You have to enter your bank account number');form.rewBankAccount.focus(); return false;}
	if (form.rewSortCode.value=="") {alert('You have to enter sort code');form.rewSortCode.focus(); return false;}
	if (form.rewBankName.value=="") {alert('You have to enter your bank name');form.rewBankName.focus(); return false;}
	if (form.termsAndConditions.checked==false) {alert('You have to agree to the sites terms and privacy policy');form.termsAndConditions.focus(); return false;}
	return true;
}

function checkRewardDelivery(form) {
	if (form.deladdress1.value=="" && form.deladdress2.value=="") {alert('You have to enter your delivery Address 1');form.deladdress1.focus(); return false;}

	
	if (form.delcity.value=="") {alert('You have to enter your delivery city');form.delcity.focus(); return false;}
	
	if (form.delcounty.value=="") {alert('You have to enter your delivery county');form.delcounty.focus(); return false;}
	
	if (form.delpostcode.value=="") {alert('You have to enter your delivery post code');form.delpostcode.focus(); return false;}
	
	if (form.delcountrycode.value=="") {alert('You have to enter your delivery country code');form.delcountrycode.focus(); return false;}
	
	if (form.termsAndConditions.checked==false) {alert('You have to agree to the sites terms and privacy policy');form.termsAndConditions.focus(); return false;}
	return true;
}

function checkContent(form){
  if(form.name.value == ''){alert('Please enter name'); form.name.focus(); return false;}
  if(form.memberID.value == ''){alert('Please enter member ID'); form.memberID.focus(); return false;}
  if(form.email.value == ''){alert('Please enter email'); form.email.focus(); return false;}
  if(form.comments.value == ''){alert('Please enter comments'); form.comments.focus(); return false;}
  return true;
}
