// JScript File
var HILITECOLOR = "#FFCC33"; //"#EFB549"; //"EE9A04"
var ie = (navigator.appName.indexOf("Explorer") > -1);

function OnChangeFormField(o, hiddenField)
{
    o.style.backgroundColor = (o.value=="")? HILITECOLOR : "";
    
    if (typeof(hiddenField) != "undefined")
    {
        document.getElementById(hiddenField).value = o.value;
    }
}

function OnClickContactPreference()
{
    var oDay = document.getElementById("day");
    var oEvening = document.getElementById("evening");
    
    var value = 0;
    
    value = (oDay.checked) ? oDay.value : 0;
    value = (oEvening.checked) ? (parseInt(value)+parseInt(oEvening.value)) : value;
    
    var o = document.getElementById("ContactPreference");
    o.value = value;
    
    ValidateContactPreference();
}

function SetPhone(id, areacode, phone1, phone2)
{
    var o = document.getElementById(id);
    o.value = document.getElementById(areacode).value + "-" + document.getElementById(phone1).value + "-" + document.getElementById(phone2).value;
}

function OnChangeState(o)
{
    OnChangeDdl(o, "State");
    
    ValidateFormField("ddlState");
}

function OnChangeVeteran(o)
{
    OnChangeDdl(o, "Veteran");

    ValidateFormField("ddlVeteran");
}

function OnChangeInitialInvestment(o) {
    OnChangeDdl(o, "InitialInvestment");
    
    ValidateFormField("ddlInitialInvestment");
    
    SetLeadStastus();
}

function OnChangeDdl(o, id)
{
    var oInitialInvestment = document.getElementById(id);
    oInitialInvestment.value = o.value;
}

function ValidateFormFields()
{
    var isValid = true;
    
    isValid =  ( ValidateFormField("FirstName") & ValidateFormField("LastName") & ValidateFormField("txtStreet") &
                 ValidateFormField("txtCity") & ValidateFormField("ddlState") & ValidateFormField("ZipCode") & ValidateEmail() & ValidateContactPreference() &
                 ValidateFormField("ddlVeteran") & ValidatePhone() & ValidateFormField("ddlInitialInvestment") );
    if (isValid)
    {
        document.forms[0].submit();    
    }
    else
    {
        alert("Please enter a valid format.");
    }
}

function ValidateFormField(fieldname)
{
    var o = document.getElementById(fieldname);
    
    o.style.backgroundColor = "";
    if (o.value == "")
    {
        o.style.backgroundColor = HILITECOLOR;
        return false;
    }
    return true;
}

function ValidatePhone()
{
    var retVal = ( ValidateFormField("areacode") & ValidateFormField("phone1") & ValidateFormField("phone2") );
    
    var o = document.getElementById("areacode");
    if (o.value.length < 3)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }
    
    o = document.getElementById("phone1");
    if (o.value.length < 3)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }
    
    o = document.getElementById("phone2");
    if (o.value.length < 4)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }

    return retVal;
}

function ValidateLength(o, len)
{
    var retVal = true;
    
    if (o.value.length < len)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }

    return retVal;
    
}

function ValidateEmail()
{
    var retVal = ValidateFormField("EmailID");
    
    if (retVal)
    {
        var o = document.getElementById("EmailID");
        document.getElementById("Email").value = o.value;
        
        var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
        var regex = new RegExp(emailReg);
        if (!regex.test(o.value))
        {
            o.style.backgroundColor = HILITECOLOR;
            return false;
        }
    }

    return retVal;
}

function ValidateContactPreference()
{
    var otd = document.getElementById("tdContactPreference");
    
    otd.style.backgroundColor = "";
    
    var o = document.getElementById("ContactPreference");
    
    if (o.value == "0")
    {
        otd.style.backgroundColor = HILITECOLOR;
        
        return false;
    }
    return true;
}

function ValidateQuestion(question)
{

    var otd = document.getElementById("td"+question);
    
    otd.style.backgroundColor = "";
    
    var a = document.getElementById("Ans"+question);
    
    if (a.value == "")
    {
        otd.style.backgroundColor = HILITECOLOR;
        return false;
    }
    
    return true;    
}

function OnClickRadHandler(o)
{
    var a = document.getElementById("Ans"+o.getAttribute("name"));
    a.value = o.value;
    
    var otd = document.getElementById("td"+o.getAttribute("name"));    
    otd.style.backgroundColor = "";
    
    SetLeadStastus();
}

function SetLeadStastus()
{
    var o = document.getElementById("LeadStatus");
    
    //var a1 = document.getElementById("Ansq1");
    //var a2 = document.getElementById("Ansq2");
    //var a3 = document.getElementById("Ansq3");
    //var a4 = document.getElementById("Ansq4");
    
    
    //o.value = ( (a1.value=="1") && (a2.value=="1") && (a3.value=="1") && (a4.value=="1") )? "RFI Qualified" : "RFI Unqualified";
}

function OnChangeStateHandler(o, hiddenId, ddlCountyId) 
{
    var state = o.value.replace(/ /g, "");
    
    document.getElementById(hiddenId).value = state;
    
    var oDdlCounty = document.getElementById(ddlCountyId);
    for (var i = oDdlCounty.options.length - 1; i > 0; i--) {
        oDdlCounty.remove(i);
    }
    j = 1;
    for (var i = 0; i < eval(state + ".length"); i++) {
        var county = eval(state + "[" + i + "]");
        oDdlCounty[j] = new Option(county, county);
        j++;
    }
}

function OnChangeCountyHandler(o, hiddenId) {
    var county = o.value.replace(/ /g, "");

    document.getElementById(hiddenId).value = county;
}

function SetAdditionalCoapplicant(value)
{
    document.getElementById('AdditionalCoapplicant').value = value; 
    document.getElementById('tdOtherCoApplicant').style.display = (value=='No') ? 'none' : '';
    document.getElementById('trCoApplicantInfo').style.display = (value=='No') ? 'none' : '';
    if (value=="Yes")
    {
        OnChangeTotalCoApplicants(document.getElementById('txtOtherCoApplicantNumber'));
    }
}

function OnChangeTotalCoApplicants(o)
{
    if (isNaN(o.value) || (o.value == ""))
    {
        o.value = 1;
    }
    var total = parseInt(o.value);
    total = (total>10)? 10: total;
    o.value = total;
    
    document.getElementById("trCoApplicantInfo").style.display = "none";
    if (total == 0)
    {
        return;
    }
    
    document.getElementById("trCoApplicantInfo").style.display = "";
    for (var i=1; i<11; i++)
    {
        document.getElementById("trCoApplicant"+i).style.display = "none";
    }
    for (var i=1; i<total+1; i++)
    {
        document.getElementById("trCoApplicant"+i).style.display = "";
    }
    
}

function AllowNumericOnly(o)
{
	if(/[^0-9]/.test(o.value))
	{
		o.value=o.value.replace(/([^0-9])/g,"");
	}
}

function AllowNumericAndDash(o)
{
	if(/[^0-9-]/.test(o.value))
	{
		o.value=o.value.replace(/([^0-9-])/g,"");
	}
}

function ddlSelectedItemByValue(oDdl, value) {
    for (var i = 0; i < oDdl.options.length; i++) {
        if (oDdl.options[i].value == value) {
            oDdl.options[i].selected = true;
            break;
        }
    }
}

function ddlGetSelectedItemValue(oDdl) {
    var value = "";
    for (var i = 0; i < oDdl.options.length; i++) {
        if (oDdl.options[i].selected) {
            value = oDdl.options[i].value;
            break;
        }
    }
    return value;
}

function radioGetSelectedValue(radioname)
{
    var retVal = "";
    var radioGroup = eval("document.forms[0]." + radioname);
    for (var i=0; i < radioGroup.length; i++) 
    {
        if (radioGroup[i].checked) 
        {
            retVal = radioGroup[i].value;
            break;
        }
    }
    return retVal;
}

function GetXmlHttpObject() {
    var objXMLHttp = null;
    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        var aVersions = new Array();
        aVersions[0] = "MSXML2.XMLHttp.6.0";
        aVersions[1] = "MSXML2.XMLHttp.5.0";
        aVersions[2] = "MSXML2.XMLHttp.4.0";
        aVersions[3] = "MSXML2.XMLHttp.3.0";
        aVersions[4] = "MSXML2.XMLHttp";
        aVersions[5] = "Microsoft.XMLHttp";
        for (var i = 0; i < aVersions.length; i++) {
            try {
                objXMLHttp = new ActiveXObject(aVersions[i]);
                break;
            }
            catch (e) { }
        }
    }
    if (!objXMLHttp) {
        alert('Cannot create objXMLHttp instance');
        return false;
    }
    else {
        return objXMLHttp;
    }
}
