var brands = new Array();

// DeWalt

var washers = new Object();
washers.Text = "pressure washers";
washers.Value = "000,180,182,282,283,480,482,780,782,783,784,786,790,791,792,793,794,795,880,883,884,885,886,887,888";

var dwProduct = new Object();
dwProduct.Text = "any other dewalt product";
dwProduct.Value = "000,001,160,162,163,182,400,402,450,480,482,704,705,706,707,708,709,710,711,712,790,791,792,794,795,900F2,900G2,BDDW,BDDWE,DW,DWE,ODDWE";

var electric = new Object();
electric.Text = "electric-compressor";
electric.Value = "000,001,140,160,162,163,180,182,200,202,282,400,402,450,480,482,704,705,706,707,708,709,710,711,712,713,714,715,784,786,790,791,792,793,794,795,900F2,900G2,900N8,900N9,BD,BDDW,BDDWP,DW,ODDWP";

var gasCompressor = new Object();
gasCompressor.Text = "gas-compressor";
gasCompressor.Value = "000,001,140,162,163,180,182,202,282,402,450,480,482,704,706,710,711,712,713,714,715,784,786,790,791,792,793,794,795,900G2,900N8,900N9,BDDW,DW";

var laser = new Object();
laser.Text = "laser";
laser.Value = "000,001,100,BDDW,DW";

brands["DEWALT"] = [dwProduct, washers, electric, gasCompressor, laser];

//--

// BlackAndDecker

var cleaning = new Object();
cleaning.Text = "home cleaning products";
cleaning.Value = "000,001,135,155,400,402,482,705,706,BD,BDDW,BDDWE,BDDWP";

var outdoor = new Object();
outdoor.Text = "outdoor products";
outdoor.Value = "000,001,130,135,140,400,402,450,482,480,704,784,705,706,811,812,BD,BDDW,BDDWE,BDDWP,OD,ODDWE,ODDWP";

var household = new Object();
household.Text = "household products";
household.Value = "HOUSEHOLD,705,706";

var consumer = new Object();
consumer.Text = "consumer power tools";
consumer.Value = "000,001,135,155,400,402,450,480,482,704,784,705,706,811,812,BD,BDDW,BDDWE,BDDWP";

brands["BlackAndDecker"] = [cleaning, outdoor, household, consumer];

brands["Delta"] = new Array();
brands["Devilbiss"] = new Array();
brands["PorterCable"] = new Array();
brands["Bostitch"] = new Array();

// Gets a reference to the "select brand combo"
var brandCombo = $("#OptionSelectBrand");

// Gets a reference to a "select tool type" combo
var productCombo = $("#OptionSelectProductType");

// Gets a reference to the list elements that shows the "select tool type"
var toolTypeLi = $("#liToolType");

$("#ssSearch").click(function() {
    SearchServiceCenters();
});

$("#zipCode").focus(function() {
    $('input[type="text"], input[type="password"]').die('keydown');
});
$("#zipCode").blur(function() {
    $('input[type="text"], input[type="password"]').live('keydown', function(e) {
        if (e.keyCode === 13) {
            $(this).closest('form').submit();
        }
    });
});

$("#zipCode").keydown(function(e) {
    var code = e.keyCode ? e.keyCode : e.which;
    if (code === 13) {
        SearchServiceCenters();
        return false;
    }
});


function SearchServiceCenters() {
    var selectedOption = $("#OptionSelectBrand option:selected");

    // Gets the selected product values and zip
    var seletedValues = $("#OptionSelectProductType option:selected").val();
    var zipValue = $("#zipCode").val();

    // Gets the refirect url for that brand
    var urlToReplace = $(selectedOption).attr("redirectUrl");

    if (typeof (urlToReplace) != 'undefined') {
        // Replace the parameter with the provided values
        urlToReplace = urlToReplace.replace("{zip}", zipValue);
        urlToReplace = urlToReplace.replace("{Codes}", seletedValues);

        // Opens a window with the generated url
        window.open(urlToReplace, '');
    }
}

// By default hides the select tool type combo
$(toolTypeLi).hide();

$(brandCombo).change(function() {

    // Gets the selected brand
    var currentBrand = $(brandCombo).val();

    // If a brand was selected and has items
    if (currentBrand != "0" && brands[currentBrand].length > 0) {

        var brandCollection = brands[currentBrand];
        var productComboDOM = document.getElementById("OptionSelectProductType");

        productComboDOM.innerHTML = "";

        var optionHtml = "";

        for (var i in brandCollection) {
            var option = brandCollection[i];

            optionHtml += '<option value="' + option.Value + '">' + option.Text + '</option>';
        }

        $("#OptionSelectProductType").html(optionHtml);

        // Shows the select tool option
        $(toolTypeLi).show();
    }
    else {

        // Otherwise hides the select brand combo
        $(toolTypeLi).hide();
    }

});

