/**********************************************/
/* ajax functions to do the work for updating */
/**********************************************/
/* sets a preferenes, it callback is true the it reloads the page once the
 * server responds to the request */
function set_pref(pref, value, needs_reload) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	var url = html_root + "js/prefs.php?" + pref + "=" + value + "&sid=" + Math.random();
	if (needs_reload) {
		xmlHttp.onreadystatechange = reload;
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
} 

/* sets a preferenes, it callback is true the it reloads the page once the
 * server responds to the request */
function set_pref(pref, value, needs_reload) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	var url = html_root + "js/prefs.php?" + pref + "=" + value + "&sid=" + Math.random();
	if (needs_reload) {
		xmlHttp.onreadystatechange = reload;
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
} 


/*******************/
/* style functions */
/*******************/
var collapsed = new Object(); // contains the states of the collapsable objects
/* set a sidebar to be collapsed if mode is true, and open if mode is false */
function set_collapse(obj_id, mode) {
	if (mode) {
		var obj = document.getElementById(obj_id);	
		var caller = document.getElementById(obj_id + "_collapse");	
		var img_dir = caller.src.substring(0, caller.src.lastIndexOf("/")+1);
		caller.src = img_dir + "closed.gif";
		obj.style.overflow = "hidden";
		obj.style.padding = "0px";
		obj.style.margin = "0px";
		obj.style.right = "0px";
		obj.style.height = "21px";
		obj.style.width = "21px";
		obj.style.borderWidth = "0px";
		obj.style.position = "absolute";
		obj.style.backgroundColor="transparent";

		collapsed[obj_id] = true;
		set_pref("sub_nav-collapsed", "true");
	} else {
		var obj = document.getElementById(obj_id);	
		var caller = document.getElementById(obj_id + "_collapse");	
		var img_dir = caller.src.substring(0, caller.src.lastIndexOf("/")+1);
		caller.src = img_dir + "open.gif";

		obj.style.overflow = "";
		obj.style.height = "";
		obj.style.width = "";
		obj.style.padding = "";
		obj.style.margin = "";
		obj.style.borderWidth = "";
		obj.style.position = "";
		obj.style.right = "";

		obj.parentNode.style.display = "";
		obj.parentNode.style.position = "";
		obj.parentNode.style.right = "";
		obj.parentNode.style.width = "";
		obj.style.backgroundColor="";

		collapsed[obj_id] = false;
		set_pref("sub_nav-collapsed", "false");
	}
}

/* switches the given element from collapsed to open */
function toggle_collapse(obj_id) {
	set_collapse(obj_id, !collapsed[obj_id]);
}

/* sets the language to be used for the user */
function set_language(lang_name) {
	set_pref("language", lang_name, true);
}

/* sets the captioning to be used for the user */
function set_captioning(enabled) {
	set_pref("captioning", enabled);
}

/* changes the users text size */
function changeText(size) {
	document.body.style.fontSize = size;
	set_pref("font-size", size);
}
	
/* changes the users theme */
function setStylesheet(title) {
	var i, cacheobj
	for(i=0; cacheobj = document.getElementsByTagName("link")[i]; i++) {
		if(
			cacheobj.getAttribute("rel").indexOf("style") != -1 &&
			cacheobj.getAttribute("title")
		) {
			cacheobj.disabled = true
			if (cacheobj.getAttribute("title") == title) {
				cacheobj.disabled = false
			}
		}
		//alert(cacheobj.getAttribute("title"));
	}
	set_pref("theme", title);
}

function toggle_visible(id) {
	style = document.getElementById(id).style;
	if (style.visibility == 'visible') {
		style.visibility = 'hidden';
	} else {
		style.visibility = "visible";
	}
}

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

function set_order(type, cur_reversed) {
	set_pref('order_type', type, false);
	set_pref('order_reversed', !cur_reversed, true);
}
