function validPostcode(frmElement){
/*
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 to submit a form containing an a href
function submitForm(oEventSrc)
{
	if(!oEventSrc)
	{
		if(event)
		{
			if(event.srcElement) oEventSrc = event.srcElement;
			else if(event.target) oEventSrc = event.target;
		}
	}
    var oForm = oEventSrc.parentNode;
	if(typeof(oEventSrc.onsubmit)+""!="undefined") oForm = oEventSrc;
	else
	{
		while(oForm = oForm.parentNode)
		{
			if(typeof(oForm.nodeName)  == "string" && oForm.nodeName == "FORM") break;
		}
	}
    if(oForm.onsubmit)
    {
		if(oForm.onsubmit()!=false) oForm.submit();
		else return false;
	}
    else oForm.submit();
}

function validRadio(oRadio){
	if(!oRadio){return false;}
	for(var i = 0; i < oRadio.length; i++){
		if(oRadio[i].checked){
			if(typeof(oRadio[i].action)+""!="undefined") oRadio[i].form.action = oRadio[i].action;
			else if(typeof(returnAttribute(oRadio[i],"action"))+""!="undefined" && returnAttribute(oRadio[i],"action") != null) oRadio[i].form.action = returnAttribute(oRadio[i],"action");
			return true;
		}
	}
		
	if(oRadio[0].alt.indexOf("EXEPTION:") != -1){
		alert(oRadio[0].alt.replace("EXEPTION:",""));
	}else{
		alert("Please tell us " + oRadio[0].alt);
	}
	oRadio[0].focus();
	return false;
}
