window.onload = init;

function init() {
	makeTable();
	addHandlers();
	showCumulative();
}

function addHandlers() {
	document.getElementById('more_classes').onclick = function() { addRow(); return false; };
	document.getElementById('fewer_classes').onclick = function() { removeRow(); return false; };
	document.getElementById('calculate').onclick = function() { calculateGPA(); return false; };
	document.getElementById('reset').onclick = function() { clearValues(); };
	document.getElementById('advanced').onclick = function() { showCumulative(); };
	document.getElementById('calculate_cumulative').onclick = function() { calculateCumulative(); return false; };
}

tableRows = 5;
function makeTable() {
	var rowTemplate = document.getElementById('row_template');
	rowTemplate.style.display = 'none';
	for (var i=1; i <= tableRows; i++) {
		printRow(i);	
	}
}

function printRow(x) {
	var newRow = document.getElementById('row_template').cloneNode(true);
	newRow.style.display = '';
	newRow.id = "row" + x;
	var classVar = newRow.getElementsByTagName('span')[0];
	classVar.id = "class" + x;
	classVar.firstChild.nodeValue = x;
	var creditsVar = newRow.getElementsByTagName('input')[0];
	creditsVar.id = "credits" + x;
	var gradeVar = newRow.getElementsByTagName('select')[0];
	gradeVar.id = "grade" + x;
	var classgpaVar = newRow.getElementsByTagName('td')[3];
	classgpaVar.id = "classgpa" + x;
	newRow = document.createElement('TR').appendChild(newRow);
	document.getElementById('add_classes').parentNode.insertBefore(newRow, document.getElementById('add_classes'));
}

function addRow() {
	tableRows++;
	printRow(tableRows);
}

function removeRow() {
	if (tableRows <= 1) return;
	tableRows--;
	var x = document.getElementById('add_classes');
	x.parentNode.removeChild(x.previousSibling);
}

function calculateGPA() {
	var totalPoints = 0;
	var totalCredits = 0;
	for (var i=1; i <= tableRows; i++) {
		if (document.getElementById('credits'+i).value > 0) {
			var classGPA = (document.getElementById('credits'+i).value)*(document.getElementById('grade'+i).value);
			document.getElementById('classgpa'+i).innerHTML = document.getElementById('grade'+i).value;
			totalPoints += classGPA;
			totalCredits += parseFloat(document.getElementById('credits'+i).value);
		}
	}
	var totalGPA = totalPoints / totalCredits;
	document.getElementById('total_credits').innerHTML = totalCredits;
	document.getElementById('total_gpa').innerHTML = (Math.round(totalGPA*100)/100).toFixed(2);
	return totalPoints;
	return totalCredits;
}

function clearValues() {
	var cells = document.getElementsByTagName('td');
	for (var i=0; i < cells.length; i++) {
		if (cells[i].id.match('classgpa')) cells[i].innerHTML = '';
		if (cells[i].className == "total") cells[i].innerHTML = '0';
	}
}

function showCumulative() {
	if (document.getElementById('advanced').checked == false) document.getElementById('cumulative').style.display = 'none';
	else document.getElementById('cumulative').style.display = '';
}

function calculateCumulative() {
	calculateGPA();
	var prevCredits = parseFloat(document.getElementById('previous_credits').value);
	var prevGPA = parseFloat(document.getElementById('previous_gpa').value);
	var prevPoints = prevCredits * prevGPA;
	var semesterCredits = parseFloat(document.getElementById('total_credits').firstChild.nodeValue);
	var semesterGPA = parseFloat(document.getElementById('total_gpa').firstChild.nodeValue);
	var semesterPoints = semesterCredits * semesterGPA;
	var newCredits = prevCredits + semesterCredits;
	var newPoints = prevPoints + semesterPoints;
	var newGPA = newPoints / newCredits;

	document.getElementById('cumulative_gpa').innerHTML = (Math.round(newGPA*100)/100).toFixed(2);
}