﻿
function Move2Page(page,type)
{
	var url = new String(window.location);

	var queryStringData = url.substring(url.indexOf("?"));

	//queryStringData += "&type=" + type;

	//get the type out of the querystring
	var partofQueryString = queryStringData.substring(queryStringData.lastIndexOf("&")+1);

	var pagewithoutext= page.substring(0,page.indexOf("."));

	/*if(type == "rate reducer" && page.indexOf("sensitive") == -1)
	{
		window.location = pagewithoutext + "2.stm" + queryStringData;
	}
	else
	{*/
		window.location = page + queryStringData;
	//}
}

//*********************************************************************************************************

function SubmitWeightRatesBalances(type) {
	
	if(!CheckRatesBalances()) { return; }
		
	var totalBalance = SumBalances();

	var theUrl = type + "?balances="+FormatAsCurrency(totalBalance.toString())+"&rate="+FormatAsPercent(GetRateWeightedAvg());
	
	var top = (screen.height /2)-300 ;
	var left = (screen.width / 2)-360;
	var theFeatures = "width=720,height=500,menubar=yes,scrollbars=yes,status=yes,resizable=yes,top=" + top + ",left=" + left;
	var calcResults= window.open(theUrl,"CalcResults",theFeatures);
	
	calcResults.focus();
}

//*********************************************************************************************************

function SumBalances() {

	//first make sure the numbers are kosher
	var balance1 = parseFloat(GetNumber(document.forms[0].balance1.value));
	var balance2 = parseFloat(GetNumber(document.forms[0].balance2.value));
	var balance3 = parseFloat(GetNumber(document.forms[0].balance3.value));
	var balance4 = parseFloat(GetNumber(document.forms[0].balance4.value));
	var balance5 = parseFloat(GetNumber(document.forms[0].balance5.value));
	var balance6 = parseFloat(GetNumber(document.forms[0].balance6.value));
	var balance7 = parseFloat(GetNumber(document.forms[0].balance7.value));
	var balance8 = parseFloat(GetNumber(document.forms[0].balance8.value));
	var balance9= parseFloat(GetNumber(document.forms[0].balance9.value));
	var balance10 = parseFloat(GetNumber(document.forms[0].balance10.value));

	var ReturnValue = 0;
	ReturnValue = balance1 + balance2 + balance3 + balance4 + balance5 +
				 balance6 + balance7 + balance8 + balance9 + balance10;

	return ReturnValue;
}

//*********************************************************************************************************

function GetRateWeightedAvg() {

	var balance = new Array(10);
	var rate = new Array(10);
	var theTotal = 0;

	//first make sure the balance numbers are kosher
	balance[0] = parseFloat(GetNumber(document.forms[0].balance1.value));
	balance[1] = parseFloat(GetNumber(document.forms[0].balance2.value));
	balance[2] = parseFloat(GetNumber(document.forms[0].balance3.value));
	balance[3] = parseFloat(GetNumber(document.forms[0].balance4.value));
	balance[4] = parseFloat(GetNumber(document.forms[0].balance5.value));
	balance[5] = parseFloat(GetNumber(document.forms[0].balance6.value));
	balance[6] = parseFloat(GetNumber(document.forms[0].balance7.value));
	balance[7] = parseFloat(GetNumber(document.forms[0].balance8.value));
	balance[8]= parseFloat(GetNumber(document.forms[0].balance9.value));
	balance[9] = parseFloat(GetNumber(document.forms[0].balance10.value));

	//first make sure the numbers are kosher
	rate[0] = parseFloat(GetNumber(document.forms[0].rate1.value));
	rate[1] = parseFloat(GetNumber(document.forms[0].rate2.value));
	rate[2] = parseFloat(GetNumber(document.forms[0].rate3.value));
	rate[3] = parseFloat(GetNumber(document.forms[0].rate4.value));
	rate[4] = parseFloat(GetNumber(document.forms[0].rate5.value));
	rate[5] = parseFloat(GetNumber(document.forms[0].rate6.value));
	rate[6] = parseFloat(GetNumber(document.forms[0].rate7.value));
	rate[7] = parseFloat(GetNumber(document.forms[0].rate8.value));
	rate[8]= parseFloat(GetNumber(document.forms[0].rate9.value));
	rate[9] = parseFloat(GetNumber(document.forms[0].rate10.value));

	//get the total of the balances multiplied by the interest rate
	for(var i=0;i<10;i++)
	{
		theTotal += balance[i] * rate[i];
	}

	//get the total for the balances
	var totalBalances = SumBalances();

	if(totalBalances > 0)
		return (theTotal / totalBalances); 
	else
		return 0;
}

//*********************************************************************************************************

function CheckRatesBalances() {

	var balance = new Array(10);
	var rate = new Array(10);
	var theTotal = 0;

	//first make sure the balance numbers are kosher
	balance[0] = document.forms[0].balance1.value;
	balance[1] = document.forms[0].balance2.value;
	balance[2] = document.forms[0].balance3.value;
	balance[3] = document.forms[0].balance4.value;
	balance[4] = document.forms[0].balance5.value;
	balance[5] = document.forms[0].balance6.value;
	balance[6] = document.forms[0].balance7.value;
	balance[7] = document.forms[0].balance8.value;
	balance[8]= document.forms[0].balance9.value;
	balance[9] = document.forms[0].balance10.value;
	
	
	for(var i=0;i<10;i++)
	{
		//remove any dollar signs
		if(balance[i].indexOf("$") != -1)
		{
			//take everything after the dollar sign
			balance[i] = balance[i].substring(balance[i].indexOf("$") + 1);
		}
		
		//remove any commas
		while(balance[i].indexOf(",") != -1)
		{
			var part1 = balance[i].substring(0,balance[i].indexOf(","));
			var part2 = balance[i].substring(balance[i].indexOf(",")+1);
			balance[i] = part1 + part2;
		}
		
		//check to see if it's a number and if it isn't then alert the user
		var theNumber = i;
		if(isNaN(balance[i]))
		{
			alert("Balance # " + (theNumber + 1) + " is not a number!");
			return false;
		}
	}

	
	//first make sure the numbers are kosher
	rate[0] = document.forms[0].rate1.value;
	rate[1] = document.forms[0].rate2.value;
	rate[2] = document.forms[0].rate3.value;
	rate[3] = document.forms[0].rate4.value;
	rate[4] = document.forms[0].rate5.value;
	rate[5] = document.forms[0].rate6.value;
	rate[6] = document.forms[0].rate7.value;
	rate[7] = document.forms[0].rate8.value;
	rate[8]=  document.forms[0].rate9.value;
	rate[9] = document.forms[0].rate10.value;
	
	
	for(var i=0;i<10;i++)
	{
		//remove % sign from the value
		if(rate[i].indexOf("%") != -1)
			rate[i] = rate[i].substring(0,rate[i].indexOf("%"));
		
		var theNumber = i;
		if(isNaN(rate[i]))
		{
			alert("Rate # " + (theNumber+1) + " is not a number");
			return false;
		}
	}
	
	return true;
}

//*********************************************************************************************************

function FormatAsCurrency(value2Format) {

	//if there isn't a decimal place then add the .00 to the end
	if(value2Format.indexOf(".") == -1)
	{
		value2Format = value2Format + ".00";
	}
	else
	{	
		var temp = value2Format.substring(value2Format.indexOf(".")+1);
	
	
		if(temp.length > 2)
		{
			//round to 2 decimal places
			temp = Math.round(temp/Math.pow(10,temp.length-2));
			
			if(temp.toString().length == 1)
			{
				temp = "0" + temp;
			}
			value2Format = value2Format.substring(0,value2Format.indexOf(".")) + "." + temp;
		}
		
	}
	
	//get the integer portion of the currency value
	var intpart = value2Format.substring(0,value2Format.indexOf("."));
	var decpart = value2Format.substring(value2Format.indexOf(".")+1);
	
	if(decpart.length == 1)
		decpart = decpart + "0";
		
	var theString="";
	var theCount = 0;

	//put the comma's in place
	for(var i=intpart.length-1;i >=0;i--)
	{
		
			theString = intpart.toString().charAt(i) + theString; 
			theCount +=1;
			if(theCount == 3 && i != 0)
			{	
				theCount = 0;
				theString = "," + theString;
			}
		
	}
	//stick a dollar sign on the front
	var holder  = "$" + theString + "." +  decpart.toString();
	
	return holder;
}

//*********************************************************************************************************

function FormatAsPercent(theNumber) {
	
	//check to see if the number is already a percentage or in integer format\
	if(theNumber < 1)
		theNumber *= 100;
		
		var temp = "";
	
		if(theNumber.toString().indexOf(".") != -1)
			temp = theNumber.toString().substring(theNumber.toString().indexOf(".")+1);
		
		if(temp.length > 3) {
		
			//round to 2 decimal places
			var value2round = temp/Math.pow(10,temp.length-3);
			
			var frontSide = value2round.toString().substring(0,value2round.toString().indexOf("."));
			
			if(frontSide.length == 1) {
				temp = "0" + frontSide;
			} else {
				temp = Math.round(value2round);
			}
		}
		
		if(temp.length !=0) {
		
			if(temp.length == 1)
				temp *= 100;
			if(temp.length == 2)
				temp *= 10;
				
			//get it back into a decimal	
			temp = temp/1000;
			
			var extra = 0;
			
			//drop through and round to next 1/8th
			if (temp > 0 && temp <= .125) {
				temp = "125";
			} else if(temp > .125 && temp <= .250) {
				temp = "250";
			} else if(temp > .250 && temp <= .375) {
				temp = "375";
			} else if(temp > .375 && temp <= .5) {
				temp = "5";
			} else if(temp > .5 && temp <= .625) {
				temp = "625";
			} else if(temp > .625 && temp <= .750) {
				temp = "750";
			} else if(temp > .750 && temp <= .875) {
				temp = "875";
			} else if(temp > .875 && temp <= 1) {
				extra = 1;
				temp = "000";
			}
						
			//put the number back together with the rounded decimal
			theNumber = parseInt(theNumber.toString().substring(0,theNumber.toString().indexOf("."))) + extra +  "." + temp;
		}
		 
	return theNumber;
	
}

//******************************************************************************************************************************

function GetNumber(theNumber) {	
	 
	//remove dollar sign from value
	if(theNumber.indexOf("$") != -1)
		theNumber = theNumber.substring(theNumber.indexOf("$") + 1);
	
	//remove % sign from the value
	if(theNumber.indexOf("%") != -1)
		theNumber = theNumber.substring(0,theNumber.indexOf("%"));
		
	while(theNumber.indexOf(",") != -1)
	{
		var part1 = theNumber.substring(0,theNumber.indexOf(","));
		var part2 = theNumber.substring(theNumber.indexOf(",")+1);
		theNumber = part1 + part2;
	}
	
	if(isNaN(theNumber))
		return 0;
	
	if(theNumber.length == 0)
		return 0;
	
	return theNumber;
}

//*******************************************************************************************************************************

// JScript source code
var secondPayment = 0;

function PerformStandardCalculations()
	{
		var item = document.getElementById("cellPrincipal");
		
		var principal = GetNumber(document.forms[0].txtPrincipal.value);
		
		if(principal == 0)
		{
			alert("The value entered for the principal is not a number!");
			return;
		}	
		
		item.innerHTML = "<b>" + FormatAsCurrency(principal) + "</b>";

		var rate = GetNumber(document.forms[0].txtInterestRate.value);
		
		if(rate == 0)
		{
			alert("The value entered for the interest rate is not a number!");
			return;
		}	

		var newPrincipal=0;
		var reducedPrincipal=0;
		
		var term = parseInt(document.forms[0].cboTerm.value);
		

		/*var savings = principal * .05;

		newPrincipal = principal - savings;

		//set the new principal after the 5% deduction
		item = document.getElementById("newPrincipal");
		item.innerHTML = "<p align='right'>" + FormatAsCurrency(newPrincipal.toString()) ;

		//show the savings
		item = document.getElementById("savings");
		item.innerHTML = "<b><font color='#980C48'>" + FormatAsCurrency(savings.toString()) + "</font></b>";
		
		reducedPrincipal = newPrincipal;
		*/								
		//set the terms
		item = document.getElementById("term");
		item.innerHTML = document.forms[0].cboTerm.value + " months";
		
		//item = document.getElementById("reducedTerm");
		//item.innerHTML = document.forms[0].cboTerm.value + " months";
		
		//fill in the interest rates
		item = document.getElementById("rate");
		item.innerHTML = FormatAsPercent(rate) + "%";
		
		//item = document.getElementById("reducedRate");
		//item.innerHTML = FormatAsPercent(rate - .25) + "%";
				
		var fixedPayment = ReturnPaymentAmt(rate,newPrincipal,term,false);
		if(fixedPayment == 0) return;
		
		item = document.getElementById("fixedPayment");
		item.innerHTML = FormatAsCurrency(fixedPayment.toString());
		
		/*var reducedfixedPayment = ReturnPaymentAmt(rate,reducedPrincipal,document.forms[0].cboTerm.value,true);
		item = document.getElementById("reducedfixedPayment");
		item.innerHTML = FormatAsCurrency(reducedfixedPayment.toString());*/
				
}

//********************************************************************************************************************
	
function RateReducerStandard()
{
	var principal = GetNumber(document.forms[0].txtPrincipal.value);
	var rate = GetNumber(document.forms[0].txtInterestRate.value);
	document.forms[0].txtInterestRate.value = FormatAsPercent(document.forms[0].txtInterestRate.value);
	//var prepaymentTerm = 48;
	var term = parseInt(document.forms[0].cboTerm.value);//prepaymentTerm;
	//var reducedTerm = parseInt(document.forms[0].cboTerm.value) - prepaymentTerm;

	//get the principal that starts after the intro period for normal
	var firstPart = principal * Math.pow((rate/12)/100+1,term);
	
	var theRate = (rate /12) /100;
	
	//get init payments 
	var initPayments = ReturnPaymentAmt(rate,principal,document.forms[0].cboTerm.value,false);
	
	var loopValue =0;
	
	for(var i=0;i<=term-1;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	var secPart = initPayments * loopValue;
	
	var newPrincipal = firstPart - secPart;
	

	//get the principal that starts after the intro period for reduced
	/*firstPart = principal * Math.pow(((rate -.25)/12)/100+1,term);
	
	theRate = ((rate-.25) /12) /100;
					
	//get init payments 
	var reducedinitPayments = ReturnPaymentAmt(rate,principal,document.forms[0].cboTerm.value,true);

	loopValue =0;
	
	for(var i=0;i<=term-1;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = reducedinitPayments * loopValue;
	
	var reducedPrincipal = firstPart - secPart;
	*/
	
	//reduce the rate by 1%
	//rate -= 1;
	
	
	//These are the fixed payments for the first period before the reduce interst kicks in
	var item = document.getElementById("initfixedPayment");
	item.innerHTML = FormatAsCurrency(initPayments.toString());
	
	//This is the fixed payment for the first period before the reduced interest kicks in but
	//it incorporates the .25% reduction for the auto-deduction
	//item = document.getElementById("reducedfixedPayment");
	//item.innerHTML = FormatAsCurrency(reducedinitPayments.toString());
	
	//This is the payment after the initial period with the reduced interest rate
	/*item = document.getElementById("percReducedFixedPayment");
	var temp = ReturnPaymentAmt(rate,newPrincipal,reducedTerm,false);
	item.innerHTML = FormatAsCurrency(temp.toString());*/
	
	//This is the payment after the initial period with the reduced interest rate but
	//it incorporates the .25% reduction for the auto-deduction
	/*item = document.getElementById("priorReducedFixedPayment");
	temp = ReturnPaymentAmt(rate,reducedPrincipal,reducedTerm,true);
	item.innerHTML = FormatAsCurrency(temp.toString());*/
	
	//set descriptors to take into account the appropriate term.
	item = document.getElementById("paymentheader1");
	item.innerHTML = "<p align='left'>Payment on principal for " + term.toString() + " months</p>";
	
	/*item = document.getElementById("paymentheader2");
	item.innerHTML = "<p align='left'>Payment after 1% reduction after " + term.toString() + " months</p>";*/
	
	/*item = document.getElementById("paymentheader3");
	item.innerHTML = "<p align='left'>Payment on principal first "+term.toString()+" months</p>";
	
	item = document.getElementById("paymentheader4");
	item.innerHTML = "<p align='left'>Payment after 1% reduction after "+ term.toString() + " months</p>";*/
	
	item = document.getElementById("term");
	item.innerHTML = "<p align='center'>" + term.toString() + " months</p>";
	
	item = document.getElementById("rate");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(document.forms[0].txtInterestRate.value) + "%</p>";
	
	/*item = document.getElementById("term2");
	item.innerHTML = "<p align='center'>" + reducedTerm.toString() + " months</p>";
	
	item = document.getElementById("rate2");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(rate) + "%";*/
	
	/*item = document.getElementById("reducedTerm");
	item.innerHTML = "<p align='center'>" + term.toString() + " months</p>";
	
	item = document.getElementById("reducedRate");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(rate + 1 -.25) + "%";*/
	
	/*item = document.getElementById("reducedTerm2");
	item.innerHTML = "<p align='center'>" + reducedTerm.toString() + " months</p>";
	
	item = document.getElementById("reducedRate2");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(rate - .25) + "%";*/
	
}

//**********************************************************************************************************

function PerformGraduatedCalculations()
{
		//var item = document.getElementById("cellPrincipal");
		
		var principal = GetNumber(document.forms[0].txtPrincipal.value);
		
		if(principal == 0)
		{
			alert("The value entered for the principal is not a number!");
			return;
		}	
		
		//item.innerHTML = "<b>" + FormatAsCurrency(principal) + "</b>";

		/*var savings = principal * .05;
		
		var newPrincipal = principal - savings;
		
		//set the new principal after the 5% deduction
		item = document.getElementById("newPrincipal");
		item.innerHTML = "<p align='right'>" + FormatAsCurrency(newPrincipal.toString()) ;
		
		//show the savings
		item = document.getElementById("savings");
		item.innerHTML = "<b><font color='#980C48'>" + FormatAsCurrency(savings.toString()) + "</font></b>";
		*/
		var rate = FormatAsPercent(GetNumber(document.forms[0].txtInterestRate.value));
		document.forms[0].txtInterestRate.value = rate;
		
		if(rate == 0)
		{
			alert("The value entered for the interest rate is not a number!");
			return;
		}	
						
		//set the terms
		var item = document.getElementById("term");
		item.innerHTML = document.forms[0].cboTerm.value + " months";
		
		/*item = document.getElementById("reducedTerm");
		item.innerHTML = document.forms[0].cboTerm.value + " months";*/
		
		//fill in the interest rates
		item = document.getElementById("rate");
		item.innerHTML = rate + "%";
		
		/*item = document.getElementById("reducedRate");
		item.innerHTML = (rate - .25) + "%";*/

		var payment24Months = InterestOnlyGraduated(rate,principal,false);
		item = document.getElementById("payment24Months");
		item.innerHTML = FormatAsCurrency(payment24Months.toString());

		var select5payment24Months = InterestOnlyGraduated(rate,principal,false);
		item = document.getElementById("5payment24Months");
		item.innerHTML = FormatAsCurrency(payment24Months.toString());
		
		/*var reducedpayment24Months = InterestOnlyGraduated(rate,principal,true)
		item = document.getElementById("reducedpayment24Months");
		item.innerHTML = FormatAsCurrency(reducedpayment24Months.toString());*/
		
		var remainder24Months = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value) -24,false);
		item = document.getElementById("remainder24Months");
		item.innerHTML = FormatAsCurrency(remainder24Months.toString());
		
		/*var reducedremainder24Months = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value) -24,true);
		item = document.getElementById("reducedremainder24Months");
		item.innerHTML = FormatAsCurrency(reducedremainder24Months.toString());*/
		
		//item = document.getElementById("payment48Months");
		//item.innerHTML = FormatAsCurrency(payment24Months.toString());
		
		var select5Payment = CalculateSelect5Payment(rate,principal,parseInt(document.forms[0].cboTerm.value));
		item = document.getElementById("payment36Months");
		item.innerHTML = FormatAsCurrency(secondPayment.toString());

		/*item = document.getElementById("reducedpayment48Months");
		item.innerHTML = FormatAsCurrency(reducedpayment24Months.toString());*/

		item = document.getElementById("remainder48Months");
		item.innerHTML = FormatAsCurrency(select5Payment.toString());
		
		/*var reducedremainder48Months = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value) -48,true);
		item = document.getElementById("reducedremainder48Months");
		item.innerHTML = FormatAsCurrency(reducedremainder48Months.toString());*/
}
//****************************************************************************************************************

 function CalculateSelect5Payment(interestRate,principal,term)
 {
	var rate = 0;
	var q = 0;
	var f2 = 0;
	var f =0;
	var pwr = 0;
	var avgf = 0;
	var select2b = 0;
	var tmp = 0;

	rate = interestRate / 100 * 31 / 365;

	var payment1 = (principal * rate * 100 + 0.5) / 100;

	for(var n = 1;n<=36;n++)
		f = f + Math.pow((1 - rate),(n - 1));
				
	f2 = f / 2;
	avgf = f2 / 36;

	for(var j = 1;j<=term - 24;j++)
		pwr += Math.pow((1 / (1 + rate)), j);

	select2b = principal / pwr;

	q = (select2b - principal * rate) * avgf;

	secondPayment = ((payment1 + q) * 100 + 0.5) / 100;

	for(var j = 1;j<=term - 60;j++)
		tmp += Math.pow((1 / (1 + rate)),j);
				
	return ((((principal - 36 * q) / tmp) * 100 + 0.5) / 100);

}


//****************************************************************************************************************

function RateReducerGraduated()
{
	var principal = GetNumber(document.forms[0].txtPrincipal.value);
	var rate = GetNumber(document.forms[0].txtInterestRate.value);
	var term = GetNumber(document.forms[0].cboTerm.value);
	/*var initPeriod = 48;//GetNumber(document.forms[0].txtInitTerm.value);
	
	var headerName = new Array("header1","header2","header3");
	var header1Value = new Array("First 36 months:&nbsp;","First 48 months:&nbsp;",
								 "First 48 months:&nbsp;");
	var header2Value = new Array("Third Year:&nbsp;","Third and Fourth Years:&nbsp;",
								 "Third, Fourth, and Fifth Years:&nbsp;");
	var header3Value = new Array("Fourth Year:","","Fifth Year:");
	
	var theIndex = 0;
	
	switch(initPeriod)
	{
		case 36:
			theIndex = 0;
			break;
		case 48:
			theIndex = 1;
			break;
		case 60:
			theIndex = 2;
			break;
	}
	
	var item = null;
	*/
	/*for(var j=0;j<3;j++)
	{
		item = document.getElementById(headerName[j]);
		switch(j)
		{
			case 0:
				item.innerHTML = header1Value[theIndex];
				item = document.getElementById(headerName[j] + "2");
				item.innerHTML = header1Value[theIndex];
				break;

			case 1:
				item.innerHTML = header2Value[theIndex];
				item = document.getElementById(headerName[j] + "2");
				item.innerHTML = header2Value[theIndex];
				break;
			case 2:
				item.innerHTML = header3Value[theIndex];
				item = document.getElementById(headerName[j] + "2");
				item.innerHTML = header3Value[theIndex];
				break;
		}	
	}
	*/
	
	//get the initial interest only payments
	/*var fixed2yrInterest = principal * (rate/100) /12;
	item = document.getElementById("fixedPaymentInterest");
	item.innerHTML = FormatAsCurrency(fixed2yrInterest.toString());*/
	
	/*item = document.getElementById("fixedPaymentInterest2");
	item.innerHTML = FormatAsCurrency(fixed2yrInterest.toString());
	*/
	/*var reduced2yrInterest = principal * ((rate -.25)/100) /12;
	item = document.getElementById("reducedPaymentInterest");
	item.innerHTML = FormatAsCurrency(reduced2yrInterest.toString());
	
	item = document.getElementById("reducedPaymentInterest2");
	item.innerHTML = FormatAsCurrency(reduced2yrInterest.toString());*/
	
	
	//set the term
	var item = document.getElementById("term");
	item.innerHTML = "<p align ='center'>" + term.toString() + " months</p>";
	/*item = document.getElementById("reducedTerm");
	item.innerHTML = "<p align='center'>" + term.toString() + " months</p>";*/
	
	//get the data for the inbetween yrs
	/*var fixedNormalPayments = ReturnPaymentAmt(rate,principal,term -24,false);
	item = document.getElementById("betweenYrs2Yr");
	item.innerHTML = FormatAsCurrency(fixedNormalPayments.toString());
	
	if(initPeriod == 36)
	{
		var fixed4yrNormalPayments =  principal * ((rate-1)/100) /12;
		item = document.getElementById("betweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrNormalPayments.toString());
		
		var fixed4yrReducedPayments =  principal * ((rate-1.25)/100) /12;
		item = document.getElementById("reducedbetweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrReducedPayments.toString());
	}
	else if(initPeriod == 60)
	{	
		var fixed4yrNormalPayments = ReturnPaymentAmt(rate,principal,term -48,false);
		item = document.getElementById("betweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrNormalPayments.toString());
		
		var fixed4yrReducedPayments = ReturnPaymentAmt(rate,principal,term -48,true);
		item = document.getElementById("reducedbetweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrReducedPayments.toString());
	}*/
		
	//get the data for the inbetween yrs
	/*var fixedReducedPayments = ReturnPaymentAmt(rate,principal,term -24,true);
	item = document.getElementById("reducedbetweenYrs2Yr");
	item.innerHTML = FormatAsCurrency(fixedReducedPayments.toString());*/
	
	//get the principal that starts after the intro period for normal
	var firstPart = principal * Math.pow((rate/12)/100+1,initPeriod-24);
	
	var theRate = (rate /12) /100;
	
	var loopValue =0;
	
	for(var i=0;i<=initPeriod-25;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	var secPart = fixedNormalPayments * loopValue;

	var newPrincipal = firstPart - secPart;

	//get the principal that starts after the intro period for reduced
	/*firstPart = principal * Math.pow(((rate -.25)/12)/100+1,initPeriod-24);
	
	theRate = ((rate-.25) /12) /100;
					
	loopValue =0;
	
	for(var i=0;i<=initPeriod-25;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = fixedReducedPayments * loopValue;
	
	var reducedPrincipal = firstPart - secPart;*/
	
	
	//get the data for the remainder 2yr 
	var remainder2yrPayments = ReturnPaymentAmt(rate,newPrincipal,term -initPeriod,false);
	item = document.getElementById("remainder24Months");
	item.innerHTML = FormatAsCurrency(remainder2yrPayments.toString());
	
	//get the data for the remainder 2yr 
	/*var reducedremainder2yrPayments = ReturnPaymentAmt(rate-1,reducedPrincipal,term -initPeriod,true);
	item = document.getElementById("reducedremainder24Months");
	item.innerHTML = FormatAsCurrency(reducedremainder2yrPayments.toString());*/
	
	
	//get the principal that starts after the intro period for normal
	firstPart = principal * Math.pow((rate/12)/100+1,initPeriod-48);
	
	theRate = (rate /12) /100;
	
	//get init payments 
	initPayments = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value)-48,false);

	loopValue =0;
	
	for(var i=0;i<=initPeriod-49;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = initPayments * loopValue;
	
	newPrincipal = firstPart - secPart;

	//get the principal that starts after the intro period for reduced
	/*firstPart = principal * Math.pow(((rate -.25)/12)/100+1,initPeriod-48);
	
	theRate = ((rate-.25) /12) /100;
					
	//get init payments 
	var reducedinitPayments = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value)-48,true);

	loopValue =0;
	
	for(var i=0;i<=initPeriod-49;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = reducedinitPayments * loopValue;
	
	reducedPrincipal = firstPart - secPart;
	
	*/
	/*if(initPeriod == 36)
	{
		newPrincipal = principal;
		reducedPrincipal = principal;
	}
	
	if(initPeriod != 60)
	{*/
		//get the data for the remainder 4yrs
		var remainderPayments = ReturnPaymentAmt(rate,newPrincipal,term -48,false);
		item = document.getElementById("remainder48Months");
		item.innerHTML = FormatAsCurrency(remainderPayments.toString());
		
		//get the data for the remainder 4yrs
		/*var reducedremainderPayments = ReturnPaymentAmt(rate-1,reducedPrincipal,term -48,true);
		item = document.getElementById("reducedremainder48Months");
		item.innerHTML = FormatAsCurrency(reducedremainderPayments.toString());
	}
	else{
		//get the data for the remainder 4yrs
		var remainderPayments = ReturnPaymentAmt(rate-1,newPrincipal,term -initPeriod,false);
		item = document.getElementById("remainder48Months");
		item.innerHTML = FormatAsCurrency(remainderPayments.toString());
		
		//get the data for the remainder 4yrs
		var reducedremainderPayments = ReturnPaymentAmt(rate-1,reducedPrincipal,term -initPeriod,true);
		item = document.getElementById("reducedremainder48Months");
		item.innerHTML = FormatAsCurrency(reducedremainderPayments.toString());
	}*/
	
	
}

//****************************************************************************************************************

function PerformIncomeSensitiveCalculations()
{
		//var item = document.getElementById("cellPrincipal");
		
		var principal = GetNumber(document.forms[0].txtPrincipal.value);
		
		if(principal == 0)
		{
			alert("The value entered for the principal is not a number!");
			return;
		}	
		
		//item.innerHTML = "<b>" + FormatAsCurrency(principal) + "</b>";

		//var savings = principal * .05;
		
		var newPrincipal = principal; //- savings;
		
		//set the new principal after the 5% deduction
		//item = document.getElementById("newPrincipal");
		//item.innerHTML = "<p align='right'>" + FormatAsCurrency(newPrincipal.toString()) ;
		
		//show the savings
		/*item = document.getElementById("savings");
		item.innerHTML = "<b><font color='#980C48'>" + FormatAsCurrency(savings.toString()) + "</font></b>";*/
		
		var rate = FormatAsPercent(GetNumber(document.forms[0].txtInterestRate.value));
		document.forms[0].txtInterestRate.value = rate;
		if(rate == 0)
		{
			alert("The value entered for the interest rate is not a number!");
			return;
		}	
		
		//fill in the interest rates
		var item = document.getElementById("rate");
		item.innerHTML = rate + "%";
		
		/*item = document.getElementById("reducedRate");
		item.innerHTML = (rate - .25) + "%";*/
		
		var MonthlyIncome = GetNumber(document.forms[0].txtMonthlyIncome.value);
		
		var fixedPayment = GetIncomeSensitivePayment(newPrincipal,MonthlyIncome,rate,false);
		if(fixedPayment == 0) return;
		
		item = document.getElementById("fixedPayment");
		item.innerHTML = FormatAsCurrency(fixedPayment.toString());
		
		/*var reducedfixedPayment =  GetIncomeSensitivePayment(newPrincipal,MonthlyIncome,rate,true);
		item = document.getElementById("reducedfixedPayment");
		item.innerHTML = FormatAsCurrency(reducedfixedPayment.toString());*/
}

//******************************************************************************************************************************

function CheckPrincipal()
{
	var principal = document.forms[0].txtPrincipal.value;
	
	var value = 0;

	if(principal.length == 0 )
	{
		value = 0;
	}else{	
		value = GetNumber(principal);
	}
	
	if(value == 0)
	{
		alert("You have to enter the amount borrowed first!");
		document.forms[0].txtPrincipal.focus();
	}
}

//*******************************************************************************************************************************

function FillInitialData()
{
	//get the url to pull the querysting elements out of it
	var theUrl = new String(document.location);
	
	var startIndex = theUrl.indexOf("?");
	var endIndex = theUrl.indexOf("&");

	var queryStringValue = theUrl.substring(startIndex+1);

	if(startIndex != -1)
	{
		var newQueryString = queryStringValue.substring(queryStringValue.indexOf("&")+1);
		var balance = queryStringValue.substring(0,queryStringValue.indexOf("&"));
		document.forms[0].txtPrincipal.value = balance.substring(balance.indexOf("=")+1);

		document.forms[0].txtInterestRate.value = newQueryString.substring(newQueryString.indexOf("=")+1);//,newQueryString.indexOf("&"));
		
		/*newQueryString = newQueryString.substring(newQueryString.indexOf("&")+1);
		
		document.forms[0].txtType.value = newQueryString.substring(newQueryString.indexOf("=")+1);*/

		if(theUrl.indexOf("sensitive") == -1)
		{
			ChangeTerms();
		}
	}
	
	
}

//***********************************************************************************************************

function ChangeTerms()
{
	var principal =  document.forms[0].txtPrincipal.value;
	
	//call function to make sure that the princpal is a number
	principal = GetNumber(principal);
	
	//if it's not a number or 0 then let the user know and exit the fcn
	if(principal == 0 )
	{
		alert("The amount borrowed entered is not a valid amount!");
		return;
	}else{
		//determine how many months to put in the combo box based upon the principal
		if(document.forms[0].cboTerm != null){
		
		
		if(principal < 7500)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			
		}else if(principal >= 7500 && principal < 10000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
		}else if(principal >= 10000 && principal < 20000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
		}else if(principal >= 20000 && principal < 40000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
			newOption = new Option("240 months","240");
			document.forms[0].cboTerm.options[4] = newOption;
		}else if(principal >= 40000 && principal < 60000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
			newOption = new Option("240 months","240");
			document.forms[0].cboTerm.options[4] = newOption;
			newOption = new Option("300 months","300");
			document.forms[0].cboTerm.options[5] = newOption;
		}else if(principal >= 60000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
			newOption = new Option("240 months","240");
			document.forms[0].cboTerm.options[4] = newOption;
			newOption = new Option("300 months","300");
			document.forms[0].cboTerm.options[5] = newOption;
			newOption = new Option("360 months","360");
			document.forms[0].cboTerm.options[6]= newOption;
		}
		}
	}
}

//***********************************************************************************************************

function ReturnPaymentAmt(interestRate,principal,term,autoDeduction)
{
	//do error checking
	if(interestRate.length == 0)
	{ alert("You did not enter an interest rate");}

	if(principal.length == 0)
	{ alert("You did not enter the amount borrowed!"); }
	
	if(term.length == 0)
	{ alert("You did not select a term!"); return 0;}
	
	if(isNaN(principal))
	{ alert("The total borrowed amount is not a number!"); return 0;}
	else
	{ if(principal <= 0 ) {alert("The amount borrowed cannot be zero or less!"); return 0;}}
	
	if(isNaN(interestRate))
	{	alert("The interest rate you entered is not a number!"); return 0; }
	else
	{  if(interestRate <= 0) {alert("The interest rate cannot be zero or less!"); return 0;}}
		
	var theRate;
	//check to see if they are using auto-deduction and if they are subtract .25% off the interest rate
	if(autoDeduction == true)
	{
		//get the monthly interest rate as a decimal
		theRate = ((interestRate-.25)/12 ) /100 ;
	}else{
		theRate = (interestRate/12) / 100;
	}
	
	var numerator = 1 - (1 + theRate);
	var holder = 1 + theRate ;
	
	var temp =  Math.pow(holder,term);
	
	var denominator = 1 - temp;
	var innervalue = (numerator / denominator) + theRate;
	
	var value =  principal * innervalue ;
	
	//make sure the value isn't less than the minimum amount
	if(value < 50)
	{
		return 50;
	}else{
		return value;
	}

}

//********************************************************************************************************************

function InterestOnlyGraduated(interestRate, principal,autoDeduction)
{
	var term = document.forms[0].cboTerm.value;

	//do error checking
	if(interestRate.length == 0)
	{ alert("You did not enter an interest rate");}

	if(principal.length == 0)
	{ alert("You did not enter the amount borrowed!"); }
	
	if(term.length == 0)
	{ alert("You did not select a term!"); return 0;}
	
	if(isNaN(principal))
	{ alert("The total borrowed amount is not a number!"); return 0;}
	else
	{ if(principal <= 0 ) {alert("The amount borrowed cannot be zero or less!"); return 0;}}
	
	if(isNaN(interestRate))
	{	alert("The interest rate you entered is not a number!"); return 0; }
	else
	{  if(interestRate <= 0) {alert("The interest rate cannot be zero or less!"); return 0;}}

	var value=0;
	
	//check if you need to decrease the interest rate due to auto deduction
	if(autoDeduction == true)
	{
		value =  (principal * (((interestRate -.25) /100 )) /12);
	}else{
		value =  (principal * ((interestRate /100 )) /12);
	}
	
	//make sure the value isn't less than the minimum amount
	if(value < 50)
	{
		return 50;
	}else{
		return value;
	}
}

//******************************************************************************************************************

function GetIncomeSensitivePayment(principal,monthlyGrossIncome,interestRate,autoDeduction)
{
	//do error checking
	if(interestRate.length == 0)
	{ alert("You did not enter an interest rate");}

	if(principal.length == 0)
	{ alert("You did not enter the amount borrowed!"); }
	
	if(monthlyGrossIncome.length == 0)
	{ alert("You did not enter your gross monthly income!"); return 0;}
		
	if(isNaN(principal))
	{ alert("The total borrowed amount is not a number!"); return 0;}
	else
	{ if(principal <= 0 ) {alert("The amount borrowed cannot be zero or less!"); return 0;}}
	
	if(isNaN(interestRate))
	{	alert("The interest rate you entered is not a number!"); return 0; }
	else
	{  if(interestRate <= 0) {alert("The interest rate cannot be zero or less!"); return 0;}}
	
	var theRate = 0;
	
	//check to see if they are using auto-deduction and if they are subtract .25% off the interest rate
	if(autoDeduction == true)
	{
		//get the monthly interest rate as a decimal
		theRate = ((interestRate-.25) ) /100 ;
	}else{
		theRate = (interestRate) / 100;
	}
	
	monthlyGrossIncome = GetNumber(monthlyGrossIncome);
	
	if(monthlyGrossIncome == 0)
	{
		alert("The number entered for monthly gross income is not a valid number!");
		return;
	}
	
	//get the income-to-debt ratio
	var ratio = monthlyGrossIncome / principal;
	
	var monthlyPaymentPercentage = 0;
	
	if(ratio < .09)
	{
		monthlyPaymentPercentage = .04;
	}
	else if(ratio >= .09 && ratio <= .119)
	{
		monthlyPaymentPercentage = .06;
	}
	else if(ratio >= .12 && ratio <= .149)
	{
		monthlyPaymentPercentage = .09;
	}
	else if(ratio >= .15 && ratio <= .179)
	{
		monthlyPaymentPercentage = .10;
	}
	else if(ratio >= .18 && ratio <= .209)
	{
		monthlyPaymentPercentage = .12;
	}
	else if(ratio >= .21 && ratio <= .239)
	{
		monthlyPaymentPercentage = .14;
	}
	else if(ratio >= .24)
	{
		monthlyPaymentPercentage = .15;
	}

	var monthlyPayment = monthlyGrossIncome * monthlyPaymentPercentage;

	var monthlyInterest = (principal * theRate) / 12;
		
	if(monthlyPayment < monthlyInterest)
	{
		alert("Your monthly payment is less than the monthly interest accrued on the loan!");
		return 0;
	}

	return monthlyPayment;
}

//******************************************************************************************************************************

function GetNumber(theNumber)
{	
	 
	//remove dollar sign from value
	if(theNumber.indexOf("$") != -1)
		theNumber = theNumber.substring(theNumber.indexOf("$") + 1);
	
	//remove % sign from the value
	if(theNumber.indexOf("%") != -1)
		theNumber = theNumber.substring(0,theNumber.indexOf("%"));
		
	while(theNumber.indexOf(",") != -1)
	{
		var part1 = theNumber.substring(0,theNumber.indexOf(","));
		var part2 = theNumber.substring(theNumber.indexOf(",")+1);
		theNumber = part1 + part2;
	}
	
	if(isNaN(theNumber))
		return 0;
	
	if(theNumber.length == 0)
		return 0;
	
	return theNumber;
}

//*****************************************************************************************************************

function FormatAsCurrency(value2Format)
{
	//if there isn't a decimal place then add the .00 to the end
	if(value2Format.indexOf(".") == -1)
	{
		value2Format = value2Format + ".00";
	}
	else
	{	
		var temp = value2Format.substring(value2Format.indexOf(".")+1);
	
	
		if(temp.length > 2)
		{
			//round to 2 decimal places
			temp = Math.round(temp/Math.pow(10,temp.length-2));
			
			if(temp.toString().length == 1)
			{
				temp = "0" + temp;
			}
			value2Format = value2Format.substring(0,value2Format.indexOf(".")) + "." + temp;
		}
		
	}
	
	//get the integer portion of the currency value
	var intpart = value2Format.substring(0,value2Format.indexOf("."));
	var decpart = value2Format.substring(value2Format.indexOf(".")+1);
	
	if(decpart.length == 1)
		decpart = decpart + "0";
		
	var theString="";
	var theCount = 0;

	//put the comma's in place
	for(var i=intpart.length-1;i >=0;i--)
	{
		
			theString = intpart.toString().charAt(i) + theString; 
			theCount +=1;
			if(theCount == 3 && i != 0)
			{	
				theCount = 0;
				theString = "," + theString;
			}
		
	}
	//stick a dollar sign on the front
	var holder  = "$" + theString + "." +  decpart.toString();
	
	return holder;
}

//************************************************************************************************************************************

function FormatAsPercent(theNumber)
{
	
	//check to see if the number is already a percentage or in integer format\
	if(theNumber < 1)
		theNumber *= 100;
		
		var temp = "";
	
		if(theNumber.toString().indexOf(".") != -1)
			temp = theNumber.toString().substring(theNumber.toString().indexOf(".")+1);
		
		
		
		if(temp.length > 3)
		{
			//round to 2 decimal places
			var value2round = temp/Math.pow(10,temp.length-3);
			
			var frontSide = value2round.toString().substring(0,value2round.toString().indexOf("."));
			
			if(frontSide.length == 1)
			{
				temp = "0" + frontSide;
			}
			else
			{
				temp = Math.round(value2round);
			}
									
		}
		
		if(temp.length !=0)
		{
			if(temp.length == 1)
				temp *= 100;
			if(temp.length == 2)
				temp *= 10;
				
			//get it back into a decimal	
			temp = temp/1000;
			
			var extra = 0;
			
			//drop through and round to next 1/8th
			if(temp > 0 && temp <= .125)
			{
				temp = "125";
			}
			else if(temp > .125 && temp <= .250)
			{
				temp = "250";
			}
			else if(temp > .250 && temp <= .375)
			{
				temp = "375";
			}
			else if(temp > .375 && temp <= .5)
			{
				temp = "5";
			}
			else if(temp > .5 && temp <= .625)
			{
				temp = "625";
			}
			else if(temp > .625 && temp <= .750)
			{
				temp = "750";
			}
			else if(temp > .750 && temp <= .875)
			{
				temp = "875";
			}
			else if(temp > .875 && temp <= 1)
			{
				extra = 1;
				temp = "000";
			}
						
			//put the number back together with the rounded decimal
			theNumber = parseInt(theNumber.toString().substring(0,theNumber.toString().indexOf("."))) + extra +  "." + temp;
		}
		 
	return theNumber;
	
}

//************************************************************************************************************************************

// JScript source code
var secondPayment = 0;

function PerformStandardCalculations()
	{
		var item = document.getElementById("cellPrincipal");
		
		var principal = GetNumber(document.forms[0].txtPrincipal.value);
		
		if(principal == 0)
		{
			alert("The value entered for the principal is not a number!");
			return;
		}	
		
		item.innerHTML = "<b>" + FormatAsCurrency(principal) + "</b>";

		var rate = GetNumber(document.forms[0].txtInterestRate.value);
		
		if(rate == 0)
		{
			alert("The value entered for the interest rate is not a number!");
			return;
		}	

		var newPrincipal=0;
		var reducedPrincipal=0;
		
		var term = parseInt(document.forms[0].cboTerm.value);
		

		/*var savings = principal * .05;

		newPrincipal = principal - savings;

		//set the new principal after the 5% deduction
		item = document.getElementById("newPrincipal");
		item.innerHTML = "<p align='right'>" + FormatAsCurrency(newPrincipal.toString()) ;

		//show the savings
		item = document.getElementById("savings");
		item.innerHTML = "<b><font color='#980C48'>" + FormatAsCurrency(savings.toString()) + "</font></b>";
		
		reducedPrincipal = newPrincipal;
		*/								
		//set the terms
		item = document.getElementById("term");
		item.innerHTML = document.forms[0].cboTerm.value + " months";
		
		//item = document.getElementById("reducedTerm");
		//item.innerHTML = document.forms[0].cboTerm.value + " months";
		
		//fill in the interest rates
		item = document.getElementById("rate");
		item.innerHTML = FormatAsPercent(rate) + "%";
		
		//item = document.getElementById("reducedRate");
		//item.innerHTML = FormatAsPercent(rate - .25) + "%";
				
		var fixedPayment = ReturnPaymentAmt(rate,newPrincipal,term,false);
		if(fixedPayment == 0) return;
		
		item = document.getElementById("fixedPayment");
		item.innerHTML = FormatAsCurrency(fixedPayment.toString());
		
		/*var reducedfixedPayment = ReturnPaymentAmt(rate,reducedPrincipal,document.forms[0].cboTerm.value,true);
		item = document.getElementById("reducedfixedPayment");
		item.innerHTML = FormatAsCurrency(reducedfixedPayment.toString());*/
				
}

//********************************************************************************************************************
	
function RateReducerStandard()
{
	var principal = GetNumber(document.forms[0].txtPrincipal.value);
	var rate = GetNumber(document.forms[0].txtInterestRate.value);
	document.forms[0].txtInterestRate.value = FormatAsPercent(document.forms[0].txtInterestRate.value);
	//var prepaymentTerm = 48;
	var term = parseInt(document.forms[0].cboTerm.value);//prepaymentTerm;
	//var reducedTerm = parseInt(document.forms[0].cboTerm.value) - prepaymentTerm;

	//get the principal that starts after the intro period for normal
	var firstPart = principal * Math.pow((rate/12)/100+1,term);
	
	var theRate = (rate /12) /100;
	
	//get init payments 
	var initPayments = ReturnPaymentAmt(rate,principal,document.forms[0].cboTerm.value,false);
	
	var loopValue =0;
	
	for(var i=0;i<=term-1;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	var secPart = initPayments * loopValue;
	
	var newPrincipal = firstPart - secPart;
	

	//get the principal that starts after the intro period for reduced
	/*firstPart = principal * Math.pow(((rate -.25)/12)/100+1,term);
	
	theRate = ((rate-.25) /12) /100;
					
	//get init payments 
	var reducedinitPayments = ReturnPaymentAmt(rate,principal,document.forms[0].cboTerm.value,true);

	loopValue =0;
	
	for(var i=0;i<=term-1;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = reducedinitPayments * loopValue;
	
	var reducedPrincipal = firstPart - secPart;
	*/
	
	//reduce the rate by 1%
	//rate -= 1;
	
	
	//These are the fixed payments for the first period before the reduce interst kicks in
	var item = document.getElementById("initfixedPayment");
	item.innerHTML = FormatAsCurrency(initPayments.toString());
	
	//This is the fixed payment for the first period before the reduced interest kicks in but
	//it incorporates the .25% reduction for the auto-deduction
	//item = document.getElementById("reducedfixedPayment");
	//item.innerHTML = FormatAsCurrency(reducedinitPayments.toString());
	
	//This is the payment after the initial period with the reduced interest rate
	/*item = document.getElementById("percReducedFixedPayment");
	var temp = ReturnPaymentAmt(rate,newPrincipal,reducedTerm,false);
	item.innerHTML = FormatAsCurrency(temp.toString());*/
	
	//This is the payment after the initial period with the reduced interest rate but
	//it incorporates the .25% reduction for the auto-deduction
	/*item = document.getElementById("priorReducedFixedPayment");
	temp = ReturnPaymentAmt(rate,reducedPrincipal,reducedTerm,true);
	item.innerHTML = FormatAsCurrency(temp.toString());*/
	
	//set descriptors to take into account the appropriate term.
	item = document.getElementById("paymentheader1");
	item.innerHTML = "<p align='left'>Payment on principal for " + term.toString() + " months</p>";
	
	/*item = document.getElementById("paymentheader2");
	item.innerHTML = "<p align='left'>Payment after 1% reduction after " + term.toString() + " months</p>";*/
	
	/*item = document.getElementById("paymentheader3");
	item.innerHTML = "<p align='left'>Payment on principal first "+term.toString()+" months</p>";
	
	item = document.getElementById("paymentheader4");
	item.innerHTML = "<p align='left'>Payment after 1% reduction after "+ term.toString() + " months</p>";*/
	
	item = document.getElementById("term");
	item.innerHTML = "<p align='center'>" + term.toString() + " months</p>";
	
	item = document.getElementById("rate");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(document.forms[0].txtInterestRate.value) + "%</p>";
	
	/*item = document.getElementById("term2");
	item.innerHTML = "<p align='center'>" + reducedTerm.toString() + " months</p>";
	
	item = document.getElementById("rate2");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(rate) + "%";*/
	
	/*item = document.getElementById("reducedTerm");
	item.innerHTML = "<p align='center'>" + term.toString() + " months</p>";
	
	item = document.getElementById("reducedRate");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(rate + 1 -.25) + "%";*/
	
	/*item = document.getElementById("reducedTerm2");
	item.innerHTML = "<p align='center'>" + reducedTerm.toString() + " months</p>";
	
	item = document.getElementById("reducedRate2");
	item.innerHTML = "<p align='center'>" + FormatAsPercent(rate - .25) + "%";*/
	
}

//**********************************************************************************************************

function PerformGraduatedCalculations()
{
		//var item = document.getElementById("cellPrincipal");
		
		var principal = GetNumber(document.forms[0].txtPrincipal.value);
		
		if(principal == 0)
		{
			alert("The value entered for the principal is not a number!");
			return;
		}	
		
		//item.innerHTML = "<b>" + FormatAsCurrency(principal) + "</b>";

		/*var savings = principal * .05;
		
		var newPrincipal = principal - savings;
		
		//set the new principal after the 5% deduction
		item = document.getElementById("newPrincipal");
		item.innerHTML = "<p align='right'>" + FormatAsCurrency(newPrincipal.toString()) ;
		
		//show the savings
		item = document.getElementById("savings");
		item.innerHTML = "<b><font color='#980C48'>" + FormatAsCurrency(savings.toString()) + "</font></b>";
		*/
		var rate = FormatAsPercent(GetNumber(document.forms[0].txtInterestRate.value));
		document.forms[0].txtInterestRate.value = rate;
		
		if(rate == 0)
		{
			alert("The value entered for the interest rate is not a number!");
			return;
		}	
						
		//set the terms
		var item = document.getElementById("term");
		item.innerHTML = document.forms[0].cboTerm.value + " months";
		
		/*item = document.getElementById("reducedTerm");
		item.innerHTML = document.forms[0].cboTerm.value + " months";*/
		
		//fill in the interest rates
		item = document.getElementById("rate");
		item.innerHTML = rate + "%";
		
		/*item = document.getElementById("reducedRate");
		item.innerHTML = (rate - .25) + "%";*/

		var payment24Months = InterestOnlyGraduated(rate,principal,false);
		item = document.getElementById("payment24Months");
		item.innerHTML = FormatAsCurrency(payment24Months.toString());

		var select5payment24Months = InterestOnlyGraduated(rate,principal,false);
		item = document.getElementById("5payment24Months");
		item.innerHTML = FormatAsCurrency(payment24Months.toString());
		
		/*var reducedpayment24Months = InterestOnlyGraduated(rate,principal,true)
		item = document.getElementById("reducedpayment24Months");
		item.innerHTML = FormatAsCurrency(reducedpayment24Months.toString());*/
		
		var remainder24Months = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value) -24,false);
		item = document.getElementById("remainder24Months");
		item.innerHTML = FormatAsCurrency(remainder24Months.toString());
		
		/*var reducedremainder24Months = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value) -24,true);
		item = document.getElementById("reducedremainder24Months");
		item.innerHTML = FormatAsCurrency(reducedremainder24Months.toString());*/
		
		//item = document.getElementById("payment48Months");
		//item.innerHTML = FormatAsCurrency(payment24Months.toString());
		
		var select5Payment = CalculateSelect5Payment(rate,principal,parseInt(document.forms[0].cboTerm.value));
		item = document.getElementById("payment36Months");
		item.innerHTML = FormatAsCurrency(secondPayment.toString());

		/*item = document.getElementById("reducedpayment48Months");
		item.innerHTML = FormatAsCurrency(reducedpayment24Months.toString());*/

		item = document.getElementById("remainder48Months");
		item.innerHTML = FormatAsCurrency(select5Payment.toString());
		
		/*var reducedremainder48Months = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value) -48,true);
		item = document.getElementById("reducedremainder48Months");
		item.innerHTML = FormatAsCurrency(reducedremainder48Months.toString());*/
}
//****************************************************************************************************************

 function CalculateSelect5Payment(interestRate,principal,term)
 {
	var rate = 0;
	var q = 0;
	var f2 = 0;
	var f =0;
	var pwr = 0;
	var avgf = 0;
	var select2b = 0;
	var tmp = 0;

	rate = interestRate / 100 * 31 / 365;

	var payment1 = (principal * rate * 100 + 0.5) / 100;

	for(var n = 1;n<=36;n++)
		f = f + Math.pow((1 - rate),(n - 1));
				
	f2 = f / 2;
	avgf = f2 / 36;

	for(var j = 1;j<=term - 24;j++)
		pwr += Math.pow((1 / (1 + rate)), j);

	select2b = principal / pwr;

	q = (select2b - principal * rate) * avgf;

	secondPayment = ((payment1 + q) * 100 + 0.5) / 100;

	for(var j = 1;j<=term - 60;j++)
		tmp += Math.pow((1 / (1 + rate)),j);
				
	return ((((principal - 36 * q) / tmp) * 100 + 0.5) / 100);

}


//****************************************************************************************************************

function RateReducerGraduated()
{
	var principal = GetNumber(document.forms[0].txtPrincipal.value);
	var rate = GetNumber(document.forms[0].txtInterestRate.value);
	var term = GetNumber(document.forms[0].cboTerm.value);
	/*var initPeriod = 48;//GetNumber(document.forms[0].txtInitTerm.value);
	
	var headerName = new Array("header1","header2","header3");
	var header1Value = new Array("First 36 months:&nbsp;","First 48 months:&nbsp;",
								 "First 48 months:&nbsp;");
	var header2Value = new Array("Third Year:&nbsp;","Third and Fourth Years:&nbsp;",
								 "Third, Fourth, and Fifth Years:&nbsp;");
	var header3Value = new Array("Fourth Year:","","Fifth Year:");
	
	var theIndex = 0;
	
	switch(initPeriod)
	{
		case 36:
			theIndex = 0;
			break;
		case 48:
			theIndex = 1;
			break;
		case 60:
			theIndex = 2;
			break;
	}
	
	var item = null;
	*/
	/*for(var j=0;j<3;j++)
	{
		item = document.getElementById(headerName[j]);
		switch(j)
		{
			case 0:
				item.innerHTML = header1Value[theIndex];
				item = document.getElementById(headerName[j] + "2");
				item.innerHTML = header1Value[theIndex];
				break;

			case 1:
				item.innerHTML = header2Value[theIndex];
				item = document.getElementById(headerName[j] + "2");
				item.innerHTML = header2Value[theIndex];
				break;
			case 2:
				item.innerHTML = header3Value[theIndex];
				item = document.getElementById(headerName[j] + "2");
				item.innerHTML = header3Value[theIndex];
				break;
		}	
	}
	*/
	
	//get the initial interest only payments
	/*var fixed2yrInterest = principal * (rate/100) /12;
	item = document.getElementById("fixedPaymentInterest");
	item.innerHTML = FormatAsCurrency(fixed2yrInterest.toString());*/
	
	/*item = document.getElementById("fixedPaymentInterest2");
	item.innerHTML = FormatAsCurrency(fixed2yrInterest.toString());
	*/
	/*var reduced2yrInterest = principal * ((rate -.25)/100) /12;
	item = document.getElementById("reducedPaymentInterest");
	item.innerHTML = FormatAsCurrency(reduced2yrInterest.toString());
	
	item = document.getElementById("reducedPaymentInterest2");
	item.innerHTML = FormatAsCurrency(reduced2yrInterest.toString());*/
	
	
	//set the term
	var item = document.getElementById("term");
	item.innerHTML = "<p align ='center'>" + term.toString() + " months</p>";
	/*item = document.getElementById("reducedTerm");
	item.innerHTML = "<p align='center'>" + term.toString() + " months</p>";*/
	
	//get the data for the inbetween yrs
	/*var fixedNormalPayments = ReturnPaymentAmt(rate,principal,term -24,false);
	item = document.getElementById("betweenYrs2Yr");
	item.innerHTML = FormatAsCurrency(fixedNormalPayments.toString());
	
	if(initPeriod == 36)
	{
		var fixed4yrNormalPayments =  principal * ((rate-1)/100) /12;
		item = document.getElementById("betweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrNormalPayments.toString());
		
		var fixed4yrReducedPayments =  principal * ((rate-1.25)/100) /12;
		item = document.getElementById("reducedbetweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrReducedPayments.toString());
	}
	else if(initPeriod == 60)
	{	
		var fixed4yrNormalPayments = ReturnPaymentAmt(rate,principal,term -48,false);
		item = document.getElementById("betweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrNormalPayments.toString());
		
		var fixed4yrReducedPayments = ReturnPaymentAmt(rate,principal,term -48,true);
		item = document.getElementById("reducedbetweenYrs4Yr");
		item.innerHTML = FormatAsCurrency(fixed4yrReducedPayments.toString());
	}*/
		
	//get the data for the inbetween yrs
	/*var fixedReducedPayments = ReturnPaymentAmt(rate,principal,term -24,true);
	item = document.getElementById("reducedbetweenYrs2Yr");
	item.innerHTML = FormatAsCurrency(fixedReducedPayments.toString());*/
	
	//get the principal that starts after the intro period for normal
	var firstPart = principal * Math.pow((rate/12)/100+1,initPeriod-24);
	
	var theRate = (rate /12) /100;
	
	var loopValue =0;
	
	for(var i=0;i<=initPeriod-25;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	var secPart = fixedNormalPayments * loopValue;

	var newPrincipal = firstPart - secPart;

	//get the principal that starts after the intro period for reduced
	/*firstPart = principal * Math.pow(((rate -.25)/12)/100+1,initPeriod-24);
	
	theRate = ((rate-.25) /12) /100;
					
	loopValue =0;
	
	for(var i=0;i<=initPeriod-25;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = fixedReducedPayments * loopValue;
	
	var reducedPrincipal = firstPart - secPart;*/
	
	
	//get the data for the remainder 2yr 
	var remainder2yrPayments = ReturnPaymentAmt(rate,newPrincipal,term -initPeriod,false);
	item = document.getElementById("remainder24Months");
	item.innerHTML = FormatAsCurrency(remainder2yrPayments.toString());
	
	//get the data for the remainder 2yr 
	/*var reducedremainder2yrPayments = ReturnPaymentAmt(rate-1,reducedPrincipal,term -initPeriod,true);
	item = document.getElementById("reducedremainder24Months");
	item.innerHTML = FormatAsCurrency(reducedremainder2yrPayments.toString());*/
	
	
	//get the principal that starts after the intro period for normal
	firstPart = principal * Math.pow((rate/12)/100+1,initPeriod-48);
	
	theRate = (rate /12) /100;
	
	//get init payments 
	initPayments = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value)-48,false);

	loopValue =0;
	
	for(var i=0;i<=initPeriod-49;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = initPayments * loopValue;
	
	newPrincipal = firstPart - secPart;

	//get the principal that starts after the intro period for reduced
	/*firstPart = principal * Math.pow(((rate -.25)/12)/100+1,initPeriod-48);
	
	theRate = ((rate-.25) /12) /100;
					
	//get init payments 
	var reducedinitPayments = ReturnPaymentAmt(rate,principal,parseInt(document.forms[0].cboTerm.value)-48,true);

	loopValue =0;
	
	for(var i=0;i<=initPeriod-49;i++)
	{
		loopValue += Math.pow(theRate+1,i);
	}
	
	secPart = reducedinitPayments * loopValue;
	
	reducedPrincipal = firstPart - secPart;
	
	*/
	/*if(initPeriod == 36)
	{
		newPrincipal = principal;
		reducedPrincipal = principal;
	}
	
	if(initPeriod != 60)
	{*/
		//get the data for the remainder 4yrs
		var remainderPayments = ReturnPaymentAmt(rate,newPrincipal,term -48,false);
		item = document.getElementById("remainder48Months");
		item.innerHTML = FormatAsCurrency(remainderPayments.toString());
		
		//get the data for the remainder 4yrs
		/*var reducedremainderPayments = ReturnPaymentAmt(rate-1,reducedPrincipal,term -48,true);
		item = document.getElementById("reducedremainder48Months");
		item.innerHTML = FormatAsCurrency(reducedremainderPayments.toString());
	}
	else{
		//get the data for the remainder 4yrs
		var remainderPayments = ReturnPaymentAmt(rate-1,newPrincipal,term -initPeriod,false);
		item = document.getElementById("remainder48Months");
		item.innerHTML = FormatAsCurrency(remainderPayments.toString());
		
		//get the data for the remainder 4yrs
		var reducedremainderPayments = ReturnPaymentAmt(rate-1,reducedPrincipal,term -initPeriod,true);
		item = document.getElementById("reducedremainder48Months");
		item.innerHTML = FormatAsCurrency(reducedremainderPayments.toString());
	}*/
	
	
}

//****************************************************************************************************************

function PerformIncomeSensitiveCalculations()
{
		//var item = document.getElementById("cellPrincipal");
		
		var principal = GetNumber(document.forms[0].txtPrincipal.value);
		
		if(principal == 0)
		{
			alert("The value entered for the principal is not a number!");
			return;
		}	
		
		//item.innerHTML = "<b>" + FormatAsCurrency(principal) + "</b>";

		//var savings = principal * .05;
		
		var newPrincipal = principal; //- savings;
		
		//set the new principal after the 5% deduction
		//item = document.getElementById("newPrincipal");
		//item.innerHTML = "<p align='right'>" + FormatAsCurrency(newPrincipal.toString()) ;
		
		//show the savings
		/*item = document.getElementById("savings");
		item.innerHTML = "<b><font color='#980C48'>" + FormatAsCurrency(savings.toString()) + "</font></b>";*/
		
		var rate = FormatAsPercent(GetNumber(document.forms[0].txtInterestRate.value));
		document.forms[0].txtInterestRate.value = rate;
		if(rate == 0)
		{
			alert("The value entered for the interest rate is not a number!");
			return;
		}	
		
		//fill in the interest rates
		var item = document.getElementById("rate");
		item.innerHTML = rate + "%";
		
		/*item = document.getElementById("reducedRate");
		item.innerHTML = (rate - .25) + "%";*/
		
		var MonthlyIncome = GetNumber(document.forms[0].txtMonthlyIncome.value);
		
		var fixedPayment = GetIncomeSensitivePayment(newPrincipal,MonthlyIncome,rate,false);
		if(fixedPayment == 0) return;
		
		item = document.getElementById("fixedPayment");
		item.innerHTML = FormatAsCurrency(fixedPayment.toString());
		
		/*var reducedfixedPayment =  GetIncomeSensitivePayment(newPrincipal,MonthlyIncome,rate,true);
		item = document.getElementById("reducedfixedPayment");
		item.innerHTML = FormatAsCurrency(reducedfixedPayment.toString());*/
}

//******************************************************************************************************************************

function CheckPrincipal()
{
	var principal = document.forms[0].txtPrincipal.value;
	
	var value = 0;

	if(principal.length == 0 )
	{
		value = 0;
	}else{	
		value = GetNumber(principal);
	}
	
	if(value == 0)
	{
		alert("You have to enter the amount borrowed first!");
		document.forms[0].txtPrincipal.focus();
	}
}

//*******************************************************************************************************************************

function FillInitialData()
{
	//get the url to pull the querysting elements out of it
	var theUrl = new String(document.location);
	
	var startIndex = theUrl.indexOf("?");
	var endIndex = theUrl.indexOf("&");

	var queryStringValue = theUrl.substring(startIndex+1);

	if(startIndex != -1)
	{
		var newQueryString = queryStringValue.substring(queryStringValue.indexOf("&")+1);
		var balance = queryStringValue.substring(0,queryStringValue.indexOf("&"));
		document.forms[0].txtPrincipal.value = balance.substring(balance.indexOf("=")+1);

		document.forms[0].txtInterestRate.value = newQueryString.substring(newQueryString.indexOf("=")+1);//,newQueryString.indexOf("&"));
		
		/*newQueryString = newQueryString.substring(newQueryString.indexOf("&")+1);
		
		document.forms[0].txtType.value = newQueryString.substring(newQueryString.indexOf("=")+1);*/

		if(theUrl.indexOf("sensitive") == -1)
		{
			ChangeTerms();
		}
	}
	
	
}

//***********************************************************************************************************

function ChangeTerms()
{
	var principal =  document.forms[0].txtPrincipal.value;
	
	//call function to make sure that the princpal is a number
	principal = GetNumber(principal);
	
	//if it's not a number or 0 then let the user know and exit the fcn
	if(principal == 0 )
	{
		alert("The amount borrowed entered is not a valid amount!");
		return;
	}else{
		//determine how many months to put in the combo box based upon the principal
		if(document.forms[0].cboTerm != null){
		
		
		if(principal < 7500)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			
		}else if(principal >= 7500 && principal < 10000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
		}else if(principal >= 10000 && principal < 20000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
		}else if(principal >= 20000 && principal < 40000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
			newOption = new Option("240 months","240");
			document.forms[0].cboTerm.options[4] = newOption;
		}else if(principal >= 40000 && principal < 60000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
			newOption = new Option("240 months","240");
			document.forms[0].cboTerm.options[4] = newOption;
			newOption = new Option("300 months","300");
			document.forms[0].cboTerm.options[5] = newOption;
		}else if(principal >= 60000)
		{
			var newOption = new Option("120 months","120");
			document.forms[0].cboTerm.options[1] = newOption;
			newOption = new Option("144 months","144");
			document.forms[0].cboTerm.options[2] = newOption;
			newOption = new Option("180 months","180");
			document.forms[0].cboTerm.options[3] = newOption;
			newOption = new Option("240 months","240");
			document.forms[0].cboTerm.options[4] = newOption;
			newOption = new Option("300 months","300");
			document.forms[0].cboTerm.options[5] = newOption;
			newOption = new Option("360 months","360");
			document.forms[0].cboTerm.options[6]= newOption;
		}
		}
	}
	
	
}

