var City;
var State;
var ZipCode;
var CityZipColumnModel;
var CityZipListingGrid;
var TypeOfOperation;
var pag_ZipSearch;
var pag_ConSearch;
Ext.onReady(function() {
    var H;
    var L;
    var J;
    var Q;
    var O = 1;
    Ext.QuickTips.init();
    var K = Ext.get("btnSearch");
    pag_ConSearch = new Ext.data.Store({
        id: "pag_ConSearch",
        proxy: new Ext.data.HttpProxy({ url: "DMCDataFetch.aspx", method: "POST" }),
        baseParams: { methodName: "DMC_Search" },
        reader: new Ext.data.JsonReader(
            { root: "DMCDealer", totalProperty: "total", id: "id" },
            [{ name: "DealerId" }, { name: "DealerName" }, { name: "Distance" }, { name: "City" }, { name: "State" }, { name: "address1" }, { name: "address2" }, { name: "phone" }, { name: "fax" }, { name: "email" }, { name: "zip" }, { name: "websiteaddress" }, { name: "sitestatus"}]), remoteSort: false, autoExpandColumn: "DealerName"
    });
    function N(B) {
        B = B.replace(/</g, "*abc*");
        B = B.replace(/&#/g, "*bcd*");
        return B
    }
    function P(B) {
        return N(document.getElementById(B).value)
    }
    function G(B) {
        return document.getElementById(B).checked
    }
    K.on("click", function() {
        if (P("ddCountry") == "US" || P("ddCountry") == "CA") {
            if (P("ddTypeOfOperation") == 0) {
                Ext.MessageBox.show({
                    title: "Information",
                    msg: "Please select Type of Operation!",
                    width: 325,
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                })
            } else {

                if (P("txt_City") == "" && P("txt_Zip") == "") {
                    Ext.MessageBox.show({
                        title: "Information", msg: "Please enter either City or Zip/Postal Code!", width: 325, buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.INFO
                    })
                } else {
                    City = P("txt_City");
                    State = P("txt_State");
                    ZipCode = P("txt_Zip");
                    Ext.MessageBox.show({
                        title: 'Validating',
                        msg: "Validating City, State  & Zip/Postal Code, please wait...",
                        progressText: "Validating...",
                        width: 400,
                        wait: true,
                        waitConfig: { interval: 200 },
                        icon: "ext-mb-download",
                        animEl: "btn_Search"
                    });
                    pag_ZipSearch = new Ext.data.Store({
                        id: "pag_ZipSearch",
                        proxy: new Ext.data.HttpProxy({ url: "DMCDataFetch.aspx", method: "POST" }),
                        baseParams: { methodName: "DMC_ZipSearch" },
                        reader: new Ext.data.JsonReader({ root: "DMCDealer", totalProperty: "total", id: "id" },
                    [{ name: "zip" }, { name: "city" }, { name: "state"}]),
                        sortInfo: { field: "city", direction: "ASC" }, remoteSort: false
                    });
                    var B = new Ext.grid.CheckboxSelectionModel({ header: "", width: 20 });
                    CityZipColumnModel = new Ext.grid.ColumnModel([B,
                { id: "zip", header: "Zip/Postal Code", dataIndex: "zip", sortable: true, width: 150 },
                { id: "city", header: "City", dataIndex: "city", sortable: true, width: 200 },
                { id: "state", header: "State", dataIndex: "state", sortable: true, width: 50}]);
                    CityZipColumnModel.defaultSortable = true;
                    CityZipListingGrid = new Ext.grid.GridPanel({
                        id: "CityZipListingGrid",
                        title: "", store: pag_ZipSearch,
                        cm: CityZipColumnModel, enableColLock: false,
                        width: 400, height: 380, loadMask: true, autoExpandColumn: "city",
                        viewConfig: { forceFit: true },
                        selModel: new Ext.grid.RowSelectionModel({ singleSelect: true })
                    });
                    pag_ZipSearch.load({
                        params: {
                            start: 0,
                            limit: 15,
                            p2_City: P("txt_City"),
                            p3_State: P("txt_State"),
                            p4_Zip: P("txt_Zip"),
                            p5_Country: P("ddCountry")
                        }
                    });
                    L = new Ext.Window({
                        title: "Dealer Locator : City, State and Zip/Postal Code List", modal: true, height: 450, width: 415, items: [new Ext.Panel({ items: [CityZipListingGrid] })], buttons: [{ text: "OK", handler: function() { D() } }]
                    });
                    CityZipListingGrid.on("rowdblclick", D, CityZipListingGrid);
                    pag_ZipSearch.on("load", S, CityZipListingGrid)
                }

            }
        }
        else {
            E();
            /*if (P("txt_City") == "") {
            Ext.MessageBox.show({
            title: "Information",
            msg: "Please enter City!",
            width: 325,
            buttons: Ext.MessageBox.OK,
            icon: Ext.MessageBox.INFO
            })
            }
            else {
            E();
            }*/
        }
    });
    function S() {
        Ext.MessageBox.hide();
        if (CityZipListingGrid.store.getTotalCount() == 1) {
            document.getElementById("txt_Zip").value = pag_ZipSearch.data.items[0].data.zip;
            document.getElementById("txt_City").value = pag_ZipSearch.data.items[0].data.city;
            document.getElementById("txt_State").value = pag_ZipSearch.data.items[0].data.state;
            E()
        } else {
            if (CityZipListingGrid.store.getTotalCount() == 0) {
                Ext.MessageBox.show({
                    title: "Please Contact:",
                    msg: "<div class='info'><h1>David Manufacturing Co.</h1><p>1004 E. Illinois St. Assumption, IL 62510</p><p>Phone: 888-474-2467 Intl. Tel.: 1-217-226-2467</p><p>U.S. Fax: 800-800-5329 Intl.Fax: 1-217-226-3404</p><p>Email: <a href='mailto:sales@gsiag.com'>sales@gsiag.com</a></p></div>",
                    width: 450,
                    buttons: Ext.MessageBox.OK,
                    icon: "infoDMCLogo"
                });
            } else { L.show() }
        }
    }
    function E() {
        Ext.MessageBox.show({
            title:'Searching',
            msg: "Searching Dealer, please wait...", progressText: "Searching...", width: 300, wait: true, waitConfig: { interval: 200 }, icon: "ext-mb-download", animEl: "btn_Search"
        });
        pag_ConSearch.load({
            params: { start: 0,
                limit: 15,
                p2_City: P("txt_City"),
                p3_State: P("txt_State"),
                p4_Zip: P("txt_Zip"),
                p5_Country: P("ddCountry"),
                p1_TypeOfOperation: (P("ddCountry") == "US" || P("ddCountry") == "CA") ? P("ddTypeOfOperation") : "0"
            },
            callback: function(records, options, success) {
                if (records.length > 0) {
                    Ext.MessageBox.hide(); J.show()
                }
                else {
                    Ext.MessageBox.show({
                        title: "Please Contact:",
                        msg: "<div class='info'><h1>David Manufacturing Co.</h1><p>1004 E. Illinois St. Assumption, IL 62510</p><p>Phone: 888-474-2467 Intl. Tel.: 1-217-226-2467</p><p>U.S. Fax: 800-800-5329 Intl.Fax: 1-217-226-3404</p><p>Email: <a href='mailto:sales@gsiag.com'>sales@gsiag.com</a></p></div>",
                        width: 450,
                        buttons: Ext.MessageBox.OK,
                        icon: "infoDMCLogo"
                    });
                }
            }
        });
        var F = new Ext.grid.CheckboxSelectionModel({ header: "", width: 20 });
        DealerListColumnModel = new Ext.grid.ColumnModel([{
            id: "PreferredDealer",
            header: "Preferred Dealer",
            dataIndex: "PreferredDealer",
            renderer: PD,
            sortable: true,
            width: 100
        },
            { id: "DealerName",
                header: "Dealer Information",
                dataIndex: "DealerName",
                renderer: B,
                sortable:
                true,
                width: 500
            }, {
                id: "Distance",
                header: "Distance(in miles)",
                dataIndex: "Distance",
                renderer: BB,
                hidden: (P("ddCountry") == "US" || P("ddCountry") == "CA") ? false : true,
                sortable: true,
                width: 200}]);
        DealerListColumnModel.defaultSortable = false;
        DealerListingGrid = new Ext.grid.GridPanel({
            id: "DealerListingGrid",
            title: "", store: pag_ConSearch, cm: DealerListColumnModel, enableColLock: false, width: 700, height: 380, loadMask: true, autoExpandColumn: "DealerName", viewConfig: { forceFit: true }, selModel: new Ext.grid.RowSelectionModel({ singleSelect: true })
        });
        function B(V, W, T) {
            var U = T.data.websiteaddress.replace("http://", "");
            if (U != "") {
                U = "http://" + U
            }
            if (T.data.sitestatus == "2") {
                V = '<a href="../dealers/' + T.data.DealerId + "/" + V.replace(/ /g, "").replace(/\./g, "") + '.html" target="_blank">' + V + "</a>"
            } return String.format('<b>{0}</b></br>{1}</br>{2}, {3} - {4}</br><b>Phone:</b>{5}</br><b>Fax:</b>{6}</br><b>Email:</b>{7}</br><b>Website:</b><a href="{8}" target="_blank">{8}</a>', V, T.data.address1, T.data.City, T.data.State, T.data.zip, T.data.phone, T.data.fax, T.data.email, U)
        }
        function BB(S, T, Q) {
            return String.format('<span style="font-size:40px; line-height:85px;">{0}</span><span> mile(s)</span>', S, Q.data.Distance);
        }
        function PD(S, T, Q) {
            if (Q.data.PreferredDealer == 1) {
                return '<img src="../images/preferred.png" alt="Preferred Dealer" title="GSI Preferred Dealer" />';
            }
            else {
                return '';
            }
        }
        J = new Ext.Window({
            title: "Dealer Locator : Search Results", modal: true, height: 450, width: 715,
            items: [new Ext.Panel({ items: DealerListingGrid })],
            buttons: [{ text: "Print", handler: function() { C() } }]
        });

    }
    function D() {
        var B = CityZipListingGrid.getSelections();
        if (B.length > 0) {
            document.getElementById("txt_Zip").value = B[0].get("zip");
            document.getElementById("txt_City").value = B[0].get("city");
            document.getElementById("txt_State").value = B[0].get("state");
            L.hide();
            E()
        }
        else {
            Ext.MessageBox.show({
                title: "Information", msg: 'Select a City, State or Zip from grid before you click on "Ok" button', width: 350, buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.INFO
            })
        }
    }
    function C() {
        var F = pag_ConSearch.data.items.length;
        var B;
        B = '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Dealer Information List</title><style type="text/css">body{font-family:Calibri,Verdana,Tahoma,Arial;font-size:10px !important; margin:0px 0px 0px 0px;}table{border:solid 1px #000; font-size:12px;}th{background-color:#333;color:#fff}td{background-color:#f1f1f1;border-bottom:#000 solid 1px;}</style></head>';
        B += '<body onload="window.print();"><table cellpadding="3" cellspacing="0" border="0"><tr><th style="width:150px">Preferred Dealer</th><th style="width:300px">Dealer Information</th><th style="width:150px">Distance(in miles)</th></tr>';
        for (i = 0; i < F; i++) {
            B += "<tr>";
            if (pag_ConSearch.data.item(i).data.PreferredDealer == 1) {
                B += "<td style='text-align:center;'><img src='../images/preferred.png' alt='Preferred Dealer' title='GSI Preferred Dealer' /></td>";
            }
            else {
                B += "<td>&nbsp;</td>";
            }
            B += "<td><b>" + pag_ConSearch.data.item(i).data.DealerName + "</b>";
            B += "<br />" + pag_ConSearch.data.item(i).data.address1 + " " + pag_ConSearch.data.item(i).data.address2;
            B += "<br />" + pag_ConSearch.data.item(i).data.City + "," + pag_ConSearch.data.item(i).data.State + "-" + pag_ConSearch.data.item(i).data.zip;
            B += "<br /><b>Phone:</b>" + pag_ConSearch.data.item(i).data.phone;
            B += "<br /><b>Email:</b>" + pag_ConSearch.data.item(i).data.email;
            B += "<br /><b>Fax:</b>" + pag_ConSearch.data.item(i).data.fax;
            B += "<br /><b>Website:</b>" + pag_ConSearch.data.item(i).data.websiteaddress;
            B += "</td><td style='text-align:center;'><span style='font-size:40px;'>" + pag_ConSearch.data.item(i).data.Distance + "</span><span> mile(s)</span></td></tr>"
        }
        B += "</table></body></html>";
        pwin = window.open(); newdocument = pwin.document; newdocument.write(B)
    } function A(B) { B.focus(); B.print() }
    function M(B) {
        I("ddProductType");
        R("ddProductType", "Select Product Type", 0);
        document.getElementById("ddProductType").disabled = false;
        if (B == 1) {
            R("ddProductType", "Fans & Heaters", 1);
            R("ddProductType", "Portable Dryers", 2);
            R("ddProductType", "Parts", 4)
        }
        else {
            if (B == 2 || B == 3 || B == 4) {
                R("ddProductType", "Fans & Heaters", 1);
                R("ddProductType", "Portable Dryers", 2);
                R("ddProductType", "Tower Dryer", 3);
                R("ddProductType", "Parts", 4)
            }
        }
    }
    function R(T, F, V) {
        var U; var B = document.getElementById(T);
        U = document.createElement("Option");
        U.text = F;
        U.value = V;
        B.options.add(U)
    }
    function I(T) {
        var F = document.getElementById(T);
        var B = F.options.length;
        for (i = 0; i < B; i++) { F.remove(0) }
    }
});

function countryChange() {
    var country = document.getElementById("ddCountry").value;
    if (country == "US" || country == "CA") {
        if (navigator.appName != "Microsoft Internet Explorer") {
            document.getElementById("idStateTR").style.display = "table-row";
            document.getElementById("idZipTR").style.display = "table-row";
            document.getElementById("idOr").style.display = "table-row";
            document.getElementById("idCityTR").style.display = "table-row";
            document.getElementById("idTOTR").style.display = "table-row";
        }
        else {
            document.getElementById("idStateTR").style.display = "block";
            document.getElementById("idZipTR").style.display = "block";
            document.getElementById("idOr").style.display = "block";
            document.getElementById("idCityTR").style.display = "block";
            document.getElementById("idTOTR").style.display = "block";
        }
    }
    else {
        document.getElementById("txt_State").value = "";
        document.getElementById("txt_Zip").value = "";
        document.getElementById("txt_City").value = "";
        document.getElementById("idStateTR").style.display = "none";
        document.getElementById("idZipTR").style.display = "none";
        document.getElementById("idOr").style.display = "none";
        document.getElementById("idCityTR").style.display = "none";
        document.getElementById("idTOTR").style.display = "none";
    }
}
