/***
calculates montly rates for different credits
@author: harald friessnegger <harald at webmeisterei (dot) com>

***/


$(document).ready(function() {
	$('#calculate').click(calculate);
	$('#calculateform').keypress(registerSubmit);

	calculate();
});

function registerSubmit(e) {
    /*when enter is pressed, calculate, instead of submitting the form
    */

    if (e.keyCode == 13) {
        calculate();
        return false;
    }

}

function cleanup() {
    /*
    clear all fields that will contain data
    */

    $('#rateCHF').html('');
    $('#rateEUR').html('');
    $('#rateStepCredit').html('');
    $('#rateHPS').html('');
}


function parseInput() {
    /*
    */

    var capital = $('#capital').val();

    // get rid of thousands separators
    capital = capital.replace('.', '');

    //replace german decimal sep with englisch one
    capital = capital.replace(',','.');

    capital = parseFloat(capital)
    if (isNaN(capital)) {
       return 0.0
    }
    return capital
}

function calculate() {
	/*update monthly rates*/

	cleanup();

    var total = Number($('#total').val());
    var capital = parseInput();


	//format the capital field
	$('#capital').val(fmtMoney(capital));


    if (isNaN(parseFloat(capital))) {
        // if bad input is provided, simply do nothing
        return;
    }

    var need = total - capital;
    if (need<0) {
        need = 0.0;
    }

    $('#need').html(fmtMoney(String(need)));

    euro = euroRate(need);
    chfRate(need);
    stepRate(need);
    hpsRate(need);
	totalRate(euro);

}

function monthlyRate(need, df) {
    var duration = Number($('#duration').val());
    var result = need * df * (df-1)/(Math.pow(df,(12*duration))-1);
    //round the result to two decimals:
    return Math.round(result * 100)/100;
}

function chfRate(need) {
    var chfDF = Number($('#chfDF').val());
    var rate = monthlyRate(need, chfDF);
    $('#rateCHF').html(fmtMoney(rate));
}

function euroRate(need) {
    var euroDF = Number($('#euroDF').val());
    var rate = monthlyRate(need, euroDF);
    $('#rateEUR').html(fmtMoney(rate));
	return rate;
}

function stepRate(need) {
    var firstStep = Number($('#firstStep').val());
    var result = firstStep / 100000 * need;
    var rounded = Math.round(result * 100) / 100;
    $('#rateStepCredit').html(fmtMoney(rounded));
}

function hpsRate(need) {
	var hpsDF = Number($('#hpsDF').val());
	var rate = monthlyRate(need, hpsDF);
    $('#rateHPS').html(fmtMoney(rate));

}

function totalRate(euroRate) {
	/* simply sum up euroRate and sponsorshipRate */
	var sponsorshipRate = Number($('#sponsorshipRate').val());
	$('#rateTotal').html(fmtMoney(euroRate + sponsorshipRate))
}

function fmtMoney(num, d, t) {
    /*
    http://javascript.internet.com/forms/currency-format.html
    */

    var d = d || ',';
    var t = t || '.';

    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();

	if (cents < 10) {
        cents = "0" + cents;
    }
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3)) + t + num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') +  num + d + cents);
}


