/* Javscript Document  */

$(document).ready(function(){

	$('.active').addClass("over");
	$("#leftmenu li ul").css("display", "none");
	$('.over ul').slideDown(500);
	$("#leftmenu").children('li').children('a').mouseover(function(){
		if(!$(this).parent().hasClass("over")){
			$(this).parent().siblings('li').removeClass("over");
			$(this).parent().addClass("over");
			$("#leftmenu li ul").css("display", "none");
			$('.over ul').slideDown(500);
		}
	});

	
	
	$('#slide1').fadeIn(100);
	$('#pag1').addClass("slideOn");
	numpag = $('.pag').length;
	slideshow();
	
	$('.pag').click(function(){
		id = $(this).attr('id');
		id = id.substring(3);
		slide = $('#slide'+id);
		$('.slide').css("display","none");
		$('.pag').removeClass("slideOn");
		slide.fadeIn(500);
		$(this).addClass("slideOn");
	});
	
	function slideshow(){
		t=setTimeout(function(){
			id = $('.pag.slideOn').attr('id');
			id = parseInt(id.substring(3)) +1;
			if(id > numpag) id = 1;
			slide = $('#slide'+id);
			$('.slide').css("display","none");
			$('.pag').removeClass("slideOn");
			slide.fadeIn(500);
			$('#pag'+id).addClass("slideOn");
			slideshow();
		},8000) 
	}
		
});

function floor(number){
return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function dosum(){
	var mi = document.temps.IR.value / 1200;
	var base = 1;
	var mbase = 1 + mi;
	for (i=0; i<document.temps.YR.value * 12; i++){
		base = base * mbase
	}
	document.temps.PI.value = floor(document.temps.LA.value * mi / ( 1 - (1/base)))
	document.temps.MT.value = floor(document.temps.AT.value / 12)
	document.temps.MI.value = floor(document.temps.AI.value / 12)
	var dasum = document.temps.LA.value * mi / ( 1 - (1/base)) + 
	document.temps.AT.value / 12 + 
	document.temps.AI.value / 12;
	document.temps.MP.value = floor(dasum);
}


function validateForm(){
// This function checks for empty required fields
// With Netscape focus is placed on empty fields
// Inputs are hard coded, nothing is passed to it
// It returns a true or false depending on validity
var amount = document.forms['cacl'].amount.value
var payment = document.forms['cacl'].payment.value
var rate = document.forms['cacl'].rate.value
var months = document.forms['cacl'].months.value
var comma = ","
var temparry = new Array(10)
temparray = amount.split(comma)
amount = temparray.join("")
temparray = payment.split(comma)
payment = temparray.join("")
temparray = rate.split(comma)
rate = temparray.join("")
temparray = months.split(comma)
months = temparray.join("")
if (isNaN(amount)) {
alert("Amount must be a number!");
document.forms['cacl'].amount.focus();
return false;
}
if (isNaN(payment)) {
alert("Payment must be a number!");
document.forms['cacl'].payment.focus();
return false;
}
if (isNaN(rate)) {
alert("Rate must be a number!");
document.forms['cacl'].rate.focus();
return false;
}
if (isNaN(months)) {
alert("Length of Loan must be a number!");
document.forms['cacl'].months.focus();
return false;
}
if(((amount != 0) && (amount != "")) &&
((payment != 0) && (payment != ""))){
alert("Please clear either the loan payment or the loan amount to continue.");
document.forms['cacl'].amount.focus();
return false;
}
if(((amount == 0) || (amount == "")) &&
((payment == 0) || (payment == ""))){
alert("You must select either the loan payment or the loan amount!");
document.forms['cacl'].amount.focus();
return false;
}
if((rate == 0) || (rate == "")){
alert("You must select a loan rate!");
document.forms['cacl'].rate.focus();
return false;
}
else {
rate = rate/1200;
}
if((months == 0) || (months == "")){
alert("You must provide the term of the loan!");
document.forms['cacl'].months.focus();
return false;
}
else {
if(document.forms['cacl'].frequency.options[document.forms['cacl'].frequency.selectedIndex].value == '1'){
months = months * 12;
}
}
if(payment == 0 || payment == ""){
document.forms['cacl'].payment.value = parseInt(100 * ((amount * ( rate / (1 - (Math.pow(1 + rate, -months))))) + .005)) / 100;
}
else {
document.forms['cacl'].amount.value = parseInt(100 * ((((Math.pow(1 + rate, -months)*(-payment + (Math.pow(1 + rate, months)* payment))))/rate) + .005)) / 100;
}
return false;
} 

function formatNum(Vnum) {
if(Vnum > 99000000) {
alert("Sorry, this will not generate numbers larger that 99 million.");
focus();
} else {
var V10million = parseInt(Vnum / 10000000);
var V1million = (Vnum % 10000000) / 1000000;
if(V1million / 1000000 == 1) {
V1million = 1;
} else
if(V1million < 1) {
V1million = "0";
} else {
V1million = parseInt(V1million,10);
}
var V100thousand = (Vnum % 1000000) / 100000;
if(V100thousand / 100000 == 1) {
V100thousand = 1;
} else
if(V100thousand < 1) {
V100thousand = "0";
} else {
V100thousand = parseInt(V100thousand,10);
}
var V10thousand = (Vnum % 100000) / 10000;
if(V10thousand / 10000 == 1) {
V10thousand = 1;
} else
if(V10thousand < 1) {
V10thousand = "0";
} else {
V10thousand = parseInt(V10thousand,10);
}
var V1thousand = (Vnum % 10000) / 1000;
if(V1thousand / 1000 == 1) {
V1thousand = 1;
} else
if(V1thousand < 1) {
V1thousand = "0";
} else {
V1thousand = parseInt(V1thousand,10);
}
var Vhundreds = (Vnum % 1000) / 100;
if(Vhundreds / 100 == 1) {
Vhundreds = 1;
} else
if(Vhundreds < 1) {
Vhundreds = "0";
} else {
Vhundreds = parseInt(Vhundreds,10);
}
var Vtens = (Vnum % 100) / 10;
if(Vtens / 10 == 1) {
Vtens = 1;
} else
if(Vtens < 1) {
Vtens = "0";
} else {
Vtens = parseInt(Vtens,10);
}
var Vones = (Vnum % 10) / 1;
if(Vones / 1 == 1) {
Vones = 1;
} else
if(Vones < 1) {
Vones = "0";
} else {
Vones = parseInt(Vones,10);
}
var Vcents = parseInt(((Vnum % 1) * 100),10);
if(Vcents < 1) {
Vcents = "00";
}
else
if(Vcents % 10 == 0) {
Vcents = Vcents + "0";
}
else
if(Vcents % 10 == Vcents) {
Vcents = "0" + Vcents;
} else {
Vcents = Vcents;
}
if(Vcents == "900") {
Vcents = "90";
} else
if(Vcents == "800") {
Vcents = "80";
} else
if(Vcents == "700") {
Vcents = "70";
} else
if(Vcents == "600") {
Vcents = "60";
} else
if(Vcents == "500") {
Vcents = "50";
} else
if(Vcents == "400") {
Vcents = "40";
} else
if(Vcents == "300") {
Vcents = "30";
} else
if(Vcents == "200") {
Vcents = "20";
} else
if(Vcents == "100") {
Vcents = "10";
} else {
Vcents = Vcents;
}
var Vformat = "";
if(Vnum >= 10000000) {
Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
}
else
if(Vnum >= 1000000) {
Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
}
else
if(Vnum >= 100000) {
Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
}
else
if(Vnum >= 10000) {
Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
}
else
if(Vnum >= 1000) {
Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
}
else
if(Vnum >= 100) {
Vformat = (Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
}
else
if(Vnum >= 10) {
Vformat = (Vtens + "" + Vones + "." + Vcents);
}
else
if(Vnum >= 1) {
Vformat = (Vones + "." + Vcents);
} else {
Vformat = ("0." + Vcents);
}
return Vformat;
}
}

function computeForm(form) {
	if(form.interest.value == "") {
		alert("Please enter the Interest Rate.");
	} else if(form.moAdd.value == "") {
		alert("Please enter the Monthly Addition.");
	} else if(form.payments.value == "") {
		alert("Please enter the Number of Years.");
	} else {
		var i = form.interest.value;
		i = i / 100;
		i /= 12;
		var ma = eval(form.moAdd.value);
		if(form.principal.value == "") {form.principal.value = 0; } else {form.principal.value = form.principal.value; };
		var prin = eval(form.principal.value);
		var pmts = eval(form.payments.value * 12);
		var count = 0;
		while(count < pmts) {
			newprin = prin + ma;
			prin = (newprin * i) + eval(prin + ma);
			count = count + 1;
		}
		var Vfv = prin;
		form.fv.value = "$" + formatNum(prin);
		var totinv = eval(count * ma) + eval(form.principal.value);
		var Vtotalint = eval(prin - totinv);
		form.totalint.value = "$" + formatNum(Vtotalint);
		//END NESTED IF TO CHECK FOR NON-NUMERIC ENTRIES
	}
}
function clearForm(form){
	form.payments.value = "";
	form.interest.value = "";
	form.principal.value = "";
	form.moAdd.value = "";
} 
