var numPointsReturned = 0;

document.cookie = "myseflsubmitpage=";
function setErrorMsg(msg) {
    if (msg == undefined) {
        msg = "&nbsp;";
    }
    document.getElementById("errorMsg").innerHTML = msg;
}
function validateForm(theForm) {
    setErrorMsg();
    var futureyear;    
    if (isNaN(parseInt(theForm.PickupDateMM.value)) || (theForm.PickupDateMM.value) <= 0 || (theForm.PickupDateMM.value) > 12) {
        setErrorMsg("Invalid Pickup Month.  Please correct and submit the form again.");
        theForm.PickupDateMM.focus();
        return false;
    }
    if (isNaN(parseInt(theForm.PickupDateDD.value)) || (theForm.PickupDateDD.value) <= 0 || (theForm.PickupDateDD.value) > 31) {
        setErrorMsg("Invalid Pickup Day.  Please correct and submit the form again.");
        theForm.PickupDateDD.focus();
        return false;
    }
    if (isNaN(parseInt(theForm.PickupDateYYYY.value)) || parseInt(theForm.PickupDateYYYY.value) < 2003 || parseInt(theForm.PickupDateYYYY.value) > 2010) {
        setErrorMsg("Invalid Pickup Year.  Please correct and submit the form again.");
        theForm.PickupDateYYYY.focus();
        return false;
    }
    if  (isNaN(Date.parse(Date(theForm.PickupDateYYYY.value,theForm.PickupDateMM.value,theForm.PickupDateDD.value)))) {
        setErrorMsg("Invalid Date.  Please correct and submit the form again.");
        theForm.PickupDateDD.focus();
        return false;
    }
    futureyear = new Date();
    futureyear.setHours(0);
    futureyear.setSeconds(0);
    futureyear.setMinutes(0);
    futureyear.setMilliseconds(0);
    if (new Date(theForm.PickupDateYYYY.value,theForm.PickupDateMM.value-1,theForm.PickupDateDD.value,0,0,0,0).getTime() < futureyear.getTime()) {
        setErrorMsg("Date must be at least today's date: " + (new Date).toString() + ".  Please correct and submit the form again.");
        theForm.PickupDateDD.focus();
        return false;
    }        
    if (theForm.name == "CarpetQuoteSubmit") {
        if (theForm.EmailAddress.value.length == 0) {
            setErrorMsg("Please enter a valid Email Address and submit the form again.");
            theForm.EmailAddress.focus();
            return false;
        }
        var tmpCount1 = 0;
        var tmpCount2 = 0;
        for (i = 0; i < theForm.EmailAddress.value.length; i++) {
            if (theForm.EmailAddress.value.toString().charAt(i) == "@") { tmpCount1++; }
            if (theForm.EmailAddress.value.toString().charAt(i) == ".") { tmpCount2++; }
        }
        if (tmpCount1 != 1 || tmpCount2 == 0) {
            setErrorMsg("Please enter a valid Email Address and submit the form again.");
            theForm.EmailAddress.focus();
            return false;
        }
        if (theForm.EmailAddress.value.toString().substring(0,1) == "@") {
            setErrorMsg("Please enter a valid Email Address and submit the form again.");
            theForm.EmailAddress.focus();
            return false;
        }
        if (theForm.EmailAddress.value.toString().substring(0,1) == ".") {
            setErrorMsg("Please enter a valid Email Address and submit the form again.");
            theForm.EmailAddress.focus();
            return false;
        }
        var tmp = theForm.EmailAddress.value.toString();
        if ((tmp.substring(tmp.length - 1,tmp.length) == "@") || (tmp.substring(tmp.length - 1,tmp.length) == ".")) {
            setErrorMsg("Please enter a valid Email Address and submit the form again.");
            theForm.EmailAddress.focus();
            return false;
        }
    }
    
    if (!theForm.pointsEntry.checked) {
        if (theForm.OriginZip.value.length < 5) {
            setErrorMsg("Please enter a valid Origin Zip and submit the form again.");
            theForm.OriginZip.focus();
            return false;
        }
        if (theForm.DestinationZip.value.length < 5) {
            setErrorMsg("Please enter a valid Destination Zip and submit the form again.");
            theForm.DestinationZip.focus();
            return false;
        }
        if (theForm.origZipSelectedPoint) {
            if (theForm.origZipSelectedPoint.options.length > 1 &&
                       theForm.origZipSelectedPoint.selectedIndex == 0) {
                setErrorMsg("Please select an Origin Point from the drop down list.");
                theForm.origZipSelectedPoint.focus();
                return false;
            }
        }
        
        if (theForm.destZipSelectedPoint) {
            if (theForm.destZipSelectedPoint.options.length > 1 &&
                       theForm.destZipSelectedPoint.selectedIndex == 0) {
                setErrorMsg("Please select a Destination Point from the drop down list.");
                theForm.destZipSelectedPoint.focus();
                return false;
            }
        }
    } else {            
        if (theForm.origPointSelectedPoint) {
            if (theForm.origPointSelectedPoint.options.length == 0) {
                if (theForm.OriginPoint.value != "" && theForm.OriginPoint.value.length >= 3) {
                    var URLLink = "/seflWebsite/servlet?action=sbmtRateQuote_getPoints" +            
                                  "&output=XML&origDest=O&point=" + theForm.OriginPoint.value +                                  
                                  "&country=U";       
                    if (theForm.OriginStateSelect.selectedIndex != 0) {
                        URLLink += "&state=" + theForm.OriginStateSelect.options[theForm.OriginStateSelect.selectedIndex].text;
                    }
                    numPointsReturned = -1;
                    var ajax = new SeflAjax();
                    ajax.submit(URLLink, validationBuildPointsDropdown);                    
                    return false;           
                } else {
                    setErrorMsg("Please enter a valid Origin Point.");
                    theForm.OriginPoint.focus();
                    return false;
                }
            } else if (theForm.origPointSelectedPoint.options.length > 1 &&
                       theForm.origPointSelectedPoint.selectedIndex == 0) {
                setErrorMsg("Please select an Origin Point from the drop down list.");
                theForm.origPointSelectedPoint.focus();
                return false;
            }
        }
        
        if (theForm.destPointSelectedPoint) {
            if (theForm.destPointSelectedPoint.options.length == 0) {
                if (theForm.DestPoint.value != "" && theForm.DestPoint.value.length >= 3) {
                    var URLLink = "/seflWebsite/servlet?action=sbmtRateQuote_getPoints" +            
                                  "&output=XML&origDest=D&point=" + theForm.DestPoint.value +                                  
                                  "&country=U";
                    if (theForm.DestStateSelect.selectedIndex != 0) {
                        URLLink += "&state=" + theForm.DestStateSelect.options[theForm.DestStateSelect.selectedIndex].text;
                    }
                    numPointsReturned = -1;
                    var ajax = new SeflAjax();
                    ajax.submit(URLLink, validationBuildPointsDropdown);                    
                    return false;           
                } else {
                    setErrorMsg("Please enter a valid Destination Point.");
                    theForm.DestPoint.focus();
                    return false;
                }
            } else if (theForm.destPointSelectedPoint.options.length > 1 &&
                       theForm.destPointSelectedPoint.selectedIndex == 0) {
                setErrorMsg("Please select a Destination Point from the drop down list.");
                theForm.destPointSelectedPoint.focus();
                return false;
            }
        }
    } 
    
    if (!checkCommodities(theForm)) {
        return false;
    }
    if (theForm.Sqyds1.value == "" &&
    theForm.Sqyds2.value == "" &&
    theForm.Sqyds3.value == "" &&
    theForm.Sqyds4.value == "" &&
    theForm.Sqyds5.value == "" &&
    theForm.Sqyds6.value == "" &&
    theForm.Sqyds7.value == "" &&
    theForm.Sqyds8.value == "" &&    
    (theForm.name != "CarpetQuoteSubmit" ||
    (theForm.name == "CarpetQuoteSubmit" && 
    theForm.Sqyds9.value == "" &&
    theForm.Sqyds10.value == "" &&
    theForm.Sqyds11.value == "" &&
    theForm.Sqyds12.value == "" &&
    theForm.Sqyds13.value == "" &&
    theForm.Sqyds14.value == "" &&
    theForm.Sqyds15.value == "" &&
    theForm.Sqyds16.value == "" &&
    theForm.Sqyds17.value == "" &&
    theForm.Sqyds18.value == "" &&
    theForm.Sqyds19.value == "" &&
    theForm.Sqyds20.value == ""))) {
        setErrorMsg("Please enter the square yards and type for at least one detail line.");
        theForm.Sqyds1.focus();
        return false;
    }
    var checkAcc = checkAccessorials(theForm);
    if (checkAcc != null) {
        setErrorMsg(checkAcc);
        return false;
    }
    futureyear = new Date();
    futureyear.setFullYear(futureyear.getFullYear() + 1);
    setErrorMsg("Submitting request...");
    return true;
}

function validationBuildPointsDropdown(_req) {
    var resp = _req.responseXML;
    var origDest = "";
    var theForm = document.forms[0];
    if (resp.getElementsByTagName("origDest").item(0)) {        
        origDest = getValue(resp.getElementsByTagName("origDest")[0].firstChild);

        buildPointsDropdown(_req);
        if (numPointsReturned == 0) {
            if (origDest == "O") {
                setErrorMsg("Please enter a valid Origin Point.");
                theForm.OriginPoint.focus();        
            } else if (origDest = "D") {
                setErrorMsg("Please enter a valid Destination Point.");
                theForm.DestPoint.focus();
            }
        } else if (numPointsReturned == 1) {
            if (validateForm(theForm)) {
                theForm.submit();
            }
        } else if (numPointsReturned > 1) {
            if (origDest == "O") {
                setErrorMsg("Please select an Origin Point from the drop down list.");
                theForm.origPointSelectedPoint.focus();        
            } else if (origDest == "D") {
                setErrorMsg("Please select a Destination Point from the drop down list.");
                theForm.destPointSelectedPoint.focus();
            }    
        }
    }
}    

function updateQueryString(inStr, qString) {        
    var zipSelectBox;
    var pointSelectBox;
    if (qString.substring(0,4) == "Dest") {
        document.getElementById("DestinationZip").value = "";
        pointSelectBox = document.getElementById("destPointSelectedPoint");
        zipSelectBox = document.getElementById("destZipSelectedPoint");
        
        qString += "&point=" + document.getElementById("DestPoint").value;                      
        qString += "&country=U";
        qString += "&state=" + document.getElementById("DestStateSelect").options[document.getElementById("DestStateSelect").selectedIndex].text;
    } else {
        document.getElementById("OriginZip").value = "";
        pointSelectBox = document.getElementById("origPointSelectedPoint");
        zipSelectBox = document.getElementById("origZipSelectedPoint");
        
        qString += "&point=" + document.getElementById("OriginPoint").value;    
        qString += "&country=U";
        qString += "&state=" + document.getElementById("OriginStateSelect").options[document.getElementById("OriginStateSelect").selectedIndex].text;
    }
    pointSelectBox.length = 0;
    zipSelectBox.length = 0;
    pointSelectBox.disabled = true;
    zipSelectBox.disabled = true;
    
    return qString;
}

function parsePointInfo(li) {
        
    var itemParts = li.id.split(";");
    var type = itemParts[0];
    var city = itemParts[1];
    var state = itemParts[2];
    var zip = itemParts[3];
    var zipSelectBox
    var pointSelectBox;
    var stateSelect;
    if (type == "dest") {
        document.getElementById("DestPoint").value = city;
        document.getElementById("DestinationZip").value = zip;
        document.getElementById("DestinationCity").value = city;
        document.getElementById("DestinationState").value = state;
        zipSelectBox = document.getElementById("destZipSelectedPoint");
        pointSelectBox = document.getElementById("destPointSelectedPoint");  
        stateSelect = document.getElementById("DestStateSelect");
    } else {
        document.getElementById("OriginPoint").value = city;
        document.getElementById("OriginZip").value = zip;
        document.getElementById("OriginCity").value = city;
        document.getElementById("OriginState").value = state;
        zipSelectBox = document.getElementById("origZipSelectedPoint");
        pointSelectBox = document.getElementById("origPointSelectedPoint");
        stateSelect = document.getElementById("OriginStateSelect");
    }
    var option = document.createElement("OPTION");
    option.text = city + ", " + state + " " + zip;
    option.value = city + ";" + state + ";" + zip;
    zipSelectBox.options.add(option);   
    option = document.createElement("OPTION");
    option.text = city + ", " + state + " " + zip;
    option.value = city + ";" + state + ";" + zip;
    pointSelectBox.options.add(option);
    if (!stateSelect.disabled) {
        for (var i=0;i<stateSelect.options.length;i++) {
            if (stateSelect.options[i].text == state) {
                stateSelect.selectedIndex = i;
                break;
            }
        }    
    } 
    zipSelectBox.disabled = true;
    pointSelectBox.disabled = true;    
}

function getPointsFromZip(elem) {
    var zipCode;
    var name = elem.name.substring(0,4);       
    var pointSelection1;
    var pointSelection2;    
    var origDest;
    if (name == "Orig") {
        zipCode = document.getElementById("OriginZip").value;        
        pointSelection1 = document.getElementById("origZipSelectedPoint");
        pointSelection2 = document.getElementById("origPointSelectedPoint");
        document.getElementById("OriginPoint").value = "";
        document.getElementById("OriginStateSelect").selectedIndex = 0;
        origDest = "O";
    } else {
        zipCode = document.getElementById("DestinationZip").value;        
        pointSelection1 = document.getElementById("destZipSelectedPoint");
        pointSelection2 = document.getElementById("destPointSelectedPoint");
        document.getElementById("DestPoint").value = "";
        document.getElementById("DestStateSelect").selectedIndex = 0;
        origDest = "D";
    }    
    if (zipCode.length >= 5) {
        var URLLink = "/seflWebsite/servlet?action=sbmtRateQuote_getPoints" +            
        "&output=XML&origDest=" + origDest + "&zipCode=" + zipCode +
        "&country=U";       
        var ajax = new SeflAjax();
        ajax.submit(URLLink, buildPointsDropdown);        
    } else {
        pointSelection1.length = 0;
        pointSelection1.disabled = true;
        pointSelection2.length = 0;
        pointSelection2.disabled = true; 
        numPointsReturned = 0;
    }
}

function buildPointsDropdown(_req) {        
    var zipSelectBox;
    var pointSelectBox;
    var resp = _req.responseXML;    
    if (resp.getElementsByTagName("origDest").item(0)) {
        var type = "Z";
        if (document.getElementById("pointsEntry").checked) {
            type = "P";
        }
        var origDest = getValue(resp.getElementsByTagName("origDest")[0].firstChild);
        var points = resp.getElementsByTagName("pointEntry");       
        if (origDest == "O") {
            zipSelectBox = document.getElementById("origZipSelectedPoint");                                
            pointSelectBox = document.getElementById("origPointSelectedPoint");
        } else {
            zipSelectBox = document.getElementById("destZipSelectedPoint");                
            pointSelectBox = document.getElementById("destPointSelectedPoint");
        }
        if (points.length > 1) {
            if (type == "P") {
                zipSelectBox.disabled = true;
                pointSelectBox.disabled = false;
            } else {
                zipSelectBox.disabled = false;
                pointSelectBox.disabled = true;
            }
        } else {
            zipSelectBox.disabled = true;   
            pointSelectBox.disabled = true;
        }        
        zipSelectBox.length = 0;
        pointSelectBox.length = 0;
        
        numPointsReturned = points.length;
        
        for (var i=0; i<points.length; i++) {            
            var option;
            if (i == 0 && points.length > 1) {
                option = document.createElement("OPTION");
                option.text = "-- Please Select --";
                option.value = "";
                if (type == "P") {
                    pointSelectBox.options.add(option);
                } else {                    
                    zipSelectBox.options.add(option);
                }                
            } else if (points.length == 1) {
                var stateSelect;
                if (origDest == "O") {
                    document.getElementById("OriginState").value = getValue(points[i].childNodes[1].firstChild);
                    document.getElementById("OriginCity").value = getValue(points[i].childNodes[0].firstChild);
                    document.getElementById("OriginPoint").value = getValue(points[i].childNodes[0].firstChild);
                    document.getElementById("OriginZip").value = getValue(points[i].childNodes[2].firstChild);
                    stateSelect = document.getElementById("OriginStateSelect");                    
                } else {
                    document.getElementById("DestinationState").value = getValue(points[i].childNodes[1].firstChild);
                    document.getElementById("DestinationCity").value = getValue(points[i].childNodes[0].firstChild);
                    document.getElementById("DestPoint").value = getValue(points[i].childNodes[0].firstChild);
                    document.getElementById("DestinationZip").value = getValue(points[i].childNodes[2].firstChild);
                    stateSelect = document.getElementById("DestStateSelect");
                }
                for (j=0;j<stateSelect.options.length;j++) {
                    if (stateSelect.options[j].text == getValue(points[i].childNodes[1].firstChild)) {
                        stateSelect.selectedIndex = j;
                        break;
                    }
                }
            }
            if (type == "Z" || points.length == 1) {
                option = document.createElement("OPTION");
                option.text = getValue(points[i].childNodes[0].firstChild) + ", " + getValue(points[i].childNodes[1].firstChild) + " " + getValue(points[i].childNodes[2].firstChild);
                option.value = getValue(points[i].childNodes[0].firstChild) + ";" + getValue(points[i].childNodes[1].firstChild) + ";" + getValue(points[i].childNodes[2].firstChild);
                zipSelectBox.options.add(option);
            }
            if (type == "P" || points.length == 1) {
                option = document.createElement("OPTION");
                option.text = getValue(points[i].childNodes[0].firstChild) + ", " + getValue(points[i].childNodes[1].firstChild) + " " + getValue(points[i].childNodes[2].firstChild);
                option.value = getValue(points[i].childNodes[0].firstChild) + ";" + getValue(points[i].childNodes[1].firstChild) + ";" + getValue(points[i].childNodes[2].firstChild);
                pointSelectBox.options.add(option);
            }
        }
    } else {
        numPointsReturned = 0;
    }
}

function getValue(object, defaultValue) {
    if (defaultValue == null) {
        defaultValue = "";
    }
    var returnValue = defaultValue;
    if (object != null) {
        returnValue = object.nodeValue;
    }
    if (returnValue == "false") {
        returnValue = new Boolean(false).valueOf();
    }
    if (returnValue == "true") {
        returnValue = new Boolean(true).valueOf();
    }
    return returnValue;
}

function updatePoint(elem) {
    var name = elem.name.substring(0,4);
    var type = elem.name.substring(4,5);
    var point = elem.options[elem.selectedIndex].value;
    var city = "";
    var state = "";
    var zip = "";
    var option;
    var stateSelect;
    if (point != "") {
        point = point.split(";");
        city = point[0];
        state = point[1];
        zip = point[2];          
        option = document.createElement("OPTION");
        option.text = city + ", " + state + " " + zip;
        option.value = city + ";" + state + ";" + zip;        
    }
    if (name == "orig") {        
        if (type == "Z") {
            document.getElementById("origPointSelectedPoint").length = 0;
        } else {
            document.getElementById("origZipSelectedPoint").length = 0;
        }
        document.getElementById("OriginPoint").value = city;
        document.getElementById("OriginZip").value = zip;
        document.getElementById("OriginCity").value = city;
        document.getElementById("OriginState").value = state;
        if (city != "") {
            if (type == "Z") {
                document.getElementById("origPointSelectedPoint").options.add(option);            
            } else {
                document.getElementById("origZipSelectedPoint").options.add(option);
            }
        } 
        stateSelect = document.getElementById("OriginStateSelect");
    } else {        
        if (type == "Z") {
            document.getElementById("destPointSelectedPoint").length = 0;
        } else {
            document.getElementById("destZipSelectedPoint").length = 0;
        }
        document.getElementById("DestPoint").value = city;
        document.getElementById("DestinationZip").value = zip;
        document.getElementById("DestinationCity").value = city;
        document.getElementById("DestinationState").value = state;
        if (city != "") {
            if (type == "Z") {
                document.getElementById("destPointSelectedPoint").options.add(option);            
            } else {
                document.getElementById("destZipSelectedPoint").options.add(option);
            }
        }
        stateSelect = document.getElementById("DestStateSelect");
    }
    for (i=0;i<stateSelect.options.length;i++) {
        if (stateSelect.options[i].text == state) {
            stateSelect.selectedIndex = i;
            break;
        }
    }
}

function showHideLayers(elem) {
    var zipLayerValue = "none";
    var pointLayerValue = "none";
    if (elem.checked) {
        pointLayerValue = "block";
    } else {
        zipLayerValue = "block";
    }    
    document.getElementById("pointLayer").style.display = pointLayerValue;
    document.getElementById("zipLayer").style.display = zipLayerValue;         
}

function resetFields(elem) {
    var name = elem.name;    
    if (name.indexOf("Orig") > -1) {        
        document.getElementById("OriginZip").value = "";
        document.getElementById("origZipSelectedPoint").length = 0;
        document.getElementById("origZipSelectedPoint").disabled = true;
        document.getElementById("origPointSelectedPoint").length = 0;
        document.getElementById("origPointSelectedPoint").disabled = true;        
    } else {        
        document.getElementById("DestinationZip").value = "";
        document.getElementById("destZipSelectedPoint").length = 0;
        document.getElementById("destZipSelectedPoint").disabled = true;
        document.getElementById("destPointSelectedPoint").length = 0;
        document.getElementById("destPointSelectedPoint").disabled = true;
    }
}
