<!--

	/***********************************************\
	  VERSION CONTROL TAG
	  Postcode_validation.js Version 29Nov01
	\***********************************************/

function validPostcode(frmElement){
/*
sjm 24/05/2001 
There are 6 possible valid UK postcodes:
where L = letter and N = Number
LN NLL
LLN NLL
LNN NLL
LNL NLL
LLNN NLL
LLNL NLL
*/

var pcode = frmElement.value;
var pcType1 = /^[A-Z]{1}\d{1}\s{1}\d{1}[A-Z]{2}/;
var pcType2 = /^[A-Z]{2}\d{1}\s{1}\d{1}[A-Z]{2}/;
var pcType3 = /^[A-Z]{1}\d{2}\s{1}\d{1}[A-Z]{2}/;
var pcType4 = /^[A-Z]{2}\d{2}\s{1}\d{1}[A-Z]{2}/;
var pcType5 = /^[A-Z]{2}\d{1}[A-Z]{1}\s{1}\d{1}[A-Z]{2}/;
var pcType6 = /^[A-Z]{1}\d{1}[A-Z]{1}\s{1}\d{1}[A-Z]{2}/;

var sAlertEmpty = "Please enter a valid UK postcode.";
var sAlertLength = "Sorry, but the postcode you entered was too long or too short. Please enter a valid UK postcode.";
var sAlertInvalid = "Sorry, but the postcode you entered was not valid. Please enter a valid UK postcode.";
	
	if (pcode==null||pcode==""){alert(sAlertEmpty);return false;}
	
	pcode = formatPostcode(pcode);
	
	if (pcode.length<6||pcode.length>8){alert(sAlertLength);return false;}
	
	if (pcode.match(pcType1)||pcode.match(pcType2)||pcode.match(pcType3)||pcode.match(pcType4)||pcode.match(pcType5)||pcode.match(pcType6)){
		frmElement.value = pcode; return true;
	}
	else {alert(sAlertInvalid);return false;}
}

function formatPostcode(xPC){

	xPC = xPC.toUpperCase();

// replace all spaces
	xRegExp = / /g;
	xPC = xPC.replace(xRegExp, "");

// replace all !
	xRegExp = /!/g;
	xPC = xPC.replace(xRegExp, "");

// now put the space back in the right place
	xLen = xPC.length;
	xLastStart = xLen - 3;
	xPart1 = xPC.substr(0, xLastStart);
	xPart2 = xPC.substr(xLastStart, 3);
	xPC = xPart1 + " " + xPart2;
	return xPC;
}
function validate(frm){
	if (!validPostcode(frm.postcode)){return false;}
	else {return true;}
}
		
// -->

