<!--
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_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_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 returnArrtib(element, attrib){
	for (i=0;i<element.attributes.length;i++) {
		if(element.attributes[i].name == attrib){return element.attributes[i].value; break;}
	}
}

fieldNames = Array("title", "foreName", "surName", "homeTel", "email", "confirmEmail", "address1", "townCity", "postCode");
fieldFullNames = Array("Title", "Forename", "Surname", "Home telephone", "Email address", "Confirm email address", "Address line 1", "Town / City",  "Postcode");

function formHandler(theForm){
	formError = false;
	for(x=0;x<fieldNames.length;x++){
		formVal = eval("MM_findObj('"+theForm+"')."+fieldNames[x]+".value");
		if(formVal == ""){
			MM_findObj("statusDiv").innerHTML = "Please complete the "+fieldFullNames[x]+" field.";
			//set form error to prevent form submit
			formError = fieldNames[x];
			break;
		}
		if(fieldNames[x] == "homeTel" && !varifyNumber(formVal)){
			MM_findObj("statusDiv").innerHTML = "The 'Home telephone' field must only contain numbers and spaces.";
			//set form error to prevent form submit
			formError = fieldNames[x];
			break;
		}
		if(fieldNames[x] == "email" && !varifyEmail(formVal)){
			MM_findObj("statusDiv").innerHTML = "The email address does not appear to be valid.";
			//set form error to prevent form submit
			formError = fieldNames[x];
			break;
		}
		emailVal = eval("MM_findObj('"+theForm+"').email.value");
		if(fieldNames[x] == "confirmEmail" && emailVal != formVal){
			MM_findObj("statusDiv").innerHTML = "Email and Confirm email fields do not match!";
			//set form error to prevent form submit
			formError = fieldNames[x];
			break;
		}
	}
	/*
	if(MM_findObj("furtherAddress").style.display == "none" && formError == false){
		MM_findObj("statusDiv").innerHTML = "Please click the 'Find address' button.";
		//set form error to prevent form submit
		formError = 1;
	}
	*/
	//if no errors, submit
	if(!formError){
		MM_findObj(theForm).submit();
	}else{
		if(formError!=1){
			formField = eval("MM_findObj('"+theForm+"')."+formError);
			formField.focus();
		}
	}
}

function varifyEmail(string){
	//set error status variable.
	varified = false;
	//check string for @ sign after 1 character
	if(string.indexOf("@") >= 1){
		//if found, split string into array using the @ sign
		emailArray = string.split("@");
		//check 2nd array index for full-stop after 1 character, and set varified var as true (varified!)
		if(emailArray[1].indexOf(".") >= 1 && emailArray[1].indexOf(".") < emailArray[1].length-1){varified = true;}
	}
	return varified;
}

function varifyNumber(string){
	//set regular expression - 0 to9 and space
	numericExp = RegExp("[0-9 ]");
	//loop through each character of the string
	for(y=0;y<string.length;y++){
		//if a char is found that is not in the regular expression, return false (stops function at this point)
		if(!numericExp.test(string.substr(y,1))){
			return(false);
			break;
		}
	}
	//if no errors, returns true
	return(true);
}

function formElementFocus(formID, elementID){
	theForm = MM_findObj(formID);
	theElement = eval("theForm."+elementID);
	theElement.focus();
}
function getPostcode(){
	postcode = MM_findObj("formClaim").postCode.value;
	if(postcode == ""){
		MM_findObj("statusDiv").innerHTML = "Please enter a postcode!";
	}else{
		response = "";
		new Ajax.Request("http://www.challengeyour.com/includes/getPostcode.php?postcode="+postcode,
		{method:'get',
			 onSuccess: function(transport){
				postcodeReturned(transport.responseText);
			 },
			  onFailure: function(){
				postcodeFailed();
			 }
		})
		setTimeout("postcodeFailed()",4000);
	}
}
function postcodeFailed(){
	if(MM_findObj("formClaim").address2.value == ""){
		MM_findObj("statusDiv").innerHTML = "Your postcode was not found.<br>Please try again or complete your address manually.";
		MM_findObj("furtherAddress").style.display = "block";
		MM_findObj("findPostcodeDiv").style.display = "none";
	}
}
function postcodeReturned(postcode){
	if(postcode!="KO"){
		address = postcode.split("<->");
		x=0;
		MM_findObj("formClaim").address2.value = address[x];x++;
		if(address.length==5){
			MM_findObj("formClaim").address3.value = address[x];x++;
		}
		MM_findObj("formClaim").townCity.value = address[x];x++;
		MM_findObj("formClaim").county.value = address[x];x++;
		MM_findObj("formClaim").postCode.value = address[x];
		MM_findObj("statusDiv").innerHTML = "Postcode found.";
		MM_findObj("furtherAddress").style.display = "block";
		MM_findObj("findPostcodeDiv").style.display = "none";
		MM_findObj("formClaim").address1.focus();
	}else{
		postcodeFailed();
	}
}

function disableEnterKey(e){
	var key;
	if(window.event){key = window.event.keyCode;}else{key = e.which;}
	if(key == 13){return false;}else{return true;}
}