/* CREATES HTTP OBJECT */
function getHTTPObject() { var xmlhttp; /*@cc_on @if (@_jscript_version >= 5) try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @else xmlhttp = false; @end @*/  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } return xmlhttp; } var http = getHTTPObject(); // We create the HTTP Object 
var undefMatch;
//var baseURL = "http://www.gtp.com.au/~academic/product/findCourse/"
var baseURL = "http://www.academictaskforce.com.au/product/findCourse/"
var brand	= "0";
var cat		= "0"; 
var misc1	= "0";

function getValues() {
	brand	= document.getElementById('loc').options[document.getElementById('loc').selectedIndex].value;
	// cat		= document.getElementById('cor').options[document.getElementById('cor').selectedIndex].value;
	cat		= document.getElementById('cor').value;
	misc1	= document.getElementById('sub').options[document.getElementById('sub').selectedIndex].value;
}

function reloadData(mode) {
	getValues();
	switch(mode) {
		case 1:	loadLocation(); break;
		case 2:	loadSubject(); break;
		case 3:	loadClass();	break;
	}
}

function reDrawPage() {
	var theTable = document.getElementById('listProdTable');
	var theCol   = document.getElementById('contentColumn');
	var tableHeight = Math.ceil(getHeight(theTable))-0;
	var colHeight   = Math.ceil(getHeight(theCol))-0;
	if (tableHeight>colHeight) {
		tableHeight+=220;
		theCol.style.height=tableHeight+"px";
	}
	eatCookie();
}

function loadLocation() {
	http.open("GET", baseURL+"sel_loc.php??loc="+brand+"&cat="+cat+"&sub="+misc1, true);
	http.onreadystatechange = writeLocation;
	http.send(null);
}
function writeLocation() {
	if (http.readyState == 4)
	{
		document.getElementById("locField").innerHTML  = http.responseText;
		getValues();
		loadSubject();
	}
}

function loadSubject() {
	http.open("GET", baseURL+"sel_sub.php?loc="+brand+"&cat="+cat+"&sub="+misc1, true);
	http.onreadystatechange = writeSubject;
	http.send(null);
}
function writeSubject() {
	if (http.readyState == 4)
	{
		document.getElementById("subField").innerHTML  =  http.responseText;
		getValues();
		loadClass();
	}
}

function loadClass() {
	http.open("GET", baseURL+"list_classes.php?loc="+brand+"&cat="+cat+"&sub="+misc1, true);
	http.onreadystatechange = writeClass;
	http.send(null);
}
function writeClass() {
	if (http.readyState == 4)
	{
		document.getElementById("classArea").innerHTML  =  http.responseText;
		reDrawPage();
	}
}

/**********************************************************************/

function clearOthers(check,block) {
	var theBlock = document.getElementById(block);
	var checkboxes = theBlock.getElementsByTagName('input');
	for (i=0;i<checkboxes.length;i++) {
		var theEl=checkboxes[i];
		if (theEl!=check) {
			if (theEl.type=="checkbox") theEl.checked=false;
		}
	}
	bakeCookie();
}


function masterClass(check,block) {
	bakeCookie();
}


/**********************************************************************/
/* Cookie code to store checkbox selections                           */
/**********************************************************************/

function bakeCookie() {
	cookieName = "acad";

	// compile cookie data
	var cookieData = "";
	var theBlock = document.getElementById('classArea');
	var checkboxes = theBlock.getElementsByTagName('input');
	for (i=0;i<checkboxes.length;i++) {
		var theEl=checkboxes[i];
		if (theEl.type=="checkbox" && theEl.checked==true) {
			i++; theEl=checkboxes[i];
			cookieData+=("|"+theEl.value);
		}
	}
	if (cookieData.length>0) {
		// write cookie
		document.cookie = cookieName+"="+escape(cookieData)+";path=/;";
	} else {
		// delete cookie
		var d = new Date();
	    document.cookie = cookieName+"="+escape(cookieData)+";expires=" + d.toGMTString() + ";path=/;";
	}
}

function eatCookie() {
	cookieName = "acad";

	// does cookie exist
	var allCookies = document.cookie;
	var pos = allCookies.indexOf(cookieName+"=");
	cookie = "";
	if (pos!=-1) {
		var start = pos+cookieName.length+1;
		var cookie = allCookies.substring(start);
		var end = cookie.indexOf(";");
		if (end!=-1) cookie = cookie.substring(0,end);
	} 

	// process that cookie!
	if (cookie.length>0) {
		cookie = "|"+unescape(cookie)+"|";
		var theBlock = document.getElementById('classArea');
		var checkboxes = theBlock.getElementsByTagName('input');
		for (i=0;i<checkboxes.length;i++) {
			var theEl=checkboxes[i];
			if (theEl.type!="checkbox") {
				var thisOne = theEl.value;
				var onePos = cookie.indexOf("|"+thisOne+"|");
				if (onePos!=-1) checkboxes[i-1].checked=true;
			}
		}
	}

}


function showdesc(el) {
	el = document.getElementById(el);
	if (el.style.display!="block") {
		el.style.display= "block";
	} else {
		el.style.display= "none";
	}

//	classArea = document.getElementById('classArea');
//	classAreaHeight = getHeight(classArea) + 476;

//	bodyBlock = document.getElementById('bodyBlock');
//	bodyBlock.style.height = classAreaHeight+"px";

	newHeight = $('#classArea').height() + 476;
	$('#bodyBlock').height(newHeight);
}


