function UtilAJAX() { };

//document.layers 			The surfer is using NS 4
//document.all 				The surfer is using IE 4+
//window.opera 				The surfer is using Opera of some version
//document.getElementById 	The surfer is using IE5+ OR NS6+/ Firefox
//document.getElementById &&
//!document.all 			The surfer is using NS6+ or Firefox	
	
UtilAJAX.isIE = function() {
	if ( ( !document.all ) && ( document.getElementById ) ) return false;
	else if ( ( document.all ) && ( document.getElementById ) ) return true;
	else return false;
}
	
	
UtilAJAX.getThreadID = function() {
 	return document.forms[0]['com.ats.webbeans.struts.thread'].value;
};

UtilAJAX.newObj = function(name) {
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
  	if (this.obj)
		this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	if (this.obj)
		this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	if (this.obj)
	   	this.style = document.layers[name];
  }
}

UtilAJAX.findCoords = function(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {"left":curleft,"top":curtop};
};

UtilAJAX.findWidth = function(obj) {
	var width = obj.offsetWidth;
	return width;
}

UtilAJAX.findHeight = function(obj) {
	var height = obj.offsetHeight;
	return height;
}

UtilAJAX.capitalize = function(str) {
	var cap = str.substring(0,1).toUpperCase();
	 	cap += (str.substring(1,str.length).toLowerCase());
	return str;
}
UtilAJAX.escapeString = function(texto){
	var res = texto.replace('"','/%');
	return res;
}
UtilAJAX.unescapeString = function(texto){
	var res = texto.replace('/%','"');
	return res;
}

// ****************************** *************** *************** ********
// ShowMsg ******* 	Muestra una capa de información con un texto. 
// 				  	Si 'visible' es false no hacen falta más parámetros 
//
// parámetros: 		- idRef = id del elemento en el que se mostrará la capa
//					- idMsg = id de la capa oculta
//					- visible = boolean de mostrar/ocultar
//					- width/height = dimensiones de la capa oculta
//					- texto = texto del mensaje

UtilAJAX.showInfo = function(idRef, idMsg, visible, width, height, texto)
{
	var objMsg = new UtilAJAX.newObj(idMsg);
	var objRef = new UtilAJAX.newObj(idRef);

	if (visible)
	{
		var coords = UtilAJAX.findCoords(objRef.obj);
		var offsetLeft = coords["left"]+16;
		var offsetTop = coords["top"]-(height); 
				
		objMsg.style.display = 'block';
		objMsg.style.top = ''+(offsetTop)+'px';	
		objMsg.style.left = ''+(offsetLeft)+'px';	
		objMsg.style.height = ''+(height)+'px';	
		if (width) objMsg.style.width = ''+(width)+'px';	
		objMsg.obj.innerHTML = texto;
	}
	else
	{	
		objMsg.style.display	= 'none';
	}	
}



// *******************
// getPagina() necesita las librerías JS del pop-up emergente y RecogerPagina.js del bridge
// *******************
UtilAJAX.getPagina = function(url, title, width, height, loadingImagePath) {
		
	var win = new Window(url, {className: "dialog",  width: width, height: height, zIndex: 100, maximizable:false, title: title, showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true});
	
	if (win.getContent()) 
	{
		win.setDestroyOnClose();
		win.getContent().innerHTML = UtilAJAX.getHTML_Espere(height, loadingImagePath);
		win.showCenter();
		
		
		var dataFromBrowser = {win:win, height:height};
		
		// Llamar al servidor
		RecogerPagina.getPagina(url, {
		  	callback:function(dataFromServer) {
		    	UtilAJAX.pintarPagina(dataFromServer, dataFromBrowser);
			}
		});
	}	
}

UtilAJAX.pintarPagina = function (dataFromServer, dataFromBrowser) {

	var string = dataFromServer;
	var win = dataFromBrowser['win'];
	var height = dataFromBrowser['height'];

	var content  = '<div class="emergente" style="height:'+height+'px">';
		content += string + '</div>';
	win.getContent().innerHTML = content;
}


UtilAJAX.getHTML_Espere = function (height, loadingImagePath) {
	var res = '<img src="'+loadingImagePath+'" />';
	return UtilAJAX.getHTML_Mensaje(height, res);
}

UtilAJAX.getHTML_Mensaje = function (height, mensaje) {	
	var res = '<div class="emergente" style="height:'+height+'px;">';
		res += '<div align="center" style="padding-top:45%">';
		res += mensaje;
		res += '</div></div>';
	return res;	
}


// *******************
// *******************	

