var City;
var State;
var ZipCode;
var CityZipColumnModel;
var CityZipListingGrid;
var TypeOfOperation;
var pag_ZipSearch;
var pag_ConSearch;
Ext.onReady(function() {
    var H;
    var K;
    var I;
    var O;
    var M = 1;
    Ext.QuickTips.init();
    var J = Ext.get("btnSearch");
    pag_ConSearch = new Ext.data.Store({
        id: "pag_ConSearch",
        proxy: new Ext.data.HttpProxy({ url: "FFIDataFetch.aspx", method: "POST" }),
        baseParams: { methodName: "FFI" },
        reader: new Ext.data.JsonReader(
            { root: "FFIDealer", 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 L(B) {
        B = B.replace(/</g, "*abc*");
        B = B.replace(/&#/g, "*bcd*");
        return B
    }
    function N(B) {
        return L(document.getElementById(B).value)
    }
    function G(B) {
        return document.getElementById(B).checked
    }
    J.on("click", function() {
        if (N("ddCountry") == "US" || N("ddCountry") == "CA") {
            if (N("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 (N("ddProductType") == 0) {
                    Ext.MessageBox.show({
                        title: "Information",
                        msg: "Please select Product Type!",
                        width: 325,
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.INFO
                    })
                }
                else {

                    if (N("txt_City") == "" && N("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 = N("txt_City");
                        State = N("txt_State");
                        ZipCode = N("txt_Zip");
                        Country = N("ddCountry");
                        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: "FFIDataFetch.aspx", method: "POST" }),
                            baseParams: { methodName: "FFI_ZipSearch" },
                            reader: new Ext.data.JsonReader(
                        { root: "FFIDealer", 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: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("txt_City") : "",
                                p3_State: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("txt_State") : "",
                                p4_Zip: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("txt_Zip") : "",
                                p5_Country: N("ddCountry")
                            }
                        });
                        K = 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", P, CityZipListingGrid)
                    }
                }
            }
        }
        else {
            E();
            /*if (N("txt_City") == "") {
            Ext.MessageBox.show({
            title: "Information",
            msg: "Please enter City!",
            width: 325,
            buttons: Ext.MessageBox.OK,
            icon: Ext.MessageBox.INFO
            })
            }
            else {
            E();
            }*/
        }
    });
    function P() {
        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>FFI Corp</h1><p>1004 E. Illinois St. Assumption, IL 62510</p><p>Phone: 217-226-5400</p><p>Fax: 800-800-5329</p><p>Email: <a href='mailto:info@fficorp.com'>info@fficorp.com</a></p></div>",
                    width: 450,
                    buttons: Ext.MessageBox.OK,
                    icon: "infoFFILogo"
                });
            }
            else { K.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: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("txt_City") : "",
                p3_State: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("txt_State") : "",
                p4_Zip: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("txt_Zip") : "",
                p5_Country: N("ddCountry"),
                p1_TypeOfOperation: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("ddTypeOfOperation") : "0",
                p_ProductType: (N("ddCountry") == "US" || N("ddCountry") == "CA") ? N("ddProductType") : "0"
            },
            callback: function(records, options, success) {
                if (records.length > 0) {
                    Ext.MessageBox.hide();
                    I.show()
                }
                else {
                    Ext.MessageBox.show({
                        title: "Please Contact:",
                        msg: "<div class='info'><h1>FFI Corp</h1><p>1004 E. Illinois St. Assumption, IL 62510</p><p>Phone: 217-226-5400</p><p>Fax: 800-800-5329</p><p>Email: <a href='mailto:info@fficorp.com'>info@fficorp.com</a></p></div>",
                        width: 450,
                        buttons: Ext.MessageBox.OK,
                        icon: "infoFFILogo"
                    });
                }
            }
        });
        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: (N("ddCountry") == "US" || N("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(S, T, Q) {
            var R = Q.data.websiteaddress.replace("http://", "");
            if (R != "") {
                R = "http://" + R
            }
            if (Q.data.sitestatus == "2") {
                S = '<a href="../dealers/' + Q.data.DealerId + "/" + S.replace(/ /g, "").replace(/\./g, "") + '.html" target="_blank">' + S + "</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>', S, Q.data.address1, Q.data.City, Q.data.State, Q.data.zip, Q.data.phone, Q.data.fax, Q.data.email, R)
        }
        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.jpg" alt="Preferred Dealer" />';
            }
            else {
                return '';
            }
        }
        I = 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");
            K.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 prodChange(A) {
    cleardd("ddProductType");
    addItem("ddProductType", "Select Product Type", 0);
    document.getElementById("ddProductType").disabled = false;
    if (A == 0) {
        document.getElementById("ddProductType").disabled = true
    }
    if (A == 1) {
        addItem("ddProductType", "Fans & Heaters", 1);
        addItem("ddProductType", "Portable Dryers", 2);
        addItem("ddProductType", "Parts", 4)
    }
    else {
        if (A == 2 || A == 3 || A == 4) {
            addItem("ddProductType", "Fans & Heaters", 1);
            addItem("ddProductType", "Portable Dryers", 2);
            addItem("ddProductType", "Tower Dryer", 3);
            addItem("ddProductType", "Parts", 4)
        }
    }
}
function addItem(C, B, E) {
    var D;
    var A = document.getElementById(C);
    D = document.createElement("Option");
    D.text = B;
    D.value = E;
    A.options.add(D)
}
function cleardd(C) {
    var B = document.getElementById(C);
    var A = B.options.length;
    for (i = 0; i < A; i++) {
        B.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";
            document.getElementById("idPTTR").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";
            document.getElementById("idPTTR").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";
        document.getElementById("idPTTR").style.display = "none";
    }
}
