/*
 // +----------------------------------------------------------------------+
 // | Copyright (c) 2004-2008 Tom Levitt, All Rights Reserved.             |
 // +----------------------------------------------------------------------+
 // | Redistribution and use in source form, with or without modification  |
 // | is NOT permitted without consent from the copyright holder.          |
 // |                                                                      |
 // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
 // | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,    |
 // | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A          |
 // | PARTICULAR PURPOSE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,    |
 // | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  |
 // | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   |
 // | PROFITS; OF BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  |
 // | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         |
 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    |
 // | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     |
 // | DAMAGE.                                                              |
 // +----------------------------------------------------------------------+
 // | Authors: Tom Levitt <oproot@gmail.com>                               |
 // +----------------------------------------------------------------------+
*/

var url_base         = "";

/* Configuration */
var html_busy        = "ajax_busy";
var html_status      = "ajax_status";
var html_status_box  = "ajax_status_box";
var html_content     = "ajax_content";

/* global "session" varibles */

//var current_tab = "overview";

/* create XMLHTTP object, cross browser style */
var request1 = false;
var request2 = false;
var request3 = false;
var request1_in_use = false;
var request2_in_use = false;
var request3_in_use = false;

try {
	request1 = new XMLHttpRequest();
	request2 = new XMLHttpRequest();
	request3 = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request1 = new ActiveXObject("Msxml2.XMLHTTP");
		request2 = new ActiveXObject("Msxml2.XMLHTTP");
		request3 = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
		try {
			request1 = new ActiveXObject("Microsoft.XMLHTTP");
			request2 = new ActiveXObject("Microsoft.XMLHTTP");
			request3 = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			request1 = false;
			request2 = false;
			request3 = false;
		}  
	}
}

if (!request1) {
	//document.getElementById(html_status).innerHTML = "Error initializing XMLHttpRequest!";
}

/* New ajax Engine ******************************************** */
function updateGUI() {
	var response = false;
	var proceed = false;

	/* Determine which request object triggered the onstatechange event  */
	if (request1_in_use) {
		if (request1.readyState == 4) {
			request1_in_use = false;
			if (request1.status == 200) {
				response = request1.responseText.split("||||");
				response_raw = request1.responseText;
				proceed = true;
			}
		}
	} 

	if (request2_in_use) {
		if (request2.readyState == 4) {
			request2_in_use = false;
			if (request2.status == 200) {
				response = request2.responseText.split("||||");
				response_raw = request2.responseText;
				proceed = true;
			}
		}
	} 

	if (request3_in_use) {
		if (request3.readyState == 4) {
			request3_in_use = false;
			if (request3.status == 200) {
				response = request3.responseText.split("||||");
				response_raw = request3.responseText;
				proceed = true;
			}
		}
	} 

/* ajax regex */
var regex_ajax = /AJAX/;

	/* Debug - Output the 3 http request object's status (request#.readystate.in_use) */
	/*document.getElementById("Notes_Link").innerHTML =
		'r1.'+request1.readyState+'.'+request1_in_use+
		':r2.'+request2.readyState+'.'+request2_in_use+
		':r3.'+request3.readyState+'.'+request3_in_use;*/

if( proceed ) {
	//alert( "'"+response[1]+"'" );
}

	/* Abort function if none of the request objects were in the propper state */
	if (!proceed) {
		return false;
	/* Refresh the entire page if the response does not contain the ajax signiture */
	/* This will occur if the login session expires, for example. */
	} else if (!regex_ajax.test(response[1])) {
	//} else if (response[0] != "ajax") {
		//alert(response[1]);
		//document.getElementById(html_status).innerHTML = "AJAX signigutre was not found. Aborting.";
		//cssHideLayer(html_busy);
		//document.getElementById(html_content).innerHTML = response_raw;
	/* Proceed only if the response's ip matches the ip input text field */
	} else  {
		/* Iterate through each response object */
		for ( i=3; i<response.length; i++ ) {
			/* Further seperate the data */
			dataset = response[i].split("::::");
			object_type = dataset[0];
			object_id = dataset[1];
			object_content = dataset[2];
			
			/* Fill the object's content */
			if (object_type == "div") {
				document.getElementById(object_id).innerHTML = object_content;
			} else if (object_type == "var") {
				eval_string=object_id+' = \''+object_content.trim()+'\';';
				eval (eval_string);
			} else if (object_type == "show") {
				cssShowLayer(object_id);
			} else if (object_type == "hide") {
				cssHideLayer(object_id);
			} else if (object_type == "class") {
				cssChangeClass(object_id, object_content);
			} else if (object_type == "javascript") {
				eval(object_id);
			} else if (object_type == "alert") {
				alert(object_id);
			//document.getElementById(object_id).className = object_content.trim();
				//alert("'"+object_id+"','"+object_content.trim()+"'");
			}
		}
	}
	/* Hide and show applicable div objects */
	if (!request1_in_use && !request2_in_use && !request3_in_use) {
		//cssHideLayer(html_busy);
		//document.getElementById(html_status).innerHTML = "Done"
	}
}

function refresh() {
	/* internet explorer is lame.. the following code causes ie6 to crash */
	/*window.location.reload( true ); */
	var sURL = unescape(window.location.pathname);
	window.location.href = sURL;
}




function ajaxGet( url, create_history ) {
	if( create_history == "0") {
		create_history = "0";
	} else {
		create_history = "1";
	}

	method = "GET";
	
//	document.getElementById(html_busy).innerHTML = "<img src=\"templates/default/control/images/busy.gif\" width=\"16\" height=\"16\">";
	if (!request1_in_use) {	
		request1_in_use = true;
		request1.open(method, url, true);
		request1.onreadystatechange = updateGUI;
		//if( create_history = "1" ) { dhtmlHistory.add(url,"CaroNet :: NocWorx"); }
		request1.send(null);
		//if( create_history == "1") { dhtmlHistory.add( url, '' ); }
	} else if (!request2_in_use) {
		request2_in_use = true;
		request2.open(method, url, true);
		request2.onreadystatechange = updateGUI; 
		//if( create_history = "1" ) { dhtmlHistory.add(url,"CaroNet :: NocWorx"); }
		request2.send(null);
		//if( create_history == "1") { dhtmlHistory.add( url, '' ); }
	} else if (!request3_in_use) {
		request3_in_use = true;
		request3.open(method, url, true);
		request3.onreadystatechange = updateGUI; 
		//if( create_history = "1" ) { dhtmlHistory.add(url,"CaroNet :: NocWorx"); }
		request3.send(null);
		//if( create_history == "1") { dhtmlHistory.add( url, '' ); }
	} else { 
		//document.getElementById(html_status).innerHTML = 'There are already three active requests in progress. Please wait and try again.';
		return;
		//alert('There are already three active requests in progress. Please wait and try again.');
	}

}


function sendAjax (url,create_history) {
regex_question_mark = /\?/;
regex_method_ajax   = /method=ajax/;
if( !regex_method_ajax.test(url) ) {
	if( regex_question_mark.test(url) ) {
	url = url+"&method=ajax";
	} else {
	url = url+"?method=ajax";
	}
}
 
//document.getElementById(html_status).innerHTML = "Fetching "+url; 
//cssShowLayer(html_busy);
//cssShowLayer(html_status_box);
ajaxGet(url,create_history);
} 
