<!--

// Netscape or Microsoft Internet Explorer
function linkItTo(sentURL)
{
	
	if(navigator.appName == "Microsoft Internet Explorer"){
		sentURL = sentURL + ".htm#calculator";
	}else if(navigator.appName == "Netscape"){
		sentURL = sentURL + "_Netscape.htm#calculator";
	}else{
		sentURL = sentURL + ".htm#calculator";
	}
	window.location.href = sentURL;
}

//function to redirect the HTML page
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; 
	document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

//function to round to 2 number after the decimal
function decimal(num)
{
	return Math.round(num * 100)/100;
}
//===========================================================================================

//function to round to 6 number after the decimal
function decimal_6(num)
{
	return Math.round(num * 1000000)/1000000;
}
//===========================================================================================

//function to convert the decimal value into percentage with 2 numbers after the decimal
function percentage(num)
{
	return Math.round(num * 10000)/100;
}
//===========================================================================================


// function disallowing anything but numbers
function isNumeric(num)
{
	var numOK,numLen,numVal,decimalCount,numChar,newNum;
	numOK = true;
	numLen = num.length;
	var_num = num;
	numVal = "";
	decimalCount = 0;
	
	
	
	
	for(i=0; i<numLen; i++)
	{		
		numChar = var_num.charAt(i);
		
		// remove all commas
		if(numChar != ","){
			numVal = numVal + numChar;
		}
	}
	
	// if num is "-" return 0
	if(var_num != "-"){
	
		// checking validity of the number
		for(i=0; i<numLen; i++)
		{		
			numChar = numVal.charAt(i);
			
			// counting the decimal points
			if(numChar == ".")
			{
				decimalCount++;
			}
			
			// checking the count and alerting the event
			if(decimalCount > 1)
			{
				alert("You have too many decimal points [" + decimalCount + "]");
				return false;
				break;
			}
			
			// checking for illegal characters (only numbers and one decimal point allowed)
			if( !(numChar >= 0 && numChar <= 9) && numChar != "." || numChar == " ")
			{
				alert(num + " is not a number. Please reset your form field otherwise you will get erroneous results.");
				numOK = false;
				return false;
				break;
			}
			// checking for illegal characters (only numbers and one decimal point allowed)
			if(numChar == "-" && i != 0)
			{
				alert(num + " is not a number. Please reset your form field otherwise you will get erroneous results.");
				numOK = false;
				return false;
				break;
			}
			// eval the number 
			/*if(isNaN(numChar) )
			{
				
			}*/
			
		}//end of "checking validity of the number"
		
	}else{
		return 0;
	} // end of "if num is "-" return 0"
	
	
	if(numOK != false)
	{
		newNum = eval(numVal);	
		return newNum;
	}
	
}

// use for dollar formatting
function cash(num) 
{
	
	num = Math.round(num * 100)/100;
	num = "" + num;
	
	var newString = "";
	var charCount = 0;
	for (i=(num.length-1);i>=0;i--)
	{
		newString = num.charAt(i) + newString;
		charCount++;
		// reset the counter if a decimal point is present
		if(num.charAt(i) == ".")
		{
			charCount = 0;
		}
		// puts a comma at every 3 characters starting from the end of the number string
		// (if there's "-" negative signs, stop after the 2nd character of the string) OR (else, stop after the 1st)
		if((charCount == 3 && i > 1 && num.charAt(0) == "-") || (charCount == 3 && i > 0 && num.charAt(0) != "-"))
		{
			charCount = 0;
			newString = "," + newString;
		}
	}
	return newString;
}

// use for dollar formatting
function cashSign(num1, num2, num3, num4, num5) 
{
	//this is necessary when a string is sent with commas is sent. In javascript it seen as several values instead of one string 
	//This will work for number values containing up to 4 commas i.e. 999,999,999,999,999.99
	//basically, it removes the commas from the comma delimited string
	num = "" + num1;
	if(num2){
		num = num + numnum;
	}
	if(num3){
		num = num + qwe;
	}
	if(num4){
		num = num + qweqwe;
	}
	if(num5){
		num = num + qweqwe;
	}
	if(num.indexOf(","))
	{
		var tmp = "";
		for(i=0;i<num.length;i++)
		{
			//alert(i + "  "  + num.charAt(i));
			/**/
			if(num.charAt(i) != ",")
			{
				//alert(num.charAt(i));
				tmp = tmp + num.charAt(i);
			}
			
		}
		num = tmp;
	}
	// convert the string to a number
	num = eval(num);
	
	//round it off to 2 digits after the decimal point
	num = Math.round(num * 100)/100;
	num = "" + num;
	
	
	var newString = "";
	var charCount = 0;
	// starting form the last character, start putting in the commas at every 1000 multiple
	for (i=(num.length-1);i>=0;i--)
	{
		newString = num.charAt(i) + newString;
		charCount++;
		// reset the counter if a decimal point is present
		if(num.charAt(i) == ".")
		{
			// if there is only one number after the DOT, put an extra zero at the end
			if(charCount == 2){
				newString = newString + "0";
			}
			charCount = 0;
		}
		// puts a comma at every 3 characters starting from the end of the number string
		// (if there's "-" negative signs, stop after the 2nd character of the string) OR (else, stop after the 1st)
		if((charCount == 3 && i > 1 && num.charAt(0) == "-") || (charCount == 3 && i > 0 && num.charAt(0) != "-"))
		{
			charCount = 0;
			newString = "," + newString;
		}
	}
	// add the .00 to every price where no decimal point was found
	if(newString.indexOf(".") == -1){
		newString = newString + ".00";
	}
	// add the $ sign to the right (en francais)
	newString = "$" + newString;
	return newString;
}

//===========================================================================================

// for more information on the presentValue and futureValue functions see "PV_information.htm"
// 
function presentValue(rate,nper,pmt,fv,type)
{
	if (rate != 0)
	{    //(PMT*(1+rate*type)*(1-(1+rate)^NPER)-rate*FV)/(rate*(1+rate)^NPER)
		// pv = (pmt*(1+rate*type)* (1-Math.pow((1+rate),nper)) -rate*fv ) /(rate*Math.pow((1+rate),nper));
		//pv = (pmt*(1+rate*type)*((Math.pow((1+rate),nper)-1)/rate)+fv)/(Math.pow((1+rate),nper));
		
		pv = -(pmt*(1+rate*type)*((Math.pow((1+ rate),nper)-1)/rate)+fv)/(Math.pow((1+rate),nper))
		
	}else{
		pv = -((pmt*nper)-fv)
	}
	return pv;
}
//===========================================================================================

function futureValue(rate,nper,pmt,pv,type)
{
	if (rate != 0)
	{ 
		//fv = (pmt*(1+rate*type)*(1-Math.pow((1+ rate)^nper))/rate)-pv*Math.pow((1+rate),nper) 
		fv = -(pv*Math.pow((1+rate),nper)+pmt*(1+rate*type)*((Math.pow((1+rate),nper)-1)/rate));
	}else{
		fv = -((pmt*nper)-pv);
	}
	return fv;
}
//===========================================================================================



function numberPeriod(rate, pmt, pv, fv, type)
{
	if (rate != 0)
	{ 
		nper = (Math.log((pmt*(1+rate*type)-fv*rate)/(pmt*(1+rate*type)+pv*rate))/Math.log(10))/ (Math.log(1+rate)/Math.log(10)) ;
	//	NPER =     LOG10((PMT*(1+rate*type)-FV*rate)/(PMT*(1+rate*type)+PV*rate))/ LOG10(1+rate) 
	}else{
		nper = -1(fv+pv)/pmt;
	}
	return nper;
}
//===========================================================================================



function PayMenT(rate,nper,pv,fv,type)
{
	if (rate != 0)
	{ 
		// original microsoft EXCEL formula
		//PMT = (rate*(FV+PV*(1+ rate)^NPER))/((1+rate*type)*(1-(1+ rate)^NPER)) 
		//pmt = -(rate*(fv+pv*Math.pow((1+ rate),nper)))/((1+rate*type)*(Math.pow((1+rate),nper)-1)) //perry's version
		
		pmt = (rate*(fv+pv*Math.pow((1+ rate),nper)))/((1+rate*type)*(1-Math.pow((1+rate),nper))); // microsoft
		
	}else{
		pmt = -1*(fv+pv)/nper; 
	}
	return pmt;
}



function InterestRate(nper,pmt,pv)
{	
/*
Basically, you have to start the loop with a dummy value of i=0, and
then increase the value of i by increments of 1/10000, for example, until the
value of the FUNCTION called PV(NPER,PMT,i) is less than or equal to PV.
You then stop and report the i value.

Thus, at a value of i=0, the loop start with a very large value of
PV(NPER,PMT,i), and then, as the i value is increases the PV(.) function
goes down in value until it HITS the PV input. The loop then exits and
report the value of i as the answer to the RATE formula.

Basically, we are searching for the interest rate such that the present
value of the payments is exactly equal to the input value.*/

	var i,rate;
	i=0;
	rate=0;
	do
	{
		i++;
		rate = i/250000;
		//alert("rate = " + rate + " \n presentValue(rate,nper,pmt,0,0) = " + presentValue(rate,nper,pmt,0,0) + "\n pv = " + pv);
	}while(Math.abs(presentValue(rate,nper,pmt,0,0)) > Math.abs(pv));		
	return rate;
	alert(rate);
}


//===========================================================================================

function HLOOKUP(lookup_valueA,lookup_valueB)
{	
/*
Searches for a value in the top row of a table or an array of values, and then returns a value in the same column from a row you specify in the table or array. Use HLOOKUP when your comparison values are located in a row across the top of a table of data, and you want to look down a specified number of rows. Use VLOOKUP when your comparison values are located in a column to the left of the data you want to find.


Maintenance Costs:
the array represent the maintenance cost per [NUM] month period. 
NUM would be replace by the number enclosed by the brackets 
ex:   ar_MC[12]  represent a 12 month period, and the cost would be $248.00       
*/
	
	var message = "Warning!";
	if(lookup_valueA + lookup_valueB> 96)
		message = message +"\n    -You are exceeding the allowable Lease Term of 96 month!\n";
		
	var ar_MC = new Array();
	for(i=0;i<97;i++){
		ar_MC[i] = 0;
	}
	ar_MC[12] = 248;
	ar_MC[24] = 603;
	ar_MC[36] = 1104;
	ar_MC[48] = 1755;
	ar_MC[60] = 2566;
	ar_MC[72] = 3491;
	ar_MC[84] = 4471;
	ar_MC[96] = 5530;
	
	if(ar_MC[lookup_valueA] == 0)
		message = message +"\n    -Only multiple of 12 months is allowed. \n Please reset your input otherwise you might erroneous results!";
	
	if(message != "Warning!")	
		alert(message);
			
	return ar_MC[lookup_valueA];
}


//===========================================================================================

function arSUM(start_value,end_value)
{	
/**/
			
	var ar_yMC = new Array();
	
	ar_yMC[0] = 0;
	ar_yMC[1] = 248;
	ar_yMC[2] = 355;
	ar_yMC[3] = 501;
	ar_yMC[4] = 651;
	ar_yMC[5] = 811;
	ar_yMC[6] = 925;
	ar_yMC[7] = 980;
	ar_yMC[8] = 1059;
	ar_yMC[9] = 1042;
	ar_yMC[10] = 1045;
	ar_yMC[11] = 1063;
	ar_yMC[12] = 1052;
	ar_yMC[13] = 996;
	ar_yMC[14] = 1056;
	ar_yMC[15] = 936;
	
	var sumVal = 0;
	for(i=start_value;i<=end_value;i++){
		sumVal = sumVal + ar_yMC[i];
	}
			
	return sumVal;
}
//===========================================================================================



// New popup window

function MM_openBrWindow(theURL,winName,features) { 
	newWin = window.open(theURL,winName,features);
	newWin.focus();
}
//===========================================================================================

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
//===========================================================================================

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//===========================================================================================

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
//===========================================================================================

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//-->
