function AJAX(url, metodo, params, processa, modo) {
	this.url = url;
	this.metodo = (metodo) ? metodo : 'GET';
	this.params  = (metodo='GET') ? "" : params;
	this.retorno = processa;
	this.modo = (modo) ? modo : 'T';
	if (this.modo != 'T' && this.modo != 'X') { this.modo = 'T'; }
	this.conectar();
}

AJAX.prototype = {
	conectar: function() {
		if (this.url == undefined || this.url == '') {
			return; 
		}
		this.httprequest = null;
	   	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	     	this.httprequest = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
	     	try {
		     	 this.httprequest = new ActiveXObject("Msxml2.XMLHTTP");
	     	} catch (e) {
	       		try {
	           	 this.httprequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (this.httprequest != null && this.httprequest != undefined) {
			var obj = this;
			this.httprequest.onreadystatechange = function() {
				obj.processaRetorno.call(obj);
			}
			if (this.metodo == undefined || this.metodo == '') { this.metodo = 'GET';}
	    	this.httprequest.open(this.metodo, this.url, true);			
			this.httprequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			this.httprequest.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			this.httprequest.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			this.httprequest.setRequestHeader("Pragma", "no-cache");			
	        this.httprequest.send(this.params);
		}
	},
	processaRetorno: function() {
		if (this.httprequest.readyState == 4) {
			if (this.httprequest.status == 200) {
				var resp = (this.modo == 'T') ? this.httprequest.responseText : this.httprequest.responseXML;
				if (this.retorno != null) {
					if (this.modo == 'T') {
						//resp = resp.replace(/\+/g," ");
						resp = unescape(resp); 
					}					
					this.retorno(resp);
				} else {
					document.write(resp);
				}
			} else { 
				this.processaErro();
			}
		}
	},

	processaErro: function() {
		if (document.getElementById("container") != undefined) {
			document.getElementById("container").innerHTML = this.httprequest.responseText;
		} else {
			if (document.getElementById("div_centro") != undefined) {
				// exibe erro na tela
				document.getElementById("div_centro").innerHTML = this.httprequest.responseText;				
				// tela de erro para memória do DH
				//AjaxInclude("/imagem/erro", "div_centro");
			}
		}
		//alert(this.httprequest.status + '-' + this.httprequest.statusText + ' :-> ' + this.url);
	}
}

function AjaxInclude(url, containerid) {

	var ajax = new AJAX();
	ajax.url = url;	
	ajax.metodo = "POST";
	ajax.retorno = function(texto) {
		document.getElementById(containerid).innerHTML = texto;
		ExecutaScript(texto);
		// Limpa da tela a mensagem de aguarde
		AguardeBotaoImagem(GLOBAL_AGUARDE1, GLOBAL_AGUARDE2, false);
	}
	ajax.conectar();
}

function AjaxIncludeObj(url, id_obj) {
	
	var ajax = new AJAX();
	ajax.url = url;	
	ajax.metodo = "POST";
	ajax.retorno = function(texto) {
		document.getElementById(id_obj).value = texto;		
	}
	ajax.conectar();	
}

var loadedobjects = "";
function AjaxIncludeJsCss() {
	if (!document.getElementById)
		return
	for (i = 0; i < arguments.length; i++) {
		var file=arguments[i]
		var fileref=""
		if (loadedobjects.indexOf(file)==-1) { //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1) { //If object is a js file
				fileref=document.createElement('script')
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}
			else if (file.indexOf(".css")!=-1) { //If object is a css file
				fileref=document.createElement("link")
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file); 
			}
		}
		if (fileref!="") {
			document.getElementsByTagName("head").item(0).appendChild(fileref)
			loadedobjects += file+" " //Remember this object as being already added to page
		}
	}
}

function ExecutaScript(texto){
    var ini, pos_src, fim, codigo;
    var objScript = null;
    ini = texto.indexOf('<script', 0)
    while (ini!=-1){
        var objScript = document.createElement("script");
        pos_src = texto.indexOf(' src', ini)
        ini = texto.indexOf('>', ini) + 1;
        if (pos_src < ini && pos_src >=0){
            ini = pos_src + 4;
            fim = texto.indexOf('.', ini)+4;
            codigo = texto.substring(ini,fim);
            codigo = codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">","");
            objScript.src = codigo;
        }else{
            fim = texto.indexOf('</script>', ini);
            codigo = texto.substring(ini,fim);
            objScript.text = codigo;
        }
        document.body.appendChild(objScript);
        ini = texto.indexOf('<script', fim);
        objScript = null;
    }
}