var validBalloon="balloon2";

// This validation  below does not to be there but is referred to later in script

/*function IsNaN(sText) {

	var ValidChars = "0123456789.";

	var char;

	var decimals = 0;



	for (i = 0, l=sText.length; i < l; i++) {

		char = sText.charAt(i);

		if (ValidChars.indexOf(char) == -1) {

			return false;

		}

		if ( char == '.' ) {

			decimals++;

		}

	}

	if ( decimals > 1 ) {

		return false;

	}

	return true;

}*/

// Function to calculate Periodic Payments.

function pmt() {

            // balloon value

            var fv = document.getElementById("conmsg_balloon1").value;

            // payment term

            var nper = document.getElementById("conmsg_term").value;

            // interest rate

            var rate = document.getElementById("conmsg_rate").value;

            // amount to be financed

            var pv = document.getElementById("conmsg_amount").value;

            // baloon percent

            var balper = document.getElementById("conmsg_balloon2").value;

            // baloon $

            var balpay = document.getElementById("conmsg_balloon1").value;



          document.getElementById("conmsg_amount").value = pv;

            //used by program

            pv = -pv;


            document.getElementById("conmsg_balloon1").value = (((-(pv*(balper/100))*100))/100).toFixed(2);
            


            if ((fv == 0) || (fv == ""))  {

                        fv = -(pv*balper/100);

            }

            fv = parseFloat(fv);

            nper = parseFloat(nper);

            pv = parseFloat(pv);

            rate = parseFloat(rate);



            rate = eval((rate)/(12 * 100));

            if ( rate == 0 ) {

                        // interest rate is 0

                        pmt_value = - (fv + pv)/nper;

            } else {

                        x = Math.pow(1 + rate,nper);

                        pmt_value = -((rate * (fv + x * pv))/(-1 + x));

            }

            pmt_value = conv_number(pmt_value,2);              

            document.getElementById("conmsg_repayment").value = pmt_value;

}

function conv_number(expr, decplaces) {

            // This function is from David Goodman's Javascript Bible.  

            var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));

            while (str.length <= decplaces) {

                        str = "0" + str;

            }

            var decpoint = str.length - decplaces;

            return (str.substring(0,decpoint) + "." + str.substring(decpoint,str.length));

}

function reset() {

            document.getElementById("conmsg_amount").value = "";

            document.getElementById("conmsg_rate").selectedIndex = 0;

            document.getElementById("conmsg_term").selectedIndex = 4;

            document.getElementById("conmsg_balloon1").value = "0.00";

            document.getElementById("conmsg_balloon2").value = "0.00";

            document.getElementById("conmsg_repayment").value = "";

}



function update_balloon_type(elem) {

            update_balloon_values();

            validBalloon = elem.id;

}

function update_balloon_values() {

            // we update the opposite balloon figure from the 'valid' one

            /*if ( ! IsNaN ( document.getElementById('conmsg_amount') ) ) {

                        return;

            }*/

            if ( validBalloon == 'conmsg_balloon1' ) {

                        /*if ( ! IsNaN(document.getElementById("conmsg_balloon1").value) ) {

                                    return;

                        }*/

                        value = (document.getElementById("conmsg_balloon1").value * 100 / document.getElementById('conmsg_amount').value).toFixed(2);

                        /*if ( value > 100 ) {

                                    alert("Balloon payment larger than total amount to finance, please lower the balloon amount");

                                    return;

                        }*/

                        document.getElementById('conmsg_balloon2').value = value;

            } 

                        

                        value = (document.getElementById("conmsg_balloon2").value * document.getElementById('conmsg_amount').value / 100).toFixed(2);

                        document.getElementById('conmsg_balloon1').value = value;

            }


function update_values() {

            update_balloon_values();

            if ( document.getElementById('conmsg_amount').value != "" ) {

                        pmt();

            }

}

/*function submit_calculator() {

update_values();

document.forms[0].submit();

}*/
