﻿/* ********************************************************** */
/* ECBCalculator declaration                                  */        
/* ********************************************************** */
if (!this.ECBCalculator) {
    ECBCalculator = {};
}

(function() {
    var 
        QTN_STATE_DRAFT = "1",
        QTN_STATE_ACTIVE = "2",
        QTN_STATE_TRANSFERRED = "3",
        QTN_STATE_PUBLISHED = "9";
    var questionnaireState = QTN_STATE_DRAFT;

    // Updates the state after a callback.
    var updateStateAfterCallback = function() {
        state = GuiController.getCallbackValue('QuestionnaireState');
        if (state) {
            ECBCalculator.setState(state);
        }
    };

    // Sets the questionnaire state.                               
    if (typeof ECBCalculator.setState !== 'function') {
        ECBCalculator.setState = function(state) {
            ECBCalculator.questionnaireState = state;
            questionnaireState = state;
        };
    }

    // State type helpers.
    if (typeof ECBCalculator.isStateDraft !== 'function') {
        ECBCalculator.isStateDraft = function() {
            var result = false;
            if (questionnaireState === QTN_STATE_DRAFT) {
                result = true;
            }
            return result;
        };
    }
    if (typeof ECBCalculator.isStateActive !== 'function') {
        ECBCalculator.isStateActive = function() {
            var result = false;
            if (questionnaireState === QTN_STATE_ACTIVE) {
                result = true;
            }
            return result;
        };
    }
    if (typeof ECBCalculator.isStateTransferred !== 'function') {
        ECBCalculator.isStateTransferred = function() {
            var result = false;
            if (questionnaireState === QTN_STATE_TRANSFERRED) {
                result = true;
            }
            return result;
        };
    }
    if (typeof ECBCalculator.isStatePublished !== 'function') {
        ECBCalculator.isStatePublished = function() {
            var result = false;
            if (questionnaireState === QTN_STATE_PUBLISHED) {
                result = true;
            }
            return result;
        };
    }

    // On page loaded event handler                               
    if (typeof ECBCalculator.onPageLoad !== 'function') {
        ECBCalculator.onPageLoad = function(s, e) {
            ECBCalculator.tabManager.setActiveTab(0);
            GuiController.registerCallbackHandler(updateStateAfterCallback);
            ECBCalculator.setState(GuiController.getCachedValue('QuestionnaireState'));
        };
    }

    // Renders the UI after a significant event (e.g. tab load)   
    if (typeof ECBCalculator.renderUI !== 'function') {
        ECBCalculator.renderUI = function() {
            if (window['_t1p2_BuildingCategory'] !== undefined) {
                ECBCalculator.tabManager.tab1.onBuildingTypeChange(_t1p2_BuildingCategory.GetValue());
            }
            if (window['_t2p1_BasementOrCellar'] !== undefined) {
                ECBCalculator.tabManager.tab2.toggleBasementOrCellar(_t2p1_BasementOrCellar.GetSelectedIndex());
            }
            if (window['_t2p1_AtticFloorOrRoof'] !== undefined) {
                ECBCalculator.tabManager.tab2.toggleAtticFloorOrRoof(_t2p1_AtticFloorOrRoof.GetSelectedIndex());
            }
            if (window['_t2p2_HasRoofRenovated'] !== undefined) {
                ECBCalculator.tabManager.tab2.toggleHasRoofRenovated(_t2p2_HasRoofRenovated.GetSelectedIndex());
            }
            if (window['_t2p3_HasAtticFloorRenovated'] !== undefined) {
                ECBCalculator.tabManager.tab2.toggleHasAtticFloorRenovated(_t2p3_HasAtticFloorRenovated.GetSelectedIndex());
            }
            if (window['_t2p4_HasExternalWallsRenovated'] !== undefined) {
                ECBCalculator.tabManager.tab2.toggleHasExternalWallsRenovated(_t2p4_HasExternalWallsRenovated.GetSelectedIndex());
            }
            if (window['_t2p5_HasPartsNearEarthRenovated'] !== undefined) {
                ECBCalculator.tabManager.tab2.toggleHasPartsNearEarthRenovated(_t2p5_HasPartsNearEarthRenovated.GetSelectedIndex());
            }
            if (window['_t2p6_HasWindowsRenovated'] !== undefined) {
                ECBCalculator.tabManager.tab2.toggleHasWindowsRenovated(_t2p6_HasWindowsRenovated.GetSelectedIndex());
            }
            if (window['_t4p3_HasVentilation'] !== undefined) {
                ECBCalculator.tabManager.tab4.toggleVentilationTypeP3(_t4p3_HasVentilation.GetSelectedIndex());
            }
            if (window['_t4p4_HasVentilation'] !== undefined) {
                ECBCalculator.tabManager.tab4.toggleVentilationTypeP4(_t4p4_HasVentilation.GetSelectedIndex());
            }
            if (window['_expertSearchResultCallbackPanel'] !== undefined) {
                ECBCalculator.tabManager.tab6.onTabLoaded();
            }
            // Refresh building technical data.
            if (!(ECBCalculator.tabManager.tab5 === undefined ||
                  ECBCalculator.tabManager.tab5 === null)) {
                ECBCalculator.tabManager.tab5.toggleBuildingTechnicalData();
            }
        };
    }

    // Toggles the state of an input control                      
    if (typeof ECBCalculator.toggleInputControlState !== 'function') {
        ECBCalculator.toggleInputControlState = function(controlId, state, clearValue) {
            var control = window[controlId]; // this is DX specific as DX loads all controls into the window global dictionary..
            if (control) {
                if (state) {
                    // Check if the edit mode is in update or insert mode.
                    if (GuiController.getEditMode() === 2 ||
                        GuiController.getEditMode() === 3) {
                        control.SetVisible(true);
                        control.SetClientVisible(true);
                    }
                }
                else {
                    if (clearValue) {
                        control.SetValue(null);
                    }
                    control.SetVisible(false);
                    control.SetClientVisible(false);
                }
            }
        };
    }

})();

/* ********************************************************** */
/* Add page loaded event hander                               */        
/* ********************************************************** */
(function () {
    $(document).ready(function() {
        ECBCalculator.onPageLoad();
    });
})();