// JavaScript Document


/*
	DRM - 01/20/07 
	This is the function for the College Cost Calculator.  
	Paul wrote this originally.
*/
function calculate(){
  //this would be a good place to start the actually calculation 
    var cost = 0;
    var singleYearCost = new Array(23);
    var tuitionTotal = getTuitionTotal();
		//document.writeln(tuitionTotal);
		var rate = new Number(document.getElementById("rate").value);
    var years = new Number(document.getElementById("years").value);
    var untilSchoolEnds = (19 + years) - new Number(document.getElementById("age").value);
    
		/* 
			loop through the years setting setting cost for the current year
			and then multiplying the yearly cost by the college cost inflation rate
		*/
		var i = 0;
    for(i=0;i<untilSchoolEnds;i++){
        singleYearCost[i] = (tuitionTotal);    			// save cost for current year
				tuitionTotal = tuitionTotal * (1 + rate);  	// increase cost for next year
    }
    
		// add up all the singleYearCosta
		for(i=1;i<=years;i++){
      cost = cost + singleYearCost[untilSchoolEnds - i] 
    }
    
		// round and format result
		cost = Math.round(cost); 
		var formattedCost = formatCurrency(cost);		
		
		document.getElementById("Cost").value = formattedCost;
  }
	
function getTuitionTotal()
{
	// initialize multidimentional array for college costs
	var arraySize = 4
	var collegeCostArray = new Array(arraySize);
	for (i=0; i < arraySize; i++) { collegeCostArray[i]=new Array(2) } ;
	//collegeCostArray[0] = [2272, 8571];
	//collegeCostArray[1] = [5836, 12796];
	//collegeCostArray[2] = [15783, 22743];
	//collegeCostArray[3] = [22218, 30367];

  //As of August 19, 2010::Tuition, Tuition+Room&Board
  collegeCostArray[0] = [2544, 10737]; //In State Community
	collegeCostArray[1] = [7020, 15213]; //In State Public
	collegeCostArray[2] = [18548, 26741]; //Out of State Public
	collegeCostArray[3] = [26273, 35636]; //Private
	
	var collegeValue = new Number(document.getElementById("college").value);
	var tuitionValue = new Number(document.getElementById("tuition").value);
	
	//document.writeln(collegeCostArray[collegeValue][tuitionValue]);
	return collegeCostArray[collegeValue][tuitionValue];
	
}
	
	
/*
	DRM - 02/01/07 
	This function formats currency for the college cost calculator
*/
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
