﻿var sent = 0;
var buttonHtml;
var req;
var reqShip;
var response;
var city;
var state;
var secureURL;
var shipCarrier;
var calculatedSalesTax = 0;
var calculatedSalesSet = false;


function setCountry(country, theForm, field) {


    if (country == "Canada") {
        if (field.indexOf("ship") >= 0) {
            eval(theForm + ".shipstate.selectedIndex = 1");
        }
        else {
            eval(theForm + ".billstate.selectedIndex = 1");

        }
        eval(theForm + "." + field + ".value = '" + country + "'");
    }

    if (country == "USA") {
        if (field.indexOf("ship") >= 0) {
            eval(theForm + ".shipotherprovence.selectedIndex = 0");
            if (eval(theForm + ".shipstate.selectedIndex != 1")) {
                eval(theForm + "." + field + ".value = '" + country + "'");
            }
        }
        else {
            eval(theForm + ".otherprovence.selectedIndex = 0");
            if (eval(theForm + ".billstate.selectedIndex != 1")) {
                eval(theForm + "." + field + ".value = '" + country + "'");
            }
        }
    }
}




function copyValues() {
    if (document.form2.shipinfo.checked == true) {
        document.form2.shipadd1.value = document.form2.billadd1.value;
        document.form2.shipadd2.value = document.form2.billadd2.value;
        document.form2.shipzip1.value = document.form2.billzip1.value;
        document.form2.shipcity.value = document.form2.billcity.value;
        document.form2.shipstate.selectedIndex = document.form2.billstate.selectedIndex;
        document.form2.shipphone.value = document.form2.billphone.value;
        document.form2.shipfax.value = document.form2.billfax.value;
        document.form2.shipotherprovence.selectedIndex = document.form2.otherprovence.selectedIndex;
        document.form2.shipcountry.selectedIndex = document.form2.billcountry.selectedIndex;
        document.form2.shipattn.value = document.form2.fname.value + ' ' + document.form2.lname.value;
    }
    else {
        document.form2.shipotherprovence.selectedIndex = 0;
        document.form2.shipadd1.value = '';
        document.form2.shipadd2.value = '';
        document.form2.shipzip1.value = '';
        document.form2.shipcity.value = '';
        document.form2.shipstate.selectedIndex = 0;
        document.form2.shipcountry.selectedIndex = 0;
        document.form2.shipphone.value = '';
        document.form2.shipfax.value = '';
        document.form2.shipattn.value = '';
    }

    
}


function loadXMLDoc(url, shipType) {
    // branch for native XMLHttpRequest object
    if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            if (shipType == "ship") {

                req.onreadystatechange = processReqChangeShip;
            }
            else {

                req.onreadystatechange = processReqChange;
            }


            req.open("GET", url, true);
            req.send();
        }
    }

    else if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();

        if (shipType == "ship") {
            req.onreadystatechange = processReqChangeShip;
        }
        else {
            req.onreadystatechange = processReqChange;
        }

        req.open("GET", url, true);
        req.send(null);
        // branch for IE/Windows ActiveX version
    }


}

//
function loadShipXMLDoc(url) {

    // branch for native XMLHttpRequest object
    if (window.ActiveXObject) {
        isIE = true;
        reqShip = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqShip) {
            reqShip.onreadystatechange = processReqChangeFunctionality;
            reqShip.open("GET", url, true);
            reqShip.send();
        }
    }

    else if (window.XMLHttpRequest) {
        reqShip = new XMLHttpRequest();
        reqShip.onreadystatechange = processReqChangeFunctionality;
        reqShip.open("GET", url, true);
        reqShip.send(null);
        // branch for IE/Windows ActiveX version
    }


}



//JL AJAX Billing postal lookup
function processReqChange() {

    if (req.readyState == 4) {
        if (req.status == 200) {
            response = req.responseXML.documentElement;
            //if the elements exist then populate, otherwise, blank the values out 
            try {
                city = response.getElementsByTagName('city')[0].firstChild.data;
                state = response.getElementsByTagName('state')[0].firstChild.data;
                country = response.getElementsByTagName('CountryCode')[0].firstChild.data;

                //N.F. ticket 421 added a check for TaxVal, as for editcustomer.aspx it was causing the city/state and country not populating (element doesn't exists there)
                //12/22/2008 N.F. add check for null
                //if (document.getElementById("TaxVal")!=="undefined" )
                var tempVar = document.getElementById("TaxVal");
                if (tempVar != 'undefined' && tempVar != null) {
                    calculatedSalesTax = response.getElementsByTagName('TaxVal')[0].firstChild.data;
                }

                calculatedSalesSet = true;

                if (country == 'CA') {
                    document.form2.billcity.value = city;
                    document.form2.billstate.selectedIndex = 1;
                    document.form2.otherprovence.value = state;
                    document.form2.billcountry.value = "Canada";
                    setSalesTax();
                }
                else if (country == 'US') {

                    document.form2.billcity.value = city;
                    document.form2.billstate.value = state;
                    document.form2.billcountry.value = "USA";
                    document.form2.otherprovence.selectedIndex = 0;
                    setSalesTax();

                }
            }
            catch (error) {
                //alert(error.description);
                document.form2.billcity.value = "";
                document.form2.billstate.selectedIndex = 0;
                document.form2.billcountry.selectedIndex = 0;
                setSalesTax();
            }

        }
        else {
            alert("Please enter a valid zip code:\n" +
                  req.statusText);
        }
    }
}

//DDS AJAX Shipping lookup and postal lookup
function processReqChangeShip() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            response = req.responseXML.documentElement;

            //if the elements exist then populate, otherwise, blank the values out 
            try {
                city = response.getElementsByTagName('city')[0].firstChild.data;
                state = response.getElementsByTagName('state')[0].firstChild.data;
                country = response.getElementsByTagName('CountryCode')[0].firstChild.data;
                calculatedSalesTax = response.getElementsByTagName('TaxVal')[0].firstChild.data;
                calculatedSalesSet = true;

                //alert(city);
                //alert(state);
                //alert(country);
                //alert(calculatedSalesTax);

                if (country == 'CA') {
                    document.form2.shipcity.value = city;
                    document.form2.shipstate.selectedIndex = 1;
                    document.form2.shipotherprovence.value = state;
                    document.form2.shipcountry.value = "Canada";
                    setSalesTax();
                    loadShipxml(document.form2, secureURL, shipCarrier, 'OPTION');
                }
                else if (country == 'US') {
                    document.form2.shipcity.value = city;
                    document.form2.shipstate.value = state;
                    document.form2.shipcountry.value = "USA";
                    document.form2.shipotherprovence.selectedIndex = 0;

                    loadShipxml(document.form2, secureURL, shipCarrier, 'OPTION');

                    setSalesTax();
                }
                else {
                    //alert('or here 2');                    
                }
            }
            catch (error) {
                //12/16/2008
                var tempVar = document.getElementById("ShipOptionsTable");
                if (tempVar != 'undefined' && tempVar != null) {
                    document.getElementById("ShipOptionsTable").innerHTML = "<b>The postal code is outside the United States.<br>International rates may apply.</b>";
                }
                //document.getElementById("ShipOptionsTable").innerHTML = error;                  
            }


        } else {
            alert("Please enter a valid zip code:\n" +
                  req.statusText);
        }
    }
}

//Called from Billing postal Code onBlur
function loadxml(form, url) {
    if (form.billzip1.value != "") {
        loadXMLDoc(url + form.billzip1.value, "bill");
    }
}

//Called from Shipping zip onBlur if the not ByPassing AJAX lookup
function loadxmlShipFields(form, url, lcSecureURL, lcShipCarrier) {
    secureURL = lcSecureURL;
    shipCarrier = lcShipCarrier;
    loadXMLDoc(url + form.shipzip1.value, "ship");
}

//  
function loadShipxml(form, url, carrier, format) {
    var FreightAccCheckboxes = document.getElementsByName("chkAcc");
    var freightAccValue = "";
    if (FreightAccCheckboxes != 'undefined') {
        var boxes = FreightAccCheckboxes.length;
        for (i = 0; i < boxes; i++) {
            if (FreightAccCheckboxes[i].checked) {
                freightAccValue = freightAccValue + FreightAccCheckboxes[i].value + "|";
            }
        }
    }
    if (format == 'OPTION') {

        // 8/27/2008 ticket 266  if (form.shipzip1.value != "") {
        if ((form.shipzip1.value != "" && (form.shipcountry.value == "USA")) || (form.shipcountry.value != "USA")) {
            if (form.shipcountry.selectedIndex != 0) {
                //check to see if the zip or country has changed
                //if not, dont go get the rates again
                //9/25/2008 - Causing too much grief, check the rate everytime
                //if (form.shipzip1.value != form.PrevShipZip.value || form.shipcountry.value != form.PrevShipCountry.value) {
                document.getElementById("ShipOptionsTable").innerHTML = "<img alt='Calculating Shipping Rates...' src='" + url + "images/loading.gif' /><br><br>";
                //Set "new" previous values
                form.PrevShipZip.value = form.shipzip1.value;
                form.PrevShipCountry.value = form.shipcountry.value;

                //Call rate functionality
                loadShipXMLDoc(url + "estimatedshipping.asp?ReturnFormat=" + format + "&ZipCodeFrom=" + form.shipzip1.value + "&CountryFrom=" + form.shipcountry.value + "&CarrierCode=" + carrier + "&OrderFreightAcc=" + freightAccValue);
                //}

                if (freightAccValue != "") {
                    if (FreightAccCheckboxes != 'undefined') {
                        var boxes = FreightAccCheckboxes.length;
                        for (i = 0; i < boxes; i++) {
                            if (freightAccValue.indexOf(FreightAccCheckboxes[i].value + "|") >= 0) {
                                FreightAccCheckboxes[i].checked = true;
                            }
                        }
                    }
                }
            }
            else {
                //alert('country is not selected yet');
                // document.getElementById("ShipOptionsTable").innerHTML = "<font color=red>Enter a valid Shipping Zip/Postal Code and/or Country to see Shipping options & fees.</font><br><br>";
                document.getElementById("ShipOptionsTable").innerHTML = "<font color=red>Enter a valid Shipping Country to see Shipping options & fees.</font><br><br>";
                form.PrevShipZip.value = "";
                form.PrevShipCountry.value = "";
            }
        }
        else {
            //7/30/2008 ticket 156 check first if there is a blurb for shipping message, if so, use that one instead
            if (form.isCustomShippingMessage.value == "" || (form.shipcountry.value == "USA")) {
                document.getElementById("ShipOptionsTable").innerHTML = "<font color=red>Enter a valid Shipping Zip/Postal Code and/or Country to see Shipping options & fees.</font><br><br>";
            }
            else {
                document.getElementById("ShipOptionsTable").innerHTML = "<font color=red>" + form.isCustomShippingMessage.value + "</font><br><br>";
            }
            form.PrevShipZip.value = "";
            form.PrevShipCountry.value = "";
        }
    }

    else {
        if (form.txtEstShipZip.value != "") {

            //check to see if the zip or country has changed
            //if not, dont go get the rates again
            //9/25/2008 - Causing too much grief, check the rate everytime
            //if (form.txtEstShipZip.value != form.PrevShipZip.value || form.cboEstCountry.value != form.PrevShipCountry.value) {

            document.getElementById("ShipOptionsTable").innerHTML = "<div align='center'><img alt='Calculating Shipping Rates...' src='" + url + "images/loading.gif' /></div><br><br>";
            //alert('calculating');
            //Set "new" previous values
            form.PrevShipZip.value = form.txtEstShipZip.value;
            form.PrevShipCountry.value = form.cboEstCountry.value;

            //Call rate functionality
            loadShipXMLDoc(url + "estimatedshipping.asp?ReturnFormat=" + format + "&ZipCodeFrom=" + form.txtEstShipZip.value + "&CountryFrom=" + form.cboEstCountry.value + "&CarrierCode=" + carrier);

            //}
        }
        else {
            //alert('zip is not entered yet 2');
            //7/30/2008 ticket 156 check first if there is a blurb for shipping message, if so, use that one instead
            if (document.getElementById("isCustomShippingMessage").value == "" || (form.cboEstCountry.value == "USA" || form.cboEstCountry.value == "United States")) {
                document.getElementById("ShipOptionsTable").innerHTML = "<font color=red>Enter a valid Shipping Zip/Postal Code and/or Country to see Shipping options & fees.</font><br><br>";
            }
            else {
                document.getElementById("ShipOptionsTable").innerHTML = "<font color=red>" + document.getElementById("isCustomShippingMessage").value + "</font><br><br>";
            }
            form.PrevShipZip.value = "";
            form.PrevShipCountry.value = "";
        }
    }
}

