﻿// ---------------------------------------------------------------------
// Description:		ExtendedSearch.js
//					ClientSide Script for ExtendedSearch.cs control
// 
// Autor:			Herbert Granofszky
// Date:			16.10.2007
// 
// (c) by team ModulAcht. All rights reserved
// ---------------------------------------------------------------------

var ExtendedSearch =
{
    DROPDOWN_CASC_DELAY: 50,

    defaultCategoryIndex: -1,
    categoryCtrlId: null,
    producerCtrlId: null,
    modelCtrlId: null,
    compGroupCtrlId: null,
    typeCtrlId: null,
    componentCtrlId: null,
    colorCtrlId: null,
    cyldrCtrlId: null,
    fuelCtrlId: null,
    milgFCtrlId: null,
    milgTCtrlId: null,
    yocFCtrlId: null,
    yocTCtrlId: null,
    ccmFCtrlId: null,
    ccmTCtrlId: null,
    powerFCtrlId: null,
    powerTCtrlId: null,
    countryCtrlId: null,
    plzCtrlId: null,
    costCtrlId: null,
    taxCtrlId: null,
    stateCtrlId: null,
    submitButtonCtrlId: null,
    abortButtonCtrlId: null,
    ajaxLoaderUrl: null,
    preloaded: false,

    initialize: function()
    {
        Event.observe('extsearch_panel', 'keyup', this._onPanelKeyUp.bindAsEventListener(this), true);
        Event.observe('extsearch_panel', 'keydown', this._onPanelKeyDown.bindAsEventListener(this), true);
        Event.observe('extsearch_panel', 'keypress', this._onPanelKeyPress.bindAsEventListener(this), true);

        // hook into additional search arguments - textbox controls event handler (for synchronizing with keynumber search)
        Event.observe($(this.ccmFCtrlId), 'keyup', this._onExtArgTextBoxKeyUp.bindAsEventListener(this));
        Event.observe($(this.ccmTCtrlId), 'keyup', this._onExtArgTextBoxKeyUp.bindAsEventListener(this));
        Event.observe($(this.powerFCtrlId), 'keyup', this._onExtArgTextBoxKeyUp.bindAsEventListener(this));
        Event.observe($(this.powerTCtrlId), 'keyup', this._onExtArgTextBoxKeyUp.bindAsEventListener(this));
        Event.observe($(this.plzCtrlId), 'keyup', this._onExtArgTextBoxKeyUp.bindAsEventListener(this));
        Event.observe($(this.costCtrlId), 'keyup', this._onExtArgTextBoxKeyUp.bindAsEventListener(this));
        
        // hook into text box search arguments for hits counter update
// TODO: uncommented because hits count is requested on keyup events -----^        
//        Event.observe($(this.ccmFCtrlId), 'change', this._requestNumberOfHits.bindAsEventListener(this));
//        Event.observe($(this.ccmTCtrlId), 'change', this._requestNumberOfHits.bindAsEventListener(this));
//        Event.observe($(this.powerFCtrlId), 'change', this._requestNumberOfHits.bindAsEventListener(this));
//        Event.observe($(this.powerTCtrlId), 'change', this._requestNumberOfHits.bindAsEventListener(this));
//        Event.observe($(this.plzCtrlId), 'change', this._requestNumberOfHits.bindAsEventListener(this));
//        Event.observe($(this.costCtrlId), 'change', this._requestNumberOfHits.bindAsEventListener(this));

        // hook into tax and state controls event handler (for synchronizing with keynumber search)
        Event.observe($(this.taxCtrlId), 'keyup', this._onTaxChange.bindAsEventListener(this));
        Event.observe($(this.taxCtrlId), 'click', this._onTaxChange.bindAsEventListener(this));
        Event.observe($(this.stateCtrlId + '_0'), 'keyup', this._onStateChange.bindAsEventListener(this));
        Event.observe($(this.stateCtrlId + '_0'), 'click', this._onStateChange.bindAsEventListener(this));
        Event.observe($(this.stateCtrlId + '_1'), 'keyup', this._onStateChange.bindAsEventListener(this));
        Event.observe($(this.stateCtrlId + '_1'), 'click', this._onStateChange.bindAsEventListener(this));
        Event.observe($(this.stateCtrlId + '_2'), 'keyup', this._onStateChange.bindAsEventListener(this));
        Event.observe($(this.stateCtrlId + '_2'), 'click', this._onStateChange.bindAsEventListener(this));

        // hook into tax label and cost label elements select start events in order to suppress text selection in ie
        Event.observe('extsearch_taxbox', 'selectstart', this._suppressTextSelect.bindAsEventListener(this));
        Event.observe('extsearch_statebox', 'selectstart', this._suppressTextSelect.bindAsEventListener(this));

        this._keyNumberRequested = false;

        // hook into hits counter click event in order to perform search when clicked on the hits counter
        Event.observe('extsearch_hits', 'click', this._onHitsCounterClick.bindAsEventListener(this));

    },

    _onHitsCounterClick: function(e)
    {
        if (BrowserDetect.browser == "Explorer")
        {
            $('extsearch_hits').style.cursor = 'wait';
        }
        $(this.submitButtonCtrlId).click();
    },

    _onPanelKeyDown: function(e)
    {
        if (e.keyCode == Event.KEY_RETURN)
        {
            $(this.submitButtonCtrlId).click();
            Event.stop(e);
            return false;
        }
        return true;
    },

    _onPanelKeyUp: function(e)
    {
        if (e.keyCode == Event.KEY_RETURN)
        {
            $(this.submitButtonCtrlId).click();
            Event.stop(e);
            return false;
        }
        return true;
    },

    _onPanelKeyPress: function(e)
    {
        if (e.keyCode == Event.KEY_RETURN)
        {
            $(this.submitButtonCtrlId).click();
            Event.stop(e);
            return false;
        }
        return true;
    },

    initFromKeyNumberInfo: function(argKeyNumberInfo)
    {
        eval(this.categoryCtrlId + ".setSelectedItemValue('" + argKeyNumberInfo.CategoryId + "');");
        eval(this.categoryCtrlId + ".setCaption('" + argKeyNumberInfo.CategoryDesc + "');");

        eval(this.producerCtrlId + ".setSelectedItemValue('" + argKeyNumberInfo.ProducerId + "');");
        eval(this.producerCtrlId + ".setCaption('" + argKeyNumberInfo.ProducerName + "');");
        eval(this.producerCtrlId + ".addDemandArg('category', '" + argKeyNumberInfo.CategoryId + "');");

        eval(this.modelCtrlId + ".setSelectedItemValue('" + argKeyNumberInfo.ModelId + "');");
        eval(this.modelCtrlId + ".setCaption('" + argKeyNumberInfo.ModelDesc + "');");
        eval(this.modelCtrlId + ".addDemandArg('producer', '" + argKeyNumberInfo.ProducerId + "');");

        eval(this.typeCtrlId + ".setSelectedItemValue('" + argKeyNumberInfo.TypeId + "');");
        eval(this.typeCtrlId + ".setCaption('" + argKeyNumberInfo.TypeDesc + "');");
        eval(this.typeCtrlId + ".addDemandArg('category', '" + argKeyNumberInfo.CategoryId + "');");

        eval(this.compGroupCtrlId + ".addDemandArg('type', '" + argKeyNumberInfo.TypeId + "');");
    },

    initCompGrp: function(argCompGrpInfo)
    {
        if (argCompGrpInfo != null)
        {
            eval(this.compGroupCtrlId + ".setSelectedItemValue('" + argCompGrpInfo.value + "');");
            eval(this.compGroupCtrlId + ".setCaption('" + argCompGrpInfo.text + "');");
            eval(this.componentCtrlId + ".addDemandArg('compgrp', '" + argCompGrpInfo.value + "');");
        }
        else
        {
            eval(this.componentCtrlId + ".unloadItems();");
            eval(this.compGroupCtrlId + ".changeSelection(-1, true);");
            eval(this.componentCtrlId + ".addDemandArg('compgrp', '');");
        }
        this.onCompGrpSelChngd();
    },

    initComponent: function(argComponentInfo)
    {
        if (argComponentInfo != null)
        {
            eval(this.componentCtrlId + ".setSelectedItemValue('" + argComponentInfo.value + "');");
            eval(this.componentCtrlId + ".setCaption('" + argComponentInfo.text + "');");
        }
        else
        {
            eval(this.componentCtrlId + ".changeSelection(-1, true);");
        }
        this.onCompSelChngd();
    },

    initColor: function(argColorInfo)
    {
        if (argColorInfo != null)
        {
            eval(this.colorCtrlId + ".setSelectedIndex(" + argColorInfo.index + ");");
            eval(this.colorCtrlId + ".setCaption('" + argColorInfo.text + "');");
        }
    },

    initCylinder: function(argCylinderInfo)
    {
        if (argCylinderInfo != null)
        {
            eval(this.cyldrCtrlId + ".setSelectedIndex(" + argCylinderInfo.index + ");");
            eval(this.cyldrCtrlId + ".setCaption('" + argCylinderInfo.text + "');");
        }
    },

    initFuel: function(argFuelInfo)
    {
        if (argFuelInfo != null)
        {
            eval(this.fuelCtrlId + ".setSelectedIndex(" + argFuelInfo.index + ");");
            eval(this.fuelCtrlId + ".setCaption('" + argFuelInfo.text + "');");
        }
    },

    initMilageFrom: function(argMilageInfo)
    {
        if (argMilageInfo != null)
        {
            eval(this.milgFCtrlId + ".setSelectedIndex(" + argMilageInfo.index + ");");
            eval(this.milgFCtrlId + ".setCaption('" + argMilageInfo.text + "');");
        }
    },

    initMilageTill: function(argMilageInfo)
    {
        if (argMilageInfo != null)
        {
            eval(this.milgTCtrlId + ".setSelectedIndex(" + argMilageInfo.index + ");");
            eval(this.milgTCtrlId + ".setCaption('" + argMilageInfo.text + "');");
        }
    },

    initYocFrom: function(argYocInfo)
    {
        if (argYocInfo != null)
        {
            eval(this.yocFCtrlId + ".setSelectedIndex(" + argYocInfo.index + ");");
            eval(this.yocFCtrlId + ".setCaption('" + argYocInfo.text + "');");
        }
    },

    initYocTill: function(argYocInfo)
    {
        if (argYocInfo != null)
        {
            eval(this.yocTCtrlId + ".setSelectedIndex(" + argYocInfo.index + ");");
            eval(this.yocTCtrlId + ".setCaption('" + argYocInfo.text + "');");
        }
    },

    initCubicFrom: function(argCubic)
    {
        if (argCubic != null)
        {
            $(this.ccmFCtrlId).value = argCubic;
        }
    },

    initCubicTill: function(argCubic)
    {
        if (argCubic != null)
        {
            $(this.ccmTCtrlId).value = argCubic;
        }
    },

    initPowerFrom: function(argPower)
    {
        if (argPower != null)
        {
            $(this.powerFCtrlId).value = argPower;
        }
    },

    initPowerTill: function(argPower)
    {
        if (argPower != null)
        {
            $(this.powerTCtrlId).value = argPower;
        }
    },

    initCountry: function(argCountryInfo)
    {
        if (argCountryInfo != null)
        {
            eval(this.countryCtrlId + ".setSelectedIndex(" + argCountryInfo.index + ");");
            eval(this.countryCtrlId + ".setCaption('" + argCountryInfo.text + "');");
        }
    },

    initPlz: function(argPlz)
    {
        if (argPlz != null)
        {
            $(this.plzCtrlId).value = argPlz;
        }
    },

    initCost: function(argCost)
    {
        if (argCost != null)
        {
            $(this.costCtrlId).value = argCost;
        }
    },

    initTax: function(argTax)
    {
        if (argTax != null)
        {
            $(this.taxCtrlId).checked = argTax;
        }
    },

    initState: function(argStateInfo)
    {
        if (argStateInfo != null)
        {
            $(this.stateCtrlId + "_0").checked = argStateInfo.all;
            $(this.stateCtrlId + "_1").checked = argStateInfo.used;
            $(this.stateCtrlId + "_2").checked = argStateInfo._new;
        }
    },

    resetAll: function()
    {
        eval(this.categoryCtrlId + ".changeSelection(" + this.defaultCategoryIndex + ", true);");
        this.onCategorySelChngd();
    },

    preloadDropDownBoxes: function()
    {
        if (this.preloaded)
        {
            return;
        }

        // get control objects
        var categoryCtrl = document["cb_" + this.categoryCtrlId];
        var producerCtrl = document["cb_" + this.producerCtrlId];
        var typeCtrl = document["cb_" + this.typeCtrlId];
        var modelCtrl = document["cb_" + this.modelCtrlId];
        var compGroupCtrl = document["cb_" + this.compGroupCtrlId];
        var componentCtrl = document["cb_" + this.componentCtrlId];

        if (componentCtrl == null || compGroupCtrl == null || modelCtrl == null || typeCtrl == null || producerCtrl == null || categoryCtrl == null)
        {
            setTimeout(this.preloadDropDownBoxes.bind(this), 100);
            return;
        }

        var category = categoryCtrl.getSelectedItemValue();

        // preload producers
        setTimeout(producerCtrl.loadItems.bind(producerCtrl, true, true), 50);

        // preload models
        var producer = producerCtrl.getSelectedItemValue();
        if (producer != null)
        {
            setTimeout(modelCtrl.loadItems.bind(modelCtrl, true, true), 50);
        }

        // preload types
        setTimeout(typeCtrl.loadItems.bind(typeCtrl, true, true), 50);

        // preload component groups
        var type = typeCtrl.getSelectedItemValue();
        //if (type != null)
        //{
        setTimeout(compGroupCtrl.loadItems.bind(compGroupCtrl, true, true), 50);

        // preload components
        var compGroup = compGroupCtrl.getSelectedItemValue();
        if (compGroup != null)
        {
            setTimeout(componentCtrl.loadItems.bind(componentCtrl, true, true), 50);
        }
        //}
        this.preloaded = true;

        // update hits count
        this._requestNumberOfHits();
    },

    onCategorySelChngd: function()
    {
        // get control objects
        var categoryCtrl = document["cb_" + this.categoryCtrlId];
        var producerCtrl = document["cb_" + this.producerCtrlId];
        var modelCtrl = document["cb_" + this.modelCtrlId];
        var typeCtrl = document["cb_" + this.typeCtrlId];
        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 types
        typeCtrl.setNoSelectionText();
        typeCtrl.setSelectedItemValue('all');
        typeCtrl.addDemandArg('category', categoryVal + '');
        setTimeout(typeCtrl.loadItems.bind(typeCtrl, true, 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);

        // disable ext fields
        this._disableExFields();

        // 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();
    },

    onTypeSelChngd: function()
    {
        var typeCtrl = document["cb_" + this.typeCtrlId];
        var compGroupCtrl = document["cb_" + this.compGroupCtrlId];
        var componentCtrl = document["cb_" + this.componentCtrlId];
        var selectedItemValue = typeCtrl.getSelectedItemValue();

        // load component groups
        //compGroupCtrl.setNoSelectionText();
        //compGroupCtrl.setSelectedItemValue('all');
        if (selectedItemValue == null || selectedItemValue == "all")
        {
            compGroupCtrl.addDemandArg('type', '');
            componentCtrl.addDemandArg('type', '');
        }
        else
        {
            compGroupCtrl.addDemandArg('type', selectedItemValue + '');
            componentCtrl.addDemandArg('type', selectedItemValue + '');
        }
        setTimeout(compGroupCtrl.loadItems.bind(compGroupCtrl, true, true), this.DROPDOWN_CASC_DELAY);

        // unload components
        //componentCtrl.setNoSelectionText();
        //componentCtrl.setSelectedItemValue('all');
        //componentCtrl.addDemandArg('compgrp', '');
        setTimeout(componentCtrl.unloadItems.bind(componentCtrl, true), this.DROPDOWN_CASC_DELAY);

        // disable ext fields
        this._disableExFields();

        // 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', '');
            // synchronize with keynumber search
            if (SearchPanel != null)
            {
                SearchPanel.compGrp = null;
                setTimeout(SearchPanel.notifyKeyNumberSearch.bind(SearchPanel, "compGrp"), 50);
            }
        }
        else
        {
            componentCtrl.addDemandArg('compgrp', selectedItemValue + '');
            // synchronize with keynumber search
            if (SearchPanel != null)
            {
                SearchPanel.compGrp = { text: selectedItemText, value: selectedItemValue };
                setTimeout(SearchPanel.notifyKeyNumberSearch.bind(SearchPanel, "compGrp"), 50);
            }
        }
        setTimeout(componentCtrl.loadItems.bind(componentCtrl, true, true), this.DROPDOWN_CASC_DELAY);

        // disable ext fields
        this._disableExFields();

        // update hits count
        this._requestNumberOfHits();
    },

    onCompSelChngd: function()
    {
        var componentCtrl = document["cb_" + this.componentCtrlId];
        var selectedItemValue = componentCtrl.getSelectedItemValue();
        var selectedItemText = componentCtrl.getCaption();

        // synchronize with keynumber search
        if (selectedItemValue == null || selectedItemValue == "all")
        {
            if (SearchPanel != null)
            {
                SearchPanel.component = null;
            }
        }
        else
        {
            if (SearchPanel != null)
            {
                SearchPanel.component = { text: selectedItemText, value: selectedItemValue };
            }

        }
        if (SearchPanel != null)
        {
            setTimeout(SearchPanel.notifyKeyNumberSearch.bind(SearchPanel, "component"), 50);
        }


        if (selectedItemValue != null && selectedItemValue != "all")
        {
            // do ajax request to get extended properties
            var ajaxRequest = new SmartAjax();
            ajaxRequest.serviceId = "SmabusService";
            ajaxRequest.url = this.ajaxLoaderUrl;
            ajaxRequest.appendRequest("command", "GetComponentSmabus");
            ajaxRequest.appendRequest("componentId", selectedItemValue);
            ajaxRequest.onResponse = this._onGetExPropertiesResponse.bind(this);
            ajaxRequest.sendRequest();
        }
        else
        {
            this._disableExFields();
            this._clearExFields();
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onColorSelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.colorCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.colorCtrlId + ".getCaption();");

            SearchPanel.color = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("color");
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onCylinderSelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.cyldrCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.cyldrCtrlId + ".getCaption();");

            SearchPanel.cylinder = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("cylinder");
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onFuelSelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.fuelCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.fuelCtrlId + ".getCaption();");

            SearchPanel.fuel = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("fuel");
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onMilageFromSelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.milgFCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.milgFCtrlId + ".getCaption();");

            SearchPanel.milageFrom = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("milageFrom");
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onMilageTillSelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.milgTCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.milgTCtrlId + ".getCaption();");

            SearchPanel.milageTill = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("milageTill");
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onYocFromSelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.yocFCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.yocFCtrlId + ".getCaption();");

            SearchPanel.yocFrom = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("yocFrom");
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onYocTillSelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.yocTCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.yocTCtrlId + ".getCaption();");

            SearchPanel.yocTill = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("yocTill");
        }

        // update hits count
        this._requestNumberOfHits();
    },

    onCountrySelChngd: function()
    {
        // synchronize with exteded search
        if (SearchPanel != null)
        {
            eval("var selectedItemIndex = " + this.countryCtrlId + ".getSelectedIndex();");
            eval("var selectedItemText = " + this.countryCtrlId + ".getCaption();");

            SearchPanel.country = { index: selectedItemIndex, text: selectedItemText };
            SearchPanel.notifyKeyNumberSearch("country");
        }

        // 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);");
        }
    },

    onTypeDemandLoad: function()
    {
        eval("var itemCount = " + this.typeCtrlId + ".length;");
        if (itemCount == 1)
        {
            eval(this.typeCtrlId + ".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);");
        }
    },

    // --------------------------------------------------------------------    
    // KeyUp eventhandler for extended search arguments - text boxes
    //
    // Used for synchronizing with the key search tab
    // 
    //  e                     :   event object
    // --------------------------------------------------------------------    
    _onExtArgTextBoxKeyUp: function(e)
    {
        // check if we are inside a search panel
        if (SearchPanel == null)
        {
            return true;
        }
        var element = Event.element(e);
        var id = element.id;
        var value = element.value;

        // notify search panel for change
        switch (id)
        {
            case this.ccmFCtrlId:
                SearchPanel.cubicFrom = value;
                SearchPanel.notifyKeyNumberSearch("cubicFrom");
                break;
            case this.ccmTCtrlId:
                SearchPanel.cubicTill = value;
                SearchPanel.notifyKeyNumberSearch("cubicTill");
                break;
            case this.powerFCtrlId:
                SearchPanel.powerFrom = value;
                SearchPanel.notifyKeyNumberSearch("powerFrom");
                break;
            case this.powerTCtrlId:
                SearchPanel.powerTill = value;
                SearchPanel.notifyKeyNumberSearch("powerTill");
                break;
            case this.plzCtrlId:
                SearchPanel.plz = value;
                SearchPanel.notifyKeyNumberSearch("plz");
                break;
            case this.costCtrlId:
                SearchPanel.cost = value;
                SearchPanel.notifyKeyNumberSearch("cost");
                break;
        }

        // update hits count
        this._requestNumberOfHits();
        
        return true;
    },

    _onTaxChange: function(e)
    {
        // check if we are inside a search panel
        if (SearchPanel == null)
        {
            return true;
        }
        SearchPanel.tax = Event.element(e).checked;
        SearchPanel.notifyKeyNumberSearch("tax");

        // update hits count
        this._requestNumberOfHits();
    },

    _onStateChange: function(e)
    {
        // check if we are inside a search panel
        if (SearchPanel == null)
        {
            return true;
        }
        // determine checked-states of all state radio buttons
        var stateInfo = { all: $(this.stateCtrlId + '_0').checked,
            used: $(this.stateCtrlId + '_1').checked,
            _new: $(this.stateCtrlId + '_2').checked
        };
        SearchPanel.state = stateInfo;
        SearchPanel.notifyKeyNumberSearch("state");

        // update hits count
        this._requestNumberOfHits();
    },

    _suppressTextSelect: function(e)
    {
        Event.stop(e);
    },

    _onGetExPropertiesResponse: function(argAjaxObj)
    {
        if (argAjaxObj.error == null)
        {
            var component = null;
            eval("var component = " + argAjaxObj.response + ";");

            // enable/disable other boxes according to response

            // cubic from
            if (this.ccmFCtrlId != null)
            {
                if (component.ccmEx)
                {
                    $(this.ccmFCtrlId).disabled = false;
                    $(this.ccmFCtrlId).className = "txt";
                }
                else
                {
                    $(this.ccmFCtrlId).disabled = true;
                    $(this.ccmFCtrlId).className = "txt_disabled";
                }
            }

            // cubic till
            if (this.ccmTCtrlId != null)
            {
                if (component.ccmEx)
                {
                    $(this.ccmTCtrlId).disabled = false;
                    $(this.ccmTCtrlId).className = "txt";
                }
                else
                {
                    $(this.ccmTCtrlId).disabled = true;
                    $(this.ccmTCtrlId).className = "txt_disabled";
                }
            }

            // color
            if (this.colorCtrlId != null)
            {
                if (component.colorEx)
                {
                    eval(this.colorCtrlId + ".setEnabled(true);");
                }
                else
                {
                    eval(this.colorCtrlId + ".setEnabled(false);");
                }
            }

            // cylinder count
            if (this.cyldrCtrlId != null)
            {
                if (component.cylinderCountEx)
                {
                    eval(this.cyldrCtrlId + ".setEnabled(true);");
                }
                else
                {
                    eval(this.cyldrCtrlId + ".setEnabled(false);");
                }
            }

            // fuel
            if (this.fuelCtrlId != null)
            {
                if (component.fuelEx)
                {
                    eval(this.fuelCtrlId + ".setEnabled(true);");
                }
                else
                {
                    eval(this.fuelCtrlId + ".setEnabled(false);");
                }
            }

            // milage
            if (this.milgFCtrlId != null)
            {
                if (component.milageEx)
                {
                    eval(this.milgFCtrlId + ".setEnabled(true);");
                    eval(this.milgTCtrlId + ".setEnabled(true);");
                }
                else
                {
                    eval(this.milgFCtrlId + ".setEnabled(false);");
                    eval(this.milgTCtrlId + ".setEnabled(false);");
                }
            }

            // power from
            if (this.powerFCtrlId != null)
            {
                if (component.powerEx)
                {
                    $(this.powerFCtrlId).disabled = false;
                    $(this.powerFCtrlId).className = "txt";
                }
                else
                {
                    $(this.powerFCtrlId).disabled = true;
                    $(this.powerFCtrlId).className = "txt_disabled";
                }
            }

            // power till
            if (this.powerTCtrlId != null)
            {
                if (component.powerEx)
                {
                    $(this.powerTCtrlId).disabled = false;
                    $(this.powerTCtrlId).className = "txt";
                }
                else
                {
                    $(this.powerTCtrlId).disabled = true;
                    $(this.powerTCtrlId).className = "txt_disabled";
                }
            }
        }
    },

    _requestNumberOfHits: function()
    {
        eval("var producer = " + this.producerCtrlId + ".getSelectedItemValue();");
        eval("var model = " + this.modelCtrlId + ".getSelectedItemValue();");
        eval("var type = " + this.typeCtrlId + ".getSelectedItemValue();");
        eval("var category = " + this.categoryCtrlId + ".getSelectedItemValue();");
        eval("var color = " + this.colorCtrlId + ".getSelectedItemValue();");
        eval("var fuelType = " + this.fuelCtrlId + ".getSelectedItemValue();");
        eval("var cylinder = " + this.cyldrCtrlId + ".getSelectedItemValue();");
        eval("var milgf = " + this.milgFCtrlId + ".getSelectedItemValue();");
        eval("var milgt = " + this.milgTCtrlId + ".getSelectedItemValue();");
        eval("var yocf = " + this.yocFCtrlId + ".getSelectedItemValue();");
        eval("var yoct = " + this.yocTCtrlId + ".getSelectedItemValue();");
        eval("var component = " + this.componentCtrlId + ".getSelectedItemValue();");
        eval("var compGroup = " + this.compGroupCtrlId + ".getSelectedItemValue();");
        eval("var country = " + this.countryCtrlId + ".getSelectedItemValue();");

        var ccmFrom = $(this.ccmFCtrlId).value;
        var ccmTill = $(this.ccmTCtrlId).value;
        var powerFrom = $(this.powerFCtrlId).value;
        var powerTill = $(this.powerTCtrlId).value;
        var plz = $(this.plzCtrlId).value;
        var cost = $(this.costCtrlId).value;
        var tax = ($(this.taxCtrlId).checked ? "1" : "0");

        if ($(this.stateCtrlId + '_2').checked)
        {
            var state = "_new";
        }
        else if ($(this.stateCtrlId + '_1').checked)
        {
            var state = "used";
        }
        else
        {
            var state = "all";
        }


        var ajaxRequest = new SmartAjax();
        ajaxRequest.serviceId = "SmabusService";
        ajaxRequest.url = this.ajaxLoaderUrl;
        ajaxRequest.appendRequest("command", "GetOfferCount");
        ajaxRequest.appendRequest("producer", producer);
        ajaxRequest.appendRequest("model", model);
        ajaxRequest.appendRequest("type", type);
        ajaxRequest.appendRequest("category", category);
        ajaxRequest.appendRequest("component", component);
        ajaxRequest.appendRequest("compGroup", compGroup);
        ajaxRequest.appendRequest("cylinder", cylinder);
        ajaxRequest.appendRequest("color", color);
        ajaxRequest.appendRequest("fuelType", fuelType);
        ajaxRequest.appendRequest("milgf", milgf);
        ajaxRequest.appendRequest("milgt", milgt);
        ajaxRequest.appendRequest("yocf", yocf);
        ajaxRequest.appendRequest("yoct", yoct);
        ajaxRequest.appendRequest("ccmf", ccmFrom);
        ajaxRequest.appendRequest("ccmt", ccmTill);
        ajaxRequest.appendRequest("powerf", powerFrom);
        ajaxRequest.appendRequest("powert", powerTill);
        ajaxRequest.appendRequest("country", country);
        ajaxRequest.appendRequest("plz", plz);
        ajaxRequest.appendRequest("cost", cost);
        ajaxRequest.appendRequest("tax", tax);
        ajaxRequest.appendRequest("usedState", state);
        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("Treffer: 0");
        }
    },

    _disableExFields: function()
    {
        // color
        if (this.colorCtrlId != null)
        {
            var colorCtrl = document["cb_" + this.colorCtrlId];
            colorCtrl.setEnabled(false);
        }

        // cylinder count
        if (this.cyldrCtrlId != null)
        {
            var cyldrCtrl = document["cb_" + this.cyldrCtrlId];
            cyldrCtrl.setEnabled(false);
        }

        // fuel
        if (this.fuelCtrlId != null)
        {
            var fuelCtrl = document["cb_" + this.fuelCtrlId];
            fuelCtrl.setEnabled(false);
        }

        // milage
        var milgFCtrl = document["cb_" + this.milgFCtrlId];
        var milgTCtrl = document["cb_" + this.milgTCtrlId];
        milgFCtrl.setEnabled(false);
        milgTCtrl.setEnabled(false);

        // power
        if (this.powerFCtrlId != null)
        {
            $(this.powerFCtrlId).disabled = true;
            $(this.powerFCtrlId).className = "txt_disabled";
        }
        if (this.powerTCtrlId != null)
        {
            $(this.powerTCtrlId).disabled = true;
            $(this.powerTCtrlId).className = "txt_disabled";
        }

        // cubic
        if (this.ccmFCtrlId != null)
        {
            $(this.ccmFCtrlId).disabled = true;
            $(this.ccmFCtrlId).className = "txt_disabled";
        }
        if (this.ccmTCtrlId != null)
        {
            $(this.ccmTCtrlId).disabled = true;
            $(this.ccmTCtrlId).className = "txt_disabled";
        }
    },

    _clearExFields: function()
    {
        // color
        if (this.colorCtrlId != null)
        {
            var colorCtrl = document["cb_" + this.colorCtrlId];
            colorCtrl.changeSelection(-1, true);
        }

        // cylinder count
        if (this.cyldrCtrlId != null)
        {
            var cyldrCtrl = document["cb_" + this.cyldrCtrlId];
            cyldrCtrl.changeSelection(-1, true);
        }

        // fuel
        if (this.fuelCtrlId != null)
        {
            var fuelCtrl = document["cb_" + this.fuelCtrlId];
            fuelCtrl.changeSelection(-1, true);
        }

        // milage
        var milgFCtrl = document["cb_" + this.milgFCtrlId];
        var milgTCtrl = document["cb_" + this.milgTCtrlId];
        milgFCtrl.changeSelection(-1, true);
        milgTCtrl.changeSelection(-1, true);

        // power
        if (this.powerFCtrlId != null)
        {
            $(this.powerFCtrlId).value = "";
        }
        if (this.powerTCtrlId != null)
        {
            $(this.powerTCtrlId).value = "";
        }

        // cubic
        if (this.ccmFCtrlId != null)
        {
            $(this.ccmFCtrlId).value = "";
        }
        if (this.ccmTCtrlId != null)
        {
            $(this.ccmTCtrlId).value = "";
        }
    }
};
