function UtilAJAX() { };


UtilAJAX.getThreadID = function() {
 	return document.forms[0]['com.ats.webbeans.struts.thread'].value;
};

UtilAJAX.findPos = 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 [curleft,curtop];
};

UtilAJAX.findWidth = function(obj) {
	var width = obj.offsetWidth;
	return width;
}

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;
}

// *******************
// 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.getPaginaFija = 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, minimizable:false, closable:false});
	
	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 heightS = dataFromServer['height'];
	var height = dataFromBrowser['height'];

	var content  = '<div style="height:'+height+'px; background-color: #ffffff"><div class="emergente" style="height:'+heightS+'px;">';
		content += string + '</div></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">';
		res += '<div align="center" style="padding-top:'+(height/3)+'px">';
		res += mensaje;
		res += '</div></div>';
	return res;	
}

UtilAJAX.isIE = function() {
	if ( ( !document.all ) && ( document.getElementById ) ) return false;
	else if ( ( document.all ) && ( document.getElementById ) ) return true;
	else return false;
}

UtilAJAX.newObj = function(name) {
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	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
		}
	}
	var ie = UtilAJAX.isIE();
	curtop  += (ie)?(-18):(-6);
	return [curleft,curtop];
}

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[0]+16;
		var offsetTop = coords[1]-(height); 
		objMsg.style.display	= 'block';
		objMsg.style.top = ''+(offsetTop)+'px';	
		objMsg.style.left = ''+(offsetLeft)+'px';	
		objMsg.style.height = ''+(height)+'px';	
		objMsg.style.width = ''+(width)+'px';	
		objMsg.obj.innerHTML = texto;
	}
	else
	{	
		objMsg.style.display	= 'none';
	}	
}




// *******************
// *******************	