var commentlength=0;
		
// Called onChange in the comment field
function getcomment(thisobject) {
	commentlength=thisobject.value.length;
	return;
}
		
function checkcomment(commentlabel) {
	if (commentlength>255) {
		alert(commentlabel+": The maximum length is 255 characters.");
		return false;
	}
	return true;
}
		
<!--- General hook where all custom client-side validation should be performed --->

var lock_form = false;

function ShopcreatorChecks(obj, commentlabel, mandatoryemail)
{


 if (lock_form != true)
 {
    lock_form = true;
  

   if(typeof(mandatoryemail)=="undefined"){
     mandatoryemail = true;
   }
	//Check comment field length
	if (commentlabel!=null) {
		if(checkcomment(commentlabel) == false) {
			//obj.comment.focus();
			lock_form = false;
			return false;
		}
	}
	if (obj.mail!=null) {
		if(emailCheck(obj.mail.value, false) == false) {
			alert("There was a problem with the email address you have entered, please enter a valid email address.");
			obj.mail.focus();
			lock_form = false;
			return false;
		}
		if(mandatoryemail == true)
		{
		  if(obj.mail.value == '')
		  {
		     alert("You must provide a valid email address to place the order.");
			 obj.mail.focus();
			 lock_form = false;
			 return false;
		  }
		}
	}
	else if (obj.email!= null) {
		if(emailCheck(obj.email.value, false) == false) {
			alert("There was a problem with the email address you have entered, please enter a valid email address.");
			obj.email.focus();
			lock_form = false;
			return false;
		}
		if(mandatoryemail == true)
		{
		  if(obj.email.value == '')
		  {
		     alert("You must provide a valid email address to place the order.");
			 obj.email.focus();
			 lock_form = false;
			 return false;
		  }
		}
		
	}
			
	if (obj.ctype!=null) {
		if (obj.ctype.value=="") {
				alert("Please choose a card type!");
				obj.ctype.focus();
				lock_form = false;
				return false;
		}
	}
	if (obj.cnum!=null) {
		if (obj.cnum.value=="") {
				alert("Please enter a card number!");
				obj.cnum.focus();
				lock_form = false;
				return false;
		}
	}
	if (obj.ctype!=null && obj.ciss!=null) {
/* not mandatory for switch/maestro/solo anymore
		if ((obj.ctype.value=="Switch" || obj.ctype.value=="Solo") && obj.ciss.value=="" && obj.validfrom.value == "") {
			alert("For "+obj.ctype.value+" cards you must enter the Issue Number or Valid From/Start Date (or both)");
			obj.ciss.focus();
			lock_form = false;
			return false;	
		} */
		if (obj.ciss!=null && obj.ciss.value.length > 0) {
			var RegExpver1 = /^[0-9 ]+$/;
			if (obj.ciss.value.length > 2 || !RegExpver1.test(obj.ciss.value)){
				alert("Invalid Issue Number. Issue numbers should be numeric and no more than two digits. If your card does not have a switch number then please leave this field blank.");
				obj.ciss.focus();
				lock_form = false;
				return false;			
			}	
		}
						
	}
	if (obj.cardExpMonth!=null && obj.cardExpYear!=null ) {
		if (obj.cardExpMonth.value=="" || obj.cardExpYear.value=="") {
			alert("Please enter an expiry date!");
			obj.cardExpMonth.focus();
			lock_form = false;
			return false;
		}
	}
	if(obj.validfrom!=null)
	{
		var RegExpver1 = /^([0][1-9]|[1][0-2])\/([0-9][0-9])$/; 
		if (obj.validfrom.value.length > 0 && !RegExpver1.test(obj.validfrom.value))
		{
				alert("Invalid card Valid From date. This should either be blank, or in the format MM/YY");
				obj.validfrom.focus();
				lock_form = false;
				return false;			
		}	
	}
	if(obj.expiry!=null)
	{
		var RegExpver1 = /^([0][1-9]|[1][0-2])\/([0-9][0-9])$/; 
		if (!RegExpver1.test(obj.expiry.value))
		{
				alert("Invalid card Expiry date. This should be in the format MM/YY");
				obj.expiry.focus();
				lock_form = false;
				return false;			
		}	
	}	
	
	
	if (obj.MobileNumber) {
		if (obj.MobileNumber.value=="") {
				alert("Please enter your Mobile Phone Number!");
				obj.MobileNumber.focus();
				lock_form = false;
				return false;	
				
		}
	}
	
	  // new locking functionality to have a stab at ensuring someone can't submit twice by double clicking
	  // not foolproof so need to stick something 
	
	/* dont think we need this now, the lock_form stuff should make this redundo. - tricky. 
	 for (i = 0; i < obj.length; i++)
	 {
	 	if(obj.elements[i].type == "submit")
		{
		  obj.elements[i].disabled = true;
		}
		  
	 }	
	 */
	// Implementation of a function-hook to allow specific sites to include their own bit of custom validation
	// via the checkout wrapper. It passes the form object to a function called customChecks() if it exists.
	// NOTE: THIS MUST ALWAYS BE THE LAST CHECK IN THE SHOPCREATOR CHECKS FUNCTION.
			
	//modified so that it actually unlocks the form to let you correct your mistakes ;) - NickJ
	 if (typeof(customChecks) != "undefined") {
		custom_result = customChecks(obj);
		if (!custom_result) {
			lock_form = false;
		}
		return custom_result;
	} else {
		return true;
	}	 
	 
 }
 else
 {
 	alert("You have clicked the submission button once already; we have blocked you from clicking it again as this could result in the details being processed twice");
	return false;
 }
}

function CopyDetails(obj)
{
	var failed=false;
	var myCountryIndex=obj.adr4.selectedIndex;
	if (obj.ddr4!=null && obj.adr4!=null) {
		if (obj.ddr4.type=="select-one") {
			var found=false;
			for (var i=0; (!found && i<obj.ddr4.length); i++) {
				found=(obj.adr4.options[myCountryIndex].value == obj.ddr4.options[i].value);
			}
			if (found) {
				obj.ddr4.selectedIndex=i-1;
			} else {
				var i = obj.adr4.options[myCountryIndex].value.length;
				while (i>0 && obj.adr4.options[myCountryIndex].value.charAt(i-1)==' ') i--;
				alert("Cannot deliver to "+obj.adr4.options[myCountryIndex].value.substring(0, i)+".");
				obj.ddr4.focus();
				failed=true;
			}
		} else
		 	failed=true;
	} else
	 	failed=true;
	if (!failed && obj.dnme!=null && obj.name!=null) {
		obj.dnme.value = obj.name.value;
	} else
	 	failed=true;
	if (!failed && obj.ddr1!=null && obj.adr1!=null) {
		obj.ddr1.value = obj.adr1.value;
	} else
	 	failed=true;
	if (!failed && obj.ddr2!=null && obj.adr2!=null) {
		obj.ddr2.value = obj.adr2.value;
	} else
	 	failed=true;
	if (!failed && obj.ddr3!=null && obj.adr3!=null) {
		obj.ddr3.value = obj.adr3.value;
	} else
	 	failed=true;
	if (!failed && obj.dcde!=null && obj.pcde!=null) {
		obj.dcde.value = obj.pcde.value;
	} else
	 	failed=true;
	if (failed) {
		alert("Could not copy all of the card holders details, please enter valid delivery details.");
	}
}


