function CheckPostcode(form)
{
	var strPostcode = form.Postcode.value;
	if ((strPostcode=="") || (strPostcode==null))
	{
		alert("Invalid postcode entered, please try again.");
		return false;
	}
	if (strPostcode.length < 5)
	{
		alert("Invalid postcode entered, please try again.");
		return false;
	}
	strPostcode = strPostcode.toUpperCase();

	// Strip all spaces and store the result in a new string...
	var strPostcode2 = ""
	var i, ch
	var validChars = /[a-zA-Z0-9]/
	for ( i = 0; i < strPostcode.length; i++ )
	{
		ch = strPostcode.charAt(i)
		if ( validChars.test(ch) )
		{
			strPostcode2 = strPostcode2 + ch
		}
	}

	var strIn, strOut;
	strOut = strPostcode2.substring(0,strPostcode2.length-3);
	strIn = strPostcode2.substring(strPostcode2.length-3);

	var n, strArea, strDistrict, strSector, strUnit;
	for (i = 1; i < 4; i++)
	{
		// Check each character in turn
		// First numeric digit is start of district code
		if(!isNaN(parseInt(strOut.charAt(i))))
		{
			strArea = strOut.substring(0, i);
			strDistrict = strOut.substring(i);
			break
		}
	}
	// Check that we found a number in the outward part of the postcode...
	if (i == 4)
	{
		alert("Invalid postcode entered, please try again.");
		return false
	}
	
	if (!isNaN(strArea))
	{
		alert("Invalid postcode entered, please try again.");
		return false;
	}

	if ((strArea.length > 2) || (strDistrict.length > 2))
	{
		alert("Invalid postcode entered, please try again.");
		return false;
	}

	// Inward code is simple...
	strSector = strIn.charAt(0);
	if (isNaN(strSector))
	{
		if (strSector == "O")
		{
			// The user has entered the letter "o" instead of the number zero
			strSector = "0";
		}
		else
		{
			alert("Invalid postcode entered, please try again.");
			return false;
		}
	}

	strUnit = strIn.substring(1);
	if (!isNaN(strUnit))
	{
		alert("Invalid postcode entered, please try again.");
		return false;
	}

	strPostcode = strArea + strDistrict + ' ' + strSector + strUnit;

	form.Postcode.value = strPostcode; 
	return true;
}

function ProcessHomeCheckForm()
{
	if (CheckPostcode(document.HomeCheckForm))
	{
url="http://www.homecheck.co.uk/default.asp?Postcode="+document.HomeCheckForm.Postcode.value;
properties='history=no,toolbar=0,location=0,directories=0,scrollbars=yes,status=0,menubar=0,width=800,height=600';
window.open(url,"HomeCheck",properties);
	}
}

function ProcessHomeCheckForm2(page)
{
	if (CheckPostcode(document.HomeCheckForm))
	{
		if (processForm(document.HomeCheckForm, page))
		{
			document.HomeCheckForm.submit();
		}
	}
}

function processForm(f, page) {

	if (page == "risk")
	{
		document.thisform.action = "result.asp";	
	}
	else
	{
		document.thisform.action = "neighbourhood.asp";	
	}
	return true;
}

