var xmlHttp;
var i = 0;

if(document.images)
{
	image =  new Image();
	image.src = '../images/loading.gif';
}

function loadcontent(url, post, value, type)
{
	if(value == 'content' && type == 'id')
	{
		document.getElementById('content').innerHTML = '<div id="black_background"></div><div id="loading_message" class="div_align_middle text_color_white">Loading, please wait...<br /><img src="'+image.src+'" alt="" /><br />Coded by Jeffrey.</div>';
		//document.getElementById('black').style.display = 'block'; //document.getElementById('content').innerHTML = '<br /><div class="black_overlay"></div><div id="loading"><img src="'+image.src+'" alt="" />  Loading, please wait...</div><br />';
		//document.getElementById('loading').style.display = 'block';
	}
	
	try
	{ // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{ // Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			if(type == 'id')	document.getElementById(value).innerHTML = xmlHttp.responseText;
			else	value.innerHTML = xmlHttp.responseText;
		}
	};
	
	if(!post)	xmlHttp.open('GET', url, true);
	else
	{
		xmlHttp.open('POST', url, true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	if(post)alert(post);
	xmlHttp.send(post);

}

function stateChanged()
{
	if(xmlHttp.readyState == 4)
	{
			document.getElementById(id).innerHTML = xmlHttp.responseText;
	}
}

function addgraderow(semester, year, department, course)
{
	if(department == 'null' || course == 'null')
	{
		alert('Department or Course did not specified.');
		return;
	}
	
	var form = document.createElement('form');
	var table = document.createElement('table');
	var tr = document.createElement('tr');
	var td_semester = document.createElement('td');
	var td_year = document.createElement('td');
	var td_department = document.createElement('td');
	//var input_department = document.createElement('input');
	var td_course = document.createElement('td');
	//var input_course = document.createElement('input');
	var td_sid = document.createElement('td');
	var input_sid = document.createElement('input');
	var td_name = document.createElement('td');
	var td_grade = document.createElement('td');
	var input_grade = document.createElement('input');
	var td_action = document.createElement('td');
	var input_submit = document.createElement('input');
	
	
	form.setAttribute('action', '');
	//form.setAttribute('method', 'post');
	table.setAttribute('class', 'table_align_middle');
	td_semester.setAttribute('class', 'table_column_100px');
	td_semester.innerHTML = semester;
	td_year.setAttribute('class', 'table_column_100px');
	td_year.innerHTML = year;
	td_department.setAttribute('class', 'table_column_100px');
	td_department.innerHTML = department;
	//input_department.setAttribute('type', 'hidden');//
	//input_department.setAttribute('name', 'department');//
	//input_department.setAttribute('value', department);//
	td_course.setAttribute('class', 'table_column_100px');
	td_course.innerHTML = course;
	//input_course.setAttribute('type', 'hidden');//
	//input_course.setAttribute('name', 'course');//
	//input_course.setAttribute('value', course);//
	td_sid.setAttribute('class', 'table_column_100px');
	input_sid.setAttribute('type', 'text');
	input_sid.setAttribute('name', 'sid');
	//input_sid.setAttribute('id', 'sid');//
	input_sid.setAttribute('class', 'width_100');
	input_sid.setAttribute('onkeyup', 'loadcontent(\'addtranscripts.php?sid=\'+this.value, null, \'name_\'+'+i+', \'id\')');
	td_name.setAttribute('id', 'name_'+i);
	td_name.setAttribute('class', 'table_column_100px');
	td_grade.setAttribute('class', 'table_column_50px');
	input_grade.setAttribute('type', 'text');
	input_grade.setAttribute('name', 'grade');
	//input_grade.setAttribute('id', 'grade');
	input_grade.setAttribute('class', 'width_100');
	td_action.setAttribute('id', 'action_'+i);
	td_action.setAttribute('class', 'table_column_100px');
	input_submit.setAttribute('type', 'button');
	//input_submit.setAttribute('name', 'submit');
	input_submit.setAttribute('value', 'Submit');
	//alert(department);
	input_submit.setAttribute('onclick', 'loadcontent(\'addtranscripts.php\', \'action=\'+this.value+\'&semester='+semester+'&year='+year+'&department='+department+'&course='+course+'&sid=\'+form.sid.value+\'&grade=\'+form.grade.value, \'action_\'+'+i+', \'id\')');
	//\'addtranscripts.php\'+\'submit=\'+this.value+\'&amp;department=\'+department+\'&amp;course=\'+course+\'&amp;sid=\'+form.sid.value+\'&amp;grade=\'+form.grade.value+\'action\'
	
	form.appendChild(table);
	table.appendChild(tr);
	tr.appendChild(td_semester);
	tr.appendChild(td_year);
	tr.appendChild(td_department);
	//td_department.appendChild(input_department);
	tr.appendChild(td_course);
	//td_course.appendChild(input_course);
	tr.appendChild(td_sid);
	td_sid.appendChild(input_sid);
	tr.appendChild(td_name);
	tr.appendChild(td_grade);
	td_grade.appendChild(input_grade);
	tr.appendChild(td_action);
	td_action.appendChild(input_submit);
	
	document.getElementById('graderow').appendChild(form);
	
	i++;
}

function addgrade2()
{
	var div = document.getElementById('grade').innerHTML;
	alert(div);
	document.getElementById('grade').innerHTML = div+'<form action=""><input type="text" /></form>';
}

function menu_show_hide(id)
{
	var element = document.getElementById(id);
	
	if(element.style.display == 'block')	element.style.display = 'none';
	else	element.style.display = 'block';
}
	
/*
function onreadyaction()
{	alert('hi');
	if(xmlHttp.readyState == 4)
	{
		alert('hi');
		//document.a.b.maincontent.innerHTML = xmlHttp.responseText;
	}
}
*/
