﻿// ---------------------------------------------------------------------
// Description:		SearchPanel.js
//					ClientSide Script for SearchPanel.cs Control
// 
// Autor:			Herbert Granofszky
// Date:			23.10.2007
// 
// (c) by team ModulAcht. All rights reserved
// ---------------------------------------------------------------------

var SearchPanel =
{
    tabContainerCtrlId: null,
    keyNumberPanelId: null,
    extSearchPanelId: null,
    quickSearchPanelId: null,
    keyNumber: null,
    compGrp: null,
    component: null,
    color: null,
    cylinder: null,
    fuel: null,
    milageFrom: null,
    milageTill: null,
    yocFrom: null,
    yocTill: null,
    cubicFrom: null,
    cubicTill: null,
    powerFrom: null,
    powerTill: null,
    country: null,
    plz: null,
    cost: null,
    tax: null,
    state: null,
    selectedTabId: null,

    initialize: function()
    {
        //eval(this.tabContainerCtrlId + ".registerCallback(" + this.tabContainerCtrlId + ".EVENTID_TABCHANGE, SearchPanel.onTabChange.bind(SearchPanel));");  
    },

    notifyExtSearch: function(argChangedPropertyName)
    {
        // check if there is an Extended Search Panel to synchronize with
        if (ExtendedSearch == null)
        {
            return;
        }

        // don't notify extended search if it is the active tab
        if (this.selectedTabId == "erweitert")
        {
            return;
        }

        // initialize extended search fields from key number info
        if (argChangedPropertyName == "keyNumber")
        {
            if (this.keyNumber != null)
            {
                ExtendedSearch.initFromKeyNumberInfo(this.keyNumber);
            }
            else
            {
                ExtendedSearch.resetAll();
            }
        }
        else if (argChangedPropertyName == "compGrp")
        {
            ExtendedSearch.initCompGrp(this.compGrp);
        }
        // component
        else if (argChangedPropertyName == "component")
        {
            ExtendedSearch.initComponent(this.component);
        }
        // color
        else if (argChangedPropertyName == "color")
        {
            ExtendedSearch.initColor(this.color);
        }
        // cylinder
        else if (argChangedPropertyName == "cylinder")
        {
            ExtendedSearch.initCylinder(this.cylinder);
        }
        // fuel
        else if (argChangedPropertyName == "fuel")
        {
            ExtendedSearch.initFuel(this.fuel);
        }
        // milage from        
        else if (argChangedPropertyName == "milageFrom")
        {
            ExtendedSearch.initMilageFrom(this.milageFrom);
        }
        // milage till
        else if (argChangedPropertyName == "milageTill")
        {
            ExtendedSearch.initMilageTill(this.milageTill);
        }
        // year of construction from
        else if (argChangedPropertyName == "yocFrom")
        {
            ExtendedSearch.initYocFrom(this.yocFrom);
        }
        // year of construction till
        else if (argChangedPropertyName == "yocTill")
        {
            ExtendedSearch.initYocTill(this.yocTill);
        }
        // cubic from
        else if (argChangedPropertyName == "cubicFrom")
        {
            ExtendedSearch.initCubicFrom(this.cubicFrom);
        }
        // cubic till
        else if (argChangedPropertyName == "cubicTill")
        {
            ExtendedSearch.initCubicTill(this.cubicTill);
        }
        // power from
        else if (argChangedPropertyName == "powerFrom")
        {
            ExtendedSearch.initPowerFrom(this.powerFrom);
        }
        // power till
        else if (argChangedPropertyName == "powerTill")
        {
            ExtendedSearch.initPowerTill(this.powerTill);
        }
        // country
        else if (argChangedPropertyName == "country")
        {
            ExtendedSearch.initCountry(this.country);
        }
        // plz
        else if (argChangedPropertyName == "plz")
        {
            ExtendedSearch.initPlz(this.plz);
        }
        // cost
        else if (argChangedPropertyName == "cost")
        {
            ExtendedSearch.initCost(this.cost);
        }
        // tax
        else if (argChangedPropertyName == "tax")
        {
            ExtendedSearch.initTax(this.tax);
        }
        // state  
        else if (argChangedPropertyName == "state")
        {
            ExtendedSearch.initState(this.state);
        }
    },

    notifyKeyNumberSearch: function(argChangedPropertyName)
    {
        // check if there is an Extended Search Panel to synchronize with
        if (KeyNumberSearch == null)
        {
            return;
        }

        // don't notify key number search if it is the active tab
        if (this.selectedTabId == this.keyNumberPanelId)
        {
            return;
        }

        if (argChangedPropertyName == "category" || argChangedPropertyName == "producer")
        {
            KeyNumberSearch.resetAll();
        }
        else if (argChangedPropertyName == "keyNumber")
        {
            // initialize extended search fields from key number info
            if (this.keyNumber != null)
            {
                KeyNumberSearch.initFromKeyNumberInfo(this.keyNumber);
            }
            else
            {
                KeyNumberSearch.resetTSN();
            }
        }
        else if (argChangedPropertyName == "keyNumberJson")
        {
            if (this.keyNumber != null)
            {
                KeyNumberSearch.initFromKeyNumberJson(this.keyNumber);
            }
            else
            {
                KeyNumberSearch.resetTSN();
            }
        }
        else if (argChangedPropertyName == "compGrp")
        {
            KeyNumberSearch.initCompGrp(this.compGrp);
        }
        // component
        else if (argChangedPropertyName == "component")
        {
            KeyNumberSearch.initComponent(this.component);
        }
        // color
        else if (argChangedPropertyName == "color")
        {
            KeyNumberSearch.initColor(this.color);
        }
        // cylinder
        else if (argChangedPropertyName == "cylinder")
        {
            KeyNumberSearch.initCylinder(this.cylinder);
        }
        // fuel
        else if (argChangedPropertyName == "fuel")
        {
            KeyNumberSearch.initFuel(this.fuel);
        }
        // milage from        
        else if (argChangedPropertyName == "milageFrom")
        {
            KeyNumberSearch.initMilageFrom(this.milageFrom);
        }
        // milage till
        else if (argChangedPropertyName == "milageTill")
        {
            KeyNumberSearch.initMilageTill(this.milageTill);
        }
        // year of construction from
        else if (argChangedPropertyName == "yocFrom")
        {
            KeyNumberSearch.initYocFrom(this.yocFrom);
        }
        // year of construction till
        else if (argChangedPropertyName == "yocTill")
        {
            KeyNumberSearch.initYocTill(this.yocTill);
        }
        // cubic from
        else if (argChangedPropertyName == "cubicFrom")
        {
            KeyNumberSearch.initCubicFrom(this.cubicFrom);
        }
        // cubic till
        else if (argChangedPropertyName == "cubicTill")
        {
            KeyNumberSearch.initCubicTill(this.cubicTill);
        }
        // power from
        else if (argChangedPropertyName == "powerFrom")
        {
            KeyNumberSearch.initPowerFrom(this.powerFrom);
        }
        // power till
        else if (argChangedPropertyName == "powerTill")
        {
            KeyNumberSearch.initPowerTill(this.powerTill);
        }
        // plz
        else if (argChangedPropertyName == "plz")
        {
            KeyNumberSearch.initPlz(this.plz);
        }
        else if (argChangedPropertyName == "country")
        {
            KeyNumberSearch.initCountry(this.country);
        }
        // cost
        else if (argChangedPropertyName == "cost")
        {
            KeyNumberSearch.initCost(this.cost);
        }
        // tax
        else if (argChangedPropertyName == "tax")
        {
            KeyNumberSearch.initTax(this.tax);
        }
        // state  
        else if (argChangedPropertyName == "state")
        {
            KeyNumberSearch.initState(this.state);
        }
    },

    onTabChange: function(argTab)
    {
        this.selectedTabId = argTab;

        if (argTab == this.keyNumberPanelId)
        {
            if (KeyNumberSearch != null)
            {
                KeyNumberSearch.focusDefaultElement();
            }
        }
        else if (argTab == "erweitert")
        {
            if (ExtendedSearch != null)
            {
                this._preLoadExtSearchDropDownWhenReady();
            }
        }
        else if (argTab == this.quickSearchPanelId)
        {
            if (QuickSearch != null)
            {
                QuickSearch.focusDefaultElement();
            }
        }
    },

    _preLoadExtSearchDropDownWhenReady: function()
    {
        if (this.tabContainerCtrlId == null)
        {
            setTimeout(this._preLoadExtSearchDropDownWhenReady.bind(this), 100);
            return;
        }
        if (!this._isInsideSearchDialog())
        {
            ExtendedSearch.preloadDropDownBoxes();
        }
    },

    _isInsideSearchDialog: function()
    {
        if (this.tabContainerCtrlId == null)
        {
            return false;
        }
        var dialogElem = $(this.tabContainerCtrlId).up('.searchdialog');
        if (dialogElem == null)
        {
            return false;
        }
        return true;
    }
};
