/**
 *	Ajax class constructor
 */
function c_Ajax () {

	this.request 			= null;
	this.request_tab_name	= null;
	this.module  			= null;
	this.onLoadFunction 	= null;
	this.loadingBox			= null;

	if (window.XMLHttpRequest)
		this.request = new XMLHttpRequest();
	else if (window.ActiveXObject)
		this.request = new ActiveXObject("Microsoft.XMLHTTP");
}


/**
 *
 */
c_Ajax.prototype.sendRequest = function ( url, HttpMethod, params, onLoadFunction, loadingBox ) {

	var loader = this;

	if (onLoadFunction)
		this.onLoadFunction = onLoadFunction;

	if (loadingBox)
		this.loadingBox = loadingBox;

	if (!HttpMethod)
		HttpMethod="GET";

	if (this.request) {
		this.request.onreadystatechange = function() {
			loader.onLoadingHandler.call(loader);
		}

		this.request.open(HttpMethod, url );
		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		if (params == undefined || params == '')
			this.request.send(null);
		else
			this.request.send(params);
	}
}


/**
 *
 */
c_Ajax.prototype.sendSimpleRequest = function ( url, HttpMethod, params, onLoadFunction, loadingBox ) {

	var ajax_handler = this;

	if (loadingBox)
		this.loadingBox = loadingBox;

	if (!HttpMethod)
		HttpMethod="GET";

	if (this.request) {
		this.request.onreadystatechange=function() {
			if ( ajax_handler.request.readyState==4 ) {
				if (ajax_handler.loadingBox)
					ajax_handler.loadingBox.hide();
				if (onLoadFunction)
					onLoadFunction(ajax_handler.request.responseText);
			}
			else {
				if (ajax_handler.loadingBox)
					ajax_handler.loadingBox.show();
				return false;
			}
		}
		this.request.open(HttpMethod, url );
		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		if (params == undefined || params == '')
			this.request.send(null);
		else
			this.request.send(params);
	}
}


/**
 *
 */
c_Ajax.prototype.setModule = function( module ) {

	if (module instanceof c_Module)
		this.module = module;
	else alert("ERROR: invalid module '" + module + "'");
}


/**
 *
 */
c_Ajax.prototype.onLoadingHandler = function() {

	if ( this.request.readyState==4 ) {

		if (this.loadingBox)
			Divs_manager.hideModule('loading_data_box');

		if (this.module.type == 'tabs') {
			for (var n=0;n<this.module.tabs_array.length;n++)
				if ( this.request_tab_name == this.module.tabs_array[n].name ) {
					document.getElementById(this.module.name + '_' + this.module.tabs_array[n].name).innerHTML = this.request.responseText;
				}
		}
		else
			this.module.content.content_center.innerHTML = this.request.responseText;

		if (this.module.show_on_load == 'Y')
			Divs_manager.showModule(this.module.name);

		//This executes the module on_load_function if it is different of the passed onLoadFunction
		if ( this.module.on_load_function && this.onLoadFunction && this.module.on_load_function != this.onLoadFunction)
			this.module.on_load_function();

		//This executes the passed onLoadFunction
		if ( this.onLoadFunction )
			this.onLoadFunction();
	}
	else {
		if (this.loadingBox)
			Divs_manager.showModule('loading_data_box');
		return false;
	}
}


/**
 *
 */
c_Ajax.prototype.submitForm = function( form, url, onLoadFunction, loading_box ) {

	var num_controls = form.elements.length;
	var params		 = "";
	var controls	 = new Array();

	//checkbox and radio button
	for(var i=0;i<num_controls;i++) {
		if ( ( form.elements[i].type=='checkbox' || form.elements[i].type=='radio' ) && form.elements[i].checked ) {
		 	controls[controls.length] = new Array();
			controls[controls.length-1]['type']  = form.elements[i].type;
			controls[controls.length-1]['name']  = form.elements[i].name;
			controls[controls.length-1]['value'] = escape(form.elements[i].value);
		}
		//another controls (All controls Types: button, checkbox, file, hidden, image, password, radio, reset, submit, text)
		else if (	form.elements[i].type &&
					form.elements[i].type!='radio' &&
					form.elements[i].type!='checkbox' &&
					form.elements[i].type!='button' &&
					form.elements[i].type!='image' &&
					form.elements[i].type!='reset' &&
					form.elements[i].type!='submit' ) {
			 	controls[controls.length] = new Array();
				controls[controls.length-1]['type']  = form.elements[i].type;
				controls[controls.length-1]['name']  = form.elements[i].name;
				var regex = /&/g;
				controls[controls.length-1]['value'] = form.elements[i].value.replace( regex, '%26');
		}
	}

	for(var i=0;i<controls.length;i++)
		params += controls[i]['name'] + "=" + controls[i]['value'] + "&";

	this.sendSimpleRequest( url, 'POST', params, onLoadFunction, loading_box );
}