﻿var ConfigSearch = {
    VirtualPath: '',
    ManufacturerBox: '',
    ModelBox: '',
    FamilyBox: '',
    CustomerID: null,
    VirtualPath: null,
    Init: function(customerId, virtualPath) {
        this.CustomerID = customerId;
        this.VirtualPath = virtualPath;
    },
    OnSearchTypeChange: function(sender) {
        var _selectedType = $('#' + sender.id).val();
        ConfigSearch._hideObject('divManufacturer');
        ConfigSearch._hideObject('divFamily');
        ConfigSearch._hideObject('divModel');
        ConfigSearch._hideObject('divPartnumber');
        if (_selectedType == '1') {
            ConfigSearch._bindManufacturers();
        } else if (_selectedType == '2') {
            var _result = 'Enter an Axiom part number or a manufacturer\'s oem part number<input style="margin:0px 4px 0px 0px; width:140px;" type="text" id="txtPartNumber" class="shortBox" /> <a id="searchPartNum" href="javascript:ConfigSearch._searchPart()"><img runat="server" id="img2" src="/images/button_go09.gif" /></a>';
            ConfigSearch._setAttributes('divPartnumber', _result);
        }

    },
    _asyncCall: function(url, pars, callback) {
        $.ajax({
            type: "GET",
            url: url,
            data: pars,
            dataType: 'text',
            success: callback,
            error: function(e, xhr) {
                console.log(e);
                console.log(xhr);
            }
        });
    },
    _createOptions: function(json, arg) {
        json = json.replace('<?xml version="1.0" encoding="utf-8"?>', '');
        json = json.replace('<string xmlns="http://tempuri.org/">', '');
        json = json.replace('</string>', '');

        var _options = '';
        //var _json = JSON.parse(json);
        var _json = eval(json);
        switch (arg) {
            case 'model':
                for (var _i = 0; _i < _json.length; _i++) {
                    var newName = _json[_i].name.replace('@@##$$', '\"');
                    _options += '<li value=\'' + _json[_i].id + '\'>' + newName + '</li>';
                }
                break;
            case 'family':
                for (var _i = 0; _i < _json.length; _i++) {
                    var newId = _json[_i].id.replace('@@##$$', '\"');
                    var newName = _json[_i].name.replace('@@##$$', '\"');
                    _options += '<option value=\'' + newId + '\'>' + newName + '</option>';
                }
                break;
            default:
                for (var _i = 0; _i < _json.length; _i++)
                    _options += '<option value=\'' + _json[_i].id + '\'>' + _json[_i].name + '</option>';
                break;
        }
        return _options;
    },
    _bindManufacturers: function() {
        var _loading = '<img src="/images/ajax-loader.gif" alt="" />';
        ConfigSearch._setAttributes('divManufacturer', _loading);
        var _url = '/webservice/enumerations.asmx/GetManufactures';
        var _pars = { cid: ConfigSearch.CustomerID };
        ConfigSearch._asyncCall(_url, _pars, function(response) {
            var _result = '<div><label>Manufacturer</label></div><select id=\'manufacturer\' onchange="javascript:ConfigSearch._bindFamily(this)">' + ConfigSearch._createOptions(response, 'manufacturer') + '</select>';
            ConfigSearch._setAttributes('divManufacturer', _result);
        });
    },
    _bindFamily: function(sender) {
        ConfigSearch._hideObject('divFamily');
        ConfigSearch._hideObject('divModel');
        var _mid = $('#manufacturer').val();
        if (_mid == '-1') return;

        var _url = '/webservice/enumerations.asmx/GetFamily'
        var _pars = { cid: ConfigSearch.CustomerID, mid: _mid };

        ConfigSearch._asyncCall(_url, _pars, function(response) {
            var _result = '<div><label>Product Family</label></div><select id=\'family\' onchange="javascript:ConfigSearch._bindModels(this)">' + ConfigSearch._createOptions(response, 'family') + '</select>';
            ConfigSearch._setAttributes('divFamily', _result);
        });
    },
    _bindModels: function(sender) {
        ConfigSearch._hideObject('divModel');

        var _familyId = $('#family').val();
        if (_familyId == '-1') return;

        var _url = '/webservice/enumerations.asmx/GetModels';
        var _pars = { cid: ConfigSearch.CustomerID, mid: $('#manufacturer').val(), familyid: _familyId };

        ConfigSearch._asyncCall(_url, _pars, function(response) {
            var _result = '<div><label>Product Model</label></div><span class="textBoxWrapper"> \
                <input type="text" style="-moz-border-radius:0px;margin:0px;padding:0px; width:170px; height:18px" id="ddText" class="ddTextBox" readonly="readonly" value="- Model -"> \
                <img src="/images/AdjustableDropDown.gif" id="btn" align="texttop" class="arrowImg"></span>\
                <input id="ddValue" type="text" style="display:none;">\
                <div id="listBox" class="listBox" style="display:none;" tabindex="0">\
                <ul>' + ConfigSearch._createOptions(response, 'model') + '</ul>\
                </div>';


            ConfigSearch._setAttributes('divModel', _result);

            // Start
            var $items = $('#listBox ul li');
            var $listBox = $('#listBox');

            $('#btn').click(function() {
                var minWidth = $('#ddText').outerWidth() + $('#btn').outerWidth();
                $listBox.css("left", $('#ddText').eq(0).offset().left - 125).css("min-width", minWidth);
                if ($listBox.css("display") == "none") {
                    $listBox.show("fast", function() {
                        $listBox.scrollTop(0).eq(0).focus();
                    });
                }
                else {
                    $listBox.hide("fast");
                }
            });

            $items.click(function(e) {
                var _modelName = $(this).text();
                var _modelId = $(this).attr('value');
                ConfigSearch._searchSystems(_modelName, _modelId);
            });

            $('#ddText').click(function() {
                $('#btn').click();
            });

            $items.hover(function() {
                $(this).addClass("highLight");
            }, function() {
                $(this).removeClass("highLight");
            });

            $listBox.blur(function(e) {
                $listBox.hide("fast");
            });


        });
    },
    _hideObject: function(controlId) {
        if ($('#' + controlId).css('display') == 'block') {
            $('#' + controlId).css('display', 'none');
        }
    },
    _setAttributes: function(controlId, result) {
        $('#' + controlId).addClass('highlight').css('display', 'block').html(result);
    },
    _searchSystems: function(modelName, modelId) {
        var _url = modelName.replace(/ /g, '-').replace(/~/g, '') + '/' + modelId + '.aspx'; ;

        //window.location.href = ConfigSearch.VirtualPath + 'config/models/' + $('manufacturer').text().replace(' ', '-') + '-' + modelName.replace('&', '~').replace(/\//g, '~') + '/' + modelId + '.aspx';
        window.location.href = ConfigSearch.VirtualPath + 'config/models/' + _url;
    },
    _searchPart: function() {
        window.location.href = ConfigSearch.VirtualPath + 'config/SearchPartNumber.aspx?pn=' + $('#txtPartNumber').val();
    }
};

var Util = {
    ConvertToHTML: function(str) {
        if (str == null) {
            return null;
        }
        str = str.toString().replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&nbsp;/g, "\xa0").replace(/&amp;/g, "&");
        return str;
    }
};


