function getArticle(id, toc){
	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){
				document.getElementById("divBody").innerHTML = xmlHttp.responseText;
				if(document.location.href.indexOf("#")){
					var myurl = document.location.href.split("#");
					if(document.getElementById(myurl[1])){
						window.navigate("#" + myurl[1]);
						window.navigate("#");
					} else {
						document.getElementById("divBody").scrollTop = 0;	
					}
				}
			}
		}
		
		xmlHttp.open("GET", "http://codebook.mahanoycity.org/articles.php?grp=" + id + "&now=" + Date(), true);
		xmlHttp.send(null); 
}

function getSearch(keywords, offset, perpg){
	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){
			document.getElementById("divBody").innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("GET", "http://codebook.mahanoycity.org/search.php?keywords=" + keywords + "&offset=" + offset + "&perpg=" + perpg + "&now=" + Date(), true);
	xmlHttp.send(null); 
}

function getTOC(id, opento){
	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){
			document.getElementById("divTOC").innerHTML = xmlHttp.responseText;
			toggleOpen(opento, opento);
		}
	}
	
	xmlHttp.open("GET", "http://codebook.mahanoycity.org/toc.php?id=" + id + "&now=" + Date(), true);
	xmlHttp.send(null); 
}

function openTree(id){
	if(document.getElementById('sub' + id)){
		if(document.getElementById('sub' + id).style.display == "block"){
			document.getElementById('sub' + id).style.display = "none";
			if(id != "000000" && document.getElementById('i' + id).src == "http://codebook.mahanoycity.org/iopen.gif"){
				document.getElementById('i' + id).src = "http://codebook.mahanoycity.org/iclosed.gif";
			}
		} else {
			document.getElementById('sub' + id).style.display = "block";
			if(id != "000000" && document.getElementById('i' + id).src == "http://codebook.mahanoycity.org/iclosed.gif"){
				document.getElementById('i' + id).src = "http://codebook.mahanoycity.org/iopen.gif";
			}
		}
	}
}

function toggleOpen(id, orig){
	if(document.getElementById('sub' + id)){
		var parentId = document.getElementById('sub' + id).parentNode.id;

		if(id == orig){
			window.navigate(document.getElementById('lnk' + orig).href);
			document.getElementById('lnk' + orig).setActive();
		} else {		
			openTree(id);
		}
		
		if(parentId != "divTOC"){
			toggleOpen(parentId.substr(3,8), orig);
		}
	}
}

function setFontSize(size){
	if(size == "lg"){
		//Increase Size
		document.getElementById("divBody").className = "lgfont";
	}
	if(size == "sm"){
		//Decrease Size
		document.getElementById("divBody").className = "smfont";
	}
	if(size == "rg"){
		//Normal Size
		document.getElementById("divBody").className = "rgfont";
	} 
}

function pgPrint(){
	window.print();	
}

function setCodebook(){
	var winH = document.documentElement.clientHeight;	
	var pgY = document.getElementById('divCodebook').offsetTop;
	return winH-pgY;
}

window.onresize = function(){
	document.getElementById('divCodebook').style.height = (setCodebook()-4)+"px";
}

window.onload = function(){
	document.getElementById('divCodebook').style.height = (setCodebook()-4)+"px";
}