﻿var __visiblePage = 0;
var __formDivs = new Array();
__formDivs[0] = "__quotePage0";
__formDivs[1] = "__quotePage1";

function __SubmitQuoteForm(isMultiForm)
{
    var oForm = document.forms.namedItem("aspnetForm");
    var oDiv = null;
    if (oForm && oForm != undefined)
    {
        if (isMultiForm)
        {
            switch (__visiblePage)
            {
                case 0:
                    if (!__ValidateField(oForm.txtSalesPrice, "text_currency", "Sales Price")) return false;
                    if (!__ValidateField(oForm.txtCurBal, "text_currency", "Current Balance")) return false;
                    if (!__ValidateField(oForm.type, "select", "Property Type")) return false;
                    
                    oDiv = document.getElementById(__formDivs[__visiblePage]);
                    oDiv.className = "divHomeHidden";
                    __visiblePage = 1;
                    oDiv = document.getElementById(__formDivs[__visiblePage]);
                    oDiv.className = "divSidebar";
                    
                    __QuoteChangeHeaderGraphic(oForm);
                    return false
                    break;
                    
                case 1:
                    if (!__ValidateField(oForm.txtFirstName, "text_req", "First Name")) return false;
                    if (!__ValidateField(oForm.txtLastName, "text_req", "Last Name")) return false;
                    if (!__ValidateField(oForm.txtEmail, "text_email", "Email")) return false;
                    oForm.action = oForm.controlGetQuoteFormAction.value; //Read form action from /Control/GetQuote.asxc
                    oForm.submit();
                    break;
            }
        }
        else
        {
            if (!__ValidateField(oForm.txtSalesPrice, "text_currency", "Sales Price")) return false;
            if (!__ValidateField(oForm.txtCurBal, "text_currency", "Current Balance")) return false;
            if (!__ValidateField(oForm.type, "select", "Property Type")) return false;
            if (!__ValidateField(oForm.txtFirstName, "text_req", "First Name")) return false;
            if (!__ValidateField(oForm.txtLastName, "text_req", "Last Name")) return false;
            if (!__ValidateField(oForm.txtEmail, "text_email", "Email")) return false;
            oForm.action = oForm.benefitsGetQuoteFormAction.value; //Read form action from /Benefits/Quote.aspx
            oForm.submit();
        }
    }
}
function __ValidateField(oInput, oType, fieldText)
{
    var testValue;
    switch (oType)
    {
        case "text_currency":
            var vlow, vhigh, result;
            vlow = "$1,000.00";      //Low value limit for numeric fields
            vhigh = "$9,999,999.99"; //High value limit for numeric fields
            testValue = parseNumeric(oInput.value);
            if (isNaN(testValue)) {
                alert("The value for " + fieldText + " must be a number.");
                oInput.focus();		
                return false;
            } 
            else 
            {
                result = isNumberInRange( oInput.value, parseNumeric(vlow), parseNumeric(vhigh) );
                if (result == false) {
                    alert("The value for " + fieldText + " must be greater than " + vlow + " and less than " + vhigh + ".");
                    oInput.focus();		
                    return false;
                } 
                else 
                {
                    oInput.value = testValue;
                }
            }
            break;

       case "text_num":
            testValue = parseNumeric(oInput.value);
            if (isNaN(testValue)) {
                alert("The value for " + fieldText + " must be a number.");
                oInput.focus();		
                return false;
            } 
            else 
            {
                oInput.value = testValue;
            }
            break;

        case "text_email":
            testValue = oInput.value;
            var msg = "The value for " + fieldText + " was not a valid email address. Please try again.";
            if (testValue.length == 0)
            {
                alert(msg);
                oInput.focus();		
                return false;
            }
            var index = testValue.indexOf("@");
            if (index <= 0 || index > testValue.indexOf(".")) 
            {
                alert(msg);
                oInput.focus();		
                return false;
            }
            
            break;

        case "text_req":
            testValue = oInput.value;
            if (testValue.length == 0) {
                alert("The value for " + fieldText + " is required.");
                oInput.focus();
                return false;
            } 
            break;
            
        case "date":
            testValue = oInput.value;
            if (testValue.length == 0) {
                alert("The value for " + fieldText + " is required.");
                oInput.focus();
                return false;
            } 
            var re = new RegExp("[0-9]?[0-9]\/[0-9]?[0-9]\/[0-9][0-9][0-9]?[0-9]?", "g");
            if (re.test(testValue) == false)
            {
                alert("The value for " + fieldText + " was not in the proper format. \nAccepted format is: mm/dd/yyyy");
                oInput.focus();
                return false;
            }
            if (isDate(testValue,1) == false)
            {
                alert("The value for " + fieldText + " is not a valid date or is not in the proper format. \nAccepted format is: mm/dd/yyyy");
                oInput.focus();
                return false;
            }
            break;

        case "ssn":
            //DefectID: 1992 - Make SSN not required
            //if (testValue.length == 0) {
            //    alert("The value for " + fieldText + " is required.");
            //    oInput.focus();
            //    return false;
            //} 

            testValue = oInput.value;
            if (testValue.length == 0) {
                return true;
            } 
            var stripped = testValue.replace(/[\(\)\.\-\ ]/g, '');
            //strip out acceptable non-numeric characters
            if (isNaN(stripped)) 
            {
                alert("The value for " + fieldText + " contains illegal characters.");
                oInput.focus();
                return false;
            }
            stripped = parseInt(stripped).toString();
            if (!(stripped.length == 9))
            {
                alert("The value for " + fieldText + " was not in the proper format. Accepted formats are:\n 123456789\n123 45 6789\n123-45-6789");
                oInput.focus();
                return false;
            }
            break;

        case "text_zip":
            testValue = oInput.value;
            if (testValue.length == 0) {
                alert("The value for " + fieldText + " is required.");
                oInput.focus();
                return false;
            } 
            var stripped = testValue.replace(/[\(\)\.\-\ ]/g, '');
            //strip out acceptable non-numeric characters
            stripped = parseInt(stripped).toString();
            if ((!(stripped.length == 5)) && (!(stripped.length == 9)))
            {
                alert("The value for " + fieldText + " is not a valid zip code.");
                oInput.focus();
                return false;
            }
            if (isNaN(stripped)) 
            {
                alert("The value for " + fieldText + " contains illegal characters.");
                oInput.focus();
                return false;
            }
            oInput.value = testValue;
            break;

        case "phone":
            testValue = oInput.value;
            if (testValue.length == 0) {
                alert("The value for " + fieldText + " is required.");
                oInput.focus();
                return false;
            } 
            var stripped = testValue.replace(/[\(\)\.\-\ ]/g, '');
            //strip out acceptable non-numeric characters
            stripped = parseInt(stripped).toString();
            if (!(stripped.length == 10))
            {
                alert("The value for " + fieldText + " was not a valid phone number. Accepted formats are:\n 1234567890\n 123 456 7890\n123-456-7890\n123.456.7890");
                oInput.focus();
                return false;
            }
            if (isNaN(stripped)) 
            {
                alert("The value for " + fieldText + " contains illegal characters.");
                oInput.focus();
                return false;
            }
            oInput.value = testValue;
            break;

        case "select":
            testValue = oInput.value;
            if (testValue == "") {
                alert("Please select a value from the " + fieldText + " drop-down list.");
                oInput.focus();
                return false;
            } 
            break;

        case "radio":
            testValue = null;
            for (var i=0; i<oInput.length; i++)
            {
                if (oInput[i].checked)
                {
                    testValue = oInput[i].value;
                    break;
                }
            }
            if (!testValue) {
                alert("Please select a choice from the " + fieldText + " list.");
                return false;
            } 
            break;
    }
    return true;
}
function parseNumeric(num)
{
	var i;
	var ss = new String(num);
	var testList = new Array(",","$"," ", "%");
	for (i=0; i<(testList.length); i++) {	
		while (ss.indexOf(testList[i],0)>=0) {
			ss = ss.replace(testList[i], "");
		}
	}
	return(parseFloat(ss));
}
function isNumberInRange(num, low, high)
{
    try {
	    var val, vlow, vhigh;
	    val = parseFloat(num);
	    vlow = parseFloat(low);
	    vhigh = parseFloat(high);
	    return((val >= vlow)&(val <= vhigh));
    }
    catch(e) {
        //alert("error");
        return(false);
    }
}
function __QuoteChangeHeaderGraphic(oForm)
{
    var img = oForm;
    while (img && img != undefined)
    {
        if (img.tagName == "IMG" && img.src.indexOf("getaninstantquote.gif") >= 0)
            break;
        img = img.previousSibling;
    }
    
    if (img == null || img == undefined) return;
    
    var src = img.src;
    var index = src.lastIndexOf("/");
    img.src = src.substr(0, index) + "/onemorestep.gif";
    img.style.width = "126px";
    img.style.height = "15px";
}



/**********************************************************************/ 
/*Function name :isDigit(theDigit) */ 
/*Usage of this function :test for an digit */ 
/*Input parameter required:thedata=string for test whether is digit */ 
/*Return value :if is digit,return true */ 
/* else return false */ 
/**********************************************************************/ 
function isDigit(theDigit) 
{ 
    var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

    for (j = 0; j < digitArray.length; j++) 
    {
        if (theDigit == digitArray[j]) 
            return true 
    } 
    return false 

} 
/*************************************************************************/ 
/*Function name :isPositiveInteger(theString) */ 
/*Usage of this function :test for an +ve integer */ 
/*Input parameter required:thedata=string for test whether is +ve integer*/ 
/*Return value :if is +ve integer,return true */ 
/* else return false */ 
/*function require :isDigit */ 
/*************************************************************************/ 
function isPositiveInteger(theString) 
{ 
    var theData = new String(theString) 

    if (!isDigit(theData.charAt(0))) 
        if (!(theData.charAt(0)== '+')) 
            return false 

    for (var i = 1; i < theData.length; i++) 
        if (!isDigit(theData.charAt(i))) 
            return false 
    return true 
} 
/**********************************************************************/ 
/*Function name :isDate(s,f) */ 
/*Usage of this function :To check s is a valid format */ 
/*Input parameter required:s=input string */ 
/* f=input string format */ 
/* =1,in mm/dd/yyyy format */ 
/* else in dd/mm/yyyy */ 
/*Return value :if is a valid date return 1 */ 
/* else return 0 */ 
/*Function required :isPositiveInteger() */ 
/**********************************************************************/ 
function isDate(s,f) 
{
    var a1=s.split("/"); 
    var a2=s.split("-"); 
    var e=true; 
    if ((a1.length!=3) && (a2.length!=3)) 
    { 
        e=false; 
        } 
        else 
        {if (a1.length==3) 
        var na=a1; 
        if (a2.length==3) 
        var na=a2; 
        if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
        { 
            if (f==1) 
            {
                var d=na[1],m=na[0]; 
            } 
            else 
            {
                var d=na[0],m=na[1]; 
            } 
            var y=na[2]; 
            if (((e) && (y<1000)||y.length>4)) 
                e=false 
            if (e) 
            { 
                v=new Date(m+"/"+d+"/"+y); 
                if (v.getMonth()!=m-1) 
                    e=false; 
            } 
        } 
        else 
        { 
            e=false; 
        } 
    } 
    return e 
} 
