function calculateRepayment(frm) {
	if (!checkNumber(frm.interest,'Please enter only digits')) return false;
	if (frm.price.value == 0 || frm.price.value.length == 0) {
		$.modaldialog.error('<p style="text-align: center;"><strong>Please enter a valid Purchase Price!</strong></p><p style="text-align: center; margin: 50px 0 0 0;">Autoclosing in 4 seconds.</p>', { title: 'Purchase Price', timeout: 4, width: 300, height: 150 });
		frm.price.focus(); 
	} else if (frm.interest.value == 0 || frm.interest.value.length == 0) {
		$.modaldialog.error('<p style="text-align: center;"><strong>Interest Rate can not be zero!</strong></p><p style="text-align: center; margin: 50px 0 0 0;">Autoclosing in 4 seconds.</p>', { title: 'Interest Rate', timeout: 4, width: 300, height: 150 });
		frm.interest.focus(); 
	} else if (frm.months.value == 0 || frm.months.value.length == 0) {
		$.modaldialog.error('<p style="text-align: center;"><strong>Please enter a repayment period!</strong></p><p style="text-align: center; margin: 50px 0 0 0;">Autoclosing in 4 seconds.</p>', { title: 'Number of Months', timeout: 4, width: 300, height: 150 });
		frm.months.focus(); 
	} else {
		calculatePayment(frm);
	}
	return false;
}

function checkNumber(input, msg) {
    msg = msg;
    var str = input.value;
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1);
        if ((ch < "0" || "9" < ch) && ch != '.') {
            $.modaldialog.error('<p style="text-align: center;"><strong>'+msg+'!</strong></p><p style="text-align: center; margin: 50px 0 0 0;">Autoclosing in 4 seconds.</p>', { title: 'Invalid Number', timeout: 4, width: 300, height: 150 });
            return false;
        }
    }
    input.value = str;
    return true;
}

function getValue(str) {
	str2 = "";
	for( i = 0; i < str.length; i++ ) {
		if( str.charAt(i) >= '0' && str.charAt(i) <= '9' ) str2 = str2 + str.charAt(i);
	}
	return str2;
}

function calculatePayment(frm) {
	princ = getValue(frm.price.value) - getValue(frm.deposit.value);
	intRate = ((frm.interest.value)/100)/ 12;
	months = frm.months.value
	frm.total.value = Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
	frm.ammount.value = princ;
}
