<!-- //hide the script
function strltrim() {
	return this.replace(/^\s+/,'');
}

function strrtrim() {
	return this.replace(/\s+$/,'');
}
function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;
	
function checkEmail(strng) {
	var error = "";

	var emailFilter=/^.+@.+\..{2,6}$/;

	if (!(emailFilter.test(strng))) {
		return false;
	}

	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\] ']/

	if (strng.match(illegalChars)) {
		return false;
	}

	return true;
}


function form_validator(theForm)
{

 	if(theForm.companyname.value == "") {
 		 alert("Please enter the company name.");
 		 theForm.companyname.focus();
 		 return(false);
 	}

 	if(theForm.sender.value == "") {
 		 alert("Please enter your Full Name.");
 		 theForm.sender.focus();
 		 return(false);
 	}

 	if(theForm.address_1.value == "") {
 		 alert("Please enter your Address.");
 		 theForm.address_1.focus();
 		 return(false);
 	}

 	if(theForm.town.value == "") {
 		 alert("Please enter your Town.");
 		 theForm.town.focus();
 		 return(false);
 	}

 	if(theForm.county.value == "") {
 		 alert("Please enter your County.");
 		 theForm.county.focus();
 		 return(false);
 	}

 	if(theForm.postcode.value == "") {
 		 alert("Please enter your Post Code.");
 		 theForm.postcode.focus();
 		 return(false);
 	}

 	if(theForm.telephone.value == "") {
 		 alert("Please enter your Telephone Number.");
 		 theForm.telephone.focus();
 		 return(false);
 	}

	/*
	if(!checkEmail(theForm.email.value)) {
		 alert("Please enter a valid Email Address");
		theForm.email.focus();
		 return false;

	}
	*/

	return (true);
}

$(document).ready(function() {
	// COMPANY NAME AUTOCOMPLETE
	$("#companyname").autocomplete("xhr-companysearch.asp",{
		max: 50,
		minChars: 3,
		width: 500
	});
	
	// POSTCODE AFD INTEG.
	$("#postcodeholder").css("margin-top","20px").remove().insertBefore("label[for=address_1]");
	$("<input class='addybutton' type='button' value='Get Address' />").insertAfter("#postcode").click(function() {
		if ($("#postcode").val().length >= 5) {
			// REMOVE PREVIOUS OPTIONS BOX
			$("#pcoptions, #pcoptionslabel").remove();
		
			// NOW MAKE A NEW ONE
			$("<label id='pcoptionslabel' for='pcoptions'>Select Address:</label> <select class='contform' style='width: 400px' id='pcoptions' name='pcoptions' size='10'></select>").appendTo("#postcodeholder");
			
			// ATTACH EVENT TO SET ADDRESS
			$("#pcoptions").change(function() {
				addressVal = $("#pcoptions").val();
				arrSelectedAddress = addressVal.split(/\|/);
				
				/*
				for (i = 0; i < arrSelectedAddress.length; i++) {
					alert(i + ": " + arrSelectedAddress[i]);
				}
				*/
				
				$("#address_1").val(arrSelectedAddress[1].trim() + ' ' + arrSelectedAddress[2].trim());
				$("#address_2").val(arrSelectedAddress[3].trim());
				
				$("#town").val(arrSelectedAddress[4].trim());
				$("#county").val(arrSelectedAddress[6].trim());
				$("#postcode").val(arrSelectedAddress[5].trim());
				
				$("#telephone")[0].focus();
				
				// HIDE THE BOX
				$("#pcoptionslabel, #pcoptions").fadeOut();
			});
			
			// SEND THE POSTCODE LOOKUP REQUEST
			$.post("xhr-postcodesearch.asp",{ "pc": $("#postcode").val() },function(responseText) {
				if (responseText == "EOF") {
					alert("Error performing search");
				} else {
					arrAddresses = responseText.split(/\n/);
					
					for (aRecord = 0; aRecord < arrAddresses.length; aRecord++) {
						if (arrAddresses[aRecord] > "") {
							arrDetail = arrAddresses[aRecord].split(/\^/);				
							$("<option value='" + arrDetail[0] + "'>" + arrDetail[1].trim() + "</option>").appendTo("#pcoptions");
						}
					}					
				}
			},"text");
		} else {
			alert("Please enter a full postcode");
		}
	});
});

// end script hiding -->