﻿//Calculator business and interface logic
var calculator = {

    //Prepare and display the calculator
    init: function() {

        this.localize('span');
        this.localize('h2');
        this.localizeImages();

        this.activateTab('tIncreasedSprayCapacity');

        document.getElementById('mainDiv').style.visibility = 'visible';

        return true;
    },

    //Activates the specified tab and hides the rest
    activateTab: function(tabId) {

        var chemicalSavingsTab = document.getElementById('tChemicalSavings');
        var chemicalSavingsTabButton = document.getElementById('btnActivateChemicalSavings');

        var increasedSprayCapacityTab = document.getElementById('tIncreasedSprayCapacity');
        var increasedSprayCapacityTabButton = document.getElementById('btnActivateIncreasedSprayCapacity');

        switch (tabId) {
            case 'tChemicalSavings':
                increasedSprayCapacityTab.style.display = 'none';
                increasedSprayCapacityTabButton.disabled = false;
                increasedSprayCapacityTabButton.className = 'able';

                chemicalSavingsTab.style.display = 'block';
                chemicalSavingsTabButton.disabled = true;
                chemicalSavingsTabButton.className = '';
                break;
            case 'tIncreasedSprayCapacity':

                chemicalSavingsTab.style.display = 'none';
                chemicalSavingsTabButton.disabled = false;
                chemicalSavingsTabButton.className = 'able';

                increasedSprayCapacityTabButton.className = '';
                increasedSprayCapacityTab.style.display = 'block';
                increasedSprayCapacityTabButton.disabled = true;
                break;
        }

        return true;
    },

    //Calculates the values for IncreasedSprayCapacity tab
    calculateIncreasedSprayCapacity: function() {

        //validate that all textboxes are filled in correctly
        var A = parseFloat(document.getElementById('paramSprayedAreaPerYear').value);
        var B = parseFloat(document.getElementById('paramSprayerTankCapacity').value);
        var C = parseFloat(document.getElementById('paramBoomWidth').value);
        var D = parseFloat(document.getElementById('paramFillingTime').value);
        var E = parseFloat(document.getElementById('paramWaterAmountConventional').value);
        var F = parseFloat(document.getElementById('paramWaterAmountTwin').value);
        var G = parseFloat(document.getElementById('paramDrivingSpeedConventional').value);
        var H = parseFloat(document.getElementById('paramDrivingSpeedTwin').value);

        if (isNaN(A + B + C + D + E + F + G + H)) {
            alert(lang.msgFillIncreasedSprayCapacityParams);
            return false;
        }

        //calculate values
        var workRateConventional = eval(config.workRateConventionalFormula);
        var workRateTwin = eval(config.workRateTwinFormula);
        var hoursPerYearConventional = eval(config.hoursPerYearConventionalFormula);
        var hoursPerYearTwin = eval(config.hoursPerYearTwinFormula);
        var savedHoursPerYear = eval(config.savedHoursPerYearFormula);
        var savedHoursPercant = eval(config.savedHoursPercantFormula);
        var extraCapacityPerYear = eval(config.extraCapacityPerYearFormula);
        
        //display values
        document.getElementById('valResultWorkRateConventional').innerHTML = Math.round(workRateConventional * 10) / 10;
        document.getElementById('valResultWorkRateTwin').innerHTML = Math.round(workRateTwin * 10) / 10;
        document.getElementById('valResultHoursPerYearConventional').innerHTML = Math.round(hoursPerYearConventional * 10) / 10;
        document.getElementById('valResultHoursPerYearTwin').innerHTML = Math.round(hoursPerYearTwin * 10) / 10;
        document.getElementById('valResultSavedHoursPerYear').innerHTML = Math.round(savedHoursPerYear * 10) / 10;
        document.getElementById('valSavedTimePercent').innerHTML = Math.round(savedHoursPercant * 10) / 10;
        document.getElementById('valResultExtraCapacity').innerHTML = Math.round(extraCapacityPerYear * 10) / 10;        

        document.getElementById('tIncreasedSprayCapacityResults').style.display = 'block';
        document.getElementById('tIncreasedSprayCapacityResultsSplash').style.display = 'none';

        return true;

    },

    //Resets the values for IncreasedSprayCapacity tab
    resetIncreasedSprayCapacity: function() {

        document.getElementById('valResultWorkRateConventional').innerHTML = '';
        document.getElementById('valResultWorkRateTwin').innerHTML = '';
        document.getElementById('valResultHoursPerYearConventional').innerHTML = '';
        document.getElementById('valResultHoursPerYearTwin').innerHTML = '';
        document.getElementById('valResultSavedHoursPerYear').innerHTML = '';
        document.getElementById('valSavedTimePercent').innerHTML = '';
        document.getElementById('valResultExtraCapacity').innerHTML = '';
        
        document.getElementById('paramSprayedAreaPerYear').value = '';
        document.getElementById('paramSprayerTankCapacity').value = '';
        document.getElementById('paramBoomWidth').value = '';
        document.getElementById('paramFillingTime').value = '';
        document.getElementById('paramWaterAmountConventional').value = '';
        document.getElementById('paramWaterAmountTwin').value = '';
        document.getElementById('paramDrivingSpeedConventional').value = '';
        document.getElementById('paramDrivingSpeedTwin').value = '';

        document.getElementById('tIncreasedSprayCapacityResults').style.display = 'none';
        document.getElementById('tIncreasedSprayCapacityResultsSplash').style.display = 'block';

    },

    //Calculates the values for Chemical Savings tab
    calculateChemicalSavings: function() {
        //validate that all textboxes are filled in correctly
        var iLifetimeText = document.getElementById('paramLifetimeText');
        var iYearlyCostsText = document.getElementById('paramYearlyCostsText');
        var iExpectedSavingsText = document.getElementById('paramExpectedSavings');

        var X = parseInt(iLifetimeText.value);
        var Y = parseInt(iYearlyCostsText.value);
        var Z = parseInt(iExpectedSavingsText.value);

        if (isNaN(X) || isNaN(Y) || isNaN(Z)) {
            alert(lang.msgFillChemicalSavingsParams);
            return false;
        }

        //calculate and display the result
        var chemicalsCost = eval(config.chemicalsCostFormula);
        var periodSavings = eval(config.periodSavingsFormula);
        var yearlySavings = eval(config.yearlySavingsFormula);

        var iChemicalsCost = document.getElementById('valPeriodChemicalsCost');
        iChemicalsCost.innerHTML = chemicalsCost;

        var iPeriodSavings = document.getElementById('valPeriodSavings');
        iPeriodSavings.innerHTML = periodSavings;

        var iYearlySavings = document.getElementById('valYearlySavings');
        iYearlySavings.innerHTML = yearlySavings;

        document.getElementById('tChemicalSavingsResultsSplash').style.display = 'none';
        document.getElementById('tChemicalSavingsResults').style.display = 'block';
        
        return true;
    },

    resetChemicalSavings: function() {

        var iLifetimeText = document.getElementById('paramLifetimeText');
        iLifetimeText.value = '';

        var iYearlyCostsText = document.getElementById('paramYearlyCostsText');
        iYearlyCostsText.value = '';

        var iExpectedSavingsText = document.getElementById('paramExpectedSavings');
        iExpectedSavingsText.value = '';

        var iChemicalsCost = document.getElementById('valPeriodChemicalsCost');
        iChemicalsCost.innerHTML = '';

        var iPeriodSavings = document.getElementById('valPeriodSavings');
        iPeriodSavings.innerHTML = '';

        var iYearlySavings = document.getElementById('valYearlySavings');
        iYearlySavings.innerHTML = '';

        document.getElementById('tChemicalSavingsResultsSplash').style.display = 'block';
        document.getElementById('tChemicalSavingsResults').style.display = 'none';

        return true;
    },

    //Validates numbers entered into textboxes
    validateInput: function(el, allowDecimals) {

        if (allowDecimals) {

            el.value = el.value.replace(',', '.');
            var val = parseFloat(el.value);
            if (isNaN(val)) el.value = '';
        }
        else {
            var intval = parseInt(el.value);
            if (isNaN(intval)) el.value = ''
            else el.value = intval;
        }

        return true;
    },

    //enables the tooltip hidden near 'i' image
    displayTooltip: function(elementId) {
        var oTooltip = document.getElementById(elementId);
        if (null != oTooltip) {
            oTooltip.style.display = 'block';
        }
    },

    //hides the tooltip near 'i' image
    hideTooltip: function(elementId) {
        var oTooltip = document.getElementById(elementId);
        if (null != oTooltip) {
            oTooltip.style.display = 'none';
        }
    },

    //Performs automated localization: substitutes inner HTML of <span> elements
    //whose id is equal to one of the lang object properties with the value of that property   
    localize: function(tagName) {

        var arr = document.getElementsByTagName(tagName);
        for (var i = 0; i < arr.length; i++) {
            var span = arr[i];
            for (var j in lang) {
                if (span.id == j) {
                    span.innerHTML = lang[j];
                }
            }
        }
    },

    localizeImages: function() {

        var arr = document.getElementsByTagName('img');
        for (var i = 0; i < arr.length; i++) {
            var img = arr[i];
            for (var j in lang) {
                if (img.id == j) {
                    img.src = lang[j];
                }
            }
        }
    }

}
