
function absoluteLeft(obj){

    var curleft = 0;

    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }else if (obj.x) {
        curleft += obj.x;
	}
	
    return curleft;

}

function absoluteTop(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;

}

function Redimensionar(idObj,height,width){

   /* Pasmos el obj directamente */

	var obj=idObj;
	
	if(obj && obj.style){
		if(height) obj.style.height = height;
		if(width)  obj.style.width  = width;
		
		return obj;    
	}

   /* Pasamos el idObj */   
   obj=document.getElementById(idObj);

	if(obj && obj.style){
		if(height) obj.style.height = height;
		if(width){
			obj.style.width  = width;
		}
		
		return obj;    
	}

   

   /* Pasamos el nameObj */
   obj=document.getElementsByName(idObj);

	if(obj && obj.length){
		for(var i=0;i<obj.length;i++){
		var o=obj[i];
		if(o && o.style){
			if(height) o.style.height = height;
			if(width)  o.style.width  = width;
			}
		}
		
		return obj;
	}

   /* No hemos pasado nada com sentido*/
   return null;

}

function body_onload(){
	//ShowSubMenus(null);
	//GetContenido();
	//GetMenus();
	//ProcessAnchorsExtend2();

	/*ProcessAnchors(document.getElementById("td_piepagina"));
	ProcessAnchors(document.getElementById("menu_location"));*/

}

function ProcessAnchorsExtend2(){
	var elementos = document.getElementsByTagName("a");
	for(var i = 0; i < elementos.length; i++){
		ProcessAnchor(elementos[i]);
	}		
}

function ProcessAnchorsExtend(){
	
	var elementos = document.body.childNodes;
	
	for(var i = 0; i < elementos.length; i++){
		if(elementos[i].getAttribute("parse")){
			ProcessAnchors(elementos[i]);
		}
	}

}


function ProcessAnchors(obj){/* Esta funci�n procesa todas las anclas que est�n dentro del objeto obj */
	try{
		var as=obj.getElementsByTagName("A");
		for(var i=0;i<as.length;i++){
			ProcessAnchor(as[i]);
		}
	}catch(ex){
	
	}
}

function ProcessAnchor(a){
	/* Esa funci�n parsea el ancla a, transformandola cambiando el href por la funci�n GotoLocalMenu, en el caso de que sea un ancla.*/
	try{
		var idMenu = ifIsIndexPhpThenGetIdOfMenu(a);
		if(idMenu){
			a.href = '#null';
			a.target = '_self';
			a.onclick = new Function("","return GotoLocalMenu('"+idMenu+"')");
		}else{
			if(!a.href.indexOf("lang")){
				a.href = "&lang=<?=$_SESSION['lang_code']?>";
			}
		}
	}catch(ex){
		//alert(ex.description);
	}

}

function ifIsIndexPhpThenGetIdOfMenu(a){
	/* Esta funci�n es la que dice su el ancla a es o no un hiperenlace a parsear*/
	
	var funct = a.onclick;
	
	if(funct) return false;
	if((a.target) && (a.target != '_self') && (a.target !='_parent')) return false;
	
	var h=a.href.toLowerCase().split("/");
	
	h=h.pop();
	if((h.indexOf("index.php")>-1) && (h.indexOf("idmenu=")>-1)){
	h=h.split("idmenu=");
	h=h.pop()+"&";
	h=h.split("&");
	h=h.shift();
	h=parseInt(h,10);
	return isNaN(h) ? false:h;
	}
	
	return false;

}

function GetContenido(){
	try{
		//si ha sido establecido el ultimo idMenu, los usamos antes de cargar...
		var lastIdMenu = 0;
		try{
			lastIdMenu = document.forms['form_menu'].idLastMenu.value;
		}catch(e){}
		var frm = document.getElementById("form_contenido");
		if(!frm) return;
		if(parseInt(lastIdMenu) > 0) {
			frm.idMenu.value=lastIdMenu;
			frm.submit();
			return false;
		}
	}catch(e){
		alert(e.description);
		return false;
	}
	
	frm.submit();
	return true;

}

function GetMenus(){

	var frm = document.getElementById("form_menu");
	
	if(!frm) return;
	frm.submit();

}

function SetInnerHTMLOfMenu(idMenu, html, idDefaultMenu){
	
	var div= document.getElementById('menu_lateral');
	if (div){
		div.innerHTML = html;
		
		var ol = document.getElementById('ol_'+idMenu);
		if (!ol){
			ol = document.getElementById('ol_'+idDefaultMenu);
		}

		if(ol)
			ShowSubMenus(ol);
	}
}

function LocateFirstLi(){
	var li = null;
	var div = document.getElementById('menu_lateral');
	if (!div) return null;
	var li = div.firstChild.firstChild;
	return li;
}

function ShowSubMenus(li){

	try{
		
		HideAllSubMenus();
	
		if(!li) {
			li = LocateFirstLi();
		}
		if (!li) return null;
	
		var p=li;
	
		while(p && p.nodeName != 'DIV'){
			if(p.nodeName.toLowerCase() == 'ol'){
				p.style.display = '';
			}
			p=p.parentNode;
		}
	
		var id = li.id.replace(/.*_/,'');
		var ol = document.getElementById('ol_'+id);
	
		if(ol)
		{
			ol.style.display = '';
		}
	
		var a=document.getElementById('a_'+id);
		
		if(a){
			a.className = a.className.replace(/ selected/gi,'');	
			a.className += ' selected';
		}
	
	}catch(e){
		alert(e.description);
	}

}

function HideAllSubMenus(){

	var div = document.getElementById('menu_lateral');
	var ols = div.getElementsByTagName('ol');

	//Disable for this web only.
	for(i=0;i<ols.length;i++){
		ols[i].style.display = 'none';
	}

	var lis = div.getElementsByTagName('a');

	for(i=0;i<lis.length;i++){
		var li = lis[i];
		if (li.className.indexOf('selected')>-1) {
			//li.className = li.className.replace(/ selected/gi,'');
		}
	}

	return true;

}

function SetInnerHTMLOfContenido(idContenido,html){
	try{
		var obj = document.getElementById(idContenido);

		if (obj){
			obj.innerHTML = html;
			ProcessAnchors(obj);
		}

	}catch(e){
		alert(e.description);
	}
}

function EventReturn(b){

   if(window.event) event.returnValue=b;
   return b;

}

function ReleaseClass(){
	return;
	var xdiv=document.getElementById('main_contenido').className="main_contenido";
}

function ReleaseClassOff(){
	return;
	var xdiv=document.getElementById('main_contenido').className="main_contenido_off";
}

function menu_lateral_onclick(obj){

	var id=obj.id;
	var frm = document.getElementById('form_contenido');

	if(!frm) return;
	
	id = id.replace(/.*_/,'');
	frm.idMenu.value = id;
	frm.date.value = new Date().valueOf();

	var li = document.getElementById('li_'+id);

	/* Show the SubMenus only if it have childs!! otherway, it fail :(*/
	if(li && li.childNodes.length > 0){
		ShowSubMenus(li);
	}

	var a = document.getElementById('a_'+id);
	var text = (li && li.innerText) ? li.innerText : (a ?  a.innerHTML : obj.innerHTML);
	var xdiv=document.getElementById('main_contenido');

	document.title = '::: ' + text + ' :::';
	//xdiv.className="main_contenido";

   //set the last menu in we do
	try{
		var idLast = document.getElementById('form_menu');
		idLast.idLastMenu.value = id;
	}catch(e){}   

	/* EXPLORE HREF */	

	var href = a ?  a.getAttribute('xhref'):null;

	if(!href){
		if(!a){	GetMenus();}
		xdiv.innerHTML = "";
		GetContenido();
		EventReturn(true);
		return true;
	}

	

	/* si el menu es un menu interno, nos posicionamos en el... */
	if(href.indexOf("idMenu")==0){
		arguments = href.split("=");
		GotoLocalMenu(arguments[1]);
		EventReturn(true);
		return true;		
	}

	href = href.replace('<?="http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]/"?>','/');
	href = href.replace('<?="http://$_SERVER[SERVER_NAME]/"?>','/');
	
	/** EXPLORE PARAMS OF ANCHOR **/
	var target = a.getAttribute('xtarget');
	target = target.replace(/\;/g,',');
	var tg=target.split(",");
	
	tg=tg[0];
	tg=tg.replace(/.*\=/,"");
	tg=tg.replace(/ /gi,'');

	if(!tg) tg='__IFRAME_CONTENIDO__';
	
	var options = target.split(",");
	options.shift();
	options = options.join(",");
   
	/* Case cont */
	if(tg.indexOf('cont')>-1 && href.indexOf("xhide")<0){
		/** External site **/
		if((href.indexOf("http://")>-1) || (href.indexOf("/")==0) ){
			var div=document.getElementById('main_contenido');
			var w='100%;';var h='100%;';
			
			if(div){
			   h=(document.body.clientHeight);
			}
			
			var iframe="<iframe id='iframe_contenido' style='width:"+w+";height:"+h+"; border:0px solid #000;' name='the_content' src='"+href+"' scrolling='no' frameborder='0' framespace='0'></iframe>";
			xdiv.innerHTML = "";
			if(div) div.innerHTML=iframe;
			EventReturn(true);
			return true;
		}
	}else{
		if(parseInt(href.indexOf("xhide")) >= 0){ 
			tg = "_null";
		}
		window.open(href,tg,options);
		EventReturn(true);
		return true;
	}	

}

function element_onclick(obj){

	var id=obj.id;
	var frm = document.getElementById('form_contenido');

	if(!frm) return;

	id = id.replace(/.*_/,'');
	frm.idMenu.value = id;
	frm.date.value = new Date().valueOf();

	var text = (obj ?  obj.innerHTML : "");
	var xdiv=document.getElementById('main_contenido');

	document.title = '::: ' + text + ' :::';
	xdiv.className="main_contenido";

	/* EXPLORE HREF */	
	var href = obj ?  obj.getAttribute('xhref'):null;

	if(!href){
		if(!obj){GetMenus();}
		GetContenido();
		EventReturn(true);
		return true;
	}

	/* si el menu es un menu interno, nos posicionamos en el... */
	if(href.indexOf("idMenu")==0){
		arguments = href.split("=");
		GotoLocalMenu(arguments[1]);
		EventReturn(true);
		return true;		
	}

   

	href = href.replace('<?="http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]/"?>','/');
	href = href.replace('<?="http://$_SERVER[SERVER_NAME]/"?>','/');
	tg = "_blank";

	if(parseInt(href.indexOf("xhide")) >= 0) tg = "_null";

	window.open(href,tg);
	EventReturn(true);

	return true;

}

function menu_lateral_get_contenido(obj){

   var frm=document.getElementById('main_form');

   if(frm){
       frm.idMenu.value=obj.id.replace(/.*_/,'');
       frm.accion.value='get.contenido';
       frm.action='/html/services/content.php';
       frm.callback.value='SetInnerHTML';
       frm.element.value='div_contenido'
       frm.submit();
   }      
   
}


function SimulaClick(obj) {
	
	var evt = document.createEvent("MouseEvents");
	
	evt.initMouseEvent("click", true, true, window,	0, 0, 0, 0, 0, false, false, false, false, 0, null);
	
	var cb = obj; 
	var canceled = !cb.dispatchEvent(evt);
	
	if(canceled) {
		// A handler called preventDefault
	} else {
		// None of the handlers called preventDefault
	}
	
}

function GotoLocalMenu(idMenu){
	
	try{
		target = document.getElementById("a_" + idMenu);
		
		if(target){
				
			if(target.click) {
				
				target.click(); //asume iexplorer
			}else{
				//alert(target);
				location.href = target ;
				//SimulaClick(target); //asume mozilla compatible
			}
			
		}else{
		
			var xdiv = document.getElementById("main_contenido");
		
			if(xdiv){
				xdiv.innerHTML = "";
				//xdiv.className="main_contenido";
			}
	
			var frm = document.getElementById("form_contenido");
			if(frm){
				frm.idMenu.value=idMenu;
				frm.accion.value='get.contenido';
				frm.action='/html/services/content.php';
				frm.callback.value='SetInnerHTML';
				frm.element.value='main_contenido'
				frm.submit();
			}
		
		}
	
	}catch(ex){
		alert(ex.description);
	}

}

function GotoLocalMenuUp(idMenu){

	try{
	
		var wx = window.parent ? window.parent : window.opener;
		wx.GotoLocalMenu(idMenu);
	
	}catch(ex){

	}

}

function GotoLocalMenuOnClick(idMenu){

	try{
		
		var wx = window.parent ? window.parent : window.opener;
		
		target = wx.document.getElementById("a_" + idMenu);
		
		if(target){
			//alert(target);
			//target.click();
			location.href= target;
		}else{
			wx.GotoLocalMenu(idMenu);	
		}

   }catch(ex){

   }

}

function SetInnerHTML(element,html){

   var div=document.getElementById(element);

   if(!div) return;
   div.innerHTML=html;
   ProcessAnchors(div);
   
}

function MENU_ANCHOR_onclick(obj,href,the_target){


}

function trim(s){
	var s = new String(s);
	s = s.replace(/\s+/gi, ' '); //sacar espacios repetidos dejando solo uno
	s = s.replace(/^\s+|\s+$/gi, ''); //sacar espacios blanco principio y final
	return s;
}





function CheckRequiredValues(obj)
{
	try{
		obj.frm_html.value = obj.outerHTML;
		return true;

	}catch(e){
		return false;
	}

}

function MostrarInforme()
{
	try
	{
		var ref = "/modulos/Quest/public/quest_cuestionario.php";
		var opts = "Width=615px, Height=640px, Statusbar=no, Scrollbars=yes, Resizable=no, Menubar=yes, Toolbar=yes";
		var w = window.open(ref, "_blank", opts);

		if(!w)
		{
			var w = window.open(ref, "_blank", opts);
		}

	}catch(e){
		alert("Script.php::"+e.description);
	}

}

function CloseSession()
{
	try
 	{
 		b_l = document.getElementById("b_logout");
		if(b_l) b_l.click();
 	}
 	catch(Exception){

 	}

}

function LoadUrl(linkObject) {

	var href = linkObject.getAttribute("href");
	var a = linkObject;
	href = href.replace('<?="http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]/"?>','/');
	href = href.replace('<?="http://$_SERVER[SERVER_NAME]/"?>','/');

   /** EXPLORE PARAMS OF ANCHOR **/

   var target = a.getAttribute('xtarget');
   var tg='__IFRAME_CONTENIDO__';
   var options = new Array();
   
   if(target) {
		target = target.replace(/\;/g,',');
		tg=target.split(",");
		tg=tg[0];
		tg=tg.replace(/.*\=/,"");
		tg=tg.replace(/ /gi,'');
   		options = target.split(",");
	}

   options.shift();
   options = options.join(",");

   /* Case cont */

   if(true){
       /** External site **/
       if((href.indexOf("http://")>-1) || (href.indexOf("/")==0) ){
           var div=document.getElementById('main_contenido');
           var w='100%;';var h='100%;';
           if(div){
               h=(document.body.clientHeight);
           }

	       var iframe="<iframe id='iframe_contenido' style='width:"+w+";height:"+h+"; border:0px solid #000;' name='the_content' src='"+href+"' scrolling='no' frameborder='0' framespace='0'></iframe>";
           if(div) div.innerHTML=iframe;
           return false;
       }

	}	

}


function UnregisterUser(formx, msg) {
	try {
		if(confirm(msg)) {
			formx.accion.value='disable_user';
			return true;
		}
    }catch(Exception) { return false; }
    return false;
}

function UpdateUser(formx, msg) {
	try {
		if(confirm(msg)) {
			formx.accion.value='update_user';
			return true;
		}
	}catch(Exception) { return false; }
    return false;
}

