// funciones que permiten el desplazamiento de iframes

var timer_id, strTitle, strID;

function noImage(object)
{
	try
	{
		var objectID = object.name;
		object.src = '/ima/transparente.gif';
		document.getElementById(objectID).rel = '';
		document.getElementById(objectID).href = 'javascript:void(0)';
		document.getElementById(objectID).disabled = 1;
	}
	catch(ex)
	{
		alert(ex);
	}
}

function setNav()
{
	try
	{
		var objLink = document.getElementById("navLink");
		var objNavTag = parent.document.getElementById("navTag");
				
		if (objNavTag != null && objLink!= null)
		{
			if (objLink!=null)//(typeof(objBody.nav)=='undefined')
				objNavTag.innerHTML = objLink.value;
		}
	}
	catch(ex)
	{
		alert("Excepcion: setNav() ha producido una excepcion");
	}
}

function scroll_iframe(frm,inc,dir) 
{
  try
  {
	  if (timer_id) clearTimeout(timer_id);
	  if (window.frames[frm]) 
	  {
		if (dir == "v") window.frames[frm].scrollBy(0, inc);
		else window.frames[frm].scrollBy(inc, 0);
		timer_id = setTimeout("scroll_iframe('" + frm + "'," + inc + ",'" + dir + "')", 20);
	  }
  }
  catch(err)
  {
	alert("Excepcion: scroll_iframe() ha producido una excepcion");
  }	 	  
}

function stopScroll() 
{
	try
	{
		if (timer_id) clearTimeout(timer_id); 
	}
	catch(err)
	{
		alert("Excepcion: stopScroll() ha producido una excepcion");
	}	
}

function loadPage(Page, ConfigID, ID, Title, FrameID) 
{
	try
	{
		var objFrame = document.getElementById(FrameID);
		var strURL = Page+"?P_CONFIGURACION="+ConfigID+"&P_ELEMENTO_ARBOL="+ID;
		setTitle(Title);
		
		if (FrameID!='')
		{
			if (objFrame!=null)
				objFrame.src = strURL;
		}
		else
		{
			document.location = strURL;
		}
		
		/*Quick fix: establece el codigo del nivel uno para luego poder recuperarlo en el nivel 3*/
		/*if (ConfigID=='CATALOGO_N2')
			setID(ID);*/
		
	}
	catch(err)
	{
		alert("Excepcion: stopScroll() ha producido una excepcion");
	}	
}

/* MM */
function MM_reloadPage(init) 
{
	if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
   		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) 
		location.reload();
}

/*
#BEGIN: ALI KETTANI
*/

function getURL(pstrURL, pstrParam)
{
	try
	{
		var strParam = pstrParam.toString();
		var strURL = pstrURL.toString();
		var intCount = strURL.indexOf(strParam);
		var strResult =  "";
	
		if (intCount == -1)
			strResult = strURL;
		else
			strResult = strURL.substring(0 , intCount-1);
		
		return strResult;
	}
	catch(err)
	{
		alert("Excepcion: getURL() ha producido una excepcion");
	}		
}

function setTitle(Title)
{
	try
	{
		objTitle = parent.document.getElementById("txtTitle");
		
		if (objTitle!=null)
			objTitle.value = Title.toString();
	}
	catch(err)
	{
		alert("Excepcion: setTitle() ha producido una excepcion");
	}
}

function getTitle(Nivel)
{
	try
	{
		if (Nivel==2)
			objTitle = parent.document.getElementById("txtTitle");
		else if (Nivel==3)
			objTitle = parent.parent.document.getElementById("txtTitle");
			
		objTitle2 = document.getElementById("spanTitle");
		
		if (objTitle!=null)
			txtTitle = objTitle.value;
		
		if(objTitle2!=null)	
			objTitle2.innerHTML = txtTitle;			
			
	}
	catch(err)
	{
		alert("Excepcion: getTitle() ha producido una excepcion");
	}
}

function setID(ID)
{
	try
	{
		objID = parent.document.getElementById("txtID");
		
		if (objID!=null)
			objID.value = ID.toString();
	}
	catch(err)
	{
		alert("Excepcion: setID() ha producido una excepcion");
	}
}

function getID(Nivel)
{
	try
	{
		if (Nivel==2)
		{
			objID = parent.document.getElementById("txtID");
			strID = objID.value;
		}
		else if (Nivel==3)
		{
			objID = parent.parent.document.getElementById("txtID");
			strID = objID.value;
		}
				
		return strID;		
			
	}
	catch(err)
	{
		alert("Excepcion: getTitle() ha producido una excepcion");
	}
}

// Desplega el arbol
function processAll(Action)
{
	try
	{
		var arrInputs, i;
		var arrDivs, j;
		var arrSpans, k;
		var index, display, sign;
		
		if (Action=='close')
		{
			index = '0'
			display = 'none'
			sign = '+'
		}
		else
		{
			index = '1'
			display = ''
			sign = '-'
		}	
							
		// Establece el input control a 1
		arrInputs=document.getElementsByTagName('input');
		if (arrInputs!=null)
		{
			for(i in arrInputs)
			{
				if (arrInputs[i].id!=null)
					if (arrInputs[i].id.toString().indexOf("inputControl")==0)
						arrInputs[i].value =index;	
			}
		}
							
		// Abre todas las capas
		arrDivs=document.getElementsByTagName('div');
		if (arrDivs!=null)
		{
			for(j in arrDivs)
			{
				if (arrDivs[j].id!=null)
					if (arrDivs[j].id.toString().indexOf("divDetalle")==0)
						arrDivs[j].style.display = display;	
			}
		}
		
		// Cambiar + por -
		arrSpans=document.getElementsByTagName('span');
		if (arrSpans!=null)
		{
			for(k in arrSpans)
			{
				if (arrSpans[k].id!=null)
					if (arrSpans[k].id.toString().indexOf("spanTipoArticulo")==0)
						arrSpans[k].innerHTML = sign;	
			}
		}
	}
	catch(err)
	{
		alert("Excepcion: openAll() ha producido una excepcion");
	}		
}

function login ()
{
	var frmRegistroUsuarios = document.getElementById ("frmRegistroUsuarios");
	animarregistro();
						
	// actualiza el registro
	if (frmRegistroUsuarios!= null)
	{
		frmRegistroUsuarios.src = "ctx_registro.jsp";
	}
}

// abre el registro de usuarios 
function desconectaUsuarios ()
{
	// actualiza el registro
	document.location = "ctx_cerrar_sesion.jsp";
}		
						
// cierra de cliente 
function cierra_seleccion_cliente ()
{
	if (parent.document.frames!=null)
	{
		if (parent.document.frames.ifrSeleccionCliente!=null)
		{
			parent.document.frames.ifrSeleccionCliente.location = "about:blank"
			if (parent.MM_showHideLayers)
				parent.MM_showHideLayers('seleccion_cliente','','hide');
		}
	}
}

// Abre la seleccion de cliente 
function abre_seleccion_cliente ()
{

	/*obj = parent.frames.cuerpo;						
	alert("HREF: " + obj.window.location.href);*/
	
	if (parent.document.frames!=null)
	{	
		if (parent.document.frames.ifrSeleccionCliente!=null)
		{					
			parent.document.frames.ifrSeleccionCliente.location = "ctx_cliente.jsp"
			if (parent.MM_showHideLayers)								
			{
				parent.MM_showHideLayers('seleccion_cliente','','show')				
			}
		}
	}
}								

// comprueba conexion 
function compruebaConexion ()
{
	// variablers
	var objdivCompruebaConexion = document.getElementById("divCompruebaConexion");

	// Obtien el frame 
	if (objdivCompruebaConexion!=null)
		objdivCompruebaConexion.style.display = '';
		
	if (document.frames.ifrOculto)
		document.frames.ifrOculto.location = "comprueba_conexion.jsp"

	setTimeout("compruebaConexion()", 10000);
	
}				

// avisa de la perdida de session
function avisoPerdidaSession ()
{
	// Avisa 
	alert('<%=objTraduccion.getTexto("TEXTO_PERDIDA_SESSION")%>');
	
	// refresca la pagina 
	top.location ="../index.htm";						
	
}

function displaydatetime() 
{
	if (!document.layers && !document.all) return;
	var today;
	var timeLocal;
	today = new Date();
	timeLocal = today.toLocaleString(); //Convert to current locale.
	if (document.layers) 
	{
		document.layers.clockLocal.document.write(timeLocal);
		document.layers.clockLocal.document.close();
	}
	else if (document.all) 
	{
		clockLocal.innerHTML = timeLocal;
	}
	setTimeout("displaydatetime()", 500)
}

//window.onload = displaydatetime;

// ------------------------------------------------------------------------
// Anima capas
function animateLayer(el, x, y, a, b, c, s) { //v2.1 PVII
	var elo=el,f="",m=false,d="";
	x=parseInt(x);
	y=parseInt(y);
	
	if (parseInt(s)>0) 
	{
		setTimeout("animateLayer(\""+elo+"\",\""+x+"\",\""+y+"\",\""+a+"\",\""+b+"\",\""+c+"\",\""+0+"\")", s);
		return;
	}
	
	if (el.indexOf("?")>0) 
	{
		p=el.split("?");
		f="parent.frames['"+p[1]+"'].";
		el=p[0];
	}
	if (document.all) /*parent.document.all&&*/ 
	{
		/*if(eval("parent.document.all['"+el+"']")!=null)
			g=eval(f+"parent.document.all['"+el+"']");
		else*/
		g=eval(f+"document.all['"+el+"']");
		d=g.style;
		
		if(!d.left) 
			d.left=(!g.currentStyle)?0:g.currentStyle.left;
	
		if(!d.top) 
			d.top=(!g.currentStyle)?0:g.currentStyle.top;
	}
	else if (document.getElementById) 
	{
		g=eval(f+"document.getElementById('"+el+"')");
		d=g.style;

		if(!d.left)
			d.left=(!document.defaultView)?0:document.defaultView.getComputedStyle(g,"").getPropertyValue("left");
			
		if(!d.top) 
			d.top=(!document.defaultView)?0:document.defaultView.getComputedStyle(g,"").getPropertyValue("top");
	}
	else
	{
		d=eval(MM_findObj(elo));
	}
	
	var xx=(parseInt(d.left))?parseInt(d.left):0;
	var yy=(parseInt(d.top))?parseInt(d.top):0;
	
	if(c==1)
	{
		x+=xx;
		y+=yy;
		m=true;
		c=0;
	}
	else if (c==2)
	{
		m=false;
		clearTimeout(d.doIt);
	}
	else 
	{
		var i=parseInt(a);
		
		if (eval(d.moved)){clearTimeout(d.doIt);}
		
		if (xx<x)
		{
			xx+=i;
			m=true;
			
			if(xx>x)
				xx=x;
		}
		if (xx>x)
		{
			xx-=i;
			m=true;
			
			if(xx<x)
				xx=x;
		}
		if (yy<y)
		{
			yy+=i;
			m=true;
			
			if(yy>y)
				yy=y;
		}
		if (yy>y)
		{
			yy-=i;
			m=true;
			
			if(yy<y)
				yy=y;
		}
	}
	if (m) 
	{
		d.left=xx;
		d.top=yy;
		d.moved=true;
		d.doIt=setTimeout("animateLayer(\""+elo+"\",\""+x+"\",\""+y+"\",\""+a+"\",\""+b+"\",\""+c+"\",\""+0+"\")", b);
	}
	else 
		d.moved=false;
}

// -------------------------------------------------------------				
// Abre o cierra el registro 
function animarregistro ()
{							
	/*if (parent.document.getElementById("layerState")!=null)
		objLayerState = parent.document.getElementById("layerState");
	else if (document.getElementById("ifmContenido").document.getElementById("layerState"))*/
	objLayerState = document.getElementById("layerState");
	
	if (objLayerState.value == "0") 
	{
		animateLayer('registro',540,-5,20,30,0,0);
		objLayerState.value = "1";
	 //document.frames.ifrRegistro.location = "ctx_registro.jsp";			
	}
	else
	{
		animateLayer('registro',540,-100,20,30,0,0);
		objLayerState.value = "0";
		//document.frames.ifrRegistro.location = "about:blank";			
	}
}
/*
#END
*/