﻿// ****************************************
// * AreaUtil
// ****************************************

ClimaController.PageLogic.AreaUtil =
{

    SwitchView: function(reportArea, val, posid, inputName, section, meassure) {

        var oSection = document.getElementById(section);
        var oMeassure = document.getElementById(meassure);
        var styleDisplay = "block";

        if (!oSection) return;

        switch (val) {
            case posid:
                styleDisplay = "block";
                break;
            default:
                styleDisplay = "none";
                break;
        }

        if (oMeassure) oMeassure.style.display = styleDisplay;
        oSection.style.display = styleDisplay;

        ClimaController.Actions.Request.UpdateReportAreaInput(reportArea, inputName, val);
    },

    RemoveValueFromDropDown: function(dropdown, valueToRemove) {
        // Remove value
        var valueToRemoveFound = false;
        var index = 0;
        for (i = 0; i < dropdown.length && !valueToRemoveFound; i++) {
            if (dropdown.options[i].value == valueToRemove) {
                index = i;
                valueToRemoveFound = true;
            }
        }
        if (valueToRemoveFound) dropdown.options[index] = null;
    },

    AddValueToDropDown: function(dropdown, valueToAdd, textToAdd) {
        // valueToAdd exists?
        var valueToAddExists = false;
        for (i = 0; i < dropdown.length && !valueToAddExists; i++) {
            if (dropdown.options[i].value == valueToAdd) {
                valueToAddExists = true;
            }
        }

        // Add 'option'
        if (!valueToAddExists) {
            var oOption = document.createElement("option");
            oOption.value = valueToAdd;
            oOption.text = textToAdd;
            dropdown.options[dropdown.options.length] = oOption;
        }
    },

    EmissionFactorRound: function(theValueToRound) {
        if (!theValueToRound) return "";
        var val = theValueToRound.replace(",", ".");
        if (isNaN(val)) return theValueToRound;
        return ('' + (Math.round(val * 10000) / 10000)).replace(".", ",");
    },

    RoundNumber: function(theValueToRound, numberOfDecimals) {
        if ((theValueToRound == null) || numberOfDecimals == null || isNaN(numberOfDecimals) || isNaN(theValueToRound)) return;
        return Math.round(theValueToRound * Math.pow(10, numberOfDecimals)) / Math.pow(10, numberOfDecimals);
    },

    RemoveAllValuesFromDropDown: function(dropdown) {
        dropdown.options.length = 0;
    },

    AddValuesToDropDown: function(dropdown, valueTextArray) {
        for (i = 0; i < valueTextArray.length; i++) {
            ClimaController.PageLogic.AreaUtil.AddValueToDropDown(dropdown, valueTextArray[i].Value, valueTextArray[i].Text);
        }
    },

    SelectByValue: function(dropdown, value) {
        for (i = 0; i < dropdown.options.length; i++) {
            if (dropdown.options[i].value == value) {
                dropdown.selectedIndex = i;
                break;
            }
        }
    },

    SelectByText: function(dropdown, text) {
        for (i = 0; i < dropdown.options.length; i++) {
            if (dropdown.options[i].text == text) {
                dropdown.selectedIndex = i;
                break;
            }
        }
    },

    GetTextByValue: function(dropdown, value) {
        var text;
        for (i = 0; i < dropdown.options.length; i++) {
            if (dropdown.options[i].value == value) {
                text = dropdown.options[i].text;
                break;
            }
        }
        return text;
    }
}
    
    
