/* 
 * Pao
 */

var XML_Http;
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

function Get_XML_Http_Object() {
    if( window.XMLHttpRequest ) {
        return new XMLHttpRequest();
    }
    if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

function Consultation_Progress() {
    if( XML_Http.readyState<4 ) {
        document.getElementById("pao_lock").style.visibility = "visible";
    } else {
        document.getElementById("pao_lock").style.visibility = "hidden";
        alert('Your information has been submitted. Thank you.');
        document.frmSidebar.reset();
    }
}

function Post_Consultation_Data( strName, strPhone, strEmail, strState, strDebt, strHost ) {
    var url = "dot.php?Form=Consultation&";
    url = url + "Name=" + strName + "&Phone=" + strPhone + "&Email=" + strEmail + "&State=" + strState + "&Debt=" + strDebt + "&Host" + strHost;
    XML_Http = Get_XML_Http_Object();
    XML_Http.onreadystatechange=Consultation_Progress;
    XML_Http.open("GET", url, true);
    XML_Http.send(null);
}

function Sidebar_Check() {
    var doc = document.frmSidebar;
    if( !doc.Name.value || !doc.Phone.value || !doc.States.value || !doc.Amount.value || isNaN(doc.Amount.value) || isNaN(doc.Phone.value) || doc.Phone.value.length!=10 ) {
        alert('Please fill the form correctly.');
    } else {
        Post_Consultation_Data( doc.Name.value, doc.Phone.value, doc.Email.value, doc.States.value, doc.Amount.value, doc.Host.value );
    }
    return false;
}

function Contact_Progress() {
    if( XML_Http.readyState==4 ) {
        alert('Your information has been submitted. Thank you.');
        document.frmContact.reset();
    }
}

function Post_Contact_Data( strName, strEmail, strQuestion, strMessage, strHost ) {
    var url = "dot.php?Form=Contact&";
    url = url + "Name=" + strName + "&Email=" + strEmail + "&Question=" + strQuestion + "&Message=" + strMessage + "&Host" + strHost;
    XML_Http = Get_XML_Http_Object();
    XML_Http.onreadystatechange=Contact_Progress;
    XML_Http.open("GET", url, true);
    XML_Http.send(null);
}

function Contact_Check() {
    var doc = document.frmContact;
    if( !doc.Name.value || !doc.Email.value || reg.test(doc.Email.value)==false || !doc.Question.value || !doc.Message.value ) {
        alert('Please complete and fill the form correctly.');
    } else {
        Post_Contact_Data( doc.Name.value, doc.Email.value, doc.Question.value, doc.Message.value, doc.Host.value );
    }
    return false;
}

function Loan_Progress() {
    if( XML_Http.readyState==4 ) {
        alert('Your information has been submitted. Thank you.');
        document.frmLoan.reset();
    }
}

function Post_Loan_Data( strFirst, strLast, strHome_Value, strLoan_Balance, strProperty_State, strHardship, strMonths_Behind, strAdjustable_Loan, strHome_Phone, strWork_Phone, strEmail, strComments, strNotice, strBankruptcy, strHost ) {
    var url = "dot.php?Form=Loan&";
    url = url + "First=" + strFirst + '&Last=' + strLast+ '&Home_Value=' + strHome_Value + '&Loan_Balance=' + strLoan_Balance + '&Property_State=' + strProperty_State + '&Hardship=' + strHardship + '&Months_Behind=' + strMonths_Behind + '&Adjustable_Loan=' + strAdjustable_Loan + '&Home_Phone=' + strHome_Phone + '&Work_Phone=' + strWork_Phone + '&Email=' + strEmail + '&Comments=' + strComments+ '&Notice=' + strNotice+ '&Bankruptcy=' + strBankruptcy+ '&Host=' + strHost;
    XML_Http = Get_XML_Http_Object();
    XML_Http.onreadystatechange=Loan_Progress;
    XML_Http.open("GET", url, true);
    XML_Http.send(null);
}

function Loan_Check() {
	var noticeOption, bankruptcyOption, i;
	var doc = document.frmLoan;
	for(i=doc.Notice.length-1; i > -1; i--) {
		if(doc.Notice[i].checked) {
			noticeOption = i;
			i = -1;
		}
	}
	
	for(i=doc.Bankruptcy.length-1; i > -1; i--) {
		if(doc.Bankruptcy[i].checked) {
			bankruptcyOption = i;
			i = -1;
		}
	}
	
	if( !doc.First.value || 
		!doc.Last.value || 
		!doc.Home_Value.value || 
		!doc.Loan_Balance.value || 
		!doc.Property_State.value || 
		!doc.Hardship.value || 
		!doc.Months_Behind.value || 
		!doc.Adjustable_Loan.value || 
		!doc.Home_Phone.value || 
		isNaN(doc.Home_Phone.value) || 
		doc.Home_Phone.value.length!=10 || 
		noticeOption==undefined || 
		!bankruptcyOption==undefined ) {
		alert( 'Please fill in all required (*) fields.' );
	} else {
		Post_Loan_Data( doc.First.value, doc.Last.value, doc.Home_Value.value, doc.Loan_Balance.value, doc.Property_State.value, doc.Hardship.value, doc.Months_Behind.value, doc.Adjustable_Loan.value, doc.Home_Phone.value, doc.Work_Phone.value, doc.Email.value, doc.Comments.value, noticeOption, bankruptcyOption, doc.Host.value );
	}
	return false;
}

