
function AJAXRequest(url,parameters,callback_function,object_name,return_xml){
	var http_request=false;
	if(window.XMLHttpRequest){
		http_request=new XMLHttpRequest();
		if(http_request.overrideMimeType){
			http_request.overrideMimeType('text/xml');
		}
	} else if(
		window.ActiveXObject){
			try{
				http_request=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					http_request=new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
				}
			}
		}
	if(!http_request){alert('Browser doesn\'t support this feature.');
		return false;
	}
	http_request.onreadystatechange=function(){
		if(http_request.readyState==1&&(object_name=="principal"||object_name=="conteudo"||object_name=="EntregaDados")){
			eval(callback_function+'("<table height=300 width=100% border=0 valign=center><tr align=center><td align=center class=texto_cinza><img src=\'/Comuns/Imagens/ajaxloader.gif\' border=0><br><br>  Carregando ...</td></tr></table>","'+object_name+'")');
		} else if(http_request.readyState==4){
			if(http_request.status==200){
				if(return_xml){
					eval(callback_function+'(http_request.responseXML,"'+object_name+'", "'+url+'")');
				} else {
					eval(callback_function+'(http_request.responseText,"'+object_name+'")');
				}
			} else if(http_request.status==404) { // Page-not-found error
                alert('Error: Not Found. The requested URL ' + url + ' could not be found.');

			} else if(http_request.status==500) {  // Display results in a full window for server-side errors
				//alert(http_request.responseText);
				handleErrFullPage(http_request.responseText);
			} else if(http_request.status==122) {  // Display results in a full window for server-side errors
				//alert(http_request.responseText);
				handleErrFullPage(http_request.responseText+'<br>'+'Error Code: '+http_request.status+' <br>'+parameters);
			} else {
				alert('****There was a problem with the request.(Code: '+http_request.status+')');
//				handleErrFullPage('Error Code: '+http_request.status+': '+parameters);
				eval(callback_function+'("&nbsp;","'+object_name+'")');
			}
		}
	}
	parameters=URLEncode(parameters);
	http_request.open("POST",url+"?"+parameters,true);
	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length",parameters.length);
	http_request.send(parameters);
}
// http://www.onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html
function handleErrFullPage(strIn) {

        var errorWin;

        // Create new window and display error
        try {
                errorWin = window.open('', 'errorWin');
                errorWin.document.body.innerHTML = strIn;
        }
        // If pop-up gets blocked, inform user
        catch(e) {
                alert('An error occurred, but the error message cannot be' +
                        ' displayed because of your browser\'s pop-up blocker.\n' +
                        'Please allow pop-ups from this Web site.');
        }
}

function AJAXRequestAdmin(url,parameters,callback_function,object_name,return_xml)
{
	var http_request=false;
	if(window.XMLHttpRequest){
		http_request=new XMLHttpRequest();
		if(http_request.overrideMimeType){
			http_request.overrideMimeType('text/xml');
			}
			}else if(window.ActiveXObject){
				try{http_request=new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					try{
						http_request=new ActiveXObject("Microsoft.XMLHTTP");
						}catch(e){}
						}
						}
if(!http_request){
	alert('Browser doesn\'t support this feature.');
	return false;
	}
http_request.onreadystatechange=function(){
	if(http_request.readyState==1){
		eval(callback_function+'("Processando as informaçoes, aguarde ...","'+object_name+'")');
		}else if(http_request.readyState==4){
			if(http_request.status==200){
			if(return_xml){
				eval(callback_function+'(http_request.responseXML,"'+object_name+'", "'+url+'")');
				}else{
					eval(callback_function+'(http_request.responseText,"'+object_name+'")');
					}
					} else if(http_request.status==404) { // Page-not-found error
                alert('Error: Not Found. The requested URL ' + url + ' could not be found.');

			} else if(http_request.status==500) {  // Display results in a full window for server-side errors
				alert('Error 500');
				handleErrFullPage(http_request.responseText);
					}else{
						alert('***There was a problem with the request.(Code: '+http_request.status+')');eval(callback_function+'("&nbsp;","'+object_name+'")');
						}
						}
						}
parameters=URLEncode(parameters);
http_request.open("POST",url+"?"+parameters,true);
http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length",parameters.length);
http_request.send(parameters);
}

function FillHTML(xmlDom,ObjetoID){
	if(getObj(ObjetoID)!=null){
		getObj(ObjetoID).innerHTML=xmlDom;ExtraiScript(xmlDom);
		}
}
function URLEncode(url){
	var SAFECHARS="0123456789"+"ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvwxyz"+"-_.!~*'()=&";
	var HEX="0123456789ABCDEF";
	var plaintext=url;
	var encoded="";
	for(var i=0;i<plaintext.length;i++){
		var ch=plaintext.charAt(i);
		if(ch==" "){
			encoded+="+";
			}else if(SAFECHARS.indexOf(ch)!=-1){
				encoded+=ch;
				}else{
					var charCode=ch.charCodeAt(0);
					if(charCode>255){
						alert("Char '"+ch+"' cannot be encoded.\n");
						encoded+="+";
						}else{
							encoded+="%";
							encoded+=HEX.charAt((charCode>>4)&0xF);
							encoded+=HEX.charAt(charCode&0xF);
							}
					}
				}
return encoded;
}

function URLDecode(url)	{
	var HEXCHARS="0123456789ABCDEFabcdef";
	var encoded=url;
	var plaintext="";
	var i=0;
	while(i<encoded.length){
		var ch=encoded.charAt(i);
		if(ch=="+"){
			plaintext+=" ";i++;
		} else if(ch=="%"){
			if(i<(encoded.length-2)&&HEXCHARS.indexOf(encoded.charAt(i+1))!=-1&&HEXCHARS.indexOf(encoded.charAt(i+2))!=-1){
				plaintext+=unescape(encoded.substr(i,3));
				i+=3;
			} else {
				alert('Bad escape at ...'+encoded.substr(i));
				plaintext+="%[ERROR]";
				i++;
			}
		} else {
			plaintext+=ch;
			i++;
		}
	}
	return plaintext;
}
function GetFormFields(idForm){
	var elementosFormulario=getObj(idForm).elements;
	var qtdElementos=elementosFormulario.length;
	var queryString="";
	var elemento;
	this.ConcatenaElemento=function(nome,valor){
		if(queryString.length>0){
			queryString+="&";
		}
		queryString+=nome+"="+valor;};
		for(var i=0;i<qtdElementos;i++){
			elemento=elementosFormulario[i];
			if(!elemento.disabled){
				switch(elemento.type){
					case'text':
					case'password':
					case'hidden':
					case'textarea':
						this.ConcatenaElemento(elemento.name,elemento.value);
						break;
					case'select-one':
						if(elemento.selectedIndex>=0){
							this.ConcatenaElemento(elemento.name,elemento.options[elemento.selectedIndex].value);
						}
						break;
					case'select-multiple':
						for(var j=0;j<elemento.options.length;j++){
							if(elemento.options[j].selected){
								this.ConcatenaElemento(elemento.name,elemento.options[j].value);
							}
						}
						break;
					case'checkbox':
					case'radio':
						if(elemento.checked){
							this.ConcatenaElemento(elemento.name,elemento.value);
						}
						break;
				}
			}
		}
	return queryString;
}

// Funçao corrigida para codificar caracteres especiais
function GetFormFieldsNovo(idForm){
	var elementosFormulario=getObj(idForm).elements;
	var qtdElementos=elementosFormulario.length;
	var queryString="";
	var elemento;
	this.ConcatenaElemento=function(nome,valor){
		if(queryString.length>0){
			queryString+="&";
		}
		queryString+=nome+"="+valor;};
		for(var i=0;i<qtdElementos;i++){
			elemento=elementosFormulario[i];
			if(!elemento.disabled){
				switch(elemento.type){
					case'text':
					case'password':
					case'hidden':
					case'textarea':
						this.ConcatenaElemento(elemento.name,escape(elemento.value));
						break;
					case'select-one':
						if(elemento.selectedIndex>=0){
							this.ConcatenaElemento(elemento.name,escape(elemento.options[elemento.selectedIndex].value));
						}
						break;
					case'select-multiple':
						for(var j=0;j<elemento.options.length;j++){
							if(elemento.options[j].selected){
								this.ConcatenaElemento(escape(elemento.name,elemento.options[j].value));
							}
						}
						break;
					case'checkbox':
					case'radio':
						if(elemento.checked){
							this.ConcatenaElemento(elemento.name,escape(elemento.value));
						}
						break;
				}
			}
		}
	return queryString;
}


function ExtraiScript(texto){
	var ini,pos_src,fim,codigo,texto_pesquisa;
	var objScript=null;

	texto_pesquisa=texto.toLowerCase();
	ini=texto_pesquisa.indexOf('<script',0);
	while(ini!=-1){
		var objScript=document.createElement("script");
		pos_src=texto_pesquisa.indexOf(' src',ini);
		ini=texto_pesquisa.indexOf('>',ini)+1;
		if(pos_src<ini&&pos_src>=0){
			ini=pos_src+4;
			fim=texto_pesquisa.indexOf('.',ini)+4;
			codigo=texto.substring(ini,fim);
			codigo=codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">","");
			objScript.src=codigo;
		} else {
			fim=texto_pesquisa.indexOf('</script>',ini);
			codigo=texto.substring(ini,fim);
			objScript.text=codigo;
		}
		document.body.appendChild(objScript);
		ini=texto.indexOf('<script',fim);
		objScript=null;
	}
}

