var _GLOBAL_AJAX 	= null;
var _DELEGADO 		= null;
function AjaxCall(){
	
	//declaracion de variables
	
	this.version 		= "1.0";
	this.ajax 			= null;
	this.div_info 		= null;
	this.mensajeExito 	= "";
	this.url 			= "";
	this.directorio_imagenes = "";	
	this.parametros 	= "";
	this.metodo 		= "GET";
	this.delegado 		= null;	
	this.formulario 	= "";
	this.funcionCallBack= null;
	this.responseText 	= "";
		
	/*this.id_intervalo = 20000;
	this.imagen = "wait.gif";
	this.auto_close = false;	
	this.showTicket = true;*/
	
	//declaracion de funciones	
	this.setVersion 		= setVersion;
	this.getVersion 		= getVersion;
	this.setImageDirectory 	= setImageDirectory;
	this.getImageDirectory 	= getImageDirectory;
	this.setMensajeExito 	= setMensajeExito;
	this.getMensajeExito 	= getMensajeExito;
	this.obtenerDatosPOST	= obtenerDatosPOST; 
	this.printMessage		= printMessage;
	this.setDivOutput		= setDivOutput;
	this.getDivOutput 		= getDivOutput;
	this.setURL				= setURL;
	this.getURL				= getURL;
	this.setParametros		= setParametros;
	this.getParametros		= getParametros;
	this.setMetodo 			= setMetodo;
	this.getMetodo			= getMetodo;
	this.setDelegado		= setDelegado;
	this.getDelegado		= getDelegado;
	this.runDelegado		= runDelegado;
	this.Llamar				= Llamar;
	this.setFormulario		= setFormulario;
	this.getFormulario		= getFormulario;
	this.validaParametros	= validaParametros;
	this.setFuncionCallBack = setFuncionCallBack;
	this.getFuncionCallBack = getFuncionCallBack;	
	this.callFuntionCallBack = callFuntionCallBack;
	this.DefaultFunctionCallBack	= DefaultFunctionCallBack;	
	this.procesoInterno 	= procesoInterno;
	this.setResponseText	= setResponseText;
	this.getResponseText	= getResponseText;		
	this.setAjax			= setAjax;
	this.getAjax			= getAjax;	
	this.useDefaultHandler 	= useDefaultHandler;
	
}
function useDefaultHandler(){
	this.setFuncionCallBack(this.DefaultFunctionCallBack);	
}
function procesoInterno(){
	
	if( window.XMLHttpRequest ){
        _GLOBAL_AJAX  = new XMLHttpRequest(); // No Internet Explorer
    }else{
        _GLOBAL_AJAX  =  new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer
    }
	//this.setAjax(obj);
	//alert(_GLOBAL_AJAX);
	this.getAjax().onreadystatechange = this.funcionCallBack;   
	//alert(parametros_GET + " URL: " + url );
    d = new Date();
    //alert(url + parametros_GET);
	parametros_GET 	= "";
	parametros_POST = "";
	
	if(this.getMetodo()=="GET" || this.getMetodo()=="get"){
		parametros_GET = this.getParametros(); 		
	}else{
		parametros_POST = this.getParametros();	
	}	
   //alert(this.getMetodo());
	//alert(this.getURL() + parametros_GET);
	//alert(parametros_POST);	
	//alert( this.funcionCallBack);
	//alert("get ajax: " + this.getAjax());
	str_url = this.getURL() + "?" + d.getTime() + "&" + parametros_GET;
	//alert(str_url);
	this.getAjax().open(this.getMetodo(),str_url,true);
    this.getAjax().setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1"); 
    //alert(parametros_POST);
    this.getAjax().send(parametros_POST); 
	
}
function setResponseText(s){ this.responseText = s;}
function getResponseText(){	return this.responseText;}

function DefaultFunctionCallBack(){
	
	//alert(this.ajax);
	//alert("AJAX OBJ: " + this.getAjax());
	//alert(ajax);
	if(this.getAjax().readyState == 1){		
       //iniciando
	  // this.setResponseText("Iniciando proceso");
    }
    if(this.getAjax().readyState == 4){
		// listo
		isError = false;
		
		
        if( this.getAjax().status == 200 ){
			//mensaje normal
            resultado = this.getAjax().responseText;
			this.setResponseText("Mensaje listo: " + resultado);

            vec = resultado.split("&");
            if(resultado.indexOf("error") != -1){
              //cuando hay errores
			  this.setResponseText(vec[1]);
			  isError = true;
            }
            if(resultado.indexOf("ok") != -1){
                // cuando es exito
				this.setResponseText(vec[1]);	
				isError = false;
            }            
        }
        if( this.getAjax().status == 404 ){
          this.setResponseText("La pagina no fue encontrada: " + this.getURL());
		  isError= true;
        } 
        if(this.getAjax().status == 500 ){
           this.setResponseText("Error al procesar");
		   isError = true;
        }
		this.runDelegado(this.getResponseText(),isError);
    }	
}

function setFuncionCallBack(f){ this.funcionCallBack = f;}
function getFuncionCallBack(){ 
	if(this.funcionCallBack==undefined || this.funcionCallBack==null || this.funcionCallBack==""){
		setFuncionCallBack(this.DefaultFunctionCallBack);			
	}	
	return this.funcionCallBack;
}
function Llamar(){
	if(this.getURL()==undefined){
		this.showError("No esta establecida la URL");
		return;
	}
	if(this.validaParametros()){
		this.procesoInterno();
	}
}
function validaParametros(){
	method = this.getMetodo();
	param = this.getParametros();
	if(method=="GET" || method=="get"){
		if(param==undefined || param==""){
			showError("No se enviaron parametros GET");
			return false;
		}
	}else if(method=="POST" || method=="post"){
		//alert(this.obtenerDatosPOST(this.getFormulario()));
		this.setParametros(this.obtenerDatosPOST(this.getFormulario()));
		param = this.getParametros();
		//alert(this.getParametros());
		if(param==undefined || param==""){
			showError("No se enviaron parametros POST");
			return false;
		}		
	}else{
		showError("No se establecio el metodo de envio correctamente");
		return false;
	}
	//alert(this.getMetodo());
	//alert(this.getURL() );
	return true;
}
function showError(causa){
	alert("No se puede llamar a procedimiento remoto.("+causa+")");	
}
function setAjax(a){ this.ajax = a;}
function getAjax(){ return _GLOBAL_AJAX ;}
function setFormulario(f){ this.formulario = f;}
function getFormulario(){ return this.formulario;}
function setDelegado(d){ _DELEGADO = d;}
function getDelegado(){ return _DELEGADO;}
function setMetodo(m){ this.metodo = m;}
function getMetodo(){ return this.metodo; }
function setParametros(param){ this.parametros = param;}
function getParametros(){ return this.parametros; }
function setMensajeExito(mensaje){ this.mensajeExito = mensaje;}
function getMensajeExito(mensaje){ return this.mensajeExito;}
function setImageDirectory(dir){ this.directorio_imagenes = dir;}
function getImageDirectory(){ return this.directorio_imagenes;}
function setVersion(v){ 	this.version = v; }
function getVersion(v){ return this.version; }
function obtenerDatosPOST(form_id){
	//var Formulario = document.getElementById(form_id); 
	var Formulario = form_id;
	
    var longitudFormulario = Formulario.elements.length; 
    var cadenaFormulario = ""; 
    var sepCampos; 
    sepCampos = "" ;
	
    for (var i=0; i <= Formulario.elements.length-1;i++) { 
        //alert(Formulario.elements[i].name + " " + Formulario.elements[i].type);
        if(Formulario.elements[i].type=="checkbox" || Formulario.elements[i].type=="radio"){
            if(Formulario.elements[i].checked){
                cadenaFormulario += sepCampos+Formulario.elements[i].name+'=true';
            }else{
                cadenaFormulario += sepCampos+Formulario.elements[i].name+'=false';
            }
        }else if(Formulario.elements[i].type=="select-one"){
            sel= Formulario.elements[i];            
            for(cont = 0;cont < sel.length; cont++){
                //alert(sel.options[cont].value + " " + sel.options[cont].text);
                cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(sel.options[cont].value); 
            }
        }else{
            cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value); 
        }
        sepCampos="&"; 
    }
	//alert("cadena form: " + cadenaFormulario);
    return cadenaFormulario;
}
function setDivOutput(div){ this.div_info = div;}
function getDivOutput(){ return this.div_info;}
function printMessage(str){
    getDivOutput().style.display  = "block";
    getDivOutput().innerHTML      = "<b>"+str + "</b>"; 
}
function setURL(new_url){this.url = new_url;}
function getURL(){ return this.url;}
function runDelegado(responseText,isError){	
	this._DELEGADO(responseText,isError);
}
function callFuntionCallBack(){	
	alert("FUNCTION DEPRECATED");
	this.funcionCallBack();
}
function printTo(div,texto){
	div.style.display  = "block";
    div.innerHTML      = "<b>"+ texto + "</b>"; 	
}
function printFormatTo(div,texto,isError,image_dir){
    if(this.getImageDirectory()==undefined){
        this.setImageDirectory(image_dir);
    }   
	div.style.display  = "block";
	if(!isError){
    	div.innerHTML      = "<b><img src='"+ this.getImageDirectory() +"ok_small.gif' alt='Ok' width='13' height='13' /> "+ texto + "</b>"; 
	}else{
		div.innerHTML      = "<b><img src='"+ this.getImageDirectory() +"critical_medium.gif' alt='Ok' width='13' height='13' /> "+ texto + "</b>"; 
	}
}