// JavaScript Document
var VPOST_CODE="Please enter valid postcode";
var VEmail="Please enter valid Email Id";
var VTELEPHONE="Please enter valid Telephone number";
var VMOB="Please enter valid Mobile number";
var POST_CODE="Plese enter the Post Code"
var VPOST_CODE="Please Enter Valid PostCode"

var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

var  ns_r=document.referrer;
var  ns_t=document.URL;

if (ns_t!="")
{
	ns_t=ns_t.replace(/&/g,"^^")
}
var  ns_pg="eastman_track.asp?referrer="+ns_r+"&pname="+ns_t;
ns_track_img = new Image();  
ns_track_img.src=ns_pg;


function preValidate(srcObj,dataType) {
	// This function prevalidates and changes user input to match
	// what we require.
	if (!srcObj || !dataType) return;
	
	switch(dataType) {
		case "currency":
			srcObj.value = srcObj.value.replace(/[^\d\.]+/g,'');
			break;
		case "numeric":
			// Remove any non-numeric character (0-9) in the field
			srcObj.value = srcObj.value.replace(/[^0-9\ ]/g,'');
			break;
		case "ucase":
			srcObj.value = srcObj.value.replace(/[a-z]/g,'$&').toUpperCase();
			break;
		case "alphabet":
			//Remove any non-alphabetic characters in the fiels
			srcObj.value=srcObj.value.replace(/[^a-zA-Z\ ]/g,''); 
	}
}

// This array defines which variables depend on others for their "requiredness".
// Some fields on the form only need to be filled in as the result of selections 
// made on other options, and we only need this information IF the user has selected
// pre-defined options





function showMissingFields(ivFlds) {
	// This function basically just removes duplicate entries in the array and formats
	// it for display in an alert box.
	if (!ivFlds) return "";
	
	var tmpFlds = new Array();
	ivFlds.sort();
	for (i=0;i<ivFlds.length;i++) {
		if (ivFlds[i] == ivFlds[i+1])
			continue;
		tmpFlds[tmpFlds.length] = "- "+ivFlds[i];
	}
				
	ivFlds = tmpFlds.join("\n");
	return ivFlds;
}	
function maritalStatus(input)
{var subinput;
	subinput=input.split(" ");
	//alert(subinput[0]);
	if(input == "Married" || subinput[0] == "Living")
	{
	document.getElementById("maritStatus").style.display="";
	}
	else
	document.getElementById("maritStatus").style.display="none";
}
function validateForm() {
	var elm, fldName;
	var myForm = document.everForm;
	var isValid = true, fldValid, optValid;
	var subinput;
	var input;
	
	var ivFlds = new Array();
	var igFlds = new Array();
	
	if (!myForm) return;
	
	// We check for required fields through a bastardisation of the "Title" attribute of
	// HTML tags.  Each form field has a title tag, and part of this title may specify
	// the string "(REQUIRED)".  We look for this string and if found check to see whether
	// the user has supplied data for it.  If not, we prompt them.
	for (i=0;i<myForm.elements.length;i++) {
		elm = myForm.elements[i];		
		if (elm.title.indexOf("(REQUIRED)") > 0) {
			// This field is required, find out what it is
			fldName = elm.title.replace(/ \(REQUIRED\)/,"");
			fldValid = true;			
			if (elm.type == 'select-one' || elm.type == 'select') {
				if (elm.options.selectedIndex == 0 ) {
					isValid = false;
					fldValid = false;
					ivFlds.push(fldName);					
				}
			} else if (elm.type == 'text') {
				if (elm.value.length < 1) {
					isValid = false;
					fldValid = false;
					ivFlds.push(fldName);
				}
			} else if (elm.type == 'checkbox') {
				// Assume it has to be checked if its a required field
				if (!elm.checked) {
					isValid = false;
					fldValid = false;
					ivFlds.push(fldName);
				}
			} else if (elm.type == 'textarea') {
				if (elm.value.length < 1) {
					isValid = false;
					fldValid = false;
					ivFlds.push(fldName);
				}
			} else if (elm.type == 'radio') {
				// Radio buttons are a bit more complicated as there can be several of them with
				// the same name on the form, and we only need to check that one of them (from a 
				// group) has been selected.
				if (checkPreReqs(elm.name)) {
					// Loop through all named elements of the same type and only invalidate if 
					// none of them are set
					optValid = false;
					
					optFlds = document.getElementsByName(elm.name);
					for (j=0;j<optFlds.length;j++) {
						if (optFlds[j].checked) {
							optValid = true;
							break;
						}
					}
					if (!optValid) {
						isValid = false;
						fldValid = false;
						ivFlds.push(fldName);
					}
				}
			} 
			
			// Toggle CSS class (redness) based on field required status
			if (!fldValid) {
				elm.className = ((elm.className.indexOf("Req") > 0) ? elm.className : elm.className+"Req");
			} else {
				elm.className = ((elm.className.indexOf("Req") > 0) ? elm.className.substr(0,elm.className.length-3) : elm.className);
			}
		}
	}
	
	// Show error message if one or more fields are invalid
	if (!isValid) {
		alert("One or more required fields have not been completed.\nPlease go back and enter details for all fields\nmarked in RED:\n\n"+showMissingFields(ivFlds)+" "+showExtraMessages(myForm));
	return false;
	}				 
	
	else if(emailcheck(myForm.email,"Please enter valid email id"))
		return false;	 	
	 
	
	//var dts=myForm.visit
	//if (!isDate(dts.value)){
	//	dts.focus();
	//	return false;
	//}
	
	if(fnTrim(myForm.details.value).length<10 ){
		
		alert("Please Tell Us More About Details of Services Required");
		return false;	 	
	}
	
	//else if(myForm.email.value != myForm.cemail.value)
		//alert("Please enter ");
		//return false;	
		
		if(document.getElementById('referrer'))
		{
			document.getElementById('referrer').value = ns_r;
			
		}
		if(document.getElementById('pname'))
		{
			document.getElementById('pname').value = ns_t;
			
		}
		
		if(document.getElementById('visitdate'))
		{
			
			document.getElementById('visitdate').value = document.getElementById('monthdropdown').value+"/"+document.getElementById('yeardropdown').value;
		}
		
		
	if (isValid ) {
		document.everForm.action='sendmail.php';
		document.everForm.submit();
		
	}
	
	
}



function checkPhone(fld,mes)
{
	var x = fld.value;
	var filter  =/^\d{10,11}$/;
	if (!filter.test(x)) 
	{
	alert(mes);
	fld.select();
	}
	
	
	else
	return false;
	
	return true;
	
}

// 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;
}

// Extra validations and corresponding messages 

function showExtraMessages(myForm)
	{
		Extramsg='';
		var myForm = document.everForm;
		if(document.getElementById("lff_Title"))
			{ 
			//alert(document.getElementById('lff_Title').value);
			if(document.getElementById("lff_Title").value == 'Mrs')
			{
				if(fnTrim(document.getElementById("lff_MaidenName").value) == '')
					{
						Extramsg='\n- Please fill in your Maiden Name \n';
						myForm.lff_MaidenName.className='txtBoxReq';
						}
				}
			else
				{
					myForm.lff_MaidenName.className='txtBox';
					}
			}
		
		return Extramsg;
		}



function checkEmail(txtFld){
	 if(emailcheck(txtFld,"Please enter valid email id for "+txtFld.title))
		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;}
}

//Phone number validation
function phoneCheck(txtFld){
	if(fnTelNonZero(txtFld,"Please enter valid telephone number for "+txtFld.title))
			return false;	
	else if(chkTel(txtFld,"Please enter valid telephone number for "+txtFld.title))
			return false;
}

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 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 postcodeCheck(txtFld){
	var ch1=checkPostCode(txtFld.value);
	if(!ch1){
		alert(VPOST_CODE);
		txtFld.focus();
		txtFld.select();
		return false;
	}
}
//Postcode validation
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;
}
// 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;
}
//-------------------------------------
function dropDownMenu(menu, mes) 
   {
   var myindex=menu.selectedIndex;
   if (myindex==0)
      {
      alert(mes);
      menu.focus();
	  return true;
	  }
	else 
		{
		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
}
var dge=document.getElementById;
function go1(vall)
{
stat=vall
//alert(stat)
if (stat =='yes') 
 cl_expcol('lff_AdditionalIncome',true);
else  cl_expcol('lff_AdditionalIncome',false);
 
}
function cl_expcol(a,b){
  frm=document.everForm;
  
  if(!dge)return;
    if(b)document.getElementById('hc_mortgage').style.display = 'none';
	else document.getElementById('hc_mortgage').style.display = 'block';
}

// Check box

function valButton(chekbox,mes) {
   //alert(chekbox.value);
   var a=0;
	 
	 for (var i = 0; i<chekbox.length; i++)
	  {
		  if(chekbox.value!="" || chekbox.value!=null){
			   checked =chekbox[i].checked;
      if (checked)
	   {
         val=chekbox[i].value;
		 
		 alert(val);
		 a=a+1;		
		 }
		  }
     
	  }
	     if(a>0)
	   {
	 return false;
	   }
	    else
		 {
	   alert(mes);
	   
	   return true;
	    }
}
  // radio button check
  function get_radio_value()
{var check;
   check=0;
   var rad_val;
for (var i=0; i < document.everForm.lff_debtStatus.length; i++)
   {
   
   if (document.everForm.lff_debtStatus[i].checked)
      {
      rad_val = document.everForm.lff_debtStatus[i].value;
	  check=check+1;
      }
   }
   if(check>0){
	   return false;
   } else {
   alert(rad_val);
   return true;
   }
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true;
}



