jQuery(function() {
    jQuery('#roiResults').hide();
    
    jQuery('#roiCalcSubmit').click(function() {
        var roiProcedure = jQuery("#roiProcedure :selected").text();
        var roiProcsPerMonth = jQuery("#roiProcsPerMonth").val();
        var roiPricePerProc = jQuery("#roiPricePerProc").val();
        roiPricePerProc = roiPricePerProc.replace(/,/g,'');
        roiPricePerProc = roiPricePerProc.replace(/\$/g,'');
        //roiPricePerProc = roiPricePerProc.replace(/\./g,'');
        roiPricePerProc = parseInt(roiPricePerProc);
        var roiGrossPerProc = jQuery("#roiGrossPerProc").val();
        roiGrossPerProc = roiGrossPerProc.replace(/,/g,'');
        roiGrossPerProc = roiGrossPerProc.replace(/\$/g,'');
        //roiGrossPerProc = roiGrossPerProc.replace(/\./g,'');
        roiGrossPerProc = parseInt(roiGrossPerProc);
        
        var hasError = false;
        if (roiProcsPerMonth == '' || isNaN(roiProcsPerMonth)) {
            hasError = true;
            jQuery("#roiProcsPerMonth").addClass('error');
        } else {
            jQuery("#roiProcsPerMonth").removeClass('error');
        }
        if (roiPricePerProc == '' || isNaN(roiPricePerProc)) {
            hasError = true;
            jQuery("#roiPricePerProc").addClass('error');
        } else {
            jQuery("#roiPricePerProc").removeClass('error');
        }
        if (roiGrossPerProc == '' || isNaN(roiGrossPerProc)) {
            hasError = true;
            jQuery("#roiGrossPerProc").addClass('error');
        } else {
            jQuery("#roiGrossPerProc").removeClass('error');
        }
        
        if (!hasError) {
            runROICalc();
        } else {
            jQuery('#roiResults').hide();
        }
        
    });
    
    function runROICalc() {
        jQuery('#roiResults').show();
        var roiProcedure = jQuery("#roiProcedure :selected").text();
        var roiProcsPerMonth = jQuery("#roiProcsPerMonth").val();
        var roiPricePerProc = jQuery("#roiPricePerProc").val();
        roiPricePerProc = roiPricePerProc.replace(/,/g,'');
        roiPricePerProc = roiPricePerProc.replace(/\$/g,'');
        //roiPricePerProc = roiPricePerProc.replace(/\./g,'');
        roiPricePerProc = parseInt(roiPricePerProc);
        var roiGrossPerProc = jQuery("#roiGrossPerProc").val();
        roiGrossPerProc = roiGrossPerProc.replace(/,/g,'');
        roiGrossPerProc = roiGrossPerProc.replace(/\$/g,'');
        //roiGrossPerProc = roiGrossPerProc.replace(/\./g,'');
        roiGrossPerProc = parseInt(roiGrossPerProc);
        
        // monthly inc 
        var mi_1 = roiProcsPerMonth * .1;
        jQuery("#mi-1").html(formatNumber(mi_1));
        var mi_2 = roiProcsPerMonth * .2;
        jQuery("#mi-2").html(formatNumber(mi_2));
        var mi_3 = roiProcsPerMonth * .3;
        jQuery("#mi-3").html(formatNumber(mi_3));
        var mi_4 = roiProcsPerMonth * .4;
        jQuery("#mi-4").html(formatNumber(mi_4));
        var mi_5 = roiProcsPerMonth * .5;
        jQuery("#mi-5").html(formatNumber(mi_5));
        
        // monthly inc gross
        var mig_1 = mi_1 * roiPricePerProc;
        jQuery("#mig-1").html("$"+formatNumber(mig_1));
        var mig_2 = mi_2 * roiPricePerProc;
        jQuery("#mig-2").html("$"+formatNumber(mig_2));
        var mig_3 = mi_3 * roiPricePerProc;
        jQuery("#mig-3").html("$"+formatNumber(mig_3));
        var mig_4 = mi_4 * roiPricePerProc;
        jQuery("#mig-4").html("$"+formatNumber(mig_4));
        var mig_5 = mi_5 * roiPricePerProc;
        jQuery("#mig-5").html("$"+formatNumber(mig_5));
        
        // monthly inc net
        var min_1 = mi_1 * roiGrossPerProc;
        jQuery("#min-1").html("$"+formatNumber(min_1));
        var min_2 = mi_2 * roiGrossPerProc;
        jQuery("#min-2").html("$"+formatNumber(min_2));
        var min_3 = mi_3 * roiGrossPerProc;
        jQuery("#min-3").html("$"+formatNumber(min_3));
        var min_4 = mi_4 * roiGrossPerProc;
        jQuery("#min-4").html("$"+formatNumber(min_4));
        var min_5 = mi_5 * roiGrossPerProc;
        jQuery("#min-5").html("$"+formatNumber(min_5));
        
        // yearly inc
        var yi_1 = mi_1 * 12;
        jQuery("#yi-1").html(formatNumber(yi_1));
        var yi_2 = mi_2 * 12;
        jQuery("#yi-2").html(formatNumber(yi_2));
        var yi_3 = mi_3 * 12;
        jQuery("#yi-3").html(formatNumber(yi_3));
        var yi_4 = mi_4 * 12;
        jQuery("#yi-4").html(formatNumber(yi_4));
        var yi_5 = mi_5 * 12;
        jQuery("#yi-5").html(formatNumber(yi_5));
        
        // yearly inc gross
        var yig_1 = mig_1 * 12;
        jQuery("#yig-1").html("$"+formatNumber(yig_1));
        var yig_2 = mig_2 * 12;
        jQuery("#yig-2").html("$"+formatNumber(yig_2));
        var yig_3 = mig_3 * 12;
        jQuery("#yig-3").html("$"+formatNumber(yig_3));
        var yig_4 = mig_4 * 12;
        jQuery("#yig-4").html("$"+formatNumber(yig_4));
        var yig_5 = mig_5 * 12;
        jQuery("#yig-5").html("$"+formatNumber(yig_5));
        
        // yearly inc net
        var yin_1 = min_1 * 12;
        jQuery("#yin-1").html("$"+formatNumber(yin_1));
        var yin_2 = min_2 * 12;
        jQuery("#yin-2").html("$"+formatNumber(yin_2));
        var yin_3 = min_3 * 12;
        jQuery("#yin-3").html("$"+formatNumber(yin_3));
        var yin_4 = min_4 * 12;
        jQuery("#yin-4").html("$"+formatNumber(yin_4));
        var yin_5 = min_5 * 12;
        jQuery("#yin-5").html("$"+formatNumber(yin_5));
    }
    
    function formatNumber(nStr) {
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        //x2 = x.length > 1 ? '.' + x[1] : '';
        x2 = '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }
});
