
function done(x){
//  alert('done: ' + x.responseText);
};

function wrapPostData(){
  var postData = "";
  var theForm = document.forms["lead"];  
  for(var i=0; i<theForm.length; i++){
    if(theForm[i].type.indexOf('select') != -1 ){
      postData += theForm[i].name + "=" + escape(theForm[i][theForm[i].selectedIndex].value) + "&";
    } else {
      postData += theForm[i].name + "=" + escape(theForm[i].value) + "&";
    };
  };
  return postData;
};


function submitIt() {

  var theForm = document.forms["lead"];  

  if(validate()) {
    //  alert('thank you');
    //    var postData = wrapPostData();
    //    sendRequest("process.asp", done, postData);    
      theForm.submit();
  } else {
    return false;
  };
};

function validate_apply(){
	if (document.getElementById('FirstName').value==''){alert('Please enter your first name');document.getElementById('FirstName').focus(); return false;} 
	if (document.getElementById('LastName').value==''){alert('Please enter your last name');document.getElementById('LastName').focus(); return false;} 
	if (document.getElementById('State').value==''){alert('Please select a state.');document.getElementById('State').focus(); return false;}
	if (document.getElementById('email').value=='email'){alert('Please enter your email address');document.getElementById('email').focus(); return false;} 
	else if(((document.getElementById('email').value.search(exclude) != -1)||(document.getElementById('email').value.search(check)) == -1)||(document.getElementById('email').value.search(checkend) == -1)) {alert('Please enter a valid email address.'); document.getElementById('email').focus(); return false;}
	if (document.getElementById('HomePhone').value==''){alert('Please enter your home phone');document.getElementById('HomePhone').focus(); return false;} 
	if (document.getElementById('LoanType').value==''){alert('Please select a loan type');document.getElementById('LoanType').focus(); return false;} 
	if (document.getElementById('LoanAmount').value==''){alert('Please enter loan amount');document.getElementById('LoanAmount').focus(); return false;} 
	return true;
}


function validate() {

    var theForm = document.forms["lead"];

    // non empty checks
    var nonEmptyFields = ["first_name", "last_name"];
    var nonEmptyMsgs   = ["Please enter your first name.",
                          "Please enter your last name.",];

    for(var i=0; i < nonEmptyFields.length; i++){
      if( theForm[nonEmptyFields[i]].value == "" ) {
        theForm[nonEmptyFields[i]].focus();
        alert(nonEmptyMsgs[i]);
        return false;
      };
    };
	
	if (theForm.address.value == "")
    {
        alert("Please enter your Address.");
        theForm.address.focus();
        return false;
    };
	
	if (theForm.city.value == "")
    {
        alert("Please enter your City.");
        theForm.city.focus();
        return false;
    };

    if (theForm.state.selectedIndex == 0)
    {
        alert("Please select your state.");
        theForm.state.focus();
        return false;
    };

    if (theForm.zip.value == "")
    {
        alert("Please enter your zip code.");
        theForm.zip.focus();
        return false;
    };
	
    var strEmail = theForm.email.value;
    if (strEmail == "")
    {
        alert("Please enter your Email Address.");
        theForm.email.focus();
        return false;
    }
    else if (!isEmail(strEmail))
    {
        alert("Please enter a valid Email Address.");
        theForm.email.focus();
        return false;
    };

    if (theForm.home_phone.value == "")
    {
        alert("Please enter your home phone number.");
        theForm.home_phone.focus();
        return false;
    };	
	
    if (theForm.loan_type.selectedIndex == 0)
    {
        alert("Please select a Loan Type.");
        theForm.loan_type.focus();
        return false;
    };

    if (theForm.loan_amount.value == "")
    {
        alert("Please enter a Loan Amount.");
        theForm.loan_amount.focus();
        return false;
    };

    var ranNum = Math.round(Math.random() * 100000000);
    theForm.Ref_Id.value = ranNum;

    return true;
};

function isEmail(strEmail)
{
	var re = /^([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/
	return (re.test(strEmail));
};

function sendRequest(url,callback,postData) {
  var req = createXMLHTTPObject();
  if (!req) return;
  var method = (postData) ? "POST" : "GET";
  req.open(method,url,true);
  req.setRequestHeader('User-Agent','XMLHTTP/1.0');
  if (postData)
    req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  req.onreadystatechange = function () {
    if (req.readyState != 4) return;
    if (req.status != 200 && req.status != 304) {
      alert('HTTP error ' + req.status);
      return;
    }
    callback(req);
  }
  if (req.readyState == 4) return;
  req.send(postData);
}

var XMLHttpFactories = [
                        function () {return new XMLHttpRequest()},
                        function () {return new ActiveXObject("Msxml2.XMLHTTP")},
                        function () {return new ActiveXObject("Msxml3.XMLHTTP")},
                        function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
  var xmlhttp = false;
  for (var i=0;i<XMLHttpFactories.length;i++) {
    try {
      xmlhttp = XMLHttpFactories[i]();
    }
    catch (e) {
      continue;
    }
    break;
  }
  return xmlhttp;
}

