//mortcalc javascript
//to be used with iphone.js

function doAll(){
getPayTable('int1', 'total1', '1');
}

function selectAll(div) {
$(div).select();
}


function checkIsNumber(div){
        var input = $(div).value;
        if(input == null || input == ''){
	changeStatus("Please enter a valid number!", "error", '', "ic");
	clearAll();
        }else if(input != null && input.match(/^([0-9]+(\.[0-9]{0,2})?)$/)){
	changeStatus("Press Calculate", "msgonly", 'MC', "ic");
        clearAll();
        }else{
        $('icstatdiv').innerHTML = input + " Is an invalid entry!";
	changeStatus(input + " Is an invalid entry!", "error", '', "ic");
        clearAll();
        }
}


function clearAll() {
$('ipmt1').value = "0.00";
$('totint1').value = "0.00";
$('total1').value = "0.00";
$('cpmt1').value = "0.00";
}


function getPayTable(interest, divloc, divnum){
var pv = $('totamt').value;
var ti1 = $(interest).value * .01;
var years = $('nyears').value;
var ps = 12;

var interestpay = "ipmt" + divnum;
var totalint = "totint" + divnum;
var totalspd = "total" + divnum;
var caprepay = "cpmt" + divnum;


var url = "/cgi-bin/icalc.cgi";
var variables = "?pv=" + pv + "&ti1=" + ti1 + "&years=" + years + "&ps=" + ps;
        var myAjax = new Ajax.Request(url, {
        parameters: variables,
                onLoading: function(response){
		changeStatus('Calculating', 'loading', '', 'ic'); 
                },
                onSuccess: function(response){
                var json = response.responseText.evalJSON();
                var data = json.icalc;
                var cpmt = data[0].cpmt;
                var ipmt = data[0].ipmt;
                var tint = data[0].tint;
                var tspd = data[0].tspd;
                $(caprepay).value = cpmt;
                $(interestpay).value = ipmt;
                $(totalint).value = tint;
                $(totalspd).value = tspd;
		changeStatus('Calculations Complete', 'msgonly', 'MC', 'ic');
                }
        });
}


function showPaym(type, divnum){
var pv = $('totamt').value;
var ti1 = $('int' + divnum).value * .01;
var years = $('nyears').value;
var ps = 12;
var mult;
var finish;

if(type.value == "annual"){
mult = 12;
finish = years;
}else if(type.value == "monthly"){
mult = 1;
finish = years * ps;
}else{
$('showpaym').innerHTML = "";
return;
}

var url = "/cgi-bin/icalcall.cgi";
var variables = "?pv=" + pv + "&ti1=" + ti1 + "&years=" + years + "&ps=" + ps + "&mult=" + mult + "&fn=" + finish;
        var myAjax = new Ajax.Request(url, {
        parameters: variables,
                onLoading: function(response){
                $('statdiv').innerHTML = "Calculating...";
                },
                onSuccess: function(response){
                var results = response.responseText;
                $('showpaym').innerHTML = results;
                $('statdiv').innerHTML = "Calculations Complete";
                }
        });

}
                                                                       
