/***************************************************************************
# File: lib_ajax.js - JavaScript AJAX Engine
# Version: 1.2.3
***************************************************************************/

/************ AJAX Engine ************/
function AjaxEngine()
{
	if(window.bjs_debugger_flag != undefined)
		this.debuggr = true; // Ex: debugger call >> ob_ajax.AddDebug("debug message here");

	this.http_request = false;
	
	this.ready_state_msg_0 = "0. Open hasn't been called";
	this.ready_state_msg_1 = "1. Open has been called; Send hasn't";
	this.ready_state_msg_2 = "2. Open has been called; Send has been called; No response from the server";
	this.ready_state_msg_3 = "3. Server has responded; Ready for data";
	this.ready_state_msg_4 = "4. Server has responded; Returned data!";

	this.http_request_waiting = "We're waiting for a response on your request";

	this.doc_kids_anchors = document.getElementsByTagName("A");
	this.doc_kids_inputs = document.getElementsByTagName("INPUT");
	this.doc_kids_textareas = document.getElementsByTagName("TEXTAREA");
	this.doc_kids_images = document.getElementsByTagName("IMG");
	
	this.method = '';
	this.parts = '';
	this.url = '';
	this.query = '';
	this.query_string = '';
	this.callback = '';
	this.update_content_id = '';

	this.match_anchor_onclick_class = "ajax_anchor_onclick";
	this.match_input_onclick_class = "ajax_input_onclick";

	return this;
}


// Method -- Init any AJAX Flags
AjaxEngine.prototype.AjaxFlagsInit = function()
{
	if(window.ajax_anchor_onclick_flag != undefined)
		ob_ajax.AjaxAnchorOnClickInit();

	if(window.ajax_input_onclick_flag != undefined)
		ob_ajax.AjaxInputOnClickInit();
}


// Method -- Init the XML request
AjaxEngine.prototype.XmlRequestInit = function()
{
	ob_ajax.AddDebug("Executing >> ob_ajax.XmlRequestInit()");
	
	try
	{
		ob_ajax.http_request = new ActiveXObject('Msxml2.XMLHTTP');
		//ob_ajax.AddDebug("ob_ajax.http_request = Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			ob_ajax.http_request = new ActiveXObject('Microsoft.XMLHTTP');
			//ob_ajax.AddDebug("ob_ajax.http_request = Microsoft.XMLHTTP");
		}
		catch(e)
		{
			ob_ajax.http_request = new XMLHttpRequest();
			//ob_ajax.AddDebug("ob_ajax.http_request = XMLHttpRequest()");
		}
	}
}


// Method -- Make the actual rquest
AjaxEngine.prototype.AjaxRequest = function()
{
	ob_ajax.AddDebug("Executing >> ob_ajax.AjaxRequest()");
	
	ob_ajax.query_string = encodeURI(ob_ajax.query) + "&ajax_call=" + new Date().getTime();
	ob_ajax.XmlRequestInit();

	if(ob_ajax.http_request)
	{
		ob_ajax.http_request.onreadystatechange = eval(ob_ajax.callback);
		// ob_ajax.http_request.onreadystatechange = ob_ajax["callback"](); sigh.

		switch(ob_ajax.method)
		{
			case "GET":
				ob_ajax.http_request.open("GET", ob_ajax.url + "?" + ob_ajax.query_string, true);
				ob_ajax.AddDebug("ob_ajax.http_request.open(\"GET\", " + ob_ajax.url + "?" + ob_ajax.query_string + ", true)");
				
				ob_ajax.http_request.send(null);
				ob_ajax.AddDebug("ob_ajax.http_request.send(null)");
				
				break;

			case "POST":
				ob_ajax.http_request.open("POST", ob_ajax.url, true);
				ob_ajax.AddDebug("ob_ajax.http_request.open(\"POST\", " + in_url + ", true)");

				ob_ajax.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				ob_ajax.http_request.setRequestHeader("Content-length", ob_ajax.query_string.length);
				ob_ajax.http_request.setRequestHeader("Connection", "close");
				ob_ajax.http_request.send(ob_ajax.query_string);
		}
	}
}


// Method -- Init anchor onclicks
AjaxEngine.prototype.AjaxAnchorOnClickInit = function()
{
	ob_ajax.AddDebug("Executing >> ob_ajax.AjaxAnchorOnClickInit()");

	for(var i=0; i < ob_ajax.doc_kids_anchors.length; i++)
	{
		if(ob_ajax.doc_kids_anchors[i].className.match(ob_ajax.match_anchor_onclick_class))
			ob_ajax.doc_kids_anchors[i].onclick = this.AjaxAnchorOnClick;
	}
}
	// Method -- Execute anchor onclicks
	AjaxEngine.prototype.AjaxAnchorOnClick = function()
	{
		ob_ajax.AddDebug("Executing >> ob_ajax.AjaxAchorOnClick() by " + this.id);
	
		ob_ajax.method = "GET";
		ob_ajax.parts = this.rel.split("|");
		ob_ajax.url = ob_ajax.parts[0];
		ob_ajax.query = ob_ajax.parts[1];
		ob_ajax.callback = ob_ajax.parts[2];
		ob_ajax.update_content_id = ob_ajax.parts[3];
		
		/*
		ob_ajax.AddDebug("ob_ajax.method = " + ob_ajax.method);
		ob_ajax.AddDebug("ob_ajax.url = " + ob_ajax.url);
		ob_ajax.AddDebug("ob_ajax.query = " + ob_ajax.query);
		ob_ajax.AddDebug("ob_ajax.callback = " + ob_ajax.callback);
		ob_ajax.AddDebug("ob_ajax.update_content_id = " + ob_ajax.update_content_id);
		*/
		
		ob_ajax.AjaxRequest();

		ob_ajax.AddDebug("ob_ajax.AjaxInputOnClick() returning false");
	
		return false;
	}


// Method -- Init input onclicks
AjaxEngine.prototype.AjaxInputOnClickInit = function()
{
	ob_ajax.AddDebug("Executing >> ob_ajax.AjaxInputOnClickInit()");

	for(var i=0; i < ob_ajax.doc_kids_inputs.length; i++)
	{
		if(ob_ajax.doc_kids_inputs[i].className.match(ob_ajax.match_input_onclick_class))
			ob_ajax.doc_kids_inputs[i].onclick = this.AjaxInputOnClick;
	}
}
	// Method -- Execute input onclicks
	AjaxEngine.prototype.AjaxInputOnClick = function()
	{
		ob_ajax.AddDebug("Executing >> ob_ajax.AjaxInputOnClick() by " + this.id);
	
		ob_ajax.method = "GET";
		ob_ajax.parts = this.alt.split("|");
		ob_ajax.url = ob_ajax.parts[0];
		ob_ajax.query = ob_ajax.parts[1];
		ob_ajax.callback = ob_ajax.parts[2];
		ob_ajax.update_content_id = ob_ajax.parts[3];
		
		/*
		ob_ajax.AddDebug("ob_ajax.method = " + ob_ajax.method);
		ob_ajax.AddDebug("ob_ajax.url = " + ob_ajax.url);
		ob_ajax.AddDebug("ob_ajax.query = " + ob_ajax.query);
		ob_ajax.AddDebug("ob_ajax.callback = " + ob_ajax.callback);
		ob_ajax.AddDebug("ob_ajax.update_content_id = " + ob_ajax.update_content_id);
		*/
		
		ob_ajax.AjaxRequest();
		
		return false;
	}


// Method -- Simple way to update an element ID's innerHTML
AjaxEngine.prototype.ContentUpdate = function()
{
	ob_ajax.AddDebug("Executing >> ob_ajax.ContentUpdate(" + ob_ajax.update_content_id + ")");
	
	update_id = document.getElementById(ob_ajax.update_content_id);
	update_id.style.opacity = ".5";

	switch(ob_ajax.http_request.readyState)
	{
		case 0:
			ob_ajax.AddDebug("ob_ajax.http_request.readyState = " + ob_ajax.ready_state_msg_0);
			break;
		case 1:
			ob_ajax.AddDebug("ob_ajax.http_request.readyState = " + ob_ajax.ready_state_msg_1);
			break;
		case 2:
			ob_ajax.AddDebug("ob_ajax.http_request.readyState = " + ob_ajax.ready_state_msg_2);
			break;
		case 3:
			ob_ajax.AddDebug("ob_ajax.http_request.readyState = " + ob_ajax.ready_state_msg_3);
			break;
		case 4:
			ob_ajax.AddDebug("ob_ajax.http_request.readyState = " + ob_ajax.ready_state_msg_4);

			if(ob_ajax.http_request.status == 200 || window.location.href.indexOf("http") == -1)
			{
				if(ob_ajax.http_request.responseText == 0)
				{
					update_id.innerHTML = ob_ajax.http_request_waiting;
					ob_ajax.AddDebug("ob_ajax.http_request.responseText == 0");
				}
				else
				{
					update_id.innerHTML = ob_ajax.http_request.responseText;
					ob_ajax.AddDebug("ob_ajax.http_request.responseText = " + ob_ajax.http_request.responseText);
					ob_ajax.AjaxFlagsInit();
					
					// Possible?
					// - windows.location.pathname
					// - windows.location.href
					// - windows.location.hash
				}
			}
			
			ob_ajax.AddDebug("ob_ajax.http_request.status = " + ob_ajax.http_request.status);
			break;
	}
	
	update_id.style.opacity = "1";
}


// Method -- Output Debug Messages
AjaxEngine.prototype.AddDebug = function(in_line)
{
	if(ob_ajax.debuggr)
		ob_win_bjs.AddItem(in_line);
}
