var objRetId=false;
var objRet;
var removeFistOption;
var innerHTMLAntes=false;
var innerHTMLDepois=false;

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try{//Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{	
		try{// Internet Explorer
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function ajaxHTML(url, ret, antes, depois)
{
	objRetId=TRIM(ret);
	innerHTMLAntes=antes;
	innerHTMLDepois=depois;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Seu navegador não suporta AJAX!");
		return;
	}
	xmlHttp.onreadystatechange=stateChangedHTML;
	xmlHttp.open("GET",TRIM(url),true);
	xmlHttp.send(null);
}

function stateChangedHTML()
{
	var retorno="";
	if (xmlHttp.readyState==4)
	{
		if (objRetId) {
			if (innerHTMLAntes) {
				retorno = retorno+innerHTMLAntes;
				innerHTMLAntes = false;
			}
			retorno = retorno+xmlHttp.responseText;
			if (innerHTMLDepois) {
				retorno = retorno+innerHTMLDepois;
				innerHTMLDepois = false;
			}
			document.getElementById(objRetId).innerHTML = retorno;
		}
		objRetId = false;
	}
}

function ajaxCMB(url,obj,limpa,textoLimpa)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Seu navegador não suporta AJAX!");
		return;
	}
	objRet=obj;
	objRet.disabled=true
	if (limpa) {
		removeSelectOptions(objRet);
		op=document.createElement('option');
		op.value="";
		op.text=textoLimpa;
		try{
			objRet.add(op,null); // standards compliant
		}
		catch(ex){
			objRet.add(op); // IE only
		}
		removeFistOption=true;
	}
	
	xmlHttp.onreadystatechange=stateChangedCmb;
	xmlHttp.open("GET",TRIM(url),true);
	xmlHttp.send(null);
}

function stateChangedCmb()
{
	if (xmlHttp.readyState==4) {
		if (xmlHttp.status==200) {
			var xmlDoc = xmlHttp.responseXML;
			if (xmlDoc.hasChildNodes()) {
				var nos = xmlDoc.getElementsByTagName('opcao');
				if (removeFistOption)
 					objRet.remove(0);
				for(var i=0;i<nos.length;i++) {
					var no = nos[i];
					var valor = no.childNodes[0].firstChild.nodeValue;
					var texto = no.childNodes[1].firstChild.nodeValue;
					op=document.createElement('option');
					op.value = valor;
					op.text = texto;
					try{
						objRet.add(op,null); // standards compliant
					}
					catch(ex){
						objRet.add(op); // IE only
					}
				}
			}
			objRet.disabled=false;
		}else{
			alert("Erro no retorno do servidor "+xmlHttp.statusText);
		}
	}
}

function removeSelectOptions(sel)
{
	for(i=(sel.length-1);i>=0;i--)
		sel.remove(i)
}

function TRIM(v)
{
	return v.replace(/^\s*/, "").replace(/\s*$/, "");
}

function redireciona(e){
	window.location = e
}

function ajaxJS(url)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Seu navegador não suporta AJAX!");
		return;
	}
	xmlHttp.onreadystatechange=stateChangedJS;
	xmlHttp.open("GET",TRIM(url),true);
	xmlHttp.send(null);
}

function stateChangedJS()
{
	var retorno="";
	if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status==200) {
			var retorno = xmlHttp.responseText;
			eval(retorno)
		}
	}
}