// JavaScript Document
var EMAIL="Please enter Email Address"
var VALID_EMAIL = "Please provide us valid Email Address"
var POST_CODE="Please enter the Post Code"
var VPOST_CODE="Please enter Valid Post Code"
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 Contact number"
var V_TEL="Please Provide us valid Contact number"
var STEL="Please Provide us your alternate Contact number"
var V_STEL="Please Provide us valid alternate Contact 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;


var dge=document.getElementById;



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;
		
}

function AllowNumericChk() {
document.frmindex.propertyworth.value=filterNum(document.frmindex.propertyworth.value)
function filterNum(str) {
var  re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$/g;
return str.replace(re, "");
}
}


function fnvalidatecallbk() {
	frm=document.frmcallbk;

	if(fnIsNull(frm.fullname,"Please enter your Name"))
		return false;	
		
		
	if(fnIsValidChar1(frm.fullname,"Please enter your Name properly"))
		return false;

	if(fnIsNull(frm.phonenumber,TEL))
			return false;
	if(AllowNumeric(frm.phonenumber,V_TEL))
			return false;		
	if(fnNumberOnly(frm.phonenumber,V_TEL))
			return false;	
	if(chkTel(frm.phonenumber,V_TEL))
			return false;	
	if(fnNonZero(frm.phonenumber,V_TEL))
			return false;
	if(fnTelNonZero(frm.phonenumber,V_TEL))
			return false;		
	

		
document.frmcallbk.submit();
	
}
function fnvalidateclaim() {
	
	frm=document.frmclaim;
	
	if(fnIsNull(frm.firstname,"Please enter your Forename"))
		return false;	
	if(fnIsValidChar1(frm.firstname,"Please enter your Forename properly"))
		return false;
			if(fnIsNull(frm.surname,"Please enter your surname"))
		return false;	
	if(fnIsValidChar1(frm.surname,"Please enter your surname properly"))
		return false;
		
		
		  		   if(fnIsNull(frm.homephonenumber,"Please Provide us your Home Telephone number"))
			return false;
	if(AllowNumeric(frm.homephonenumber,"Please Provide us your valid Home Telephone number"))
			return false;		
	if(fnNumberOnly(frm.homephonenumber,"Please Provide us your valid Home Telephone number"))
			return false;	
	
				
			if(dropDownMenu(frm.day_of_birth,"Please Day of birth") )
		return false;
				if(dropDownMenu(frm.month_of_birth,"Please Select month of birth") )
		return false;
				if(dropDownMenu(frm.year_of_birth,"Please Select year of birth") )
		return false;
		
	
		
document.frmclaim.submit();
	
}



// 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 dropDownMenu(menu, mes) 
   {
   var myindex=menu.selectedIndex;
   if (myindex==0 || myindex==-2)
      {
      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 ||spChar.indexOf(txtString.charAt(i))==" ")
	{
	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;}
}

//..............................................................................
//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
}
}


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;
}
//Check percentage
function calculatePercent(){
	frm=document.frmindex;
	var per=frm.propertyworth.value;
	if(frm.AmtRequired.value>0.9*per){
		alert("Sorry, we can Offer Remortgage for amount only upto 90% of the property value");
		frm.AmtRequired.focus();
		return true;
	}
	else
	return false;
}


//Check amount

function checkAmount(){
	frm=document.frmindex;
	if(frm.propertyworth.value>999999){
		alert("Sorry, we cant Offer Remortgage for this amount")
		frm.propertyworth.focus();
		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;}
}
