var ajax = {
	
	ajax_debug_mode : false,//Debug flag
	ajax_request_type : "GET", //GET or POST
	uri : "std.php",
	
	/*
	Debug method
	*/
	debug: function (text) {
		if (this.ajax_debug_mode)
			alert("DEBUG: " + text)
	},
	/*
	Criate XMLHttpRequest object
	*/
	init: function () {
		this.debug("ajax_init_object() called..")
		
		var A;
		try {
			A=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				A=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				A=null;
			}
		}
		if(!A && typeof XMLHttpRequest != "undefined")
			A = new XMLHttpRequest();
		if (!A)
			this.debug("Could not create connection object.");
		return A;
	},
	/*
	Simulate include function ( like PHP )
	*/
	include: function(nome,url){
		uri = url;
		this.uri = url;
		A = this.init();
		A.open("GET",url,true);
		A.onreadystatechange=function() {
			if (A.readyState==4){
				retorno=unescape(A.responseText.replace(/\+/g," "));
				document.getElementById(nome).innerHTML =retorno;
			}
		}
		A.send(null);	
	},
	/*
	Make a call to execute the function
	*/
	execute: function (page ,func_name, func_js ,args, retarg ) {
		var i, x, n;
		var post_data;
		uri  = page;
		this.uri = page;
		ajax_request_type = this.ajax_request_type;

		if (ajax_request_type == "GET") {
			if (uri.indexOf("?") == -1) 
				uri = uri + "?rname=" + escape(func_name);
			else
				uri = uri + "&rname=" + escape(func_name);
			for (var key in args){ 
				uri = uri + "&"+key+"=" + escape(args[key]);
			}	
			uri = uri + "&rtime=" + new Date().getTime();
			post_data = null;
		} else {
			post_data = "rname=" + escape(func_name);
			for (var key in args){ 
				post_data  = post_data + "&"+key+"=" + escape(args[key]);
			}					
		}
		
		x = this.init();
		x.open(ajax_request_type, uri, true);
		if (ajax_request_type == "POST") {
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		x.onreadystatechange = function() {
			if (x.readyState != 4) 
				return;
			ajax.debug("received " + x.responseText);
			var data;
			data = x.responseText;
			
			if ( func_js ) { // call function
				if ( retarg )
					func_js(data, retarg);
				else func_js(data);
					
			}
			else {
				
				return (data); // return data
				
			}
					
		}
		x.send(post_data);
		this.debug(func_name + " uri = " + uri + "/post = " + post_data);
		this.debug(func_name + " waiting..");
		delete x;
	},
	/*
	Aliases for document.getElementById(id).innerHTML
	*/
	insertHTML : function(id,content){
		document.getElementById(id).innerHTML = content;
	},
	getHTML : function(id){
		return document.getElementById(id).innerHTML;
	}	
}
