// JavaScript Document
function calculate() {
    // Get the user's input from the form. Assume it is all valid.
    // Convert interest from a percentage to a decimal, and convert from
    // an annual rate to a monthly rate. Convert payment period in years
    // to the number of monthly payments.
	var c1 = document.loandata.c1.value;
	var c2 = document.loandata.c2.value;
	var c3 = document.loandata.c3.value;
	var auto = document.loandata.auto.value;
	var stu = document.loandata.stu.value;
	var other = document.loandata.other.value;
	var other2 = document.loandata.other2.value;
	var mort = document.loandata.mort.value;
	
	var c1i = document.loandata.c1i.value / 100 / 12;
	var c2i = document.loandata.c2i.value / 100 / 12;
	var c3i = document.loandata.c3i.value / 100 / 12;
	
	
	var autop = document.loandata.autop.value;
	var stup = document.loandata.stup.value;
	var otherp = document.loandata.otherp.value;
	var other2p = document.loandata.other2p.value;
	var mortp = document.loandata.mortp.value;
	var autol = document.loandata.autol.value;
	var stul = document.loandata.stul.value;
	var otherl = document.loandata.otherl.value;
	var other2l = document.loandata.other2l.value;
	var mortl = document.loandata.mortl.value;
	
	var interest = document.loandata.interest.value / 100 / 12;
	var interest2 = document.loandata.interest.value;
	var term = document.loandata.term.value;
	
	var x = Math.pow(1 + c1i, term);
    var c1p = (c1*x*c1i)/(x-1);
	
	var x = Math.pow(1 + c2i, term);
    var c2p = (c2*x*c2i)/(x-1);
	
	var x3 = Math.pow(1 + c3i, term);
    var c3p = (c3*x3*c3i)/(x3-1);
	
	var c1interest = ((c1p * term) - c1);
	var c2interest = (c2p * term) - (c2);
	var c3interest = (c3p * term) - (c3);
	
	var totald = (c1*1 + c2*1 + c3*1+auto*1+mort*1+stu*1+other*1+other2*1);
	var totalm = (c1p*1+c2p*1+c3p*1+autop*1+mortp*1+stup*1+otherp*1+other2p*1);
	
	var x4 = Math.pow(1 + interest, term);
    var proposed = (totald * x4 * interest)/(x4-1);
	
	var autoi = (autop * autol) - (auto);
	var morti = (mortp * mortl) - (mort);
	var stui = (stup * stul) - (stu);
	var otheri = (otherp * otherl) - (other);
	var other2i = (other2p * other2l) - (other2);
	
	var totalint = (c1interest*1 + c2interest*1 + c3interest*1 + autoi*1 + morti*1 + stui*1 + otheri*1 + other2i*1);
	
		var proposedi = (proposed * term) - (totald);
		
		var reqauto = (auto / autol);
		var validat = (auto * autol);
if(validat > 0 && autop < reqauto){
alert('The monthly payment for auto loan should be atleast ' + round(reqauto));
document.loandata.autop.value = round(reqauto);
}
else{
}
		var reqmort = (mort / mortl);
		var validat = (mort * mortl);
if(validat > 0 && mortp < reqmort){
alert('The monthly payment for mortgage should be atleast ' + round(reqmort));
document.loandata.mortp.value = round(reqmort);
}
else{
}
		var reqstu = (stu / stul);
		var validat = (stu * stul);
if(validat > 0 && stup < reqstu){
alert('The monthly payment for student loan should be atleast ' + round(reqstu));
document.loandata.stup.value = round(reqstu);
}
else{
}
		var reqother = (other / otherl);
		var validat = (other * otherl);
if(validat > 0 && otherp < reqother){
alert('The monthly payment for other debts should be atleast ' + round(reqother));
document.loandata.otherp.value = round(reqother);
}
else{
}
		var reqother2 = (other2 / other2l);
		var validat = (other2 * other2l);
if(validat > 0 && other2p < reqother2){
alert('The monthly payment for other debts 2 should be atleast ' + round(reqother2));
document.loandata.other2p.value = round(reqother2);
}
else{
}
	

    // Check that the result is a finite number. If so, display the results
    if (!isNaN(c1) && 
        (c1 != Number.POSITIVE_INFINITY) &&
        (c1 != Number.NEGATIVE_INFINITY)) {

        document.loandata.c1p.value = round(c1p);
		document.loandata.c2p.value = round(c2p);
		document.loandata.c3p.value = round(c3p);
		document.loandata.totald.value = round(totald);
		document.loandata.totald2.value = round(totald);
		document.loandata.totalm.value = round(totalm);
		document.loandata.time.value =(term);
		document.loandata.totalint.value = round(totalint);
		document.loandata.rate.value =(interest2);
		document.loandata.int.value =round(proposedi);
		document.loandata.permon.value = round(proposed);
        document.loandata.alltotal.value = round(totald*1 + totalint*1);
		document.loandata.alltotal2.value = round(totald*1 + proposedi*1);
    }
    // Otherwise, the user's input was probably invalid, so don't
    // display anything.
    else {
        document.loandata.clp.value = "error";
        document.loandata.total.value = "";
        document.loandata.totalinterest.value = "";
    }
}

// This simple method rounds a number to two decimal places.
function round(x) {
  return Math.round(x*100)/100;
}