<?php
/** 
* o------------------------------------------------------------------------------o
* | FUNCTION: setGridCombinaPrint  						| 02 de Octubre del 2011 |
* |------------------------------------------------------------------------------|
* | Desarrollado por	: Kenett R. Serrano Carranza							 |
* | Email				: ro_ken777sc@hotmail.com								 |
* | Definición			: Grid con columnas Combinadas y Agrupadas por Niveles	 |
* | 					  Para el formato de Impresion	 						 |
* o------------------------------------------------------------------------------o

* o------------------------------------------------------------------------------o
* | CLASE MYSQL, Conexion y Mantenimiento  				  | 01 de Abril del 2009 |
* |------------------------------------------------------------------------------|
* | Desarrollado por	: Kenett R. Serrano Carranza							 |
* | Email				: ro_ken777sc@hotmail.com								 |
* | Definición			: Clase para conectarse a una Base de Datos SQL o MySQL	 |
* o------------------------------------------------------------------------------o
* o------------------------------------------------------------------------------o
* | FUNCTION: setGridCombina  							| 18 de Octubre del 2010 |
* |------------------------------------------------------------------------------|
* | Desarrollado por	: Kenett R. Serrano Carranza							 |
* | Email				: ro_ken777sc@hotmail.com								 |
* | Definición			: Grid con columnas Combinadas y Agrupadas por Niveles	 |
* o------------------------------------------------------------------------------o
* o------------------------------------------------------------------------------o
* | FUNCTION: setTablaFormTabs 						  | 01 de Noviembre del 2010 |
* |------------------------------------------------------------------------------|
* | Desarrollado por	: Kenett R. Serrano Carranza							 |
* | Email				: ro_ken777sc@hotmail.com								 |
* | Definición			: Formulario agrupado en Tabs(Pestañas)					 |
* o------------------------------------------------------------------------------o
**/
class clsConexion {
/*
	var $strServidor;	//Direccion Ip o Nombre del Servidor
	var $strUsuario;	//Nombre del Usuario
	var $strClave;		//Clave
	var $strBD;			//Nombre de la Base de Datos
	var $link;			//Link de Conexion
	var $type;
*/
	protected $strServidor, $strUsuario, $strClave, $strBD, $type, $link;
	var $RutaImage = "images/";
	var $BotDefault = "b_table.png";

	public function __construct($strServidor,$strUsuario,$strClave,$strBD,$type) {
		$this->strServidor 	= $strServidor;
		$this->strUsuario 	= $strUsuario;
		$this->strClave 	= $strClave;
		$this->strBD 		= $strBD;
		$this->type			= $type;
	 	}

  	public function AbrirConexion() {  	
		//Establecemos la Conexion
		if (strtoupper($this->type)=="MYSQL") {
			$this->link = mysql_connect($this->strServidor, 
										$this->strUsuario,
										$this->strClave, false, 65536);	
			if(!mysql_select_db($this->strBD,$this->link))	{
				echo "ERROR SELECCIONANDO LA BASE DE DATOS :".$this->strServidor;
				exit();
				}		
			}
		elseif (strtoupper($this->type)=="MYSQLI") {
			$this->link = mysqli_connect($this->strServidor, 
										$this->strUsuario,
										$this->strClave,
										$this->strBD);	
								
			if(!mysqli_select_db($this->strBD,$this->link))	{
				echo "ERROR SELECCIONANDO LA BASE DE DATOS :".$this->strServidor;
				exit();
				}		
			
			}
			
		elseif (strtoupper($this->type)=="SQL") {
			$this->link = mssql_connect($this->strServidor, 
										$this->strUsuario, 
										$this->strClave);	
			if(!mssql_select_db($this->strBD,$this->link))	{
				echo "ERROR SELECCIONANDO LA BASE DE DATOS :".$this->strBD."EN ".$this->strServidor;
				exit();
				}
			}
		elseif (strtoupper($this->type)=="SQL2008") {
			$this->link = sqlsrv_connect($this->strServidor, 
										$this->strUsuario, 
										$this->strClave);	
			if(!sqlsrv_select_db($this->strBD,$this->link))	{
				echo "ERROR SELECCIONANDO LA BASE DE DATOS :".$this->strBD."EN ".$this->strServidor;
				exit();
				}
			}
		elseif (strtoupper($this->type)=="ORACLE" || strtoupper($this->type)=="OCI") {	
			if(ocilogon($this->strUsuario, 
									$this->strClave,
									$this->strServidor."/".$this->strBD
									))
				{
				$this->link = ocilogon(	$this->strUsuario, 
										$this->strClave,
										$this->strServidor."/".$this->strBD
										);
				}
//			else echo "ERROR SELECCIONANDO LA BASE DE DATOS :".$this->strBD." EN ".$this->strServidor;
			}
		else {
			echo "No selecciono el Tipo de Conexion: SQL o MySQL";
			exit;
			}	
		if(!($this->link))	{
				echo "ERROR CONECTANDO AL SERVIDOR : ".$this->strServidor;
				exit();
				}		
		return $this->link;	
		}
	
	function CerrarConexion() {
		if(strtoupper($this->type)=="MYSQL") mysql_close($this->link);
		elseif(strtoupper($this->type)=="MYSQLI") mysqli_close($this->link);
		elseif(strtoupper($this->type)=="SQL") mssql_close($this->link);
		elseif(strtoupper($this->type)=="ORACLE") ocilogoff($this->link);
		elseif(strtoupper($this->type)=="OCI") ocilogoff($this->link);
		}
	function Begin() {
		mysql_query("BEGIN", $this->link);
		}

	function Rollback() {
		mysql_query("ROLLBACK", $this->link);
		}

	function Commit() {
		mysql_query("COMMIT", $this->link);
		}
		
	function Query($sql) {
		if($sql=="") return NULL;
		elseif(strtoupper($this->type)=="MYSQL") {
			if($rs = mysql_query($sql,$this->link)) return $rs;	
			else return mysql_error();
			}
		elseif(strtoupper($this->type)=="MYSQLI") {
			if($rs = mysqli_query($this->link, $sql)) return $rs;	
			else return mysqli_error();
			}
		elseif(strtoupper($this->type)=="SQL") {
			if($rs = mssql_query($sql,$this->link)) return $rs;
			else echo "ERROR AL PROCESAR: ".$sql;
			}
		elseif(strtoupper($this->type)=="ORACLE") {
			if($rs = oci_parse($this->link, $sql)) {
				OCIExecute($rs);
				return $rs;
				}
			else echo "ERROR AL PROCESAR: ".$sql;
			}
		}
		
	function FetchArray($rs) {
		if(strtoupper($this->type)=="MYSQL") return  mysql_fetch_array($rs);
		elseif(strtoupper($this->type)=="MYSQLI") return mysqli_fetch_array($rs);	
		elseif(strtoupper($this->type)=="SQL") return mssql_fetch_array($rs);
		elseif(strtoupper($this->type)=="ORACLE") return oci_fetch_array($rs);
		
		}
	
	function NumFilas($rs) { 
		if(strtoupper($this->type)=="MYSQL") return mysql_num_rows($rs);	
		elseif(strtoupper($this->type)=="MYSQLI") return mysqli_num_rows($rs);
		elseif(strtoupper($this->type)=="SQL") return mssql_num_rows($rs);
		}
	
	function NumField($rs) { 
		if(strtoupper($this->type)=="MYSQL") return mysql_num_fields($rs);
		elseif(strtoupper($this->type)=="MYSQLI") return mysqli_num_fields($rs);
		elseif(strtoupper($this->type)=="SQL") return mssql_num_fields($rs); 
		}
		
	function FieldType($rs, $i) { 
		if(strtoupper($this->type)=="MYSQL") return mysql_field_type($rs, $i);	
		elseif(strtoupper($this->type)=="SQL") return mssql_field_type($rs, $i);
		}

	function FieldName($rs, $i) { 
		if(strtoupper($this->type)=="MYSQL") return mysql_field_name($rs, $i);	
		elseif(strtoupper($this->type)=="MYSQL") {
			 $finfo = mysqli_fetch_field_direct($rs, $i);
			 return $finfo->name;
			}
		elseif(strtoupper($this->type)=="SQL") return mssql_field_name($rs, $i);
		}
			
	function FieldLength($rs, $i) {
		if(strtoupper($this->type)=="MYSQL") return mysql_field_len($rs, $i);	
		elseif(strtoupper($this->type)=="SQL") return mssql_field_length($rs, $i);			
		}
		
	function LiberaMemoria($rs) { 
		if(strtoupper($this->type)=="MYSQL") return mysql_free_result($rs);
		elseif(strtoupper($this->type)=="MYSQLI") return mysqli_free_result($rs);
		elseif(strtoupper($this->type)=="SQL") return mssql_free_result($rs); 
		}
		
	function ErrorConsulta() {
		$tabla = "<table id=\"TableError\" ";
		$tabla.= "style=\"padding:0; margin-top:0px;border-collapse:collapse;\" "; 
		$tabla.= "width=350 align=center  cellpadding=0 cellspacing=0>";
		$tabla.= "<tr>";
		$tabla.= "<td height=19 colspan=2 align=center valign=middle class=\"FormTitulo\">";
		$tabla.= "ERROR AL REALIZAR CONSULTA...!!!!</th>";
		$tabla.= "</tr>";
		$tabla.= "<tr>";
		$tabla.= "<td height=35 colspan=2 valign=middle align=center class=interior>";
		$tabla.= "<b><BR>  LOS PARAMAETROS DE BUSQUEDA QUE INGRESO<BR>";
		$tabla.= "NO DEVOLVIERON DATOS<BR>";
		$tabla.= "SELECCIONE OTROS PARAMETROS Y VUELVA A INTENTARLO";
		$tabla.= "<br>O CONTACTESE CON EL ADMINISTRADOR DEL SISTEMA<BR>";
		$tabla.= "<br></b>";
		$tabla.= "</td>";
		$tabla.= "</tr>";
		$tabla.= "</table>";
		return $tabla;
		}
	
	function SetLista2($Nombre,  	//Nombre del Combo				  
					  $CadSql,		//Cadena de Consulta SQL
					  $Valor,		//Valor Seleccionado
					  $Ancho,		//Ancho del Combo
					  $Reload=NULL,	//Opcion de Recargar el Combo
					  $Titulo=NULL,	//Campo Titulo a Mostrar en el Combo
					  $Todos =NULL,	//Campo Seleccionar Totos
					  $SinCod=NULL,	//Muestra el Codigo en el Combo
					  $Locked=NULL,	//Bloquear el Combo
					  $Clase =NULL	//Stylo
					  ) {
		
		$this->AbrirConexion();
		if(isset($Reload) && $Reload<>"") $Reload=" onChange=\"$Reload\"";
		if(isset($Locked) && $Locked<>"" && $Locked=="SI") $Locked=" disabled";
		$varClass = (isset($Clase) && $Clase<>"")? " class='$Clase' " : " class='input' ";
		
		$combo = "<select name='$Nombre' id='$Nombre' size=\"5\" multiple=\"multiple\" $varClass $Reload ";
		$combo.= "style=\"width:".$Ancho."px; height:80px\" $Locked>";	
		if(isset($CadSql) && $CadSql<>"") {
			$rs = $this->Query($CadSql);
			while($row=$this->FetchArray($rs)) {
				if(isset($SinCod) && $SinCod<>"") $MiValor = strtoupper($row[1]);
				else $MiValor = $row[0]." - ".strtoupper($row[1]);
				$Selected="";
				if($row[0]==$Valor) $Selected="selected";
				$combo.= "<option value='$row[0]' ".$SinCod." - ".$Selected.">$MiValor</option>";
				}
			}
		$combo.= "</select>";
		$this->CerrarConexion();
		return $combo;
		}
	
	function setLista($jcamp) {
		$varCampo = "";
		if(isset($jcamp) && count($jcamp)>0) {
			$varCampo.="<select  ";
			$varCampo.= (isset($jcamp["id"]))?" id=\"".$jcamp["id"]."\" " : NULL;
			$varCampo.= (isset($jcamp["name"]))?" name=\"".$jcamp["name"]."\" " : NULL;
			$varCampo.= (isset($jcamp["filas"]) && $jcamp["filas"]<>"")?" size=\"".$jcamp["filas"]."\" " : " size=\"5\" ";			
			$varCampo.= "multiple=\"multiple\" ";
			$varCampo.= (isset($jcamp["clase"]))?" class=\"".$jcamp["clase"]."\" " : " class=\"input\" ";
			$varCampo.= (isset($jcamp["click"]))?" onclick=\"".$jcamp["click"]."\" " : NULL;
			$varCampo.= (isset($jcamp["dclick"]))?" onDblClick=\"".$jcamp["dclick"]."\" " : NULL;	
			$varCampo.= " style=\"";
			$varCampo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px;" : NULL;
			$varCampo.= (isset($jcamp["alto"]))?  " higth:".$jcamp["alto"]."px;" : NULL;
			$varCampo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
			$varCampo.= " \" ";
			$varCampo.= (isset($jcamp["locked"]) && $jcamp["locked"]=="SI")? " disabled " : NULL;
			$varCampo.=">";
			if(isset($jcamp["sql"]) && $jcamp["sql"]<>"") {
				$this->AbrirConexion();
				$rs = $this->Query($jcamp["sql"]);
				$this->CerrarConexion();
				while($row=$this->FetchArray($rs)) {
					if(isset($jcamp["sincod"]) && $jcamp["sincod"]=="SI") $MiValor = strtoupper($row[1]);
					else $MiValor = $row[0]." - ".strtoupper($row[1]);
					$Selected="";
					if($row[0]==$jcamp["valor"]) $Selected="selected";
					$varCampo.= "<option value='$row[0]' ".$Selected." >$MiValor</option>";
					}
				}
			$varCampo.= "</select>";
			return $varCampo;
			}
		}
	function SetCombo($Nombre,  	//Nombre del Combo				  
					  $CadSql,		//Cadena de Consulta SQL
					  $Valor,		//Valor Seleccionado
					  $Ancho,		//Ancho del Combo
					  $Reload=NULL,	//Opcion de Recargar el Combo
					  $Titulo=NULL,	//Campo Titulo a Mostrar en el Combo
					  $Todos =NULL,	//Campo Seleccionar Totos
					  $SinCod=NULL,	//Muestra el Codigo en el Combo
					  $Locked=NULL,	//Bloquear el Combo
					  $Clase =NULL	//Stylo
					  ) {
		
		$this->AbrirConexion();
		if(isset($Reload) && $Reload<>"") $Reload=" onChange=\"$Reload\"";
		if(isset($Locked) && $Locked<>"" && $Locked=="SI") $Locked=" disabled";
		$varClass = (isset($Clase) && $Clase<>"")? " class='$Clase' " : " class='input' ";
		
		$combo = "<select name='$Nombre' id='$Nombre' $varClass $Reload ";
		$combo.= "style=\"width:".$Ancho."px; height:17px\" $Locked>";	
		//Opcion Titulo
		if(isset($Titulo)) 
			$combo.= "<option value='Ninguno'>".strtoupper(html_entity_decode($Titulo))."</option>";
		
		$rs = $this->Query($CadSql);
		while($row=$this->FetchArray($rs)) {
			if(isset($SinCod) && $SinCod<>"") $MiValor = strtoupper($row[1]);
			else $MiValor = $row[0]." - ".strtoupper($row[1]);
			$Selected="";
			if($row[0]==$Valor) $Selected="selected";
			$combo.= "<option value='$row[0]' ".$SinCod." - ".$Selected.">$MiValor</option>";
			}
		//Opcion Todos
		$Selected="";
		if(isset($Todos)) {
			if($Valor=="Todos") $Selected="selected";
			$combo.= "<option value='Todos' $Selected>".strtoupper(html_entity_decode($Todos))."</option>";
			}
		$combo.= "</select>";
		$this->CerrarConexion();
		return $combo;
		}	
	
	function SetComboSimple ( 
		$Nombre,  		//Nombre del Combo				  
		$Array,			//Array Con Datos
		$Valor,			//Valor Seleccionado
		$Ancho,			//Ancho del Combo
		$Reload=NULL,	//Opcion de Recargar el Combo
		$Titulo=NULL,	//Campo Titulo a Mostrar en el Combo
		$Todos =NULL,	//Campo Seleccionar Totos
		$SinCod=NULL,	//Muestra el Codigo en el Combo
		$Locked=NULL	//Bloquear el Combo
		) {
		
		if(isset($Reload) && $Reload<>"") $Reload=" onChange=\"$Reload\"";
		if(isset($Locked) && $Locked=="SI") $Locked=" disabled";
		
		$combo = "<select name='$Nombre' id='$Nombre' class='input' $Reload ";
		$combo.= "style=\"width:".$Ancho."px; height:17px\" $Locked>";
		//Opcion Titulo
		if(isset($Titulo)) 
			$combo.= "<option value='Ninguno'>".strtoupper($Titulo)."</option>";		
		//Datos
		if(isset($Array)) {
			foreach ($Array as $CampoId=>$Campo) {
				$seleccionar="";		
				if($CampoId==$Valor && isset($Valor)) $seleccionar="selected";
				$combo.= "<option value=".$CampoId." ".$seleccionar.">$Campo</option>";
				}
			}
		//Opcion Todos
		$Selected="";
		if(isset($Todos)) {
			if($Valor=="Todos") $Selected="selected";
			$combo.= "<option value='Todos' $Selected>".strtoupper($Todos)."</option>";
			}
		$combo.= "</select>";
		return $combo;
		}	
	
	function setCampos($json) {
		$k = json_decode($json, true);
		$i = 0;
		while($i<count($k['contenido'])) {
			$Campo.=$this->SetCampo($k[contenido][$i]);
			$i++;
			}
		return $Campo;
		}
	
	function setBotones($json) {
		$k = json_decode($json, true);
		$i = 0;
		while($i<count($k['botones'])) {
			$Boton.=$this->SetBoton($k[botones][$i])."&nbsp;";
			$i++;
			}
		return $Boton;
		}
		
	function setBoton($jcamp) {
		$Campo.= "<input ";
		$Campo.= (isset($jcamp["id"]))? 	" id=\"".$jcamp["id"]."\" " : NULL;
		$Campo.= (isset($jcamp["name"]))? 	" name=\"".$jcamp["name"]."\" " : NULL;
		$Campo.= (isset($jcamp["tipo"]))? 	" type=\"".$jcamp["tipo"]."\" " : "type=\"submit\" ";
		$Campo.= (isset($jcamp["texto"]))? 	" value=\"".$jcamp["texto"]."\" " : "value=\"\" ";
		$Campo.= (isset($jcamp["clase"]))? 	" class=\"".$jcamp["clase"]."\" " : NULL;
		$Campo.= (isset($jcamp["click"]))? 	" onclick=\"".$jcamp["click"]."\" " : NULL;	
		$Campo.= (isset($jcamp["toltip"]))? " onMouseOver=\"MensajeTip('".$jcamp["toltip"]."')\" onMouseOut=\"UnTip()\" ":NULL;
		$Campo.= " style=\"cursor:pointer;";
		$Campo.= (isset($jcamp["ancho"]))? 	" width:".$jcamp["ancho"]."px;" : NULL;
		$Campo.= (isset($jcamp["alto"]))? 	" height:".$jcamp["alto"]."px;" : NULL;
		$Campo.= "\"/>";
		return $Campo;
		}	
	function setCampo($jcamp) {
		$Campo = "";
		switch ($jcamp["tipo"]) {
			case "imagen":
			case "image":
			case "img":
				$Campo.= "<img ";
				$Campo.= (isset($jcamp["img"]))? " src=\"".$jcamp["img"]."\" " : "src=\"images/b_error.png\" ";
				$Campo.= (isset($jcamp["ancho"]))? " width=\"".$jcamp["ancho"]."\" " : NULL;
				$Campo.= (isset($jcamp["alto"]))? "  height=\"".$jcamp["alto"]."\" " : NULL;
				$Campo.= (isset($jcamp["clase"]))? "  class=\"".$jcamp["clase"]."\" " : NULL;
				$Campo.= (isset($jcamp["click"]))? " onclick=\"javascript:".$jcamp["click"]."\" " : NULL;	
				$Campo.= (isset($jcamp["toltip"]))? " onMouseOver=\"MensajeTip('".$jcamp["toltip"]."')\" onMouseOut=\"UnTip()\" ":NULL;
				$Campo.= " style=\"cursor:pointer\"/>";
				break;
			case "simple":
				$Campo.= (isset($jcamp["valor"]))? $jcamp["valor"] : NULL;
				break;
			case 'textarea':
			case 'area':
				$Campo.="<textarea ";
				$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
				$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : NULL;
				$Campo.= (isset($jcamp["locked"]) && $jcamp["locked"]=="SI")? " disabled " : NULL;
				$Campo.= " style=\"";
				$Campo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px; " : NULL;
				$Campo.= (isset($jcamp["alto"]))?  " height:".$jcamp["alto"]."px; " : NULL;
				$Campo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
				$Campo.= " \" >";				
				$Campo.= (isset($jcamp["valor"]))? $jcamp["valor"]: NULL;
				$Campo.= "</textarea>";
				break;
			case 'texto': 
			case 'text' :
				$Campo.= "<input type=\"text\" ";
				$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
				$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : NULL;
				$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : NULL;
				$Campo.= (isset($jcamp["locked"]) && $jcamp["locked"]=="SI")? " readonly " : NULL;
				$Campo.= (isset($jcamp["bloqueado"]) && $jcamp["bloqueado"]=="SI")? " disabled " : NULL;
				$Campo.= (isset($jcamp["blur"]))? " onblur=\"".$jcamp["blur"]."\" " : NULL;
				$Campo.= (isset($jcamp["keyup"]))? " onkeyup=\"".$jcamp["keyup"]."\" " : NULL;
				$Campo.= (isset($jcamp["dclick"]))? " onDblClick=\"".$jcamp["dclick"]."\" " : NULL;	
				$Campo.= (isset($jcamp["keypress"]))? " onkeypress=\"".$jcamp["keypress"]."\" " : NULL;
				$Campo.= (isset($jcamp["maxchar"]))? " maxlength=\"".$jcamp["maxchar"]."\" " : NULL;	
				$Campo.= (isset($jcamp["clase"]) && $jcamp["clase"]<>"")? " class=\"".$jcamp["clase"]."\" ":"";
				$Campo.= " style=\"";
				$Campo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px; " : NULL;
				$Campo.= (isset($jcamp["alto"]))?  " height:".$jcamp["alto"]."px; " : " height:18px; ";
				$Campo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
				$Campo.= " \" autocomplete='off' >";
				break;
			case 'archivo': 
			case 'file' :
				$Campo.= "<input type=\"file\" ";
				$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
				$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : NULL;
				$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : NULL;
				$Campo.= (isset($jcamp["locked"]) && $jcamp["locked"]=="SI")? " readonly " : NULL;
				$Campo.= (isset($jcamp["bloqueado"]) && $jcamp["bloqueado"]=="SI")? " disabled " : NULL;
				$Campo.= (isset($jcamp["blur"]))? " onblur=\"".$jcamp["blur"]."\" " : NULL;
				$Campo.= (isset($jcamp["keyup"]))? " onkeyup=\"".$jcamp["keyup"]."\" " : NULL;
				$Campo.= (isset($jcamp["dclick"]))? " onDblClick=\"".$jcamp["dclick"]."\" " : NULL;	
				$Campo.= (isset($jcamp["keypress"]))? " onkeypress=\"".$jcamp["keypress"]."\" " : NULL;
				$Campo.= (isset($jcamp["maxchar"]))? " maxlength=\"".$jcamp["maxchar"]."\" " : NULL;	
				$Campo.= (isset($jcamp["clase"]) && $jcamp["clase"]<>"")? " class=\"".$jcamp["clase"]."\" ":"";
				$Campo.= " style=\"";
				$Campo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px; " : NULL;
				$Campo.= (isset($jcamp["alto"]))?  " height:".$jcamp["alto"]."px; " : " height:18px; ";
				$Campo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
				$Campo.= " \" autocomplete='off' >";
				break;
			case 'oculto':				
			case 'hidden':				
				$Campo.= "<input type=\"hidden\" ";
				$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
				$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : NULL;
				$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : NULL;
				$Campo.= (isset($jcamp["locked"]))? "readonly " : NULL;
				$Campo.= (isset($jcamp["blur"]))? " onblur=\"".$jcamp["blur"]."\" " : NULL;
				$Campo.= (isset($jcamp["keyup"]))? " onkeyup=\"".$jcamp["keyup"]."\" " : NULL;
				$Campo.= (isset($jcamp["dclick"]))? " onDblClick=\"".$jcamp["dclick"]."\" " : NULL;	
				$Campo.= (isset($jcamp["keypress"]))? " onkeypress=\"".$jcamp["keypress"]."\" " : NULL;
				$Campo.= (isset($jcamp["maxchar"]))? " maxlength=\"".$jcamp["maxchar"]."\" " : NULL;	
				$Campo.= (isset($jcamp["clase"]))?"class=\"".$jcamp["clase"]."\" ":"";
				$Campo.= " style=\"";
				$Campo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px; " : NULL;
				$Campo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
				$Campo.= " \" autocomplete='off' >";
				break;
			case 'clave':
			case 'pass':
			case 'password':
				$Campo.= "<input type=\"password\" ";
				$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
				$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : NULL;
				$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : NULL;
				$Campo.= (isset($jcamp["locked"]))? "readonly " : NULL;
				$Campo.= " style=\"";
				$Campo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px; " : NULL;
				$Campo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
				$Campo.= " \" >";
				break;
			case 'select':
				$varComboTodos = (isset($jcamp["combo"]["todos"]))?$jcamp["combo"]["todos"]:NULL;
				$Campo.=$this->SetCombo((isset($jcamp["name"]))? $jcamp["name"] : NULL, 
										$jcamp["combo"]["sql"], 
										(isset($jcamp["valor"]))? $jcamp["valor"] : NULL,
										(isset($jcamp["combo"]["ancho"]))?$jcamp["combo"]["ancho"]:NULL,
										(isset($jcamp["combo"]["reload"]))?$jcamp["combo"]["reload"]:NULL,
										(isset($jcamp["combo"]["titulo"]))?$jcamp["combo"]["titulo"]:NULL,
										(isset($jcamp["combo"]["todos"]))?$jcamp["combo"]["todos"]:NULL,
										(isset($jcamp["combo"]["sincod"]))?$jcamp["combo"]["sincod"]:NULL,
										(isset($jcamp["combo"]["locked"]))?$jcamp["combo"]["locked"]:NULL,
										(isset($jcamp["combo"]["clase"]) && $jcamp["combo"]["clase"]<>"")?$jcamp["combo"]["clase"]:NULL
										);
				break;
			case 'lista':
				$varComboTodos = (isset($jcamp["combo"]["todos"]))?$jcamp["combo"]["todos"]:NULL;
				$Campo.=$this->SetLista((isset($jcamp["name"]))? $jcamp["name"] : NULL, 
										$jcamp["combo"]["sql"], 
										(isset($jcamp["valor"]))? $jcamp["valor"] : NULL,
										(isset($jcamp["combo"]["ancho"]))?$jcamp["combo"]["ancho"]:NULL,
										(isset($jcamp["combo"]["reload"]))?$jcamp["combo"]["reload"]:NULL,
										(isset($jcamp["combo"]["titulo"]))?$jcamp["combo"]["titulo"]:NULL,
										(isset($jcamp["combo"]["todos"]))?$jcamp["combo"]["todos"]:NULL,
										(isset($jcamp["combo"]["sincod"]))?$jcamp["combo"]["sincod"]:NULL,
										(isset($jcamp["combo"]["locked"]))?$jcamp["combo"]["locked"]:NULL,
										(isset($jcamp["combo"]["clase"]) && $jcamp["combo"]["clase"]<>"")?$jcamp["combo"]["clase"]:NULL
										);
				break;
			case "lista_doble":
				$Campo.= "<table>";
				$Campo.= "<tr>";
				$Campo.= "<td>";
				
				$varLista1 = array(	"id"	=> (isset($jcamp["id1"]))? $jcamp["id1"]:NULL, 
								  	"name"	=> (isset($jcamp["name1"]))? $jcamp["name1"]:NULL, 
								  	"sql"	=> (isset($jcamp["sql"]))? $jcamp["sql"] : NULL,
								  	"filas"	=> (isset($jcamp["filas"]))? $jcamp["filas"] : NULL, 
									"valor"	=> (isset($jcamp["valor"]))? $jcamp["valor"] : NULL,
									"ancho"	=> (isset($jcamp["ancho"]))?$jcamp["ancho"]:NULL,
									"alto"	=> (isset($jcamp["alto"]))?$jcamp["alto"]:NULL,
									"sincod"=> (isset($jcamp["sincod"]))?$jcamp["sincod"]:NULL,
									"clase"	=> (isset($jcamp["clase"]))?$jcamp["clase"]:NULL,
									"locked"=> (isset($jcamp["locked"]))?$jcamp["locked"]:NULL,
									"dclick"=> (isset($jcamp["dclick1"]))?$jcamp["dclick1"]:NULL
									);
				$Campo.= $this->setLista($varLista1);
				$Campo.= "</td>";
				$Campo.= "<td>";
				$Campo.="<input type=\"button\" value=\">>\" onclick=\"".$jcamp["dclick1"]."\">";
				$Campo.="<br><br><input type=\"button\" value=\"<<\"  onclick=\"".$jcamp["dclick2"]."\">";
				$Campo.= "</td>";
				$Campo.= "<td>";
				$varLista1 = array(	"id"	=> (isset($jcamp["id2"]))? $jcamp["id2"]:NULL, 
								  	"name"	=> (isset($jcamp["name2"]))? $jcamp["name2"]:NULL, 
								  	"filas"	=> (isset($jcamp["filas"]))? $jcamp["filas"] : NULL, 
									"valor"	=> (isset($jcamp["valor"]))? $jcamp["valor"] : NULL,
									"ancho"	=> (isset($jcamp["ancho"]))?$jcamp["ancho"]:NULL,
									"alto"	=> (isset($jcamp["alto"]))?$jcamp["alto"]:NULL,
									"sincod"=> (isset($jcamp["sincod"]))?$jcamp["sincod"]:NULL,
									"clase"	=> (isset($jcamp["clase"]))?$jcamp["clase"]:NULL,
									"locked"=> (isset($jcamp["locked"]))?$jcamp["locked"]:NULL,
									"dclick"=> (isset($jcamp["dclick2"]))?$jcamp["dclick2"]:NULL
									);
				$Campo.= $this->setLista($varLista1);
				$Campo.= "</td>";
				$Campo.= "</tr>";
				$Campo.= "</table>";
				break;
			case "fecha":
			case "date":
				$Campo.= "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
				$Campo.= "<tr>";
				$Campo.= "<td ";
				$Campo.= (isset($jcamp["ancho"]))? " width=\"".($jcamp["ancho"]+10)."\" " : NULL;
				$Campo.= "valign=\"middle\">";
				$Campo.= "<input type=\"text\" autocomplete='off' ";
				$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
				$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : NULL;
				$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : NULL;
				$Campo.= (isset($jcamp["locked"]))? "readonly " : NULL;
				$Campo.= (isset($jcamp["blur"]))? " onblur=\"".$jcamp["blur"]."\" " : NULL;
				$Campo.= (isset($jcamp["dclick"]))? " onDblClick=\"".$jcamp["dclick"]."\" " : NULL;	
				$Campo.= (isset($jcamp["keypress"]))? " onkeypress=\"".$jcamp["keypress"]."\" " : NULL;	
				$Campo.= (isset($jcamp["clase"]) && $jcamp["clase"]<>"")? " class=\"".$jcamp["clase"]."\" ":"";
				$Campo.= " style=\"";
				$Campo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px; " : NULL;
				$Campo.= (isset($jcamp["alto"]))? " height:".$jcamp["alto"]."px; " : "height:18px; ";
				$Campo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
				
				$Campo.= " \" >";
				$Campo.= "</td>";
				$Campo.= "<td align=\"left\" valign=\"middle\">";
				$Campo.= "&nbsp;&nbsp;<img id=\"imgfech\" name=\"imgfech\" class=\"PopcalTrigger\" style=\"cursor:pointer\" src=\"images/b_calendario.gif\" ";
				$Campo.= "width=34 height=22 border=0 ";
				$Campo.= "onClick=\"if(self.gfPop)gfPop.fPopCalendar(";
				$Campo.= (isset($jcamp["name"]))?"document.getElementById('".$jcamp["name"]."')": NULL;
				$Campo.= ");return false;\" >";
				//$Campo.= ">";
				$Campo.= "</td>";
				$Campo.= "<td valign=\"middle\">";
				$Campo.= "&nbsp;&nbsp;(Ej. ".date("d/m/Y",time()).")";
				$Campo.= "</td>";
				$Campo.= "</tr>";
				$Campo.= "</table>";
				break;
			
			case "fecha2":
			case "date2":
				$Campo.= "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
				$Campo.= "<tr>";
				$Campo.= "<td ";
				$Campo.= (isset($jcamp["ancho"]))? " width=\"".($jcamp["ancho"]+10)."\" " : NULL;
				$Campo.= "valign=\"middle\">";
				$Campo.= "<input type=\"text\" autocomplete='off' ";
				$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
				$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : NULL;
				$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : NULL;
				$Campo.= (isset($jcamp["locked"]))? "readonly " : NULL;
				$Campo.= (isset($jcamp["blur"]))? " onblur=\"".$jcamp["blur"]."\" " : NULL;
				$Campo.= (isset($jcamp["dclick"]))? " onDblClick=\"".$jcamp["dclick"]."\" " : NULL;	
				$Campo.= (isset($jcamp["keypress"]))? " onkeypress=\"".$jcamp["keypress"]."\" " : NULL;	
				$Campo.= (isset($jcamp["clase"]) && $jcamp["clase"]<>"")? " class=\"".$jcamp["clase"]."\" ":"";
				$Campo.= " style=\"";
				$Campo.= (isset($jcamp["ancho"]))? " width:".$jcamp["ancho"]."px; " : NULL;
				$Campo.= (isset($jcamp["alinear"]))? "text-align:".$jcamp["alinear"] : NULL;
				
				$Campo.= " \" >";
				$Campo.= "</td>";
				$Campo.= "<td align=\"left\" valign=\"middle\">";
				$Campo.= "&nbsp;&nbsp;<img id=\"imgfech\" name=\"imgfech\" class=\"ui-datepicker-trigger\" style=\"cursor:pointer\" src=\"images/b_calendario.gif\" ";
				$Campo.= "width=34 height=22 border=0 >";
				//$Campo.= ">";
				$Campo.= "</td>";
				$Campo.= "<td valign=\"middle\">";
				$Campo.= "&nbsp;&nbsp;(Ej. ".date("d/m/Y",time()).")";
				$Campo.= "</td>";
				$Campo.= "</tr>";
				$Campo.= "</table>";
				break;
				
			case 'select_simple':
				$Campo.=$this->SetComboSimple($jcamp["name"], 
											  $jcamp["combo"]["sql"], 
											  $jcamp["valor"], 
											  (isset($jcamp["combo"]["ancho"]))?$jcamp["combo"]["ancho"]:NULL,
											  (isset($jcamp["combo"]["reload"]))?$jcamp["combo"]["reload"]:NULL,
											  (isset($jcamp["combo"]["titulo"]))?$jcamp["combo"]["titulo"]:NULL,
											  (isset($jcamp["combo"]["todos"]))?$jcamp["combo"]["todos"]:NULL,
											  (isset($jcamp["combo"]["sincod"]))?$jcamp["combo"]["sincod"]:NULL,
											  (isset($jcamp["combo"]["locked"]))?$jcamp["combo"]["locked"]:NULL
											  );
				break;	
			case 'varios':
				$i = 0;
				$Campo.= "<table><tr>";
				while($i<count($jcamp["contenido"])) {
					$Campo.= "<td";
					$Campo.= (isset($jcamp["contenido"][$i]["clase_in"]))?" class = \"".$jcamp["contenido"][$i]["clase_in"]."\" ":NULL;
					$Campo.= (isset($jcamp["contenido"][$i]["ancho"]))?" width = \"".$jcamp["contenido"][$i]["ancho"]."\" ":NULL;
					$Campo.= ">";
					$Campo.= (isset($jcamp["contenido"][$i]["div"]))?"<div id=\"".$jcamp["contenido"][$i]["div"]."\">":"";
					$Campo.= $this->SetCampo($jcamp["contenido"][$i]);
					$Campo.= (isset($jcamp["contenido"][$i]["div"]))?"</div>":"";
					$Campo.= "</td>";
					$i++;
					}
				$Campo.= "</tr></table>";
				break;
			case "tabla":
				$i = 0;
				$Campo.= "<table>";
				while($i<count($jcamp["contenido"])) {
					$Campo.= "<tr>";
					$Campo.= "<td>";
					$Campo.= (isset($jcamp["contenido"][$i]["div"]))?"<div id=\"".$jcamp["contenido"][$i]["div"]."\">":"";
					$Campo.= $this->SetCampo($jcamp["contenido"][$i]);
					$Campo.= (isset($jcamp["contenido"][$i]["div"]))?"</div>":"";
					$Campo.= "</td>";
					$Campo.= "</tr>";
					$i++;
					}
				$Campo.= "</table>";
				break;
			case "multiple":
			case "multi":
				if (isset($jcamp["sql"]) && $jcamp["sql"]<>"") {					
					$Campo.= "<table id=\"tabla\" width=\"100%\" cellspacing=0 cellpadding=0 align=\"center\" ";
					$Campo.= "style=\"padding:0;margin-top:0px;border-collapse:collapse;\">";
					$this->AbrirConexion();
					$rs = $this->Query($jcamp["sql"]);
					$this->CerrarConexion();
					while($fila = $this->FetchArray($rs)) {
						$Campo.="<tr>";						
						$Campo.="<td width=\"100\" nowrap=\"nowrap\"><b>";
						$Campo.= $fila[0]." - ".$fila[1]; 
						$Campo.="</b></td>";
						$Campo.="<td class=\"interior\">"; 
						$Campo.= $fila[1];
						$Campo.="</td>";
						$Campo.="</tr>"; 
						}					 
					$Campo.="</table>";
					}
				break;
			case "check":
				if (isset($jcamp["sql"]) && $jcamp["sql"]<>"") {					
					$Campo.= "<table width=\"100%\" border=0 cellpadding=0 cellspacing=0>";
					$this->AbrirConexion();
					$rs = $this->Query($jcamp["sql"]);
					$this->CerrarConexion();
					while($fila = $this->FetchArray($rs)) {
						$Campo.="<tr>";
						$Campo.="<td width=\"20\" height=\"18\" align=\"center\" valign=\"middle\">";						
						$varPos = $_POST[$jcamp["name"]][$fila[0]];
						$Campo.="<input ";
						$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
						$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."[".$fila[0]."]\" " : NULL;
						$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : " value=\"".$fila[0]."\" ";
						$Campo.= (isset($jcamp["checked"]) && $jcamp["checked"]=="SI")? " checked " : NULL;
						$Campo.= (isset($varPos))? " checked " : NULL;
						$Campo.=" type=\"checkbox\" >";
						$Campo.="</td>";
						$Campo.="<td width=\"100%\" valign=\"middle\">&nbsp;";
						$Campo.=$fila[0]." - ".$fila[1];
						$Campo.="</td>";
						$Campo.="</tr>";  
						}
					$Campo.="<tr>";
					$Campo.="<td width=\"20\" height=\"18\" align=\"center\" valign=\"middle\">";
					$Campo.="<input ";
					$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : NULL;
					$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."[Todos]\" " : NULL;
					$Campo.= " type=\"checkbox\" value=\"Todos\" ";
					$varPos = $_POST[$jcamp["name"]]["Todos"];
					$Campo.= (isset($varPos))? " checked " : NULL;
					$Campo.="/>";    
					$Campo.="</td>";
					$Campo.="<td width=\"100%\" valign=\"middle\">&nbsp;";
					$Campo.="Todos - TODOS LOS ANTERIORES";
					$Campo.="</td>";
					$Campo.="</tr>";  
					$Campo.="</table>";
					}
				else {
					$Campo.="<input ";
					$Campo.= (isset($jcamp["id"]))? " id=\"".$jcamp["id"]."\" " : "";
					$Campo.= (isset($jcamp["name"]))? " name=\"".$jcamp["name"]."\" " : "";
					$Campo.= (isset($jcamp["valor"]))? " value=\"".$jcamp["valor"]."\" " : "";
					$Campo.= (isset($jcamp["checked"]) && $jcamp["checked"]=="SI")? " checked " : "";
					$Campo.=" type=\"checkbox\" >";
					}
				break;
			}
		return $Campo;
		}
	
	function setTablaForm($varTable) {
		$Form = "";
		//Creamos la Cabecera del Formulario si Existe
		if(isset($varTable["formu"]) && count($varTable["formu"])>0) {
			$Form.= "<form ";
			$Form.= (isset($varTable["formu"]["id"]))? "id=\"".$varTable["formu"]["id"]."\" ":"";
			$Form.= (isset($varTable["formu"]["name"]))? "name=\"".$varTable["formu"]["name"]."\" ":"";
			$Form.= (isset($varTable["formu"]["metodo"]))?"method=\"".$varTable["formu"]["metodo"]."\" ":"";
			$Form.= (isset($varTable["formu"]["accion"]))?"action=\"".$varTable["formu"]["accion"]."\" ":"";
			$Form.= (isset($varTable["formu"]["onsubmit"]))?"onSubmit=\"".$varTable["formu"]["onsubmit"]."\" ":"";
			$Form.= (isset($varTable["formu"]["encripta"]) && $varTable["formu"]["encripta"]=="SI")?"enctype=\"multipart/form-data\" ":"";
			$Form.= ">";			
			}		
		//Cuerpo de la Tabla		
		$Form.= "<table style=\"padding:0;margin-top:0px;border-collapse:collapse;\" ";
		$Form.= (isset($varTable["id"]))? "id=\"".$varTable["id"]."\" ":"";
		$Form.= (isset($varTable["name"]))? "name=\"".$varTable["name"]."\" ":"";
		$Form.= (isset($varTable["tamano"]))?"width=\"".$varTable["tamano"]."\" ":"";
		$Form.= "align=\"center\"  cellpadding=\"0\" cellspacing=\"0\">";
		$Form.="<tr>";		
		$Form.="<td height=30 colspan=2 align=center valign=middle ";
		$Form.= (isset($varTable["clase"]))?"class=\"".$varTable["clase"]."\" ":"";
		$Form.=">";
		$Form.= (isset($varTable["titulo"]))?$varTable["titulo"]:"SIN TITULO";
		//Contenido Campos Ocultos
		if(isset($varTable["ocult"]) && count($varTable["ocult"])>0) {
			foreach($varTable["ocult"] as $varCampo) {			
				if(isset($varCampo)) {
					$Form.= $this->SetCampo($varCampo);
					}
				}
			}
		$Form.="</td>";
		$Form.="</tr>";	
		//Contenido Campos de la Tabla
		if(isset($varTable["conte"]) && count($varTable["conte"])>0) {
			foreach($varTable["conte"] as $varCampo) {
				$Form.= "<tr><td ";
				$Form.= (isset($varCampo["tamano"]))?"width=\"".$varCampo["tamano"]."\" ":"";
				$Form.= (isset($varCampo["clase1"]))?"class=\"".$varCampo["clase1"]."\" ":"";		
				$Form.= ">";
				$Form.= (isset($varCampo["titulo"]))?$varCampo["titulo"]:"Sin Titulo";
				$Form.="</td><td ";
				$Form.= (isset($varCampo["clase2"]))?"class=\"".$varCampo["clase2"]."\" ":"";
				$Form.= ">";
				$Form.= (isset($varCampo["div"]))?"<div id=\"".$varCampo["div"]."\">":"";
				if(isset($varCampo["campo"])) {
					$Form.= $this->SetCampo($varCampo["campo"]);
					}
				elseif(isset($varCampo["texto"]) && $varCampo["texto"]<>"") {
					$Form.= $varCampo["texto"];
					}		
				$Form.= (isset($varCampo["div"]))?"</div>":"";			
				$Form.="</td>";
				$Form.="</tr>";
				}
			}
		//Lista de Botones
		if(isset($varTable["boton"]) && count($varTable["boton"])>0) {
			$Form.= "<tr>";		
			$Form.= "<td height=\"35\" colspan=2 align=center valign=middle ";
			$Form.= (isset($varTable["clase"]))?"class=\"".$varTable["clase"]."\" ":"";
			$Form.= ">";		
			foreach($varTable["boton"] as $varBoton) {
				$Form.=$this->SetBoton($varBoton)."&nbsp;";
				}
			$Form.="</td>";
			$Form.="<tr>";
			}	
		$Form.="</table>";
		
		//Creamos el Pie del Formulario si Existe
		if(isset($varTable["formu"]) && count($varTable["formu"])>0) {
			$Form.= "</form>";
			}
		return $Form;
		}
	
	function setTablaFormTabs($varForm, $varTable, $varTabs) {
		$Form = "";
		//Creamos la Cabecera del Formulario si Existe
		if(isset($varForm) && count($varForm)>0) {
			$Form.= "<form ";
			$Form.= (isset($varForm["id"]))? "id=\"".$varForm["id"]."\" ":"";
			$Form.= (isset($varForm["name"]))? "name=\"".$varForm["name"]."\" ":"";
			$Form.= (isset($varForm["metodo"]))?"method=\"".$varForm["metodo"]."\" ":"";
			$Form.= (isset($varForm["accion"]))?"action=\"".$varForm["accion"]."\" ":"";
			$Form.= (isset($varForm["onsubmit"]))?"onSubmit=\"".$varForm["onsubmit"]."\" ":"";
			$Form.= ">";			
			}
		if(isset($varTable) && count($varTable)>0) {	
			$Form.= $this->setTablaForm($varTable);
			}
		if(isset($varTabs) && count($varTabs)>0) {
			$Form.= "<table align=\"center\">";
			$Form.= "<tr>";
			$Form.= "<td>";
			$Form.= "<div id=\"TabbedPanels1\" class=\"TabbedPanels\">";
		  	$Form.= "<ul class=\"TabbedPanelsTabGroup\">";			
			/*Cabecera de los Tabs*/
			foreach($varTabs as $varCampo) {
				$Form.="<li class=\"TabbedPanelsTab\" tabindex=\"0\">";
				$Form.= (isset($varTable["name"]))? $varCampo["name"]:"Sin Nombre";
				$Form.="</li>";
				}
			$Form.= "</ul>";
			$Form.= "<div class=\"TabbedPanelsContentGroup\">";
			/*Contenido de los Tabs*/
			foreach($varTabs as $varCampo) {
				$Form.="<div class=\"TabbedPanelsContent\" style=\"";
				//$Form.= (isset($varCampo["alto"]))?  "height:".$varCampo["alto"]."px; ": NULL;
				$Form.= (isset($varCampo["ancho"]))?  "width:".$varCampo["ancho"]."px; ": NULL;
				$Form.="\">";
				if($varCampo["tipo"]=="form") {
					$Form.= $this->setTablaForm($varCampo["tabla"]);
					}
				elseif($varCampo["tipo"]=="det") {
					$Form.= $this->setTablaFormDet($varCampo["tabla"]);
					}
				else {
					$Form.= (isset($varTable["name"]))? $varCampo["name"]:"Sin Nombre";
					}
				$Form.="</div>";
				}			
			$Form.= "</div>";
			$Form.= "</div>";
			$Form.= "</td>";
			$Form.= "</tr>";
			$Form.= "</table>";
			}
		//Creamos el Pie del Formulario si Existe
		if(isset($varForm) && count($varForm)>0) {
			$Form.= "</form>";
			}
		return $Form;
		}
	
	function setTablaFormDet($varTable) {	
		$varComb = count($varTable["conte"]);		
		
		//Cuerpo de la Tabla				
		$Form.= "<table style=\"padding:0;margin-top:0px;border-collapse:collapse;\" ";
		$Form.= (isset($varTable["id"]))? "id=\"".$varTable["id"]."\" ":"";
		$Form.= (isset($varTable["name"]))? "name=\"".$varTable["name"]."\" ":"";
		$Form.= (isset($varTable["tamano"]))?"width=\"".$varTable["tamano"]."\" ":"";
		$Form.= "align=\"center\"  cellpadding=\"0\" cellspacing=\"0\">";		
		$Form.= "<thead>";	
		$Form.="<tr>";		
		$Form.="<td height=\"30\" colspan=$varComb align=center valign=middle ";
		$Form.= (isset($varTable["clase"]))?"class=\"".$varTable["clase"]."\" ":"";
		$Form.=">";
		$Form.= (isset($varTable["titulo"]))?$varTable["titulo"]:"SIN TITULO";
		
		//Contenido Campos Ocultos
		if(isset($varTable["ocult"]) && count($varTable["ocult"])>0) {
			foreach($varTable["ocult"] as $varCampo) {			
				if(isset($varCampo)) {					
					$Form.= $this->SetCampo($varCampo);
					}
				}
			}
		$Form.="</td>";
		$Form.="</tr>";		
		
		//Contenido Cabecera de la Tabla		
		if(isset($varTable["conte"]) && count($varTable["conte"])>0) {						
			$Form.= "<tr>";
			foreach($varTable["conte"] as $varCampo) {
				if(!isset($varCampo["oculto"]) || $varCampo["oculto"]=="NO") {
					$Form.= "<td ";
					$Form.= (isset($varCampo["tamano"]))?"width=\"".$varCampo["tamano"]."\" ":"";
					$Form.= (isset($varCampo["alto"]))?"height=\"".$varCampo["alto"]."\" ":"";
					$Form.= (isset($varCampo["clase"]))?"class=\"".$varCampo["clase"]."\" ":"";		
					$Form.= ">";
					$Form.= (isset($varCampo["titulo"]))?$varCampo["titulo"]:"Sin Titulo";						
					$Form.="</td>";	
					}
				}
			$Form.="</tr>";			
			}
		$Form.= "</thead>";	
		
		//Contenido Detalle de la Tabla	
		if(isset($varTable["sql"]) && count($varTable["sql"])>0) {
			$this->AbrirConexion();
			$rs = $this->Query($varTable["sql"]);
			$this->CerrarConexion();
			$NumCol =  $this->NumField($rs);
			$j=0;
			$Form.= "<tbody>";
			$varPos=0;
			while($fila = $this->FetchArray($rs)) {
				$j++;
				$varPos=$varPos+1;
				$varChecke = (isset($varTable["conte"][0]["campo"]["id_check"]))? $fila[$varTable["conte"][0]["campo"]["id_check"]]:NULL;
				$varMarcar = (isset($varChecke) &&  $varChecke=="SI")?"marked":"";
				if($j%2==0) $Form.= "<tr class=\"par $varMarcar\">";
				else $Form.= "<tr class=\"impar $varMarcar\">";				
				for ($i=0;$i<=$NumCol-1;$i++) {	
					if(!isset($varTable["conte"][$i]["oculto"]) || $varTable["conte"][$i]["oculto"]=="NO") {
						$Form.= "<td class=\"interior\" height=\"22\" ";
						if(isset($varTable["conte"][$i]["alinear"])) $Form.="align=".$varTable["conte"][$i]["alinear"];
						if(isset($varTable["conte"][$i]["fijo"])) $Form.=" nowrap=\"nowrap\" ";
						$Form.= ">";										
						if(isset($varTable["conte"][$i]["campo"]) && count($varTable["conte"][$i]["campo"])>=1) {						
							//Funcion Doble Click
							$icon = 0;
							$varDClick="";
							if(count($varTable["conte"][$i]["campo"]["dclick"])>1) {
								foreach($varTable["conte"][$i]["campo"]["dclick"] as $valor=>$ArrayDClick) {
									if($valor=="funcion") {
										$varDClick.= $ArrayDClick."(";
										}
									else {
										foreach($ArrayDClick as $varCampo) {
											if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="IN") {
												if ($icon==0)
													$varDClick.= "'".$varCampo["campo"]."'";
												else
													$varDClick.= ",'".$varCampo["campo"]."'";
												}
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX") {
												if ($icon==0)
													$varDClick.= "'".$varPos."'";
												else
													$varDClick.= ",'".$varPos."'";
												}
											else
												if ($icon==0)
													$varDClick.= "'".$varTable["conte"][$varCampo["campo"]]["campo"]["id"].$varPos."'";
												else
													$varDClick.= ",'".$varTable["conte"][$varCampo["campo"]]["campo"]["id"].$varPos."'";
											$icon++;
											}									
										}
									
									}
								$varDClick.= ")";
								}
							else {
								$varDClick = $varTable["conte"][$i]["campo"]["dclick"];
								}
							
							//Funcion BLUR
							$icon = 0;
							$varBlur="";
							if(count($varTable["conte"][$i]["campo"]["blur"])>1) {
								foreach($varTable["conte"][$i]["campo"]["blur"] as $valor=>$ArrayBlur) {
									if($valor=="funcion") {
										$varBlur.= $ArrayBlur."(";
										}
									else {
										foreach($ArrayBlur as $varCampo) {
											if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="IN") {
												if ($icon==0)
													$varBlur.= "'".$varCampo["campo"]."'";
												else
													$varBlur.= ",'".$varCampo["campo"]."'";
												}
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM") {
												if ($icon==0)
													$varBlur.= $varCampo["campo"];
												else
													$varBlur.= $varCampo["campo"];
												}
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX") {
												if ($icon==0)
													$varBlur.= "'".$varPos."'";
												else
													$varBlur.= ",'".$varPos."'";
												}
											else
												if ($icon==0)
													$varBlur.= "'".$varTable["conte"][$varCampo["campo"]]["campo"]["id"].$varPos."'";
												else
													$varBlur.= ",'".$varTable["conte"][$varCampo["campo"]]["campo"]["id"].$varPos."'";
											$icon++;
											}									
										}
									
									}
								$varBlur.= ")";
								}
							else {
								$varBlur = $varTable["conte"][$i]["campo"]["blur"];
								}
							
							//Funcion Key UP
							$icon = 0;
							$varKeyUp="";
							if(count($varTable["conte"][$i]["campo"]["keyup"])>1) {
								foreach($varTable["conte"][$i]["campo"]["keyup"] as $valor=>$ArrayKeyUp) {
									if($valor=="funcion") {
										$varKeyUp.= $ArrayKeyUp."(";
										}
									else {
										foreach($ArrayKeyUp as $varCampo) {
											if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="IN") {
												if ($icon==0)
													$varKeyUp.= "'".$varCampo["campo"]."'";
												else
													$varKeyUp.= ",'".$varCampo["campo"]."'";
												}
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX") {
												if ($icon==0)
													$varKeyUp.= "'".$varPos."'";
												else
													$varKeyUp.= ",'".$varPos."'";
												}
											else
												if ($icon==0)
													$varKeyUp.= "'".$varTable["conte"][$varCampo["campo"]]["campo"]["id"].$varPos."'";
												else
													$varKeyUp.= ",'".$varTable["conte"][$varCampo["campo"]]["campo"]["id"].$varPos."'";
											$icon++;
											}									
										}
									
									}
								$varKeyUp.= ")";
								}
							else {
								$varKeyUp = $varTable["conte"][$i]["campo"]["keyup"];
								}
							$varTipoDato = (isset($varTable["conte"][$i]["tipodat"]))? $varTable["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
							$varNumDec   = (isset($varTable["conte"][$i]["decimal"]))? $varTable["conte"][$i]["decimal"]:NULL;
							$varValor = ($varTable["conte"][$i]["campo"]["tipo"]<>"check")?$this->setFormatTipoDato($fila[$i], $varTipoDato, $varNumDec):$fila[$i];
							$varCheck = (isset($varTable["conte"][$i]["campo"]["id_check"]))? $fila[$varTable["conte"][$i]["campo"]["id_check"]]:NULL;
							$varCampo = array("tipo"	=>$varTable["conte"][$i]["campo"]["tipo"], 
											  "id"		=>$varTable["conte"][$i]["campo"]["id"].$varPos,
											  "name"	=>$varTable["conte"][$i]["campo"]["name"]."[]", 										  
											  "locked"	=>$varTable["conte"][$i]["campo"]["locked"],
											  "clase"	=>$varTable["conte"][$i]["campo"]["clase"], 
											  "img"		=>$varTable["conte"][$i]["campo"]["img"], 
											  "click"	=>$varTable["conte"][$i]["campo"]["click"], 
											  "checked"	=>$varCheck, 
											  "dclick"	=>$varDClick, 
											  "blur"	=>$varBlur, 
											  "keyup"	=>$varKeyUp,
											  "keypress"=>$varTable["conte"][$i]["campo"]["keypress"], 
											  "valor"	=>$varValor, 
											  "ancho"	=>$varTable["conte"][$i]["campo"]["ancho"],
											  "alinear"	=>$varTable["conte"][$i]["campo"]["alinear"]);
							$Form.=	$this->SetCampo($varCampo);
							
							}
						else {
							$varTipoDato = (isset($varTable["conte"][$i]["tipodat"]))? $varTable["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
							$varNumDec   = (isset($varTable["conte"][$i]["decimal"]))? $varTable["conte"][$i]["decimal"]:NULL;
							$Form.= $this->setFormatTipoDato($fila[$i], $varTipoDato, $varNumDec);							
							}
						$Form.="</td>";	
						}
					}
				$Form.="</tr>";
				}
			$Form.= "</tbody>";
			}
		//Lista de Botones
		if(isset($varTable["boton"]) && count($varTable["boton"])>0) {
			$Form.= "<tr>";		
			$Form.= "<td height=20 colspan=$varComb align=center valign=middle ";
			$Form.= (isset($varTable["clase"]))?"class=\"".$varTable["clase"]."\" ":"";
			$Form.= ">";		
			foreach($varTable["boton"] as $varBoton) {
				$Form.=$this->SetBoton($varBoton)."&nbsp;";
				}
			$Form.="</td>";
			$Form.="<tr>";
			}	
		$Form.="</table>";
		return $Form;
		}
				
	function setFormulario(
		$varForm	  = NULL, //Parametros de Formulario,
		$varTable	  = NULL, //Parametros de la Tabla
		$varContenido = NULL, //Parametros del Contenido
		$varBotones   = NULL, //Parametros de los Botones
		$varOculto    = NULL  //Parametros de los Campos Ocultos
		) {
		//Formulario
		$Form = "<form ";
		$Form.= (isset($varForm["id"]))? "id=\"".$varForm["id"]."\" ":"";
		$Form.= (isset($varForm["name"]))? "name=\"".$varForm["name"]."\" ":"";
		$Form.= (isset($varForm["metodo"]))?"method=\"".$varForm["metodo"]."\" ":"";
		$Form.= (isset($varForm["accion"]))?"action=\"".$varForm["accion"]."\" ":"";
		$Form.= (isset($varForm["onsubmit"]))?"onSubmit=\"".$varForm["onsubmit"]."\" ":"";
		$Form.= ">";
		//Tabla		
		$Form.= "<table id=\"tabla\" style=\"padding: 0; margin-top:0px;border-collapse:collapse;\" ";		
		$Form.= (isset($varTable["tamano"]))?"width=\"".$varTable["tamano"]."\" ":"";
		$Form.= "align=\"center\"  cellpadding=\"0\" cellspacing=\"0\">";
		$Form.="<tr>";		
		$Form.="<td height=18 colspan=2 align=center valign=middle ";
		$Form.= (isset($varTable["clase"]))?"class=\"".$varTable["clase"]."\" ":"";
		$Form.=">";
		$Form.= (isset($varTable["titulo"]))?$varTable["titulo"]:"SIN TITULO";
		//Contenido Campos Ocultos
		if(isset($varOculto)) {
			foreach($varOculto as $varCampo) {			
				if(isset($varCampo)) {
					$Form.= $this->SetCampo($varCampo);
					}
				}
			}
		$Form.="</td>";
		$Form.="</tr>";	
		//Contenido Campos
		foreach($varContenido as $varCampo) {
			$Form.= "<tr><td ";
			$Form.= (isset($varCampo["tamano"]))?"width=\"".$varCampo["tamano"]."\" ":"";
			$Form.= (isset($varCampo["clase1"]))?"class=\"".$varCampo["clase1"]."\" ":"";		
			$Form.= ">";
			$Form.= (isset($varCampo["titulo"]))?$varCampo["titulo"]:"Sin Titulo";
			$Form.="</td><td ";
			$Form.= (isset($varCampo["clase2"]))?"class=\"".$varCampo["clase2"]."\" ":"";
			$Form.= ">";
			$Form.= (isset($varCampo["div"]))?"<div id=\"".$varCampo["div"]."\">":"";
			if(isset($varCampo["campo"])) {
				$Form.= $this->SetCampo($varCampo["campo"]);
				}			
			$Form.= (isset($varCampo["div"]))?"</div>":"";			
			$Form.="</td>";
			$Form.="</tr>";
			}
		//Lista de Botones
		if(isset($varBotones) && count($varBotones)>0) {		
			$Form.= "<tr>";		
			$Form.= "<td height=20 colspan=2 align=center valign=middle ";
			$Form.= (isset($varTable["clase"]))?"class=\"".$varTable["clase"]."\" ":"";
			$Form.= ">";		
			foreach($varBotones as $varBoton) {
				$Form.=$this->SetBoton($varBoton)."&nbsp;";
				}
			$Form.="</td>";
			$Form.="<tr>";
			}		
		$Form.="</table>";
		$Form.= "</form>";
		return $Form;
		}
	
	function SetForm($json) {
		$k = json_decode($json, true);
		
		$Form ="<form ";
		$Form.= (isset($k["id"]))?"id=\"".$k["id"]."\" ":"";
		$Form.= (isset($k["name"]))?"name=\"".$k["name"]."\" ":"";
		$Form.= (isset($k["metodo"]))?"method=\"".$k["metodo"]."\" ":"";
		$Form.= (isset($k["accion"]))?"action=\"".$k["accion"]."\" ":"";
		$Form.= (isset($k["valida"]))?"onSubmit=\"".$k["valida"]."\" ":"";
		$Form.= " >";
		$Form.= "<table id=\"Busqueda\" style=\"padding: 0; margin-top:0px;border-collapse:collapse;\" align=\"center\"  cellpadding=\"0\" cellspacing=\"0\" ";		
		$Form.= (isset($k["tam_tabla"]))?"width=\"".$k["tam_tabla"]."\" ":"";		
		$Form.= " >";
		$Form.="<tr>";
		$Form.="<td height=18 colspan=2 align=center valign=middle class=\"$k[cla_titu]\">";
		$Form.= (isset($k["titulo"]))?$k["titulo"]:"";
		//Lista de Campos Ocultos
		$i = 0; 
		if(isset($k["oculto"])) {
			while($i<count($k["oculto"])) {
				$Form.=$this->SetCampo($k["oculto"][$i]);
				$i++;
				}
			}
		$Form.="</td>";
		$Form.="</tr>";
		
		//Lista de Campos
		$i = 0; 
		while($i<count($k['contenido'])) {
			$Form.="<tr>";
			$Form.="<td width=\"$k[tam_campo1]\" class=\"$k[cla_campo1]\">";
			$Form.=$k['contenido'][$i]['texto'];
			$Form.="</td>";			
			$Form.="<td width=\"$k[tam_campo2]\" class=\"$k[cla_campo2]\">";
			if(isset($k["contenido"][$i]["div"]))	$Form.= "<div id=\"".$k["contenido"][$i]["div"]."\">";
			$Form.=$this->SetCampo($k["contenido"][$i]);
			if(isset($k["contenido"][$i]["div"]))	$Form.= "</div>";
			$Form.="</td>";
			$Form.="</tr>";
			$i++;
			}
		$Form.="<tr>";		
		
		//Lista de Botones
		$Form.="<td height=20 colspan=2 align=center valign=middle class=\"$k[cla_titu]\">";		
		$i = 0; 
		if(isset($k["botones"])) {
			while($i<count($k["botones"])) {
				$Form.=$this->SetBoton($k["botones"][$i])."&nbsp;";
				$i++;
				}
			}
		$Form.="</td>";
		$Form.="<tr>";
		$Form.="</table>";
		$Form.="</form>";
		return $Form;
		}

	function SetTabla($json) {
		$k = json_decode($json, true);
		$Form ="<table id=\"$k[id]\" name=\"$k[name]\" style=\"padding: 0; margin-top:0px;border-collapse:collapse;\" ";
		$Form.="width=\"$k[tam_tabla]\" align=\"center\"  cellpadding=\"0\" cellspacing=\"0\">";
		$Form.="<!--DWLayoutTable-->";
		$Form.="<tr>";
		$Form.="<td height=18 colspan=2 align=center valign=middle class=\"$k[cla_titu]\">";
		$Form.= (isset($k["titulo"]))?$k["titulo"]:"";
		//Lista de Campos Ocultos
		$i = 0; 
		if(isset($k["oculto"])) {
			while($i<count($k["oculto"])) {
				$Form.=$this->SetCampo($k["oculto"][$i]);
				$i++;
				}
			}
		$Form.="</td>";
		$Form.="</tr>";
		
		//Lista de Campos
		$i = 0; 
		while($i<count($k["contenido"])) {
			$Form.="<tr>";
			$Form.="<td ";
			$Form.= (isset($k["tam_campo1"]))?"width=\"".$k["tam_campo1"]."\" ":"";
			$Form.= (isset($k["cla_campo1"]))?"class=\"".$k["cla_campo1"]."\" ":"";
			$Form.= ">";
			$Form.=$k['contenido'][$i]["texto"];
			$Form.="</td>";			
			$Form.="<td ";
			$Form.= (isset($k["tam_campo2"]))?"width=\"".$k["tam_campo2"]."\" ":"";
			$Form.= (isset($k["cla_campo2"]))?"class=\"".$k["cla_campo2"]."\" ":"";
			$Form.= ">";
			if(isset($k["contenido"][$i]["tipo"])) {
				$Form.=$this->SetCampo($k["contenido"][$i]);
				}
			else {
				$Form.=$k["contenido"][$i]["valor"];
				}
			$Form.="</td>";
			$Form.="</tr>";
			$i++;
			}
		if(isset($k["botones"])) {
			$Form.="<tr>";		
			//Lista de Botones
			$Form.="<td height=20 colspan=2 align=center valign=middle class=\"$k[cla_titu]\">";		
			$i = 0; 
			while($i<count($k["botones"])) {			
				$Form.="<input id=\"".$k["botones"][$i]["id"]."\" name=\"".$k["botones"][$i]["name"]."\" ";
				$Form.="type=\"".$k["botones"][$i]["tipo"]."\" style=\"width:".$k["botones"][$i]["ancho"]."px; ";
				$Form.="height:".$k["botones"][$i]["alto"]."px; font-size:10px; \" ";
				$Form.=" value=\"".$k["botones"][$i]["texto"]."\" onClick=\"".$k["botones"][$i]["click"]."\">&nbsp;";
				$i++;
				}
			$Form.="</td>";
			$Form.="<tr>";
			}
		$Form.="</table>";
		return $Form;
		}
	
	function setGridCombina($ArrayCabe 	  = NULL, //Array con las Cabeceras Combinadas
							$SqlDato	  = NULL, //Query que contiene los Datos para la Tabla
							$ArrayNivel	  = NULL, //Array con los Niveles de Agrupamiento de la Data
							$ArrayBoton	  = NULL, //Array con los Botones
							$ArrayFormat  = NULL  //Array con Formato Condicional
							)
	{	
	ini_set("memory_limit","64M");
	if(isset($ArrayCabe) && isset($SqlDato) && isset($ArrayNivel)) {		
		$varNumNiv = count($ArrayNivel);
		$varNumCol = 0;
		$varNumComb= count($ArrayCabe);
		foreach($ArrayCabe as $varCombina) {
			$varNumCol = $varNumCol + count($varCombina["det"]);
			}
		$varTamTab = $varNumCol*60+150;
		$this->AbrirConexion();
		$ArrayDato = $this->Query($SqlDato);
		$this->CerrarConexion();
		$Tabla = "<table class=\"matriz\" style=\"padding:0;margin-top:0px;border-collapse:collapse;\" ";
		$Tabla.= "width=\"$varTamTab\" align=center cellpadding=0 cellspacing=0>";	
		$Tabla.= "<tr>";
		$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\" height=20 ></td>";
		$Tabla.= "<td width=20  align=\"center\" class=\"FormTitulo\" rowspan=\"2\">".$ArrayNivel[0]["COD"]."</td>";
		$Tabla.= "<td width=150 align=\"center\" class=\"FormTitulo\" rowspan=\"2\" NOWRAP>".$ArrayNivel[0]["DES"]."</td>";
		$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
		foreach($ArrayCabe as $varCombina) {
			$varNumCob = count($varCombina["det"]);
			if(count($varCombina["bot"])>0)
				$varNumCob = $varNumCob + 1;
			$Tabla.= "<td width=50  align=\"center\" class=\"FormTitulo\" colspan=\"$varNumCob\">".$varCombina["cab"]."</td>";
			$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
			}	
		$Tabla.= "</tr>";
		$Tabla.= "<tr>";
		$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
		$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
		$varPos = $varNumNiv*2;

		$varDat[0] = $varDat[1] = $varDat[2] = $varDat[3] = $varDat[4] = $varDat[5] = $varDat[6] = $varDat[7] = 0;
		foreach($ArrayCabe as $varCombina) {
			foreach($varCombina["det"] as $varIndex=>$Data) {
				if(isset($varCombina["factor"])) {
					$varDat[$varPos] = $varCombina["factor"];
					}
				else {
					$varDat[$varPos] = 0;
					}
				
				$Tabla.= "<td width=50 align=\"center\" class=\"FormTitulo\">";
				if(isset($varCombina["toltip"][$varIndex]) && $varCombina["toltip"][$varIndex]<>"") {		
					$Tabla.= "<a href=\"#\" onMouseOver=\"MensajeTip('".$varCombina["toltip"][$varIndex]."')\" onMouseOut=\"UnTip()\"/>";
					$Tabla.= $Data;
					$Tabla.= "</a>";
					}
				else {
					$Tabla.= $Data;
					}
				$varPos = $varPos + 1;
				$Tabla.= "</td>";
				
				}
			if(count($varCombina["bot"])>0)
				$Tabla.= "<td  align=\"center\" class=\"FormTitulo\">OPT</td>";
			$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
			}
		$Tabla.= "</tr>";		
				
		$color1 = "#F5FAFA";
		$color2 = "#edf3f3";
		$colors = "#FFFF33";		
		$bgcolor1 = "bgcolor=$color1 onMouseOver=\"uno(this,'$colors')\" onMouseOut=\"dos(this,'$color1')\" ";		
		$bgcolor2 = "bgcolor=$color2 onMouseOver=\"uno(this,'$colors')\" onMouseOut=\"dos(this,'$color2')\" ";	
		
		$varNivel_3_Cod_ant = "";
		$varNivel_3_Cod_act = "";
		$varNivel_3_Nom = "";
		
		$varNivel_2_Cod_ant = "";
		$varNivel_2_Cod_act = "";
		$varNivel_2_Nom = "";
		
		$varNivel_1_Cod_ant = "";
		$varNivel_1_Cod_act = "";
		$varNivel_1_Nom = "";
		
		for($i=0; $i<=$varNumCol; $i++) {
			$varSumNiv_1[$i] = 0;
			$varSumNiv_2[$i] = 0;
			$varSumNiv_3[$i] = 0;
			$varSumTotal[$i] = 0;
			}
			
		$k=0;
		$varPosCol = 2;

		while($fila = $this->FetchArray($ArrayDato)) {			
			//Nivel 1 Por lo General Grupo de Supervisores
			//=============================================
			if($varNumNiv >= 2) {
				$varPosCol = 4;
				if($varNivel_1_Cod_act=="" && $varNivel_1_Cod_ant=="") {
					$varNivel_1_Cod_act = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Nom = $fila[$ArrayNivel[1]["DES"]];
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + 
											   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
											   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + $fila[$i+$varNumNiv*2-1];
						}		
					}
				else{
					$varNivel_1_Cod_act = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Cod_ant = $varNivel_1_Cod_ant;
					if($varNivel_1_Cod_act == $varNivel_1_Cod_ant) {
						for($i=0; $i<=$varNumCol; $i++) {
							if($varDat[$i+$varNumNiv*2-1]<>0) {
								$varFactor = $varDat[$i+$varNumNiv*2-1];
								$varSumNiv_1[$i] = $varSumNiv_1[$i] + 
												   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
												   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
								}
							else
								$varSumNiv_1[$i] = $varSumNiv_1[$i] + $fila[$i+$varNumNiv*2-1];							
							}	
						}
					}
				
				if($varNivel_1_Cod_act <> $varNivel_1_Cod_ant) {
					$Tabla.= "<tr>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\" height=20><b>$varNivel_1_Cod_ant</b></td>";
					$Tabla.= "<td class=\"FormTituloSub\" NOWRAP>$varNivel_1_Nom</td>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";	
					
					$varPos = 1;			
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $Data) {
							$Tabla.= "<td align=\"right\" class=\"FormTituloSub\">";
							if($varSumNiv_1[$varPos]>0) {
								if($varDat[$varPos+5]<>0) {
									$varFactor = $varDat[$varPos+5];
									$varSuma = floor($varSumNiv_1[$varPos]/$varFactor) + round($varSumNiv_1[$varPos])%round($varFactor)/100;								
									}
								else
									$varSuma = $varSumNiv_1[$varPos];
								
								if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
									$varPos1 = $varSumNiv_1[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
									$varPos2 = $varSumNiv_1[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
									$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
									}
								$Tabla.= number_format($varSuma, 2,'.',',');
								if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $Tabla.="%";
								}							
							$Tabla.="</td>";
							$varPos = $varPos + 1;
							}
						$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
						}		
					$Tabla.= "</tr>";
					
					$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Nom     = $fila[$ArrayNivel[1]["DES"]];
					for($i=1; $i<=$varNumCol;$i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_1[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_1[$i] = $fila[$i+$varNumNiv*2-1];
						}
					}
				}
			
			//Nivel 2: Por lo General Grupo de CD'S
			//=============================================
			if($varNumNiv >= 3) {
				$varPosCol = 6;
				$varColData = $i+$varNumNiv*2-1;
				if($varNivel_2_Cod_act=="" && $varNivel_2_Cod_ant=="") {
					$varNivel_2_Cod_act = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Nom = $fila[$ArrayNivel[2]["DES"]];
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_2[$i] = $varSumNiv_2[$i] + floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
											   ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_2[$i] = $varSumNiv_2[$i] + $fila[$i+$varNumNiv*2-1];
						}		
					}
				else{
					$varNivel_2_Cod_act = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Cod_ant = $varNivel_2_Cod_ant;
					if($varNivel_2_Cod_act == $varNivel_2_Cod_ant) {
						for($i=0; $i<=$varNumCol; $i++) {
							if($varDat[$i+$varNumNiv*2-1]<>0) {
								$varFactor = $varDat[$i+$varNumNiv*2-1];
								$varSumNiv_2[$i] = $varSumNiv_2[$i] + 
												   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
												   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
								}
							else
								$varSumNiv_2[$i] = $varSumNiv_2[$i] + $fila[$i+$varNumNiv*2-1];							
							}	
						}
					}
				
				if($varNivel_2_Cod_act <> $varNivel_2_Cod_ant) {
					$Tabla.= "<tr>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\" height=20><b>$varNivel_2_Cod_ant</b></td>";
					$Tabla.= "<td class=\"FormTituloSub\" NOWRAP>$varNivel_2_Nom</td>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
					$varPos = 1;			
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $Data) {
							$Tabla.= "<td align=\"right\" class=\"FormTituloSub\">";
							if($varSumNiv_2[$varPos]>0) {
								if($varDat[$varPos+5]<>0) {
									$varFactor = $varDat[$varPos+5];
									$varSuma = floor($varSumNiv_2[$varPos]/$varFactor) + round($varSumNiv_2[$varPos])%round($varFactor)/100;								
									}
								else
									$varSuma = $varSumNiv_2[$varPos];
								
								if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
									$varPos1 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
									$varPos2 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
									$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
									}
								$Tabla.= number_format($varSuma, 2,'.',',');
								if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $Tabla.="%";
								
								}
							$Tabla.="</td>";
							$varPos = $varPos + 1;
							}
						$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
						}			
					$Tabla.= "</tr>";
					
					$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Nom     = $fila[$ArrayNivel[2]["DES"]];
					for($i=1; $i<=$varNumCol;$i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_2[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_2[$i] = $fila[$i+$varNumNiv*2-1];	
						}
					
					if($varNumNiv == 3) {
						$Tabla.= "<tr>";
						$Tabla.= "<td height=25></td>";
						$Tabla.= "</tr>";
						
						$Tabla.= "<tr>";
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\" height=20 ></td>";
						$Tabla.= "<td width=20  align=\"center\" class=\"FormTitulo\" rowspan=\"2\">".$ArrayNivel[0]["COD"]."</td>";
						$Tabla.= "<td width=350 align=\"center\" class=\"FormTitulo\" rowspan=\"2\">".$ArrayNivel[0]["DES"]."</td>";
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
						foreach($ArrayCabe as $varCombina) {
							$varNumCob = count($varCombina["det"]);
							$Tabla.= "<td width=50  align=\"center\" class=\"FormTitulo\" colspan=\"$varNumCob\">".$varCombina["cab"]."</td>";
							$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
							}	
						$Tabla.= "</tr>";
						$Tabla.= "<tr>";
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
						foreach($ArrayCabe as $varCombina) {
							foreach($varCombina["det"] as $varIndex=>$Data) {
								$Tabla.= "<td width=50 align=\"center\" class=\"FormTitulo\">";
								if(isset($varCombina["toltip"][$varIndex]) && $varCombina["toltip"][$varIndex]<>"") {		
									$Tabla.= "<a href=\"#\" onMouseOver=\"MensajeTip('".$varCombina["toltip"][$varIndex]."')\" onMouseOut=\"UnTip()\"/>";
									$Tabla.= $Data;
									$Tabla.= "</a>";
									}
								else {
									$Tabla.= $Data;
									}
								$Tabla.= "</td>";
								}
							$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
							}
						$Tabla.= "</tr>";
						}
					}
				}
			
			// NIVEL 3: AGRUPACION NUEVA AGREGADA EL 17/05/2011
			// ================================================
			if($varNumNiv >= 4) {
				$varPosCol = 8;
				$varColData = $i+$varNumNiv*2-1;
				if($varNivel_3_Cod_act=="" && $varNivel_3_Cod_ant=="") {
					$varNivel_3_Cod_act = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Cod_ant = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Nom = $fila[$ArrayNivel[3]["DES"]];
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_3[$i] = $varSumNiv_3[$i] + 
											   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
											   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_3[$i] = $varSumNiv_3[$i] + $fila[$i+$varNumNiv*2-1];
						}		
					}
				else{
					$varNivel_3_Cod_act = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Cod_ant = $varNivel_3_Cod_ant;
					if($varNivel_3_Cod_act == $varNivel_3_Cod_ant) {
						for($i=0; $i<=$varNumCol; $i++) {
							if($varDat[$i+$varNumNiv*2-1]<>0) {
								$varFactor = $varDat[$i+$varNumNiv*2-1];
								$varSumNiv_3[$i] = $varSumNiv_3[$i] + 
												   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
												   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
								}
							else
								$varSumNiv_3[$i] = $varSumNiv_3[$i] + $fila[$i+$varNumNiv*2-1];							
							}	
						}
					}
				
				if($varNivel_3_Cod_act <> $varNivel_3_Cod_ant) {
					$Tabla.= "<tr>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\" height=20><b>$varNivel_3_Cod_ant</b></td>";
					$Tabla.= "<td class=\"FormTituloSub\" NOWRAP>$varNivel_3_Nom</td>";
					$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
					$varPos = 1;			
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $Data) {
							$Tabla.= "<td align=\"right\" class=\"FormTituloSub\">";
							if($varSumNiv_3[$varPos]>0) {
								if($varDat[$varPos+5]<>0) {
									$varFactor = $varDat[$varPos+5];
									$varSuma = floor($varSumNiv_3[$varPos]/$varFactor) + round($varSumNiv_3[$varPos])%round($varFactor)/100;								
									}
								else
									$varSuma = $varSumNiv_3[$varPos];
								
								if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
									$varPos1 = $varSumNiv_3[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
									$varPos2 = $varSumNiv_3[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
									$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
									}
								$Tabla.= number_format($varSuma, 2,'.',',');
								if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="%")  $Tabla.="%";
								
								}
							$Tabla.="</td>";
							$varPos = $varPos + 1;
							}
						$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
						}			
					$Tabla.= "</tr>";
					
					$varNivel_3_Cod_ant = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Nom     = $fila[$ArrayNivel[3]["DES"]];
					for($i=1; $i<=$varNumCol;$i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_3[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_3[$i] = $fila[$i+$varNumNiv*2-1];	
						}
					$Tabla.= "<tr>";
					$Tabla.= "<td height=25></td>";
					$Tabla.= "</tr>";
					
					$Tabla.= "<tr>";
					$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\" height=20 ></td>";
					$Tabla.= "<td width=20  align=\"center\" class=\"FormTitulo\" rowspan=\"2\">".$ArrayNivel[0]["COD"]."</td>";
					$Tabla.= "<td width=350 align=\"center\" class=\"FormTitulo\" rowspan=\"2\">".$ArrayNivel[0]["DES"]."</td>";
					$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
					foreach($ArrayCabe as $varCombina) {
						$varNumCob = count($varCombina["det"]);
						$Tabla.= "<td width=50  align=\"center\" class=\"FormTitulo\" colspan=\"$varNumCob\">".$varCombina["cab"]."</td>";
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
						}	
					$Tabla.= "</tr>";
					$Tabla.= "<tr>";
					$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
					$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $varIndex=>$Data) {
							$Tabla.= "<td width=50 align=\"center\" class=\"FormTitulo\">";
							if(isset($varCombina["toltip"][$varIndex]) && $varCombina["toltip"][$varIndex]<>"") {		
								$Tabla.= "<a href=\"#\" onMouseOver=\"MensajeTip('".$varCombina["toltip"][$varIndex]."')\" onMouseOut=\"UnTip()\"/>";
								$Tabla.= $Data;
								$Tabla.= "</a>";
								}
							else {
								$Tabla.= $Data;
								}
							$Tabla.= "</td>";
							}
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
						}
					$Tabla.= "</tr>";
					
					}
				}
				
			//Data del Contenido
			//=============================================
			$k++;
			$Tabla.= ($k%2==0)?"<tr class=\"par\">":"<tr class=\"impar\">";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\">";
			if(isset($ArrayBoton) && count($ArrayBoton)>0) {
				foreach($ArrayBoton as $varBoton) {
					if(count($varBoton)>0)
						//--------------------------------------------------------------------------------------------------------------//
						//Opcion CLICK																									//
						//--------------------------------------------------------------------------------------------------------------//
						$icon = 0;
						$varClick="";
						if(count($varBoton["click"])>1) {
							foreach($varBoton["click"] as $valor=>$ArrayClick) {
								if($valor=="funcion") {
									$varClick.= $ArrayClick."(";
									}
								elseif($valor=="param") {
									foreach($ArrayClick as $varCampo) {
										if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM")
											$varClick.= ($icon==0)?$varCampo["valor"]:",".$varCampo["valor"];
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
											$varClick.= ($icon==0)?"'".$fila[$varCampo["valor"]]."'": ",'".$fila[$varCampo["valor"]]."'";
										else
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
															
										$icon++;
										}									
									}
								
								}
							$varClick.= ")";
							}
						else {
							$varClick = $varBoton["click"];
							}
						
						//--------------------------------------------------------------------------------------------------------------//
						//Opcion TOLTIP																									//
						//--------------------------------------------------------------------------------------------------------------//
						$varTolTip = "";
						if(count($varBoton["toltip"])>1) {
							foreach($varBoton["toltip"] as $varCampo) {
								if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
									//$varTolTip.= str_replace("'","\'",$fila[$varCampo["valor"]]) ;
									$varTolTip.= htmlentities($fila[$varCampo["valor"]]);							
								else
									$varTolTip.= htmlentities($varCampo["valor"]);							
								}
							
							//if($MyValor=="") $MyValor = "'".str_replace("'","\'",$row[$Contenido["Valor"]])."'";
							//else $MyValor = $MyValor.",'".mysql_real_escape_string(str_replace("\"","'",$row[$Contenido["Valor"]]))."'";
											
							}
						else {
							$varTolTip = $varBoton["toltip"];
							}
							
						$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
											  "id"		=>$varBoton["id"],
											  "name"	=>$varBoton["name"], 										  
											  "locked"	=>$varBoton["locked"],
											  "clase"	=>$varBoton["clase"], 
											  "img"		=>$varBoton["img"], 
											  "click"	=>$varClick, 
											  "toltip"	=>$varTolTip, 
											  "dclick"	=>$varBoton["dclick"],
											  "keyup"	=>$varBoton["keyup"],
											  "keypress"=>$varBoton["keypress"], 
											  "valor"	=>$varBoton["valor"], 											  
											  "ancho"	=>$varBoton["ancho"],
											  "alinear"	=>$varBoton["alinear"]);
						$Tabla.= $this->setCampo($varCampoBoton)."&nbsp;";
					}
				}
			$Tabla.= "</td>";
			$Tabla.= "<td align=\"center\" class=\"interior\" height=18><b>".$fila[$ArrayNivel[0]["COD"]]."</b></td>";
			$Tabla.= "<td class=\"interior\" NOWRAP>".$fila[$ArrayNivel[0]["DES"]]."</td>";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
			
//			$varPos = $varPosCol;
			$varPos = $varNumNiv*2;
			$varTexto = "";
			// RECORREMOS EL CONTENIDO DEL GRID
			// ================================
			foreach($ArrayCabe as $varCombina) {
				$varFlagContenido = "";
				foreach($varCombina["det"] as $Data) {
					//Formato de Texto Condicional
					$varClase = "interior";
					if(isset($ArrayFormat[$varPos]["formato"]) && count($ArrayFormat[$varPos]["formato"])>0) {
						foreach($ArrayFormat[$varPos]["formato"] as $varCond) {
							$varClase1 = $this->setFormatCondicion($fila[$varPos], $varCond["cond"], $varCond["formato"], 
																   $varCond["valor1"], $varCond["valor2"]);
							if(isset($varClase1)) $varClase = $varClase1;
							}
						}
					//Formato Estilo de Texto
					$varClase = (isset($ArrayFormat[$varPos]["estilo"]))?$ArrayFormat[$varPos]["estilo"]:$varClase;						
					//Formato Tipo de Dato
					$varTipoDato = (isset($ArrayFormat[$varPos]["tipodat"]))? strtoupper($ArrayFormat[$varPos]["tipodat"]):$this->Fieldtype($ArrayDato, $varPos);
					$varNumDec   = (isset($ArrayFormat[$varPos]["decimal"]))? $ArrayFormat[$varPos]["decimal"]:NULL;
					$varTexto	 = $this->setFormatTipoDato($fila[$varPos], $varTipoDato, $varNumDec);
					//Formato Hipervinculo
					if(isset($ArrayFormat[$varPos]["hiper"]) && count($ArrayFormat[$varPos]["hiper"])>0) {
						$varBoton = $ArrayFormat[$varPos]["hiper"];
						//-------------------------------//
						//Opcion CLICK		 			 //
						//-------------------------------//
						$icon = 0;
						$varClick="";
						if(count($varBoton["click"])>1) {
							foreach($varBoton["click"] as $valor=>$ArrayClick) {
								if($valor=="funcion") {
									$varClick.= $ArrayClick."(";
									}
								elseif($valor=="param") {
									foreach($ArrayClick as $varCampo) {
										if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM")
											$varClick.= ($icon==0)?$varCampo["valor"]:",".$varCampo["valor"];
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
											$varClick.= ($icon==0)?"'".$fila[$varCampo["valor"]]."'": ",'".$fila[$varCampo["valor"]]."'";
										else
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
															
										$icon++;
										}									
									}
								
								}
							$varClick.= ")";
							}
						else {
							$varClick = $varBoton["click"];
							}
						
						//--------------------------------------------------------------------------------------------------------------//
						//Opcion TOLTIP																									//
						//--------------------------------------------------------------------------------------------------------------//
						$varTolTip = "";
						if(count($varBoton["toltip"])>1) {
							foreach($varBoton["toltip"] as $varCampo) {
								if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
									$varTolTip.= htmlentities($fila[$varCampo["valor"]]);							
								else
									$varTolTip.= htmlentities($varCampo["valor"]);							
								}			
							}
						else {
							$varTolTip = $varBoton["toltip"];
							}
						$varTexto = $this->setFormatHipervinculo($varTexto, $varClick, $varTolTip);
						}					
					$Tabla.= "<td align=\"right\" class=\"$varClase\">";
					$Tabla.= $varTexto;
					$varFlagContenido.= $varTexto;
					$Tabla.="</td>";
					$varPos = $varPos + 1;
					}
				//+-==>>INICIO: BOTONES DENTRO DE UN GRUPO DE DATOS
				if(count($varCombina["bot"])>0) {					
					$Tabla.= "<td align=\"center\" class=\"$varClase\">";					
					foreach($varCombina["bot"] as $varBoton) {
						if(count($varBoton)>0)
							//Opcion CLICK																									//
							$icon = 0;
							$varClick="";
							if(count($varBoton["click"])>1) {
								foreach($varBoton["click"] as $valor=>$ArrayClick) {
									if($valor=="funcion") {
										$varClick.= $ArrayClick."(";
										}
									elseif($valor=="param") {
										foreach($ArrayClick as $varCampo) {
											if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM")
												$varClick.= ($icon==0)?$varCampo["valor"]:",".$varCampo["valor"];
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
												$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
												$varClick.= ($icon==0)?"'".$fila[$varCampo["valor"]]."'": ",'".$fila[$varCampo["valor"]]."'";
											else
												$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
																
											$icon++;
											}									
										}
									
									}
								$varClick.= ")";
								}
							else {
								$varClick = $varBoton["click"];
								}
							//Opcion TOLTIP
							$varTolTip = "";
							if(count($varBoton["toltip"])>1) {
								foreach($varBoton["toltip"] as $varCampo) {
									if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
										$varTolTip.= htmlentities($fila[$varCampo["valor"]]);							
									else
										$varTolTip.= htmlentities($varCampo["valor"]);							
									}								
								}
							else {
								$varTolTip = $varBoton["toltip"];
								}
								
							$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
												  "id"		=>$varBoton["id"],
												  "name"	=>$varBoton["name"], 										  
												  "locked"	=>$varBoton["locked"],
												  "clase"	=>$varBoton["clase"], 
												  "img"		=>$varBoton["img"], 
												  "click"	=>$varClick, 
												  "toltip"	=>$varTolTip, 
												  "dclick"	=>$varBoton["dclick"],
												  "keyup"	=>$varBoton["keyup"],
												  "keypress"=>$varBoton["keypress"], 
												  "valor"	=>$varBoton["valor"], 											  
												  "ancho"	=>$varBoton["ancho"],
												  "alinear"	=>$varBoton["alinear"]);
						if($varFlagContenido<> "")
							$Tabla.= $this->setCampo($varCampoBoton)."&nbsp;";
						}					
					$Tabla.= "</td>";
					}
				//<<==FIN: BOTONES DENTRO DE UN GRUPO DE DATOS
				$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
				}				
			$Tabla.= "</tr>";
			}
		//Ultimo Nivel 1
		if($varNumNiv >= 2) {
			$Tabla.= "<tr>";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\">&nbsp;</td>";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\" height=20><b>$varNivel_1_Cod_ant</b></td>";
			$Tabla.= "<td class=\"FormTituloSub\">$varNivel_1_Nom</td>";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\">&nbsp;</td>";	
			$varPos = 1;
			$varPosSum  = $varNumNiv*2-1;
			foreach($ArrayCabe as $varCombina) {
				foreach($varCombina["det"] as $Data) {
					$Tabla.= "<td align=\"right\" class=\"FormTituloSub\">";
					if($varSumNiv_1[$varPos]>0) {
						if($varDat[$varPos+$varPosSum]<>0) {
							$varFactor = $varDat[$varPos+$varPosSum];
							$varSuma = floor($varSumNiv_1[$varPos]/$varFactor) + round($varSumNiv_1[$varPos])%round($varFactor)/100;								
							}
						else
							$varSuma = $varSumNiv_1[$varPos];
						
						if(isset($ArrayFormat[$varPos+$varPosSum]["formula"]) && count($ArrayFormat[$varPos+$varPosSum]["formula"])>0) {
							
							$varPos1 = $varSumNiv_1[$ArrayFormat[$varPos+$varPosSum]["formula"]["valor1"]-$varPosSum];
							$varPos2 = $varSumNiv_1[$ArrayFormat[$varPos+$varPosSum]["formula"]["valor2"]-$varPosSum];
							$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+$varPosSum]["formula"]["funcion"]);
							}
						$Tabla.= number_format($varSuma, 2,'.',',');
						if(isset($ArrayFormat[$varPos+$varPosSum]["tipodat"]) && $ArrayFormat[$varPos+$varPosSum]["tipodat"]=="porc")  $Tabla.="%";
						}
					$Tabla.= "</td>";
					$varPos = $varPos + 1;
					}				
				$Tabla.= "<td align=\"center\" class=\"FormTituloSub\">&nbsp;</td>";
				
				}			
			$Tabla.= "</tr>";
			
			$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
			$varNivel_1_Nom     = $fila[$ArrayNivel[1]["DES"]];
			for($i=1; $i<=$varNumCol;$i++) {
				if($varDat[$i+$varNumNiv*2-1]<>0) {
					$varFactor = $varDat[$i+$varNumNiv*2-1];
					$varSumNiv_1[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
					}
				else
					$varSumNiv_1[$i] = $fila[$i+$varNumNiv*2-1];
				}
			}
		if($varNumNiv >= 3) {
			$Tabla.= "<tr>";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\" height=20><b>$varNivel_2_Cod_ant</b></td>";
			$Tabla.= "<td class=\"FormTituloSub\">$varNivel_2_Nom</td>";
			$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";	
			$varPos = 1;			
			foreach($ArrayCabe as $varCombina) {
				foreach($varCombina["det"] as $Data) {
					$Tabla.= "<td align=\"right\" class=\"FormTituloSub\">";
					if($varSumNiv_2[$varPos]>0) {
						if($varDat[$varPos+5]<>0) {
							$varFactor = $varDat[$varPos+5];
							$varSuma = floor(round($varSumNiv_2[$varPos])/$varFactor) + (round($varSumNiv_2[$varPos],0))%$varFactor/100;								
							}
						else
							$varSuma = $varSumNiv_2[$varPos];
						if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
							$varPos1 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
							$varPos2 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
							$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
							}
						$Tabla.= number_format($varSuma, 2,'.',',');
						if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $Tabla.="%";
						}
					$Tabla.="</td>";
					$varPos = $varPos + 1;
					}
				$Tabla.= "<td align=\"center\" class=\"FormTituloSub\"></td>";
				}			
			$Tabla.= "</tr>";
			
			$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
			$varNivel_2_Nom     = $fila[$ArrayNivel[2]["DES"]];
			for($i=1; $i<=$varNumCol;$i++) {
				if($varDat[$i+$varNumNiv*2-1]<>0) {
					$varFactor = $varDat[$i+$varNumNiv*2-1];
					$varSumNiv_2[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
					}
				else
					$varSumNiv_2[$i] = $fila[$i+$varNumNiv*2-1];
				}
			}
		$Tabla.= "</table>";
		}
	else
		{
		$Tabla = $this->ErrorConsulta();
		}
	return $Tabla;
	}
	
	function setGridCombinaPrint($ArrayCabe   = NULL, //Array con las Cabeceras Combinadas
								 $SqlDato	  = NULL, //Query que contiene los Datos para la Tabla
								 $ArrayNivel  = NULL, //Array con los Niveles de Agrupamiento de la Data								
								 $ArrayFormat = NULL  //Array con Formato Condicional
								 )
	{	
	ini_set("memory_limit","64M");
	if(isset($ArrayCabe) && isset($SqlDato) && isset($ArrayNivel)) {		
		$varNumNiv = count($ArrayNivel);
		$varNumCol = 0;
		$varNumComb= count($ArrayCabe);
		foreach($ArrayCabe as $varCombina) {
			$varNumCol = $varNumCol + count($varCombina["det"]);
			}
		$varTamTab = $varNumCol*60+150;
		$this->AbrirConexion();
		$ArrayDato = $this->Query($SqlDato);
		$this->CerrarConexion();
		$Tabla.= "<table style=\"width:100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;\" align=\"center\">";
		$Tabla.= "<thead>";
		$Tabla.= "<tr class=\"head\">";
		$Tabla.= "<td width=5   align=\"center\" height=20 ></td>";
		$Tabla.= "<td width=20  align=\"center\" rowspan=\"2\">".$ArrayNivel[0]["COD"]."</td>";
		$Tabla.= "<td width=150 align=\"center\" rowspan=\"2\" NOWRAP>".$ArrayNivel[0]["DES"]."</td>";
		$Tabla.= "<td width=5   align=\"center\" ></td>";
		foreach($ArrayCabe as $varCombina) {
			$varNumCob = count($varCombina["det"]);
			if(count($varCombina["bot"])>0)
				$varNumCob = $varNumCob + 1;
			$Tabla.= "<td width=50  align=\"center\" colspan=\"$varNumCob\">".$varCombina["cab"]."</td>";
			$Tabla.= "<td width=5   align=\"center\"></td>";
			}	
		$Tabla.= "</tr>";
		$Tabla.= "<tr class=\"head\">";
		$Tabla.= "<td width=5   align=\"center\"></td>";
		$Tabla.= "<td width=5   align=\"center\"></td>";
		$varPos = $varNumNiv*2;

		$varDat[0] = $varDat[1] = $varDat[2] = $varDat[3] = $varDat[4] = $varDat[5] = $varDat[6] = $varDat[7] = 0;
		foreach($ArrayCabe as $varCombina) {
			foreach($varCombina["det"] as $varIndex=>$Data) {
				if(isset($varCombina["factor"])) {
					$varDat[$varPos] = $varCombina["factor"];
					}
				else {
					$varDat[$varPos] = 0;
					}
				
				$Tabla.= "<td width=50 align=\"center\" class=\"FormTitulo\">";
				if(isset($varCombina["toltip"][$varIndex]) && $varCombina["toltip"][$varIndex]<>"") {		
					$Tabla.= "<a href=\"#\" onMouseOver=\"MensajeTip('".$varCombina["toltip"][$varIndex]."')\" onMouseOut=\"UnTip()\"/>";
					$Tabla.= $Data;
					$Tabla.= "</a>";
					}
				else {
					$Tabla.= $Data;
					}
				$varPos = $varPos + 1;
				$Tabla.= "</td>";
				
				}
			if(count($varCombina["bot"])>0)
				$Tabla.= "<td  align=\"center\" class=\"FormTitulo\">OPT</td>";
			$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
			}
		$Tabla.= "</tr>";		
		$Tabla.= "</thead>";		
		
		$varNivel_3_Cod_ant = "";
		$varNivel_3_Cod_act = "";
		$varNivel_3_Nom = "";
		
		$varNivel_2_Cod_ant = "";
		$varNivel_2_Cod_act = "";
		$varNivel_2_Nom = "";
		
		$varNivel_1_Cod_ant = "";
		$varNivel_1_Cod_act = "";
		$varNivel_1_Nom = "";
		
		for($i=0; $i<=$varNumCol; $i++) {
			$varSumNiv_1[$i] = 0;
			$varSumNiv_2[$i] = 0;
			$varSumNiv_3[$i] = 0;
			$varSumTotal[$i] = 0;
			}
			
		$k=0;
		$varPosCol = 2;

		while($fila = $this->FetchArray($ArrayDato)) {			
			//Nivel 1 Por lo General Grupo de Supervisores
			//=============================================
			if($varNumNiv >= 2) {
				$varPosCol = 4;
				if($varNivel_1_Cod_act=="" && $varNivel_1_Cod_ant=="") {
					$varNivel_1_Cod_act = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Nom = $fila[$ArrayNivel[1]["DES"]];
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + 
											   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
											   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + $fila[$i+$varNumNiv*2-1];
						}		
					}
				else{
					$varNivel_1_Cod_act = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Cod_ant = $varNivel_1_Cod_ant;
					if($varNivel_1_Cod_act == $varNivel_1_Cod_ant) {
						for($i=0; $i<=$varNumCol; $i++) {
							if($varDat[$i+$varNumNiv*2-1]<>0) {
								$varFactor = $varDat[$i+$varNumNiv*2-1];
								$varSumNiv_1[$i] = $varSumNiv_1[$i] + 
												   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
												   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
								}
							else
								$varSumNiv_1[$i] = $varSumNiv_1[$i] + $fila[$i+$varNumNiv*2-1];							
							}	
						}
					}
				
				if($varNivel_1_Cod_act <> $varNivel_1_Cod_ant) {
					$Tabla.= "<tr class=\"head\">";
					$Tabla.= "<td align=\"center\"></td>";
					$Tabla.= "<td align=\"center\" height=20><b>$varNivel_1_Cod_ant</b></td>";
					$Tabla.= "<td NOWRAP>$varNivel_1_Nom</td>";
					$Tabla.= "<td align=\"center\"></td>";	
					
					$varPos = 1;			
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $Data) {
							$Tabla.= "<td align=\"right\">";
							if($varSumNiv_1[$varPos]>0) {
								if($varDat[$varPos+5]<>0) {
									$varFactor = $varDat[$varPos+5];
									$varSuma = floor($varSumNiv_1[$varPos]/$varFactor) + round($varSumNiv_1[$varPos])%round($varFactor)/100;								
									}
								else
									$varSuma = $varSumNiv_1[$varPos];
								
								if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
									$varPos1 = $varSumNiv_1[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
									$varPos2 = $varSumNiv_1[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
									$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
									}
								$Tabla.= number_format($varSuma, 2,'.',',');
								if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $Tabla.="%";
								}							
							$Tabla.="</td>";
							$varPos = $varPos + 1;
							}
						$Tabla.= "<td align=\"center\"></td>";
						}		
					$Tabla.= "</tr>";
					
					$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
					$varNivel_1_Nom     = $fila[$ArrayNivel[1]["DES"]];
					for($i=1; $i<=$varNumCol;$i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_1[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_1[$i] = $fila[$i+$varNumNiv*2-1];
						}
					}
				}
			
			//Nivel 2: Por lo General Grupo de CD'S
			//=============================================
			if($varNumNiv >= 3) {
				$varPosCol = 6;
				$varColData = $i+$varNumNiv*2-1;
				if($varNivel_2_Cod_act=="" && $varNivel_2_Cod_ant=="") {
					$varNivel_2_Cod_act = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Nom = $fila[$ArrayNivel[2]["DES"]];
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_2[$i] = $varSumNiv_2[$i] + floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
											   ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_2[$i] = $varSumNiv_2[$i] + $fila[$i+$varNumNiv*2-1];
						}		
					}
				else{

					$varNivel_2_Cod_act = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Cod_ant = $varNivel_2_Cod_ant;
					if($varNivel_2_Cod_act == $varNivel_2_Cod_ant) {
						for($i=0; $i<=$varNumCol; $i++) {
							if($varDat[$i+$varNumNiv*2-1]<>0) {
								$varFactor = $varDat[$i+$varNumNiv*2-1];
								$varSumNiv_2[$i] = $varSumNiv_2[$i] + 
												   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
												   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
								}
							else
								$varSumNiv_2[$i] = $varSumNiv_2[$i] + $fila[$i+$varNumNiv*2-1];							
							}	
						}
					}
				
				if($varNivel_2_Cod_act <> $varNivel_2_Cod_ant) {
					$Tabla.= "<tr class=\"head\">";
					$Tabla.= "<td align=\"center\"></td>";
					$Tabla.= "<td align=\"center\" height=20><b>$varNivel_2_Cod_ant</b></td>";
					$Tabla.= "<td  NOWRAP>$varNivel_2_Nom</td>";
					$Tabla.= "<td align=\"center\"></td>";
					$varPos = 1;			
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $Data) {
							$Tabla.= "<td align=\"right\">";
							if($varSumNiv_2[$varPos]>0) {
								if($varDat[$varPos+5]<>0) {
									$varFactor = $varDat[$varPos+5];
									$varSuma = floor($varSumNiv_2[$varPos]/$varFactor) + round($varSumNiv_2[$varPos])%round($varFactor)/100;								
									}
								else
									$varSuma = $varSumNiv_2[$varPos];
								
								if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
									$varPos1 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
									$varPos2 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
									$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
									}
								$Tabla.= number_format($varSuma, 2,'.',',');
								if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $Tabla.="%";
								
								}
							$Tabla.="</td>";
							$varPos = $varPos + 1;
							}
						$Tabla.= "<td align=\"center\"></td>";
						}			
					$Tabla.= "</tr>";
					
					$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
					$varNivel_2_Nom     = $fila[$ArrayNivel[2]["DES"]];
					for($i=1; $i<=$varNumCol;$i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_2[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_2[$i] = $fila[$i+$varNumNiv*2-1];	
						}
					
					if($varNumNiv == 3) {
						$Tabla.= "<tr class=\"head\">";
						$Tabla.= "<td height=25></td>";
						$Tabla.= "</tr>";
						
						$Tabla.= "<tr class=\"head\">";
						$Tabla.= "<td width=5   align=\"center\" height=20 ></td>";
						$Tabla.= "<td width=20  align=\"center\" rowspan=\"2\">".$ArrayNivel[0]["COD"]."</td>";
						$Tabla.= "<td width=350 align=\"center\" rowspan=\"2\">".$ArrayNivel[0]["DES"]."</td>";
						$Tabla.= "<td width=5   align=\"center\"></td>";
						foreach($ArrayCabe as $varCombina) {
							$varNumCob = count($varCombina["det"]);
							$Tabla.= "<td width=50  align=\"center\" colspan=\"$varNumCob\">".$varCombina["cab"]."</td>";
							$Tabla.= "<td width=5   align=\"center\"></td>";
							}	
						$Tabla.= "</tr>";
						$Tabla.= "<tr>";
						$Tabla.= "<td width=5   align=\"center\"></td>";
						$Tabla.= "<td width=5   align=\"center\"></td>";
						foreach($ArrayCabe as $varCombina) {
							foreach($varCombina["det"] as $varIndex=>$Data) {
								$Tabla.= "<td width=50 align=\"center\">";
								if(isset($varCombina["toltip"][$varIndex]) && $varCombina["toltip"][$varIndex]<>"") {		
									$Tabla.= "<a href=\"#\" onMouseOver=\"MensajeTip('".$varCombina["toltip"][$varIndex]."')\" onMouseOut=\"UnTip()\"/>";
									$Tabla.= $Data;
									$Tabla.= "</a>";
									}
								else {
									$Tabla.= $Data;
									}
								$Tabla.= "</td>";
								}
							$Tabla.= "<td width=5   align=\"center\"></td>";
							}
						$Tabla.= "</tr>";
						}
					}
				}
			
			// NIVEL 3: AGRUPACION NUEVA AGREGADA EL 17/05/2011
			// ================================================
			if($varNumNiv >= 4) {
				$varPosCol = 8;
				$varColData = $i+$varNumNiv*2-1;
				if($varNivel_3_Cod_act=="" && $varNivel_3_Cod_ant=="") {
					$varNivel_3_Cod_act = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Cod_ant = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Nom = $fila[$ArrayNivel[3]["DES"]];
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_3[$i] = $varSumNiv_3[$i] + 
											   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
											   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_3[$i] = $varSumNiv_3[$i] + $fila[$i+$varNumNiv*2-1];
						}		
					}
				else{
					$varNivel_3_Cod_act = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Cod_ant = $varNivel_3_Cod_ant;
					if($varNivel_3_Cod_act == $varNivel_3_Cod_ant) {
						for($i=0; $i<=$varNumCol; $i++) {
							if($varDat[$i+$varNumNiv*2-1]<>0) {
								$varFactor = $varDat[$i+$varNumNiv*2-1];
								$varSumNiv_3[$i] = $varSumNiv_3[$i] + 
												   floor($fila[$i+$varNumNiv*2-1])*$varFactor + 
												   ($fila[$i+$varNumNiv*2-1] - floor($fila[$i+$varNumNiv*2-1]))*100;
								}
							else
								$varSumNiv_3[$i] = $varSumNiv_3[$i] + $fila[$i+$varNumNiv*2-1];							
							}	
						}
					}
				
				if($varNivel_3_Cod_act <> $varNivel_3_Cod_ant) {
					$Tabla.= "<tr class=\"head\">";
					$Tabla.= "<td align=\"center\"></td>";
					$Tabla.= "<td align=\"center\" height=20><b>$varNivel_3_Cod_ant</b></td>";
					$Tabla.= "<td NOWRAP>$varNivel_3_Nom</td>";
					$Tabla.= "<td align=\"center\"></td>";
					$varPos = 1;			
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $Data) {
							$Tabla.= "<td align=\"right\">";
							if($varSumNiv_3[$varPos]>0) {
								if($varDat[$varPos+5]<>0) {
									$varFactor = $varDat[$varPos+5];
									$varSuma = floor($varSumNiv_3[$varPos]/$varFactor) + round($varSumNiv_3[$varPos])%round($varFactor)/100;								
									}
								else
									$varSuma = $varSumNiv_3[$varPos];
								
								if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
									$varPos1 = $varSumNiv_3[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
									$varPos2 = $varSumNiv_3[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
									$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
									}
								$Tabla.= number_format($varSuma, 2,'.',',');
								if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="%")  $Tabla.="%";
								
								}
							$Tabla.="</td>";
							$varPos = $varPos + 1;
							}
						$Tabla.= "<td align=\"center\"></td>";
						}			
					$Tabla.= "</tr>";
					
					$varNivel_3_Cod_ant = $fila[$ArrayNivel[3]["COD"]];
					$varNivel_3_Nom     = $fila[$ArrayNivel[3]["DES"]];
					for($i=1; $i<=$varNumCol;$i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_3[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_3[$i] = $fila[$i+$varNumNiv*2-1];	
						}
					$Tabla.= "<tr class=\"head\">";
					$Tabla.= "<td height=25></td>";
					$Tabla.= "</tr>";
					
					$Tabla.= "<tr class=\"head\">";
					$Tabla.= "<td width=5   align=\"center\" height=20 ></td>";
					$Tabla.= "<td width=20  align=\"center\" rowspan=\"2\">".$ArrayNivel[0]["COD"]."</td>";
					$Tabla.= "<td width=350 align=\"center\" rowspan=\"2\">".$ArrayNivel[0]["DES"]."</td>";
					$Tabla.= "<td width=5   align=\"center\"></td>";
					foreach($ArrayCabe as $varCombina) {
						$varNumCob = count($varCombina["det"]);
						$Tabla.= "<td width=50  align=\"center\" class=\"FormTitulo\" colspan=\"$varNumCob\">".$varCombina["cab"]."</td>";
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
						}	
					$Tabla.= "</tr>";
					$Tabla.= "<tr class=\"head\">";
					$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
					$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
					foreach($ArrayCabe as $varCombina) {
						foreach($varCombina["det"] as $varIndex=>$Data) {
							$Tabla.= "<td width=50 align=\"center\" class=\"FormTitulo\">";
							if(isset($varCombina["toltip"][$varIndex]) && $varCombina["toltip"][$varIndex]<>"") {		
								$Tabla.= "<a href=\"#\" onMouseOver=\"MensajeTip('".$varCombina["toltip"][$varIndex]."')\" onMouseOut=\"UnTip()\"/>";
								$Tabla.= $Data;
								$Tabla.= "</a>";
								}
							else {
								$Tabla.= $Data;
								}
							$Tabla.= "</td>";
							}
						$Tabla.= "<td width=5   align=\"center\" class=\"FormTitulo\"></td>";
						}
					$Tabla.= "</tr>";
					
					}
				}
				
			//Data del Contenido
			//=============================================
			$k++;
			$Tabla.= "<tr  class=\"head_data\">";
			$Tabla.= "<td align=\"center\" class=\"head_col\">";
			if(isset($ArrayBoton) && count($ArrayBoton)>0) {
				foreach($ArrayBoton as $varBoton) {
					if(count($varBoton)>0)
						//--------------------------------------------------------------------------------------------------------------//
						//Opcion CLICK																									//
						//--------------------------------------------------------------------------------------------------------------//
						$icon = 0;
						$varClick="";
						if(count($varBoton["click"])>1) {
							foreach($varBoton["click"] as $valor=>$ArrayClick) {
								if($valor=="funcion") {
									$varClick.= $ArrayClick."(";
									}
								elseif($valor=="param") {
									foreach($ArrayClick as $varCampo) {
										if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM")
											$varClick.= ($icon==0)?$varCampo["valor"]:",".$varCampo["valor"];
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
											$varClick.= ($icon==0)?"'".$fila[$varCampo["valor"]]."'": ",'".$fila[$varCampo["valor"]]."'";
										else
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
															
										$icon++;
										}									
									}
								
								}
							$varClick.= ")";
							}
						else {
							$varClick = $varBoton["click"];
							}
						
						//--------------------------------------------------------------------------------------------------------------//
						//Opcion TOLTIP																									//
						//--------------------------------------------------------------------------------------------------------------//
						$varTolTip = "";
						if(count($varBoton["toltip"])>1) {
							foreach($varBoton["toltip"] as $varCampo) {
								if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
									//$varTolTip.= str_replace("'","\'",$fila[$varCampo["valor"]]) ;
									$varTolTip.= htmlentities($fila[$varCampo["valor"]]);							
								else
									$varTolTip.= htmlentities($varCampo["valor"]);							
								}
							
							//if($MyValor=="") $MyValor = "'".str_replace("'","\'",$row[$Contenido["Valor"]])."'";
							//else $MyValor = $MyValor.",'".mysql_real_escape_string(str_replace("\"","'",$row[$Contenido["Valor"]]))."'";
											
							}
						else {
							$varTolTip = $varBoton["toltip"];
							}
							
						$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
											  "id"		=>$varBoton["id"],
											  "name"	=>$varBoton["name"], 										  
											  "locked"	=>$varBoton["locked"],
											  "clase"	=>$varBoton["clase"], 
											  "img"		=>$varBoton["img"], 
											  "click"	=>$varClick, 
											  "toltip"	=>$varTolTip, 
											  "dclick"	=>$varBoton["dclick"],
											  "keyup"	=>$varBoton["keyup"],
											  "keypress"=>$varBoton["keypress"], 
											  "valor"	=>$varBoton["valor"], 											  
											  "ancho"	=>$varBoton["ancho"],
											  "alinear"	=>$varBoton["alinear"]);
						$Tabla.= $this->setCampo($varCampoBoton)."&nbsp;";
					}
				}
			$Tabla.= "</td>";
			$Tabla.= "<td align=\"center\" height=18><b>".$fila[$ArrayNivel[0]["COD"]]."</b></td>";
			$Tabla.= "<td class=\"interior\" NOWRAP>".$fila[$ArrayNivel[0]["DES"]]."</td>";
			$Tabla.= "<td align=\"center\" class=\"head_col\"></td>";
			
//			$varPos = $varPosCol;
			$varPos = $varNumNiv*2;
			$varTexto = "";
			// RECORREMOS EL CONTENIDO DEL GRID
			// ================================
			foreach($ArrayCabe as $varCombina) {
				$varFlagContenido = "";
				foreach($varCombina["det"] as $Data) {
					//Formato de Texto Condicional
					$varClase = "interior";
					if(isset($ArrayFormat[$varPos]["formato"]) && count($ArrayFormat[$varPos]["formato"])>0) {
						foreach($ArrayFormat[$varPos]["formato"] as $varCond) {
							$varClase1 = $this->setFormatCondicion($fila[$varPos], $varCond["cond"], $varCond["formato"], 
																   $varCond["valor1"], $varCond["valor2"]);
							if(isset($varClase1)) $varClase = $varClase1;
							}
						}
					//Formato Estilo de Texto
					$varClase = (isset($ArrayFormat[$varPos]["estilo"]))?$ArrayFormat[$varPos]["estilo"]:$varClase;						
					//Formato Tipo de Dato
					$varTipoDato = (isset($ArrayFormat[$varPos]["tipodat"]))? strtoupper($ArrayFormat[$varPos]["tipodat"]):$this->Fieldtype($ArrayDato, $varPos);
					$varNumDec   = (isset($ArrayFormat[$varPos]["decimal"]))? $ArrayFormat[$varPos]["decimal"]:NULL;
					$varTexto	 = $this->setFormatTipoDato($fila[$varPos], $varTipoDato, $varNumDec);
					//Formato Hipervinculo
					if(isset($ArrayFormat[$varPos]["hiper"]) && count($ArrayFormat[$varPos]["hiper"])>0) {
						$varBoton = $ArrayFormat[$varPos]["hiper"];
						//-------------------------------//
						//Opcion CLICK		 			 //
						//-------------------------------//
						$icon = 0;
						$varClick="";
						if(count($varBoton["click"])>1) {
							foreach($varBoton["click"] as $valor=>$ArrayClick) {
								if($valor=="funcion") {
									$varClick.= $ArrayClick."(";
									}
								elseif($valor=="param") {
									foreach($ArrayClick as $varCampo) {
										if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM")
											$varClick.= ($icon==0)?$varCampo["valor"]:",".$varCampo["valor"];
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
											$varClick.= ($icon==0)?"'".$fila[$varCampo["valor"]]."'": ",'".$fila[$varCampo["valor"]]."'";
										else
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
															
										$icon++;
										}									
									}
								
								}
							$varClick.= ")";
							}
						else {
							$varClick = $varBoton["click"];
							}
						
						//--------------------------------------------------------------------------------------------------------------//
						//Opcion TOLTIP																									//
						//--------------------------------------------------------------------------------------------------------------//
						$varTolTip = "";
						if(count($varBoton["toltip"])>1) {
							foreach($varBoton["toltip"] as $varCampo) {
								if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
									$varTolTip.= htmlentities($fila[$varCampo["valor"]]);							
								else
									$varTolTip.= htmlentities($varCampo["valor"]);							
								}			
							}
						else {
							$varTolTip = $varBoton["toltip"];
							}
						$varTexto = $this->setFormatHipervinculo($varTexto, $varClick, $varTolTip);
						}					
					$Tabla.= "<td align=\"right\" class=\"$varClase\">";
					$Tabla.= $varTexto;
					$varFlagContenido.= $varTexto;
					$Tabla.="</td>";
					$varPos = $varPos + 1;
					}
				//+-==>>INICIO: BOTONES DENTRO DE UN GRUPO DE DATOS
				if(count($varCombina["bot"])>0) {					
					$Tabla.= "<td align=\"center\" class=\"$varClase\">";					
					foreach($varCombina["bot"] as $varBoton) {
						if(count($varBoton)>0)
							//Opcion CLICK																									//
							$icon = 0;
							$varClick="";
							if(count($varBoton["click"])>1) {
								foreach($varBoton["click"] as $valor=>$ArrayClick) {
									if($valor=="funcion") {
										$varClick.= $ArrayClick."(";
										}
									elseif($valor=="param") {
										foreach($ArrayClick as $varCampo) {
											if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM")
												$varClick.= ($icon==0)?$varCampo["valor"]:",".$varCampo["valor"];
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
												$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
											elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
												$varClick.= ($icon==0)?"'".$fila[$varCampo["valor"]]."'": ",'".$fila[$varCampo["valor"]]."'";
											else
												$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
																
											$icon++;
											}									
										}
									
									}
								$varClick.= ")";
								}
							else {
								$varClick = $varBoton["click"];
								}
							//Opcion TOLTIP
							$varTolTip = "";
							if(count($varBoton["toltip"])>1) {
								foreach($varBoton["toltip"] as $varCampo) {
									if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
										$varTolTip.= htmlentities($fila[$varCampo["valor"]]);							
									else
										$varTolTip.= htmlentities($varCampo["valor"]);							
									}								
								}
							else {
								$varTolTip = $varBoton["toltip"];
								}
								
							$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
												  "id"		=>$varBoton["id"],
												  "name"	=>$varBoton["name"], 										  
												  "locked"	=>$varBoton["locked"],
												  "clase"	=>$varBoton["clase"], 
												  "img"		=>$varBoton["img"], 
												  "click"	=>$varClick, 
												  "toltip"	=>$varTolTip, 
												  "dclick"	=>$varBoton["dclick"],
												  "keyup"	=>$varBoton["keyup"],
												  "keypress"=>$varBoton["keypress"], 
												  "valor"	=>$varBoton["valor"], 											  
												  "ancho"	=>$varBoton["ancho"],
												  "alinear"	=>$varBoton["alinear"]);
						if($varFlagContenido<> "")
							$Tabla.= $this->setCampo($varCampoBoton)."&nbsp;";
						}					
					$Tabla.= "</td>";
					}
				//<<==FIN: BOTONES DENTRO DE UN GRUPO DE DATOS
				$Tabla.= "<td align=\"center\" class=\"head_col\"></td>";
				}				
			$Tabla.= "</tr>";
			}
		//Ultimo Nivel 1
		if($varNumNiv >= 2) {
			$Tabla.= "<tr class=\"head\">";
			$Tabla.= "<td align=\"center\"></td>";
			$Tabla.= "<td align=\"center\" height=20><b>$varNivel_1_Cod_ant</b></td>";
			$Tabla.= "<td>$varNivel_1_Nom</td>";
			$Tabla.= "<td align=\"center\"></td>";	
			$varPos = 1;
			$varPosSum  = $varNumNiv*2-1;
			foreach($ArrayCabe as $varCombina) {
				foreach($varCombina["det"] as $Data) {
					$Tabla.= "<td align=\"right\" class=\"FormTituloSub\">";
					if($varSumNiv_1[$varPos]>0) {
						if($varDat[$varPos+$varPosSum]<>0) {
							$varFactor = $varDat[$varPos+$varPosSum];
							$varSuma = floor($varSumNiv_1[$varPos]/$varFactor) + round($varSumNiv_1[$varPos])%round($varFactor)/100;								
							}
						else
							$varSuma = $varSumNiv_1[$varPos];
						
						if(isset($ArrayFormat[$varPos+$varPosSum]["formula"]) && count($ArrayFormat[$varPos+$varPosSum]["formula"])>0) {
							
							$varPos1 = $varSumNiv_1[$ArrayFormat[$varPos+$varPosSum]["formula"]["valor1"]-$varPosSum];
							$varPos2 = $varSumNiv_1[$ArrayFormat[$varPos+$varPosSum]["formula"]["valor2"]-$varPosSum];
							$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+$varPosSum]["formula"]["funcion"]);
							}
						$Tabla.= number_format($varSuma, 2,'.',',');
						if(isset($ArrayFormat[$varPos+$varPosSum]["tipodat"]) && $ArrayFormat[$varPos+$varPosSum]["tipodat"]=="porc")  $Tabla.="%";
						}
					$Tabla.= "</td>";
					$varPos = $varPos + 1;
					}				
				$Tabla.= "<td align=\"center\" class=\"FormTituloSub\">&nbsp;</td>";
				
				}			
			$Tabla.= "</tr>";
			
			$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
			$varNivel_1_Nom     = $fila[$ArrayNivel[1]["DES"]];
			for($i=1; $i<=$varNumCol;$i++) {
				if($varDat[$i+$varNumNiv*2-1]<>0) {
					$varFactor = $varDat[$i+$varNumNiv*2-1];
					$varSumNiv_1[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
					}
				else
					$varSumNiv_1[$i] = $fila[$i+$varNumNiv*2-1];
				}
			}
		if($varNumNiv >= 3) {
			$Tabla.= "<tr class=\"head\">";
			$Tabla.= "<td align=\"center\"></td>";
			$Tabla.= "<td align=\"center\" height=20><b>$varNivel_2_Cod_ant</b></td>";
			$Tabla.= "<td>$varNivel_2_Nom</td>";
			$Tabla.= "<td align=\"center\"></td>";	
			$varPos = 1;			
			foreach($ArrayCabe as $varCombina) {
				foreach($varCombina["det"] as $Data) {
					$Tabla.= "<td align=\"right\">";
					if($varSumNiv_2[$varPos]>0) {
						if($varDat[$varPos+5]<>0) {
							$varFactor = $varDat[$varPos+5];
							$varSuma = floor(round($varSumNiv_2[$varPos])/$varFactor) + (round($varSumNiv_2[$varPos],0))%$varFactor/100;								
							}
						else
							$varSuma = $varSumNiv_2[$varPos];
						if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
							$varPos1 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
							$varPos2 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
							$varSuma = $this->setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
							}
						$Tabla.= number_format($varSuma, 2,'.',',');
						if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $Tabla.="%";
						}
					$Tabla.="</td>";
					$varPos = $varPos + 1;
					}
				$Tabla.= "<td align=\"center\"></td>";
				}			
			$Tabla.= "</tr>";
			
			$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
			$varNivel_2_Nom     = $fila[$ArrayNivel[2]["DES"]];
			for($i=1; $i<=$varNumCol;$i++) {
				if($varDat[$i+$varNumNiv*2-1]<>0) {
					$varFactor = $varDat[$i+$varNumNiv*2-1];
					$varSumNiv_2[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
					}
				else
					$varSumNiv_2[$i] = $fila[$i+$varNumNiv*2-1];
				}
			}
		$Tabla.= "</table>";
		}
	else
		{
		$Tabla = $this->ErrorConsulta();
		}
	return $Tabla;
	}
	
	function setFormatCondicion($varReal=NULL, $varCond=NULL, $varFormato=NULL, $varMeta1=NULL, $varMeta2=NULL) {
		switch($varCond) {
			case "entre":
				if($varReal >= $varMeta1 && $varReal <= $varMeta2 )  $varResp = $varFormato;
				break;
			case "=":
				if($varReal == $varMeta1)  $varResp = $varFormato;
				break;
			case ">":
				if($varReal > $varMeta1)  $varResp = $varFormato;
				break;
			case ">=":
				if($varReal >= $varMeta1)  $varResp = $varFormato;
				break;
			case "<":
				if($varReal < $varMeta1)  $varResp = $varFormato;
				break;
			case "<=":
				if($varReal <= $varMeta1)  $varResp = $varFormato;
				break;
			default;
				$varResp = NULL;
				break;
			}
		return $varResp;
		}	
	
	function setFormatFuncion($varValor1=NULL, $varValor2=NULL, $varFuncion=NULL) {
		$varResp = NULL;
		switch($varFuncion) {
			case "porc":
			case "PORC":
			case "%":
				if($varValor2==0) $varResp = "0.00";
				else $varResp =  $varValor2/$varValor1*100;
				break;
			case "crec":
				if($Sum[$valor1]==0) $varResp="0.00";
				else $varResp =  $varValor1/$varValor2*100-100;
				break;
			case "suma":
				$varResp = $varValor1+$varValor1;
				break;
			case "resta":
				$varResp = $varValor1-$varValor1;
				break;
			default:
				$varResp = $varValor1+$varValor1;
				break;
			}
		return $varResp;
		}
	function setFormatHipervinculo($varDato=NULL, $varClick = NULL, $varTolTip=NULL) {
		$varResp ="<a href='javascript:void(0)' onclick = \"$varClick\"  onmouseover=\"MensajeTip('$varTolTip')\" onmouseout=\"UnTip()\">".$varDato."</a>";				
		return $varResp;
		}
		
	function setFormatTipoDato($varDato=NULL, $varTipoDato="TEXTO", $varNumDecimales=NULL) {
		$varResp = NULL;
		if(isset($varDato)) {
			switch(strtoupper($varTipoDato)) {
				case "TEXTO":
				case "TEXT":
				case "STRING":
				case "CHAR":
					$varResp = $varDato;
					break;
				case "NUMERO":
				case "DECIMAL":
				case "DOUBLE":
				case "REAL":
				case "FLOAT":				
					$varResp = (isset($varNumDecimales))?number_format($varDato, $varNumDecimales,'.',','):number_format($varDato, 2,'.',',');
					break;
				case "ENTERO":
				case "INT":
					$varResp = number_format($varDato, 0,'.',',');
					break;
				case "PORC":
				case "PORCENTAJE":
					$varResp = (isset($varNumDecimales))?number_format($varDato, $varNumDecimales,'.',',')."%":number_format($varDato, 2,'.',',')."%";
					break;
				default:
					$varResp = $varDato;
					break;
				}
			}
		return $varResp;
		}	
		
	function SetGrid($ancho, 	  	//Ancho de la Tabla en Pixeles
					 $ArrayCabe, 		//[Tamano],[Titulo],[Alinear],[Negrita],[Fondo]
										//[Hiper]: Direccion el Hipervinculo,
										//[Link]: Campos con varianles del Hipervinculo
										//[Link]:[Valor],[Campo],[Tipo]
					  $SqlDato, 		//Array con los Datos de la Tabla 
					  $SqlPie,  		//Array con los Datos del Pie de Tabla
					  $Combina	= NULL,	//Numero de Combinaciones del Pie de la Tabla
					  $OptRank	= NULL,	//Agregar Campo Ranking a la Tabla
					  $AutoSuma	= NULL,	//Funcion de Autosuma
					  $Funcion	= NULL,	//Funciones en el Pie de la Tabla
					  $BotDire	= NULL,	//IZQ=Izquierda, DER=Derecha
					  $BotDet	= NULL,	//Array Con los Botones a utilizar
					  $Conten	= NULL,	//Contenido de Check y Cajas
					  $varFactor= NULL
					  )
	{	
	ini_set("memory_limit","128M");
	$this->AbrirConexion();
	$NumOculto=0;
	$ArrayDato = $this->Query($SqlDato);
	if($this->NumFilas($ArrayDato)<=0) {			
		$tabla = $this->ErrorConsulta();
		}
	else {
		$ArrayCabee=$ArrayCabe;
		$color1 = "#F5FAFA";
		$color2 = "#edf3f3";
		$colors = "#FFFF33";//"#99FFCC";
		
		$f_ins	= $_SESSION["_FINS"];
		$f_upd	= $_SESSION["_FUPD"];
		$f_del	= $_SESSION["_FDEL"];
		$bgcolor1 = "bgcolor=$color1 onMouseOver=\"uno(this,'$colors')\"";
		$bgcolor1.= "onMouseOut=\"dos(this,'$color1')\"";		
		$bgcolor2 = "bgcolor=$color2 onMouseOver=\"uno(this,'$colors')\"";
		$bgcolor2.= "onMouseOut=\"dos(this,'$color2')\"";
		
		$NumCol =  $this->NumField($ArrayDato);
		$NumComb = 0;
		$tabla = "<table id=\"theTable\" style=\"padding: 0; margin-top:5px; margin-bottom:10px; ";      
		$tabla.= "border-collapse:collapse;\" width=\"$ancho\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" ";
		$tabla.= "class=\"paginate-20 max-pages-7\" >";
		//INICIO Cabecera
		$tabla.= "<thead>";
		$tabla.= "<tr>";
		//Opcion de Ranking
		if(isset($OptRank)) {
			$tabla.= "<th width=30 height=22 class=sortable>#</td>";
			$NumComb = $NumComb + 1;
			}
		
		//Opcion de Contenido
		if(isset($Conten)) {
			$k = json_decode($Conten, true);
			$i = 0;
			while($i<count($k['check'])) {
				$tabla.= "<th width=20 class=\"sortable\">".$k[check][$i][titulo]."</th>";
				$NumComb = $NumComb + 1;
				$i++;
				}
			$i = 0;
			while($i<count($k['caja'])) {
				$tabla.= "<th width=20 class=\"sortable\">".$k[caja][$i][titulo]."</th>";
				$NumComb = $NumComb + 1;
				$i++;
				}
			}
		
		//Opcion Botenes de Mantenimiento Izquierda
		if(isset($BotDire) && $BotDire=="IZQ") {
			$tabla.= "<th width=20 class=sortable>Opt</td>";
			$NumComb = $NumComb + 1;
			}
		//Titulo de Datos		
		for ($i=0;$i<=$NumCol-1;$i++) {	
			if(!isset($ArrayCabe[$i]["Oculto"])) {
				if(isset($ArrayCabe[$i]["Style"]) && $ArrayCabe[$i]["Style"]<>"")
					$Style = $ArrayCabe[$i]["Style"];
				else
					$Style = "sortable";
				$tabla.= "<th height=\"35\" ";
				$tabla.= (isset($ArrayCabe[$i]["Tamano"]))?"width=\"".$ArrayCabe[$i]["Tamano"]."\" ":" ";
				$tabla.= (isset($ArrayCabe[$i]["Style"]))?"class=\"".$ArrayCabe[$i]["Style"]."\" ":" class=\"sortable\" ";
				$tabla.= ">";
				
				$tabla.= (isset($ArrayCabe[$i]["Titulo"]))?$ArrayCabe[$i]["Titulo"]:$this->FieldName($ArrayDato,$i);
				$tabla.= "</th>";	
				}
			else {
				$NumOculto = $NumOculto+1;
				}
			}
		//Opcion Botenes de Mantenimiento Derecho
		if(isset($BotDire) && $BotDire=="DER") {
			$tabla.= "<th width=30 height=22 class=sortable>Opt</th>";
			}
		$tabla.= "</tr>";
		$tabla.= "</thead>";
		//FIN Cabecera
		
		for ($i=0;$i<$NumCol;$i++) {
			$Sum[$i] = 0;
			}
		//INICIO Cuerpo
		
		$tabla.= "<tbody id=cuerpoTabla>";
		$j=0;
		while($row = $this->FetchArray($ArrayDato)) {
			$j++;			
			if($j%2==0) $tabla.= "<tr $bgcolor1>";
			else $tabla.= "<tr $bgcolor2>";
		
			if(isset($OptRank)) {
				$ord++;
				$tabla.= "<td width=\"5\" valign=\"middle\" align=\"center\" class=\"interior\"><b>$ord</b></td>";
				}
		
		//Opcion de Contenido
		if(isset($Conten)) {
			$k = json_decode($Conten, true);
			$i = 0;
			while($i<count($k['check'])) {
				$tabla.= "<td width=\"10\" valign=middle align=center class=\"interior\">";
				$tabla.= "<input name=\"".$k[check][$i][name]."[".$row[$k[check][$i][valor]]."]\" ";
				$tabla.= "id=\"".$k[check][$i][id]."\" type=\"checkbox\" ";
				$tabla.= "value=\"".$row[$k[check][$i][valor]]."\" style=\"width:12px; height:12px\" >"; 
				$tabla.= "</td>";
				$NumComb = $NumComb + 1;
				$i++;
				}
			$i = 0;
			while($i<count($k['caja'])) {
				if($k[caja][$i][alinear] && $k[caja][$i][alinear]<>"") $Alinear = "; text-align:".$k[caja][$i][alinear];
				else $Alinear = "";
				$tabla.= "<td width=\"10\" valign=middle align=center class=\"interior\">";
				$tabla.= "<input name=\"".$k[caja][$i][name]."[".$row[$k[caja][$i][valor]]."]\" ";
				$tabla.= "type=\"text\" id=\"".$k[caja][$i][id]."\" onkeypress=\"".$k[caja][$i][keypress]."\"";
				$tabla.= "style=\"width:".$k[caja][$i][ancho]."px; ".$Alinear."\" class=\"".$k[caja][$i][clase]."\"/>"; 

				$tabla.= "</td>";
				$NumComb = $NumComb + 1;
				$i++;
				}
			}
			

		//Opcion Botones de Mantenimiento LADO IZQUIERDO
		if(isset($BotDire) && $BotDire=="IZQ") {
			$tabla.= "<td valign=middle class=\"interior\">";
			if(isset($BotDet)) {
				if(isset($BotDet["tipo"]) && strtoupper($BotDet["tipo"])=="BOTON") {
					$tabla.= "aqio";
					}
				else {
					foreach($BotDet as $Valor1=>$Interior) {
						$MyLink = "";
						//Buscamos Imagen del Boton
						if(isset($Interior[0]["Boton"]) && $Interior[0]["Boton"]<>"")
							$MyImag = $this->RutaImage.$Interior[0]["Boton"];
						else
							$MyImag = $this->RutaImage.$this->BotDefault;	
						
						//Permiso Para mostrar el boton
						if(isset($Interior[0]["Permite"])) 
							$flag_boton = $Interior[0]["Permite"];
						else $flag_boton = "S";
						
						//Condicion Para mostrar el boton
						if(isset($Interior[0]["Condicion"]))
							$flag_cond = ($row[$Interior[0]["Condicion"]])  ? "S" : "N";						
						else $flag_cond = "S";
						
						$MyValor 	= "";
						$MyMsje 	= "";
						$MyVentana 	= "_parent";
						$TipoLink 	= "";
						$MyClick	= "";
						if($flag_boton == "S" && $flag_cond == "S") {
							foreach($Interior as $Valor2=>$Contenido) {
								if(isset($Contenido["Mensaje"])) {
									 $MyMsje = $Contenido["Mensaje"];
									}
								if(isset($Contenido["Click"])) {
									 $MyClick = "onClick=\"".$Contenido["Click"]."\"";
									}
								if(isset($Contenido["Ventana"])) {
									 $MyVentana = $Contenido["Ventana"];
									}
								if(isset($Contenido["Link"])) {
									$MyLink = $Contenido["Link"]."?";
									$TipoLink = "Link";							
									}
								elseif(isset($Contenido["Java"])) {
									$MyLink = "javascript: ".$Contenido["Java"]."(";
									$TipoLink = "Java";							
									}
								else {
									if(isset($Contenido["Tipo"]) && $Contenido["Tipo"]=="IN") {
										if($TipoLink=="Link") {
											if($MyValor=="") $MyValor = $Contenido["Campo"]."=".$Contenido["Valor"];
											else $MyValor = $MyValor."&".$Contenido["Campo"]."=".$Contenido["Valor"];
											}
										elseif($TipoLink=="Java") {
											if($MyValor=="") $MyValor = "'".$Contenido["Valor"]."'";
											else $MyValor = $MyValor.",'".$Contenido["Valor"]."'";
											}
										}
									elseif(isset($Contenido["Tipo"]) && $Contenido["Tipo"]=="NUM") {
										if($TipoLink=="Link") {
											if($MyValor=="") $MyValor = $Contenido["Campo"]."=".$Contenido["Valor"];
											else $MyValor = $MyValor."&".$Contenido["Campo"]."=".$Contenido["Valor"];
											}
										elseif($TipoLink=="Java") {
											if($MyValor=="") $MyValor = "".$Contenido["Valor"]."";
											else $MyValor = $MyValor.",".$Contenido["Valor"]."";
											}
										}
									else {
										if($TipoLink=="Link") {
											if($MyValor=="") $MyValor = $Contenido["Campo"]."=".$row[$Contenido["Valor"]];
											else $MyValor = $MyValor."&".$Contenido["Campo"]."=".$row[$Contenido["Valor"]];
											}
										elseif($TipoLink=="Java") {
											if($MyValor=="") $MyValor = "'".str_replace("'","\'",$row[$Contenido["Valor"]])."'";
											else $MyValor = $MyValor.",'".mysql_real_escape_string(str_replace("\"","'",$row[$Contenido["Valor"]]))."'";											
											}
										}
									}
								}
							if($TipoLink=="Link") $MyLink = $MyLink.$MyValor;
							elseif($TipoLink=="Java") $MyLink = $MyLink.$MyValor.")";
							
							if($MyMsje=="") $MyMsje = "Ver Detalle del Registro $row[0] - $row[1]";
							else $MyMsje = $MyMsje."<br>".$row[0]." - ".$row[1];
							
							$tabla.= "<a href=\"$MyLink\" target=\"$MyVentana\" $MyClick>";
							$tabla.= "<img src=\"$MyImag\" width=16 height=16 border=0 ";
							$tabla.= "onMouseOver=\"MensajeTip('$MyMsje')\"  ";
							$tabla.= "onMouseOut=\"UnTip()\"/></a>";
						
	
							}
						}
					}
				}			
			$tabla.= "</td>";
			}
		// Detalle de la tabla
		for ($i=0;$i<$NumCol;$i++) {
			$type = $this->Fieldtype($ArrayDato, $i);
			$long = $this->FieldLength($ArrayDato, $i);
			if(isset($varFactor))
				$Sum[$i] = $Sum[$i] + floor($row[$i])*$varFactor + ($row[$i]-floor($row[$i]))*100;
			else
				$Sum[$i] = $Sum[$i] + $row[$i];						
			$CadAlinear = "";
			$CadFondo	= "";
			$OptNegrita[0] 	= "";
			$OptNegrita[1] 	= "";
			$OptLink[0]	   	= "";
			$OptLink[1]		= "";
			//Verificamos si el Campo Tiene Tipo de Alineacion
			if(isset($ArrayCabe[$i]["Alinear"])) $CadAlinear = "align=".$ArrayCabe[$i]["Alinear"];
			//Verificamos si el Campo Tiene Color de Fondo
			if(isset($ArrayCabe[$i]["Fondo"])) $CadFondo = "bgcolor=".$ArrayCabe[$i]["Fondo"];
			//Verificamos si el Campo Tiene Valor Negrita
			if(isset($ArrayCabe[$i]["Negrita"])) {
				$OptNegrita[0] = "<b>";
				$OptNegrita[1] = "</b>";
				}
			//Verificamos si el Campo Tiene un Hipervinculo
			if (isset($ArrayCabe[$i]["Hiper"])) {
				$MyLink = $ArrayCabe[$i]["Hiper"]."?";
				$k = 1;
				if(isset($ArrayCabe[$i]["Link"])) {
					foreach($ArrayCabe[$i]["Link"] as $Valor=>$Contenido) {
						$Valor = $Contenido["Valor"];
						$Campo = $Contenido["Campo"];
						$Tipo  = $Contenido["Tipo"];
						if($k==1) {
							if(isset($Tipo)) $MyLink.= $Campo."=".$Valor;
							else $MyLink.= $Campo."=".trim($row[$Valor]);
							}
						else {
							if(isset($Tipo)) $MyLink.= "&".$Campo."=".$Valor;
							else $MyLink.= "&".$Campo."=".trim($row[$Valor]);
							}
						$k++;
						}
					}//Fin if(isset($ArrayCabe[$i]["Link"]))
				$OptLink[0] ="<b><a href='$MyLink' target=_blank ";
				$OptLink[0].="onmouseover=\"MensajeTip('Haga Click en <b>$row[$i]</b><br>";
				$OptLink[0].="para Ver el Detalle de este Registro')\" onmouseout=\"UnTip()\">";
				$OptLink[1] = "</a></b>";
				}
			//Definir Clase por Condicion
			if(isset($ArrayCabe[$i]["Condicion"]) && !is_null($row[$i])) {				
				switch($ArrayCabe[$i]["Condicion"][0]) {
						case "=":
							if($row[$i] = $ArrayCabe[$i]["Condicion"][1]) 
								$clase = $ArrayCabe[$i]["Condicion"][2];
							else 
								$clase = "interior";
							break;
						case ">":
							if($row[$i] > $ArrayCabe[$i]["Condicion"][1]) 
								$clase = $ArrayCabe[$i]["Condicion"][2];
							else 
								$clase = "interior";
							break;
						case ">=":
							if($row[$i] >= $ArrayCabe[$i]["Condicion"][1]) 
								$clase = $ArrayCabe[$i]["Condicion"][2];
							else 
								$clase = "interior";
							break;
						case "<":
							if($row[$i] < $ArrayCabe[$i]["Condicion"][1]) 
								$clase = $ArrayCabe[$i]["Condicion"][2];
							else 
								$clase = "interior";
							break;
						case "<=":
							if($row[$i] <= $ArrayCabe[$i]["Condicion"][1]) 
								$clase = $ArrayCabe[$i]["Condicion"][2];
							else 
								$clase = "interior";
							break;
						}
				}
			else $clase = "interior";
			
			if(!isset($ArrayCabe[$i]["Oculto"])) {	
				$varCabeTipo = (isset($ArrayCabe[$i]["Tipo"]))? $ArrayCabe[$i]["Tipo"] : NULL;
				$tabla.= "<td height=20 valign=middle $CadAlinear $CadFondo class=\"$clase\">";
				$tabla.= $OptNegrita[0];
				$tabla.= $OptLink[0];
				if (strtoupper($type)=="CHAR" || strtoupper($type)=="VARCHAR" || 
					strtoupper($type)=="STRING") {
					if (!is_null($row[$i])) {						
						if ($varCabeTipo=="Imagen") $tabla.= $row[$i];	
						else $tabla.= htmlentities($row[$i]);	
						}
					}
				elseif (strtoupper($type)=="INT") {
					if (!is_null($row[$i]))	$tabla.= number_format($row[$i], 0,'.',',');
					}
				elseif (strtoupper($type)=="DOUBLE" || strtoupper($type)=="REAL" || 
						strtoupper($type)=="NUMERIC" || strtoupper($type)=="DECIMAL") {				
					if (!is_null($row[$i])) {
						if(isset($ArrayCabe[$i]["Decimal"])) {
							if($ArrayCabe[$i]["Decimal"]=="E") 
								$tabla.= $row[$i];
							else
								$tabla.= number_format($row[$i], $ArrayCabe[$i]["Decimal"],'.',',');
							}
						else
							$tabla.= number_format($row[$i], 2,'.',',');
						}
					}
				else
					$tabla.= mysql_real_escape_string($row[$i]);	
				//Agregar texto Porcentaje
				if(isset($ArrayCabe[$i]["Formato"]) && !is_null($row[$i])) $tabla.= $ArrayCabe[$i]["Formato"];
				$tabla.= $OptLink[1];
				$tabla.= $OptNegrita[1];
				$tabla.= "</td>";
				}
			}
		//Opcion Botones de Mantenimiento LADO DERECHO
		if(isset($BotDire) && $BotDire=="DER") {
			$tabla.= "<td valign=middle align=center class=\"interior\">";
			if(isset($BotDet)) {	
				foreach($BotDet as $Valor1=>$Interior) {
					$MyLink = "";
					//Buscamos Imagen del Boton
					if(isset($Interior[0]["Boton"]) && $Interior[0]["Boton"]<>"")
						$MyImag = $this->RutaImage.$Interior[0]["Boton"];
					else
						$MyImag = $this->RutaImage.$this->BotDefault;	
					
					//Permiso Para mostrar el boton
					if(isset($Interior[0]["Permite"])) 
						$flag_boton = $Interior[0]["Permite"];
					else $flag_boton = "S";
					
					$MyValor = "";
					$MyMsje = "";
					$MyVentana = "_parent";
					
					if($flag_boton == "S") {
						foreach($Interior as $Valor2=>$Contenido) {
							if(isset($Contenido["Mensaje"])) {
								 $MyMsje = $Contenido["Mensaje"];
								}
							if(isset($Contenido["Ventana"])) {
								 $MyVentana = $Contenido["Ventana"];
								}
							if(isset($Contenido["Link"])) {
								$MyLink = $Contenido["Link"]."?";
								//$MyImag = $this->RutaImage.$Contenido["Boton"];							
								}
							else {
								if(isset($Contenido["Tipo"]) && $Contenido["Tipo"]=="IN") {
									if($MyValor=="") $MyValor = $Contenido["Campo"]."=".$Contenido["Valor"];
									else $MyValor = $MyValor."&".$Contenido["Campo"]."=".$Contenido["Valor"];
									}
								else {
									if($MyValor=="") $MyValor = $Contenido["Campo"]."=".$row[$Contenido["Valor"]];
									else $MyValor = $MyValor."&".$Contenido["Campo"]."=".$row[$Contenido["Valor"]];
									}
								}
							}
						if($MyMsje=="") $MyMsje = "Ver Detalle del Registro $row[0] - $row[1]";
						else $MyMsje = $MyMsje."<br>".$row[0]." - ".$row[1];
						$tabla.= "<a href=\"$MyLink$MyValor\" target=\"$MyVentana\">";
						$tabla.= "<img src=\"$MyImag\" width=16 height=16 border=0 ";
						$tabla.= "onMouseOver=\"MensajeTip('$MyMsje')\" ";
						$tabla.= "onMouseOut=\"UnTip()\"/></a>";
						}
					}
				}			
			$tabla.= "</td>";
			}
		$tabla.= "</tr>";
	}
	
	$tabla.= "</tbody>";
	$this->CerrarConexion();
	
	
	$MyComb = 0;	
	if (isset($AutoSuma)) {
		$tabla.= "<tfoot>";
		$tabla.= "<tr>";
		$tabla.= "<td height=22 ";
		for ($i=0;$i<$Combina;$i++){
			if(!isset($ArrayCabe[$i]["Oculto"])) {
				$MyComb = $MyComb + 1;
				}
			}
		$MyComb = $MyComb + $NumComb;
		if ($Combina>1) $tabla.= " colspan=".$MyComb;	
		$tabla.= " align=\"right\" valign=middle class=FormTituloSub>";
		$tabla.= " Totales : &nbsp;</td>";
	
	for ($i=$Combina;$i<$NumCol;$i++)	{
		$valDecimal = (isset($ArrayCabe[$i]["Decimal"]))?$ArrayCabe[$i]["Decimal"]:2;		
						
		if(!isset($ArrayCabe[$i]["Oculto"])) {
			$result="";
			$tabla.= "<td valign=\"middle\" class=\"FormTituloSub\" align=\"right\" >";
			if(isset($Funcion) && $Funcion<>"")	{
				foreach ($Funcion as $clave1=>$subfuncion) {
					foreach ($subfuncion as $clave=>$valor) {
						switch($clave) {
							case "funcion":
								$func = $valor;
								break;
							case "posicion":
								$posicion = $valor;
								break;
							case "valor1":
								$valor1 = $valor;
								break;
							case "valor2":
								$valor2 = $valor;
								break;
							}
						}
						//Buscamos las Funciones
						if($posicion == $i) {
							switch($func) {
								case "Porc":
									if($Sum[$valor2]==0)
										$result="0.00%";
									else
										$result =  number_format($Sum[$valor1]/$Sum[$valor2]*100, $valDecimal,'.',',')."%";
									break;
								case "Crec":
									if($Sum[$valor1]==0)
										$result="0.00%";
									else
										$result =  number_format($Sum[$valor1]/$Sum[$valor2]*100-100, $valDecimal,'.',',')."%";
									break;
								case "Suma":
									$result = number_format($Sum[$valor1]+$Sum[$valor2], $valDecimal,'.',',');
									break;
								case "Resta":
									$result = number_format($Sum[$valor1]-$Sum[$valor2], $valDecimal,'.',',');
									break;
								}
							
							}
					}
				}
			if(isset($varFactor))
				$varSuma = floor($Sum[$i]/$varFactor) + round($Sum[$i])%round($varFactor)/100;								
			else
				$varSuma = $Sum[$i];
			
			if ($result=="") $tabla.= number_format($varSuma, $valDecimal,'.',',');
			else $tabla.= $result;
			$tabla.= "</td>";
			}
		}
	//Opcion Botenes de Mantenimiento Derecho
	if(isset($BotDire) && $BotDire=="DER") {
		$tabla.= "<td align=center valign=middle class=menu>Opt</td>";
		}
	$tabla.= "</tr>";
	$tabla.= "</tfoot>";	
	}
	
	elseif (isset($SqlPie) && $SqlPie<>"") {	
		$this->AbrirConexion();
		$ArrayPie = $this->Query($SqlPie);		
		$this->CerrarConexion();
		$NumColPie = $this->NumField($ArrayPie);
		
			$row = $this->FetchArray($ArrayPie);
			$tabla.= "<tfoot>";
			$tabla.= "<tr>";
			$tabla.= "<td height=22 ";
			$NumComb=$Combina+$NumComb;
			if ($Combina>1) 
				{
				$tabla.= " colspan=".$NumComb;
				}
			$tabla.= " align=right valign=middle class=menuTotales>";		
			
			$tabla.= "Totales ".$row[0]." - ".$row[1]." : &nbsp;</td>";
			
			for ($i=2; $i<$NumColPie ; $i++) {
				$type = $this->Fieldtype($ArrayPie, $i);
				$long = $this->FieldLength($ArrayPie, $i);	
				$Diferencia = $this->NumField($ArrayDato) - $this->NumField($ArrayPie);
				$icab = $i + $Diferencia;
				$tabla.= "<td valign=middle class=menuTotales>";
				
				if (strtoupper($type)=="CHAR" || strtoupper($type)=="VARCHAR" || 
					strtoupper($type)=="STRING") {
					if (!is_null($row[$i])) $tabla.= $row[$i];	
					$Sum[$i]=$Sum[$i]+1;	
					}
				elseif (strtoupper($type)=="INT") {
					if (!is_null($row[$i]))	$tabla.= number_format($row[$i], 0,'.',',');
					$Sum[$i]=$Sum[$i]+$row[$i];
					}
				elseif (strtoupper($type)=="DOUBLE" || strtoupper($type)=="REAL" || 
						strtoupper($type)=="NUMERIC" || strtoupper($type)=="DECIMAL") {			
					if (!is_null($row[$i])) $tabla.= number_format($row[$i], 2,'.',',');
					$Sum[$i]=$Sum[$i]+$row[$i];
					}
				else
					$tabla.= $row[$i];	
				if (strpos($ArrayCabe[$icab]["Titulo"],"%") && !is_null($row[$i])) $tabla.= "%";
				$tabla.= "</td>";
				}
			//Opcion Botenes de Mantenimiento Derecho
			if(isset($BotDire) && $BotDire=="DER") {
				$tabla.= "<td align=center valign=middle class=menu>Opt</td>";
				}
			$tabla.= "</tr>";
			$tabla.= "</tfoot>";
//		$this->CerrarConexion();
		}	
	$tabla.= "</table>";
	}
	return $tabla;
	}	
	
function SetGridTabla($varTabla) {	
	$tabla 	= "";
	$color1 = "#F5FAFA";
	$color2 = "#edf3f3";
	$colors = "#FFFF33";//"#99FFCC";	
	$bgcolor1 = "bgcolor=$color1 onMouseOver=\"uno(this,'$colors')\"";
	$bgcolor1.= "onMouseOut=\"dos(this,'$color1')\"";		
	$bgcolor2 = "bgcolor=$color2 onMouseOver=\"uno(this,'$colors')\"";
	$bgcolor2.= "onMouseOut=\"dos(this,'$color2')\"";
	if(!isset($varTabla["sql_cab"]) || $varTabla["sql_cab"]=="") {
		$tabla.= $this->ErrorConsulta();
		}
	else {
		$this->AbrirConexion();
		$rs = $this->Query($varTabla["sql_cab"]);
		$this->CerrarConexion();
		$varNumCol = $this->NumField($rs);
		$varNumFil = $this->NumFilas($rs);
		/*===============================================================================================
		-------------------------------------PROPIEDADES DE LA TABLA------------------------------------
		===============================================================================================*/
		$tabla.= "<table ";
		$tabla.= (isset($varTabla["id"]))?"id=\"".$varTabla["id"]."\" ": NULL;
		$tabla.= (isset($varTabla["name"]))?"name=\"".$varTabla["name"]."\" ": NULL;
		$tabla.= "style=\"padding: 0; margin-top:5px; margin-bottom:10px; border-collapse:collapse;\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" ";
		$tabla.= (isset($varTabla["tamano"]))?"width=\"".$varTabla["tamano"]."\" ": NULL;
		$tabla.= "class=\"max-pages-7 paginate-";
		$tabla.= (isset($varTabla["pages"]))?$varTabla["pages"]: 20;
		$tabla.= " \" >";
		
		/*===============================================================================================
		--------------------------------------CABECERA DE LA TABLA--------------------------------------
		===============================================================================================*/
		$tabla.= "<thead>";
		if(isset($varTabla["titulo"])) {
			$tabla.= "<tr>";
			$tabla.= "<th align=\"center\" class=\"FormTitulo\" colspan=\"$varNumCol\">".$varTabla["titulo"]."</th>";
			$tabla.= "</tr>";
			}
		$tabla.= "<tr>";
		if(isset($varTabla["boton"]) && count($varTabla["boton"])>0) {
			$tabla.= "<th class=\"sortable\" ";
			$vartam = count($varTabla["boton"])*15;
			$tabla.= "width=\"".$vartam."\" ";
			$tabla.= ">OPT</th>";	
			}
		for ($j=0; $j< $varNumCol; $j++) {
			if(!isset($varTabla["conte"][$j]["oculto"]) || $varTabla["conte"][$j]["oculto"]<>"SI") {
				$tabla.= "<th height=\"35\" ";
				$tabla.= (isset($varTabla["conte"][$j]["ancho"]))?"width=\"".$varTabla["conte"][$j]["ancho"]."\" ":NULL;
				$tabla.= (isset($varTabla["conte"][$j]["clase"]))?"class=\"".$varTabla["conte"][$j]["clase"]."\" ":" class=\"sortable\" ";
				$tabla.= ">";			
				$tabla.= (isset($varTabla["conte"][$j]["titulo"]))?$varTabla["conte"][$j]["titulo"]:$this->FieldName($rs,$j);
				$tabla.= "</th>";
				}
			}		
		$tabla.= "</tr>";
		$tabla.= "</thead>";		
		/*===============================================================================================
		--------------------------------------CONTENIDO DE LA TABLA--------------------------------------
		===============================================================================================*/
		$tabla.= "<tbody id=cuerpoTabla>";
		$j=0;
		while($row = $this->FetchArray($rs)) {
			$j++;
			
			if($j%2==0) $tabla.= "<tr class=\"par\">";
			else $tabla.= "<tr class=\"impar\">";
				
			if(isset($varTabla["boton"]) && count($varTabla["boton"])>0) {
				$tabla.= "<td class=\"FormTituloSub\" nowrap=\"nowrap\">";
				foreach($varTabla["boton"] as $varBoton) {
					//===================================================================================================================================//
					// FUNCION CLICK
					//===================================================================================================================================//
					$icon = 0;
					$varClick="";
					if(count($varBoton["click"])>1) {
						foreach($varBoton["click"] as $valor=>$ArrayClick) {
							if($valor=="funcion") {
								$varClick.= $ArrayClick."(";
								}
							elseif($valor=="param") {
								foreach($ArrayClick as $varCampo) {
									if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM")
										$varClick.= ($icon==0)?htmlentities($varCampo["valor"]):",".htmlentities($varCampo["valor"]);
									elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
										$varClick.= ($icon==0)?"'".htmlentities($varCampo["valor"])."'":",'".htmlentities($varCampo["valor"])."'";
									elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
										$varClick.= ($icon==0)?"'".htmlentities($row[$varCampo["valor"]])."'": ",'".htmlentities($row[$varCampo["valor"]])."'";
									else
										$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
														
									$icon++;
									}									
								}
							
							}
						$varClick.= ")";
						}
					else {
						$varClick = $varBoton["click"];
						}
					//===================================================================================================================================//
					// FUNCION TOOLTIP
					//===================================================================================================================================//
					$varTolTip = "";
					if(count($varBoton["toltip"])>1) {
						foreach($varBoton["toltip"] as $varCampo) {
							if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
								$varTolTip.= htmlentities($row[$varCampo["valor"]]);
							else
								$varTolTip.= htmlentities($varCampo["valor"]);
							}
						}
					else {
						$varTolTip = $varBoton["toltip"];
						}
					//Si el tipo de Boton es un CHEKBOX
					if(isset($varBoton["valor"]) && is_array($varBoton["valor"])) {
						$varValor = "";
						foreach($varBoton["valor"] as $varCampo) {
							if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
								$varValor.= ($varValor=="")?$row[$varCampo["valor"]]:"-".$row[$varCampo["valor"]];
							else
								$varValor.= ($varValor=="")?$varCampo["valor"]:"-".$varCampo["valor"];
							}
						}
					else {
						$varValor = $varBoton["valor"];
						}
					$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
									      "id"		=>$varBoton["id"],
										  "name"	=>$varBoton["name"], 										  
										  "locked"	=>$varBoton["locked"],
										  "clase"	=>$varBoton["clase"], 
										  "img"		=>$varBoton["img"], 
										  "checked"	=>$varBoton["checked"], 
										  "click"	=>$varClick, 
										  "toltip"	=>$varTolTip, 
										  "dclick"	=>$varBoton["dclick"],
										  "keyup"	=>$varBoton["keyup"],
										  "keypress"=>$varBoton["keypress"], 
										  "valor"	=>$varValor, 
										  "ancho"	=>$varBoton["ancho"],
										  "alinear"	=>$varBoton["alinear"]);

					/*Si existe condicion para mostrar el boton*/
					if (isset($varBoton["cond"])) {						
						$varRespCond_1="SI";						
						foreach($varBoton["cond"] as $varCond) {						
							if($varRespCond_1 == "SI") {
								$varRespCond = $this->CumpleCondicion($row[$varCond[0]], $varCond[1], $varCond[2]);
								if($varRespCond=="NO"){
									$varRespCond_1="NO";
									}
								}
							}
						if($varRespCond=="SI")
							$tabla.=$this->SetCampo($varCampoBoton)."&nbsp;";
						else {
							$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
												  "id"		=>$varBoton["id"],
												  "name"	=>$varBoton["name"], 										  
												  "img"		=>"images/b_none.png", 
												  );
							$tabla.=$this->SetCampo($varCampoBoton)."&nbsp;";
							}
						}
					else {
						$tabla.=$this->SetCampo($varCampoBoton)."&nbsp;";
						}
					}				
				$tabla.="</td>";	
				}
			$varFlagAlto = 0;
			for ($i=0;$i<$varNumCol;$i++) {				
				if(isset($varTabla["conte"][$i]["factor"]))
					$varSum[$i] = $varSum[$i] + floor($row[$i])*$varTabla["conte"][$i]["factor"] + ($row[$i]-floor($row[$i]))*100;
				else
					$varSum[$i] = $varSum[$i] + $row[$i];	
				
				if(!isset($varTabla["conte"][$i]["oculto"]) || $varTabla["conte"][$i]["oculto"]<>"SI") {
					
					$tabla.= "<td class=\"interior\" ";
					if(isset($varTabla["conte"][$i]["fijo"]) || $varTabla["conte"][$i]["fijo"]=="SI") {
						$tabla.= " NOWRAP ";
						}
					$tabla.= ($varFlagAlto==0)?"height=\"22\" ":NULL;
					$varFlagAlto++;
					$tabla.= (isset($varTabla["conte"][$i]["alinear"]))? "align=\"".$varTabla["conte"][$i]["alinear"]."\" ": NULL;
					$tabla.= ">";					
					$varTipoDato = (isset($varTabla["conte"][$i]["tipodat"]))? $varTabla["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
					$varNumDec   = (isset($varTabla["conte"][$i]["decimal"]))? $varTabla["conte"][$i]["decimal"]:NULL;
					if(isset($varTabla["conte"][$i]["campo"]) && count($varTabla["conte"][$i]["campo"])>0) {
						$varId_In 	= (isset($varTabla["conte"][$i]["campo"]["id_in"]))?"[".$row[$varTabla["conte"][$i]["campo"]["id_in"]]."]":NULL;
						$varName_In = (isset($varTabla["conte"][$i]["campo"]["name_in"]))?"[".$row[$varTabla["conte"][$i]["campo"]["name_in"]]."]":NULL;
						$varCampoBoton= array("tipo"	=>$varTabla["conte"][$i]["campo"]["tipo"], 
											  "id"		=>$varTabla["conte"][$i]["campo"]["id"].$varId_In,
											  "name"	=>$varTabla["conte"][$i]["campo"]["name"].$varName_In,
											  "combo"	=>$varTabla["conte"][$i]["campo"]["combo"],											  
											  "locked"	=>$varTabla["conte"][$i]["campo"]["locked"],
											  "clase"	=>$varTabla["conte"][$i]["campo"]["clase"], 
											  "img"		=>$varTabla["conte"][$i]["campo"]["img"], 
											  "checked"	=>$varTabla["conte"][$i]["campo"]["checked"], 
											  "click"	=>$varClick, 
											  "toltip"	=>$varTolTip, 
											  "dclick"	=>$varTabla["conte"][$i]["campo"]["dclick"],
											  "keyup"	=>$varTabla["conte"][$i]["campo"]["keyup"],
											  "keypress"=>$varTabla["conte"][$i]["campo"]["keypress"], 
											  "valor"	=>$row[$i], 
											  "ancho"	=>$varTabla["conte"][$i]["campo"]["ancho"],
											  "alinear"	=>$varTabla["conte"][$i]["campo"]["alinear"]);
						$tabla.= $this->SetCampo($varCampoBoton);
						}
					else {
						$tabla.= $this->setFormatTipoDato($row[$i], $varTipoDato, $varNumDec);
						}
					$tabla.= "</td>";
					}
				}			
			$tabla.= "</tr>";
			}
		$tabla.= "</thead>";
		/*===============================================================================================
		--------------------------------------PIE DE LA TABLA--------------------------------------
		===============================================================================================*/
		$tabla.= "<tfoot>";
		$tabla.= "<tr>";
		if(isset($varTabla["boton"]) && count($varTabla["boton"])>0) {
			$tabla.= "<td class=\"FormTituloSub\" align=\"center\"></td>";				
			}
		for ($i=0;$i<$varNumCol;$i++) {
			if(!isset($varTabla["conte"][$i]["oculto"]) || $varTabla["conte"][$i]["oculto"]<>"SI") {
				$tabla.= "<td class=\"FormTituloSub\" ";
				$tabla.= ($i==0)?"height=\"22\" ":NULL;
				$tabla.= (isset($varTabla["conte"][$i]["alinear"]))? "align=\"".$varTabla["conte"][$i]["alinear"]."\" ": NULL;
				$tabla.= ">";
				if(isset($varTabla["conte"][$i]["autosuma"]) && $varTabla["conte"][$i]["autosuma"]=="SI") {
					$varTipoDato = (isset($varTabla["conte"][$i]["tipodat"]))? $varTabla["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
					$varNumDec   = (isset($varTabla["conte"][$i]["decimal"]))? $varTabla["conte"][$i]["decimal"]:NULL;					
					$tabla.= $this->setFormatTipoDato($varSum[$i], $varTipoDato, $varNumDec);
					}
				elseif(isset($varTabla["conte"][$i]["funcion"]) && count($varTabla["conte"][$i]["funcion"])>0) {
					$varFuncion  = $varTabla["conte"][$i]["funcion"][0];
					if($varFuncion == "autosuma") $varValor1 = $varSum[$i];
					elseif($varFuncion == "contar")	$varValor1 = $varNumFil;				
					else {
						$varValor1 = $varSum[$varTabla["conte"][$i]["funcion"][1]];
						$varValor2 = $varSum[$varTabla["conte"][$i]["funcion"][2]];						
						}
					$varValor  	 = $this->calcularPiePagina($varFuncion, $varValor1, $varValor2);
					$varTipoDato = (isset($varTabla["conte"][$i]["tipodat"]))? $varTabla["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
					$varNumDec   = (isset($varTabla["conte"][$i]["decimal"]))? $varTabla["conte"][$i]["decimal"]:NULL;					
					$tabla.= $this->setFormatTipoDato($varValor, $varTipoDato, $varNumDec);
					}
				$tabla.= "</td>";
				}
			}
		$tabla.= "</tr>";
		$tabla.= "</tfoot>";
		$tabla.= "</table>";		
		}
	return $tabla;
	}	
		
	function calcularPiePagina($varTipo, $varValor1, $varValor2) {
		switch ($varTipo) {
			case "autosuma":
				$varReturn = $varValor1;
				break;
			case "contar":
				$varReturn = "&nbsp;".$varValor1." Registros";
				break;
			case "porcentaje":
			case "porc":
			case "%":
				if ($varValor2==0) 
					$varReturn = 0;
				else
					$varReturn = round($varValor1/$varValor2*100,2);
				break;
			}
		return $varReturn;
		}
		
	function CumpleCondicion($varDato, $varParametro, $varCondicion) {
		switch ($varParametro) {
			case "=":
			case "==":
			case "IGUAL":
			case "IGU":
				$varResp = ($varDato==$varCondicion)?"SI":"NO"; 
				break;
			case "<>":
			case "=!":
			case "DIFER":
			case "DIF":
				$varResp = ($varDato<>$varCondicion)?"SI":"NO"; 
				break;
			case ">":
			case "MAYOR":
				$varResp = ($varDato>$varCondicion)?"SI":"NO"; 
				break;
			case "<":
			case "MENOR":
				$varResp = ($varDato<$varCondicion)?"SI":"NO"; 
				break;
			case ">=":
			case "MAYIG":
				$varResp = ($varDato>=$varCondicion)?"SI":"NO"; 
				break;
			case "<=":
			case "MENIG":
				$varResp = ($varDato<=$varCondicion)?"SI":"NO"; 
				break;
			}
		return $varResp;
		}
	
	function SetGridTabla2($varTabla) {	
		$tabla 	= "";
		$color1 = "#F5FAFA";
		$color2 = "#edf3f3";
		$colors = "#FFFF33";//"#99FFCC";	
		$bgcolor1 = "bgcolor=$color1 onMouseOver=\"uno(this,'$colors')\"";
		$bgcolor1.= "onMouseOut=\"dos(this,'$color1')\"";		
		$bgcolor2 = "bgcolor=$color2 onMouseOver=\"uno(this,'$colors')\"";
		$bgcolor2.= "onMouseOut=\"dos(this,'$color2')\"";
		if(!isset($varTabla["sql_cab"]) || $varTabla["sql_cab"]=="") {
			$tabla.= $this->ErrorConsulta();
			}
		else {
			$this->AbrirConexion();
			$rs = $this->Query($varTabla["sql_cab"]);
			$this->CerrarConexion();
			$varNumCol = $this->NumField($rs);
			$varNumFil = $this->NumFilas($rs);
			/*===============================================================================================
			-------------------------------------PROPIEDADES DE LA TABLA------------------------------------
			===============================================================================================*/
			$tabla.= "<table ";
			$tabla.= (isset($varTabla["id"]))?"id=\"".$varTabla["id"]."\" ": NULL;
			$tabla.= (isset($varTabla["name"]))?"name=\"".$varTabla["name"]."\" ": NULL;
			$tabla.= "style=\"padding: 0; margin-top:5px; margin-bottom:10px; border-collapse:collapse;\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" ";
			$tabla.= (isset($varTabla["tamano"]))?"width=\"".$varTabla["tamano"]."\" ": NULL;
			$tabla.= "class=\"matriz\" ";
			$tabla.= ">";
			
			/*===============================================================================================
			--------------------------------------CABECERA DE LA TABLA--------------------------------------
			===============================================================================================*/
			$tabla.= "<thead>";
			if(isset($varTabla["titulo"])) {
				$tabla.= "<tr>";
				$tabla.= "<th align=\"center\" class=\"FormTitulo\" colspan=\"$varNumCol\">".$varTabla["titulo"]."</th>";
				$tabla.= "</tr>";
				}
			$tabla.= "<tr>";
			if(isset($varTabla["boton"]) && count($varTabla["boton"])>0) {
				$tabla.= "<th class=\"sortable\" nowrap=\"nowrap\" ";
				$vartam = count($varTabla["boton"])*15;
				$tabla.= "width=\"".$vartam."\" ";
				$tabla.= ">OPT</th>";	
				}
			for ($j=0; $j< $varNumCol; $j++) {
				if(!isset($varTabla["conte"][$j]["oculto"]) || $varTabla["conte"][$j]["oculto"]<>"SI") {
					$varCab = (isset($varTabla["conte"][$j]["titulo"]))?$varTabla["conte"][$j]["titulo"]:$this->FieldName($rs,$j);
					$tabla.= "<th height=\"35\" ";
					$tabla.= (isset($varTabla["conte"][$j]["ancho"]))?"width=\"".$varTabla["conte"][$j]["ancho"]."\" ":NULL;
					$tabla.= (isset($varTabla["conte"][$j]["clase"]))?"class=\"".$varTabla["conte"][$j]["clase"]."\" ":" class=\"ordenar\" ";
					$tabla.= ">";
					$tabla.= "<table><tr>";
					$tabla.= "<th>";
					$tabla.= $varCab;
					$tabla.= "</th>";
					$tabla.= "<th>";
					$tabla.= "<img onclick=\"xBuscarJS('".$this->FieldName($rs,$j)."','ASC');return false;\" ";
					$tabla.= "style=\"cursor: pointer;\" title=\"Ordenar por $varCab de Forma Ascendente\" src=\"images/sort_incr.png\">";
					$tabla.= "<img onclick=\"xBuscarJS('".$this->FieldName($rs,$j)."','DESC');return false;\" ";
					$tabla.= "style=\"cursor: pointer;\" title=\"Ordenar por $varCab de Forma Descendente\" src=\"images/sort_decrease.png\">";
					$tabla.= "</th>";
					$tabla.= "</tr></table>";
					
									
	
					$tabla.= "</th>";
					}
				}		
			$tabla.= "</tr>";
			$tabla.= "</thead>";		
			/*===============================================================================================
			--------------------------------------CONTENIDO DE LA TABLA--------------------------------------
			===============================================================================================*/
			$tabla.= "<tbody id=cuerpoTabla>";
			$j=0;
			while($row = $this->FetchArray($rs)) {
				$j++;
				if($j%2==0) $tabla.= "<tr class=\"par\">";
				else $tabla.= "<tr class=\"impar\">";
				
				if(isset($varTabla["boton"]) && count($varTabla["boton"])>0) {
					$tabla.= "<td class=\"FormTituloSub\" nowrap=\"nowrap\" >";
					foreach($varTabla["boton"] as $varBoton) {
						//===================================================================================================================================//
						// FUNCION CLICK
						//===================================================================================================================================//
						$icon = 0;
						$varClick="";
						if(count($varBoton["click"])>1) {
							foreach($varBoton["click"] as $valor=>$ArrayClick) {
								if($valor=="funcion") {
									$varClick.= $ArrayClick."(";
									}
								elseif($valor=="param") {
									foreach($ArrayClick as $varCampo) {
										if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="NUM" || 
																	   strtoupper($varCampo["tipo"])=="NUMERO" || 
																	   strtoupper($varCampo["tipo"])=="NUMBER")
											$varClick.= ($icon==0)?htmlentities($varCampo["valor"]):",".htmlentities($varCampo["valor"]);
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="TEX")
											$varClick.= ($icon==0)?"'".htmlentities($varCampo["valor"])."'":",'".htmlentities($varCampo["valor"])."'";
										elseif(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
											$varClick.= ($icon==0)?"'".htmlentities($row[$varCampo["valor"]])."'": ",'".htmlentities($row[$varCampo["valor"]])."'";
										else
											$varClick.= ($icon==0)?"'".$varCampo["valor"]."'":",'".$varCampo["valor"]."'";
															
										$icon++;
										}									
									}
								
								}
							$varClick.= ")";
							}
						else {
							$varClick = $varBoton["click"];
							}
						//===================================================================================================================================//
						// FUNCION TOOLTIP
						//===================================================================================================================================//
						$varTolTip = "";
						if(count($varBoton["toltip"])>1) {
							foreach($varBoton["toltip"] as $varCampo) {
								if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
									$varTolTip.= htmlentities($row[$varCampo["valor"]]);
								else
									$varTolTip.= htmlentities($varCampo["valor"]);
								}
							}
						else {
							$varTolTip = $varBoton["toltip"];
							}
						//Si el tipo de Boton es un CHEKBOX
						if(isset($varBoton["valor"]) && is_array($varBoton["valor"])) {
							$varValor = "";
							foreach($varBoton["valor"] as $varCampo) {
								if(isset($varCampo["tipo"]) && strtoupper($varCampo["tipo"])=="INDEX")
									$varValor.= ($varValor=="")?$row[$varCampo["valor"]]:"-".$row[$varCampo["valor"]];
								else
									$varValor.= ($varValor=="")?$varCampo["valor"]:"-".$varCampo["valor"];
								}
							}
						else {
							$varValor = $varBoton["valor"];
							}
						$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
											  "id"		=>$varBoton["id"],
											  "name"	=>$varBoton["name"], 										  
											  "locked"	=>$varBoton["locked"],
											  "clase"	=>$varBoton["clase"], 
											  "img"		=>$varBoton["img"], 
											  "checked"	=>$varBoton["checked"], 
											  "click"	=>$varClick, 
											  "toltip"	=>$varTolTip, 
											  "dclick"	=>$varBoton["dclick"],
											  "keyup"	=>$varBoton["keyup"],
											  "keypress"=>$varBoton["keypress"], 
											  "valor"	=>$varValor, 
											  "ancho"	=>$varBoton["ancho"],
											  "alinear"	=>$varBoton["alinear"]);
	
						/*Si existe condicion para mostrar el boton*/
						if (isset($varBoton["cond"])) {						
							$varRespCond_1="SI";						
							foreach($varBoton["cond"] as $varCond) {						
								if($varRespCond_1 == "SI") {
									$varRespCond = $this->CumpleCondicion($row[$varCond[0]], $varCond[1], $varCond[2]);
									if($varRespCond=="NO"){
										$varRespCond_1="NO";
										}
									}
								}
							if($varRespCond=="SI")
								$tabla.=$this->SetCampo($varCampoBoton)."&nbsp;";
							else {
								$varCampoBoton= array("tipo"	=>$varBoton["tipo"], 
													  "id"		=>$varBoton["id"],
													  "name"	=>$varBoton["name"], 										  
													  "img"		=>"images/b_none.png", 
													  );
								$tabla.=$this->SetCampo($varCampoBoton)."&nbsp;";
								}
							}
						else {
							$tabla.=$this->SetCampo($varCampoBoton)."&nbsp;";
							}
						}				
					$tabla.="</td>";	
					}
				$varFlagAlto = 0;
				for ($i=0;$i<$varNumCol;$i++) {				
					if(isset($varTabla["conte"][$i]["factor"]))
						$varSum[$i] = $varSum[$i] + floor($row[$i])*$varTabla["conte"][$i]["factor"] + ($row[$i]-floor($row[$i]))*100;
					else
						$varSum[$i] = $varSum[$i] + $row[$i];	
					
					if(!isset($varTabla["conte"][$i]["oculto"]) || $varTabla["conte"][$i]["oculto"]<>"SI") {
						
						$tabla.= "<td class=\"interior\" ";
						if(isset($varTabla["conte"][$i]["fijo"]) || $varTabla["conte"][$i]["fijo"]=="SI") {
							$tabla.= " NOWRAP ";
							}
						$tabla.= ($varFlagAlto==0)?"height=\"22\" ":NULL;
						$varFlagAlto++;
						$tabla.= (isset($varTabla["conte"][$i]["alinear"]))? "align=\"".$varTabla["conte"][$i]["alinear"]."\" ": NULL;
						$tabla.= ">";					
						$varTipoDato = (isset($varTabla["conte"][$i]["tipodat"]))? $varTabla["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
						$varNumDec   = (isset($varTabla["conte"][$i]["decimal"]))? $varTabla["conte"][$i]["decimal"]:NULL;
						if(isset($varTabla["conte"][$i]["campo"]) && count($varTabla["conte"][$i]["campo"])>0) {
							$varId_In 	= (isset($varTabla["conte"][$i]["campo"]["id_in"]))?"[".$row[$varTabla["conte"][$i]["campo"]["id_in"]]."]":NULL;
							$varName_In = (isset($varTabla["conte"][$i]["campo"]["name_in"]))?"[".$row[$varTabla["conte"][$i]["campo"]["name_in"]]."]":NULL;
							$varCampoBoton= array("tipo"	=>$varTabla["conte"][$i]["campo"]["tipo"], 
												  "id"		=>$varTabla["conte"][$i]["campo"]["id"].$varId_In,
												  "name"	=>$varTabla["conte"][$i]["campo"]["name"].$varName_In,
												  "combo"	=>$varTabla["conte"][$i]["campo"]["combo"],											  
												  "locked"	=>$varTabla["conte"][$i]["campo"]["locked"],
												  "clase"	=>$varTabla["conte"][$i]["campo"]["clase"], 
												  "img"		=>$varTabla["conte"][$i]["campo"]["img"], 
												  "checked"	=>$varTabla["conte"][$i]["campo"]["checked"], 
												  "click"	=>$varClick, 
												  "toltip"	=>$varTolTip, 
												  "dclick"	=>$varTabla["conte"][$i]["campo"]["dclick"],
												  "keyup"	=>$varTabla["conte"][$i]["campo"]["keyup"],
												  "keypress"=>$varTabla["conte"][$i]["campo"]["keypress"], 
												  "valor"	=>$row[$i], 
												  "ancho"	=>$varTabla["conte"][$i]["campo"]["ancho"],
												  "alinear"	=>$varTabla["conte"][$i]["campo"]["alinear"]);
							$tabla.= $this->SetCampo($varCampoBoton);
							}
						else {
							$tabla.= $this->setFormatTipoDato($row[$i], $varTipoDato, $varNumDec);
							}
						$tabla.= "</td>";
						}
					}			
				$tabla.= "</tr>";
				}
			$tabla.= "</thead>";
			/*===============================================================================================
			--------------------------------------PIE DE LA TABLA--------------------------------------
			===============================================================================================*/
			$tabla.= "<tfoot>";
			$tabla.= "<tr>";
			if(isset($varTabla["boton"]) && count($varTabla["boton"])>0) {
				$tabla.= "<td class=\"FormTituloSub\" align=\"center\"></td>";				
				}
			for ($i=0;$i<$varNumCol;$i++) {
				if(!isset($varTabla["conte"][$i]["oculto"]) || $varTabla["conte"][$i]["oculto"]<>"SI") {
					$tabla.= "<td class=\"FormTituloSub\" ";
					$tabla.= ($i==0)?"height=\"22\" ":NULL;
					$tabla.= (isset($varTabla["conte"][$i]["alinear"]))? "align=\"".$varTabla["conte"][$i]["alinear"]."\" ": NULL;
					$tabla.= ">";
					if(isset($varTabla["conte"][$i]["autosuma"]) && $varTabla["conte"][$i]["autosuma"]=="SI") {
						$varTipoDato = (isset($varTabla["conte"][$i]["tipodat"]))? $varTabla["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
						$varNumDec   = (isset($varTabla["conte"][$i]["decimal"]))? $varTabla["conte"][$i]["decimal"]:NULL;					
						$tabla.= $this->setFormatTipoDato($varSum[$i], $varTipoDato, $varNumDec);
						}
					elseif(isset($varTabla["conte"][$i]["funcion"]) && count($varTabla["conte"][$i]["funcion"])>0) {
						$varFuncion  = $varTabla["conte"][$i]["funcion"][0];
						if($varFuncion == "autosuma") $varValor1 = $varSum[$i];
						elseif($varFuncion == "contar")	$varValor1 = $varNumFil;				
						else {
							$varValor1 = $varSum[$varTabla["conte"][$i]["funcion"][1]];
							$varValor2 = $varSum[$varTabla["conte"][$i]["funcion"][2]];						
							}
						$varValor  	 = $this->calcularPiePagina($varFuncion, $varValor1, $varValor2);
						$varTipoDato = (isset($varTabla["conte"][$i]["tipodat"]))? $varTabla["conte"][$i]["tipodat"]:$this->Fieldtype($rs, $i);
						$varNumDec   = (isset($varTabla["conte"][$i]["decimal"]))? $varTabla["conte"][$i]["decimal"]:NULL;					
						$tabla.= $this->setFormatTipoDato($varValor, $varTipoDato, $varNumDec);
						}
					$tabla.= "</td>";
					}
				}
			$tabla.= "</tr>";
			$tabla.= "</tfoot>";
			$tabla.= "</table>";		
			}
		return $tabla;
		}	
	}
?>