function HideShow(id)
{
	var obj = document.getElementById(id);
	
	if(obj.style.display == "block")
	{
		obj.style.display = "none";
	}
	else
	{	
		obj.style.display = "block";
	}
}

// Created by Sharon Paine of Dynamic Web Coding 11/20/99
// http://www.dyn-web.com
// Inspired by loan payment calculator in ch 1 of
// JavaScript Definitive Guide by David Flanagan
// Modified 3/31/2000
 
// validation function
function isValid(entry, a, b) {
  if (isNaN(entry.value) || (entry.value==null) || (entry.value=="") || (entry.value < a) || (entry.value > b)) {
  alert("Please enter a monthly payment between " + a + " and " + b + ".")
	entry.focus()
  entry.select()
	return false
  }
	return true
}

// clear results fields when input values changed
function clearCalcs(form) {
	form.num_months.value = ""
	form.total_pay.value = ""
	form.total_int.value = ""
}

function calculateCCI(form) {
// send entries to validation function
// exit if not valid
	if (!isValid(form.balance, 0, 100000)) {  
		return false
	} else if (!isValid(form.interest, 0, 30)) {  
		return false
	} else {         
		var init_bal = eval(form.balance.value);
	}
	if (!isValid(form.mnth_pay, init_bal*.02, init_bal)) {
		return false
  } else {
	// variables used in calculation
	var cur_bal = init_bal;		// used in loop
	var interest = eval(form.interest.value/100);
	var mnth_pay = eval(form.mnth_pay.value);
	var fin_chg = 0;			// finance charge
	var num_mnths = 0;
	var tot_int = 0;
  }
	
  while (cur_bal > 0) {
    fin_chg = cur_bal*interest/12;
    cur_bal = cur_bal - mnth_pay + fin_chg;
    num_mnths++;
			if (num_mnths > 75) {
      	alert("We are interrupting this process to prevent a hang which may result with a very high balance, high interest rate and low monthly payment.\n\nTry entering a higher monthly payment amount." )
				form.mnth_pay.focus()
				form.mnth_pay.select()
				return
      }
    tot_int += fin_chg;
  }
	
// display result
	form.num_months.value = num_mnths;
	form.total_pay.value ="$" + round(init_bal + tot_int);
	form.total_int.value ="$" + round(tot_int);
   }

// round to 2 decimal places
function round(x) {
	return Math.round(x*100)/100;
}


//Mortgage calculator
function checkForZero(field) {
         if (field.value == 0 || field.value.length == 0) {
             alert ("This field can't be 0!");
             field.focus(); }
         else
             calculatePayment(field.form);
     }

     function calculateM() {
		calc(window.document.myForm);
     }

     function calc(form) {
         if (form.PV.value == 0 || form.PV.value.length == 0) {
             alert ("The Price field can't be 0!");
             form.PV.focus(); }
         else if (form.IR.value == 0 || form.IR.value.length == 0) {
             alert ("The Interest Rate field can't be 0!");
             form.IR.focus(); }
         else if (form.YR.value == 0 || form.YR.value.length == 0) {
             alert ("The Term field can't be 0!");
             form.YR.focus(); }
         else
             calculatePayment(form);
     }

     function calculatePayment(form) {
        princ = form.PV.value - form.DN.value;
        intRate = (form.IR.value/100) / 12;
        months = form.YR.value * 12;
        form.MP.value = Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
   		form.PI.value = princ;
 		form.MT.value = months;
     }
	 
//Amortization calculator
var num=0;
var amt=0;
var per=0;
var months=0;
var nls="";
var rr="\r";
var b="                               ";
var d="---------------------------------------------------------------";
var s="$";

function iA(){
 this.length=iA.arguments.length;
 for (var i=0;i<this.length;i++){
  this[i]=iA.arguments[i];
 }
}

var pwr=new iA(10);
var dec=new iA(16);
pwr[0]=1;
for (var i=0;i<9;i++){
 pwr[i+1]=pwr[i]*10;
}

dec[0]=.1;
dec[1]=.01;
dec[2]=.001;
dec[3]=.0001;
dec[4]=.00001;
dec[5]=.000001;
dec[6]=.0000001;
dec[7]=.00000001;
dec[8]=.000000001;
dec[9]=.0000000001;
dec[10]=.00000000001;
dec[11]=.000000000001;
dec[12]=.0000000000001;
dec[13]=.00000000000001;
dec[14]=.000000000000001;
dec[15]=.0000000000000001;

var ns="01234567890";
var cr="";
var str="";


function stn(){
 num=0;
 pos=str.indexOf(".");
 sfx="";
 if (pos>-1){
  sfx=str.substring(pos+1,str.length);
  str=str.substring(0,pos);
 }
 strl=str.length;
 for (var i=strl-1;i>-1;i--){
  cr=str.substring(i,i+1);
  pos=ns.indexOf(cr);
  num+=pos*pwr[strl-i-1];
 }

 if (sfx!=""&&sfx.length>dp){
   pos=ns.indexOf(sfx.charAt(dp+1));
   if (pos>4){
    pos=ns.indexOf(sfx.charAt(dp));
    sfx=sfx.substring(0,dp-1)+(pos+1);
   }
 }

 if (sfx!=""){
   for (var i=0;i<dp;i++){
    cr=sfx.substring(i,i+1);
    pos=ns.indexOf(cr);
    num+=pos*dec[i];
   }

/*    sfx="";
    sfx+=num;
    pos=sfx.indexOf(".");
    sfx=sfx.substring(pos+1,sfx.length);
    if (sfx.charAt(dp+1)=="9"){
     num+=dec[sfx.length-2];
     } */

  }
}



function testIt(form){

 str=document.isn.amt.value;
 fmtIt();
 bl=str.length+3;
 dp=2;
 stn();
 amt=num;
 str=document.isn.per.value;
 dp=4;
 stn();
 per=num;
 str=document.isn.months.value;
 dp=0;
 stn();
 months=num;
 if (months<1||months>999||per<.0001||per>99||amt<1||amt>pwr[9]){
  alrt();
 }
 else{
  document.isn.sched.value = "\r\r\r\r                   PLEASE WAIT WHILE WE CALCULATE ...";
  computeForm();
 }
}



function computeForm(){

 ls="";
 iin=1;
 i=per/12/100;
 fpv=0;

 for (var j=0;j<months;j++)
  iin=iin*(1+i);
  tmp=(amt*iin*i)/(iin-1);
  fpv+=tmp;
  fcalc=((months*fpv)-amt);
  prtSched();
 }


function prtSched(){
 fpv+=.01;
 str=""
 str+=fpv;
 fmtIt();
 dp=2;
 stn();
 fpv=num;
 pct=per/12/100;
 if (bl<14){
  bl=14;
 }

 ls="Amortization Schedule: "+document.isn.months.value
 +" months to repay "+s+document.isn.amt.value
 +" at "+document.isn.per.value+"%."+rr+d+rr
 +"Payment   Payment       Interest      Principal     Balance"
 +rr
 +"Number    Amount        Amount        Reduction     Due"
 +rr+d+rr;
 for (var j=0;j<months;j++){
  ntr=(amt*pct);
  str="";
  str+=ntr;
  fmtIt();
  ntr1=s+str;
  prp=fpv-ntr;
  if (prp>amt){
   prp=amt;
  }
  str="";
  str+=prp;
  fmtIt();
  prp1=s+str;
  amt-=prp;
  str="";
  str+=amt;
  fmtIt();
  amt1=s+str;
  if (fpv>(ntr+prp)){
   fpv=ntr+prp;
  }
  str="";
  str+=fpv;
  fmtIt();
  fpv1=s+str;
  str="";
  str+=(j+1)+".";
  ls+=b.substring(0,2)+str+b.substring(0,8-str.length)
  +fpv1+b.substring(0,14-fpv1.length)+ntr1
  +b.substring(0,14-ntr1.length)+prp1
  +b.substring(0,14-prp1.length)+amt1
  +rr;
 }

 document.isn.sched.value=ls+d+rr
 +"  * Interest calculated at 1/12th of annual interest rate on"
 +rr+"    the remaining principal amount. (Rounding errors "
 +"possible)"+rr+d+rr;

}

function fmtIt(){
 pos=str.indexOf(".");
 if (pos==0){
  str="0"+str;
  pos++;
 }
 if (pos<0){
  str+=".00";
  pos=str.indexOf(".");
 }
 str+="0000";
 str=str.substring(0,pos+4);
 cr=str.charAt(str.length-1);
 pos=ns.indexOf(cr);
 str=str.substring(0,str.length-1);
 if (pos>5){
  fmtIt1();
 }
}

function fmtIt1(){
  for (var k=str.length-1;k>-1;k--){
   cr=str.charAt(k);
   posn=ns.indexOf(cr);
   if (posn<0){
    k--;
   }
   else{
    str=str.substring(0,k)+ns.substring(posn+1,posn+2)
    +str.substring(k+1,str.length);
    if (posn!=9){
     k=-1;
    }
   }
  }
}


function alrt(){
 alert("You couldn't know. Months must be from"
 +" 1 to 999, Loan amount from 1 to "+pwr[9]
 +" and Interest from .001 to 99%");
}

//compound interest
function calculate(form) {
   var type = form.type.value;
   var varb = form.varb.value;
   var i = "" + form.i.value || "0";
   var c = ""+ form.c.value || "0";
   if (form.FV) {
     var FV = ""+ form.FV.value || "0";
     FV = format(FV);
     form.FV.value = FV;
   }
   if (form.A) {
     var A = ""+ form.A.value || "0";
     A = format(A);
     form.A.value = A;
   }
   if (form.p) {
     var p = ""+ form.p.value || "0";
     p = format(p)
     form.p.value = p;
   }
   if (form.n) {
     var n = ""+ form.n.value || "0";
     n = format(n);
     form.n.value = n;
   }
   i = format(i);
   form.i.value = i;
   c = format(c);
   form.c.value = c;

   n = n * c;

   if (i <= 0) {
      alert("Please enter a valid interest rate:");
      form.i.select();
      form.i.focus();
      return;
   }
   if (i.charAt(i.length - 1) == "%") {
      i = i.substring(0,i.length - 1);
      form.i.value = i;
   }
   if (i >= 1) {
      i = i / 100;
      //form.i.value = i;
   }
   i = "" + i;
   if (i.indexOf(".") != 0) {
      i = i.substring(i.indexOf("."),i.length);
   }

   if (c <= 0) {
      alert("Please enter a valid # of times your investment is compounded each year:");
      form.c.select();
      form.c.focus();
      return;
   }

   if (type == "annuity") {
      p = (p * 12) / c;
      if (varb == "FV") {
         if (form.p.value <= 0) {
            alert("Please enter a valid monthly investment:");
            form.p.select();
            form.p.focus();
            return;
         } else if (form.n.value <= 0) {
            alert("Please enter a valid # of years:");
            form.n.select();
            form.n.focus();
            return;
         }
         FV = (p * (Math.pow((1 + i/c),n) - 1)) / (i/c);
         FV = "$" + FV;
         FV = (FV.indexOf(".") >= 0) ? FV.substring(0, FV.indexOf(".",1) + 3) : FV + ".00";
         answer(form,FV);
         return;
      } else if (varb == "p") {
         if (form.FV.value <= 0) {
            alert("Please enter a valid future value:");
            form.FV.select();
            form.FV.focus();
            return;
         } else if (form.n.value <= 0) {
            alert("Please enter a valid # of years:");
            form.n.select();
            form.n.focus();
            return;
         }
         p = (FV * i) / ((Math.pow((1 + i/c),n) - 1) * c)
         p = (p * c) / 12;
         p = "$" + p;
         p = (p.indexOf(".") >= 0) ? p.substring(0, p.indexOf(".",1) + 3) : p + ".00";
         answer(form,p);
         return;
      } else if (varb == "n") {
         if (form.FV.value <= 0) {
            alert("Please enter a valid future value:");
            form.FV.select();
            form.FV.focus();
            return;
         } else if (form.p.value <= 0) {
            alert("Please enter a valid monthly investment:");
            form.p.select();
            form.p.focus();
            return;
         }
         n = (Math.log(FV * i + c * p) - Math.log(c * p)) / (Math.log(c + i) - Math.log(c));
         n = n / c;
         n = "" + n;
         n = (n.indexOf(".") >= 0) ? n.substring(0, n.indexOf(".",1) + 3) : n + ".00";
         answer(form,n);
         return;
      }
   } else {
      if (varb == "FV") {
         if (form.A.value <= 0) {
            alert("Please enter a valid investment:");
            form.A.select();
            form.A.focus();
            return;
         } else if (form.n.value <= 0) {
            alert("Please enter a valid # of years:");
            form.n.select();
            form.n.focus();
            return;
         }
         FV = A * Math.pow((1 + i/c),n);
         FV = "$" + FV;
         FV = (FV.indexOf(".") >= 0) ? FV.substring(0, FV.indexOf(".",1) + 3) : FV + ".00";
         answer(form,FV);
         return;
      } else if (varb == "A") {
         if (form.FV.value <= 0) {
            alert("Please enter a valid future value:");
            form.FV.select();
            form.FV.focus();
            return;
         } else if (form.n.value <= 0) {
            alert("Please enter a valid # of years:");
            form.n.select();
            form.n.focus();
            return;
         }
         A = FV / Math.pow((1 + i/c),n);
         A = "$" + A;
         A = (A.indexOf(".") >= 0) ? A.substring(0, A.indexOf(".",1) + 3) : A + ".00";
         answer(form,A);
         return;
      } else if (varb == "n") {
         if (form.FV.value <= 0) {
            alert("Please enter a valid future value:");
            form.FV.select();
            form.FV.focus();
            return;
         } else if (form.A.value <= 0) {
            alert("Please enter a valid investment:");
            form.A.select();
            form.A.focus();
            return;
         }
         n = (Math.log(FV) - Math.log(A)) / (Math.log(c + i) - Math.log(c));
         n = n / c;
         n = "" + n;
         n = (n.indexOf(".") >= 0) ? n.substring(0, n.indexOf(".",1) + 3) : n + ".00";
         answer(form,n);
         return;
      }
   }
}

function format(y) {
  while (y.indexOf(",") >= 1) {
    x = y.indexOf(",");
    y = y.substring(0, x) + y.substring(x + 1, y.length);
  }
  return y;
}

function answer(form,x) {
  form.answer.value = x;
  form.answer.focus();
  form.answer.select();
}	 
	 
var ajaxRequest

function getajaxRequest()
{
//var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				//alert("Your browser broke!");
				return false;
			}
		}
	}
}

function ajaxAddYahooForm(yahooid){
	getajaxRequest();
	var ajaxDisplay = document.getElementById('ajaxYahooDIV');
// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			//ajaxDisplay.style.height = "auto";
			fade('reviewform',1000,0,100);
		}
	}
	//var ip = document.getElementById('ip').value;
	
	ajaxDisplay.style.height = "260px";
	ajaxDisplay.innerHTML = '<div style="text-align:center;margin-top:30px;width:400px"><img src="http://www.creditoptions.com/img/ajax-loader.gif"></div>';

	var queryString = "yahooid=" + yahooid;
	ajaxRequest.open("POST", "/yahooform.asp", true);
	ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajaxRequest.send(queryString); 
}

function ajaxAddYahoo(){
	getajaxRequest();
	var ajaxDisplay = document.getElementById('ajaxYahooDIV');
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			ajaxDisplay.style.height = "auto";
			fade('newanswer',1000,0,100);
		}
	}
	var yahooid = document.getElementById('yahooid').value;
	var answer = document.getElementById('answer').value;
	answer = answer.replace(/ /g, "+");
	answer = answer.replace(/\n/g, "##");
	answer = answer.replace(/&/g, "^^");
	
	ajaxDisplay.innerHTML = '<div style="text-align:center;margin-top:30px;width:400px"><img src="http://www.creditoptions.com/img/ajax-loader.gif"></div>';

	var queryString = "yahooid=" + yahooid + "&answer=" + answer;
	ajaxRequest.open("POST", "/ajaxyahoo.asp", true);
	ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajaxRequest.send(queryString); 
}
	
function changeOpacity(el,opacity) {
	var image = document.getElementById(el);
	image.style.MozOpacity = (opacity / 100);
	image.style.filter = "alpha(opacity=" + opacity + ")";
	image.style.opacity = (opacity / 100);
	if (opacity < 1) {image.style.display = "none";}
	if (opacity > 0) {image.style.display = "block";}
}	
	
function fade(el,milli,start,end) {
	var fadeTime = Math.round(milli/100);
	var i = 0;  // Fade Timer
	// Fade in
	if(start < end) {
		//document.getElementById(el).style.display = "block";
		for(j = start; j <= end; j++) {
			// define the expression to be called in setTimeout()
			var expr = "changeOpacity('" + el + "'," + j + ")";
			var timeout = i * fadeTime;
			// setTimeout will call 'expr' after 'timeout' milliseconds
			setTimeout(expr,timeout);
			i++;
		}
	}
	// Fade out
	else if(start > end) {
		for(j = start; j >= end; j--) {
			var expr = "changeOpacity('" + el + "'," + j + ")";
			var timeout = i * fadeTime;
			setTimeout(expr,timeout);
			i++;
		}
	}
} 	 	