
var refsite="";

function activate(){    //used to initialize flags
	gflg=0;				//flag for taxes
	sflg=0;				//flag for savings
//	alert (gflg);
//	alert (sflg);

	saveRefSite();
   choose_topper();
}

//Parse CallingSite's URL and save the value of site to var refsite
function saveRefSite(){
	var sitebegin = document.location.href.toLowerCase().indexOf("site=");
	var siteend=-1;
	if (sitebegin > -1)	{
		refsite = document.location.href.substring(sitebegin+5);
		siteend = refsite.indexOf("?");
		if (siteend > -1)	{
			refsite = refsite.substring(0,siteend);
		}
	}
	else {
		refsite = "eqf";
	}
	
//	alert(document.location+"\n"+sitebegin+"\n"+siteend+"\n"+refsite);
}

function choose_topper(){
   var nslp_image="pics/budget_calc_nslp.gif";
   var eqf_image="pics/bdgtCAL_top.jpg";
   var EQF_cpwrite="pics/EQF_copywrite.gif";
   var NSLP_cpwrite="pics/NSLP_copywrite.gif";

   if (refsite == "nslp")	{
	   imgs=document.getElementsByTagName("img");
	   imgs[0].src=nslp_image;
	   imgs[1].src=NSLP_cpwrite;
	   //document.images("company_logo").src=nslp_image;
	   //document.images("Copywrite").src=NSLP_cpwrite;
	   document.form1.sRefsite.value="NSLP";
   }
   else{
      addFavIcon();
	   imgs=document.getElementsByTagName("img");
	   imgs[0].src=eqf_image;
	   imgs[1].src=EQF_cpwrite;
	   //This is not supported by FireFox  dw 4/9/2007
	   //document.images("company_logo").src=eqf_image;
	   //document.images("Copywrite").src=EQF_cpwrite;
   }
};

function addFavIcon() {
   var headtg = document.getElementsByTagName('head')[0];
   if (!headtg) {
      return;
   }
   var linktg = document.createElement('link');
   linktg.rel = 'shortcut icon';
   linktg.href = 'http://www.educationquest.org/images/favicon_EQF.ico';
   linktg.type = 'image/x-icon'
   headtg.appendChild(linktg);
}

function compute(f, flg){
	f.grossincome.value = parseMoney(f.grossincome.value)
	f.taxes.value = parseMoney(f.taxes.value)
	f.netincome.value = parseMoney(f.netincome.value)
	f.takehome.value = parseMoney(f.takehome.value)
	f.stloan.value = parseMoney(f.stloan.value)
	f.rent.value = parseMoney(f.rent.value)
	f.utilities.value = parseMoney(f.utilities.value)
	f.food.value = parseMoney(f.food.value)
	f.childcare.value = parseMoney(f.childcare.value)
	f.savings.value = parseMoney(f.savings.value)
	f.carpayment.value = parseMoney(f.carpayment.value)
	f.carinsurance.value = parseMoney(f.carinsurance.value)
	f.gas.value = parseMoney(f.gas.value)
	f.clothes.value = parseMoney(f.clothes.value)
	f.recreation.value = parseMoney(f.recreation.value)
	f.misc.value = parseMoney(f.misc.value)
	//alert (flg);
	
	if (flg==1){    //this is the flag used to determine if it is the taxes field that has been changed.
		gflg = 1	//if the taxes field changes the flag needs to change to 1 indicating this field no longer auto calcs. 
	}
	
	if (flg==2){	//this is the flag used to determine if it is the savings field that has been changed.
		sflg=1		//if the savings field changes the flag needs to change to 1 indicating this field no longer auto calcs.
	}
	//alert (gflg);
	//alert (flg);
	//alert (sflg);
	
	if ((flg == 0) && (gflg != 1)){   //If not the taxes field that is changed and the tax field has never been manually changed.
	f.taxes.value = (f.grossincome.value * .25)    //auto calculates the taxes field.
	f.taxes.value = Math.round(f.taxes.value)
	} 
		
	f.netincome.value = f.grossincome.value - f.taxes.value
	f.takehome.value = ((f.netincome.value)/12)
	f.takehome.value = Math.round(f.takehome.value)
	if (sflg != 1){  //If not the savings field that is changed and the savings field has never been manually changed.
		//alert("tax is calculated");
		f.savings.value = (f.takehome.value * .10)  //auto calculates the savings field.
		f.savings.value = Math.round(f.savings.value)
	} 
	Total = (f.stloan.value - 0) + (f.rent.value - 0) + (f.utilities.value - 0) + (f.food.value - 0) + (f.childcare.value - 0)
	Total = Total + (f.savings.value - 0) + (f.carpayment.value - 0) + (f.carinsurance.value - 0)
	Total = Total + (f.gas.value - 0) + (f.clothes.value - 0) + (f.recreation.value - 0) + (f.misc.value - 0) 
	f.totexp.value = Total
	
	f.housing.value = (f.rent.value - 0) + (f.utilities.value - 0)
	f.trans.value = (f.carpayment.value - 0) + (f.carinsurance.value - 0) + (f.gas.value - 0)
	f.personal.value = (f.clothes.value - 0) + (f.recreation.value - 0) + (f.misc.value - 0)
	
		
	f.netincome.value = formatMoney(f.netincome.value)
	f.grossincome.value = formatMoney(f.grossincome.value)
	f.taxes.value = formatMoney(f.taxes.value)
	f.takehome.value = formatMoney(f.takehome.value)
	f.stloan.value = formatMoney(f.stloan.value)
	f.rent.value = formatMoney(f.rent.value)
	f.utilities.value = formatMoney(f.utilities.value)
	f.food.value = formatMoney(f.food.value)
	f.childcare.value = formatMoney(f.childcare.value)
	f.savings.value = formatMoney(f.savings.value)
	f.carpayment.value = formatMoney(f.carpayment.value)
	f.carinsurance.value = formatMoney(f.carinsurance.value)
	f.gas.value = formatMoney(f.gas.value)
	f.clothes.value = formatMoney(f.clothes.value)
	f.recreation.value = formatMoney(f.recreation.value) 
	f.misc.value = formatMoney(f.misc.value)
	f.totexp.value = formatMoney(f.totexp.value)
	f.housing.value = formatMoney(f.housing.value)
	f.trans.value = formatMoney(f.trans.value)
	f.personal.value = formatMoney(f.personal.value)
	//end hiding from old browsers -->

	}


function parseMoney(Amount){
	var fixAmt="";
	var i;
	var dollar = "$"
	for (i=0; i<Amount.length; i++)	{
		if (Amount.charAt(i)==',' || Amount.charAt(i)==dollar)
			fixAmt+="";
		else
			fixAmt+=Amount.charAt(i);
	}	
	return fixAmt;
}

//Fixes the amounts so that they contain 2 decimal places.
function formatDecimal(fixAmt){
	var decPart, intPart;
	
	if (isNaN(parseInt(fixAmt)))
		intPart = 0;
	else
		intPart = parseInt(fixAmt);	
	decPart = Math.round(100*(fixAmt - intPart));
	if (decPart == 0)
		decPart = "00";
	if (decPart > 0 && decPart < 10) 
		decPart = "0" + decPart;
	return decPart;
}

//Fixes the amounts so that they contain $ and , in the appropriate places.
function formatMoney(fixAmt){
	var Amount="";
	var i, lenA="", newAmt="";
	var dif=0;

	if (fixAmt > 0 )	{
		fixAmt = Math.round(100.0 * fixAmt)/100.0;
		lenA = "'"+parseInt(fixAmt)+"'";
		if (isNaN(parseInt(fixAmt)))
			lenA="0";
		decPart = formatDecimal(fixAmt);
		
		for (i=lenA.length; i<11; i++){
			fixAmt="0"+fixAmt;
			newAmt="'"+fixAmt+"'";
		}
		subA = newAmt.substr(1, 3);
		subB = newAmt.substr(4, 3);
		subC = newAmt.substr(7, 3);		
		Amount = subA+","+subB+","+subC+"."+decPart;
		for (i=0; i<11; i++)	{
			if (Amount.charAt(i)!='0' && Amount.charAt(i)!=',')
				break;
		}
		return "$" + Amount.substr(i,11);
	}
	else
	 	return "";
}
