/*************************************************************************
Name:	AjaxJS
Desc:	sends ajax requests for inline updating
*************************************************************************/

function AjaxJS() {
	this.httpRequest;
	this.config;
}

//trigger the ajax request
AjaxJS.prototype.request = function(config) {
	this.config = config;
	var manager =	(config['manager'] || '');	//manager to require (or null)
	var module =	(config['module'] || '');	//module to require (or null)
	var script =	(config['script'] || '');	//script to require (or null)
	var target =	config['target'];			//target class to call function on
	var func =		config['func'];				//PHP function to call
	var args = 		urlencode(JAVAtoPHP(config['args'])); 	//JAVA args to convert to PHP and pass to above function
	var url =		DOM_SCRIPT_PATH + '/ajax/ajax.php';		//where we're posting
	//data to send
	var requestURL =	"config=" + DOC_CONFIG_PATH +
						"&admin=" + DOC_BACKEND_PATH +
						"&resource=" + DOC_RESOURCE_PATH +
						"&manager=" + manager +
						"&module=" + module +
						"&script=" + script +
						"&target=" + target +
						"&func=" + func +
						"&args=" + args +
						"&ajax=1";	//flag so ajax will know to catch it
					
	if (window.XMLHttpRequest) {		//setup for Mozilla, Safari
		this.httpRequest = new XMLHttpRequest();
		//if (httpRequest.overrideMimeType) {httpRequest.overrideMimeType('text/xml');}
	} else if (window.ActiveXObject) {	//setup for IE
		try {this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {
			try	{this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e)	{
			}
		}
	}
	//error if request cannot be properly configured
	if (!this.httpRequest) {
		alert('Error - Cannot create an XMLHTTP instance');
		return false;
	}
	//send request
	var parent = this;
	this.httpRequest.onreadystatechange = function() {parent.response();};
	this.httpRequest.open("POST", url, true); 
	this.httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                
	this.httpRequest.send(requestURL);
}

//function called on the ajax return
AjaxJS.prototype.response = function() {
	var return_target = this.config['return_target']; 	//JAVA target for return data
	var return_func = 	this.config['return_func'];		//JAVA function for return data
	
	//only call func on success
	if ((this.httpRequest.readyState == 4) && (this.httpRequest.status == 200)) {
		//usually we'd get the XML as xml, but since the post included php to generate the output we need to parse it
		//xml = this.httpRequest.responseXML;
		var response = this.httpRequest.responseText;	//the responseText is a PHP page with XML written on it
		
		//find the outputted XML in the content
		var start = response.indexOf('<' + XML_ROOT + '>');
		var end = response.indexOf('</' + XML_ROOT + '>', start) + (String(XML_ROOT).length + 3);
		
		//prepare an array
		var data = new Object();
		if (start >= 0) {
			var xmlText =	response.substring(start, end);	//get string
			var xml =		stringToXML(xmlText);			//string to XML
			data = XMLtoJAVA(xml);							//XML to JAVA array
		}
		
		//a little error handling
		if ((response.indexOf('Fatal '+' error') >= 0)){ //||
			//(response.indexOf('Notice') >= 0)) {
			document.write(response);
			//alert(response.substr(0, 5000));
		}	
		//log debug data
		if ((data != null) && (data['debug'] != null)) {addLog(data['debug'], 'ajax.js - response');}
			
		//call the appropriate function and pass the returned data to it
		callFunc(return_target, return_func, data);
		
		//delete the request to free memory
		delete this.httpRequest['onreadystatechange'];
		this.httpRequest = null;
	}
}

//shortcut for making ajax requests
function ajaxRequest(config) {
	var ajax = new AjaxJS();
	ajax.request(config);
}