// JavaScript Document

/************************************/
/*		   AJAX Connections		   */
/************************************/

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

/************************************/
/*		 Task-specific funtions	    */
/************************************/

function getCatSubs(Cat_ID, target){
	http=GetXmlHttpObject();

	//document.getElementById("Cat_Loading").style.display = "";
	document.getElementById("subNav").style.display = "none";
	
	if (http==null){
	  //bah, figure out what to do with browsers that don't suppor AJAX :-/
	  document.getElementById("Cat_Subs").innerHTML = 'This service is temporarily unavilable';
	  return;
	} 
	
	var url = target+"/Inc/subresponse.php";
	var params = "Cat_ID="+Cat_ID;
	
	http.open("POST", url, true);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			//Break the response in to an array of sections			
			var sectionArray = http.responseText.split('*');
			
			if(sectionArray.length > 2){
				var bodyString = '<UL>';
				for(var i in sectionArray){
					//Break each section in to an array to build the HTML
					var responseArray = sectionArray[i].split('%');
									
					if(responseArray[0]){
						if(responseArray[2] == 'index'){
							var titleString = '<A HREF="'+ target +'/'+ responseArray[1] +'/" TITLE="'+ responseArray[0] +'">'+responseArray[0]+'</A>';
						}
						else{
							bodyString += '<LI><A HREF="'+ target +'/'+ responseArray[1] +'/'+ responseArray[2] +'/" TITLE="'+ responseArray[0] +'">'+responseArray[0]+'</A></LI>';
						}
					}
				}
				bodyString += '</UL>';
				
				document.getElementById("Cat_Title").innerHTML = titleString;
				document.getElementById("Cat_Subs").innerHTML = bodyString;
			}
			else{
				document.getElementById("subNav").innerHTML = '';				
			}
			
			//document.getElementById("Cat_Loading").style.display = "none";
			document.getElementById("subNav").style.display = "";
		}
	}
	
	http.send(params);
}
