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.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.findPosY = function(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
};
			
UtilAJAX.findPosX = function(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
};

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); 	
		if (offsetTop<0)
			offsetTop = 0;
				
		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';
	}	
}

// ShowInfo2 ******* 	Muestra la capa de información con un texto en forma de bocadillo. 
//											Se oculta con 'showInfo' poniendo visible a 'false'
// parámetros: 		- id = id del elemento en el que se mostrará la capa
//								- width/height = dimensiones del bocadillo la capa oculta
//								- style = orientación del bocadillo
// 											los estilos que acepta el parámetro 'style' son: "topleft" "topright" "bottomleft" y "bottomright"
// 								- mensaje = texto del mensaje
//
UtilAJAX.showInfo2 = function(id, width, height, style, mensaje) {
	var html = "", top = 10, left = 10;
	
	switch (style) {
	
		case "bottomright": {
			top += 20+ height;
			html = '<div class="rb_b_roundbox"><div class="rb_br_top"><div><div></div></div></div><div class="rb_lr_contentwrap"><div class="rb_content">';
			html += mensaje;
   		html += '</div></div><div class="rb_br_bot"><div><div></div></div></div></div>';
   		break;
   	}		
   	case "bottomleft": {
			top += 10+height;
			left = -width;
			html = '<div class="rb_b_roundbox"><div class="rb_bl_top"><div><div></div></div></div><div class="rb_rl_contentwrap"><div class="rb_content">';
			html += mensaje;
   		html += '</div></div><div class="rb_bl_bot"><div><div></div></div></div></div>';
   		break;
   	}			
		case "topleft": {
			left = -width;
			html = '<div class="rb_t_roundbox"><div class="rb_tl_top"><div><div></div></div></div><div class="rb_rl_contentwrap"><div class="rb_content">';
			html += mensaje;
   		html += '</div></div><div class="rb_tl_bot"><div><div></div></div></div></div>';
   		break;
		}
		case "topright": {
			top = -30;
			html = '<div class="rb_t_roundbox"><div class="rb_tr_top"><div><div></div></div></div><div class="rb_lr_contentwrap"><div class="rb_content">';
			html += mensaje;
   		html += '</div></div><div class="rb_tr_bot"><div><div></div></div></div></div>';
   		break;
		}
		default: return;
	}	
	this.showInfo(id, "ayudaDiv", true, width, height, top, left, html);
		
}


// *******************
// 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;	
}

// *******************
// *******************	



UtilAJAX.changeOpacity = function(obj, opacity) {
    var sty = obj["style"];
    var value = opacity / 100;
    sty.opacity = value;
    sty.MozOpacity = value;
    sty.KhtmlOpacity = value;
    sty.filter = "alpha(opacity=" + opacity + ")";    
} 




// onReady ***************
// si se implementa esta función, su contenido se ejecuta al cargar el DOM de la página
// sin esperar a la carga de imágenes como hace el evento onLoad
UtilAJAX.onReady = null;
function onDOMContentLoaded() {
	if (typeof UtilAJAX.onReady == 'function') UtilAJAX.onReady();
}
(function(i) {var u =navigator.userAgent;var e=/*@cc_on!@*/false; var st =
setTimeout;if(/webkit/i.test(u)){st(function(){var dr=document.readyState;
if(dr=="loaded"||dr=="complete"){i()}else{st(arguments.callee,10);}},10);}
else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
document.addEventListener("DOMContentLoaded",i,false); } else if(e){     (
function(){var t=document.createElement('doc:rdy');try{t.doScroll('left');
i();t=null;}catch(e){st(arguments.callee,0);}})();}else{window.onload=i;}})
(onDOMContentLoaded);

// ***************

UtilAJAX.findClientCoords = function() {
	var w = 0;
	var h = 0;
	//IE
	if(!window.innerWidth)	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))	{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else	{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
};

