var req;																						//	Global HTTP Request object
var response = '';																				//	Global var where the response to the transaction is stored
var	isWorking = false;																			//	Remembers if we're waiting for a response
var intervalId;																					//	The identifier for the loop
var intermediary = "http://mibook.local/wdg/intermediary.php";									//	This is the location of the intermediary PHP script
var div1;
var div2;
var resultsDiv;
var resultsText;
var num_programs;
var updateThis = "";
var id;
var loading;
var element;
var catArray = new Array();

var generalelementid;
var generalloading = 'Loading Page...';
var generalshowhide;
var generalcatid;
var curcatid = 'a';
var thethreadid = '';


/*****************************************************************************************
**																						**
**	sendRequest																			**
**																						**
**	1)	Determines from the url parameter whether an intermediary script needs to be	**
**		called. The HttpRequest is limited to grabbing URLs on the same domain, so if a	**
**		remote URL needs to be grabbed, it's gotta be passed through a local script.	**
**	2)	Instantiates the HttpRequest object depending on the platform (IE requires an	**
**		ActiveX object, other browsers can use the native DOM object).					**
**	3)	Parses the URL and encodes the parameters into their ASCII escape values.		**
**	4)	Begins the transaction, and defines the callback function. This callback		**
**		function is called at each step in the transaction.								**
**																						**
**	@param url		The URL to be grabbed.												**
**	@param id		The ID of the HTML element whose content will be replaced.			**
**																						**
*****************************************************************************************/

function sendRequest(url, theelement, elementid) {

    element = theelement;

    if(element == 'results')
    {
      resultsDiv = document.getElementById("memberinfo" + id);
      loading = "Loading...";
	}else if(element == 'subscribe') {
      resultsDiv = document.getElementById("subscribe" + thethreadid);
      loading = '<img src="subscribeloading.gif" alt="Loading..." title="Loading..." />';
    }else if(element == 'threads') {
      resultsDiv = document.getElementById("threads" + id);
      loading = 'Threads Loading...';
    }else if(element == 'general') {
      generalelementid = elementid;
      resultsDiv = document.getElementById(generalelementid);
      loading = '<img src="initializeload.gif" alt="Loading." title="Loading." border="0" />' + generalloading;
    }
	resultsDiv.innerHTML = loading;
	if (!isWorking) {																			//	If there's no current transaction, begin one

		updateThis = element;

		//resultsDiv.innerHTML = "";

		//	Instantiate the HttpRequest object, dependant on the browser
              //window.alert("About to create the ActiveXObject");
		if (window.XMLHttpRequest) {															//	Mozilla/KHTML branch
			req = new XMLHttpRequest();															//	Instantiate the native DOM XMLHttpRequest object
			}	//	End Mozilla/KHTML branch
		else if (window.ActiveXObject) {														//	IE branch
			req = new ActiveXObject("Microsoft.XMLHTTP");										//	Instantiate the ActiveX XMLHTTP object
			}	//	End IE branch
              //window.alert("About to set its req change");
		req.onreadystatechange = processReqChange;												//	Specify the callback function

		//	Check to see whether the intermediary script even needs to be called.
		//	It needs to be bounced through this script if the AJAX target is off-server,
		//	to comply with Mozilla's requirement that AJAX calls must be made to a local
		//	server or else authenticated.
		var thisDomain = location.href.split('/')[2];											//	Get the domain name of the current page
		var remoteDomain = url.split('/')[2];													//	Get the domain name of the URL to be grabbed

		if (thisDomain != remoteDomain) {														//	If it needs an intermediary (ie, the two URLs are in the same domain)
			var wholeUrl = intermediary + "?url=" + escape(url);								//	Use the intermediary, with a "url" parameter
			}	//	End needs-intermediary condition
		else {																					//	If it doesn't need the intermediary
			var wholeUrl = url;																	//	Just use the URL
			}	//	End doesn't-need-intermediary condition

		wholeUrl = url;
										//	DEBUG: Display the URL passed off to the handler
		
		//	Begin the HTTP request
              //window.alert("About to send the request");
		req.open("GET", wholeUrl, true);														//	Configure the HTTP transaction to use POST, use the url, and be asynchronous
		req.send(null);

        loading = '<img src="loading.gif" alt="Loading.." title="Loading.." border="0" />' + generalloading;

        //resultsDiv.innerHTML = loading;																				//	Begin the HTTP transaction

		//	Check periodically to see if we've gotten the final response,
		//	and flag that we're working until we do.
              //window.alert("Is working");
		isWorking = true;																		//	Indicate there is a current transaction

		}	//	End isn't-working condition
	else {
              //window.alert("Aborting The thing...");																						//	If there is a current transaction,
		req.abort();																			//	Kill it
		isWorking = false;	
              //window.alert("Resending Request");																	//	Indicate that we're no longer working
		sendRequest(url, element, elementid);																//	And try again
		}	//	End is-working condition
	}	//	End function sendRequest





/*****************************************************************************************
**																						**
**	processReqChange																	**
**																						**
**	This callback function gets called each time the transaction hits a new stage. Once	**
**	the transaction is completed, the response text is stored into a holder DIV.		**
**																						**
*****************************************************************************************/

function processReqChange() {

	if (req.readyState == 4) {	
              //window.alert("Ready State is 4");																//	Once the transaction is complete
		isWorking = false;																	//	Indicate that you're done working

		if (req.status == 200) {	
              //window.alert("Status is 200");															//	If it completed successfully													//	If the results are to be updated
				resultsDiv.innerHTML = '<img src="loadingcomplete.gif" alt="Complete..." border="0" title="Complete..." />' + generalloading;
				updateResults();																//	Call the results-specific update function
			}	//	End successful-transaction condition
		else {																					//	If there was a problem grabbing the page (404, etc)
			window.alert("There was a problem retrieving the XML data:\n" +						//	Spit out an error message
				req.statusText);
			}	//	End unsuccessful-transaction condition
		}	//	End completed-transaction condition
	}	//	End function processReqChange
	
/*******************************************************************************************
**
**  Add Category Array Element
**
*******************************************************************************************/

function addCategoryArrayElement(catID) {
  catArray[catArray.length] = catID;
}
