// $Id: postCodes_uk.js 2177 2008-10-27 07:48:39Z dmf $
// UK post code validation
var lastPostCode = "!!!";
function validatePostCodeUK (formElement, districtOnly, submitTime) {
	// First, clean out chars that are never valid
	formElement.value = formElement.value.replace(/[^a-z 0-9]/gi, '');
	var postCode = formElement.value;
	if (postCode.toUpperCase() != lastPostCode.toUpperCase()) {
		lastPostCode = formElement.value;
		var realChange = true;
	} else {
		var realChange = false;
	}
	var valid = false;
	// Valid chars in positions
    var alpha1 = "[abcdefghiklmnoprstuwyz]";
    var alpha2 = "[abcdefghklmnopqrstuvwxy]";
    var alpha3 = "[bcdfghjkstuw]";
    var alpha4 = "[abehmnprvwxy]";
	var alpha56 = "[abdefghjlnpqrstuwxyz]";
	// Build an array of valid regexp formats
	var regExps = new Array ();
	// AN NAA, ANN NAA, AAN NAA, and AANN NAA
	regExps.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha56 + "{2})$","i"));
	// ANA NAA
	regExps.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha56 + "{2})$","i"));
	// AANA  NAA
	regExps.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha56 + "{2})$","i"));
	if (districtOnly) {
		// AANA
		regExps.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)(.*)$","i"));
		// ANA
		regExps.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)(.*)$","i"));
		// AN, ANN, AAN, and AANN
		regExps.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)(.*)$","i"));
	}
	
	// Iterate through patterns doing checks
	for ( var i=0; i<regExps.length; i++) {
		if (regExps[i].test(postCode)) {
			// The post code is valid - split the post code into component parts
			regExps[i].exec(postCode);
			// Copy it back into the original string, converting it to uppercase and
			// inserting a space between the inward and outward codes
			postCode = postCodeDistrict = RegExp.$1.toUpperCase();
			if (!districtOnly) {
				postCode = postCode + " " + RegExp.$3.toUpperCase();
			}
			// Load new postcode back into the form element
			formElement.value = postCode;
			valid = true;
			break;
		}
	}
	
	if (!valid) {
		// If it hasn't matched a valid pattern yet, bleat!
		if (submitTime || realChange) {
			formElement.focus();
			if (districtOnly) {
				alert(valStrings["invalidPostCodeDFormat"]);
			} else {
				alert(valStrings["invalidPostCodeFormat"]);
			}
			setTimeout("document.frmMM." + formElement.name + ".focus();",1);
			setTimeout("document.frmMM." + formElement.name + ".select();",1);
		}
		return false;
	} else {
		// Go on to test the district
        var districtRegExp = new RegExp("^(A(B(1[0-6]|2[1-5]|3[0-9]|4[1-5]|5[1-6])|L(10?|[2-9]))|B(1[0-9]?|2[013-9]?|3[0-8]?|4[02-9]?|50?|6[0-9]?|7[0-9]?|80?|9[0-8]?|A(1[0-6]?|2[012]?|[3-9])|B(1[0128]?|[2-9])|D(1[0-9]?|2[0-4]?|[3-9])|H(1[0-9]?|2[0-5]?|31?|[4-9])|L[0-9]|N(1[0-8]?|2[0-7]?|4[1-5]|[35-9])|R[1-8]|S(1[013-6]?|2[0-9]?|3[0124-79]?|4[0189]?|[5-9])|T(1[0-9]?|2[0-9]?|3[0-9]?|4[0-9]?|5[1-7]?|6[0-9]?|7[014-9]?|8[012]?|9[234]?))|C(A(1[0-9]?|2[0-8]?|[3-9])|B(1[01]?|2[1-5]?|[3-9])|F(1[0145]|2[34]|3[1-9]?|4[0-8]|6[1-4]|7[12]|8[123]|5)|H(4[1-9]?|6[0-6]?|[123578])|M(1[1-9]?|2[0-4]?|77?|[03-689])|O(1[0-6]?|[2-9])|R[02-8]|T(1[0-9]?|2[01]?|[3-9])|V(1[0-3]?|2[123]?|3[1-7]?|47?|[5-9])|W(1[012]?|[2-9]))|D(A(1[0-8]?|[2-9])|D(1[01]?|[2-9])|E(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]?|[3-9])|S(1[0-9]?|2[0-9]?|[3-9])|W(1[0-9]?|20?|[3-9]))|UB(1[01]?|[2-9])|W(1[0-4BCDFGHJKSTUW]|A(1[0-6]?|[2-9])|C(1[ABEHNRVX]|2[ABEHNR])|D(1[789]|2[345]|[3-7])|F(1[0-7]?|[2-9])|N[1-8]|R(1[0-5]?|[2-9])|S(1[0-5]?|[2-9])|V(1[0-6]?|[2-9])|[2-9])|YO(1[0-9]?|2[1-6]|3[012]|4[123]|51|6[012]|[78])|ZE[123])$");
		if (districtRegExp.test(postCodeDistrict)) {
			//alert(postCodeDistrict + " appears to be valid");
			return true;
		} else {
			if (submitTime || realChange) {
				formElement.focus();
				alert(postCodeDistrict + valStrings["invalidPostCodeDistrict"]);
				setTimeout("document.frmMM." + formElement.name + ".focus();",1);
				setTimeout("document.frmMM." + formElement.name + ".select();",1);
			}
			return false;
		}
	}
}
