<!-- 
/*
   DynAPI Distribution
   Browser Class

   The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
*/
function Browser() {
	var b=navigator.appName;
    var agt=navigator.userAgent.toLowerCase();
	var ua=navigator.userAgent.toLowerCase();
	if (b=="Netscape") this.b="ns";
	else if ((b=="Opera") || (navigator.userAgent.indexOf("Opera")>0)) this.b = "opera";
	else if (b=="Microsoft Internet Explorer") this.b="ie";
	if (!b) alert('Unidentified browser./nThis browser is not supported');
	this.version=navigator.appVersion;
	this.v=parseInt(this.version);
	this.ns=(this.b=="ns" && this.v>=4);
	this.ns4=(this.b=="ns" && this.v==4);
    this.ns6=(this.b=="ns" && this.v==5 && (agt.indexOf("netscape6/6") != -1));
    this.ns6up=(this.b=="ns" && this.v>=5 && (agt.indexOf("netscape") != -1));
    this.ns7=(this.b=="ns" && this.v==5 && (agt.indexOf("netscape/7") != -1));
    this.ns7up=(this.ns6up && !this.ns6);
    this.ie=(this.b=="ie" && this.v>=4);
	this.ie4=(this.version.indexOf('MSIE 4')>0);
	this.ie5=(this.version.indexOf('MSIE 5')>0);
	this.ie55=(this.version.indexOf('MSIE 5.5')>0);
	this.ie6=(this.version.indexOf('MSIE 6')>0);
	this.opera=(this.b=="opera");
    this.aol=(agt.indexOf("aol") != -1);
    this.aol5=(agt.indexOf("aol 5") != -1);
    this.aol6=(agt.indexOf("aol 6") != -1);
    this.gecko = (agt.indexOf('gecko') != -1);
	this.win32=(ua.indexOf("win")>-1);
	this.mac=(ua.indexOf("mac")>-1);
}
is = new Browser();

function initialize() {
	if (!is.ie && !is.gecko && !is.ns6up){
		document.write("This calculator will not work with your browser.\n\nPlease use Internet Explorer v5.5+ or Netscape v6.2.3+");
		/*if (is.ns4) {document.layers["inputDiv"].visibility = "hide";}
		else {document.inputDiv.visibility = "hidden";}*/
	}
	else {
		resetChildrenDiv();
	}
}

window.onload=resetChildrenDiv;

function setChildren(position) {
	this.rank = position;
}

function setInputArray(formID){
	this.formID = formID;
	this.invalidKey = false;
	this.emptyKey = false;
}

var childArray = new Array();
childArray[0] = new setChildren("none");
childArray[1] = new setChildren("First");
childArray[2] = new setChildren("Second");
childArray[3] = new setChildren("Third");
childArray[4] = new setChildren("Fourth");
childArray[5] = new setChildren("Fifth");



function populateChildrenDiv(value){
	var divHTML = "<table cellpadding=2 cellspacing=0 width=100%>";
	for (i=1;i<=value;i++){
			tag = (value==1)?"":childArray[i].rank+" ";
			divHTML += "<tr><td align=left class=10>"+tag+"Child: </td><td align=right class=10><span id="+childArray[i].rank.toLowerCase()+"ChildYearsText>Years until college:</span> <select id="+childArray[i].rank.toLowerCase()+"ChildYears><option value=5>5</option><option value=10>10</option><option value=15>15</option><option value=20>20</option></select></td><td align=right class=10><span id="+childArray[i].rank.toLowerCase()+"ChildInputText>College cost:</span> $<input id="+childArray[i].rank.toLowerCase()+"ChildInput size=10></td></tr><tr><td colspan=3>&#160;</td></tr>"; 
			//alert(divHTML);
	}
	divHTML += "</table>"

	if (is.ie4) document.all["expensesChildrenDiv"].innerHTML = divHTML;
	else document.getElementById("expensesChildrenDiv").innerHTML = divHTML;
}

function clearChildrenDiv(){
	if (is.ie4) document.all["expensesChildrenDiv"].innerHTML = "";
	else document.getElementById("expensesChildrenDiv").innerHTML = "";
}

function resetChildrenDiv() {
	var form = document.forms["needsCalculator"];
	clearChildrenDiv();
	form.expensesChildren.selectedIndex = 0;
}

function changeChildrenDiv(value){
	(value==0)?clearChildrenDiv():populateChildrenDiv(value);
}

function checkNumeric(inputBoxID){
	var form = document.forms["needsCalculator"];
	var sTestValue = form[inputBoxID].value
	var re_Int = /^[0-9\,]+$/;
	return re_Int.test(sTestValue);
}

function isEmpty(inputBoxID,inputArray){
	if (is.ie4) document.all[inputBoxID+"Text"].style.color = "red";
	else document.getElementById(inputBoxID+"Text").style.color = "red";
	if (is.ie) document.all[inputBoxID].style.border = "red thin inset";
	else document.getElementById(inputBoxID).style.border = "red thin inset";
	
	for (i=0; i<inputArray.length; i++) {
		if (inputArray[i].formID == inputBoxID) {
			inputArray[i].emptyKey = true;
			break;
		}
	}
}

function isNotEmpty(inputBoxID,inputArray){
	if (is.ie4) document.all[inputBoxID+"Text"].style.color = "black";
	else document.getElementById(inputBoxID+"Text").style.color = "black";
	if (is.ie) document.all[inputBoxID].style.border = "#cccccc thin inset";
	else document.getElementById(inputBoxID).style.border = "#cccccc thin inset";

	for (i=0; i<inputArray.length; i++) {
		if (inputArray[i].formID == inputBoxID) {
			inputArray[i].emptyKey = false;
			break;
		}
	}
}

function isValid(inputBoxID,inputArray){
	if (is.ie4) document.all[inputBoxID+"Text"].style.color = "black";
	else document.getElementById(inputBoxID+"Text").style.color = "black";
	if (is.ie) document.all[inputBoxID].style.border = "#cccccc thin inset";
	else document.getElementById(inputBoxID).style.border = "#cccccc thin inset";

	for (i=0; i<inputArray.length; i++) {
		if (inputArray[i].formID == inputBoxID) {
			inputArray[i].invalidKey = false;
			break;
		}
	}
}

function isInvalid(inputBoxID,inputArray){
	if (is.ie4) document.all[inputBoxID+"Text"].style.color = "red";
	else document.getElementById(inputBoxID+"Text").style.color = "red";
	if (is.ie) document.all[inputBoxID].style.border = "red thin inset";
	else document.getElementById(inputBoxID).style.border = "red thin inset";

	for (i=0; i<inputArray.length; i++) {
		if (inputArray[i].formID == inputBoxID) {
			inputArray[i].invalidKey = true;
			break;
		}
	}
}

function checkInvalidKey(inputArray){
	for (i=0;i<inputArray.length;i++) {
		if (inputArray[i].invalidKey == true) {
			return false;
			break;
		}
	}
	return true;
}

function checkForEmpty(inputArray){
	for (i=0;i<inputArray.length;i++) {
		if (inputArray[i].emptyKey == true) {
			return true;
			break;
		}
	}
	return false;
}

function checkForInvalid(inputArray){
	var errorMessageNumeric = "Please review the form fields highlighted in red.\nThey must contain numerical values."
	checkInvalidKey(inputArray)?calculateLifeNeeds():alert(errorMessageNumeric);
}

function validateTextBoxes(){
	var form = document.forms["needsCalculator"];
	var inputArray = new Array();
	var errorMessageEmpty = "Please review the form fields highlighted in red.\nThese are required fields for this calculator."
	var j = 0
	for (i=0;i<form.length-2;i++){
		if ((form[i].type=="text")){
			inputArray[j] = new setInputArray(form[i].id);
			j++
		}	
	}
	(form["incomeAnnual"].value=="")?isEmpty(form["incomeAnnual"].id,inputArray):isNotEmpty(form["incomeAnnual"].id,inputArray);
	(form["incomeAvailable"].value=="")?isEmpty(form["incomeAvailable"].id,inputArray):isNotEmpty(form["incomeAvailable"].id,inputArray);
	(form["expensesBurial"].value=="")?isEmpty(form["expensesBurial"].id,inputArray):isNotEmpty(form["expensesBurial"].id,inputArray);
	(form["expensesDebts"].value=="")?isEmpty(form["expensesDebts"].id,inputArray):isNotEmpty(form["expensesDebts"].id,inputArray);

(form["expensesMisc"].value=="")?isEmpty(form["expensesMisc"].id,inputArray):isNotEmpty(form["expensesMisc"].id,inputArray);
	for (i=0;i<inputArray.length;i++){
		if (form[inputArray[i].formID].value !="") {
			checkNumeric(inputArray[i].formID)?isValid(inputArray[i].formID,inputArray):isInvalid(inputArray[i].formID,inputArray);
		}
	}	
	checkForEmpty(inputArray)?alert(errorMessageEmpty):checkForInvalid(inputArray);
}

function getIncomeYearsFactor(years){
 if (years=="10") return "8.1";
 else if (years=="15") return "11.1";
 else if (years=="20") return "13.6";
 else if (years=="25") return "15.6";
 else if (years=="30") return "17.3";
 else if (years=="35") return "18.7";
 else if (years=="40") return "20";
}

function getCollegeYearsFactor(years){
 if (years=="5") return "0.82";
 else if (years=="10") return "0.68";
 else if (years=="15") return "0.56";
 else if (years=="20") return "0.46";
}

function setCollegeYearsFactorArray(number,amount){
	this.factor = number;
	this.cost = amount
}

function toCurrency(anynum) {
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));
   workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));
   dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))

   while (pStr.length<3){pStr+="0"}
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval = dStr + pStr 
   if (anynum<0) {retval="("+retval+")"}
   return "$"+retval
}

function calculateLifeNeeds(){
	var form = document.forms["needsCalculator"];

	for (i=0;i<=form.length-2;i++){
		if((form[i].type=="text")&&(form[i].value!="")) {
			var inputString = form[i].value+"";
			inputString = inputString.replace(",", "");
			if (inputString!="") form[i].value = parseInt(inputString);
			else form[i].value = 0;
		} 
		if((form[i].type=="text")&&(form[i].value=="")) form[i].value = 0;
	}
	
	var incomeYearsFactor = getIncomeYearsFactor(form.incomeYearsNeeded[form.incomeYearsNeeded.selectedIndex].value);
	var incomeNeeds = Math.floor((parseInt(form["incomeAnnual"].value) - parseInt(form["incomeAvailable"].value)) * incomeYearsFactor);
	
	var collegeYearsFactorArray = new Array();
	
	var numberOfChildren = form.expensesChildren[form.expensesChildren.selectedIndex].value;
	for (i=1;i<=numberOfChildren;i++){
		var childYearsID = childArray[i].rank.toLowerCase()+"ChildYears";
		var collegeYearsFactor = getCollegeYearsFactor(form[childYearsID].value);
		var childCollegeCostID = childArray[i].rank.toLowerCase()+"ChildInput";
		var childCollegeCost = form[childCollegeCostID].value;
		
		collegeYearsFactorArray[i] = new setCollegeYearsFactorArray(collegeYearsFactor,childCollegeCost);
	}
	
	var expensesBurialValue = parseInt(form["expensesBurial"].value);
	var expensesMiscValue = parseInt(form["expensesMisc"].value);
	var expensesDebtValue = parseInt(form["expensesDebts"].value);
	
	var collegeExpenses = 0;
	
	for (i=1;i<collegeYearsFactorArray.length;i++){
		collegeExpenses += eval(collegeYearsFactorArray[i].cost * collegeYearsFactorArray[i].factor);
	}
	
	var totalExpenses = Math.floor(parseInt(collegeExpenses) + expensesBurialValue + expensesDebtValue + expensesMiscValue);
	var totalNeeds = incomeNeeds + totalExpenses;
	
	
	var assetsSavingsValue = parseInt(form["assetsSavings"].value);
	var assetsRetirementValue = parseInt(form["assetsRetirement"].value);
	var assetsLifeInsuranceValue = parseInt(form["assetsLifeInsurance"].value);
	var totalAssets = assetsSavingsValue + assetsRetirementValue + assetsLifeInsuranceValue;
	
	var totalLifeInsuranceNeeds = totalNeeds - totalAssets;

	incomeNeeds = toCurrency(incomeNeeds);
	totalExpenses = toCurrency(totalExpenses);
	totalNeeds = toCurrency(totalNeeds);
	totalAssets = toCurrency(totalAssets);
	totalLifeInsuranceNeeds = toCurrency(totalLifeInsuranceNeeds);
	
	populateResults("incomeNeedsResult",incomeNeeds);
	populateResults("totalExpensesResult",totalExpenses);
	populateResults("totalNeedsResult",totalNeeds);
	populateResults("totalAssetsResult",totalAssets);
	populateResults("totalLifeInsuranceNeedsResult",totalLifeInsuranceNeeds);
	
	numberOfKids = eval(collegeYearsFactorArray.length - 1);
	displayResult(numberOfKids);
}

function populateResults(resultsSpanID,resultsSpanMsg) {
	if (is.ie4) document.all[resultsSpanID].innerHTML = resultsSpanMsg;
	else document.getElementById(resultsSpanID).innerHTML = resultsSpanMsg;
}

function displayResult(kids){

	var yPosition = 860;	
	
	switch (kids) {
		case -1: 
			yPosition = 860;
			break;

		case 1:
			yPosition = 905;
			break;

		case 2:
			yPosition = 945;
			break;

		case 3:
			yPosition = 990;
			break;

		case 4:
			yPosition = 1035;
			break;

		case 5:
			yPosition = 1080;
			break;
	}

	//alert(yPosition);
	
	if (is.ie4) {
		document.all["resultsDiv"].style.display = "";
	} else {
		document.getElementById("resultsDiv").style.display = "";
	}
	window.scrollTo(0,yPosition)
}

function resetForm(){
	populateResults("incomeNeedsResult","");
	populateResults("totalExpensesResult","");
	populateResults("totalNeedsResult","");
	populateResults("totalAssetsResult","");
	populateResults("totalLifeInsuranceNeedsResult","");
	
	if (is.ie4) {
		document.all["resultsDiv"].style.display = "none";
	} else {
		document.getElementById("resultsDiv").style.display = "none";
	}
	document.forms["needsCalculator"].reset();
	window.scrollTo(0,140);
	resetChildrenDiv(); 
}

// -->