<!--
//------------------------------------------------
// Author: Tim Kies
//------------------------------------------------

/************************************************* 
 * Called when JSON is loaded. Main routine called
 * from the javascript that was loaded in the HTML.
 * Updates input boxes by calling information from
 * the loaded JSON file.
 * Param(object)
 *************************************************/       
function LoadJSON(json) {
	if(debug == 1) {alert('function LoadJSON called with param:\njson = ' +json)};

	var message = "function.js\n";		
	
	var residency = document.getElementById('residency').value;
	var nCredits  = document.getElementById('credits').value;
	var statis    = document.getElementById('status').value;
	var termIndex = document.getElementById('term').value;

	var rowIndex = nCredToRowIndex(nCredits, statis, termIndex);

	var tution = parseJSON(json, rowIndex, residency);
	message += "Tuition cost: $" + tution + "\n";
	
	var differential = parseJSON(json, rowIndex, "differential");
	message += "Differential tuition: $" + differential + "\n";
	
	var segregated_fees = parseFloat(parseJSON(json, rowIndex, "segregated-a")) + parseFloat(parseJSON(json, rowIndex, "segregated-b"));
	message += "Segregated Fees total: $" + segregated_fees + "\n";
	
	var tution_tot = parseJSON(json, rowIndex, residency + "-tot");
	message += "Total Wisconsin Fees: $" + tution_tot;
	
	if(debug > 0) {alert(message)};
	
if (isNaN(parseInt(tution))) 
{ 
 tution = 0; 
}

if (isNaN(parseInt(differential))) 
{ 
 differential = 0; 
} 

if (isNaN(parseInt(segregated_fees))) 
{ 
 segregated_fees = 0; 
} 

if (isNaN(parseInt(tution_tot))) 
{ 
 tution_tot = 0; 
}  

 
	document.getElementById('tution').innerHTML          = parseInt(tution);
	document.getElementById('differential').innerHTML    = parseInt(differential);
	document.getElementById('segregated_fees').innerHTML = parseInt(segregated_fees);
	document.getElementById('tution_tot').innerHTML      = parseInt(tution_tot);
	
	document.getElementById('userInOUTPUT').innerHTML = statis + ', ' + residency.toUpperCase() + ' res, ' + nCredits + ' credits, ' + param_json[parseInt(termIndex)][0];
	
	calculate(); //Keep this function call: I know that when the page first loads the calculate function is called twice.

}

/************************************************* 
 * Converts the credit load, status, and term into
 * a usable JSON row index.
 * Param(int, string, int)
 * Return int
 *************************************************/       
function nCredToRowIndex(nCredits, statis, termIndex) {
	if(debug == 1) {alert('function nCredToRowIndex called with param:\n nCredits = ' + nCredits + '\nStatus = ' + statis + '\ntermIndex = ' + termIndex)}

	var rowIndex;
	if(param_json[parseInt(termIndex)][1] == 'reg') {
		if(statis == 'Undergraduate') { /*Regular Semester*/
				if(nCredits < UNDGRAD_FULLTIME_REG_LOWER && nCredits > 0) {
						rowIndex = nCredits;
						if(debug == 1) {alert('Less than '+UNDGRAD_FULLTIME_REG_LOWER+' credits')}
						
				}	else if(nCredits >= UNDGRAD_FULLTIME_REG_LOWER && nCredits <= UNDGRAD_FULLTIME_REG_UPPER) {
						rowIndex = UNDGRAD_FULLTIME_REG_LOWER;
						if(debug == 1) {alert('Between '+UNDGRAD_FULLTIME_REG_LOWER+' and '+UNDGRAD_FULLTIME_REG_UPPER+' credits')}
						
				} else if(nCredits > UNDGRAD_FULLTIME_REG_UPPER && nCredits <= MAX_UNDGRAD_REG) {
						rowIndex = nCredits - (UNDGRAD_FULLTIME_REG_UPPER - UNDGRAD_FULLTIME_REG_LOWER);
						if(debug == 1) {alert('Over '+UNDGRAD_FULLTIME_REG_UPPER+' Credits')}
						
				}	else {
						alert('You must choose between 1 and '+MAX_UNDGRAD_REG+' credits');
				}
		} else if(statis == 'Graduate') {
				if(nCredits <= MAX_GRAD_REG && nCredits > 0) { 
					rowIndex = parseInt(nCredits) + UND_TO_GRAD_INDEX_SHIFT_REG;
				} else {
						alert('You must choose between 1 and '+MAX_GRAD_REG+' credits');
				}
		}
	} else if(param_json[parseInt(termIndex)][1] == 'wint') { /*Winterim*/
			if(statis == 'Undergraduate') {
				if(nCredits <= MAX_UNDGRAD_WINT && nCredits > 0) {
						rowIndex = nCredits;
						if(debug == 1) {alert('Less than '+MAX_UNDGRAD_WINT+' credits')}
						
				} else {
						alert('You must choose between 1 and '+MAX_UNDGRAD_WINT+' credits');
				}
		} else if(statis == 'Graduate') {
				if(nCredits <= MAX_GRAD_WINT && nCredits > 0) { 
					rowIndex = parseInt(nCredits) + UND_TO_GRAD_INDEX_SHIFT_WINT;
					
				} else {
						alert('You must choose between 1 and '+MAX_GRAD_WINT+' credits');
				}
		}
	}	else {
				alert('You must put in an integer for credit load');
		}

	return rowIndex;
}

/*************************************************
 * Updates the credit load determined by the
 * choosen status and term
 * Param(none)
 *************************************************/       
function updateCreditLoad() {
	var statis    = document.getElementById('status').value;
	var termIndex = document.getElementById('term').value;
	
	var nMAX;
	var fullTime = -1;
	
	if(param_json[parseInt(termIndex)][1] == 'reg') {
		if(statis == 'Undergraduate') { /*Regular Semester*/
			nMAX     = MAX_UNDGRAD_REG;
			fullTime = UNDGRAD_FULLTIME_REG_LOWER;
		} else if(statis == 'Graduate') {
				nMAX     = MAX_GRAD_REG;
				fullTime = GRAD_FULLTIME_REG;
		}
	} else if(param_json[parseInt(termIndex)][1] == 'wint') {
			if(statis == 'Undergraduate') { /*Winterim Session*/
				nMAX     = MAX_UNDGRAD_WINT;
				fullTime = UNDERGRAD_FULLTIME_WINT;
			} else if(statis == 'Graduate') {
					nMAX     = MAX_GRAD_WINT;
					fullTime = GRAD_FULLTIME_WINT;
			}
	} else if(param_json[parseInt(termIndex)][1] == 'summ') {
			if(statis == 'Undergraduate') { /*Summerim Session*/
				nMAX     = MAX_UNDGRAD_SUMM;
				fullTime = UNDERGRAD_FULLTIME_SUMM;
			} else if(statis == 'Graduate') {
					nMAX     = MAX_GRAD_SUMM;
					fullTime = GRAD_FULLTIME_SUMM;
			}
	}
	
	$("#credits").empty();
	
	for(var m = 1; m <= nMAX; m++) {
		if(fullTime != m) {
			$("#credits").append("<option value='"+m+"'>"+m+"</option>");
		} else {
			$("#credits").append("<option value='"+m+"' selected='selected'>"+m+"</option>");
		}
	}
}	

/*************************************************
 * Called when a value from the JSON needs to be
 * parsed.
 * Param(object, int, string)
 * Returns a single parsed float value
 *************************************************/       
function parseJSON(json, rowIndex, colName) {
  if(debug == 1) {alert('function parseJSON called with param:\njson = '+json+'\nrowIndex = '+rowIndex+'\ncolName = '+colName)}

  var entry = json.feed.entry[rowIndex];
  
  var colSearch = eval('/'+colName+'/'); //Major XSS problem
	var num   = "";
	var store = "";
	var start = entry["content"].$t.search(colSearch) + colName.length + 2;

	for(var j = start; store != ',' && j <= entry["content"].$t.length; j++) {
		num  += store;
		store = entry["content"].$t.charAt(j);
	}
	if(debug == 1) {alert(parseFloat(num) +" is a "+typeof(parseFloat(num)))}
	
	return num;
}  		      

/*************************************************
 * Creates a script tag in the page that loads in the 
 * JSON feed for the specified key/ID.
 * Parameter(string)
 *************************************************/
function getJSON(pointer) {
if(debug == 1) {alert('function getJSON called with param:\npointer = ' + pointer)}

  // Retrieve the JSON feed.
  var script = document.createElement('script');

  script.setAttribute('src', pointer + '?alt=json-in-script&callback=LoadJSON');
  script.setAttribute('class', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

if(debug == 1) {alert('fucntion.js loaded')}
-->