var iglass;
if (!iglass) iglass = new Object();

if (!iglass.pricingEstimator)
    iglass.pricingEstimator =
{
    elements:
    {
        divMetrics: undefined,
        divResults: undefined,
        indicator: undefined,
        lnkEstimatePrice: undefined,
        txtNetworkDevices: undefined,
        txtDataMetrics: undefined,
        lblNetworkDevices: undefined,
        lblDataMetrics: undefined,
        lblSetupFee: undefined,
        lblEstimatedInvestment: undefined,
        customQuote: undefined,
        generatedQuote: undefined,
        error: undefined,
        general: undefined
    },
    data:
    {
        metrics:
        {
            '0': { Pricing: 0, Metrics: 0, Fee: 0 },
            '5': { Pricing: 20, Metrics: 0, Fee: 0 },
            '10': { Pricing: 20, Metrics: 0, Fee: 0 },
            '15': { Pricing: 20, Metrics: 0, Fee: 0 },
            '20': { Pricing: 20, Metrics: 0, Fee: 0 },
            '25': { Pricing: 20, Metrics: 0, Fee: 0 },
            '50': { Pricing: 20, Metrics: 0, Fee: 0 },
            '100': { Pricing: 19, Metrics: 0, Fee: 0 },
            '200': { Pricing: 18, Metrics: 0, Fee: 0 },
            '300': { Pricing: 17, Metrics: 0, Fee: 0 },
            '400': { Pricing: 16, Metrics: 0, Fee: 0 },
            '500': { Pricing: 15.5, Metrics: 0, Fee: 0 },
            '600': { Pricing: 15, Metrics: 0, Fee: 0 },
            '700': { Pricing: 14.5, Metrics: 0, Fee: 0 },
            '800': { Pricing: 14, Metrics: 0, Fee: 0 },
            '900': { Pricing: 13.5, Metrics: 0, Fee: 0 },
            '1000': { Pricing: 13, Metrics: 0, Fee: 0 },
            '1500': { Pricing: 12.5, Metrics: 0, Fee: 0 },
            '2500': { Pricing: 12, Metrics: 0, Fee: 0 }
        }
    },
    init: function ()
    {
        var This = iglass.pricingEstimator;

        This.onLoad();
    },
    loadControls: function ()
    {
        var This = iglass.pricingEstimator;

        This.elements.divMetrics = jQuery("#PricingEstimator").find("#divMetrics")[0];
        This.elements.divResults = jQuery("#PricingEstimator").find("#divResults")[0];
        This.elements.indicator = jQuery("#PricingEstimator").find("#loading-indicator")[0];
        This.elements.lnkEstimatePrice = jQuery("#PricingEstimator").find("#lnkEstimatePrice")[0];
        This.elements.txtNetworkDevices = jQuery("#PricingEstimator").find("[name='txtNetworkDevices']")[0];
        This.elements.txtDataMetrics = jQuery("#PricingEstimator").find("[name='txtDataMetrics']")[0];
        This.elements.lblNetworkDevices = jQuery("#PricingEstimator").find("#lblNetworkDevices")[0];
        This.elements.lblDataMetrics = jQuery("#PricingEstimator").find("#lblDataMetrics")[0];
        This.elements.lblSetupFee = jQuery("#PricingEstimator").find("#lblSetupFee")[0];
        This.elements.lblEstimatedInvestment = jQuery("#PricingEstimator").find("#lblEstimatedInvestment")[0];
        This.elements.customQuote = jQuery("#PricingEstimator").find("#tbodyCustomQuote")[0];
        This.elements.generatedQuote = jQuery("#PricingEstimator").find("#tbodyGeneratedQuote")[0];
        This.elements.error = jQuery("#PricingEstimator").find("#tbodyError")[0];
        This.elements.general = jQuery("#PricingEstimator").find("#tbodyGeneral")[0];
    },
    onLoad: function ()
    {
        var This = iglass.pricingEstimator;

        This.loadControls();

        jQuery(This.elements.lnkEstimatePrice).click(This.lnkEstimatePrice_click);

        jQuery(This.elements.indicator).hide();
        jQuery(This.elements.divResults).hide();
        jQuery(This.elements.customQuote).hide();
        jQuery(This.elements.divResults).hide();
        jQuery(This.elements.generatedQuote).hide();
        jQuery(This.elements.error).hide();
        jQuery(This.elements.general).hide();
    },
    getDataKey: function (count)
    {
        var This = iglass.pricingEstimator;

        var m = This.getCeilingMultiplier(count);
        var v = Math.ceil(count / m) * m;

        var key = '0';

        if (v >= 5 && v < 10) { key = '5'; }
        else if (v >= 10 && v < 15) { key = '10'; }
        else if (v >= 15 && v < 20) { key = '15'; }
        else if (v >= 20 && v < 25) { key = '20'; }
        else if (v >= 25 && v < 50) { key = '25'; }
        else if (v >= 50 && v < 100) { key = '50'; }
        else if (v >= 100 && v < 200) { key = '100'; }
        else if (v >= 200 && v < 300) { key = '200'; }
        else if (v >= 300 && v < 400) { key = '300'; }
        else if (v >= 400 && v < 500) { key = '400'; }
        else if (v >= 500 && v < 600) { key = '500'; }
        else if (v >= 600 && v < 700) { key = '600'; }
        else if (v >= 700 && v < 800) { key = '700'; }
        else if (v >= 800 && v < 900) { key = '800'; }
        else if (v >= 900 && v < 1000) { key = '900'; }
        else if (v >= 1000 && v < 1500) { key = '1000'; }
        else if (v >= 1500 && v < 2500) { key = '1500'; }
        else if (v == 2500) { key = '2500'; }

        return key;
    },
    getCeilingMultiplier: function (count)
    {
        var This = iglass.pricingEstimator;

        return count < 100 ? 5 : (count < 251) ? 10 : 25;
    },
    calculateMonthlyInvestment: function (count, metrics)
    {
        var This = iglass.pricingEstimator;

        if (count > 2500)
        {
            return 'custom';
        }
        else
        {
            var c = This.getCeilingMultiplier(count);
            var d = Math.ceil(count / c) * c;
            var p = This.data.metrics[This.getDataKey(count)].Pricing;
            var m = metrics * d;
            var t = d * p + m;

            return (t < 1000) ? 1000 : t;
        }
    },
    formatCurrency: function (s)
    {
        s += '';
        x = s.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1))
        {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    },
    lnkEstimatePrice_click: function ()
    {
        var This = iglass.pricingEstimator;

        var count = jQuery(This.elements.txtNetworkDevices).val();
        var metrics = jQuery(This.elements.txtDataMetrics).val();

        jQuery(This.elements.divResults).show();

        if (!count.match(/^\d+$/) || !metrics.match(/^\d+$/))
        {   
            jQuery(This.elements.customQuote).hide();
            jQuery(This.elements.generatedQuote).hide();
            jQuery(This.elements.general).hide();
            jQuery(This.elements.error).show();

            return false;
        }

        var i = This.calculateMonthlyInvestment(count, metrics);
        var m = This.data.metrics[This.getDataKey(count)].Metrics;

        jQuery(This.elements.divMetrics).html(count < 0 ? "Included Data Metrics" : "Data Metrics per Network Device");
        jQuery(This.elements.lblNetworkDevices).text(count);
        jQuery(This.elements.lblDataMetrics).text(count < 0 ? m : metrics);
        jQuery(This.elements.general).show();

        if (i == 'custom')
        {
            jQuery(This.elements.customQuote).show();
            jQuery(This.elements.error).hide();
            jQuery(This.elements.generatedQuote).hide();
        }
        else
        {
            jQuery(This.elements.customQuote).hide();
            jQuery(This.elements.error).hide();
            jQuery(This.elements.generatedQuote).show();

            jQuery(This.elements.lblSetupFee).text((count < 0) ? '$250' : 'Included');
            jQuery(This.elements.lblEstimatedInvestment).text("$" + (This.formatCurrency(i.toFixed(0)) || '$0.00'));
        }

        jQuery(This.elements.txtDataMetrics).val("See Below");

        return false;
    }
}
