/**
 *
 * CBOV Framework, objects for development in php
 * Copyright (C) 2005 Cassiano B.O Velloso
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * You can contact Cassiano B.O Velloso by the e-mail cbovATcbovDOTorg 
 * 
 * 01 November of 2005, Cassiano B.O Velloso
 *
 */
 /*
  * Funções para fazer requisições HTTP via javascript e imprimir em objetos HTML
  *
  */
function cbAjaxHtmlDiv(objHtml, url)
{
	try
	{
		var ajaxHttpObj = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var ajaxHttpObj = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var ajaxHttpObj = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var ajaxHttpObj = false
			}
		}
	}

	alertaErros=false
//	ajaxHttpObj.open("GET", url,true);
	ajaxHttpObj.open("GET", url, true);

	var aux;

	if(aux=document.getElementById(objHtml))
	{
		aux.innerHTML='<img src="img/carregando.gif">';
		ajaxHttpObj.onreadystatechange=function() {
		if (ajaxHttpObj.readyState==4)
		{
			var texto=ajaxHttpObj.responseText
			texto=texto.replace(/(\n|\r|\+)/g," ");
			aux.innerHTML = texto;
		}
	}
	
	try
	{
		ajaxHttpObj.send(null)
	}
	catch(E)
	{
		var texto="<A href=\"javascript:void(0)\" ONCLICK=\"javascript:cbAjaxHtmlDiv('"+objHtml+"','"+url+"')\">Ocorreu algum erro ao carregar esta tela, clique aqui para tentar carregar denovo</A>"
		texto=texto.replace(/\+/g," ")
		texto=unescape(texto)
		aux.innerHTML = texto
	}
	
	return false
	}
	return false
}

function exeForm(local,url,fields)
{
	alertaErros=false
	url=url+'?'
	for(var i=0;i<fields.length;i++)
	{
		if (fields[i].name != ""){
			if((fields[i].type == 'radio' || fields[i].type == 'checkbox') && fields[i].checked)
			{
				url=url+fields[i].name+'='+encodeURI(fields[i].value)+'&';
			}
			else
			{
				if(fields[i].type != 'submit' && fields[i].type != 'reset' && fields[i].type!='radio' && fields[i].type!='checkbox')
					url=url+fields[i].name+'='+encodeURI(fields[i].value)+'&';
			}
		}
	}
	return cbAjaxHtmlDiv(local, url)
}



function openAjax() {
	var Ajax;
	try {
		Ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers mais populares, como: Firefox, Safari, dentre outros.
	}catch(ee){
		try {
			Ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
		}catch(e){
			try {
				Ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
			}catch(e){Ajax = false;}
		}
	}
	return Ajax;
}


function extraiScript(texto){
//Maravilhosa função feita pelo SkyWalker.TO do imasters/forum
//http://forum.imasters.com.br/index.php?showtopic=165277&
        // inicializa o inicio ><
        var ini = 0;
        // loop enquanto achar um script
        while (ini!=-1){
                // procura uma tag de script
                ini = texto.indexOf('<script', ini);
                // se encontrar
                if (ini >=0){
                        // define o inicio para depois do fechamento dessa tag
                        ini = texto.indexOf('>', ini) + 1;
                        // procura o final do script
                        var fim = texto.indexOf('</script>', ini);
                        // extrai apenas o script
                        codigo = texto.substring(ini,fim);
                        // executa o script
                        //eval(codigo);
                        /**********************
                        * Alterado por Micox - micoxjcg@yahoo.com.br
                        * Alterei pois com o eval não executava funções.
                        ***********************/
                        novo = document.createElement("script")
                        novo.text = codigo;
                        document.body.appendChild(novo);
                }
        }
}
function carregaAjax(div, getURL) {
	
	document.getElementById(div).style.display = "block";
		
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
		
		var exibeResultado = document.getElementById(div); // div que exibirá o resultado.
		
		var Ajax = openAjax(); // Inicia o Ajax.
		
		Ajax.open("GET", getURL, true); // fazendo a requisição
		
		Ajax.onreadystatechange = function(){
										if(Ajax.readyState == 1) { // Quando estiver carregando, exibe: carregando...
													exibeResultado.innerHTML = '<font style="font-family:Arial; font-size:12px;color:#060; font-weight:bold">Carregando... Aguarde!</font>';
										}
										if(Ajax.readyState == 4) { // Quando estiver tudo pronto.
											if(Ajax.status == 200) {
												var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa variável
												resultado = resultado.replace(/\+/g,""); // Resolve o problema dos acentos (saiba mais aqui: 												http://www.plugsites.net/leandro/?p=4)
//resultado = resultado.replace(/ã/g,"a");
												resultado = unescape(resultado); // Resolve o problema dos acentos
												exibeResultado.innerHTML = resultado;
												extraiScript(resultado);
												//editor();
											} else {
												exibeResultado.innerHTML = "<font class='texto_11'>Por favor, tente novamente!</font>";
											}
										}
									}
		Ajax.send(null);
		//editor();// submete
	}
}

function carregaConteudo(div, getURL) {
	
	document.getElementById(div).style.display = "block";
		
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
		
		var exibeResultado = document.getElementById(div); // div que exibirá o resultado.
		
		var Ajax = openAjax(); // Inicia o Ajax.
		
		Ajax.open("GET", getURL, true); // fazendo a requisição
		
		Ajax.onreadystatechange = function(){
										if(Ajax.readyState == 1) { // Quando estiver carregando, exibe: carregando...
											exibeResultado.innerHTML = '<table width="100%" height="300" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td bordercolor="#CCCCCC" align="center" valign="middle"></td></tr></table>';
											document.getElementById('div_fundo').style.display = 'block';
										}
										if(Ajax.readyState == 4) { // Quando estiver tudo pronto.
											if(Ajax.status == 200) {
												var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa variável
												resultado = resultado.replace(/\+/g,""); // Resolve o problema dos acentos (saiba mais aqui: 												http://www.plugsites.net/leandro/?p=4)
//resultado = resultado.replace(/ã/g,"a");
												resultado = unescape(resultado); // Resolve o problema dos acentos
												exibeResultado.innerHTML = resultado;
												setTimeout("document.getElementById('div_fundo').style.display = 'none'",1000);
												
												extraiScript(resultado);
												//editor();
											} else {
												exibeResultado.innerHTML = "<font class='texto_11'>Por favor, tente novamente!</font>";
											}
										}
									}
		Ajax.send(null);
		//editor();// submete
	}
}

function carregaAjax2(div, getURL) {
	
	document.getElementById(div).style.display = "block";
		
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
		
		var exibeResultado = document.getElementById(div); // div que exibirá o resultado.
		
		var Ajax = openAjax(); // Inicia o Ajax.
		
		Ajax.open("GET", getURL, true); // fazendo a requisição
		
		Ajax.onreadystatechange = function(){
										if(Ajax.readyState == 1) { // Quando estiver carregando, exibe: carregando...
											//exibeResultado.innerHTML = "<div align=\"center\"  ><img src=\"../../img/ajax.gif\" align=\"absmiddle\"></div";
										}
										if(Ajax.readyState == 4) { // Quando estiver tudo pronto.
											if(Ajax.status == 200) {
												var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa variável
												resultado = resultado.replace(/\+/g,""); // Resolve o problema dos acentos (saiba mais aqui: 												http://www.plugsites.net/leandro/?p=4)
//resultado = resultado.replace(/ã/g,"a");
												resultado = unescape(resultado); // Resolve o problema dos acentos
												exibeResultado.innerHTML = resultado;
												extraiScript(resultado);
												//editor();
											} else {
												//exibeResultado.innerHTML = "<font class='texto_11'>Por favor, tente novamente!</font>";
											}
										}
									}
		Ajax.send(null);
		//editor();// submete
	}
}





