﻿//This method calls the server page/method to get the city,state,country 
//details by the passed ziopcode and set the value.
function Ajax_GetCityStateByZip(ParamArr, CntrlArr, URL, CntrlToFocus, Zip) {

    var paramList = '';
    var param = "{ZipCode:" + Zip + "}";
    if (ParamArr.length > 0) {
        for (var i = 0; i < ParamArr.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '{' + ParamArr[i] + ':' + ParamArr[i + 1] + '}';
        }
    }
      $.ajax({
        type: 'POST',
        url: URL,
        data: param,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            if (msg.d != "[]") {
                var ResObj = eval(msg.d)
                if (CntrlArr[0] != "") {
                    $("#" + CntrlArr[0]).val(ResObj[0].City);
                }
                if (CntrlArr[1] != "") {
                    $("#" + CntrlArr[1]).val(ResObj[0].State);
                }
                if (CntrlArr[2] != "") {
                    $("#" + CntrlArr[2]).val(ResObj[0].Country);
                }

                $("#" + CntrlToFocus).focus();
            }
            else {
                alert("No match found, please enter the data manually.");

                if (CntrlArr[0] != "") {
                    $("#" + CntrlArr[0]).val("");
                }
                if (CntrlArr[1] != "") {
                    $("#" + CntrlArr[1]).val("");
                }
                if (CntrlArr[2] != "") {
                    $("#" + CntrlArr[2]).val("");
                }
            }
        }
    });
}

//This is a javascript client side method to reset the page control values to default value.
function ResetDHXForm(formObj) {

    var FormName = "the";

    if (document.title != "") {
        FormName = document.title;
    }

    var ConfirmMsg = "You are about to reset " + FormName + " form. Continue?";
    var ResetForm = confirm(ConfirmMsg);

    if (ResetForm) {

        var eleObj = formObj.elements;

        formObj.reset();

        for (i = 0; i < eleObj.length; i++) {

            var fieldType = eleObj[i].type.toLowerCase();

            switch (fieldType) {
                case "text":
                    eleObj[i].value = "";
                    break;
                case "radio":
                case "checkbox":
                    if (eleObj[i].parentElement.className == "checked") {
                        eleObj[i].parentElement.setAttribute("class", "");
                    }
                    break;
                case "select-one":
                case "select-multi":
                    eleObj[i].selectedIndex = 0;
                    break;
                default:
                    break;
            }
        }
        return false;
    }
    else {
        return false;
    }
}

//This method calls the server page/method to get the city,state,country 
//details by the passed ziopcode and set the value.
function Ajax_GetOceanRate(ParamKey,ParamVal, OceanURL, CntrlToHandle, CntrlToViewArr) {

    var HtmlStr = "";
    var oceanratehtml = "";
    var totalcharge = 0.0;
    var ParamList = '';
    
    oceanratehtml += "<table width='100%' align='center'><tr class='GroupHeader'>";
    oceanratehtml += "<td align='center'>Description</td><td align='center'>Rate</td><td align='center'>Charge</td></tr>";

    if (ParamKey.length > 0) {
        for (var i = 0; i < ParamKey.length; i += 1) {
            if (ParamList.length > 0) ParamList += ',';
            ParamList += ParamKey[i] + ":'" + ParamVal[i] + "'";
        }
    }
    $.ajax({
        type: 'POST',
        url: OceanURL,
        data: '{' + ParamList + '}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            if ((msg.d != "[]") && (msg.d != "")) {
                var ResObj = eval(msg.d)
                for (var i = 0; i < ResObj.length; i++) {
                    totalcharge += parseFloat(ResObj[i].Charges);
                    oceanratehtml += "<tr style='height: 20px;'><td>" + ResObj[i].Description + "</td><td align='right'>" + "$" + ResObj[i].Rate.toFixed(2) + "</td><td align='right'>" + "$" + ResObj[i].Charges.toFixed(2) + "</td></tr>"
                }
                oceanratehtml += "</table>";
                HtmlStr += "<table><tr><td align='center'>Total Charge :" + "$" + totalcharge.toFixed(2) + "</td></tr></table>"
                HtmlStr += oceanratehtml;

                //Display the controls to print and email;
                for (var index = 0; index < CntrlToViewArr.length; index += 1) {
                    $('#' + CntrlToViewArr[index]).show();
                }
            }
            else {
                HtmlStr = "Our apologies, it is not possible to provide a Quick Quote at this time. Please call: 800-488-4888, Ext. 2020 to speak with a Dependable Hawaiian Express agent."

                //Hide the controls for print and email abilty
                for (var count = 0; count < CntrlToViewArr.length; count += 1) {
                    $('#' + CntrlToViewArr[count]).hide();
                }
            }

            //Display the pop up and the html
            $('#OceanRaterDiv').html(HtmlStr);
            $('#' + CntrlToHandle).dialog('open');
        }
    });
}

//Generates the accessorial item XML for forwarders
function GetSelAcceItemForServiceRate() {

    //get the delivery location
    var DelAccItem = GetCheckedRadio("rblDeliveryLocation");

    //get the pickup location
    var PuAccItem = GetCheckedRadio("rblPickUpLocation");

    //Check if not null and then assign
    if (PuAccItem != "") {
        AccessorialItem = PuAccItem
    }
    if (DelAccItem != "") {
        AccessorialItem = AccessorialItem + "," + DelAccItem
    }
    if (CheckCheckBox("chkDeliveryLiftGate")) {
        AccessorialItem = AccessorialItem + "," + "LIFTG";
    }
    if (CheckCheckBox("chkDelivery2Man")) {
        AccessorialItem = AccessorialItem + "," + "2MAN";
    }
    if (CheckCheckBox("chkPickupLiftGate")) {
        AccessorialItem = AccessorialItem + "," + "LIFTG";
    }
    if (CheckCheckBox("chkPickup2Man")) {
        AccessorialItem = AccessorialItem + "," + "2MAN";
    }
    if (InsuranceValue != "") {
        AccessorialItem = AccessorialItem + "," + "INS";
    }
}

