

function checkno(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 = parseFloat(str)
    if (num < min || num > max) 
	{
        alert(msg + "\n must be between " + min + " and " + max);
        return false;
    }
    input.value = str;
    return true;
}

function addfld(input, buttonFlag)
{
    if (input.value != null && input.value.length != 0)
        input.value = "" + eval(input.value);
    addit(input.form, buttonFlag);
}

function addit(form, buttonFlag)
{
    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) &&
	   (form.payment.value == null || form.payment.value.length == 0))) 
	{
	    if(buttonFlag == true)
          alert("The calculator requires further information to calulate your enquiry.") 
		return;
    }
    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 &&
	   form.payment.value != null && form.payment.value.length != 0) 
		form.payment.value = "";	
	var i = form.interest.value / 1200.0;
	var soup = 1;
	for (var j = 0; j < form.years.value*12; j++)
		soup = soup * (1 + i);
	if(form.principal.value == null || form.principal.value.length == 0)
	{
		if (!checkno(form.years, 1, 30, "Number of years") ||
			!checkno(form.interest, .001, 99, "Interest") ||
			!checkno(form.payment, 1, 100000, "Payment")) 
		{
			form.principal.value = "Invalid";
			return;
		}
	    form.principal.value = Math.round((form.payment.value * (soup - 1) ) / (soup * i))
	}
	else if(form.payment.value == null || form.payment.value.length == 0)
	{
		if (!checkno(form.years, 1, 30, "Number of years") ||
			!checkno(form.interest, .001, 99, "Interest") ||
			!checkno(form.principal, 100, 10000000, "Principal")) 
		{
			form.payment.value = "Invalid";
			return;
		}
		form.payment.value = Math.round(100.0*(form.principal.value * soup * i) / (soup - 1)) / 100.0
	}
}

function clearit(form)
{
    form.years.value = "";
    form.interest.value = "";
    form.principal.value = "";
	form.payment.value = "";
}

function addfldsd(input, buttonFlag){

    if (input.value != null && input.value.length != 0)
        input.value = "" + eval(input.value);
    workit(input.form, buttonFlag);
}

function workit(form, buttonFlag) {

    if((form.pvalue.value == null || form.pvalue.value.length == 0) &&
	   (form.ptsd.value == null || form.ptsd.value.length == 0)) {
	   	   
	    	 if(buttonFlag == true)
          		alert("The calculator requires further information to calulate your enquiry.") 
			return;
    	}
    if(form.pvalue.value != null && form.pvalue.value.length != 0 &&
	   form.ptsd.value != null && form.ptsd.value.length != 0) 
		form.ptsd.value = "";	
	
	var i = form.pvalue.value;
	
	if (i <= 14000){
		form.ptsd.value=1.25 * Math.round(i/100.0);
	}
	else if (i >= 14001 && i <= 30000){
		form.ptsd.value=175 + (1.5 * Math.round((i-14000)/100.0));
	}
	else if (i >= 30001 && i <= 80000){
		form.ptsd.value=415 + (1.75 * Math.round((i-30000)/100.0));
	}
	else if (i >= 80001 && i <= 300000){
		form.ptsd.value=1290 + (3.5 * Math.round((i-80000)/100.0));
	}
	else if (i >= 300001 && i <= 1000000){
		form.ptsd.value=8990 + (4.5 * Math.round((i-300000)/100.0));
	}
	else if (i > 1000000){
		form.ptsd.value=40490 + (5.5 * Math.round((i-1000000)/100.0));
	}	
}

function clearitsd(form)
{
    form.pvalue.value = "";
	form.ptsd.value = "";
}

