//var bValidating = false;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


//used to check email field validation.
function is_email(email)
{
	if(!email.match(/^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+$/))
		return false;
	return true;
}
function IsNumeric(strString)
   //  check for valid numeric strings 
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   if (strString.length == 0) return false;
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
return blnResult;
}

function IsAlpha(strString)
   //  check for valid numeric strings 
   {
   var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ";
   var strChar;
   var blnResult = true;
   if (strString.length == 0) return false;
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
return blnResult;
}

function ismobile(string) {
  if (string.length !=10)
      return false;
   return true;
}

function ismobile3(string) {
  if (string.length !=3)
      return false;
   return true;
}
function ismobile4(string) {
  if (string.length !=4)
      return false;
   return true;
}

function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {

return false;
   }
}
return true;
}



function validate(){
/*	if(bValidating) {
		alert("Please wait, your zip code and phone are being validated");
		return false;
	}*/
	if(document.appl_frm.debt_amount.selectedIndex == 0){
	alert('Please select your mortgage balance.');
	document.appl_frm.debt_amount.focus();
	return false;
	}
	if(document.appl_frm.property_value.selectedIndex == 0){
	alert('Please select your property value.');
	document.appl_frm.property_value.focus();
	return false;
	}
	if(document.appl_frm.property_type.selectedIndex ==''){
	alert('Please select your property type.');
	document.appl_frm.property_type.focus();
	return false;
	}
	if(document.appl_frm.loan_type.selectedIndex ==''){
	alert('Please select your Loan purpose.');
	document.appl_frm.loan_type.focus();
	return false;
	}
	
	if(document.appl_frm.bankruptcy.selectedIndex ==''){
	alert('Please select your bankruptcy status.');
	document.appl_frm.bankruptcy.focus();
	return false;
	}	
	if(document.appl_frm.foreclosure.selectedIndex ==''){
	alert('Please select your foreclosure status.');
	document.appl_frm.foreclosure.focus();
	return false;
	}
	
	if(document.appl_frm.consumer_credit.selectedIndex ==''){
	alert('Please select your credit status.');
	document.appl_frm.consumer_credit.focus();
	return false;
	}
	if(document.appl_frm.fname.value==''){
	alert('Please enter your first name.');
	document.appl_frm.fname.focus();
	return false;
	}
	if (IsAlpha(document.appl_frm.fname.value) == false) 
	{
	alert("Please enter a valid first name (letters only: no numbers, or special characters)");
	document.appl_frm.fname.value="";
	document.appl_frm.fname.focus();
	return false;
    }
	if(document.appl_frm.lname.value==''){
	alert('Please enter your last name.');
	document.appl_frm.lname.focus();
	return false;
	}
	if (IsAlpha(document.appl_frm.lname.value) == false) 
	{
	alert("Please enter a valid first name (letters only: no numbers, or special characters)");
	document.appl_frm.lname.value="";
	document.appl_frm.lname.focus();
	return false;
    }
	if(document.appl_frm.state.selectedIndex==0){
	alert('Please select your state.');
	document.appl_frm.state.focus();
	return false;
	}	
	
	if (validateZIP(document.appl_frm.zip.value) == false) {
	//alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
	document.appl_frm.zip.value="";
	document.appl_frm.zip.focus();
	return false;
    }
	
	if(document.appl_frm.zip.value==''){
	alert('Please enter your zip code.');
	document.appl_frm.zip.focus();
	return false;
	}
	
	if(document.appl_frm.email.value==''){
	alert('Please enter your email address.');
	document.appl_frm.email.focus();
	return false;
	}
	else if(!is_email(document.appl_frm.email.value))
	{
	alert("Invalid email-id.");		
	document.appl_frm.email.focus();
	return false;
	}
	
	if(document.appl_frm.tel1.value==''){
	alert('Please enter your home phone area code.');
	document.appl_frm.tel1.focus();
	return false;
	}
	if (IsNumeric(document.appl_frm.tel1.value) == false) 
	{
	alert("Please enter valid home phone."+ '\n' +" (numbers only: no dashes, spaces, or brackets)");
	document.appl_frm.tel1.value="";
	document.appl_frm.tel1.focus();
	return false;
    }
	if (ismobile3(document.appl_frm.tel1.value) == false) {
	alert("Please enter valid home phone."+ '\n' +"Numbers only. No dashes, spaces, or brackets.");
	document.appl_frm.tel1.value="";
	document.appl_frm.tel1.focus();
	return false;
    }
	if(document.appl_frm.tel2.value==''){
	alert('Please enter your home phone code.');
	document.appl_frm.tel2.focus();
	return false;
	}
	if (IsNumeric(document.appl_frm.tel2.value) == false) 
	{
	alert("Please enter valid home phone."+ '\n' +" (numbers only: no dashes, spaces, or brackets)");
	document.appl_frm.tel2.value="";
	document.appl_frm.tel2.focus();
	return false;
    }
	if (ismobile3(document.appl_frm.tel2.value) == false) {
	alert("Please enter valid home phone."+ '\n' +"Numbers only. No dashes, spaces, or brackets.");
	document.appl_frm.tel2.value="";
	document.appl_frm.tel2.focus();
	return false;
    }
	
	if(document.appl_frm.tel3.value==''){
	alert('Please enter your home phone.');
	document.appl_frm.tel3.focus();
	return false;
	}
	if (IsNumeric(document.appl_frm.tel3.value) == false) 
	{
	alert("Please enter valid home phone."+ '\n' +" (numbers only: no dashes, spaces, or brackets)");
	document.appl_frm.tel3.value="";
	document.appl_frm.tel3.focus();
	return false;
    }
	if (ismobile4(document.appl_frm.tel3.value) == false) {
	alert("Please enter valid home phone."+ '\n' +"Numbers only. No dashes, spaces, or brackets.");
	document.appl_frm.tel3.value="";
	document.appl_frm.tel3.focus();
	return false;
	
	//document.appl_frm.Day_Phone.value = document.appl_frm.tel1.value + "" + document.appl_frm.tel2.value + "" + document.appl_frm.tel3.value;
	
	}
	if(document.appl_frm.cel1.value==''){
	alert('Please enter your cell phone area code.');
	document.appl_frm.cel1.focus();
	return false;
	}
	if (IsNumeric(document.appl_frm.cel1.value) == false) 
	{
	alert("Please enter valid cell phone."+ '\n' +" (numbers only: no dashes, spaces, or brackets)");
	document.appl_frm.cel1.value="";
	document.appl_frm.cel1.focus();
	return false;
    }
	if (ismobile3(document.appl_frm.cel1.value) == false) {
	alert("Please enter valid cell phone."+ '\n' +"Numbers only. No dashes, spaces, or brackets.");
	document.appl_frm.cel1.value="";
	document.appl_frm.cel1.focus();
	return false;
    }
	
		if(document.appl_frm.cel2.value==''){
	alert('Please enter your cell phone area code.');
	document.appl_frm.cel2.focus();
	return false;
	}
	if (IsNumeric(document.appl_frm.cel2.value) == false) 
	{
	alert("Please enter valid cell phone."+ '\n' +" (numbers only: no dashes, spaces, or brackets)");
	document.appl_frm.cel2.value="";
	document.appl_frm.cel2.focus();
	return false;
    }
	if (ismobile3(document.appl_frm.cel2.value) == false) {
	alert("Please enter valid cell phone."+ '\n' +"Numbers only. No dashes, spaces, or brackets.");
	document.appl_frm.cel2.value="";
	document.appl_frm.cel2.focus();
	return false;
    }
	
	if(document.appl_frm.cel3.value==''){
	alert('Please enter your cell phone.');
	document.appl_frm.cel3.focus();
	return false;
	}
	if (IsNumeric(document.appl_frm.cel3.value) == false) 
	{
	alert("Please enter valid cell phone."+ '\n' +" (numbers only: no dashes, spaces, or brackets)");
	document.appl_frm.cel3.value="";
	document.appl_frm.cel3.focus();
	return false;
    }
	if (ismobile4(document.appl_frm.cel3.value) == false) {
	alert("Please enter valid cell phone."+ '\n' +"Numbers only. No dashes, spaces, or brackets.");
	document.appl_frm.cel3.value="";
	document.appl_frm.cel3.focus();
	return false;
    }
	
	//document.appl_frm.Cell_Phone.value = document.appl_frm.cel1.value + "" + document.appl_frm.cel2.value + "" + document.appl_frm.cel3.value;
	
	
	if(document.appl_frm.security_code.value==''){
	alert('Please enter your security code.');
	document.appl_frm.security_code.focus();
	return false;
	}

	//document.dieform.action="proc_contact.php";
//gbValidate();

}

function gbValidate() {
	bValidating = true;
	$("#valid").fadeIn(300);
	params = {"zip":document.appl_frm.zip.value, "state":document.appl_frm.state.value, "what":"adr"};
	$.post("http://www.mortgagerefinancing.com/valid.php", params, function(answer) {
		if(answer!="1") {
			if(answer=="-1") alert("It seems like the zip code you entered is either invalid or doesn't match the state you have selected.  Please correct it and resubmit this form. ");
			else alert("Unknown error: "+answer);
			bValidating = false;
			$("#valid").fadeOut(300);
		} else {
			tel1 = document.appl_frm.tel1.value + document.appl_frm.tel2.value + document.appl_frm.tel3.value;
			tel2 = document.appl_frm.cel1.value + document.appl_frm.cel2.value + document.appl_frm.cel3.value;
			params = {"tel":tel1, "cel":tel2, "what":"num"};
			$.post("http://www.mortgagerefinancing.com/valid.php", params, function(answer) {
				if(answer!="1") {
					if(answer=="-1") alert("Incorrect phone numbers");
					else if(answer=="tel-1") alert("It seems like the home phone number you have entered is invalid.  Please check the number and resubmit this form.");
					else if(answer=="cel-1") alert("It seems like the cell phone number you have entered is invalid.  Please check the number and resubmit this form.");
					else alert("Unknown error: "+answer);
				} else {
					setTimeout("gbContinue();",100);
					
				}
				bValidating = false;
				$("#valid").fadeOut(300);
				
			});
		}
	}, "html");

	return false;
}

function gbContinue() {
document.appl_frm.submit();

}


var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}



