var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 11;

var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
var postexclude=/[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {1}[0-9][A-Za-z]{2}/;
var phoneexclude=/[0-9]{10,11}/;

function aG()
{
    var a=new Object();var aa=document.location.search.substring(1);var ab=aa.split("&");for(var ac=0;ac<ab.length;ac++){var ad=ab[ac].indexOf("=");if(ad==-1)continue;var ae=ab[ac].substring(0,ad);var af=ab[ac].substring(ad+1);af=af.replace(/\+/g," ");a[ae]=_unesc(af)}return a
}

function _gel(a) {return document.getElementById ? document.getElementById(a) : null}
//get querystring
function _esc(a)
{
    return window.encodeURIComponent?encodeURIComponent(a):escape(a)
}

function blnCheckEmail(pstrEmail)
{
	var k, intFirstAtSignPos, intFirstDotPos, strLastDomainInAddress
	var strValidChar = new String("abcdefghijklmnopqrstuvwxyz1234567890-_.@");
	pstrEmail = Trim(pstrEmail);
	for (k = 0; k <= pstrEmail.length; k++)
		if (strValidChar.indexOf((pstrEmail.substr(k, 1)).toLowerCase()) == -1) return false;
	if (pstrEmail.indexOf("..") >= 0) return false;		
	intFirstAtSignPos = pstrEmail.indexOf("@")
	if(intFirstAtSignPos < 1) return false;
	if (pstrEmail.indexOf("@", (intFirstAtSignPos+1)) >= 0) return false;
	intFirstDotPos = pstrEmail.indexOf(".")
	if(intFirstDotPos < 0) return false;
	if (pstrEmail.indexOf("_", intFirstAtSignPos) != -1) return false;
	strLastDomainInAddress = pstrEmail.substr((pstrEmail.lastIndexOf(".") + 1))
	if (strLastDomainInAddress.length < 2 || strLastDomainInAddress.length > 4) return false;
	return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function checkInternationalPhone(strPhone)
{
	s = stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function Trim(StrValue)
{
  var st = StrValue;
  while ((st.length > 0) && (st.charAt(0) == " ")) 
	st = st.substring(1, st.length);
  while ((st.length > 0) && (st.charAt(st.length-1) == " "))    
    st = st.substring(0, st.length-1);  
  return st;
}

function CheckTelNo(pstrTelNo)
{
	pstrTelNo = Trim(pstrTelNo);
	var IsValid = true;
	
	if (pstrTelNo.length >= 4)
	{
		if (IsValid && pstrTelNo.substring(0, 4) == "0704")
			IsValid = false;
		if (IsValid && pstrTelNo.substring(0, 1) != "0")
			IsValid = false;
		if (IsValid && (pstrTelNo.substring(0, 3) != "020" && pstrTelNo.substring(0, 3) != "023" && pstrTelNo.substring(0, 3) != "024" && pstrTelNo.substring(0, 3) != "028" && pstrTelNo.substring(0, 3) != "029" && pstrTelNo.substring(0, 2) != "07" && pstrTelNo.substring(0, 2) != "08" && pstrTelNo.substring(0, 2) != "01") )
			IsValid = false;
	}

	return IsValid;
}


function ValidateForm()
{
	var form = document.getElementById("aspnetForm");
	with (form)
	{
		if (tName.value == "")
		{
			alert("Please enter your name");
			tName.focus();
		}
		else if (((tMobile.value==null)||(tMobile.value==""))&&((tPhone.value==null)||(tPhone.value=="")))
		{
			alert("Please enter either a telephone or mobile number")
			tPhone.focus()
		}
		else if (((tPhone.value!=null)&&(tPhone.value!=""))&&checkInternationalPhone(tPhone.value)==false)
		{
			alert("Please enter a valid phone number - must be 11 digits")
			tPhone.value=""
			tPhone.focus()
		}
		else if (!CheckTelNo(tPhone.value))
		{
			alert("Please enter a valid phone number");
			tPhone.focus();
		}
		else if (((tMobile.value!=null)&&(tMobile.value!=""))&&checkInternationalPhone(tMobile.value)==false)
		{
			alert("Please enter a valid phone number - must be 11 digits")
			tMobile.value=""
			tMobile.focus()
		}
		else if (!CheckTelNo(tMobile.value))
		{
			alert("Please enter a valid phone number");
			tMobile.focus();
		}
		else if (tLoanAmount.value == "")
		{
			alert("Please enter the required loan amount");
			tLoanAmount.focus();
		}
		else if (ddlLoanPurpose.value == "0")
		{
			alert("Please select the loan purpose");
			ddlLoanPurpose.focus();
		}
		else if (cTerms.checked != true )
		{
			alert("Please confirm that you agree to our terms & conditions");
			return false;
		}
		else
		{
			return true;
		}
	}
	return false;
}

function ValidateMainForm()
{
	var form = document.getElementById("aspnetForm");
	with (form)
	{
		if (tName.value == "")
		{
			alert("Please enter your name");
			tName.focus();
		}
		else if (tDay.value == "")
		{
			alert("Please enter your day of birth");
			tDay.focus();
		}
		else if (tMonth.value == "")
		{
			alert("Please enter your month of birth");
			tMonth.focus();
		}
		else if (tYear.value == "")
		{
			alert("Please enter your year of birth");
			tYear.focus();
		}
		else if (((tMobile.value==null)||(tMobile.value==""))&&((tPhone.value==null)||(tPhone.value=="")))
		{
			alert("Please enter either a telephone or mobile number")
			tPhone.focus()
		}
		else if (((tPhone.value!=null)&&(tPhone.value!=""))&&checkInternationalPhone(tPhone.value)==false)
		{
			alert("Please enter a valid phone number - must be 11 digits")
			tPhone.value=""
			tPhone.focus()
		}
		else if (!CheckTelNo(tPhone.value))
		{
			alert("Please enter a valid phone number");
			tPhone.focus();
		}
		else if (((tMobile.value!=null)&&(tMobile.value!=""))&&checkInternationalPhone(tMobile.value)==false)
		{
			alert("Please enter a valid phone number - must be 11 digits")
			tMobile.value=""
			tMobile.focus()
		}
		else if (!CheckTelNo(tMobile.value))
		{
			alert("Please enter a valid phone number");
			tMobile.focus();
		}
		else if (tEmail.value == "")
		{
			alert("Please enter your email address");
			tEmail.focus();
		}
		else if (ddlMarried.value == "0")
		{
			alert("Please select your marital status");
			ddlMarried.focus();
		}
		else if (rblJointApp[0].checked != true && rblJointApp[1].checked != true )
		{
			alert("Please select if it is a joint application");
			return false;
		}
		else if (ddlAccomodation.value == "0")
		{
			alert("Please select your accomodation type");
			ddlAccomodation.focus();
		}
		else if (tAddress.value == "")
		{
			alert("Please enter your address");
			tAddress.focus();
		}
		else if (tTown.value == "")
		{
			alert("Please enter your town");
			tTown.focus();
		}
		else if (tPostcode.value == "")
		{
			alert("Please enter your postcode");
			tPostcode.focus();
		}
		else if (ddlTimeYears.value == "0")
		{
			alert("Please select how long you've been at this address");
			ddlTimeYears.focus();
		}
		else if (ddlTimeMonths.value == "0")
		{
			alert("Please select how long you've been at this address");
			ddlTimeMonths.focus();
		}
		else if (ddlEmploymentType.value == "0")
		{
			alert("Please select your employment type");
			ddlEmploymentType.focus();
		}
		else if (tMonthlyIncome.value == "")
		{
			alert("Please enter your monthly income");
			tMonthlyIncome.focus();
		}
		else if (tLoanAmount.value == "")
		{
			alert("Please enter the required loan amount");
			tLoanAmount.focus();
		}
		else if (ddlLoanPurpose.value == "0")
		{
			alert("Please select the loan purpose");
			ddlLoanPurpose.focus();
		}
		else if (cTerms.checked != true )
		{
			alert("Please confirm that you agree to our terms & conditions");
			return false;
		}
		else
		{
			return true;
		}
	}
	return false;
}

function SwapIn()
{
            var tempSrc, tempExt
            tempSrc = window.event.srcElement.src;
            tempExt = tempSrc.substring(tempSrc.length-4)
            window.event.srcElement.src = tempSrc.substring(0,tempSrc.length-4) + '01' + tempExt;
}

function SwapOut()
{
            var tempSrc, tempExt
            tempSrc = window.event.srcElement.src;
            tempExt = tempSrc.substring(tempSrc.length-4)
            window.event.srcElement.src = tempSrc.substring(0,tempSrc.length-6) + tempExt;
}

function show(id) {
var el;
if (typeof document.all != 'undefined') el = document.all(id);
else if (typeof document.getElementById != 'undefined') el = document.getElementById(id);
if (el && el.style) el.style.display = '';
}

function hide(id) {
var el;
if (typeof document.all != 'undefined') el = document.all(id);
else if (typeof document.getElementById != 'undefined') el = document.getElementById(id);
if (el && el.style) el.style.display = 'none';
}

function onlyNumbers(evt)
{
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) 
     return false;
     return true;
}

function onlyAlpha(evt)
{
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    if ((charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122) && (charCode < 45 || charCode > 45) && (charCode < 32 || charCode > 32))
     return false;
     return true;
}
