// JavaScript Document
var EMAIL="Please enter Email Address"
var VALID_EMAIL = "Please provide us valid Email Address"
var CONFEMAIL = "Please enter confirmation Email Address"
var VALID_CONFEMAIL = "Please provide us valid confirmation Email Address"
var POST_CODE="Plese enter the Post Code"
var VPOST_CODE="Please Enter Valid PostCode"
var VALID_YEARS="Please select Year at Address"
var VALID_MONTHS="Please select Month at Address"
var DOB="Please select the day as in your Date of Birth"
var DOM="Please select the day as in your Month of Birth"
var DOY="Please select the day as in your Year of Birth"
var VALID_AMOUNT="Amount should be Numeric"
var ADDRESS="Please Enter the House number and Street"
var V_ADDRESS="Please provide valid the House number and Street"
var TOWN="Please Enter the Town/City"
var V_TOWN="Please provide valid the Town/City"
var TEL="Please Provide us your Telephone number"
var V_TEL="Please Provide us valid Telephone number"
var MOB="Please Provide us your Mobile number"
var V_MOB="Please Provide us valid Mobile number"
var JOB="Please provide valid Job/Occupation Title"
var INCOME="Please fill in your income with numbers only"
var tnt=0


var IE4 = document.all? true : false;
var refSite=document.URL;

function fn_tenant()
{ 
//alert(IE4)
tnt=tnt+1;

	formContents="?page="+refSite;
	
	t=newConfirm("Loan Confirmation","Are You looking for Debt Management ??",1,1,0)
	if(t) 
	{	for(i=0; i<IE4.length; i++)
		{
		   if(IE4[i].name!=null && IE4[i].value!=null)
		   formContents=formContents+"&"+IE4[i].name
		}
		location.href="http://www.yourfinance.co.uk/yourfinance/debt-consolidation.asp"+formContents 
		
		//location.href="Debt Management.htm"+formContents 
	}	
}
function newConfirm(title,mess,icon,defbut,mods)
{   if (IE4) {
      icon = (icon==0) ? 0 : 2;
      defbut = (defbut==0) ? 0 : 1;
      retVal = makeMsgBox(title,mess,icon,4,defbut,mods);
      retVal = (retVal==6);
   }
   else {
      retVal = confirm(mess);
   }
   return retVal;
}
joint="no";
var dge=document.getElementById;
function go1(vall)
{
stat=vall
//alert(stat)
if (stat =='yes') 
 cl_expcol('jointmarried',true);
else  cl_expcol('jointmarried',false);
 
}
function cl_expcol(a,b){
  frm=document.frmindex;
  
  if(!dge)return;
    if(b)document.getElementById('jointInfo').style.display = 'block';
	else document.getElementById('jointInfo').style.display = 'none';
}

function chkHomeowner()
{
		frm=document.frmindex;
	if (frm.hometype.value=="Tenant")
		{
		fn_tenant();
		document.getElementById('propertyadd').style.display='none';
		}
	else
		document.getElementById('propertyadd').style.display='block';

}

function chkTel(txtfld, mes)
{
	
	var txtString= fnTrim(txtfld.value);
	telPhNo=txtString.length;
	
	if((telPhNo<10)||(telPhNo>11) )
	{
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	
	return false;
		
}

// remove special characters like "$" and "," etc...
function AllowNumericLoanChk() {
document.frmindex.AmtRequired.value=filterNum(document.frmindex.AmtRequired.value)
function filterNum(str) {
var  re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$/g;
return str.replace(re, "");
}
}


function fnvalidate() {
	frm=document.frmindex;
    ch=checkPostCode(frm.Postcode.value);
	ch1=checkPostCode(frm.prevpostcode.value);
    dob=CheckDOBforThirteen(frm.dob1_day.value,frm.dob1_month,frm.dob1_year)
		
	if(dropDownMenu(frm.hometype,"Please indicate whether you are a Homeowner or not"))
		return false;	
	if(fnIsNull(frm.AmtRequired,"Please enter your Loan Amount."))
			return false;
	if(AllowNumeric(frm.AmtRequired,"Not a valid amount."))
			return false;	
	if(AllowNumericLoanChk())
		    return false;					
	if(checkAmount())
		    return false;			
	if(fnNumberOnly(frm.AmtRequired,"Not a valid amount."))
			return false;		
	
	if(!frm.checkbox_home.checked && !frm.checkbox_debt.checked)
		{
			if(otherPurpose())
			return false;	
		}
		
	if(dropDownMenu(frm.Status,"Please indicate your Marital Status"))
		{
			return false;
		}
	if(fnIsNull(frm.Fname1,"Please fill in your First Name"))
		return false;	
	if(fnIsValidChar1(frm.Fname1,"Please fill in your First Name properly"))
		return false;
	if(fnIsNull(frm.Lname1,"Please fill in your Last Name"))
		return false;	
	if(fnIsValidChar1(frm.Lname1,"Please fill in your Last Name properly"))
		return false;
	if(dropDownMenu(frm.dob1_day,DOB))
		 return false;
	if(dropDownMenu(frm.dob1_month,DOM))
		 return false;
	if(dropDownMenu(frm.dob1_year,DOY))
		 return false;
	if (dob)
		{
			alert('You Should be Above Thirteen');frm.dob1_day.focus();
			return false;
		}

	if(fnIsNull(frm.Email,EMAIL))
		return false;	
	if(fnIsQuotes(frm.Email,VALID_EMAIL))
		return false;				
	if(emailcheck(frm.Email,VALID_EMAIL))
		return false;
	if(fnIsNull(frm.conf_email,CONFEMAIL))
		return false;	
	if(fnIsQuotes(frm.conf_email,VALID_CONFEMAIL))
		return false;				
	if(emailcheck(frm.conf_email,VALID_CONFEMAIL))
		return false;
	if(fnTrim(frm.Email.value) != fnTrim(frm.conf_email.value))
		{
			alert("Email and confirmation dont match");
			frm.conf_email.focus();
			return false;
		}
	if(fnIsNull(frm.Add1,ADDRESS))
		return false;
	if(fnIsQuotes(frm.Add1,V_ADDRESS))
		return false;
	if(fnIsNull(frm.town,TOWN))
		return false;
	if(fnIsQuotes(frm.town,V_TOWN))
		return false;
	if(fnIsNull(frm.Postcode,POST_CODE))
		return false;
	if(fnIsQuotes(frm.Postcode,VPOST_CODE))
		return false;	
	if(!ch){alert(VPOST_CODE);frm.Postcode.focus();frm.Postcode.select();
		return false;
		}
	if(dropDownMenu(frm.hlYears,VALID_YEARS))
		 return false;
	if(dropDownMenu(frm.hlMonths,VALID_MONTHS))
		 return false;
		
	if (frm.hlYears.value<=3 )
 		{
 			if(fnIsNull(frm.prevaddress,"Please provide previous address") )
				return false;
			if(fnIsNull(frm.prevpostcode,POST_CODE))
				return false;
			if(fnIsQuotes(frm.prevpostcode,"Postcode is not valid"))
				return false; 
			if(!ch1)
			{
				alert(VPOST_CODE);frm.prevpostcode.focus();frm.prevpostcode.select();
				return false;
			}
 
		if(fnIsNull(frm.Telephone,TEL))
			return false;
		if(AllowNumeric(frm.Telephone,V_TEL))
			return false;		
		if(fnNumberOnly(frm.Telephone,V_TEL))
			return false;	
		if(chkTel(frm.Telephone,V_TEL))
			return false;	
		if(fnNonZero(frm.Telephone,V_TEL))
			return false;
		if(fnTelNonZero(frm.Telephone,V_TEL))
			return false;		
		if (fnIsQuotes(frm.mnumber,MOB))
			return false;
		if (fnIsValidChar2(frm.mnumber,V_MOB))
			return false;	
		if(dropDownMenu(frm.besttime,"Please specify when you would like to speak to our executive"))
			return false;
		if(fnJoint1(frm.jointApp, "Please Provide us is it Joint Application?"))	
   			 return false;		
		if(dropDownMenu(frm.FmpStatus,"Please enter your employment status"))
			return false;
		if (fnIsQuotes(frm.occupation1,JOB))
			return false;
		if(fnIsValidChar1(frm.occupation1,"Please provide us Valid Occupation/Job Title"))
			return false;	
		
		if(fnIsNull(frm.income1,"Please fill in your income"))
			return false;	
		if(fnNumberOnly(frm.income1,INCOME))
			return false;
		if(dropDownMenu(frm.incomerange1,"Please select your income details"))
			return false;
		
		if (frm.hometype.value=="Home Owner" )	
		{
			if(fnIsNull(frm.houseworth,"Please enter your houseworth"))
				return false;
			if(fnNumberOnly(frm.houseworth,VALID_AMOUNT))
				return false;
			if(fnNonZero(frm.houseworth,"Estimated property value is not valid"))
				return false;	
			if(fnIsNull(frm.mtglender,"Please provide us Mortgage Lender"))
 				 return false;
			if(fnIsNull(frm.mortgageOutstanding,"Please Provide us your Mortgage Outstanding"))
				return false;	
			if(fnNumberOnly(frm.mortgageOutstanding,"Mortgage Outstanding is not valid"))
				return false;	
			if(fnIsNull(frm.mortgagePayment,"Please Provide us your Mortgage Payment"))
				return false;	
			if(fnNumberOnly(frm.mortgagePayment,"Mortgage Payment is not valid"))
				return false;
			if(dropDownMenu(frm.PropConstruction,"Please provide Property Construction"))
				return false;
			if(fnIsNull(frm.OrigPurchasePrice,"Please provide Original price"))
				return false;	
			if(fnNumberOnly(frm.OrigPurchasePrice,"Original price is not valid"))
				return false;
			if(fnNonZero(frm.OrigPurchasePrice,"Original price is not valid"))
				return false;		
		} 	
 	}
	
	if(fnIsNull(frm.Telephone,TEL))
			return false;
		if(AllowNumeric(frm.Telephone,V_TEL))
			return false;		
		if(fnNumberOnly(frm.Telephone,V_TEL))
			return false;		
		if(chkTel(frm.Telephone,V_TEL))
			return false;	
		 if(fnNonZero(frm.Telephone,V_TEL))
			return false;
		if(fnTelNonZero(frm.Telephone,V_TEL))
			return false;		
		if (fnIsQuotes(frm.mnumber,MOB))
			return false;
		if (fnIsValidChar2(frm.mnumber,V_MOB))
			return false;	
		if(dropDownMenu(frm.besttime,"Please specify when you would like to speak to our executive"))
			return false;
		if(fnJoint1(frm.jointApp, "Please Provide us is it Joint Application?"))	
   			 return false;		
		if(dropDownMenu(frm.FmpStatus,"Please enter your employment status"))
			return false;
		if (fnIsQuotes(frm.occupation1,JOB))
			return false;
		if(fnIsValidChar1(frm.occupation1,"Please provide us Valid Occupation/Job Title"))
			return false;	
		if(fnIsNull(frm.income1,"Please fill in your income"))
			return false;	
		if(fnNumberOnly(frm.income1,INCOME))
			return false;
		if(dropDownMenu(frm.incomerange1,"Please select your income details"))
			return false;
		
		if (frm.hometype.value=="Home Owner" )	
		{
			if(fnIsNull(frm.houseworth,"Please enter your houseworth"))
				return false;
			if(fnNumberOnly(frm.houseworth,VALID_AMOUNT))
				return false;
			if(fnNonZero(frm.houseworth,"Estimated property value is not valid"))
				return false;	
			if(fnIsNull(frm.mtglender,"Please provide us Mortgage Lender"))
 				 return false;
			if(fnIsNull(frm.mortgageOutstanding,"Please Provide us your Mortgage Outstanding"))
				return false;	
			if(fnNumberOnly(frm.mortgageOutstanding,"Mortgage Outstanding is not valid"))
				return false;	
			if(fnIsNull(frm.mortgagePayment,"Please Provide us your Mortgage Payment"))
				return false;	
			if(fnNumberOnly(frm.mortgagePayment,"Mortgage Payment is not valid"))
				return false;
			if(dropDownMenu(frm.PropConstruction,"Please provide Property Construction"))
				return false;
			if(fnIsNull(frm.OrigPurchasePrice,"Please provide Original price"))
				return false;	
				
			if(fnNumberOnly(frm.OrigPurchasePrice,"Original price is not valid"))
				return false;
			if(fnNonZero(frm.OrigPurchasePrice,"Original price is not valid"))
				return false;		
		} 	
}

// Checks if the input string is null or blanks

function fnIsNull(txtfld, mes)
{
	   var string=txtfld.value;
       if (fnTrim(string) == null || fnTrim(string) == "" )
       {
	   	alert(mes);
		txtfld.focus();
		txtfld.select();
     	return true;
       }
		return false;
}

// Trims the input string of leading and trailing spaces and returns the new string

function fnTrim(string)
{
	var cnt;	
	len = string.length;
	str = string;
	begin = -1;
	for(cnt=0;cnt<len;cnt++)
	{
		if (str.charAt(cnt) == " ")
		{	
			begin = cnt;
		}	
		else
		break;
		
	}
	str = str.slice(begin+1,len);
	len = str.length;
	end = len;
	for(cnt=len-1;cnt>=0;cnt--)
	{
		if (str.charAt(cnt) == " ")
		{	
			end = cnt;
		}	
		else
		break;
	}
	str = str.slice(0,end);
	return str;
}

//chech whether its numeric or not
function AllowNumeric(textfield, msg)
{
var val=textfield.value;
	if (val.length>0){
		var charBag = '<>/?;:"~`!#$%^&*()=+\|[]{}abcdefghijklmnopqrstuvwxyz';
		//loop for not allowed special chars
		for (j=0;j<charBag.length; j++){
			if (val.indexOf(charBag.charAt(j))==0){
				alert(msg);
				textfield.focus();
				textfield.select();
				return true;
			}
		}
	}
	return false;
}


function Add_Previous()
{
	frm=document.frmindex;
	if (frm.hlYears.value<=3 && frm.hlYears.options[0].selected!=true)
document.getElementById('prevadd').style.display='block';
else
document.getElementById('prevadd').style.display='none';
}
//-------------------------------------
function dropDownMenu(menu, mes) 
   {
   var myindex=menu.selectedIndex;
   if (myindex==0)
      {
      alert(mes);
      menu.focus();
	  return true;
	  }
	else 
		{
		return false;
		}
   }

//..............................................................................
//To check for special characters.

function fnIsValidChar1(txtfld, mes)
{
	spChar = "`~!@#$%^&*()_-+=|\\}}{{\"':;<,>.?/123456789";
	var txtString= fnTrim(txtfld.value);
	for(var i=0; i<txtString.length; i++)
	{
	if(spChar.indexOf(txtString.charAt(i))!= -1)
	{
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	}
	return false;
		
}

//To check single and double quotes.

function fnIsQuotes(txtfld, mes){
	
	var a_strString = fnTrim(txtfld.value)
	if(a_strString.indexOf('"') != -1)
	  {
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	if(a_strString.indexOf("'") != -1)
	{
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	
	return false
}

//to check email id

function emailcheck(txtfld, mes)
{
var str;
var string1=txtfld.value;
 str = "";
if (string1.indexOf("@")==-1){
	str = "NO";}
else if (string1.indexOf("@")==0){
	str = "NO";}
else if (string1.indexOf(".")==-1){
	str = "NO";}
else if (string1.indexOf(".")==0){
	str = "NO";}
else if (string1.indexOf("@") + 1 == string1.indexOf(".")){
	str = "NO";}
else if (string1.indexOf("@") + 1== string1.length){
	str = "NO";}
else if(string1.lastIndexOf(".") + 1== string1.length){
	str = "NO";}
else if(string1.indexOf("@") != string1.lastIndexOf("@")){
	str = "NO";}

else{
	str = "YES";}

if (str=="NO"){	
	alert(mes);
	txtfld.select();
	txtfld.focus();
	return true;
	}
	else{
		return false;}
}
//TYo check joint account

function fnJoint1(chekbox)
{
	
	 var a=0;
	 for (var i = 0; i<chekbox.length; i++)
	  {
      checked =chekbox[i].checked;
      if (checked)
	   {
         val=chekbox[i].value;
		 a=a+1;		
		 if (val=="yes")
		 {
         
			frm=document.frmindex
			if(fnIsNull(frm.Fname2,"Please fill in Joint Applicants First Name"))
				return true;	
			else if(fnIsValidChar1(frm.Fname2,"Please fill in Joint Applicants First Name properly"))
				return true;
			else if(fnIsNull(frm.Lname2,"Please fill in Joint Applicants Last Name"))
				return true;	
			else if(fnIsValidChar1(frm.Lname2,"Please fill in Joint Applicants Last name Name properly"))
				return true;
			else if(dropDownMenu(frm.dob2_day,"Please select the day as in Joint Applicants Date of Birth"))
				 return true;
			else if(dropDownMenu(frm.dob2_month,"Please select the month as in Joint Applicants Date of Birth"))
				 return true;
			else if(dropDownMenu(frm.dob2_year,"Please select the year as in Joint Applicants Date of Birth"))
			 return true;
			else if (dob)
			{
				alert('Applicant Should be Above Thirteen');frm.dob2_day.focus();
				return true;
			}
			else if(dropDownMenu(frm.employment2,"Please enter  Joint Applicants employment status"))
				return true;
			else if (fnIsQuotes(frm.occupation2,JOB))
				return true;
			else if(fnIsValidChar1(frm.occupation2,"Please provide us Valid Occupation/Job Title"))
				return true;	
			else if(fnIsNull(frm.income2,"Please fill in Joint Applicants income"))
				return true;		
			else if(fnNumberOnly(frm.income2,"Please fill in  Joint Applicants income with numbers only"))
				return true;
			else if(dropDownMenu(frm.incomerange2,"Please select  Joint Applicants income details"))
				return true;
					
		 }
	   }
	   }
	     if(a>0)
	   {
	 return false;
	   }
	    else
		 {
	   alert(mes);
	   return true;
	    }
}
//..............................................................................
//To check for special characters.

function fnIsValidChar2(txtfld, mes)
{
	spChar = "`~!@#$%^&*()_-+=|\\}}{{\"' :;<>?/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ£";
	var txtString= fnTrim(txtfld.value);
	for(var i=0; i<txtString.length; i++)
	{
	if(spChar.indexOf(txtString.charAt(i))!= -1)
	{
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	}
	return false;
		
}

   //Allow Numbers only to enter
function fnNumberOnly(txtfld, mes)
{
	var re5digit=/^\d+$/; if(txtfld.value.search(re5digit)==-1){alert(mes);txtfld.focus(); txtfld.select(); return true;}//if match failed
}

function fnNonZero(txtfld, mes)
{var val=txtfld.value;
if(val==0){
	alert(mes);txtfld.focus(); txtfld.select(); return true;//if match failed
}
}

//Check other purpose

function otherPurpose(){
	frm=document.frmindex;
	if(fnIsNull(frm.checkbox_other,"Please fill in  Other purpose")){
				frm.checkbox_other.focus();
				return true;
	}
	else if(fnIsValidChar1(frm.checkbox_other,"Please fill in valid Other purpose")){
				frm.checkbox_other.focus();
				return true;
	}
	else
	return false;
}



//Check amount

function checkAmount(){
	frm=document.frmindex;
	if(frm.AmtRequired.value<5000){
		alert("Our loans start at £5000 ")
		frm.AmtRequired.focus();
		return true;
	}
	else if(frm.AmtRequired.value>100000){
		alert("We can give loans only upto £100000")
		frm.AmtRequired.focus();
		return true;
	}
	else
	return false;
}


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);

  // 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 true;} else return false;
}
//Telephone Number
function fnTelNonZero(txtfld,mes)
{	
	var nonZeroDigit=/0{7}|1{10}|2{10}|3{10}|4{10}|5{10}|6{10}|7{10}|8{10}|9{10}/;
	if(txtfld.value.search(nonZeroDigit)!=-1){alert(mes);txtfld.focus(); txtfld.select(); return true;}
}

function CheckDOBforThirteen(nDay,nMonth,nYear){
  var dNow = new Date();
  dNow.setFullYear(dNow.getFullYear()-13);
  dNow.setHours(0,0,0,0); //remove time, we only need the date part
  var dBday = new Date(nYear, nMonth-1, nDay);//alert(dNow+'\n'+dBday);//test

  return (dBday-dNow==0) ? true:false;
}

var referingtest;
referingtest= document.referrer;
document.frmindex.teting.value=referingtest;