function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign

	var zeros = "";
	var scratch = tmpNum * Math.pow(10, decimalNum);
	scratch = Math.round(scratch);
// return scratch;
	for(var i = 1; i <= decimalNum; i++) {
		if((scratch % (Math.pow(10, i))) == 0) { zeros = zeros + "0"; }
		else break;
	}
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr + zeros;		// Return our formatted string!
}


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

function calc_mortgage() {
	result.style.display='';

  var mi = document.forms.calc.IR.value / 1200;
  var base = 1;
  var mbase = 1 + mi;

  for (i=0; i < document.forms.calc.YR.value * 12; i++)   {
    base = base * mbase
  }

  var mp = floor(document.forms.calc.LA.value * mi / ( 1 - (1/base)));
  var tp = mp * 12 * document.forms.calc.YR.value;
  var ti = tp - document.forms.calc.LA.value;

  document.forms.calc.MP.value = "$" + FormatNumber(mp, 2, true, false, true);
  document.forms.calc.TP.value = "$" + FormatNumber(tp, 2, true, false, true);
  document.forms.calc.TI.value = "$" + FormatNumber(ti, 2, true, false, true);
//  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);
}




// Advanced Calculator Stuff
// From The JavaScript Source!! http://javascript.internet.com
// Original:  Denis Makarov (info@binarythings.com)


var Result=0, Operator=0, Second=0, Ready=0, Done=1, Complete=0, Integer, CurrentValue;

function reset(value)

{

	document.calculator.LED.value = value;

	Result = 0, Operator = 0, Second = 0, Ready = 0; Done = 1; Complete = 0;

}

function SetValue(NewValue)

{

	Integer = 1;

	if(Second || Done)

	{

		Second = 0;

		Done = 0;

		CurrentValue = NewValue;

	}

	for(var i=0; i<CurrentValue.length; i++)

		if (CurrentValue[i]=='.')

			Integer=0;

}

function Click(Caption)

{

	CurrentValue = document.calculator.LED.value;

	if(Caption=='.')

	{

		SetValue('0');

		if(Integer)

		{

			CurrentValue += Caption;

			document.calculator.LED.value = CurrentValue;

			Complete = 0;

		}

	}

	if (Caption.length == 1 && Caption>='0' && Caption<='9')

	{

		SetValue('');

		if(CurrentValue=='0')

			CurrentValue='';

		CurrentValue += Caption;

		document.calculator.LED.value = CurrentValue;

		Complete = 1;

	}

	if (Caption=='pi')

	{

		CurrentValue = Math.PI;

		document.calculator.LED.value = CurrentValue;

		Complete = 1;

	}

	if (Caption=='e')

	{

		CurrentValue = Math.E;

		document.calculator.LED.value = CurrentValue;

		Complete = 1;

	}

	if(Caption=='-' || Caption=='+' || Caption=='/' || Caption=='*' || Caption=='^')

	{

		if(Second)

			Operator = Caption

		else

		{

			if(!Ready)

			{

				Operator = Caption;

				Result = CurrentValue;

				Ready=1;

			} else

			{

				if (Operator=='^')

					Result = Math.pow(Result, CurrentValue);

				else

					Result = eval(Result + Operator + CurrentValue);

				Operator = Caption; document.calculator.LED.value = Result;

			} Complete=0; Second = 1;

		}

	}

	if(Caption=='1/x' )

	{

		Result = eval('1/' + CurrentValue) ; reset(Result);

	}

	if(Caption=='sqrt')

	{

		Result = Math.sqrt(CurrentValue);

		reset(Result);

	}

	if(Caption=='exp' )

	{

		Result = Math.exp(CurrentValue);

		reset(Result);

	}

	if(Caption=='log' )

	{

		Result = Math.log(CurrentValue) / Math.LN10;

		reset(Result);

	}

	if(Caption=='ln' )

	{

		Result = Math.log(CurrentValue);

		reset(Result);

	}

	if(Caption=='sin' )

	{

		Result = CurrentValue;

		if (document.calculator.angle[0].checked)

			Result = Result * Math.PI / 180;

		if (document.calculator.angle[2].checked)

			Result = Result * Math.PI / 200;

		Result = Math.sin(Result);

		reset(Result);

	}

	if(Caption=='cos' )

	{

		Result = CurrentValue;

		if (document.calculator.angle[0].checked)

			Result = Result * Math.PI / 180;

		if (document.calculator.angle[2].checked)

			Result = Result * Math.PI / 200;

		Result = Math.cos(Result);

		reset(Result);

	}

	if(Caption=='tan' )

	{

		Result = CurrentValue;

		if (document.calculator.angle[0].checked)

			Result = Result * Math.PI / 180;

		if (document.calculator.angle[2].checked)

			Result = Result * Math.PI / 200;

		Result = Math.tan(Result);

		reset(Result);

	}

	if(Caption=='asin' )

	{

		Result = Math.asin(CurrentValue);

		if (document.calculator.angle[0].checked)

			Result = Result * 180 / Math.PI;

		if (document.calculator.angle[2].checked)

			Result = Result * 200 / Math.PI;

		reset(Result);

	}

	if(Caption=='acos' )

	{

		Result = Math.acos(CurrentValue);

		if (document.calculator.angle[0].checked)

			Result = Result * 180 / Math.PI;

		if (document.calculator.angle[2].checked)

			Result = Result * 200 / Math.PI;

		reset(Result);

	}

	if(Caption=='atan' )

	{

		Result = Math.atan(CurrentValue);

		if (document.calculator.angle[0].checked)

			Result = Result * 180 / Math.PI;

		if (document.calculator.angle[2].checked)

			Result = Result * 200 / Math.PI;

		reset(Result);

	}

	if(Caption=='sinh' )

	{

		Result = Math.exp(CurrentValue);

		Result = (Result - 1 / Result) / 2;

		reset(Result);

	}

	if(Caption=='cosh' )

	{

		Result = Math.exp(CurrentValue);

		Result = (Result + 1 / Result) / 2;

		reset(Result);

	}

	if(Caption=='tanh' )

	{

		Result = Math.exp(CurrentValue);

		Result = (Result - 1 / Result) / (Result + 1 / Result);

		reset(Result);

	}

	if(Caption=='asinh' )

	{

	//sign(x) * log(|x| + sqrt(x*x+1))

	//	alert(CurrentValue + Math.sqrt(CurrentValue * CurrentValue + 1));

	//	Result = Math.log(CurrentValue + Math.sqrt(CurrentValue * CurrentValue + 1));

		Result = CurrentValue / Math.abs(CurrentValue) * Math.log(Math.abs(CurrentValue) + Math.sqrt(CurrentValue * CurrentValue + 1));

		reset(Result);

	}

	if(Caption=='acosh' )

	{

	//2 log  (sqrt((x+1)/2) + sqrt((x-1)/2)) 

		Result = 2 * Math.log(Math.sqrt((CurrentValue + 1) / 2) + Math.sqrt((CurrentValue - 1) / 2));

		reset(Result);

	}

	if(Caption=='atanh' )

	{

	//0.5*log((x-1)/(x+1))

		Result = Math.log((CurrentValue - 1) / (CurrentValue + 1)) / 2;

		reset(Result);

	}

	if(Caption=='+/-')

		document.calculator.LED.value = eval(-CurrentValue);

	if(Caption=='=' && Complete && Operator!='0')

	{

		if (Operator=='^')

		{

			Result = Math.pow(Result, CurrentValue);

			reset(Result);

		} else

			reset(eval(Result + Operator + CurrentValue));

	}

	if (Caption=='C')

		reset(0);

	if(document.calculator.LED.value[0] == '.')

		document.calculator.LED.value = '0' + document.calculator.LED.value;

}

function calc_savings() { }
// fv = Pi^nt+(D-Di^(nt+1))/(1-i)
// $P = Principal
// $i = interest (1 <= i <= 2)
// $n = annual periods (n = 12; monthly compounding)
// $t = time (years)
// $D = monthly Deposit
//
// Given: n = 12
// Prequired from user: P, i, D
// Variable: t
// Solve fv(t)
//
// $nt = $n * $t
// $p = POW($i, $nt)
// $principal = $P + $p
//
// $d = POW($i, $nt+1)
// $top = $D - $D * $d
// $bottom = 1 - $i
//
// $fv = $principal + $top/$bottom

function showTable(tn) {
	table[tn].style.display = "";
	hide[tn].style.display = "";
	show[tn].style.display = "none";
	backtotop[tn].style.display = "";
	tablep[tn].style.display = "";
}

function hideTable(tn) {
	table[tn].style.display = "none";
	hide[tn].style.display = "none";
	show[tn].style.display = "";
	backtotop[tn].style.display = "none";
	tablep[tn].style.display = "none";
}
