/**
* @package AJAX Javascript Library
* @copyright (C) 2006 Horia Traian
* @license released under the BSD license
* @link http://www.retropixel.net
* @author Horia Traian realricu@gmail.com
*/

var ajax_url = 'index.php';

var ajax_method = 'GET'; 	// HTTP method, values include GET, POST (PUT not supported)
var ajax_username;			// HTTP username
var ajax_password;			// HTTP password
var ajax_cache = false;  	// allow browser to cache requested urls (default false)

var ajax_xml_http;			// the XML HTTP object
var ajax_debug = false;		// turn debug messages on/off

var ajax_callback;			// last callback function name
var ajax_reference			// last refernce string
var ajax_last_url;
var ajax_last_vars;
var ajax_last_method;
var ajax_last_response;

function ajax_post(params, callback, reference, cache, url, username, password)
{
	return ajax_call(params, callback, reference, 'POST', cache, url, username, password)
}

function ajax_get(params, callback, reference, cache, url, username, password)
{
	return ajax_call(params, callback, reference, 'GET', cache, url, username, password)
}

/**
* Parameters
* params    mixed   associative array or string (i.e var1=abc&var2=123)
*					params will be converted to the proper format for the HTTP method
* callback  string  (optional) function name to be called on completion,
*					if no function is provided the last function is used (if available)
*					callback function needs to accept at least one param i.e function do(result)
* reference	string	(optional) ref. to be sent to the callback func i.e function do(result, reference)
*					if no reference is provided the last reference is used (if available)
* method 	string	(optional) HTTP method, default: see ajax_method
* cache 	boolean (optional) cache requested urls, default: see ajax_cache
* url 		string	(optional) request url, default: see ajax_url
* username	string	(optional) HTTP username - OVERWRITES global ajax_username;
* password	string	(optional) HTTP password - OVERWRITES global ajax_password;
*/

function ajax_call(params, callback, reference, method, cache, url, username, password)
{
	
	var send_data = null;
	var conn_str = '';
	var param_str = '';

	if(callback != null) {
		ajax_callback = callback;
	}

	if(reference != null) {
		ajax_reference = reference;
	}

	if(cache == null || typeof(cache) != 'boolean') {
		cache = ajax_cache;
	}

	if(method != null) {
		var tmp = method.toUpperCase();
		switch(tmp) {
			case 'GET': method = tmp; break;
			case 'POST': method = tmp; break;
		}
	} else {
		method = ajax_method;
	}

	if(url == null) {
		url = ajax_url;
	}
	
	if(username != null) {
		ajax_username = username;
	}

	if(password != null) {
		ajax_password = password;
	}

	switch(typeof(params)) {
		case 'object':
		for (key in params) {
			if(params[key] != null) {
				param_str += '&' + key + '=' + params[key];
			}
		}
		break;

		case 'string':
		param_str = params;
		break;
	}

	if(cache == false) {
		param_str += '&rid=' + Math.random();
	}

	// get the XML HTTP object
	ajax_xml_http = GetXmlHttpObject();
	if (ajax_xml_http == null) {
		ajax_msg('Browser does not support AJAX Calls');
		return false;
	}

	ajax_xml_http.onreadystatechange = ajax_state_changed;

	switch(method) {
		case 'GET':
		conn_str = url + '?' + encodeURI(param_str);
		send_data = null;
		break;

		case 'POST':
		conn_str = url;
		send_data = param_str;
		break;
	}

	ajax_last_url = url;
	ajax_last_vars = param_str;
	ajax_last_method = method;

	if (param_str == '') {
		ajax_msg('Empty request data.\n');
		return false;
	}

	ajax_xml_http.open(method, conn_str, true, ajax_username, ajax_password);
	if(method == 'POST') {
		ajax_xml_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	ajax_xml_http.send(send_data);	

}

function ajax_state_changed()
{
	if (ajax_xml_http.readyState == 4 || ajax_xml_http.readyState == 'complete') {
		ajax_last_response = ajax_xml_http.responseText;
		//HTTP 200 - OK
		if(ajax_xml_http.status == 200)  {
			if (ajax_last_response.indexOf('logout') > -1 && login_url != null){
				document.location = login_url;
				return false;
			}
			if(ajax_callback != null) {
				var buf = 'var tmp = ' + ajax_callback + '(ajax_last_response';
				if(ajax_reference != null) {
					buf += ',"' + ajax_reference + '"';
				}
				buf +=');'
				eval(buf);
			}
		}
		ajax_msg('');
	}
}

function ajax_msg(msg)
{
	if (ajax_debug) {
		alert(msg + "\nURL: " + ajax_last_url + "\nData: " + ajax_last_vars + "\nMethod: " + ajax_last_method + "\nCallback: " + ajax_callback + "\nReference: " + ajax_reference + "\n\nResponse: \n" + ajax_last_response);
	}
}

function GetXmlHttpObject()
{
	var XMLHttp = null;

	// ie 6
	try	{
		XMLHttp=new ActiveXObject("Msxml2.XMLHTTP")
	}
	catch(e) {
		try	{
			// ie 5.5
			XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch(e) {
		}
	}
	// Firefox / Safari / Opera
	if (XMLHttp == null) {
		XMLHttp = new XMLHttpRequest();
	}

	return XMLHttp;
}