﻿function update_estimated_pmt_summary(ranges) {
    var ps_months=[24,36,48,60,72];
    var ps_apr=[5.5,6.0,6.5,7.0,7.5,8.0,8.5,9.0,9.5];
    if (typeof ranges != 'undefined') {
		ps_apr = ranges.split(',');
		for (var i = 0; i < ps_apr.length; i++) {
			ps_apr[i] = parseInt(ps_apr[i], 10);
		}
	}
	
    $j('#pc_payment').val('');
    
    var calc=new pmt_calc();
    if (isNaN(calc.principal) || isNaN(calc.down) || (calc.down>=calc.principal)) {
        $j('#pmtTable').html('');
        return;
    }    
    if (!isNaN(calc.payment) && calc.payment!=null && calc.valid)
        $j('#pc_payment').val(calc.payment);
        
    if (!isNaN(calc.rate) && calc.rate>0.0) {
		// Rebuild apr list centered around calc.rate
		ps_apr = [];
		if (calc.rate > 2.0) {
			for (var i=-4; i < 5; i++) {
				ps_apr.push((calc.rate+(i*0.5)).toFixed(1));
			}
		} else {
			// Calc.rate too low, use it as the lowest value
			for (var i=0; i < 9; i++) {
				ps_apr.push((calc.rate+(i*0.5)).toFixed(1));
			}
		}
    }
    if (!isNaN(calc.term) && calc.term>=24 && calc.term<=72) {
        for (var i=0; i<ps_months.length; i++){
            if (ps_months[i]==calc.term) break;
            if (ps_months[i]>calc.term) { ps_months.splice(i,0,calc.term); break; };
            if (i==(ps_months.length-1)) { ps_months[ps_months.length]=calc.term; break; };
        }
    }
    var ps_tbl=$j('<table class="box">');
    
    var ps_head=$j('<thead>');
    ps_tbl.append(ps_head);
    
    var ps_row=$j('<tr>');
    ps_head.append(ps_row);
    
    $j(ps_row).append($j('<td>').text('APR'));
    
    $j.each(ps_months,function() {
        $j(ps_row).append($j('<td>').text(this+' mo.'));
    });
    
    var ps_body=$j('<tbody>');
    ps_tbl.append(ps_body);
    
    var odd=true;
    $j.each(ps_apr,function() {
        ps_row=$j('<tr>').attr('class',odd?'odd':'even');
        ps_body.append(ps_row);
        var rate=this;
        //if (rate==calc.rate) $j(ps_row).attr('class','sel');
        $j(ps_row).append($j('<td>').html(rate+'%').attr('class',(rate==calc.rate)?'sel':''));
        $j.each(ps_months,function() {
            var term=this;
            var pmt=calc.get_payment(rate, term, calc.taxrate);
            $j(ps_row).append($j('<td>').html('$'+pmt).attr('class',(term==calc.term || rate==calc.rate)?'sel':''));
        });
        odd=(!odd);
    });
    
    $j('#pmtTable').html('').append(ps_tbl);    
}

function pmt_calc() {
	this.rate=$j('#pc_interest').val();	
	this.rate=Math.round(parseFloat(this.rate)*100)/100;	
	this.principal=$j('#pc_price').val();
	this.principal=Math.round(parseFloat(this.principal)*100)/100;
	this.down=$j('#pc_trade').val();
	this.down=Math.round(parseFloat(this.down)*100)/100;
	if (isNaN(this.down)) this.down=0;
	this.term=$j('#pc_months').val();
	this.term=parseInt(this.term);	
	if ($j("#pc_taxrate").length) {
		this.taxrate = $j("#pc_taxrate").val();
	} else {
		this.taxrate = 0;
	}
	this.payment=this.get_payment(this.rate, this.term, this.taxrate);
	this.valid=true;
	
	$j('#calcErrors').html('');
	if (isNaN(this.rate)) {$j('#calcErrors').html('Invalid Interest Rate');this.valid=false;}
	if (this.rate<=0.0) {$j('#calcErrors').html('Interest Rate must be greater than 0%');this.valid=false;}
	
	$j('#calcErrors').html('');
	if (isNaN(this.taxrate)) {$j('#calcErrors').html('Invalid Tax Rate');this.valid=false;}
	if (this.taxrate<0.0) {$j('#calcErrors').html('Tax Rate must be 0% or greater');this.valid=false;}
	
	$j('#calcErrors').html('');
	if (isNaN(this.principal)) {$j('#calcErrors').html('Invalid Vehicle Price');this.valid=false;}
	if (this.principal<1000.0) {$j('#calcErrors').html('Vehicle Price must be over $1000');this.valid=false;}
	
	$j('#calcErrors').html('');	
	if (this.down>=this.principal) {$j('#calcErrors').html('Down Payment or Trade must be less than Vehicle Price');this.valid=false;}
	
	$j('#calcErrors').html('');
	if (isNaN(this.term)) {$j('#calcErrors').html('Invalid Monthly Term');this.valid=false;}
	if (this.term<24 || this.term>72) {$j('#calcErrors').html('Monthly Term must be between 24 and 72');this.valid=false;}
}

pmt_calc.prototype.get_payment=function(rate, term, taxrate) {    
    var rate_to_use=rate; if (rate_to_use==null||isNaN(rate_to_use)) rate_to_use=this.rate;
	var term_to_use=term; if (term_to_use==null||isNaN(term_to_use)) term_to_use=this.term;	
	if (isNaN(rate_to_use)) return Number.NaN;
	if (isNaN(term_to_use)) return Number.NaN;
	if (isNaN(this.principal)) return Number.NaN;
	if (isNaN(this.down)) return Number.NaN;
	
	var principal=this.principal-this.down;	
	if (rate_to_use > 0){rate_to_use /= 100.0;}
	rate_to_use /= 12;
	
	var pretax = Math.floor((principal*rate_to_use)/(1-Math.pow((1+rate_to_use),(-1*term)))*100)/100;
	var taxes = 0;
	if (taxrate) {
		taxes = ((taxrate/100.0) * pretax);
	}
	
    return Math.round(pretax + taxes);
}
