<?
//Funciones Smarty
function get_fecha_hora_sistema(){
	$val = hoy();
	
	$dia = substr($val, 6, 2);
	$mes = substr($val, 4, 2);
	$año = substr($val, 0, 4);
	$hor = substr($val, 8, 2);
	$min = substr($val, 10, 2);
	
	//var_dump($mes, $año, $hor, $min);
	
	if ($hor){
		$hora = "(".$hor.":".$min.")";
	}
	
	
	echo "$dia/$mes/$año $hora";	
}

function url_secure($params){
	$href 	 = $params["href"];
	$name    = $params["name"];
	$img     = $params["img"];
	$nivel   = $params["nivel"];
	$popup 	 = $params["popup"];
	$class   = $params["class"];
	
	if ($popup)
		$popup = "{popup caption='' text='$popup'}";
		
	if (get_permiso($nivel)){
		if ($name)
			$string =  "<a href='$href' $popup>$name</a>";
		if ($img)
			$string =  "<a href='$href' $popup><img src='$img' border='0' $class></a>";
	}
	
	echo $string;
}



function permiso($params){
	
	$html 		= $params["html"];
	$seguridad  = $params["seguridad"];

	
	if (get_permiso($seguridad)){
		echo $html;
	}
}

function format_rut_sm($param){
	$rut = $param["rut"];
	//debug($rut);
	if (strlen($rut) > 6 ){
		$rut = number_format($rut, 0, ',', '.').substr($rut,-2,2);
		if (strlen($rut) < 12)
			$rut = "0".$rut;
		echo $rut;
	}else{
		echo "- -";
	}
}

function truncate($param){

	$string = $param["string"];
	$size 	= $param["size"];
	
	$string = substr ($string,0,$size);
	
	if ($size <= strlen($string))
		$string .= "...";
	
	echo $string;
}

function titulo_pagina($param){
	$titulo = $param["titulo"];
	$titulo = strtoupper($titulo);
	echo "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"titulo\" >$titulo</td></tr></table>";
}

function textile($param)
{
	$texto = $param["texto"];
	$textile = new Textile();
	$texto = trim($texto);
	$texto = $textile->TextileThis($texto);
	echo ($texto);
} 

function check($params){
	$name 	= $params["name"];
	$label 	= $params["label"];
	$id		= $params["id"];
	
	$name 	= $name . "[]";
	$chk = "<input type='checkbox' name='$name'>$label $name";
	return $chk;
}


function format_fecha_extendida($params){
	$val = $params["fecha"];
	//debug($val);
	$dia = substr($val, 6, 2);
	$mes = substr($val, 4, 2);
	$año = substr($val, 0, 4);
	$hor = substr($val, 8, 2);
	$min = substr($val, 10, 2);
	
	//var_dump($mes, $año, $hor, $min);
	$time = mktime ($hor,$min,0, $mes,$dia,$año);
	$dia_semana    =  date("N", $time);
	
	switch ($dia_semana)
	{
		case "01": $dia_semana = "Lunes";break;
		case "02": $dia_semana = "Martes";break;
		case "03": $dia_semana = "Miércoles";break;
		case "04": $dia_semana = "Jueves";break;
		case "05": $dia_semana = "Viernes";break;
		case "06": $dia_semana = "Sábado";break;
		case "07": $dia_semana = "Domingo";break;		
	}
	if ($hor){
		$hora = "(".$hor.":".$min.")";
	}
	
	switch ($mes)
	{
		case "01": $mes = "Enero";break;
		case "02": $mes = "Febrero";break;
		case "03": $mes = "Marzo";break;
		case "04": $mes = "Abril";break;
		case "05": $mes = "Mayo";break;
		case "06": $mes = "Junio";break;
		case "07": $mes = "Julio";break;
		case "08": $mes = "Agosto";break;
		case "09": $mes = "Septiembre";break;
		case "10": $mes = "Octubre";break;
		case "11": $mes = "Noviembre";break;
		case "12": $mes = "Diciembre";break;
	}
	
	echo "$dia_semana, $dia de $mes de $año";
}

function check_all($params){
	$name 	= $params["name"];
	$name 	= $name . "[]";
	$chk = '<input type="checkbox" name="cambiar_checkbox" onclick="javascript:seleccionar_checkboxs(\''.$name.'\');">' . $name;
	return $chk;	
}


function insertar_tip($params){
	$tip = $params["tip"];
	$id  = $params["id"];
	echo "<div class=\"tips\" id=\"$id\" name=\"tips\">".$tip."&nbsp;<a onclick=\"show_hide('$id',0)\";><img src=\"imagenes/iconos/close.gif\" border=\"0\" align=\"absmiddle\"></a></div>";
}

function expandir_menu($params){

	$items	     = $params["item"];
	$key	     = $params["key"];
	$labels      = $params["labels"];
	$fin		 = $params["fin"];
	$tipo 		 = $params["tipo"];
	
	$extra		 = $params["extra"];


	
	switch ($tipo){
		case "personalizado":
			$encabezado_menu = "encabezado_menu_personalizado";
			$cuerpo_menu	 = "cuerpo_menu_personalizado";
			if ($fin == $key)
				$class_menu = "borde_tabla_menu_personalizado_fin";
			else
			$class_menu = "borde_tabla_menu_personalizado";			
		break;
		default:
			$encabezado_menu = "encabezado_menu";
			if ($fin == $key)
				$class_menu = "borde_tabla_menu_fin";
			else
			$class_menu = "borde_tabla_menu";
		break;
	}
		
		
	$menu_titulo = "
	<table class=\"$class_menu\" cellpadding=\"0\" cellspacing=\"0\" width=\"98%\">
		<tr>
			<td class=\"$encabezado_menu\" width=10%><a onclick=\"cambiar_panel('label_".$key."');set_display_menu('".$key."');\"><img src=\"imagenes/iconos/shown.gif\" border=\"0\"></a></td>
			<td class=\"$encabezado_menu\"> %titulo_menu% </td>
		</tr>
		<tr><td bgcolor=\"White\" colspan=\"2\" width=90%>
			<div id='label_".$key."' style=\"%display%\">
			<table cellpadding=\"2\" cellspacing=\"1\" width=\"100%\">
	";
	
	$menu_item = "
				<tr bgcolor=#ededed onMouseOver=\"style.backgroundColor='#FEDFA2'\" onMouseOut=\"style.backgroundColor='#ededed'\"><td class=\"$cuerpo_menu\"><a href=\"%url_item_menu%\" class=link>%item_menu%</a></td></tr>
	";

	
	$menu_titulo = str_replace("%titulo_menu%", $items["nombre_menu"], $menu_titulo);
	$menu_titulo = str_replace("%display%", $items["display"], $menu_titulo);
	$string_menu .= str_replace("%key%", $key, $menu_titulo);
	
	
	foreach ($items as $key=>$item_menu){
		
		if (($key != "nombre_menu" && $key != "tip" && $key != "display" && $key != "ultimo") || $key == "0"){
			$menu_item2    = str_replace("%url_item_menu%", $item_menu["url"], $menu_item);
			$string_menu .= str_replace("%item_menu%", $item_menu["name"], $menu_item2);
		}
	}
	

	
	echo $string_menu."</table></div></td></tr></table>".$tip;
}




function printer_friendly($param){
	
	$url = $param["url"];
	$url = "javascript:PrintThisPage();";
	
	$tabla = "
	<table align=right border=0>
	<tr>
	<td align=right><img src='imagenes/imprimir.gif'></td>
	<td style=\"font-size:xx-small; \">
	<a href=$url>[ IMPRIMIR ESTA PAGINA ]</a>
	</td>
	</tr>
	</table>
	
	";
	 
	echo $tabla;
}


function mostrar_detalles($param){
	$obj 			= $param["obj"];
	$visibilidad 	= $param["vis"];
	$formato	 	= $param["format"];
	$debug_cont		= checkARRAY($param, "debug", "");
	$titulo			= checkARRAY($param, "titulo", "");
	$tabla = mostrar_detalles_objeto($obj,$visibilidad,$formato,"",$titulo,$debug_cont);
	echo $tabla;
}

function calendario($params){
	
	$name 		= $params["name"];
	$name2		= $params["name2"];
	$form_name 	= $params["form_name"];
	$value		= $params["value"];
	$fecha		= $params["fecha"];
	$tipo		= $params["tipo"];
	$required = $params["required"];
	$label	  = $params["label"];
	$disabled = $params["disabled"];

	if ($disabled) $disabled = "disabled";
	
	if ($required) $required = "required='true' label='$label'";
	
	if ($fecha == "hoy") $fecha = convertirFechaBD_fecha(hoy());
	if ($fecha)
		$fecha		= convertirFechaBD_fecha($fecha);
	
	if (!$form_name) 	$form_name = "form1";
	if (!$tipo) 		$tipo = 0;
	
	switch ($tipo){
		case "0":
			echo "
			<input $required id='$name' name='$name' value='$fecha' size='8'>
			<a href='javascript:void(0)' onclick='if(self.gfPop)gfPop.fPopCalendar(document.$form_name.$name);return false;' HIDEFOCUS>
			<img name='popcal' align='top' src='imagenes/calbtn.gif' width='34' height='22' border='0' alt=''>&nbsp;
			</a>
			";
		break;
		case "D":
			echo "
			<input readonly $required id='$name' name='$name' value='$fecha' size='8'>
			<span id='div_$name' style='display:none'>
			<a href='javascript:void(0)' onclick='if(self.gfPop)gfPop.fPopCalendar(document.$form_name.$name);return false;' HIDEFOCUS>
			<img name='popcal' align='top' src='imagenes/calbtn.gif' width='34' height='22' border='0' alt=''>&nbsp;
			</a>
			</span>
			";			
		break;
		
		case "1":
			echo "
			<input $required name='$name' value='$fecha' size='8' onfocus='this.blur()' readonly>
			<a href='javascript:void(0)' onclick='if(self.gfPop)gfPop.fAntes(document.$form_name.$name);return false;' HIDEFOCUS>
			<img name='popcal' align='top' src='imagenes/calbtn.gif' width='34' height='22' border='0' alt=''>
			</a>
			";			
		break;

		case "2":
			echo "
			<input $required name='$name' value='$fecha' size='8' onfocus='this.blur()' readonly>
			<a href='javascript:void(0)' onclick='if(self.gfPop)gfPop.fDespues(document.$form_name.$name);return false;' HIDEFOCUS>
			<img name='popcal' align='top' src='imagenes/calbtn.gif' width='34' height='22' border='0' alt=''>
			</a>
			";			
		break;
		
		case "3a":
			echo "
			<input $required name='$name' value='$fecha' size='8' onfocus='this.blur()' readonly>
			<a href='javascript:void(0)' onclick='if(self.gfPop)gfPop.fStartPop(document.$form_name.$name,document.$form_name.$name2);return false;' HIDEFOCUS>
			<img name='popcal' align='top' src='imagenes/calbtn.gif' width='34' height='22' border='0' alt=''>
			</a>
			";			
		break;

		case "3b":
			echo "
			<input $required name='$name' value='$fecha' size='8' onfocus='this.blur()' readonly>
			<a href='javascript:void(0)' onclick='if(self.gfPop)gfPop.fEndPop(document.$form_name.$name2,document.$form_name.$name);return false;' HIDEFOCUS>
			<img name='popcal' align='top' src='imagenes/calbtn.gif' width='34' height='22' border='0' alt=''>
			</a>
			";			
		break;
	
	}
	
	
}



function bg_lista($params){
		
		$color	= $params["color"];
		if (!$color) $color = "FEDFA2";
		$r = "onMouseOver=\"style.backgroundColor='#" . $color . "';\" onMouseOut=\"style.backgroundColor='';\"";
		return $r;
}

function combobox($params){
		$arr	= $params["arr"];
		$name	= $params["name"];
		$onchange = $params["onchange"];
		$id 	= $params["id"];
		$first	= $params["first"];
		$linked = $params["linked"];
		$prefix = $params["prefix"];

		if ($prefix == null) $prefix = "";
		
		//Select para el Array
		if ($name == null) $name = "arr";
		
		$pos = strpos($first, "|");
		
		if (!$pos){			
			$first_name = $first;
			$first_id 	= 0;	
		}else{
			$first_name = substr($first,0,$pos);
			$first_id 	= substr($first,$pos+1);	
		}
				
		if ($linked == 1) $onchange = $prefix . "actualizar_arr2();";
		if ($linked == 2) $onchange = $prefix . "actualizar_arr3();";
		
//		debug($first);
//		debug($pos);		
//		debug($first_name);
//		debug($first_id);
//		debug($name);
//		debug($linked);
//		debug($onchange);
		
		echo "<select name=$name onChange=\"$onchange\">";
		
		if ($first){
			echo "<option value=" . $first_id . ">" . $first_name . "</option>";
		}
		
		if ($arr){
			foreach ($arr as $item){
				$selected = "";
				if ($item["id"] == $id){$selected = " selected ";}
				$opt = "<option " . $selected . " value='" . $item["id"] . "'>" . $item["name"] . "</option>";
				echo $opt;
			}	
		}else{
			$from =  $params["from"];
			$to 	=  $params["to"];
			$step 	=  $params["step"];
			
			if ($step == null) $step = 1;
			
			for ($i=$from; $i<=$to; $i=$i+$step){
				$selected = "";
				if ($i == $id){$selected = " selected ";}
				$opt = "<option " . $selected . " value='$i'>$i</option>";
				echo $opt;
			}			
		}

		
		echo "</select>";
}

function linked_combobox_script($params){
		
		$arr1	= $params["arr1"];
		$arr2	= $params["arr2"];
		$arr3	= $params["arr3"];
		
		$name1	= $params["name1"];
		$name2	= $params["name2"];
		$name3	= $params["name3"];
		
		$prefix = $params["prefix"];
		
		$fname = $params["fname"];
		
		
		//debug($arr1);
		//debug($arr2);
		//debug($arr3);
		
		//var_dump($name1, $name2, $name3);
		
		if ($prefix == null) $prefix = "";
		if ($name1 == null) $name1 = "arr1";
		if ($name2 == null) $name2 = "arr2";
		if ($name3 == null) $name3 = "arr3";
		if ($fname == null) $fname = "form1";
		
		
		//var_dump($name1, $name2, $name3);
		//-------------------------------------------------------------------------------------
		//	Creacion de String para el array 1
		//-------------------------------------------------------------------------------------
		
		foreach ($arr1 as $item){
			$str1_id 	.= $item["id"] . "|";
			$str1_name 	.= $item["name"] . "|";
		}
				
		$str1_id 		= substr($str1_id, 0, -1);
		$str1_name 		= substr($str1_name, 0, -1);
		
		
		//-------------------------------------------------------------------------------------
		//	Creacion de String para el array 2
		//-------------------------------------------------------------------------------------
		
		foreach ($arr2 as $item){
			$str2_id 		.= $item["id"] . "|";
			$str2_id_padre 	.= $item["id_padre"] . "|";
			$str2_name 		.= $item["name"] . "|";
		}
				
		$str2_id 		= substr($str2_id, 0, -1);
		$str2_id_padre	= substr($str2_id_padre, 0, -1);
		$str2_name 		= substr($str2_name, 0, -1);
		
		
		
		//-------------------------------------------------------------------------------------
		//	Creacion de String para el array 3
		//-------------------------------------------------------------------------------------
		
		foreach ($arr3 as $item){
			$str3_id 		.= $item["id"] . "|";
			$str3_id_padre 	.= $item["id_padre"] . "|";
			$str3_name 		.= $item["name"] . "|";
		}
				
		$str3_id 		= substr($str3_id, 0, -1);
		$str3_id_padre	= substr($str3_id_padre, 0, -1);
		$str3_name 		= substr($str3_name, 0, -1);
							
		?>
		
		<script language="JavaScript">
		
		
		
		function agregarOption(texto, value, object){
			var defaultSelected = false;
		  	var selected = false;
		  
			var optionName = new Option(texto, value, defaultSelected, selected);
		    var length = object.length;
		    object.options[length] = optionName;
		}
		
					
			
			
			//Variables para el array 1
			var <?echo $prefix; ?>str1_id = "<? echo $str1_id; ?>";
			var <?echo $prefix; ?>arr1_id = <?echo $prefix; ?>str1_id.split("|");
			
			
			//Variables para el array 2
			var <?echo $prefix; ?>str2_id = "<? echo $str2_id; ?>";
			var <?echo $prefix; ?>arr2_id = <?echo $prefix; ?>str2_id.split("|");
			
			var <?echo $prefix; ?>str2_id_padre = "<? echo $str2_id_padre; ?>";
			var <?echo $prefix; ?>arr2_id_padre = <?echo $prefix; ?>str2_id_padre.split("|");
			
			var <?echo $prefix; ?>str2_name = "<? echo $str2_name; ?>";
			var <?echo $prefix; ?>arr2_name = <?echo $prefix; ?>str2_name.split("|");
			
			
			//Variables para el array 3
			var <?echo $prefix; ?>str3_id = "<? echo $str3_id; ?>";
			var <?echo $prefix; ?>arr3_id = <?echo $prefix; ?>str3_id.split("|");
			
			var <?echo $prefix; ?>str3_id_padre = "<? echo $str3_id_padre; ?>";
			var <?echo $prefix; ?>arr3_id_padre = <?echo $prefix; ?>str3_id_padre.split("|");
			
			var <?echo $prefix; ?>str3_name = "<? echo $str3_name; ?>";
			var <?echo $prefix; ?>arr3_name = <?echo $prefix; ?>str3_name.split("|");	

			<?
			
			echo "
			function " . $prefix . "actualizar_arr2(){		";
			
			?>

			
			<?
			echo "
			select_1	= document.$fname." . $name1 . "
			select_2	= document.$fname." . $name2 . "
			
			
		
			";
			
			if ($arr3){
				echo "
				select_3	= document.$fname." . $name3 . "
				select_3.options.length = 0;
				agregarOption(\"Seleccione un Item\", 0, select_3);
				";
			}
			?>

			select_2.options.length = 0;
			agregarOption("Seleccione un Item", 0, select_2);
			
			
		  
			val = select_1.options[select_1.selectedIndex].value;
			
			
			if (val != 0){		
		  		for (i=0; i<<?echo $prefix; ?>arr2_id.length; i++){	
		  			
		  			if (<?echo $prefix; ?>arr2_id_padre[i] == val){
		  				agregarOption( <?echo $prefix; ?>arr2_name[i],  <?echo $prefix; ?>arr2_id[i], select_2);
		  			}			
				}
			}

		};
		
		<?
			if ($arr3){
				echo "
					function " . $prefix . "actualizar_arr3() {
		  			
					select_1	= document.$fname." . $name1 . "
					select_2	= document.$fname." . $name2 . "
					select_3	= document.$fname." . $name3 . "
				
									
					select_3.options.length = 0;
					agregarOption(\"Seleccione un Item\", 0, select_3);
				  
					val = select_2.options[select_2.selectedIndex].value;
					
					if (val != 0){		
						for (i=0; i<".$prefix."arr3_id.length; i++){		  			
				  			if (".$prefix."arr3_id_padre[i] == val){
				  				agregarOption( ".$prefix."arr3_name[i],  ".$prefix."arr3_id[i], select_3);
				  			}			
						}
					}										
				}
				";
			}
	
			echo "</script>";
	}
?>
<?
/**
 * @return void
 * @param $params
 * @desc Despliega en mensaje en pantalla
*/
function msgbox($params){	
	
	$msg	= $params["msg"];
	$link 	= $params["link"];
	$url 	= $params["url"];
	$extra	= $params["extra"];
	
	echo "
	<p><table class=\"tabla_mensaje\" align=\"center\">
	<tr><td align=center>
		$msg 
	</td></tr>
	
	<tr><td align=center>
		<a href='$url' $extra><div style='background-color: #FFCC66;'>$link</div></a>
	</td></tr>
	</table>
	<br>

	";
}

function boton_submit($params){
	$label = $params["label"];
	
	echo '<input type="submit" value="$label" class="boton">';
}

function boton_link($params){
	$url   = $params["url"];
	$label = $params["label"];
	
	echo '<a href="'.$url.'" class="boton">'.$label.'</a>';
}

/**
 * @return void
 * @param $params $url=direccion de retorno
 * @desc Despliega un boton volver en pantalla
*/
function volver($params){	
	
	
	$url 	= checkARRAY($params, "url", "javascript:history.back(1)");
	$extra	= $params["extra"];
	$class	= $params["class"];
	$text	= $params["text"];
	echo "<a href='$url' $extra><img src=\"imagenes/botones/volver.gif\" border=0 class='$class'></a>";
}



function format_numero($params){
	
	
	$number = $params["nro"];	
	$decimals = "0";
	$decpoint= ".";
	$thousandsep=".";	
	
	return number_format($number, $decimals, $decpoint, $thousandsep);
}

function money($params){
	
	$number = $params["nro"];	
	$decimals = "0";
	$decpoint= ".";
	$thousandsep=".";	
	
	return "$" . number_format($number, $decimals, $decpoint, $thousandsep);
}

function formatFecha($params){

	if (substr_count($fecha, "/") != 0){
		return $fecha;
	}

	$val = $params["fecha"];
	//debug($val);
	if ($val == "hoy") {
		$val = hoy();
	}
	$dia = substr($val, 6, 2);
	$mes = substr($val, 4, 2);
	$año = substr($val, 0, 4);
	$hor = substr($val, 8, 2);
	$min = substr($val, 10, 2);
	
	//var_dump($mes, $año, $hor, $min);
	
	if ($hor){
		$hora = "(".$hor.":".$min.")";
	}
	
	
	echo "$dia/$mes/$año $hora";
}

function format_fecha($params){

	$val = $params["fecha"];
	
	if (strlen($val) > 6){
	if (substr_count($val, "/") != 0){
		return $val;
	}


	
	//debug($val);
	if ($val == "hoy") {
		$val = hoy();
	}
	
	$dia = substr($val, 6, 2);
	$mes = substr($val, 4, 2);
	$año = substr($val, 0, 4);
	$hor = substr($val, 8, 2);
	$min = substr($val, 10, 2);
	
	//var_dump($mes, $año, $hor, $min);
	
	if ($hor){
		$hora = "(".$hor.":".$min.")";
	}
	
	
	echo "$dia/$mes/$año $hora";
	}
	else
		echo "-";
}

function rellenar_numero($params){
	$nro 	= $params["nro"];
	$cant 	= $params["cant"];
	$prefix	= $params["prefix"];
	
	if (strlen($nro) > 8) return $nro;
	if (!$cant) $cant = 8;
	
	return $prefix . sprintf("%0" .$cant . "d",$nro);

	
}
//Fin Funciones Smarty
?>
