/********************************************** AJAXV4 ******************************************/
/* En esta version de la clase ajax se ha separado la parte grafica de la parte de comunicacion */
/************************************************************************************************/
function AjaxV4(url,f,loading){  

// Funcionamiento general (xml)
// VARIABLES LOCALES

var intentos = 0
var self = this
var indicemodal = 0
var indicetooltip = 0

// PROPIEDADES
                
this.xmlDoc = null
this.url = url? url : null
this.f = f? f: null
this.loading = loading? loading : null
this.loaderror = null
this.running = false
this.post = null
this.id = Math.random()
this.nocache = false
this.timeOut = 0;
this.name = ''
                
// METODOS 
                
// Iniciamos el objeto
this.init = function()
{     
    if (window.XMLHttpRequest) {
       this.xmlDoc = new XMLHttpRequest()
       this.xmlDoc.onreadystatechange = check
    } 
    else if (window.ActiveXObject) {                                            
       this.xmlDoc = new ActiveXObject("Microsoft.XMLHTTP")
       if (this.xmlDoc)
          this.xmlDoc.onreadystatechange = check
    } 
    else
      ('Tu navegador no puede soportar el script')
}
                
// Ejecutamos
this.run = function(run_url,run_f,run_loading)
{
    // Si nos llegan  parámetros los recogemos
    this.url = run_url? run_url : this.url
    this.f = run_f? run_f: this.f
    this.loading = run_loading? run_loading : this.loading

    // Iniciamos objeto
    try{
        this.init()
    } catch(e){
       // ActiveX desactivado
	   mensaje = 'Aviso importante!!\nHemos detectado que tu navegador tiene desactivado el uso de controles '+
	   'ActiveX en la configuraci&oacute;n de seguridad, lo que te impedir&aacute; navegar con normalidad por este sitio. Para activarlo,'+
	   'debes seguir los siguiente pasos:\n'+
	   '\n1- Accede al men&uacute; Herramientas -> Opciones de Internet.'+
	   '\n2- Haz click en la pesta&ntilde;a Seguridad.'+
	   '\n3- Haz click en el bot&oacute;n Nivel personalizado...'+
	   '\n4- Dentro del men&uacute; Controles y complementos de ActiveX, en la primera opción Activar la secuencia de comandos de los controles de ActiveX marcados como seguros, seleccionar la opci&oacute;n Activar.'+
	   '\n5- Pincha en Aceptar en las dos ventanas.'+
	   '\n6- Cuando hayas terminado, pulsa la tecla F5 para volver a cargar la p&aacute;gina.'
	   (mensaje)
        return false
    }

    this.running = true
                               
    // Función cargando
    if (this.loading!=null)
         if(typeof(self.loading)=='function')
             self.loading()
         else
             eval(self.loading)
    eval(this.loading)
                               
   // Prevenimos cacheo con IE
   if (this.nocache){
      if(this.url.indexOf('?')>0)
          this.url+='&ajax-random='+Math.random()
      else
          this.url+='?ajax-random='+Math.random()
   }
                               
   // Cargamos la URL
   //try{
      this.xmlDoc.open((this.post==null?'GET':'POST'), this.url, true);
      if(this.post!=null)
            this.xmlDoc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1')
      this.xmlDoc.send(this.post)
	  if (this.timeOut > 0)
	  {
	  	this.t = setTimeout('window.' + this.name + '.abortar()', this.timeOut)
	  }
      this.post=null
   /*}
   catch(e){
      checkerror()
   }*/
}

this.abortar = function()
{
	this.xmlDoc.onreadystatechange = function(){};
	this.xmlDoc.abort()
	checkerror()
	this.xmlDoc.onreadystatechange = check
}
                
// FUNCIONES PRIVATE                
// Chequeamos el resultado
function check() 
{
     if (self.xmlDoc.readyState==4)
	 {
         if (self.xmlDoc.status==200)
		 {
			 if (self.timeOut > 0)
			 	clearTimeout(eval('window.' + self.name + '.t'))
             self.running = false
             if(typeof(self.f)=='function')
                 self.f()
             else
                 eval(self.f)
             intentos = 0
         }
		 else checkerror()
	 }     
}

function checkerror()
{
    self.running = false
    if(self.loaderror!=null)
        if(typeof(self.loaderror)=='function')
             self.loaderror()
        else
             eval(self.loaderror)
    else
         if(intentos < 3){
             if(confirm('Ha ocurrido un error de comunicación con el servidor. ¿Desea reintentar?')){
                  intentos ++
     		      self.run()
			 }
             else{
                  intentos = 0
                  ('ERROR: No se ha podido establecer comunicación con el servidor.');
         	 }
         }
         else{
             intentos = 0
             ('Error de comunicación con el servidor.\nPor favor, vuelva a intentarlo pasados unos minutos.');
         }
                               
    return;
}
// Fin de la clase AjaxV4
}
