//  ------------------------------------------------------------------------------
//  S   O   U   R   C   E   L   A   B       T   E   C   N   O   L   O   G   I   A
//  ------------------------------------------------------------------------------
//
//                  S   L   A   J   A   X      C   L   A   S   S  
//					
//  			Este Script foi Desenviolvido por Marino Boscolo Neto.
//  			   Duvidas ou sugestões: marino@sourcelab.com.br
//  ------------------------------------------------------------------------------
// -----------------------------------------------------------
// S  O  U  R  C  E  L  A  B      T  E  C  N  O  L  O  G  I  A
// -----------------------------------------------------------
// Versão: 1.01
// Criado em: 09/07/2007 16:03:51
// Por: Alex Castilho
// -----------------------------------------------------------
// Este Script é licenciado e distribuido pelas normas GNU/GPL
// Acesse o site: http://www.sourcelab.com.br/softwarelivre
// -----------------------------------------------------------


//  DECLARE THE FRAMEWORK VARIABLES -------------------------------------------------------------
var ajax_loading_target = 'statusbar';
// [string] - The target id of preloading page while steps the readystate of object
var ajax_mouse_efx = false;
// [bool] - the effect of mouse wjile steps de readystate of object (in flash)
var ajax_loading_container;
// [bool] - The div witch contain the preload string
var ajax_loading_str;
// [string] - Opitional text separated by comma tha is used in place of ajax_loading_str_splited
var ajax_loading_str_splited = new Array('Aguarde... Conectando ao Servidor','Aguarde... Enviando Dados','Aguarde... Recebendo Resposta','Operação Efetuada.'); 
// [array] - Text of original preload string
var content_target = null;			
// [string] - The target id of object that will get the requast result
var content_target_increment; 
// [bool] - set true to increment the target inner html content
var ajax_callback_function = 'getScripts(texto)';
// [string] - set a list separated by comma of functions that will be called when readystate is 4
var sto;
// 	---------------------------------------------------------------------------------------------

//  DECLARE THE MAIN FUNCTION -------------------------------------------------------------------
function JAjax(){
	
	this.xmlhttp = false;
	this.file 		= false;
	this.param 		= null;
	this.metodo 	= 'GET';
	this.Request 	= function(){
		try{this.xmlhttp 			= new XMLHttpRequest();}
		catch(ee){try{this.xmlhttp 	= new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){try{this.xmlhttp 	= new ActiveXObject("Microsoft.XMLHTTP");}
		catch(E){this.xmlhttp 		= false;}	}}
		return req 					= this.xmlhttp;
	}
	this.CallBack 		= function(c){if(c==null){ ajax_callback_function = 'getScripts(texto)'; } else { ajax_callback_function = ajax_callback_function+','+c; } }
	this.SetMetodo 		= function(m){this.metodo = m;}
	this.SetFile 		= function(f){this.file = f;}
	this.SetParam 		= function(p,m){this.param = p;}
	this.SetLoading 	= function(r,w){ajax_loading_target = r; ajax_loading_container = w;}
	this.SetMouseEFX 	= function(g){ajax_mouse_efx = g;}
	this.SetStrLoading 	= function(ls){ajax_loading_str = ls;}
	this.SetTarget 		= function(t,cc){content_target = t; content_target_increment = cc;}
	this.Open = function(){
		req.abort();
		req.open(this.metodo,this.file,true);
		if (this.metodo == 'POST'){req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");}
		if (ajax_loading_str){
			ajax_loading_str_splited = ajax_loading_str.split(',');
		}				
		if (ajax_loading_target != 'statusbar'){
			var loading_place = document.getElementById(ajax_loading_target);
			if (ajax_loading_container == true){
				loading_place.style.visibility="visible";
				loading_place.filter="alpha(opacy=20)";
			}
		} 
		req.onreadystatechange=function() {
			if (ajax_mouse_efx){
				mousefx.style.display = "";
				//alert(mousefx.style.display);
			}
			if (ajax_loading_target != 'statusbar'){
				if (req.readyState==1){
					loading_place.innerHTML=ajax_loading_str_splited[0];
				}
				if (req.readyState==2){
					loading_place.innerHTML=ajax_loading_str_splited[1];
				}
				if (req.readyState==3){
					loading_place.innerHTML=ajax_loading_str_splited[2];
				}
			} else {
				if (req.readyState==1){
					window.status=ajax_loading_str_splited[0];
				}
				if (req.readyState==2){
					window.status=ajax_loading_str_splited[1];
				}
				if (req.readyState==3){
					window.status=ajax_loading_str_splited[2];
				}
			}
			if (req.readyState==4){
				if (ajax_loading_target){
					if (ajax_mouse_efx){
						mousefx.style.display = "none";
						//alert(mousefx.style.display);
					}
					if (ajax_loading_target != 'statusbar'){
						loading_place.innerHTML=ajax_loading_str_splited[3];
					} else {
						window.status=ajax_loading_str_splited[3];
					}
				}
				if (content_target != null) {
					var texto=req.responseText;
					var conteudo;
					//texto=texto.replace(/\+/g," ");
					texto=unescape(texto);
					if (typeof(content_target)=="string") conteudo=document.getElementById(content_target);
					else conteudo=content_target;
					if ((conteudo.type == "text") || (conteudo.type == "hidden") || (conteudo.type == "password") || (conteudo.type == "textarea") || (conteudo.type == "radio")) {
						if (content_target_increment){
							conteudo.value = conteudo.value + texto;
						} else {
							conteudo.value=texto;
						}
					} else {
						if (content_target_increment){
							conteudo.innerHTML += texto;
						} else {
							conteudo.innerHTML = texto;
						}
					}
					if (ajax_loading_container == true){
						loading_place.style.visibility = "hidden";
					}
					if (ajax_callback_function){
						var ajax_callback_function_splited = ajax_callback_function.split(',');
						for (i=0; i<ajax_callback_function_splited.length; i++){
							eval(ajax_callback_function_splited[i]);
						}
					}
				}
			}
		}
		req.send(this.param);
	}
	
	clearTimeout(sto);
	sto = setTimeout("window.status=' '",8000); 


}
function getScripts(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('</scr'+'ipt>', 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);
        }
    }
}

