/**
 * Calculator
 *
 * @description incapsulates JS functionality of the calculator
 */
var Calculator = {
    init: function() {
        var i, iCnt = aLandscapeArea.length;
        var oSelect = $('#js_landscape');

        /**
         * Landscape Area
         */
        for (i = 0; i < iCnt; ++i) {
            var sLabel = '';
            var iValue = 0;

            if (aLandscapeArea[i] instanceof Array) {
                iValue = aLandscapeArea[i][0];
                sLabel = aLandscapeArea[i][1];
            } else {
                iValue = aLandscapeArea[i];
                sLabel = aLandscapeArea[i].toString().replace(/(\d+)(\d{3})/, '$1,$2')+' sq ft';
            }


            oSelect.append(
                '<option value='+iValue+'>'+
                   sLabel +
                '</option>'
            );
        }

        /**
         * Water Rate
         */
        iCnt = aWaterRate.length;
        oSelect = $('#js_rate');

        for (i = 0; i < iCnt; ++i) {
            oSelect.append(
                '<option value='+aWaterRate[i]+'>'+
                    '$'+aWaterRate[i]+
                '</option>'
            );
        }

        /**
         * Unit Measure
         */
        iCnt = aUnitMeasure.length;
        oSelect = $('#js_unit');

        for (i = 0; i < iCnt; ++i) {
            oSelect.append(
                '<option value='+aUnitMeasure[i]+'>'+
                    aUnitMeasure[i]+
                '</option>'
            );
        }

        /**
         * 100% Eto (in inches)
         */
        iCnt = aEto.length;
        oSelect = $('#js_county');

        for (i = 0; i < iCnt; ++i) {
            oSelect.append(
                '<option value='+aEto[i][0]+'>'+
                    aEto[i][1]+
                '</option>'
            );
        }

        $('.js_calculate').click(function(){
            if ($('#js_params:visible').length) {
                var aValues = {};
                var aErrors = [];

                $('#js_params select').each(function(){
                    var sName = $(this).attr('name');
                    aValues[sName] = $(this).val();

                    if (!aValues[sName]) {
                        var sError = 'Please select ';

                        switch (sName) {
                        case 'landscape':
                            sError += 'Landscape Area';
                            break;

                        case 'rate':
                            sError += 'Water Rate';
                            break;

                        case 'unit':
                            sError += 'Unit Measure';
                            break;

                        case 'county':
                            sError += 'County';
                            break;
                        }

                        aErrors.push(sError);
                    }
                });

                var i, iCnt = aErrors.length;
                if (iCnt) {
                    var sError = aErrors[0];
                    
                    for (i = 1; i < iCnt; ++i) {
                        sError += '\n' + aErrors[i];
                    }

                    alert(sError);

                    return false
                } else {
                    var i, iCnt = aPeriods.length;
                    var fSavingsWaterHcf;
                    
                    for (i = 0; i < iCnt; ++i) {
                        fSavingsWater = aValues.landscape * aPeriods[i][0] *
                            (aValues.county / 12) * (aPeriods[i][1] / 100);

                        if (aValues.unit == 'KGAL') {
                            fSavingsWater *= 0.748;
                        }

                        $('#js_save' + aPeriods[i][0]).val(
                            '$'+(Math.round(fSavingsWater * aValues.rate *
                                (1 + (fAnnualRateOfInc * (aPeriods[i][0] - aPeriods[i][2])))
                            ) / 100)
                                .toString()
                                .replace(/(\d+)(\d{3})/, '$1,$2')
                        );
                    }
                }
            }

            $('#js_results, #js_params').toggle();
            return false;
        });
    }
};

$(document).ready(function(){
    Calculator.init();
});
