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() {
    var theForm = document.forms["lead"];  

    // non empty checks
    var nonEmptyFields = ["FirstName",
                          "LastName"];
    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;
      };
    };

    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.HomePhone.value == "")
    {
        alert("Please enter your home phone number.");
        theForm.HomePhone.focus();
        return false;
    };

    if (theForm.State.selectedIndex == 0)
    {
        alert("Please select your state.");
        theForm.State.focus();
        return false;
    };

    if (theForm.LoanType.selectedIndex == 0)
    {
        alert("Please select a loan type.");
        theForm.LoanType.focus();
        return false;
    };

    if (theForm.LoanAmount.value == "")
    {
        alert("Please enter a loan amount.");
        theForm.LoanAmount.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;
}


/* ======== New landing pages =======*/


function showNext(cond1, cond2, hidden_div){
	if (cond1 == cond2){document.getElementById(hidden_div).className='visible';}
	else {document.getElementById(hidden_div).className='hidden';}
}

function empt(){}

function more(myPage, windowName, myWidth, myHeight, resize, scroll){
	LeftPosition= (screen.width*0.5)-(myWidth*0.5);
	TopPosition= (screen.height*0.4)-(myHeight*0.5);
	window.open(myPage, windowName, 'width='+myWidth+',height='+myHeight+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=' + resize);
}											