﻿function UkPhoneNumberValidator( controlToValidate )
{
	// Check the ID is set correctly on the validator that calls here
	if( controlToValidate.IDToEval == '' )
	{
		return -1;
	}
		
	// Get the date as a string from the text box
	var telephoneNumber = VAM_GetTextValue( controlToValidate.IDToEval, controlToValidate.Trim );

	// Don't allow country codes to be included (assumes a leading "+")
	if ( telephoneNumber.match(/^\+.+$/) )
	{
		controlToValidate.Action.ErrMsg = "Enter UK number without country code";
		return 0;
	}

	// Remove everything but numeric digits from the telephone number to help validation
	telephoneNumber = telephoneNumber.replace(/[^0-9]*([0-9]*)/g, "$1");

	// Now check that it is 10 or 11 digits long
	if( telephoneNumber.match(/^[0-9]{10,11}$/) == null )
	{
		controlToValidate.Action.ErrMsg = "UK numbers should be 10 or 11 digits";
		return 0;
	}

	// Now check that the first digit is 0
	if ( telephoneNumber.match(/^0[0-9]{9,10}$/) == null )
	{
		controlToValidate.Action.ErrMsg = "Number should start with a 0";
		return 0;
	}

	// Finally check that the telephone number is appropriate.
	if ( telephoneNumber.match(/^(06|070)[0-9]+$/) )
	{
		controlToValidate.Action.ErrMsg = "06 / 070 numbers cannot be accepted";
		return 0;
	}

	// Check that the telephone number is appropriate.
	if (telephoneNumber.match(/^(01|02|03|05|07|08)[0-9]+$/) == null )
	{
		controlToValidate.Action.ErrMsg = "Enter UK home, work or mobile number";
		return 0;
	}

	// Check for too many repeating digits
	if (telephoneNumber.match(/^\d*(\d)\1{7}\d*$/) )
	{
		controlToValidate.Action.ErrMsg = "Enter UK home, work or mobile number";
		return 0;
	}

	return 1;
}


var minLoanAge = 18;
var maxLoanAge = 70;

// Checks a DOB MultiSegmentDataEntry value is inside an age range. Expects the date to be validated already.
function AgeValidator( controlToValidate )
{
	// Check the ID is set correctly on the validator that calls here
	if( controlToValidate.IDToEval == '' )
	{
		return -1;
	}
		
	// Get the date as a string from the MultiSegmentDataEntry
	var dateString = VAM_GetTextMSDE( controlToValidate.IDToEval );

	if( dateString.length < 8 )
	{
		return -1;
	}

	// Extract the separate date components from the string
	var day = dateString.substring(0,2);
	var month = dateString.substring(2,4);
	var year = dateString.substring(4,8);

	// Get current date
	var now = new Date();
	var nowYear = now.getFullYear();
	var nowMonth = now.getMonth() + 1; // +1 converts from JavaScript month numbering (0-11)
	var nowDay = now.getDate();

	var age = nowYear - year;

	// Decrease age if we're too late in the year (before day and month of birth)
	if ( ( month > nowMonth ) || ( month == nowMonth && day > nowDay ) )
	{
		age --;
	}

	if( age < minLoanAge || age > maxLoanAge )
	{
		return 0;
	}

	return 1;
}

function DateValidator( controlToValidate )
{
	// Check the ID is set correctly on the validator that calls here
	if( controlToValidate.IDToEval == '' )
	{
		return -1;
	}
		
	// Get the date as a string from the MultiSegmentDataEntry
	var dateString = VAM_GetTextMSDE( controlToValidate.IDToEval );
	
	// Less than 8 characters means they've not filled out the full date drop-down
	if( dateString.length < 8 )
	{
		return 0;
	}
	
	// Extract the separate date components from the string
	var day = dateString.substring(0,2);
	var month = dateString.substring(2,4);
	var year = dateString.substring(4,8);
	
	// Check if the day is valid for the month (e.g. 30th Feb, 31st April is never valid)
	if( daysInMonth( month, year ) < day )
	{
		return 0;
	}

	return 1;
}

function daysInFebruary(year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function daysInMonth( month, year )
{
	// April, June, September, November
	if( month == 4 || month == 6 || month == 9 || month == 11 )
	{
		return 30;
	}
	else if( month == 2 ) // February
	{
		return daysInFebruary( year );
	}
	else // January, March, May, July, August, October, December
	{
		return 31;
	}
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

var postcodeOutcodes = new Array();
postcodeOutcodes[0]="AB";
postcodeOutcodes[1]="DG";
postcodeOutcodes[2]="DD";
postcodeOutcodes[3]="FK";
postcodeOutcodes[4]="EH";
postcodeOutcodes[5]="KY";
postcodeOutcodes[6]="KA";
postcodeOutcodes[7]="IV";
postcodeOutcodes[8]="KW";
postcodeOutcodes[9]="PA";
postcodeOutcodes[10]="PH";
postcodeOutcodes[11]="ML";
postcodeOutcodes[12]="HS";
postcodeOutcodes[13]="ZE";
postcodeOutcodes[14]="CF";
postcodeOutcodes[15]="LD";
postcodeOutcodes[16]="LL";
postcodeOutcodes[17]="NP";
postcodeOutcodes[18]="SA";
postcodeOutcodes[19]="SY";
postcodeOutcodes[20]="BD";
postcodeOutcodes[21]="DH";
postcodeOutcodes[22]="DL";
postcodeOutcodes[23]="DN";
postcodeOutcodes[24]="HD";
postcodeOutcodes[25]="HG";
postcodeOutcodes[26]="HU";
postcodeOutcodes[27]="HX";
postcodeOutcodes[28]="LN";
postcodeOutcodes[29]="LS";
postcodeOutcodes[30]="NE";
postcodeOutcodes[31]="SR";
postcodeOutcodes[32]="TS";
postcodeOutcodes[33]="WF";
postcodeOutcodes[34]="YO";
postcodeOutcodes[35]="BB";
postcodeOutcodes[36]="BL";
postcodeOutcodes[37]="CA";
postcodeOutcodes[38]="CH";
postcodeOutcodes[39]="CW";
postcodeOutcodes[40]="FY";
postcodeOutcodes[41]="LA";
postcodeOutcodes[42]="M";
postcodeOutcodes[43]="OL";
postcodeOutcodes[44]="PR";
postcodeOutcodes[45]="SK";
postcodeOutcodes[46]="TF";
postcodeOutcodes[47]="WA";
postcodeOutcodes[48]="WN";
postcodeOutcodes[49]="B";
postcodeOutcodes[50]="CV";
postcodeOutcodes[51]="DE";
postcodeOutcodes[52]="DY";
postcodeOutcodes[53]="LE";
postcodeOutcodes[54]="NG";
postcodeOutcodes[55]="NN";
postcodeOutcodes[56]="ST";
postcodeOutcodes[57]="WS";
postcodeOutcodes[58]="WV";
postcodeOutcodes[59]="AL";
postcodeOutcodes[60]="CB";
postcodeOutcodes[61]="CM";
postcodeOutcodes[62]="CO";
postcodeOutcodes[63]="EN";
postcodeOutcodes[64]="IG";
postcodeOutcodes[65]="IP";
postcodeOutcodes[66]="LU";
postcodeOutcodes[67]="MK";
postcodeOutcodes[68]="NR";
postcodeOutcodes[69]="PE";
postcodeOutcodes[70]="RM";
postcodeOutcodes[71]="SG";
postcodeOutcodes[72]="SS";
postcodeOutcodes[73]="WD";
postcodeOutcodes[74]="BA";
postcodeOutcodes[75]="BH";
postcodeOutcodes[76]="BS";
postcodeOutcodes[77]="DT";
postcodeOutcodes[78]="EX";
postcodeOutcodes[79]="GL";
postcodeOutcodes[80]="HR";
postcodeOutcodes[81]="PL";
postcodeOutcodes[82]="TA";
postcodeOutcodes[83]="TQ";
postcodeOutcodes[84]="TR";
postcodeOutcodes[85]="WR";
postcodeOutcodes[86]="GU";
postcodeOutcodes[87]="HA";
postcodeOutcodes[88]="HP";
postcodeOutcodes[89]="OX";
postcodeOutcodes[90]="PO";
postcodeOutcodes[91]="RG";
postcodeOutcodes[92]="SL";
postcodeOutcodes[93]="SN";
postcodeOutcodes[94]="SO";
postcodeOutcodes[95]="SP";
postcodeOutcodes[96]="UB";
postcodeOutcodes[97]="BN";
postcodeOutcodes[98]="BR";
postcodeOutcodes[99]="CR";
postcodeOutcodes[100]="CT";
postcodeOutcodes[101]="DA";
postcodeOutcodes[102]="KT";
postcodeOutcodes[103]="ME";
postcodeOutcodes[104]="RH";
postcodeOutcodes[105]="SM";
postcodeOutcodes[106]="TN";
postcodeOutcodes[107]="TW";
postcodeOutcodes[108]="E";
postcodeOutcodes[109]="EC";
postcodeOutcodes[110]="N";
postcodeOutcodes[111]="NW";
postcodeOutcodes[112]="SE";
postcodeOutcodes[113]="SW";
postcodeOutcodes[114]="W";
postcodeOutcodes[115]="WC";
postcodeOutcodes[116]="G";
postcodeOutcodes[117]="TD";
postcodeOutcodes[118]="GY";
postcodeOutcodes[119]="JE";
postcodeOutcodes[120]="BT";
postcodeOutcodes[121]="IM";
postcodeOutcodes[122]="S";
postcodeOutcodes[123]="L";

function PostcodeValidator( controlToValidate )
{
	// Check the ID is set correctly on the validator that calls here
	if( controlToValidate.IDToEval == '' )
	{
		return -1;
	}

	var postcode = VAM_GetTextValue( controlToValidate.IDToEval, controlToValidate.Trim );

	return PostcodeCheck( postcode );
}
	
function PostcodeCheck( postcode )
{
	postcode = trim(postcode);

	// 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 pcexp = new Array ();

	// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
	
	// Expression for postcodes: ANA NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

	// Expression for postcodes: AANA  NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

	// Assume we're not going to find a valid postcode
	var valid = false;
	
	// Check the string against the types of post codes
	for ( var i=0; i<pcexp.length; i++ )
	{
		if (pcexp[i].test(postcode))
		{
		  // The post code is valid - split the post code into component parts
		  pcexp[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 = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
		  
		  // Load new postcode back into the form element
		  valid = true;
		  
		  // Remember that we have found that the code is valid and break from loop
		  break;
		}
	}
  
	if( valid )
	{
		var postcodeOutcode = '';
		
		// Pull just the Outcode from the Postcode
		if( isFinite( postcode.charAt(1) ) )
		{
			postcodeOutcode = postcode.charAt(0);
		}
		else
		{
			postcodeOutcode = postcode.substring(0, 2);
		}

		// Check the Outcode is in the list
		for( index in postcodeOutcodes )
		{
			if( postcodeOutcode == postcodeOutcodes[index] )
			{
				// Appears valid
				return 1;
			}
		}
	}

	// Is invalid
	return 0;
}

