//******************end Global Variables*******************************
var myTbody = "myTbody";	//table name holding current semester data
var numOfRows = 5;			//num of current semester rows to start off with
var numOfCols = 6;			//num of current semester cols to start off with

//help contents
var tooltip = new Array(17);
tooltip[0]= document.createTextNode("(OPTIONAL) Enter Abreviated Course Title (Acct 215)");
tooltip[1]= document.createTextNode("Enter Amount of Units Received for Corresponding Course.");
tooltip[2]= document.createTextNode("Enter Letter Grade Received in Corresponding Course (A,A-,etc.)");
tooltip[3]= document.createTextNode("When checked, corresponding grade not figured into cumulative GPA.");
tooltip[4]= document.createTextNode("Check to determine GPA for a specific course or group of courses.");
tooltip[5]= document.createTextNode("Grade Points are calculated for you based on units and letter grade.");

tooltip[6]= document.createTextNode("Total Units: Total for the current semester.");
tooltip[7]= document.createTextNode("GPA: Total grade pts divided by total units for semester.");
tooltip[8]= document.createTextNode("Total Grade Points:  Total for the current semester.");	

tooltip[9]= document.createTextNode("Adds Row for an Extra Course to be Calculated.");
tooltip[10]=document.createTextNode("Deletes the Last Row from the Calculation Table");
tooltip[11]=document.createTextNode("Clears all Entered Data");
tooltip[12]=document.createTextNode("Clears Data Entered in this Row");

tooltip[13]= document.createTextNode("Total Units: ALL units including current semester.");
tooltip[14]= document.createTextNode("GPA: Cumaltive Grade Points divided by Cumulative Units");
tooltip[15]= document.createTextNode("Total Grade Points: ALL grade pts. including current semester.");

tooltip[16]= document.createTextNode("ALL units from previous semesters and any other institutions");
tooltip[17]= document.createTextNode("ALL grade points from previous semesters and any other institutions");
tooltip[18]= document.createTextNode("Converts units/points from a Quarter system into the Semester equivalence.");

//******************end Global Variables*******************************


function help(num){
	return tooltip[num];
}

function setupRows(num){
	if(navigator.appName == 'Netscape'){
		var rowFromBod=document.getElementById(myTbody); 
		rowFromBod.deleteRow(-1); 
		fresetAll();
	}else{
		clearAll()
	}; 
	var x=0; 
	while(x<num){
		newRowValue(); 
		x++;
	}	
}

function newRowValue(){
	
	switch(navigator.appName){
		case 'Netscape':
	
			var input = new Array(numOfCols);
				input[0] = document.createElement("input");
					input[0].setAttribute('type', 'text');
					input[0].setAttribute('size', '8');
					input[0].setAttribute('alt', 'Course Input');
				input[1] = document.createElement("input");
					input[1].setAttribute('type', 'text');
					input[1].setAttribute('size', '5');
					input[1].setAttribute('onKeyUp', 'fvalidGradeCheck(this, 1); updateGPA();');
					input[1].setAttribute('alt', 'Units Input');
				input[2] = document.createElement("input");
					input[2].setAttribute('type', 'text');
					input[2].setAttribute('size', '5');
					input[2].setAttribute('onKeyUp', 'fmodInput(this); updateGPA();');
					input[2].setAttribute('alt', 'Grade Input');
				input[3] = document.createElement("input");
					input[3].setAttribute('type', 'checkbox');
					input[3].setAttribute('onClick', "fcomputeGPA();");
					input[3].setAttribute('alt', 'Exclude from GPA');
				input[4] = document.createElement("input");
					input[4].setAttribute('type', 'button');
					input[4].setAttribute('value', '<-Reset->');
					input[4].setAttribute('onMouseOver', "raiseToolTip(this, event, 12);");
					input[4].setAttribute('onClick', "freset(this); updateGPA();");
					input[4].setAttribute('alt', 'Reset this row');
				input[5] = document.createElement("input");
					input[5].setAttribute('type', 'text');
					input[5].setAttribute('size', '5');
					input[5].setAttribute('readonly', 'true');
					input[5].setAttribute('alt', 'Computed Grade Pts');
			
			var div = new Array(numOfCols);
			var cell = new Array(numOfCols);
			var fform = document.createElement("form");
				fform.setAttribute("method", "post");
			var table = document.getElementById(myTbody);
			var fnewRow = table.insertRow(-1);
			var i = 0;
			
			while(i<=numOfRows){
				div[i]=document.createElement("div");
				div[i].setAttribute("align", "center");
				div[i].appendChild(input[i]);
				cell[i] = fnewRow.insertCell(-1);
					if(i==0){cell[i].setAttribute('class', 'optCell');}
					if(i==1 || i==2){cell[i].setAttribute('class', 'reqCell');}
					if(i==5){cell[i].setAttribute('class', 'calCell');}
				cell[i].appendChild(div[i]);
				i++;	
			}
			break;
		
		default:	//IE duplication of rows
			var pointer = document.getElementById("mainRow");
			var form = pointer.getElementsByTagName("form");
			var dupNode = form[0].cloneNode(true);
	
			dupNode.reset();	
	
			var tableBody = document.getElementById(myTbody);
			var newRow = tableBody.insertRow(-1);
			newRow.appendChild(dupNode);
	} //end switch
}

function removeRow(){
	
	switch(navigator.appName){
		case 'Netscape':
			var rowCount = 1;
			var body = document.getElementById(myTbody);
			var rows = body.getElementsByTagName("tr");
			while(rows[rowCount] != null){
				rowCount++;
			}
			if (rowCount > numOfRows) { 
				body.deleteRow(-1);
			}
		break;
		default:
			var i = 0;
			var formCount = 0;
			var tableBody;
			while (document.forms[i].name != "mainForm"){i++;}
			while (document.forms[i].name != "totals"){i++; formCount++;}
        		if (formCount > 6){
					tableBody = document.getElementById(myTbody);
					tableBody.deleteRow(-1);}
	} //end switch

	//now that the last row is gone update it.
	updateGPA();

}

//reset all fields to blank and unchecked
function resetAll(){
	if(navigator.appName == 'Netscape'){
		fresetAll();
	}else{
		clearAll();
	};	
}

//resets all fields for non-netscape browsers
function clearAll(){
	var i = 0;
	while (document.forms[i] != null){document.forms[i].reset(); i++;}				
}

//change input to upper case
function modInput(form, flag){
	form.grade.value = form.grade.value.toUpperCase();
	form.grade.value = form.grade.value.replace(" ", "");
	validGradeCheck(form, flag);
}

function validGradeCheck(form, flag){

	var gradePoint;
	switch(form.grade.value){
		case 'A+':
		case 'A':
			gradePoint = 4.0;
			computeRow(form, gradePoint);
			break;
		case 'A-':
			gradePoint = 3.70;
			computeRow(form, gradePoint);
			break;
		case 'B+':
			gradePoint = 3.30;
			computeRow(form, gradePoint);
			break;
		case 'B':
			gradePoint = 3.0;
			computeRow(form, gradePoint);
			break;
		case 'B-':
			gradePoint = 2.70;
			computeRow(form, gradePoint);
			break;
		case 'C+':
			gradePoint = 2.30;
			form.cnc.checked = false;
			computeRow(form, gradePoint);
			break;
		case 'C':
			gradePoint = 2.0;
			form.cnc.checked = false;
			computeRow(form, gradePoint);
			break;
		case 'C-':
			gradePoint = 1.70;
			form.cnc.checked = false;
			computeRow(form, gradePoint);
			break;
		case 'D+':
			gradePoint = 1.30;
			computeRow(form, gradePoint);
			break;
		case 'D':
			gradePoint = 1.0;
			computeRow(form, gradePoint);
			break;
		case 'D-':
			gradePoint = 0.70;
			computeRow(form, gradePoint);
			break;
		
		// equivelant to an 'F' and will be included in GPA computation
		case 'IC':
		case 'WU':
			form.cnc.checked = false;	//uncheck to include in calculations
		case 'F':
			gradePoint = 0.0;
			computeRow(form, gradePoint);
			break;
		
		// these don't effect the GPA computation
		case 'N':	//no such grade but it's to catch 'NC' input
		case 'NC':
		case 'CR':
		case 'R':
		case 'RP':
		case 'I':
		case 'W':
			form.cnc.checked = true;
			form.gpoints.value = '';
			break;
			
		//do nothing with these
		case '':
			break;
			
		default:
			if(flag == 0){
				alert("A(n) " + form.grade.value +" is not a possible grade."); 
				form.grade.value = "";
			}
	} //end switch
}

//compute grade point for current row
function computeRow(form, grade){
	if (form.units.value != ""){
		if(Number(form.units.value) >= 0 || Number(form.units.value) <= 30){
			form.gpoints.value = (Number(form.units.value) * Number(grade)).toFixed(2);
		}
	}
}

//call on correct GPA calculator depending on browser
function updateGPA(){
	if(navigator.appName == 'Netscape'){
		fcomputeGPA();
	}else{
		computeGPA();
	}
}

//GPA calculator for IE
function computeGPA(){
	var i = 0;
	var units = 0;
	var gradePoints = 0;
	var cumultiveUnits = 0;
	var cumultiveGradePoints = 0;

	document.cumulativeForm.cumulativeGradePoints.value = 0;
	document.cumulativeForm.cumulativeUnits.value = 0;

	//skip all forms (and track # of objects) until we reach mainForm 
	while(document.forms[i].name != "mainForm"){
		i++;
	}

	//calculate all objects in mainForm
	while(document.forms[i].name != "totals"){

		if(document.forms[i].gpoints.value != "" && document.forms[i].cnc.checked != true){
			units += Number(document.forms[i].units.value);
			gradePoints += Number(document.forms[i].gpoints.value);
			cumultiveUnits += Number(document.forms[i].units.value);
			cumultiveGradePoints += Number(document.forms[i].gpoints.value);
		}		
		i++;
	}
	
	//assign totals to "totals" form
	document.totals.units.value = units;
	document.totals.gpoints.value = (gradePoints).toFixed(2);
	
	//calculate GPA for totals form
	if(units != 0 || gradePoints != 0){
		document.totals.gpa.value = (gradePoints/units).toFixed(2); 
		//document.totals.gpa.value = Math.round(100 * Number(document.totals.gpa.value))/100;
	}else{
		document.totals.gpa.value = 0.00;
	};
	
	//to convert Quarter system units/points to Semester equivalence
	var convertRate = 1.0;
	if(document.cumulativeForm.convert.checked){
		convertRate = 0.667;		
	}
	
	//gather data for previous semester units and grade points
	if(document.cumulativeForm.prevUnits.value != '' && document.cumulativeForm.prevGradePoints.value != ''){
		var temp;
		var extraNum;
		
		temp = Number(document.cumulativeForm.prevUnits.value) * convertRate;
		extraNum = (temp % .01).toFixed(3);
		if(extraNum < 0.01) temp -= extraNum;
		cumultiveUnits += temp;
		
		temp = Number(document.cumulativeForm.prevGradePoints.value) * convertRate;
		extraNum = (temp % .01).toFixed(3);
		if(extraNum < 0.01) temp -= extraNum;
		cumultiveGradePoints += temp;
	}

	//display the data
	document.cumulativeForm.cumulativeGradePoints.value = (cumultiveGradePoints).toFixed(2);
	document.cumulativeForm.cumulativeUnits.value = (cumultiveUnits).toFixed(2);

	//calculate GPA for cumulative form
	if(cumultiveUnits != 0 || cumultiveGradePoints != 0){
		document.cumulativeForm.cumulativeGPA.value = (cumultiveGradePoints/cumultiveUnits).toFixed(2); 
		//document.cumulativeForm.cumulativeGPA.value = Math.round(100 * Number(document.cumulativeForm.cumulativeGPA.value))/100;
	}else{
		document.cumulativeForm.cumulativeGPA.value = 0
	};
	
}

/* Find the row number input is from*/
function getRowNum(input, cellNum){
	
	//mark input with "locator" value (-1)
	var tempVal = input.value;
	input.value = 'locator';
	
	var i = 0;
	var found = false;
	var body = document.getElementById(myTbody);
	var row = body.getElementsByTagName("tr");    //get all rows in table
	var field;

	//iterate through table and find "locator" value
	while(found != true){
		field = row[i].getElementsByTagName("input");
		if(field[cellNum].value == 'locator'){
			found = true;
		}else{
			i++;
		};		
	}

	//set original value back into input
	field[cellNum].value = tempVal;

//	input.value = tempVal;
	return(i);
}

/***************************NETSCAPE CODE**************************************************************************/
/***************************NETSCAPE CODE**************************************************************************/
/***************************NETSCAPE CODE**************************************************************************/
/***************************NETSCAPE CODE**************************************************************************/
/***************************NETSCAPE CODE**************************************************************************/

//change to upper case
function fmodInput(input){
	input.value = input.value.toUpperCase();
	input.value = input.value.replace(" ", "");
	fvalidGradeCheck(input, 0);
}

//resets all fields for netscape browsers
function fresetAll(){
	var body = document.getElementById(myTbody);
	var row = body.getElementsByTagName("tr");
	var field; 
	var rowi = 0;
	var i;

	while(row[rowi] != null){
		field = row[rowi].getElementsByTagName("input");
		i = 0;
		while(i < numOfCols){
			if(i<3 || i==5){
				field[i].value="";
			}else if(i==3){
				field[i].checked = false;
			}
			i++;
		}	
		rowi++;
	}	
	document.totals.reset();
	document.cumulativeForm.reset();

}

/* Check input type for valid grade input
*/
function fvalidGradeCheck(input, flag){

	if(flag == 0){
		i = 2
	}else{
		i = 1
	};
	
	var rowNum = getRowNum(input, i);	
	var body = document.getElementById(myTbody);
	var row = body.getElementsByTagName("tr");
	var field = row[rowNum].getElementsByTagName("input");
	var grade = field[2].value;
	var gradePoint;
	
	switch(grade){
		case 'A+':
		case 'A':
			gradePoint = 4.0;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'A-':
			gradePoint = 3.70;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'B+':
			gradePoint = 3.30;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'B':
			gradePoint = 3.0;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'B-':
			gradePoint = 2.70;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'C+':
			gradePoint = 2.30;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'C':
			gradePoint = 2.0;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'C-':
			gradePoint = 1.70;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'D+':
			gradePoint = 1.30;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'D':
			gradePoint = 1.0;
			fcomputeRow(gradePoint, rowNum);
			break;
		case 'D-':
			gradePoint = 0.70;
			fcomputeRow(gradePoint, rowNum);
			break;
		
		//equivelant to an 'F'
		case 'IC':
		case 'WU':
			field[3].checked = false;
		case 'F':
			gradePoint = 0.0;
			fcomputeRow(gradePoint, rowNum);
			break;

		//these don't count towards GPA calculations
		case 'N':	//no such grade but it's to catch 'NC' input
		case 'NC':
		case 'CR':
		case 'R':
		case 'RP':
		case 'I':
		case 'W':
			field[3].checked = true;
			field[5].value = ''
			break;	

		//do nothing
		case '':	
			break;
			
		default:
			if(flag == 0){
				alert("A(n) " + grade +" is not a possible grade."); 
				input.value = "";
			}
	} //end switch
}

//compute grade point for current row
function fcomputeRow(grade, rowNum){
	var body=document.getElementById(myTbody);
	var row=body.getElementsByTagName("tr");
	var input = row[rowNum].getElementsByTagName("input");
	if(input[1].value != "" && input[2].value != ""){
		input[5].value = (Number(input[1].value) * grade).toFixed(2);
	}
}

//reset current row
function freset(input){
	var rownum = getRowNum(input, 4);
	var body = document.getElementById(myTbody);
	var row = body.getElementsByTagName("tr");
	var field = row[rownum].getElementsByTagName("input");
	var i = 0;
	while(i<=numOfCols){
		if(i<=2 || i==5){
			field[i].value="";
		}
		if(i==3){
			field[i].checked = false;
		}
		i++;
	}	
}

//compute GPA for netscape
function fcomputeGPA(){
	var body = document.getElementById(myTbody);
	var row = body.getElementsByTagName("tr");
	var input; 
	var i = 0;
	var units = 0;
	var gradePoints = 0;
	var cumultiveUnits = 0;
	var cumultiveGradePoints = 0;

	while(row[i] != null){
		input = row[i].getElementsByTagName("input");
		if(input[5].value != "" && input[5].value != 'NaN' && input[3].checked != true){
			units+=Number(input[1].value);
			gradePoints+=Number(input[5].value);
			cumultiveUnits+=Number(input[1].value);
			cumultiveGradePoints+=Number(input[5].value);
		}
		i++;
	}
	
	//display total units and grade points for "totals" form
	document.totals.units.value = units;
	document.totals.gpoints.value = gradePoints.toFixed(2);
	
	//compute GPA for totals
	if(units != 0 || gradePoints != 0){
		document.totals.gpa.value = (gradePoints/units).toFixed(2); 
	}else{
		document.totals.gpa.value = 0.0
	};

	//to convert Quarter system units/points to Semester equivalence
	var convertRate = 1.0;
	if(document.cumulativeForm.convert.checked){
		convertRate = 0.667;		
	}

	//gather data for previous semester units and grade points
	if(document.cumulativeForm.prevUnits.value != '' && document.cumulativeForm.prevGradePoints.value != ''){
		var temp;
		var extraNum;
		
		temp = Number(document.cumulativeForm.prevUnits.value) * convertRate;
		extraNum = (temp % .01).toFixed(3);
		if(extraNum < 0.01) temp -= extraNum;
		cumultiveUnits += temp;
		
		temp = Number(document.cumulativeForm.prevGradePoints.value) * convertRate;
		extraNum = (temp % .01).toFixed(3);
		if(extraNum < 0.01) temp -= extraNum;
		cumultiveGradePoints += temp;		
	}

	//display units and grade points "cumulative" form
	document.cumulativeForm.cumulativeUnits.value = (cumultiveUnits).toFixed(2);	
	document.cumulativeForm.cumulativeGradePoints.value = (cumultiveGradePoints).toFixed(2);

	//calculate GPA for cumulative form
	if(cumultiveUnits != 0 || cumultiveGradePoints != 0){
		document.cumulativeForm.cumulativeGPA.value = (cumultiveGradePoints/cumultiveUnits).toFixed(2); 
		//document.cumulativeForm.cumulativeGPA.value = Math.round(100 * Number(document.cumulativeForm.cumulativeGPA.value))/100;
	}else{
		document.cumulativeForm.cumulativeGPA.value = 0
	};

}
