// $Id: validation.js 13262 2008-09-15 15:32:56Z nicole.ruduss@primelocation.com $

function isValidPostcode (sPostcode) {
	// Permitted letters depend upon their position in the postcode.
	var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
	var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
	var alpha3 = "[abcdefghjkstuw]";                                // Character 3
	var alpha4 = "[abehmnprvwxy]";                                  // Character 4
	var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
	
	
	// Array holds the regular expressions for the valid postcodes
	var aRegexes = new Array ();
	
	// NB: was (\\s*) in all of following
	
	// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, AANN NAA
	// Expression for postcodes: AN NA, ANN NA, AAN NA, AANN NA
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s)([0-9]{1}" + alpha5 + "{1,2})$","i"));
	
	// Expression for postcodes: ANA NAA
	// Expression for postcodes: ANA NA
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s)([0-9]{1}" + alpha5 + "{1,2})$","i"));
	
	// Expression for postcodes: AANA NAA
	// Expression for postcodes: AANA NA
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s)([0-9]{1,2}" + alpha5 + "{1})$","i"));
	
	// Expression for postcodes: AN N, ANN N, AAN N, AANN N
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s)([0-9]{1})$","i"));
	
	// Expression for postcodes: ANA N
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s)([0-9]{1})$","i"));
	
	// Expression for postcodes: AANA N
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s)([0-9]{1,2})$","i"));
	
	// Expression for postcodes: AN, ANN, AAN, AANN
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})$","i"));
	
	// Expression for postcodes: ANA
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s)$","i"));
	
	// Expression for postcodes: AANA
	aRegexes.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s)$","i"));
	
	// Exception for the special postcode GIR 0AA
	// aRegexes.push (/^(GIR)(\s*)(0AA)$/i);
	
	// Standard BFPO numbers
	// aRegexes.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
	
	// c/o BFPO numbers
	// aRegexes.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
	
	// Load up the string to check
	var postCode = sPostcode;
	
	// Assume we're not going to find a valid postcode
	var bPostcodeOK = false;
	
	// Check the string against the types of post codes
	for ( var i=0; i<aRegexes.length; i++) {
		if (aRegexes[i].test(sPostcode)) {
		
		// The post code is valid - split the post code into component parts
		aRegexes[i].exec(sPostcode);
		
		// Copy it back into the original string, converting it to uppercase and
		// inserting a space between the inward and outward codes
		sPostcode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
		
		// If it is a BFPO c/o type postcode, tidy up the "c/o" part
		// sPostcode = sPostcode.replace (/C\/O\s*/,"c/o ");
		
		// Load new postcode back into the form element
		bPostcodeOK = true;
		
		// Remember that we have found that the code is valid and break from loop
		break;
		}
	}
	
	if (bPostcodeOK) {
		return true;
	} else {
		return false;
	}
}

function isValidEmail(sEmail) {
	var sHeadRegex  = /^[a-z0-9]([a-z0-9_\-\.]*)$/i;
	var sTailRegex  = /^([a-z0-9_\-]+)((\.[a-z0-9_\-]+)*)(\.[a-z]*){1,2}$/i;
	var bEmailOK = true;
	
	// list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt
	var aTLDs = new Array ("AC", "AD", "AE", "AERO", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "ARPA", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BIZ", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CAT", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "COM", "COOP", "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EDU", "EE", "EG", "ER", "ES", "ET", "EU", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GOV", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "INFO", "INT", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JOBS", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MIL", "MK", "ML", "MM", "MN", "MO", "MOBI", "MP", "MQ", "MR", "MS", "MT", "MU", "MUSEUM", "MV", "MW", "MX", "MY", "MZ", "NA", "NAME", "NC", "NE", "NET", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "ORG", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PRO", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SU", "SV", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TP", "TR", "TRAVEL", "TT", "TV", "TW", "TZ", "UA", "UG", "UK", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "YU", "ZA", "ZM", "ZW");
	
	if (sEmail == "") {
		bEmailOK = false;
	} else {
		aSplitAtDot = sEmail.split(".");
		sTLD = aSplitAtDot[aSplitAtDot.length-1];
		
		aSplitAtAt = sEmail.split("@");
		sHead = aSplitAtAt[0];
		sTail = aSplitAtAt[aSplitAtAt.length-1];
		
		if (aSplitAtAt.length != 2) {
			// too many or too few @'s
			bEmailOK = false;
		}
		
		if (bEmailOK) {
			if ( (sHead == "") || (sTail == "") ) {
				// head and/or tail missing
				bEmailOK = false;
			}
		}
		
		if (bEmailOK) {
			if (sHeadRegex.test(sHead)) {
				// head ok
				bEmailOK = true;
			} else {
				// head bad
				bEmailOK = false;
			}
		}
		
		if (bEmailOK) {
			if (sTailRegex.test(sTail)) {
				// tail ok (so far)
				bEmailOK = true;
			} else {
				// tail bad
				bEmailOK = false;
			}
		}
		
		if (bEmailOK) {
			if (indexInArray(aTLDs, sTLD.toUpperCase()) != -1 ) {
				// TLD ok
				bEmailOK = true;
			} else {
				// TLD bad
				bEmailOK = false;
			}
		}
		
	}

	return bEmailOK;
}

// Used by isValidEmail, so included here for convenience, but could be in a utility library.
function indexInArray(aArray2Search, sWhat2Find){
	iArrayLength = aArray2Search.length;
	for (var i=0; i<iArrayLength; i++){
		if (aArray2Search[i] == sWhat2Find){
			return i;
		}
	}
	return -1;
}

function isValidPLInvoiceNumber(sInvoiceNumber) {
	// Checks that it is a valid Primelocation invoice number
	// Function may be overkill for now, but at some point we may have invoice numbers requiring more complex validation
	
	var sInvoiceNumberRegex  = /^\d{5,6}$/;
	
	if (sInvoiceNumberRegex.test(sInvoiceNumber)) {
		//alert("invoice number ok");
		return true;
	} else {
		//alert("invoice number bad");
		return false;
	}
}

function isValidPLAccountNumber(sAccountNumber) {
	// Checks that it is a valid Primelocation account number
	// Acceptable formats are: Annn, AAnn, AAAnn, AAAn (all with optional a01 or a02 at end)
	// Regex could be written more succinctly, but is done verbosely so that other account number formats are easy to add
	
	var sAccountNumberRegex  = /^(\w{1}\d{3}|\w{2}\d{2}|\w{3}\d{1}|\w{3}\d{2})(a01|a02|A01|A02)?$/i;
	
	if (sAccountNumberRegex.test(sAccountNumber)) {
		//alert("account number ok");
		return true;
	} else {
		//alert("account number bad");
		return false;
	}
}

function isValidAmount(sNumber) {
	// Checks that it is a valid amount
	// Acceptable formats: integer or number with two decimal places (no commas)
	
	var sAmountRegex  = /^\d+(\.\d{2})?$/i;
	
	if (sAmountRegex.test(sNumber)) {
		//alert("amount ok");
		return true;
	} else {
		//alert("amount bad");
		return false;
	}
}
