﻿// ---------------------------------------------------------------------
// Description:		StartPageExtSearch.js
//					ClientSide Script for StartPageExtSearch.cs Control
// 
// Autor:			Herbert Granofszky
// Date:			01.10.2009
// 
// (c) by team ModulAcht. All rights reserved
// ---------------------------------------------------------------------

var StartPageExtSearch =
{
    DROPDOWN_CASC_DELAY: 50,

    defaultCategoryIndex: 0,
    searchSectionCtrlId: null,
    categoryCtrlId: null,
    producerCtrlId: null,
    modelCtrlId: null,
    compGroupCtrlId: null,
    componentCtrlId: null,
    submitButtonCtrlId: null,
    ajaxLoaderUrl: null,

    initialize: function()
    {
        Event.observe($(this.searchSectionCtrlId + '_0'), 'keyup', this._onSearchSectionChange.bindAsEventListener(this));
        Event.observe($(this.searchSectionCtrlId + '_0'), 'click', this._onSearchSectionChange.bindAsEventListener(this));
        Event.observe($(this.searchSectionCtrlId + '_1'), 'keyup', this._onSearchSectionChange.bindAsEventListener(this));
        Event.observe($(this.searchSectionCtrlId + '_1'), 'click', this._onSearchSectionChange.bindAsEventListener(this));

        // hook into hits counter click event in order to perform search when clicked on the hits counter
        Event.observe('extsearch_hitscount', 'click', this._onHitsCounterClick.bindAsEventListener(this));

        // Back-button/Bookmark Handling
        if (BrowserDetect.browser != "Explorer")
        {
            if ($(this.searchSectionCtrlId + '_0').checked)
            {
                // parts selected
                $(this.searchSectionCtrlId + '_0').click();
            }
            else
            {
                // vehicles selected
                $(this.searchSectionCtrlId + '_1').click();
            }
        }
        else
        {
            this._requestNumberOfHits();
        }
    },

    _onHitsCounterClick: function(e)
    {
        if (BrowserDetect.browser == "Explorer")
        {
            $('extsearch_hitscount').style.cursor = 'wait';
        }
        $(this.submitButtonCtrlId).click();
    },

    _resetAll: function()
    {
        eval(this.categoryCtrlId + ".changeSelection(" + this.defaultCategoryIndex + ", true);");
    },

    _onSearchSectionChange: function(e)
    {
        if ($(this.searchSectionCtrlId + '_0').checked)
        {
            // parts selected
            eval(this.categoryCtrlId + ".addDemandArg('showVehicle', 'false');");
            this._resetAll();

            $('extsearch_compgrouplabel').style.visibility = "visible";
            $('extsearch_componentlabel').style.visibility = "visible";
            $(this.compGroupCtrlId).style.visibility = "visible";
            $(this.componentCtrlId).style.visibility = "visible";

        }
        else
        {
            // vehicles selected
            eval(this.categoryCtrlId + ".addDemandArg('showVehicle', 'true');");
            $('extsearch_compgrouplabel').style.visibility = "hidden";
            $('extsearch_componentlabel').style.visibility = "hidden";
            $(this.compGroupCtrlId).style.visibility = "hidden";
            $(this.componentCtrlId).style.visibility = "hidden";

            this._resetAll();
        }
    },

    onCategorySelChngd: function()
    {
        // get control objects
        var categoryCtrl = document["cb_" + this.categoryCtrlId];
        var producerCtrl = document["cb_" + this.producerCtrlId];
        var modelCtrl = document["cb_" + this.modelCtrlId];
        var categoryVal = categoryCtrl.getSelectedItemValue();
        var compGroupCtrl = document["cb_" + this.compGroupCtrlId];
        var componentCtrl = document["cb_" + this.componentCtrlId];

        // load producers
        producerCtrl.setNoSelectionText();
        producerCtrl.setSelectedItemValue('all');
        producerCtrl.addDemandArg('category', categoryVal + '');
        setTimeout(producerCtrl.loadItems.bind(producerCtrl, true, true), this.DROPDOWN_CASC_DELAY);

        // unload models
        modelCtrl.setNoSelectionText();
        modelCtrl.setSelectedItemValue('all');
        modelCtrl.addDemandArg('producer', '');
        setTimeout(modelCtrl.unloadItems.bind(modelCtrl, true), this.DROPDOWN_CASC_DELAY);

        // load component groups (for all types)
        compGroupCtrl.setNoSelectionText();
        compGroupCtrl.setSelectedItemValue('all');
        compGroupCtrl.addDemandArg('type', '');
        compGroupCtrl.addDemandArg('category', categoryVal + '');
        setTimeout(compGroupCtrl.loadItems.bind(compGroupCtrl, true, true), this.DROPDOWN_CASC_DELAY);

        // unload components
        componentCtrl.setNoSelectionText();
        componentCtrl.setSelectedItemValue('all');
        componentCtrl.addDemandArg('compgrp', '');
        componentCtrl.addDemandArg('type', '');
        componentCtrl.addDemandArg('category', categoryVal + '');
        setTimeout(componentCtrl.unloadItems.bind(componentCtrl, true), this.DROPDOWN_CASC_DELAY);

        // update hits count
        this._requestNumberOfHits();
    },

    onProducerSelChngd: function()
    {
        var producerCtrl = document["cb_" + this.producerCtrlId];
        var modelCtrl = document["cb_" + this.modelCtrlId];

        modelCtrl.setNoSelectionText();
        modelCtrl.setSelectedItemValue('all');

        var selectedItemValue = producerCtrl.getSelectedItemValue();
        if (selectedItemValue == null || selectedItemValue == "all")
        {
            modelCtrl.addDemandArg('producer', '');
        }
        else
        {
            modelCtrl.addDemandArg('producer', selectedItemValue + '');
        }
        setTimeout(modelCtrl.loadItems.bind(modelCtrl, true, true), this.DROPDOWN_CASC_DELAY);

        // update hits count
        this._requestNumberOfHits();

    },

    onModelSelChngd: function()
    {
        // update hits count
        this._requestNumberOfHits();
    },

    onCompGrpSelChngd: function()
    {
        var compGroupCtrl = document["cb_" + this.compGroupCtrlId];
        var componentCtrl = document["cb_" + this.componentCtrlId];
        var selectedItemValue = compGroupCtrl.getSelectedItemValue();
        var selectedItemText = compGroupCtrl.getCaption();

        // load components
        componentCtrl.setNoSelectionText();
        componentCtrl.setSelectedItemValue('all');
        if (selectedItemValue == null || selectedItemValue == "all")
        {
            componentCtrl.addDemandArg('compgrp', '');
        }
        else
        {
            componentCtrl.addDemandArg('compgrp', selectedItemValue + '');
        }
        setTimeout(componentCtrl.loadItems.bind(componentCtrl, true, true), this.DROPDOWN_CASC_DELAY);

        // update hits count
        this._requestNumberOfHits();
    },

    onCompSelChngd: function()
    {
        // update hits count
        this._requestNumberOfHits();
    },

    onProducerDemandLoad: function()
    {
        eval("var itemCount = " + this.producerCtrlId + ".length;");
        if (itemCount == 1)
        {
            eval(this.producerCtrlId + ".changeSelection(0);");
        }
    },

    onModelDemandLoad: function()
    {
        eval("var itemCount = " + this.modelCtrlId + ".length;");
        if (itemCount == 1)
        {
            eval(this.modelCtrlId + ".changeSelection(0);");
        }
    },

    onCompGrpDemandLoad: function()
    {
        eval("var itemCount = " + this.compGroupCtrlId + ".length;");
        if (itemCount == 1)
        {
            eval(this.compGroupCtrlId + ".changeSelection(0);");
        }
    },

    onComponentDemandLoad: function()
    {
        eval("var itemCount = " + this.componentCtrlId + ".length;");
        if (itemCount == 1)
        {
            eval(this.componentCtrlId + ".changeSelection(0);");
        }
    },

    _requestNumberOfHits: function()
    {
        eval("var producer = " + this.producerCtrlId + ".getSelectedItemValue();");
        eval("var model = " + this.modelCtrlId + ".getSelectedItemValue();");
        eval("var category = " + this.categoryCtrlId + ".getSelectedItemValue();");
        eval("var component = " + this.componentCtrlId + ".getSelectedItemValue();");
        eval("var compGroup = " + this.compGroupCtrlId + ".getSelectedItemValue();");

        var ajaxRequest = new SmartAjax();
        ajaxRequest.serviceId = "SmabusService";
        ajaxRequest.url = this.ajaxLoaderUrl;
        ajaxRequest.appendRequest("category", category);
        ajaxRequest.appendRequest("model", model);
        ajaxRequest.appendRequest("producer", producer);
        if ($(this.searchSectionCtrlId + '_0').checked)
        {
            ajaxRequest.appendRequest("command", "GetOfferCount");
            ajaxRequest.appendRequest("component", component);
            ajaxRequest.appendRequest("compGroup", compGroup);
        }
        else
        {
            ajaxRequest.appendRequest("command", "GetVehicleOfferCount");
            ajaxRequest.appendRequest("replacement", '1');
            ajaxRequest.appendRequest("accident", '1');
            ajaxRequest.appendRequest("tinker", '1');
            ajaxRequest.appendRequest("spareParts", '1');
        }

        ajaxRequest.onResponse = this._onRequestNumberOfHitsResponse.bind(this);
        ajaxRequest.sendRequest();
    },

    _onRequestNumberOfHitsResponse: function(argAjaxObj)
    {
        if (argAjaxObj.error == null && argAjaxObj.response != null && argAjaxObj.response != "")
        {
            eval("var numOfHits = " + argAjaxObj.response + ";");
            $("extsearch_hits").update("Treffer: " + numOfHits);
        }
        else
        {
            $("extsearch_hits").update("Keine Treffer");
        }
    }
};


