//Componente AjaxGlobalnet
//**************************************************************
//Componente que utiliza los principios de Ajax para ejecutar un servicio Web
//funciona tanto en explorer como en Mozilla.
//**************************************************************
function AjaxGlobalnet(idAjax)
{
	try
	{
	this.requestHTTP= new this.CrearRequestHTTP();
	this.id=idAjax;	
	//variable que contiene la ruta donde se guardará el archivo
	this.servicio=new Object();
	this.servicio.nombre="";
	this.servicio.direccion="";
	this.servicio.parametros="";
	this.servicio.espacioNombres="http://www.i-globalnet.com/servicios";
	this.servicio.mensajeSOAP="";
	this.servicio.llamadaAsincrona=true;
	if(document.ajax==null)
		document.ajax=new Array();
	
	/*var siguiente=document.ajax.length;
	var k=0;
	for(k=0;k<siguiente;k++){
		if (document.ajax[k].liberada)
		{//si ya ha terminado y la hemos liberado
			siguiente=k;
			break;
		}
	}
	alert("longitud" + document.ajax.length);
	if (siguiente==document.ajax.length)
	{
		//document.ajax[siguiente]=this;
	}*/
	this.liberada=false;
	document.ajax[document.ajax.length]=this;
	
	}catch(e)
	{
		
	}
	
}




//Crea un Objeto XMLHttpRequest
AjaxGlobalnet.prototype.CrearRequestHTTP=function() 
{
	try
	{
		var httpRequest = null;
		
		if (window.XMLHttpRequest) 
		{ // Mozilla, Safari,...
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) 
			{
				httpRequest.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) 
		{ // IE
			try 
			{
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
				
			} catch (e) 
			{
				try 
				{
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		return httpRequest;
	}catch(e)
	{
		
	}
}

AjaxGlobalnet.prototype.EjecutarServicio=function(nombreServicio, direccionServicio, parametros,espacioNombres)
{
	try
	{
		if (nombreServicio)
			this.servicio.nombre=nombreServicio;
		if(direccionServicio)
			this.servicio.direccion=direccionServicio;
		if (parametros)
			this.servicio.parametros=parametros;
		if(espacioNombres)
			this.servicio.espacioNombres=espacioNombres;
		this.AlCambiarEstadoCarga(true);
		var req=this.requestHTTP;
		req.onreadystatechange = this.AlObtenerRespuesta;
		req.open('POST', this.servicio.direccion, this.servicio.llamadaAsincrona);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var parametros=String(parametros);
		parametros=parametros.replace(/\,/g,"&");
		req.send(parametros);
						
	}catch(e)
	{
		//alert(e.message);
		//window.clearInterval(intervalo)
	}
}

AjaxGlobalnet.prototype.AlObtenerRespuesta=function()
{
	try
	{
		var k=0;
		for(k=0;k<document.ajax.length;k++)
		{
			var liberada=document.ajax[k].liberada;
			var req=document.ajax[k].requestHTTP;
			//4=HTTP_REQUEST__STATE_COMPLETE
			if (req.readyState == 4)
			{
				
				//200=HTTP_REQUEST_STATUS_OK
				if ((req.status == 200) && (!document.ajax[k].liberada))
				{
					document.ajax[k].AlCambiarEstadoCarga(false);
					document.ajax[k].AlObtenerResultado();
					
				} else 
				{
					/*if(!document.ajax[k].liberada)
						alert('Invalid status: ' + req.status);*/
				}
				document.ajax[k].liberada=true;//una vez se ha completado, se libera
			}
			
		}
		
		
	}catch(e)
	{
		//alert(e.message);
	}
}

AjaxGlobalnet.prototype.AlCambiarEstadoCarga=function(iniciada)
{
	
}

AjaxGlobalnet.prototype.AlObtenerResultado=function()
{
	
}


//funciones que obtienen la hora de globalnet
//***************************************
//	utiliza las funciones ajax anteriores
//***************************************
/*Ejemplo de llamada
	ObtenerHora(nombreDivHora,nombrePais,true)
*/
	var ajaxHora=new Array();
	//variable que contiene los valores de los países.
	var idPaises=new Array();
		idPaises["espana"]=145;
		idPaises["argentina"]=7;
		idPaises["uruguay"]=168;
		idPaises["costarica"]=35;
		idPaises["republicadominicana"]=43;
		
	function ObtenerHora(idObj,idPais,mostrarSegundos)
	{
		if(!idPaises[idPais])
			return;
		if (!document.getAspElementById(idObj))
			return;
		
		var i="ajax"+idObj;
		ajaxHora[i]=new AjaxGlobalnet(i);
		
		//var urlServicioHora="http://localhost/pruebas/serviciosglobalnet.asmx/Hora";
		var urlServicioHora="http://www.i-globalnet.com/servicios/serviciosglobalnet.asmx/Hora";
		ajaxHora[i].div=document.getAspElementById(idObj);
		//idPais=idPais.toLowerCase();
		
		ajaxHora[i].idPais=parseInt(idPaises[idPais]);
		ajaxHora[i].mostrarSegundos=(mostrarSegundos==true);
		ajaxHora[i].AlCambiarEstadoCarga=AlCambiarEstado;
		ajaxHora[i].AlObtenerResultado=AlObtenerResultado;
		var parametros=new Array();
		parametros[0]="idpais=" +String(ajaxHora[i].idPais);
		//parametros[0]="idpais=5";
		//parametros[1]="idCentro=8";
		/*parametros[2]="idRec=145";*/
		ajaxHora[i].EjecutarServicio("Hora",urlServicioHora,parametros);	
	}
	
	function AlCambiarEstado(iniciada)
	{
		/*if(iniciada)
			ajaxHora[this.id].div.innerHTML="--:--";*/
		
	}
	function AlObtenerResultado()
	{
		var req=ajaxHora[this.id].requestHTTP
		var xmlDoc = req.responseXML;
		//alert(xmlDoc.documentElement.text);
		var texto="";
		
		/*if (xmlDoc.documentElement.text)
			texto=xmlDoc.documentElement.text;
		else
			texto=xmlDoc.responseText;*/
		texto=xmlDoc.documentElement.firstChild.nodeValue
		ajaxHora[this.id].div.innerHTML=texto;
		reloj=new RelojDigital(ajaxHora[this.id].div,ajaxHora[this.id].mostrarSegundos);
	}
	
	var reloj;
	function RelojDigital(div,mostrarSegundos)
	{
		this.div=div;
		this.div.reloj=this;
		this.mostrarSegundos=mostrarSegundos;
		var hora=this.div.innerHTML.split(":");
		
		if (hora.length>0)
			this.horas=parseInt(hora[0],10);
		else
			this.horas=0;
		if (hora.length>1)
			this.minutos=parseInt(hora[1],10);
		else
			this.minutos=0;
			
		if (hora.length>2)
			this.segundos=parseInt(hora[2]);
		else
			this.segundos=0;
		this.Mover();
		this.intervalo=window.setInterval("document.getAspElementById('"+div.id+"').reloj.Mover()",1000);
		
	}
	
	RelojDigital.prototype.Mover=function()
	{
	
		//alert("hora");
		//alert(this.segundos);
		this.segundos+=1;
		
		if (this.segundos>=60)
		{
			this.segundos=0;
			this.minutos+=1;
			if (this.minutos>=60)
			{
				this.minutos=0;
				this.horas+=1;
				if (this.horas>=24)
					this.horas=0;
			}
		}
		
		var horas=(this.horas<10)?("0"+String(this.horas)):(String(this.horas));
		var minutos=(this.minutos<10)?("0"+String(this.minutos)):(String(this.minutos));
		var segundos=(this.segundos<10)?("0"+String(this.segundos)):(String(this.segundos));
		//alert(this.segundos/2);
		
		if(this.mostrarSegundos)
			this.div.innerHTML=horas +":"+ minutos+":"+segundos;
		else
			this.div.innerHTML=horas +":"+ minutos;
		/*var segundosMitad=String(this.segundos/2);
		if(segundosMitad.indexOf(".")>0)
			this.div.innerHTML=this.div.innerHTML.replace(/\:/g, " ");	*/
		
	}





