	//Function to create an XMLHttp Object.
	function getxmlhttp (){
		//Create a boolean variable to check for a valid microsoft active X instance.
		var xmlhttp = false;
		
		//Check if we are using internet explorer.
		try {
			//If the javascript version is greater than 5.
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			//If not, then use the older active x object.
			try {
				//If we are using internet explorer.
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				//Else we must be using a non-internet explorer browser.
				xmlhttp = false;
			}
		}
		//If we are using a non-internet explorer browser, create a javascript instance of the object.
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		
		return xmlhttp;
	}
	
	function extraiScript(texto){
        var ini = 0;
        // loop enquanto achar um script
        while (ini!=-1){
                ini = texto.indexOf('<script', ini);
                // se encontrar
                if (ini >=0){
                        // define o inicio para depois do fechamento dessa tag
                        ini = texto.indexOf('>', ini) + 1;
                        var fim = texto.indexOf('</script>', ini);
                        // extrai apenas o script
                        codigo = texto.substring(ini,fim);
                        // executa o script
                        eval(codigo);
                }
        }
	}
	
	var ajaxatual ="home";
	//Function to process an XMLHttpRequest.
	function loadajax (serverPage, obj){
		serverPage = serverPage + '.php';
		if (serverPage != ajaxatual){	
		
		$("#mesa").fadeOut(300);
		
		var start = new Date().getTime();
  			for (var i = 0; i < 1e7; i++) {
    			if ((new Date().getTime() - start) > 300){
     			 break;
    		}
  		}
		
			showLoadMsg();
			document.getElementById(obj).style.visibility = "visible";

			xmlhttp = getxmlhttp();
			xmlhttp.open("GET", serverPage, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					// coloca o valor no objeto requisitado
                    texto=unescape(xmlhttp.responseText.replace(/\+/g," "));
                    document.getElementById(obj).innerHTML=texto;
                    // executa scripts
                    extraiScript(texto);
				}
			}
			xmlhttp.send(null);
			ajaxatual = serverPage;
			$("#mesa").fadeIn(700);
		}
	}

	
	//Function to output a loading message.
	function showLoadMsg(){
		hidden = document.getElementById('mesa');
		hidden.innerHTML = '<div id="abox_load"><img src="imgs/ajax-loader.gif" alt="" /></div>';
	}
