/*************************************************************************/
/*                   WListBox 02Sep2003 Version 1.0                      */
/*                   		Funciones varias                             */
/*             Rodrigo José Prado Mendoza, Septiembre-2003               */
/*                  Copyright (c) 2003 Rodrigo Prado.                    */
/*     Algunos procesos fueron retomados del generador de Macromedia     */
/*************************************************************************/
//genera la acion de un formulario via post o gest
function Actionform(Action,Formname,Chvalue)
{
	var obj;
	if(Chvalue) //si se van a cambiar algunos valores
	{
		var n=Chvalue.length; //se obtiene el tamaño de la matriz que es el doble de los valores a cambiar
		for(var i=0;i<n;i++)
		{		
			obj=SerchObjid(Chvalue[i]); //se localiza la primera variable
			//si existe se cambia su valor
			if(obj)
			{
				obj.value=Chvalue[i+1];
			}
			i+=1;//se incrementa debido a que la siguiente variable esta en i + 1 (Numeros pares)
		}
	}
	obj=SerchObjid(Formname); //se localiza el formulario
	if(!obj)
		return;
	//si se desea cambiar su accion original
	if(Action)
		obj.action=Action;
	obj.submit();//se genera una accion con la funcion submit
}
//funcion de remplaza cada Regexp con el remplazo dentro de la cadena
function Replace(Regexp,Remplazo,Cadena)
{
	if(Cadena.match && Cadena.replace)//si existen las funciones
	{
		//mientras exista que remplazar remplaza
		while(Cadena.match(Regexp)!=null)
		{
			Cadena=Cadena.replace(Regexp,Remplazo);
		}
	}
	return Cadena;
}
//Funcion de busqueda de objetos por medio de sus id
function SerchObjid(id)
{
	if (document.getElementById)
		return(document.getElementById(id));
	if (document.all)
		return(document.all[id]);
	return(false);
}
function WPosicionardiv(objchild,layer,anexoleft,anexotop)
{
	Posicion=Objetoxy(objchild);
	tag=SerchObjid(layer); //busca el div
	if(!tag) //si no lo encuentra
		return;
	tag.style.left=Posicion['Left'] + anexoleft; //actualiza la posicion izquierda
	tag.style.top=Posicion['Top'] + anexotop; //actualiza la posicion top		
}
function Objetoxy(IdObj)
{
	var Posicion=new Array();
	Posicion['Left']=0;
	Posicion['Top']=0;
	var tag=SerchObjid(IdObj); //Busca el objeto child
	if(!tag) //si el objeto no existe
		return Posicion;
	while(tag) //mientras sea verdadero
	{
		Posicion['Left']+=tag.offsetLeft; //suma la posicon izquierda
		Posicion['Top']+=tag.offsetTop; //Suma la posicon top
		tag=tag.offsetParent;//obtiene el objeto padre
	}
	return Posicion;
}
function Objetosize(IdObj)
{
	var Size=new Array();
	Size['Width']=0;
	Size['Height']=0;
	tag=SerchObjid(IdObj); //Busca el objeto child
	if(!tag) //si el objeto no existe
		return Size;
	Size['Width']=tag.offsetWidth;
	Size['Height']=tag.offsetHeight;
	return Size;
}
/*******INICIO DE OBJETO WLISTBOX***********************/
function WListBox(Name,Left,Top)
{
		this.Name=Name; //Nombre del Objeto
		this.Zindex=1; //Zindex Global
		this.Position='absolute'; //Tipo de posicion
		this.Nomatch=false; //Si no encontro datos en la busqueda
		this.ValueNomatch=""; //El ultimo valor antes de no encontrar
		this.Iniant=-1; //Inicio anterior
		this.Itemhead=""; //Item del menu seleccionado
		this.Mayor=0; //ancho maximo de las opciones
		this.WidthScrollbar=20;//Ancho de la barra scroll
		this.Font="Arial, Helvetica, sans-serif"; //Fuente de la lista
		this.Sizefont="12px"; //Tamao de la fuenta
		this.Forecolor=0x000000;//color de la fuenta
		this.Widthfont=10;//Ancho de la fuente
		this.Left=Left; //Posicion izquierda del objeto
		this.Top=Top; //Posicion derecha del objeto
		this.Showinlist=1; //Maximo elementos a mostrar en la lista
		this.Width=120; //Ancho del objeto
		this.Factor=20; //Factor de desplazamiento entre items
		this.AjusteHeight=0; //Ajuste del alto del objeto
		this.Itemfocus=""; //nombre de la opcion enfocada
		this.Noitem=-1; //indice de la opcion enfocada
		this.Background=0xFFFFFF; //color de fondo de la lista
		this.Shadowcolor=0x0000FF; //Color del Sobreado de las opciones
		this.Lista=new Array(); //Elementos de la lista
		this.Listapadre=new Array();//Elementos generales de la lista
		this.Visible=true;
		this.Lastitemvisible=-1;
		//Elementos del encabezado
		this.Listahead=new Array("Contenido Anterior","Filtrar Contenido","Siguiente Contenido");
		this.Enableditemhead=new Array(true,true,true);//bloqueos del encabezado
		this.Value="";//Valor del elemento seleccionado de la lista
		this.Text="";//texto del elemento seleccionado de la lista
		this.Show=WLBShow; //Metodo para mostrar el objeto
		this.Hide=WLBHide;//Metodo para ocultar el objeto
		this.Additem=WLBAdditem; //Adicionar elementos
		this.MakeObject=WLBMakeObject;//Crea el objeto
		this.Search=WLBSearch;//Metodo de busqueda dentro de la lista
		this.Sort=WLBSort; //Metodo para ordenar datos
		this.Click=WLBClick;//Evento click de la lista
		this.Clickhead=WLBClickhead; //Evento click del encabezado
		//si no existe el contenedor del WListBox
		if (!window.WListBoxs)
		{
			window.WListBoxs=new Array(); //Se crea un nuevo contenedor
			window.WListBoxs.Version="1.0"; //Se indica la version del objeto
			//Se indica el autor del objeto
			window.WListBoxs.Author="Rodrigo José Prado Mendoza";
		}
		window.WListBoxs[this.Name]=this; //anexamos el control al contenedor
	}
	/*definicion del metodo Show*/
	function WLBShow()
	{
		var i;
		//busca el objeto de la lista
		obj=SerchObjid('parent' + this.Name);
		if(obj) //si se encuentra
		{
			obj.style.visibility='visible'; //se muestra el objeto
			obj.style.Left=this.Left;
			obj.style.Top=this.Top;
		}
		this.Visible=true;
		WLBFocusitem(this.Itemfocus,this.Name,Number(this.Noitem),true);
	}
	/*Definicion del metodo Hide*/
	function WLBHide()
	{
		var i;
		obj=SerchObjid('parent' + this.Name);
		if(obj) //si se encuentra
			obj.style.visibility='hidden'; //se oculta el objeto
		this.Visible=false;
	}
	/*Definicion del proceso de los click*/
	function WLBProssClick(Padre,Eleccion)
	{
		//se ingresa el nombre del enfoque
		window.WListBoxs[Padre].Itemfocus=Padre + Eleccion;
		//se ingresa el numero de la opcion seleccionada
		window.WListBoxs[Padre].Noitem=Eleccion;
		//Se ingresa el texto de la opcion
		window.WListBoxs[Padre].Text=window.WListBoxs[Padre].Listapadre[Eleccion].split('<BR>')[0];
		window.WListBoxs[Padre].Text=Replace(/&lt;/,'<',window.WListBoxs[Padre].Text);
		window.WListBoxs[Padre].Text=Replace(/&gt;/,'>',window.WListBoxs[Padre].Text);
		//se ingresa el valor de la opcion
		window.WListBoxs[Padre].Value=window.WListBoxs[Padre].Listapadre[Eleccion].split('<BR>')[1];
		//se ejecuta la funcion Click
		window.WListBoxs[Padre].Click();
	}
	/*Funcion para el proceso del click del encabezado*/
	function WLBProssClickhead(Padre,Eleccion)
	{
		switch(Eleccion) //en caso de eleccion
		{
			case 'hda' :
				if(!window.WListBoxs[Padre].Enableditemhead[0]) 
				{
					return;
				}
				break;
			case 'hdf' : 
				if(!window.WListBoxs[Padre].Enableditemhead[1])
				{
					return;
				}
				break;
			case 'hds' :
				if(!window.WListBoxs[Padre].Enableditemhead[2])
				{
					return;
				}
				break;
		}
		window.WListBoxs[Padre].Itemhead=Eleccion; //se ingresa el item del encabezado
		window.WListBoxs[Padre].Clickhead(); //se ejecuta la funcion clickhead
		this.Itemhead=""; //se borra la opcion itemhead
	}
	//Funcion asignada a Clickhead por defecto
	function WLBClickhead()
	{
		return;
	}
	//funcion asignada a Click  de forma predeterminada
	function WLBClick()
	{
		return;
	}
	//funcion para adicionar opciones a la lista
	function WLBAdditem(Label,Value)
	{    
		var tag,w,temp;
		Label=Replace(/</,'&lt;',Label);
		Label=Replace(/>/,'&gt;',Label);
		w=Label.length * this.Widthfont; //Indica el tamaño del sobreado
		if (w<(this.Width-this.WidthScrollbar)) //Si es menor al ancho de la lista
			w=this.Width - this.WidthScrollbar;//Se toma el ancho de la lista
		if(w>this.Mayor) //Si es mayor al representado en el objeto
			this.Mayor=w;
		else
			w=this.Mayor;
		//Formateamos el color de la fuente		
		temp='000000' + this.Forecolor.toString(16);
		//preparamos los parametros de entrada para los eventos
		parametros="'" + this.Name + this.Lista.length + "','" + this.Name + "'," + this.Lista.length +",false";
		parametros1="'" + this.Name + "'," + this.Lista.length; 
		//se crea la opcion 
		tag='<!--' + Label + Value + '--><DIV ID="' + this.Name +  this.Lista.length + '" STYLE="position:static;width:' + w + 'px;height:' + this.Factor + 'px; z-index:1;visibility: inherit;';
		tag=tag + ' FONT:' + this.Font + ';FONT-SIZE:' + this.Sizefont + ';COLOR:#' + temp.slice(temp.length-6) + '" onMouseOver="WLBFocusitem(' + parametros + ')" onClick="WLBProssClick(' + parametros1 + ')">' +  Label + '</DIV>';
		//Se anexa la opcion a la lista
		if(this.Lista.length==0)
			tag='<!---->' + tag; 
		this.Lista[this.Lista.length]=tag;
		//se anexa el valor y la etiqueta a la lista padre o general
		this.Listapadre[this.Listapadre.length]=Label + '<BR>' + Value;
	}
	//Determina los enfoques por los item del encabezado
	function WLBFocusitemhead(Unfocus,Item,Padre)
	{
		var temp;
		obj=SerchObjid(Item); //Localizamos el objeto
		if(!obj)
			return;
		if(Unfocus) //Si se desea eliminar el enfoque al item
		{
			obj.style.backgroundColor="";
			obj.style.color='#000000';
		}
		else
		{
			switch(Item.slice(Padre.length))
			{
				case 'hda' : //Si esta bloqueado el anterior
						if(!window.WListBoxs[Padre].Enableditemhead[0])
							return;
						break;
				case 'hdf' : //Si esta bloqueado el del filtro
						if(!window.WListBoxs[Padre].Enableditemhead[1])
							return;
						break;
				case 'hds' : //Si esta bloqueado el del siguiente
						if(!window.WListBoxs[Padre].Enableditemhead[2])
							return;
						break;
			}			
			obj.style.backgroundColor="#0000FF"; //Se asigna el color de fondo
			//Se asinga el inverso del fondo para el texto
			temp= '000000' + (0xFFFFFF-0x0000FF).toString(16);
			obj.style.color='#' + temp.slice(temp.length - 6);
		}
	}
	//metodo para ordenar la lista
	function WLBSort()
	{
		var i,obj,mant;
		mant=window.status; //obtener el texto de la barra de estado
		window.status="0% ordenado"; //Inicia con 0%
		this.Listapadre.sort(); //Se ordena la lista padre
		//Se reasignan los valores dentro de la lista
		for(i=0;i<this.Listapadre.length;i++)
		{
			temp='000000' + this.Forecolor.toString(16);
			parametros="'" + this.Name + i + "','" + this.Name + "'," + i +",false";
			parametros1="'" + this.Name + "'," + i; 
			tag='<!--' + this.Listapadre[i].split('<BR>')[0] + this.Listapadre[i].split('<BR>')[1] + '--><DIV ID="' + this.Name +  i + '" STYLE="position:static;width:' + this.Mayor + 'px;height:' + this.Factor + 'px; z-index:1;visibility: inherit;';
			tag=tag + ' FONT:' + this.Font + ';FONT-SIZE:' + this.Sizefont + ';COLOR:#' + temp.slice(temp.length-6) + '" onMouseOver="WLBFocusitem(' + parametros + ')" onClick="WLBProssClick(' + parametros1 + ')">' +  this.Listapadre[i].split('<BR>')[0] + '</DIV>';
			this.Lista[i]=tag;
			window.status= Math.round((i*100)/(this.Listapadre.length-1)) + "% Ordenado";
		}	
		window.status=mant; //Retronamos al texto anterior
		obj=SerchObjid(this.Name); //se busca el objeto 
		if(!obj)
			return;
		obj.innerHTML=this.Lista.join(''); //se reasigna el codigo html ordenado
	}
	//Metodo de busqueda dentro de la lista
	function WLBSearch(Value)
	{
		var texto,indini,opcn;
		Value=Replace(/</,'&lt;',Value);
		Value=Replace(/>/,'&gt;',Value);
		if(Value=='') //Si hay espacios en blanco no busca
			return;
		if(this.Nomatch) //Si la busqueda anterior no fue exitosa
		{
			//Si la busqueda anterior no fue exitosa y sus datos siguen siendo los mismos
			if(this.ValueNomatch.toUpperCase()==Value.substr(0,this.ValueNomatch.length).toUpperCase())
				return;
			else
				this.Nomatch=false;			
		}
		this.ValueNomatch=Value; //Se reasigan la ultima busqueda encontrada o no
		//Verificar si la opcion que se sigue buscando no es en la enfocada
		if(Value.toUpperCase()==this.Text.substr(0,Value.length).toUpperCase())
			return;
		this.Nomatch=true; //inicia nomatch en verdadero
		texto=this.Lista.join(''); //Se convierte el arreglo a cadena
		//Se busca el texto
		indini=texto.toUpperCase().search("<!--" + Value.toUpperCase());
		//si encontro 
		if(indini>-1)
		{
			this.Nomatch=false; //nomatch es falso
			if(indini!=this.Iniant)
			{
				this.Iniant=indini;
				//obtener el nombre de la opcion y el numero
				while(1)
				{
					if(texto.substr(indini,12).toUpperCase()=='--><DIV ID="')
					{
						indini=indini+ this.Name.length+12;
						opcn='';
						while(1)
						{
							if(texto.charAt(indini)!='"')
								opcn+=texto.charAt(indini);
							else
								break;
							indini+=1;
						}
						break;		
					}
					indini+=1;
				}
				//llama a la funcion de enfoque
				WLBFocusitem(this.Name + opcn,this.Name,opcn.valueOf(Number),true);
			}
		}
		else
			return; //si no se encontró nada
	}
	//Metodo de creacion del objeto
	function WLBMakeObject()
	{
		var i,obj,tag,w,t; 
		//crear el objeto contenedor de la lista y el encabezado
		tag='<DIV ID="parent' + this.Name + '" STYLE="position:' + this.Position  + '; left:0px; top:0px; width:120px; height:40px; z-index:' + this.Zindex + '; background-color: #CCCCCC; layer-background-color: #CCCCCC; border-style:solid; border-width:1px; border-color:black; visibility: visible; overflow: auto; clip: rect(0 120 40 0);">\n';
		tag=tag + '<DIV ID="head' + this.Name + '" STYLE="position:absolute; left:0px; top:0px; width:120px; height:40px; z-index:1; background-color: #CCCCCC; layer-background-color: #CCCCCC; border-style:solid; border-width:1px; border-color:black; visibility: inherit; overflow: auto; clip: rect(0 120 40 0);">\n';
		//crear la opcion anterior
		w=85;
		if(w<this.Width -2)
			w=this.Width-2;		
		t="'" + this.Name + "hda','" + this.Name + "'";
		tag=tag + '<DIV ID="' + this.Name + 'hda" STYLE="position:static;width:' + w + 'px;height:18px; z-index:1;visibility: inherit;';
		tag=tag + ' FONT: Arial, Helvetica, sans-serif;FONT-SIZE:12px;COLOR:#000000" onMouseOver="WLBFocusitemhead(false,' + t + ')" onMouseOut="WLBFocusitemhead(true,' + t + ')"';
		t="'" + this.Name + "','hda'";
		tag=tag + ' onClick="WLBProssClickhead(' + t + ')">' +  this.Listahead[0] + '</DIV>';
		//crear la opcion filtrar 
		t="'" + this.Name + "hdf','" + this.Name + "'";
		tag=tag + '<DIV ID="' + this.Name + 'hdf" STYLE="position:static;width:' + w + 'px;height:18px; z-index:1;visibility: inherit;';
		tag=tag + ' FONT: Arial, Helvetica, sans-serif;FONT-SIZE:12px;COLOR:#000000" onMouseOver="WLBFocusitemhead(false,' + t + ')" onMouseOut="WLBFocusitemhead(true,' + t + ')"';
		t="'" + this.Name + "','hdf'";
		tag=tag + ' onClick="WLBProssClickhead(' + t + ')">' +  this.Listahead[1] + '</DIV>';
		//crear la opcion siguiente
		t="'" + this.Name + "hds','" + this.Name + "'";
		tag=tag + '<DIV ID="' + this.Name + 'hds" STYLE="position:static;width:' + w + 'px;height:18px; z-index:1;visibility: inherit;';
		tag=tag + ' FONT: Arial, Helvetica, sans-serif;FONT-SIZE:12px;COLOR:#000000" onMouseOver="WLBFocusitemhead(false,' + t + ')" onMouseOut="WLBFocusitemhead(true,' + t + ')"';
		t="'" + this.Name + "','hds'";
		tag=tag + ' onClick="WLBProssClickhead(' + t + ')">' +  this.Listahead[2] + '</DIV></DIV>';
		//crear la lista				
		tag=tag + '<DIV ID="' + this.Name + '" STYLE="position:absolute; left:2px; top:62px; width:3px;';
		tag+='height:3px; z-index:1; background-color: #CCCCCC; layer-background-color: #CCCCCC;';
		tag+='border-style:solid; border-width:1px; border-color:black; visibility: inherit; overflow: auto; clip: rect(0 3 3 0);">';
		tag=tag + this.Lista.join(''); //Convertir arreglo a cadena
		tag=tag + "</DIV></DIV>";
		document.writeln(tag); //escribir en el documento
		obj=SerchObjid('parent' + this.Name); //busca el objeto padre
		//si existe se reasignan sus parametros
		if (obj)
		{
			w=this.Width+ 8;
			t=(this.Showinlist * this.Factor) + 70 + this.AjusteHeight;
			obj.style.left=this.Left;
			obj.style.top=this.Top;
			obj.style.height=t- 2 ;
			obj.style.width=w - 2 ;
			obj.style.backgroundColor='#' + this.Background.toString(16);
			obj.style.clip='rect(0 ' + w + ' ' + t + ' 0)';
			w=this.Width  + 2
			obj=SerchObjid('head' + this.Name); //busca el encabezado
			//si existen se reasignan sus propiedades
			if(obj)
			{
				t=62;
				obj.style.left=2;
				obj.style.top=2;
				obj.style.height=t - 2;
				obj.style.width=w - 2;
				obj.style.fontSize="12px";
				obj.style.backgroundColor='#CCCCCC';
				obj.style.clip='rect(0 ' + w + ' ' + t + ' 0)';	
			}
			obj=SerchObjid(this.Name); //busca el contenedor de la lista
			//si existen se reasignan sus propiedades
			if(obj)
			{
				t=(this.Showinlist * this.Factor)+this.AjusteHeight + 4
				obj.style.left=2;
				obj.style.top=62;
				obj.style.height=t - 2;
				obj.style.width=w - 2;
				temp= '000000' + this.Background.toString(16);
				obj.style.backgroundColor='#' + temp.slice(temp.length - 6);
				obj.style.clip='rect(0 ' + w + ' ' + t + ' 0)';	
			}
		}
	}
	//funcion de enfoque de cada item
	function WLBFocusitem(Item,Padre,Itemn,Mov)
	{
		var obj,num,temp,toptemp;
		if(Mov) //si se desea mover la lista para que presenta la opcion
		{
			obj=SerchObjid(Item);
			if(obj)
			{
				if(obj.scrollIntoView)
					obj.scrollIntoView(true);
				else
				{
					obj=SerchObjid(Padre);
					if(obj)
						obj.scrollTop=Itemn * window.WListBoxs[Padre].Factor;
				}
			}
		}
		else
			window.WListBoxs[Padre].Iniant=-1;
		obj=SerchObjid(window.WListBoxs[Padre].Itemfocus);
		if (obj)
		{
			obj.style.backgroundColor="";
			temp= '000000' + window.WListBoxs[Padre].Forecolor.toString(16);
			obj.style.color='#' + temp.slice(temp.length - 6);
		}
		obj=SerchObjid(Item);
		if(!obj)
			return;
		obj.style.width=window.WListBoxs[Padre].Mayor;
		temp= '000000' + window.WListBoxs[Padre].Shadowcolor.toString(16);
		obj.style.backgroundColor="#" + temp.slice(temp.length - 6);
		num=0xFFFFFF-window.WListBoxs[Padre].Shadowcolor;
		temp='000000'+ num.toString(16);		
		obj.style.color='#' + temp.slice(temp.length -6);
		window.WListBoxs[Padre].Itemfocus=Item;
		window.WListBoxs[Padre].Noitem=Itemn;
		window.WListBoxs[Padre].Text=window.WListBoxs[Padre].Listapadre[Itemn].split('<BR>')[0];
		window.WListBoxs[Padre].Text=Replace(/&lt;/,'<',window.WListBoxs[Padre].Text);
		window.WListBoxs[Padre].Text=Replace(/&gt;/,'>',window.WListBoxs[Padre].Text);
		window.WListBoxs[Padre].Value=window.WListBoxs[Padre].Listapadre[Itemn].split('<BR>')[1];
	}
/*********************Fin del documento WListBox.js**********************/