
///BASIC MORTGAGE CALCULATOR BASLIYOR ///
///BASIC MORTGAGE CALCULATOR BASLIYOR ///
///BASIC MORTGAGE CALCULATOR BASLIYOR ///

function checkNumber(input, min, max, msg)
{
        msg = msg + " field has invalid data: " + input.value;
        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 != '.')
	        {
                alert(msg);
                return false;
            }
        }

        var num = 0 + str

        if (num < min || max < num)
	    {
            alert(msg + " not in range [" + min + ".." + max + "]");
            return false;
        }
        input.value = str;
        return true;
}

function computeField(input)
{
        if (input.value != null && input.value.length != 0)
        input.value = "" + eval(input.value);
        computeForm(input.form);
}



function computeForm(form)
{
        if ((form.years.value == null || form.years.value.length == 0) || (form.interest.value == null || form.interest.value.length == 0) || (form.principal.value == null || form.principal.value.length == 0))
	    {
            return;
        }

       var payments = form.years.value;
       payments  = payments * 12;

        if (!checkNumber(form.years, 1, 25, "# of Years"))
	    {
            window.document.getElementById("invalid_year").style.visibility= "visible";
            return;
        }
        else{window.document.getElementById("invalid_year").style.visibility= "hidden";}
        
        if (!checkNumber(form.interest, .001, 99, "Interest"))
	    {
            window.document.getElementById("invalid_rate").style.visibility= "visible";
            return;
        }
        else{window.document.getElementById("invalid_rate").style.visibility= "hidden";}
        
        if (!checkNumber(form.principal, 100, 10000000, "Principal"))
	    {
            window.document.getElementById("invalid_amount").style.visibility= "visible";
            return;
        }
        else{window.document.getElementById("invalid_amount").style.visibility= "hidden";}



        var i = form.interest.value;
        
        if (i > 1.0)
	    {
            i = i / 100.0;
            form.interest.value = i * 100;
        }

        i /= 12;

        var pow = 1;
        for (var j = 0; j < payments; j++)

        pow = pow * (1 + i);

        form.payment.value = (form.principal.value * pow * i) / (pow - 1)
        form.payment.value = Math.round(form.payment.value * 100) / 100;
        form.total.value = Math.round(form.payment.value * 12 * form.years.value * 100) / 100;
        form.interestonly.value = Math.round(form.principal.value * i * 100) /100;
        form.total_interest.value = Math.round(form.payment.value * 12 * form.years.value) - form.principal.value;
}


function clearForm(form)
{
        form.years.value = "";
        form.interest.value = "";
        form.principal.value = "";
}


///BASIC MORTGAGE CALCULATOR BITIYOR ///
///BASIC MORTGAGE CALCULATOR BITIYOR ///
///BASIC MORTGAGE CALCULATOR BITIYOR ///