﻿/* ********************************************************** */
/* Initialise tab manager                                     */
/* ********************************************************** */
(function() {

    // Add tab to the tab manager.
    if (!ECBCalculator.tabManager.tab3) {
        var tab = ECBCalculatorTab({ index: 2, validationGroupCount: 4 });
        ECBCalculator.tabManager.addTab(tab);
        ECBCalculator.tabManager.tab3 = tab;
    }

})();

/* ********************************************************** */
/* Initialise tab                                             */
/* ********************************************************** */
(function() {

    /* *************************************** */
    /*  Tab3Panel1                             */
    /* *************************************** */

    /* Control sun collector panel toggling */
    /* step 1: remove/restore item 3 from _t3p2_WarmWaterHeatingLink */
    /* step 2: show/hide WarmWaterSupplyType and RenovationYear */
    if (typeof ECBCalculator.tabManager.tab3.toggleSunCollectorPanel !== 'function') {
        ECBCalculator.tabManager.tab3.toggleSunCollectorPanel = function(sunHeatingType) {
            var sunHeatingTypeValue = parseInt(sunHeatingType);
            // step 1
            if (sunHeatingTypeValue === 2) {
                // remove code item WW_MFH_002 value 3 : index in combo 2
                KeaneUtils.removeItemAt('_t3p2_WarmWaterHeatingLink', 2);
            }
            else {
                // restore code item WW_MFH_002 value 2 : index in combo 3
                KeaneUtils.insertItemAt('_t3p2_WarmWaterHeatingLink', 2, 3);
            }
            // step 2
            if (sunHeatingTypeValue <= 2 || (sunHeatingTypeValue === 3 &&
                 _t3p2_WarmWaterHeatingLink.GetValue() === "1")) {
                ECBCalculator.tabManager.tab3.toggleStateRenovationYear(false);
            }
            else {
                ECBCalculator.tabManager.tab3.toggleStateRenovationYear(true);
            }
            // restore Keep temperature
            KeaneUtils.toggleVisibilityState('_t3p2_div6', true);
        };
    }

    /* Control warm water linked toggling */
    /* step 1: show/hide WarmWaterSupplyType and RenovationYear */
    /* step 2: remove/restore items 3..4 in WarmWaterSupplyType */
    /* step 3: show/hide KeepTemperature */
    /* step 4: show/hide sommer label when value = 2 */
    if (typeof ECBCalculator.tabManager.tab3.toggleWarmWaterHeatingLink !== 'function') {
        ECBCalculator.tabManager.tab3.toggleWarmWaterHeatingLink = function(valueAsString) {
            var value = parseInt(valueAsString);
            // step 1
            if (value >= 2 && _t3p4_SunCollectors.GetValue() === "3") {
                ECBCalculator.tabManager.tab3.toggleStateRenovationYear(true);
            }
            else {
                ECBCalculator.tabManager.tab3.toggleStateRenovationYear(false);
            }
            // step 2
            if (value === 3) {
                KeaneUtils.insertItemAtName('_t3p2_WarmWaterProduction', 2, 3, '_t3p2_WarmWaterProduction3');
                KeaneUtils.insertItemAtName('_t3p2_WarmWaterProduction', 3, 4, '_t3p2_WarmWaterProduction4');
            }
            else {
                KeaneUtils.removeItemAtName('_t3p2_WarmWaterProduction', 3, '_t3p2_WarmWaterProduction4');
                KeaneUtils.removeItemAtName('_t3p2_WarmWaterProduction', 2, '_t3p2_WarmWaterProduction3');
            }
            // step 3
            if (_t3p2_WarmWaterProduction.GetValue() >= "3") {
                KeaneUtils.toggleVisibilityState('_t3p2_div6', false);
            }
            else {
                KeaneUtils.toggleVisibilityState('_t3p2_div6', true);
            }
            // step 4
            if (value === 2) {
                _t3p2_WarmWaterProductionLabel.SetVisible(false);
                _t3p2_WarmWaterProductionLabel2.SetVisible(true);
            }
            else {
                _t3p2_WarmWaterProductionLabel.SetVisible(true);
                _t3p2_WarmWaterProductionLabel2.SetVisible(false);            
            }
        };
    }

    /* Control warm water supply type toggling */
    /* step 1: show/hide KeepTemperature if value >= 3 */
    if (typeof ECBCalculator.tabManager.tab3.toggleWarmWaterSupplyType !== 'function') {
        ECBCalculator.tabManager.tab3.toggleWarmWaterSupplyType = function(valueAsString) {
            var value = parseInt(valueAsString);
            if (value >= 3) {
                KeaneUtils.toggleVisibilityState('_t3p2_div6', false);
            }
            else {
                KeaneUtils.toggleVisibilityState('_t3p2_div6', true);
            }
        }
    }

    // Show/hide _t3p2_WarmWaterProduction and _t3p2_RenovationYear
    if (typeof ECBCalculator.tabManager.tab3.toggleStateRenovationYear !== 'function') {
        ECBCalculator.tabManager.tab3.toggleStateRenovationYear = function(state) {
            ECBCalculator.toggleInputControlState('_t3p2_RenovationYear', state, true);
            KeaneUtils.toggleVisibilityState('_t3p2_div3', state);
            KeaneUtils.toggleVisibilityState('_t3p2_div4', state);
            // Initialize renovation year.
            ECBCalculator.tabManager.tab3.initializeRenovationYear('_t3p1_RenovationYear', state);
            ECBCalculator.tabManager.tab3.initializeRenovationYear('_t3p2_RenovationYear', state);
        }
    }

    /* Consumption Type change event handler. */
    if (typeof ECBCalculator.tabManager.tab3.onConsumptionTypeChange !== 'function') {
        ECBCalculator.tabManager.tab3.onConsumptionTypeChange = function(consumptionTypeAsString) {
            if (consumptionTypeAsString) {
                // Derive consumption type.
                var consumptionType = parseInt(consumptionTypeAsString);
                var validatorState = (consumptionType === 1 ? true : false);
                ECBCalculator.toggleInputControlState('_t3p3_WarmWaterConsumption', validatorState, true);
                ECBCalculator.toggleInputControlState('_t3p3_WarmWaterConsumptionEnergySource', validatorState, true);
                // Toggle visibility state.
                var visibilityState = (consumptionType === 1 ? true : false);
                KeaneUtils.toggleVisibilityState('_t3p3_div3', visibilityState);
                // Toggle _t3p3_HeatingConsumptionLabel caption label.
                var labelText = (consumptionType === 1 ?
                                 GuiController.getCachedValue('HeatingConsumptionLabel') :
                                 GuiController.getCachedValue('WarmWaterAndHeatingConsumptionLabel'));
                var label = window['_t3p3_HeatingConsumptionLabel'];
                label.SetText(labelText);
            }
        };
    }

    /* Initialize renovation year */
    if (typeof ECBCalculator.tabManager.tab3.initializeRenovationYear !== 'function') {
        ECBCalculator.tabManager.tab3.initializeRenovationYear = function(renovationYearControl, visibilityState) {
            if (visibilityState) {
                var controlConstYear = window['_t1p2_YearOfConstruction'];
                var controlRenovYear = window[renovationYearControl];
                if (controlConstYear != null && controlRenovYear != null &&
                            controlRenovYear.GetValue() < controlConstYear.GetValue()) {
                    controlRenovYear.SetValue(controlConstYear.GetValue());
                }
            }
        };
    }
})();

/* ********************************************************** */
/* Reset unit selector on heating consumption text changed    */
/* ********************************************************** */
(function() {

    if (typeof ECBCalculator.tabManager.tab3.onHeatingConsumptionTextChanged !== 'function') {
        ECBCalculator.tabManager.tab3.onHeatingConsumptionTextChanged = function(value) {
            if (value == '') {
                var selector = window['_t3p3_HeatingConsumptionEnergySource'];
                selector.SetValue(-1);
            }
        }
    }
})();

/* ********************************************************** */
/* Reset unit selector on warm water consumption text changed */
/* ********************************************************** */
(function() {

    if (typeof ECBCalculator.tabManager.tab3.onWWConsumptionTextChanged !== 'function') {
        ECBCalculator.tabManager.tab3.onWWConsumptionTextChanged = function(value) {
            if (value == '') {
                var selector = window['_t3p3_WarmWaterConsumptionEnergySource'];
                selector.SetValue(-1);
            }
        }
    }
})();