//This is a monthly version
//** written by Simon metz 7/2000
//** webzeit

function validate(f){
	goal =parseFloat(stripBag(f.goal.value));
	initial_amount =parseFloat(stripBag(f.initial_amount.value));
	rate =parseFloat(stripBag(f.rate.value));
	months =parseFloat((f.months.value));
	fedRate =parseFloat(f.fedRate.options[f.fedRate.selectedIndex].value*0.01);
	//alert("fedRate = " + fedRate);
	staRate = parseFloat(f.stRate.options[f.stRate.selectedIndex].value*0.01);

	if (isEmpty(initial_amount) || isEmpty(goal) ||isEmpty(rate) || isEmpty(months) || isEmpty(fedRate))
		{
		alert("Please enter all of the required numbers.");
		return;
		}
	else if(!isInteger(initial_amount) || !isInteger(goal) || !isInteger(rate) || !isInteger(months)){
		alert("Please enter numbers in all fields");
		return;
		}//close if
	else if(!isInteger(rate)){
			alert("Please enter a number for the interest rate");
		return;
		}//if
	else if(rate==0){
		alert("Please enter a valid interest rate");
		f.monthly.value="";
		}// if
	else if(rate>90){
		alert("Please enter a valid interest rate");
		f.monthly.value="";
		}// if
	else if(months==0){
		alert("Please enter a valid number of months")
		f.monthly.value="";
		}
	else{
		//alert("has passed all tests")
		getTotal(f);
		}
}//close function validate


function getTotal(f)
{
	var goal,initial_amount,addAmount,rate,years,months,fedRate,staRate,combRate,effectiveRate,total,counter,monthly;

	goal =parseFloat(stripBag(f.goal.value));
	initial_amount =parseFloat(stripBag(f.initial_amount.value));
	rate =parseFloat(f.rate.value);
	months =parseFloat(f.months.value);
	fedRate =parseFloat(f.fedRate.options[f.fedRate.selectedIndex].value*0.01);
	//alert("fedRate = " + fedRate);
	staRate = parseFloat(f.stRate.options[f.stRate.selectedIndex].value*0.01);
	//alert("staRate= "+staRate);
	combRate=fedRate+staRate;
	//alert("combRate= " + combRate);
	rate=rate/12;
	//alert("rate =" + rate);
	effectiveRate=(1+((rate*.01)*(1-combRate)));
	//alert("effectiveRate =" + effectiveRate);
	monthly=(goal-(initial_amount*(Math.pow(effectiveRate,months))))/annualValues(effectiveRate,months);
	//alert("monthly = "+monthly);
		if(!isInteger(monthly)){
			alert("Please use the Clear Values button and enter your information again");
			return;//f.monthly.value="";
			}
		else if(isNaN(monthly)){
			alert("Please use the clear button and re-enter your values");
			return;
			}
		else if(monthly<0){
			alert("Please lower either the interest rate and/or number of months.The combination you have entered will result in savings greater than your goal.");
			f.monthly.value="";
			}
		else{
			//alert("in last else");
			f.monthly.value = formatDollar(monthly);
			}//close else

clearAllFields(f);

}



//function to determine the factor of monthly additions

function annualValues(rate,months){

	var amount=0;
	var i;
	var counter=months + 1;

	for( i=1; i<counter; i++)
	{

		amount=amount+(Math.pow(rate,months-i));
	}
	return amount;
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//checks if element in string is a digit

function isDigit (c){
	return ((c >= "0") && (c <= "9"))
}

function isInteger (s){
	var i;

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}
// Removes all characters which appear in string bag from string s.

function stripBag (s){
	var i;
	//bag is all unacceptable punctuations
	var bag=",/()&^%$#@!:;|\{}[]=+_";
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++){
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}//end function




//function to force 2 decimal places

function formatDollar(cost)
{
	cost *= 100;
	cost = Math.round(cost);
	var dollars = Math.floor(cost / 100);
	var cents = cost % 100;

	var result = "$" + dollars + ".";
	if (cents < 10)
	{
		result += "0" + cents;
	}
	else
	{
		result += cents;
	}

	return result;
}

//function stores old field values and then clears fields before returning old values

function clearAllFields(f)
{
var oldGoal,oldInitial_amount,oldRate,months,oldFedRate,oldStRate;

oldGoal  = f.goal.value;
oldInitial_amount = f.initial_amount.value;
oldRate = f.rate.value;
oldMonths = f.months.value;
oldFedRate = f.fedRate.options[f.fedRate.selectedIndex].value;
oldStRate = f.stRate.options[f.stRate.selectedIndex].value;

f.goal.value = "";
f.initial_amount.value = "";
f.rate.value = "";
f.months.value = "";
f.fedRate.options[f.fedRate.selectedIndex].value = "";
f.stRate.options[f.stRate.selectedIndex].value = "";

f.goal.value = oldGoal;
f.initial_amount.value = oldInitial_amount;
f.rate.value = oldRate;
f.months.value = oldMonths;
f.fedRate.options[f.fedRate.selectedIndex].value = oldFedRate;
f.stRate.options[f.stRate.selectedIndex].value = oldStRate;
}




