﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BOK");

BOK.labor_cost_calc = function(element) {
    // Constants
    this._workstationCost = 45;
    this._serverCost = 195;
    this._networkCost = 295;

    BOK.labor_cost_calc.initializeBase(this, [element]);
}

BOK.labor_cost_calc.prototype = {
    // Init
    initialize: function() {
        BOK.labor_cost_calc.callBaseMethod(this, 'initialize');

        $addHandlers(this._numberOfWorkstationsTextBox, {
            'change': this._update
        }, this);

        $addHandlers(this._numberOfServersTextBox, {
            'change': this._update
        }, this);

        $addHandlers(this._numberOfNetworksTextBox, {
            'change': this._update
        }, this);

        $addHandlers(this._vacationDaysTextBox, {
            'change': this._update
        }, this);

        $addHandlers(this._sickDaysTextBox, {
            'change': this._update
        }, this);

        $addHandlers(this._techSupportSalaryTextBox, {
            'change': this._update,
            'keyup': this._update
        }, this);

        this._load();
    },

    // Dispose
    dispose: function() {
        $clearHandlers(this._numberOfWorkstationsTextBox);
        $clearHandlers(this._numberOfServersTextBox);
        $clearHandlers(this._numberOfNetworksTextBox);
        $clearHandlers(this._vacationDaysTextBox);
        $clearHandlers(this._sickDaysTextBox);
        $clearHandlers(this._techSupportSalaryTextBox);
        BOK.labor_cost_calc.callBaseMethod(this, 'dispose');
    },

    _load: function() {
        this._update(null);
    },

    _update: function(e) {
        var manageServicesCost = this._get_managedServicesCost();
        var benefitsAndTaxes = this._get_benefitsAndTaxes();
        var totalAnnualCost = this._parseInt(this._parseInt(this._techSupportSalaryTextBox.value) + benefitsAndTaxes);
        var savingsAnnualCost = this._parseInt(totalAnnualCost - manageServicesCost);
        var costBreakdown =
            this._parseInt(this._get_costBreakdown(totalAnnualCost, 5, 8, this._parseInt(this._vacationDaysTextBox.value), this._parseInt(this._sickDaysTextBox.value)));
        var bokCostBreakdown =
            this._get_costBreakdown(manageServicesCost, 7, 24, 0, 0);
        var savingsCostBreakdown = costBreakdown - bokCostBreakdown;

        jQuery(this._managedServicesCostLabel).html(String.localeFormat("{0:c}", manageServicesCost));
        jQuery(this._benefitsAndTaxesLabel).html(String.localeFormat("{0:c}", benefitsAndTaxes));
        jQuery(this._annualCostLabel).html(String.localeFormat("{0:c}", totalAnnualCost));
        jQuery(this._bokAnnualCostLabel).html(String.localeFormat("{0:c}", manageServicesCost));
        jQuery(this._savingsAnnualCostLabel).html(String.localeFormat("{0:c}", savingsAnnualCost));
        jQuery(this._costBreakdownLabel).html(String.localeFormat("{0:c}", costBreakdown));
        jQuery(this._bokCostBreakdownLabel).html(String.localeFormat("{0:c}", bokCostBreakdown));
        jQuery(this._savingsCostBreakDownLabel).html(String.localeFormat("{0:c}", savingsCostBreakdown));
    },

    _get_managedServicesCost: function() {
        var numberOfWorkstations = this._parseInt(this._numberOfWorkstationsTextBox.value);
        var costOfWorkstations = numberOfWorkstations * this._workstationCost;

        var numberOfServers = this._parseInt(this._numberOfServersTextBox.value);
        var costOfServers = numberOfServers * this._serverCost;

        var numberOfNetworks = this._parseInt(this._numberOfNetworksTextBox.value);
        var costOfNetworks = numberOfNetworks * this._networkCost;

        var returnVal = (costOfWorkstations + costOfServers + costOfNetworks) * 12;

        if (isNaN(returnVal)) {
            return 0;
        } else {
            return returnVal;
        }
    },

    _get_benefitsAndTaxes: function() {
        var techSupportSalary = this._parseInt(this._techSupportSalaryTextBox.value);

        var returnVal = techSupportSalary * 0.3;

        if (isNaN(returnVal)) {
            return 0;
        } else {
            return returnVal;
        }
    },

    _get_costBreakdown: function(annualCost, daysWorked, hoursPerDay, vacationDays, sickDays) {
        return annualCost / ((52 * daysWorked * hoursPerDay) - (vacationDays * hoursPerDay) - (sickDays * hoursPerDay));
    },

    _parseInt: function(value) {
        var returnVal = parseInt(value);
        if (isNaN(returnVal)) {
            return 0;
        } else {
            return returnVal;
        }
    }
}

BOK.labor_cost_calc.createProperty("numberOfWorkstationsTextBox");
BOK.labor_cost_calc.createProperty("numberOfServersTextBox");
BOK.labor_cost_calc.createProperty("numberOfNetworksTextBox");
BOK.labor_cost_calc.createProperty("vacationDaysTextBox");
BOK.labor_cost_calc.createProperty("sickDaysTextBox");
BOK.labor_cost_calc.createProperty("techSupportSalaryTextBox");
BOK.labor_cost_calc.createProperty("managedServicesCostLabel");
BOK.labor_cost_calc.createProperty("benefitsAndTaxesLabel");
BOK.labor_cost_calc.createProperty("annualCostLabel");
BOK.labor_cost_calc.createProperty("bokAnnualCostLabel");
BOK.labor_cost_calc.createProperty("savingsAnnualCostLabel");
BOK.labor_cost_calc.createProperty("costBreakdownLabel");
BOK.labor_cost_calc.createProperty("bokCostBreakdownLabel");
BOK.labor_cost_calc.createProperty("savingsCostBreakDownLabel");

BOK.labor_cost_calc.registerClass('BOK.labor_cost_calc', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();