//var normal_color = '#FFFFA0';
var normal_color = '#FFFFFF';
var error_color = '#EBA2A2';

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function $(id)
{
	return document.getElementById(id);
}

function validate_form1()
{
	$('error_more_less').innerHTML = '&nbsp;';
	$('err_div_tx').innerHTML = 'Please fix all the red fields!';

	$('customer.loan.requested').style.background = '#999999';
	$('customer.gender').style.background = normal_color;
	$('customer.firstName').style.background = normal_color;
	$('customer.lastName').style.background = normal_color;
	$('customer.phone1.digits').style.background = normal_color;
	$('customer.phone3.digits').style.background = normal_color;
	$('customer.email').style.background = normal_color;
	$('customer.dob.date').style.background = normal_color;

	$('customer.employment.employerName').style.background = normal_color;

	err = 0;

	// amount requested
	if(IsEmptyField('customer.loan.requested')) { 
		$('customer.loan.requested').style.background = error_color;
		err = 1; 
	} else {
		req = parseInt($('customer.loan.requested').value);
		if(req < 80) { 
			$('error_more_less').innerHTML = "Loan Requested must be greater than 80 Pounds";
			$('customer.loan.requested').style.background = error_color;
			err=1;
		}

		if(req > 1000) { 
			$('error_more_less').innerHTML = "Loan Requested must be less than 1,000 Pounds";
			$('customer.loan.requested').style.background = error_color;
			err=1;
		}
	}

	// title/gender
	if(IsEmptyField('customer.gender')) { 
		$('customer.gender').style.background = error_color;
		err = 1; 
	}

	// first name
	if(!validName($('customer.firstName').value)) {
		$('customer.firstName').style.background = error_color;
		err = 1; 
	}

	// last name
	if(!validName($('customer.lastName').value)) {
		$('customer.lastName').style.background = error_color;
		err = 1; 
	}

	// home phone
	homePhone = $('customer.phone1.digits').value;
	if(!validPhone(homePhone)) {
		$('customer.phone1.digits').style.background = error_color;
		err = 1; 
	}

	// mobile phone
	cellPhone = $('customer.phone3.digits').value;
	if(!validCellPhone(cellPhone)) {
		$('customer.phone3.digits').style.background = error_color;
		err = 1; 
	}

	/*
	if(homePhone && workPhone && homePhone == workPhone) {
		$('err_div_tx').innerHTML = "Home phone must be different than the work phone.";
		$('customer.phone1.digits').style.background = error_color;
		$('customer.phone2.digits').style.background = error_color;
		err = 1;
	}
	*/

	// email
	if(!checkMail($('customer.email').value)) {
		$('customer.email').style.background = error_color;
		err = 1; 
	}

	var dt1 = $('customer.dob.date').value;
	if(validateDate(dt1, 1900, 2000) == false) {	
		$('customer.dob.date').style.background = error_color;
		err = 1;
	} else  if(getAge(dt1) < 18) {
		$('err_div_tx').innerHTML = "You must be 18 or over.";
		$('customer.dob.date').style.background = error_color;
		err = 1;
	}

	var ename = $('customer.employment.employerName').value;
	if(IsEmptyField('customer.employment.employerName') || ename.length < 2) {
		$('customer.employment.employerName').style.background = error_color;
		err = 1; 
	}

	if($("allow_mails").checked == false && err == 0)
	{
		alert('To submit, please check that you have read our privacy policy');
		err = 1;
	}

	if (err == 1) {
		$('errdiv').style.display = 'block';
		return false;
	}

	$('errdiv').style.display = 'none';
	return true;
}

function clear_required_fields() 
{
	document.getElementById('customer.firstName').style.background = '#FFFFFF';			
	document.getElementById('customer.lastName').style.background = '#FFFFFF';			
	document.getElementById('customer.streetNumber').style.background = '#FFFFFF';		
	document.getElementById('customer.streetName').style.background = '#FFFFFF';			
	document.getElementById('customer.state').style.background = '#FFFFFF';			
	document.getElementById('customer.city').style.background = '#FFFFFF';			
	document.getElementById('customer.zip').style.background = '#FFFFFF';				
	document.getElementById('customer.email').style.background = '#FFFFFF';			
	document.getElementById('customer.phone1.digits').style.background = '#FFFFFF';		
	document.getElementById('customer.phone3.digits').style.background = '#FFFFFF';		
	document.getElementById('customer.dob.date').style.background = '#FFFFFF';			
	document.getElementById('customer.employment.employerName').style.background = '#FFFFFF';	
	document.getElementById('customer.employment.incomeSource').style.background = '#FFFFFF';	
	document.getElementById('customer.employment.netSalary').style.background = '#FFFFFF';	
	document.getElementById('customer.bankAccount.directDeposit').style.background = '#FFFFFF';
	document.getElementById('customer.employment.howOftenPaid').style.background = '#FFFFFF';
	document.getElementById('customer.phone2.digits').style.background = '#FFFFFF';	
	document.getElementById('customer.employment.payDate1').style.background = '#FFFFFF';	
	document.getElementById('customer.employment.jobTimeMonths').style.background = '#FFFFFF';
	document.getElementById('customer.hasCC').style.background = '#FFFFFF';	
	document.getElementById('customer.loan.requested').style.background = '#FFFFFF';
	document.getElementById('error_more_less').innerHTML='';
	document.getElementById('err_div_tx').innerHTML='Please fix all the red fields!';
	document.getElementById('customer.gender').style.background = '#FFFFFF';
	document.getElementById('customer.employment.payDate2').style.background = '#FFFFFF';
/*	
	document.getElementById('customer.BankAccountType').style.background = '#FFFFFF';
*/
	document.getElementById('customer.employment.nin').style.background = '#FFFFFF';
/*
	document.getElementById('preferredPhoneNumber').style.background = '#FFFFFF';
	document.getElementById('dateCurrentAddress').style.background = '#FFFFFF';
	document.getElementById('besttime').style.background = '#FFFFFF';
	document.getElementById('maritalStatus').style.background = '#FFFFFF';
	document.getElementById('accommodationType').style.background = '#FFFFFF';
	document.getElementById('hasOtherLoans').style.background = '#FFFFFF';
 
*/
 

	document.getElementById('cash_timeAtHome').style.background = '#FFFFFF';
//	document.getElementById('employerZip').style.background = '#FFFFFF';
//	document.getElementById('employerCity').style.background = '#FFFFFF';
} 

function check_fields() 
{

	var err=0;
	clear_required_fields();

	if(document.getElementById('customer.employment.payDate2').value == '')					{	document.getElementById('customer.employment.payDate2').style.background = '#FFD7D7';	err=1;	}
	if(document.getElementById('customer.gender').value == '')								{	document.getElementById('customer.gender').style.background = '#FFD7D7';					err=1;	}
	if(document.getElementById('customer.firstName').value == '')							{	document.getElementById('customer.firstName').style.background = '#FFD7D7';					err=1;	}
	if(document.getElementById('customer.lastName').value == '')							{	document.getElementById('customer.lastName').style.background = '#FFD7D7';					err=1;	}
	if(IsEmptyField('customer.streetNumber'))						{	document.getElementById('customer.streetNumber').style.background = '#FFD7D7';				err=1;	}
	if(IsEmptyField('customer.streetName'))							{	document.getElementById('customer.streetName').style.background = '#FFD7D7';				err=1;	}
	if(document.getElementById('customer.state').value == '')								{	document.getElementById('customer.state').style.background = '#FFD7D7';						err=1;	}
	if(IsEmptyField('customer.city'))								{	document.getElementById('customer.city').style.background = '#FFD7D7';						err=1;	}
	if(document.getElementById('customer.email').value == '')								{	document.getElementById('customer.email').style.background = '#FFD7D7';						err=1;	}
	if(document.getElementById('customer.phone1.digits').value == '')						{	document.getElementById('customer.phone1.digits').style.background = '#FFD7D7';				err=1;	}
	if(document.getElementById('customer.phone3.digits').value == '')						{	document.getElementById('customer.phone3.digits').style.background = '#FFD7D7';				err=1;	}
	if(document.getElementById('customer.dob.date').value == '')							{	document.getElementById('customer.dob.date').style.background = '#FFD7D7';					err=1;	}
	if(document.getElementById('customer.employment.employerName').value == '')				{	document.getElementById('customer.employment.employerName').style.background = '#FFD7D7';	err=1;	}
	if(document.getElementById('customer.employment.incomeSource').value == '')				{	document.getElementById('customer.employment.incomeSource').style.background = '#FFD7D7';	err=1;	}
	if(document.getElementById('customer.bankAccount.directDeposit').value == '')			{	document.getElementById('customer.bankAccount.directDeposit').style.background = '#FFD7D7';	err=1;	}
	if(document.getElementById('customer.employment.howOftenPaid').value == '')				{	document.getElementById('customer.employment.howOftenPaid').style.background = '#FFD7D7';	err=1;	}
	if(document.getElementById('customer.phone2.digits').value == '')						{	document.getElementById('customer.phone2.digits').style.background = '#FFD7D7';				err=1;	}
	if(document.getElementById('customer.employment.payDate1').value == '')					{	document.getElementById('customer.employment.payDate1').style.background = '#FFD7D7';		err=1;	}
	if(document.getElementById('customer.employment.jobTimeMonths').value == '')			{	document.getElementById('customer.employment.jobTimeMonths').style.background = '#FFD7D7';	err=1;	}
	if(document.getElementById('customer.hasCC').value == '')								{	document.getElementById('customer.hasCC').style.background = '#FFD7D7';						err=1;	}
/*
	if(document.getElementById('customer.BankAccountType').value == '')						{	document.getElementById('customer.BankAccountType').style.background = '#FFD7D7';			err=1;	}

	if(document.getElementById('preferredPhoneNumber').value == '')							{	document.getElementById('preferredPhoneNumber').style.background = '#FFD7D7';			err=1;	}
	if(document.getElementById('besttime').value == '')										{	document.getElementById('besttime').style.background = '#FFD7D7';			err=1;	}
	if(document.getElementById('maritalStatus').value == '')								{	document.getElementById('maritalStatus').style.background = '#FFD7D7';			err=1;	}
	if(document.getElementById('accommodationType').value == '')							{	document.getElementById('accommodationType').style.background = '#FFD7D7';			err=1;	}
	if(document.getElementById('hasOtherLoans').value == '')								{	document.getElementById('hasOtherLoans').style.background = '#FFD7D7';			err=1;	}
	 
*/


	if(document.getElementById('cash_timeAtHome').value == '')								{	document.getElementById('cash_timeAtHome').style.background = '#FFD7D7';			err=1;	}
//	if(document.getElementById('employerZip').value == '')									{	document.getElementById('employerZip').style.background = '#FFD7D7';			err=1;	}
//	if(document.getElementById('employerCity').value == '')									{	document.getElementById('employerCity').style.background = '#FFD7D7';			err=1;	}


	if(document.getElementById('customer.loan.requested').value == '')						
	{	
		document.getElementById('customer.loan.requested').style.background = '#FFD7D7';			
		err=1;	
	}
	else
	{
		var req = parseInt(document.getElementById('customer.loan.requested').value);
		if(req<80)		{ document.getElementById('error_more_less').innerHTML = "Loan Requested must be greater than 80 Pounds"; document.getElementById('customer.loan.requested').style.background = '#FFD7D7';		err=1;}
		if(req>1000)	{ document.getElementById('error_more_less').innerHTML = "Loan Requested must be less than 1,000 Pounds"; document.getElementById('customer.loan.requested').style.background = '#FFD7D7';		err=1;}
	}

	if(document.getElementById('customer.employment.netSalary').value == '' || 
		!IsNumeric(document.getElementById('customer.employment.netSalary').value == ''))						
	{	
		document.getElementById('customer.employment.netSalary').style.background = '#FFD7D7';			
		err=1;	
	}
	else
	{
		var req = parseInt(document.getElementById('customer.employment.netSalary').value);
		if(req<200)		{ document.getElementById('err_div_tx').innerHTML = "Monthly Income must be greater than 200 Pounds"; document.getElementById('customer.employment.netSalary').style.background = '#FFD7D7';		err=1;}
	}

	if(checkMail(document.getElementById('customer.email').value)==false) 					{	document.getElementById('customer.email').style.background = '#FFD7D7';						err=1;	}

	var zc = document.getElementById('customer.zip').value;
	if(checkPostCode(zc))
	{ document.getElementById('customer.zip').value = checkPostCode (zc) } else				{	document.getElementById('customer.zip').style.background = '#FFD7D7';						err=1;	}

	/*
 	var nn = document.getElementById('customer.employment.nin').value;
	if(nn!='')
	{
		if(checkNINO(nn))
		{ document.getElementById('customer.employment.nin').value = checkNINO (nn) } else		{	document.getElementById('customer.employment.nin').style.background = '#FFD7D7';			err=1;	}
	}
    */

	if(document.getElementById('customer.firstName').value.length < 2) {
		document.getElementById('customer.firstName').style.background = '#FFD7D7';
		err=1;
	}
	
	if(document.getElementById('customer.lastName').value.length < 2) {
		document.getElementById('customer.lastName').style.background = '#FFD7D7';
		err=1;
	}
	
	if(document.getElementById('customer.employment.employerName').value.length < 2) {
		document.getElementById('customer.employment.employerName').style.background = '#FFD7D7';
		err=1;
	}
	
	homePhone = $('customer.phone1.digits').value;
	if(!validPhone(homePhone)) {
		$('customer.phone1.digits').style.background = '#FFD7D7';
		err = 1; 
	}

	cellPhone = $('customer.phone3.digits').value;
	if(!validCellPhone(cellPhone)) {
		$('customer.phone3.digits').style.background = '#FFD7D7';
		err = 1; 
	}

	workPhone = $('customer.phone2.digits').value;
	if(!validPhone(workPhone)) {
		$('customer.phone2.digits').style.background = '#FFD7D7';
		err = 1; 
	}

	if(workPhone && homePhone && workPhone == homePhone) {
		document.getElementById('err_div_tx').innerHTML = "Home phone must be different than the work phone.";
		document.getElementById('customer.phone1.digits').style.background = '#FFD7D7';
		document.getElementById('customer.phone2.digits').style.background = '#FFD7D7';
		err=1;
	}

	var dt1 = document.getElementById('customer.dob.date').value;
	if(validateDate(dt1, 1900, 2000)==false)		{	
		document.getElementById('customer.dob.date').style.background = '#FFD7D7';					err=1;	
	} else  if(getAge(dt1) < 18) {
		document.getElementById('err_div_tx').innerHTML = "You must be 18 or over.";
		document.getElementById('customer.dob.date').style.background = '#FFD7D7';					err=1;	
	}
/*
	var dt1 = document.getElementById('dateCurrentAddress').value;
	if(validateDate(dt1, 1900, 2000)==false)												{	document.getElementById('dateCurrentAddress').style.background = '#FFD7D7';					err=1;	}
*/
	var dt2 = document.getElementById('customer.employment.payDate1').value;
	if(validateDate(dt2, 2007, 2029)==false)												{	document.getElementById('customer.employment.payDate1').style.background = '#FFD7D7';					err=1;	}

	if(document.getElementById("allow_mails").checked==false && err==0)
	{
		alert('To submit, please check that you have read our privacy policy');
		err = 1;
	}

	if (err==1)
	{
		document.getElementById('errdiv').style.display = 'block';
	}
	else
	{

		document.getElementById('errdiv').style.display = 'none';
		document.getElementById('submitbutton').style.display = 'none';
		document.getElementById('processing').style.display = 'block';

		var url = "/site/javascripts/v2/step2.php?" + allFormData();
		http.abort();
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		http.send(null);	
	}
}

 
  
function handleHttpResponse() 
{
	  if (http.readyState == 4) 
	  {		
			//window.open('http://inteletrack.com/z/11132/CD765/','','width=700,height=400,left=200,top=200').blur();
			//window.focus();

			if (http.responseText=='err') 
			{	
				//window.focus();
				top.location="/thank-you/";
				//window.focus();
			}
			else
			{
				//window.focus();
				top.location=http.responseText;
				//window.focus();
			}
	  }

}
 
 
function allFormData() 
{
	var dat="";
	document.getElementById('errdiv').style.display='none';
	dat = dat + "gender=" + escape(document.getElementById('customer.gender').value);
	dat = dat + "&firstname=" + escape(document.getElementById('customer.firstName').value);
	dat = dat + "&lastname=" + escape(document.getElementById('customer.lastName').value);			
	dat = dat + "&address1=" + escape(document.getElementById('customer.streetNumber').value+' '+document.getElementById('customer.streetName').value);
	dat = dat + "&streetnumber=" + escape(document.getElementById('customer.streetNumber').value);	
	dat = dat + "&streetname=" + escape(document.getElementById('customer.streetName').value);	
	dat = dat + "&state=" + escape(document.getElementById('customer.state').value);			
	dat = dat + "&city=" + escape(document.getElementById('customer.city').value);			
	dat = dat + "&zip=" + escape(document.getElementById('customer.zip').value);				
	dat = dat + "&email=" + escape(document.getElementById('customer.email').value);			
	dat = dat + "&phonenumber=" + escape(document.getElementById('customer.phone1.digits').value);		
	dat = dat + "&phonecell=" + escape(document.getElementById('customer.phone3.digits').value);		
	dat = dat + "&dob=" + escape(document.getElementById('customer.dob.date').value);			
	dat = dat + "&employerName=" + escape(document.getElementById('customer.employment.employerName').value);	
	dat = dat + "&IncomeSource=" + escape(document.getElementById('customer.employment.incomeSource').value);	
	dat = dat + "&netSalary=" + escape(document.getElementById('customer.employment.netSalary').value);	
	dat = dat + "&directDeposit=" + escape(document.getElementById('customer.bankAccount.directDeposit').value);
	dat = dat + "&howOftenPaid=" + escape(document.getElementById('customer.employment.howOftenPaid').value);
	dat = dat + "&phonework=" + escape(document.getElementById('customer.phone2.digits').value);	
	dat = dat + "&payDate1=" + escape(document.getElementById('customer.employment.payDate1').value);	
	dat = dat + "&jobTimeMonths=" + escape(document.getElementById('customer.employment.jobTimeMonths').value);
	dat = dat + "&creditCardType=" + escape(document.getElementById('customer.hasCC').value);	
	dat = dat + "&amountRequested=" + escape(document.getElementById('customer.loan.requested').value);
	dat = dat + "&nin=" + escape(document.getElementById('customer.employment.nin').value);
	dat = dat + "&SortCode=" + escape(document.getElementById('customer.employment.sortcode').value);
	dat = dat + "&BankAccountNumber=" + escape(document.getElementById('customer.employment.bankaccount').value);
	dat = dat + "&payDate2=" + escape(document.getElementById('customer.employment.payDate2').value);
	dat = dat + "&allow_mails=" + escape(document.getElementById("allow_mails").value);
/*
	dat = dat + "&BankAccountType=" + escape(document.getElementById("customer.BankAccountType").value);

	dat = dat + "&preferredPhoneNumber=" + escape(document.getElementById("preferredPhoneNumber").value);
	dat = dat + "&besttime=" + escape(document.getElementById("besttime").value);
	dat = dat + "&maritalStatus=" + escape(document.getElementById("maritalStatus").value);
	dat = dat + "&accommodationType=" + escape(document.getElementById("accommodationType").value);
	dat = dat + "&hasOtherLoans=" + escape(document.getElementById("hasOtherLoans").value);
	dat = dat + "&dateCurrentAddress=" + escape(document.getElementById("dateCurrentAddress").value);
*/
	dat = dat + "&cash_timeAtHome=" + escape(document.getElementById("cash_timeAtHome").value);
//	dat = dat + "&employerZip=" + escape(document.getElementById("employerZip").value);
//	dat = dat + "&employerCity=" + escape(document.getElementById("employerCity").value);

	dat = dat + "&SRLP=" + escape(document.getElementById("SRLP").value);

	return dat;
}


function isAlpha(str)
{
	regex = /^\[a-zA-Z]+$/;
	return regex.test(str);
}

function isAlphaNum(str)
{
	regex = /^\[0-9a-zA-Z]+$/;
	return regex.test(str);
}


function checkMail(mail)
{
	var x = mail;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}


function validName(name)
{
	name = name.trim();

	regex = /^[a-zA-Z][a-zA-Z\s\'\.,\-]+$/
	return regex.test(name);
}

function validPhone(phone)
{
	if(!IsNumeric(phone)) {
		return false;
	}

	phone = phone.trim('0');

	if(phone.length != 11 && phone.length != 10) {
		return false;
	}

	if(phone.charAt(0) != '0') {
		return false;
	}

	if(phone.charAt(1) == '7' && phone.length != 11) {
		return false;
	}

	return true;
}

function validCellPhone(phone)
{
	if(!IsNumeric(phone)) {
		return false;
	}

	phone = phone.trim('0');

	if(phone.length != 11) {
		return false;
	}

	if(phone.substring(0, 2) != '07') {
		return false;
	}

	return true;
}

function IsEmptyField(id)
{
	value = $(id).value;
	return (value.trim() == '');
}
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function handleEnter (field, event)
{
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			checkZip();
			return false;
		} 
		else
		return true;
	}      


var http = getHTTPObject(); // We create the HTTP Object


//#### start inline ################################################################################################ 

	function shid(outurl) 
	{
		document.getElementById('spin').style.visibility = 'visible';
		http.open("GET", outurl, true);
		http.onreadystatechange = shidResponse;
		http.send(null);
	}

	function shidResponse()
	{
		  if (http.readyState == 4) 
		  {
				document.getElementById('incont').innerHTML  = http.responseText;
				 
				document.getElementById('spin').style.visibility = 'hidden';
				//document.getElementById('incont').window.scroll(0,500);
		  }

	}

//#### end inline ################################################################################################


function Select_Value_Set(SelectName, Value) {
  eval('SelectObject = document.frm.' +  SelectName + ';');
  for(index = 0; 
    index < SelectObject.length; 
    index++) {
   if(SelectObject[index].value == Value)
     SelectObject.selectedIndex = index;
   }
}

function preselect_val(ctrl, val) {
	if (document.getElementById(ctrl).value == '') {
		document.getElementById(ctrl).value = val;
	}
	return;
}

function keyRestrict(e, validchars) {
 var key='', keychar='';
 key = getKeyCode(e);
 if (key == null) return true;
 keychar = String.fromCharCode(key);
 keychar = keychar.toLowerCase();
 validchars = validchars.toLowerCase();
 if (validchars.indexOf(keychar) != -1)
  return true;
 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
  return true;
 return false;
}

function getKeyCode(e)
{
 if (window.event)
    return window.event.keyCode;
 else if (e)
    return e.which;
 else
    return null;
}

function validateDate(fld, an1, an2) {
    var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    if ((fld.match(RegExPattern)) && (fld!='')) 
	{
		var mySplitResult = fld.split("/");
		if((parseFloat(mySplitResult[2])>=an1) && (parseFloat(mySplitResult[2])<=an2))
		{ return true; } else { return false; }
	}
	else
	{ return false; }
	
}

function getAge(dobString)
{
	dobParts = dobString.split('/');

	dob = new Date(dobParts[2], dobParts[1]  - 1, dobParts[0]);
	now = new Date();

	return Math.floor((now.getTime() - dob.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
}

function checkPostCode (toCheck) {

  // 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"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // 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();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // 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;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}


//AB345345C
function checkNINO (toCheck) 
{
 
  var re = /\s/g; //Match any white space including space, tab, form-feed, etc. 
  toCheck = toCheck.replace(re, "");

  var exp1 = /^[A-CEGHJ-NOPR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D\s]{1}/i;
  var exp2 = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/i;
	
  if (toCheck.match(exp1) && !toCheck.match(exp2)) 
	  return toCheck.toUpperCase() 
	else
	  return false;
}


function getAge(dobString)
{
	dobParts = dobString.split('/');

	dob = new Date(dobParts[2], dobParts[1]  - 1, dobParts[0]);
	now = new Date();

	return Math.floor((now.getTime() - dob.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
}

