<?php

/** KumbiaForms - PHP Rapid Development Framework *****************************
*	
* Copyright (C) 2005-2007 Andrés Felipe Gutiérrez (andresfelipe at vagoogle.net)
* 	
* This framework is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* 
* This framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this framework; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
* 
* Este framework es software libre; puedes redistribuirlo y/o modificarlo
* bajo los terminos de la licencia pública general GNU tal y como fue publicada
* por la Fundación del Software Libre; desde la versión 2.1 o cualquier
* versión superior.
* 
* Este framework es distribuido con la esperanza de ser util pero SIN NINGUN 
* TIPO DE GARANTIA; dejando atrás su LADO MERCANTIL o PARA FAVORECER ALGUN
* FIN EN PARTICULAR. Lee la licencia publica general para más detalles.
* 
* Debes recibir una copia de la Licencia Pública General GNU junto con este
* framework, si no es asi, escribe a Fundación del Software Libre Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
*****************************************************************************/

function get_params($params){
	$data = array();
	$i = 0;
	foreach ($params as $p) {
		if(ereg("([a-z_0-9]+[:][ ]).+", $p, $regs)){
			$p = str_replace($regs[1], "", $p);
			$n = str_replace(": ", "", $regs[1]);
			$data[$n] = $p;
		} else $data[$i] = $p;
		$i++;
	}
	return $data;
}

function is_param($param){
	return ereg("([a-z_0-9]+[:][ ]).+", $param);
}

function get_param($param){
	$data = array();
	if(ereg("([a-z_0-9]+[:][ ]).+", $p, $regs)){
		$p = str_replace($regs[1], "", $p);
		$n = str_replace(": ", "", $regs[1]);
		$data[$n] = $p;
		return array();
	} else return array($param);
}

function link_to($action, $text=''){
	if(func_num_args()>2){
		$action = get_params(func_get_args());
	}
	if(is_array($action)){
		if($action['confirm']){
			$action['onclick'] = "if(!confirm(\"{$action['confirm']}\")) if(document.all) event.returnValue = false; else event.preventDefault(); ".$action['onclick'];
			unset($action['confirm']);
		}
		$code = "<a href='".KUMBIA_PATH."{$action[0]}' ";
		if(!$action['text']){
			$action['text'] = $action[1];
		}
		foreach($action as $key => $value){
			if(!is_numeric($key)&&$key!='text'){
				$code.=" $key='$value' ";
			}
		}
		$code.=">{$action['text']}</a>";
		return $code;
	} else {
		if(!$text) {
			$text = str_replace('_', ' ', $action);
			$text = str_replace('/', ' ', $text);
			$text = ucwords($text);
		}
		return "<a href='".KUMBIA_PATH."$action'>$text</a>";
	}
}

function link_to_action($action, $text=''){
	if(func_num_args()>2){
		$action = get_params(func_get_args());
	}
	if(is_array($action)){
		if($action['confirm']){
			$action['onclick'] = "if(!confirm(\"{$action['confirm']}\")) if(document.all) event.returnValue = false; else event.preventDefault(); ".$action['onclick'];
			unset($action['confirm']);
		}
		$code = "<a href='".KUMBIA_PATH."{$_REQUEST['action']}/{$action[0]}' ";
		foreach($action as $key => $value){
			if(!is_numeric($key)){
				$code.=" $key='$value' ";
			}
		}
		$code.=">{$action[1]}</a>";
		return $code;
	} else {
		if(!$text) {
			$text = str_replace('_', ' ', $action);
			$text = str_replace('/', ' ', $text);
			$text = ucwords($text);
		}
		return "<a href='".KUMBIA_PATH."{$_REQUEST['action']}/$action'>$text</a>";
	}
}

function link_to_remote(){
	$data = get_params(func_get_args());
	if(!$data['update']){
		$update = $data[2];
	} else {
		$update = $data['update'];
	}
	if(!$data['text']){
		$text = $data[1];
	} else {
		$text = $data['text'];
	}
	if(!$text){
		$text = $data[0];
	}
	if(!$data['action']){
		$action = $data[0];
	} else {
		$action = $data['action'];
	}
	if($data['loading']&&!$data['before']) $data['before'] = $data['loading'];
	$code = "<a href='#' onclick='new AJAX.viewRequest({action:\"$action\", container:\"$update\", callbacks: { before: function(){{$data['before']}}, complete: function(){{$data['complete']}}}, success: function(){{$data['success']}}} }); return false;'";
	unset($data['before']);
	unset($data['complete']);
	unset($data['success']);
	unset($data['loading']);
	unset($data['update']);
	foreach($data as $key => $value){
		if(!is_numeric($key)){
			$code.=" $key='$value' ";
		}
	}
	return $code.">$text</a>";
}

function javascript_include_tag($src='', $cache=true){
	if(!$src) $src = $_REQUEST['controller'];
	$src.=".js";
	if(!$cache) {
		$cache = md5(uniqid());
		$src.="?nocache=".$cache;
	}
	print "<script type='text/javascript' src='".KUMBIA_PATH."javascript/$src'></script>\r\n";
}

function javascript_library_tag($src){
	print "<script type='text/javascript' src='".KUMBIA_PATH."javascript/kumbia/$src.js'></script>\r\n";
}

function stylesheet_link($src=''){
	if(!$src) $src = $_REQUEST['action'];
	print "<link rel='stylesheet' type='text/css' href='".KUMBIA_PATH."css/$src.css'/>\r\n";
}

function stylesheet_link_tag($src=''){
	if(func_num_args()>1){
		$atts = get_params(func_get_args());
	}
	if(is_array($atts)){
		foreach($atts as $k => $v){
			stylesheet_link($v);
		}
	} else {
		if(!$src) $src = $_REQUEST['action'];
		stylesheet_link($src);
	}
}

function img_tag($img){
	$atts = get_params(func_get_args());
	if(!$atts['src']){
		$code.="<img src='".KUMBIA_PATH."img/{$atts[0]}' ";
	} else {
		$code.="<img src='{$atts['src']}' ";
	}
	unset($atts['src']);
	if(!$atts['alt']) $atts['alt'] = "";
	if($atts['drag']) {
		$drag=true;
		unset($atts['drag']);
	}
	if($atts['reflect']) {
		$reflect=true;
		unset($atts['reflect']);
	}
	if(is_array($atts)){
		if(!$atts['alt']) $atts['alt'] = "";
		foreach($atts as $at => $val){
			if(!is_numeric($at)){
				$code.="$at=\"".$val."\" ";
			}
		}
	}
	$code.= "/>\r\n";
	if($drag){
		$code.="<script type=\"text/javascript\">new Draggable('{$atts['id']}', {revert:true})</script>\r\n";
	}
	if($reflect){
		$code.="<script type=\"text/javascript\">new Reflector.reflect('{$atts['id']}')</script>\r\n";
	}
	return $code;
}

function form_remote_tag($data){
	$data = get_params(func_get_args());
	$GLOBALS['md5remote'] = md5(uniqid());
	if(!$data['action']) {
		$data['action'] = $data[0];
	}
	$data['callbacks']	= array();
	if($data['complete']){
		$data['callbacks'][] = " complete: function(){ ".$data['complete']." }";
	}
	if($data['before']){
		$data['callbacks'][] = " before: function(){ ".$data['before']." }";
	}
	if($data['success']){
		$data['callbacks'][] = " success: function(){ ".$data['success']." }";
	}
	$GLOBALS['formRemoteObjs'] = array();
	return "<form action='' onsubmit='ajaxRemoteForm(\"{$data['action']}\", \"{$data['update']}\", { ".join(",", $data['callbacks'])." }, \"{$GLOBALS['md5remote']}\"); if(document.all) event.returnValue = false; else { event.preventDefault(); event.stopPropagation()  }'>\r\n";
}


function form_tag($action){
	if(func_num_args()>1){
		$atts = get_params(func_get_args());
	}
	$GLOBALS['md5remote'] = md5(uniqid());
	if(!$action) $action = $atts[0] ? $atts[0] : $atts['action'];
	if(!$atts['method']) $atts['method'] = "post";
	$GLOBALS['formRemoteObjs'] = array();
	print "<form action='".KUMBIA_PATH."$action/{$_REQUEST['id']}' ";
	foreach($atts as $key => $value){
		if(!is_numeric($key)){
			print "$key = '$value' ";
		}
	}
	print ">\r\n";
}

function comillas($word){
	return "'$word'";
}

function end_form_tag(){
	$str = "<script type='text/javascript'>var FormObjects{$GLOBALS['md5remote']} = new Array(".join($GLOBALS['formRemoteObjs'], ",").")</script>\r\n</form>\r\n";
	unset($GLOBALS['formRemoteObjs']);
	return $str;
}

/**
 * Crea un boton de submit para el formulario actual
 *
 * @param string $caption
 * @return html code
 */
function submit_tag($caption){
	$data = get_params(func_get_args());
	if(!$data['caption']) {
		$data['caption'] = $data[0];
	}
	$code = "<input type='submit' value='{$data['caption']}' ";
	foreach($data as $key => $value){
		if(!is_numeric($key)){
			$code.="$key='$value' ";
		}
	}
	$code.=">\r\n";
	return $code;
}

function button_tag(){
	$data = get_params(func_get_args());
	if(!isset($data['value'])) $data['value'] = $data[0];
	if($data['id']&&!$data['name']) $data['name'] = $data['id'];
	if(!isset($data['id'])) $data['id'] = $data['name'];
	$code = "<input type='button' ";
	foreach($data as $key => $value){
		if(!is_numeric($key)&&$key!=$data){
			$code.="$key=\"$value\" ";
		}
	}
	return $code.">\r\n";
}

function get_value_from_action($name){
	if(!is_array($name)){
		if(!$name) return;
		$action = ucwords($_REQUEST['action']);
		$model = kumbia::$models[$action];
		if($model){
			$x = $name;
			$value = $model->$x;
		}
	} else {
		if($name['value']) return $name['value'];
		$action = ucwords($_REQUEST['action']);
		$model = kumbia::$models[$action];		
		if($model){
			$x = $name['name'] ? $name['name'] : $name[0];
			$value = $model->$x;
		}
	}
	return $value ? $value : $_REQUEST[$x];
}

# Helpers
function text_field_tag($name){
	$value = get_value_from_action($name);
	$name = get_params(func_get_args());
	if(!$name[0]) $name[0] = $name['id'];
	if(!$name['name']) $name['name'] = $name[0];
	if(!$value) $value = $name['value'];
	$code.="<input type='text' id='{$name[0]}' value='$value' ";
	foreach($name as $key => $value){
		if(!is_numeric($key)){
			$code.="$key='$value' ";
		}
	}
	$code.=">\r\n";
	if(isset($GLOBALS['formRemoteObjs'])) {
		if($name['name']){
			$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
		}
	}
	return $code;
}

function checkbox_field_tag($name){
	$value = get_value_from_action($name);
	$name = get_params(func_get_args());
	if(!$name[0]) $name[0] = $name['id'];
	if(!$name['name']) $name['name'] = $name[0];
	if(!$value) $value = $name['value'];
	$code.="<input type='checkbox' id='{$name[0]}' value='$value' ";
	foreach($name as $key => $value){
		if(!is_numeric($key)){
			$code.="$key='$value' ";
		}
	}
	$code.=">\r\n";
	if(isset($GLOBALS['formRemoteObjs'])) {
		if($name['name']){
			$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
		}
	}
	return $code;
}

function numeric_field_tag($name){
	$value = get_value_from_action($name);
	$name = get_params(func_get_args());
	$GLOBALS['formRemoteObjs'][] = "'{$name[0]}'";
	if(!$name[0]) $name[0] = $name['id'];
	if(!$name['name']) $name['name'] = $name[0];
	if(!$value) $value = $name['value'];
	if(!isset($name['onkeydown'])) {
		$name['onkeydown'] = "valNumeric(event)";
	} else {
		$name['onkeydown'].=";valNumeric(event)";
	}
	$code.="<input type='text' id='{$name[0]}' value='$value' ";
	foreach($name as $key => $value){
		if(!is_numeric($key)){
			$code.="$key='$value' ";
		}
	}
	$code.=">\r\n";
	if(isset($GLOBALS['formRemoteObjs'])) {
		if($name['name']){
			$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
		}
	}
	return $code;
}

function textupper_field_tag($name){

	$value = get_value_from_action($name);
	$name = get_params(func_get_args());
	$GLOBALS['formRemoteObjs'][] = "'{$name[0]}'";
	if(!$name[0]) $name[0] = $name['id'];
	if(!$name['name']) $name['name'] = $name[0];
	if(!$value) $value = $name['value'];
	if(!isset($name['onblur'])) {
		$name['onblur'] = "keyUpper2(this)";
	} else {
		$name['onblur'].=";keyUpper2(this)";
	}
	$code.="<input type='text' id='{$name[0]}' value='$value' ";
	foreach($name as $key => $value){
		if(!is_numeric($key)){
			$code.="$key='$value' ";
		}
	}
	$code.=">\r\n";
	if(isset($GLOBALS['formRemoteObjs'])) {
		if($name['name']){
			$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
		}
	}
	return $code;
}

function date_field_tag($name){
	$config = Config::read('core.ini');
	$value = get_value_from_action($name);
	$name = get_params(func_get_args());
	$GLOBALS['formRemoteObjs'][] = "'{$name[0]}'";
	if(!$name[0]) $name[0] = $name['id'];
	if(!$name['name']) $name['name'] = $name[0];
	if(!$value) $value = $name['value'];

	if($value){
		$ano = substr($value, 0, 4);
		$mes = substr($value, 5, 2);
		$dia = substr($value, 8, 2);
	} else {
		$ano = 0;
		$mes = 0;
		$dia = 0;
	}
	print "<table><tr><td>";

	$meses = array(
	"01" => "Ene",
	"02" => "Feb",
	"03" => "Mar",
	"04" => "Abr",
	"05" => "May",
	"06" => "Jun",
	"07" => "Jul",
	"08" => "Ago",
	"09" => "Sep",
	"10" => "Oct",
	"11" => "Nov",
	"12" => "Dic",
	);
	print "<select name='{$name[0]}_month' id='{$name[0]}_month'
	onchange=\"$('{$name[0]}').value = $('{$name[0]}_year').options[$('{$name[0]}_year').selectedIndex].value+'-'+$('{$name[0]}_month').options[$('{$name[0]}_month').selectedIndex].value+'-'+$('{$name[0]}_day').options[$('{$name[0]}_day').selectedIndex].value\"
	>";
	foreach($meses as $numero_mes => $nombre_mes){
		if($numero_mes==$mes){
			print "<option value='$numero_mes' selected>$nombre_mes\n";
		} else {
			print "<option value='$numero_mes'>$nombre_mes\n";
		}
	}
	print "</select></td><td>";

	print "<select name='{$name[0]}_day' id='{$name[0]}_day'
	onchange=\"$('{$name[0]}').value = $('{$name[0]}_year').options[$('{$name[0]}_year').selectedIndex].value+'-'+$('{$name[0]}_month').options[$('{$name[0]}_month').selectedIndex].value+'-'+$('{$name[0]}_day').options[$('{$name[0]}_day').selectedIndex].value\">";
	for($i=1;$i<=31;$i++){
		$n = sprintf("%02s", $i);
		if($n==$dia){
			print "<option value='$n' selected>$n\n";
		} else {
			print "<option value='$n'>$n\n";
		}
	}
	print "</select></td><td>";

	print "<select name='{$name[0]}_year' id='{$name[0]}_year'
	onchange=\"$('{$name[0]}').value = $('{$name[0]}_year').options[$('{$name[0]}_year').selectedIndex].value+'-'+$('{$name[0]}_month').options[$('{$name[0]}_month').selectedIndex].value+'-'+$('{$name[0]}_day').options[$('{$name[0]}_day').selectedIndex].value\"
	>";
	for($i=date("Y");$i>=1900;$i--){
		if($i==$ano){
			print "<option value='$i' selected>$i\n";
		} else {
			print "<option value='$i'>$i\n";
		}
	}
	print "</select></td><td>";
	print "</table>";

	print "<input type='hidden' id='{$name[0]}' name='{$name[0]}' value='$value'>";

	if(isset($GLOBALS['formRemoteObjs'])) {
		if($name['name']){
			$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
		}
	}
	return $code;
}



function file_field_tag($name){
	$value = get_value_from_action($name);
	$name = get_params(func_get_args());
	if(!$name[0]) $name[0] = $name['id'];
	if(!$name['name']) $name['name'] = $name[0];
	$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
	$code.="<input type='file' id='{$name[0]}' ";
	foreach($name as $key => $value){
		if(!is_numeric($key)){
			$code.="$key='$value' ";
		}
	}
	$code.=">\r\n";
	return $code;
}

function radio_field_tag($name){
	$value = get_value_from_action($name[0]);
	$name = get_params(func_get_args());
	if(!$name[0]) $name[0] = $name['id'];
	if(!$name['name']) $name['name'] = $name[0];
	if(!$value) $value = $name['value'];
	$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
	print "<table><tr>";
	foreach($name[1] as $key=>$text){
		if($value==$key){
			print "<td><input type='radio' name='{$name[0]}' id='{$name[0]}' value='$key' checked></td><td>$text</td>\r\n";
		} else {
			print "<td><input type='radio' name='{$name[0]}' id='{$name[0]}' value='$key'></td><td>$text</td>\r\n";
		}
	}
	print "</tr></table>";

	return $code;
}

function textarea_tag($configuration){
	if(func_num_args()==1){
		$value = get_value_from_action($configuration);
	} else{
		$value = get_value_from_action(get_params(func_get_args()));
	}
	if(func_num_args()==1){
		$configuration = func_get_args();
		if(isset($GLOBALS['formRemoteObjs'])) $GLOBALS['formRemoteObjs'][] = "'{$configuration[0]}'";
		return "<textarea id='{$configuration[0]}' name='{$configuration[0]}' cols=40 rows=25>$value</textarea>\r\n";
	} else {
		$configuration = get_params(func_get_args());
		if(!$configuration['name']) $configuration['name'] = $configuration[0];
		if(isset($GLOBALS['formRemoteObjs'])) $GLOBALS['formRemoteObjs'][] = "'{$configuration['name']}'";
		if(!$configuration['cols']) $configuration['cols'] = 40;
		if(!$configuration['rows']) $configuration['rows'] = 25;
		if($value===null) $value = $configuration['value'];
		return "<textarea id='{$configuration['name']}' name='{$configuration['name']}' cols={$configuration['cols']} rows={$configuration['rows']}>$value</textarea>\r\n";
	}
}

function password_field_tag($name){
	$value = get_value_from_action($name);
	if(func_num_args()>1){
		$name = get_params(func_get_args());
	}
	if(!is_array($name)){
		$GLOBALS['formRemoteObjs'][] = "'$name'";
		return "<input type='password' id='$name' name='$name' value='$value'>\r\n";
	} else {
		if(!$name[0]) $name[0] = $name['id'];
		if(!$name['name']) $name['name'] = $name[0];
		$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
		$code.="<input type='password' id='{$name[0]}'";
		foreach($name as $key => $value){
			if(!is_numeric($key)){
				$code.="$key='$value' ";
			}
		}
		$code.=">\r\n";
		return $code;
	}
}

function hidden_field_tag($name){
	$value = get_value_from_action($name);
	if(func_num_args()>1){
		$name = get_params(func_get_args());
	}
	if(!is_array($name)){
		$GLOBALS['formRemoteObjs'][] = "'$name'";
		return "<input type='hidden' id='$name' name='$name' value='$value'>\r\n";
	} else {
		$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
		if(!$name[0]) $name[0] = $name['id'];
		if(!$name['name']) $name['name'] = $name[0];
		$code.="<input type='hidden' id='{$name[0]}'";
		foreach($name as $key => $value){
			if(!is_numeric($key)){
				$code.="$key='$value' ";
			}
		}
		$code.=">\r\n";
		return $code;
	}
}

function select_tag($name='', $data=''){
	if(func_num_args()>1){
		$opts = get_params(func_get_args());
	}
	if(is_array($opts)){
		$code.="<select id='{$opts[0]}' name='{$opts[0]}' ";
		if(is_array($opts)){
			foreach($opts as $at => $val){
				if(!is_numeric($at)){
					$code.="$at = '".$val."' ";
				}
			}
		}
		$code.=">\r\n";
		if(is_array($opts[1])){
			if(is_object($opts[1][0])){

			} else {
				foreach($opts[1] as $d){
					$code.="\t<option value='{$d[0]}'>{$d[1]}</option>\r\n";
				}
			}
		}
		$code.= "</select>\r\n";
	} else {
		$code.="<select id='$opts' name='$opts'></select>";
	}
	print $code;
}

function option_tag($value, $text){
	if(func_num_args()>1){
		$opts = get_params(func_get_args());
		$value = $opts[0];
		$text = $opts[1];
	}
	$code.="<option value='$value' ";
	if(is_array($opts)){
		foreach($opts as $at => $val){
			if(!is_numeric($at)){
				$code.="$at = '".$val."' ";
			}
		}
	}
	$code.= " >$text</option>\r\n";
	print $code;
}

function upload_image_tag(){
	$opts = get_params(func_get_args());
	if(!$opts['name']){
		$opts['name'] = $opts[0];
	}
	$code.="<span id='{$opts['name']}_span_pre'>
	<select name='{$opts[0]}' id='{$opts[0]}' onchange='show_upload_image(this)'>";
	$code.="<option value='@'>Seleccione...\n";
	foreach(scandir("public/img/upload") as $file){
		if($file!='index.html'&&$file!='.'&&$file!='..'&&$file!='Thumbs.db'&&$file!='desktop.ini'){
			$nfile = str_replace('.gif', '', $file);
			$nfile = str_replace('.jpg', '', $nfile);
			$nfile = str_replace('.png', '', $nfile);
			$nfile = str_replace('.bmp', '', $nfile);
			$nfile = str_replace('_', ' ', $nfile);
			$nfile = ucfirst($nfile);
			if(urlencode("upload/$file")==$opts['value']){
				$code.="<option selected value='upload/$file' style='background: #EAEAEA'>$nfile</option>\n";
			} else {
				$code.="<option value='upload/$file'>$nfile</option>\n";
			}
		}
	}
	$code.="</select> <a href='#{$opts['name']}_up' name='{$opts['name']}_up' id='{$opts['name']}_up' onclick='enable_upload_file(\"{$opts['name']}\")'>Subir Imagen</a></span>
	<span style='display:none' id='{$opts['name']}_span'>
	<input type='file' id='{$opts['name']}_file' onchange='upload_file(\"{$opts['name']}\")'> 
	<a href='#{$opts['name']}_can' name='{$opts['name']}_can' id='{$opts['name']}_can' style='color:red' onclick='cancel_upload_file(\"{$opts['name']}\")'>Cancelar</a></span>
	";
	if(!$opts['width']) $opts['width'] = 128;
	if($opts['value']){
		$opts['style']="border: 1px solid black;margin: 5px;".$opts['value'];
	} else {
		$opts['style']="border: 1px solid black;display:none;margin: 5px;".$opts['value'];
	}
	$code.="<div>".img_tag(urldecode($opts['value']), 'width: '.$opts['width'], 'style: '.$opts['style'], 'id: '.$opts['name']."_im")."</div>";
	return $code;
}

class table_tag {

	var $row_colors;
	var $table_headers;
	var $tableHtmlAttributes;
	var $cellHtmlAttributes;
	var $rows;

	function table_tag ($opts = ''){
		$this->tableHtmlAttributes["cellspacing"] = $cellspacing;
		$this->tableHtmlAttributes["cellpadding"] = $cellpadding;
		if(is_array($opts)){
			if(is_array($opts['table_attributes'])){
				$this->tableHtmlAttributes = array_merge($this->tableHtmlAttributes, $opts['table_attributes']);
			}
			if(is_array($opts['row_colors'])){
				$this->row_colors = $opts['row_colors'];
			}
			if(is_array($opts['table_headers'])){
				$this->table_headers = $opts['table_headers'];
			}
			if(is_array($opts['styles'])){
				$this->styles = $opts['styles'];
			}
			if($opts['style_default']){
				$this->style_default = $opts['style_default'];
			}
		}

	}

	function add_row($cells){
		if(is_array($cells))
		$this->rows[] = $cells;
	}

	function add_rows($cells){
		if(is_array($cells))
		$this->rows = array_merge($this->rows, $cells);
	}

	function render(){
		$str = "<table ";
		if(count($this->tableHtmlAttributes)){
			foreach($this->tableHtmlAttributes as $keyName => $value){
				$str.=" $keyName=\"$value\"";
			}
		}
		$str.=">\r\n";

		if(count($this->table_headers)){
			$str.="<thead>\r\n\t<tr>\r\n";
			foreach($this->table_headers as $th){
				$str.="\t\t<th>$th</th>\r\n";
			}
			$str.="\t</tr>\r\n</thead>\r\n";
		}
		$c = 0;

		$str.="<tbody>\r\n";
		if(count($this->rows)){

			foreach($this->rows as $row){
				$str.="\t<tr bgcolor='{$this->row_colors[$c]}'>\r\n";
				$n = 0;
				foreach($row as $field){
					if(!$this->styles[$n]) $this->styles[$n] = $this->style_default;
					$str.="\t\t<td class='{$this->styles[$n]}'>$field</td>\r\n";
					$n++;
				}
				$str.="\t</tr>\r\n";
				if($c==count($this->row_colors)-1) $c = 0; else $c++;
			}
		}
		$str.="</tbody>\r\n</table>\r\n";
		print $str;
		return $str;
	}

	function add_table_att($att, $value=''){
		if(is_array($att)){
			$this->tableHtmlAttributes = array_merge($this->tableHtmlAttributes, $att);
		} else
		$this->tableHtmlAttributes[$att] = $value;
	}

	function add_cell_att(){
	}

	function set_data($data, $column_order = ''){
		if(is_array($data)){
			if(is_object($data[0])){
				foreach($data as $ar){
					$d = array();
					if(is_array($column_order)){
						foreach($column_order as $f){
							$d[]=$ar->$f;
						}
					} else {
						foreach($ar->fields as $f){
							$d[]=$ar->$f;
						}
					}
					$this->add_row($d);
				}
			} else {
				foreach($data as $ar){
					$d = array();
					if(is_array($column_order)){
						foreach($column_order as $f){
							$d[]=$ar["$f"];
						}
					} else {
						$d = array_merge($d, $ar);
					}
					$this->add_row($d);
				}
			}
		}
	}

}

function set_droppable($obj, $action=''){
	$opts = get_params(func_get_args());
	if(!$opts['name']){
		$opts['name'] = $opts[0];
	}
	print "<script type=\"text/javascript\">Droppables.add('{$opts['name']}', {hoverclass: '{$opts['hover_class']}',onDrop:{$opts['action']}})</script>";
}

function redirect_to($action, $seconds = 0.01){
	$seconds*=1000;
	print "<script type=\"text/javascript\">setTimeout('window.location=\"?/$action\"', $seconds)</script>";
}

function render_partial($partial_view, $value=''){
	if(file_exists("views/{$_REQUEST['controller']}/_$partial_view.phtml")){
		if(is_array(Kumbia::$models)){
			foreach(Kumbia::$models as $model_name => $model){
				$$model_name = $model;
			}
		}
		if(is_subclass_of(kumbia::$controller, "ApplicationController")){
			foreach(kumbia::$controller as $var => $value) {
				$$var = $value;
			}
		}
		$$partial_view = $value;
		include "views/{$_REQUEST['controller']}/_$partial_view.phtml";
	} else {
		Flash::kumbia_error('No existe la Vista<br>
			  <span style="font-size:16px">Kumbia no puede encontrar la vista "'.$partial_view.'"
			  </span>');
	}
}

function tab_tag($tabs, $color='green', $width=800){

	switch($color){
		case 'blue':
		$col1 = '#D9ECFF'; $col2 = '#E8F8FF'; $col3 = '#000080';
		break;

		case 'pink':
		$col1 = '#FFE6F2'; $col2 = '#FFCCE4'; $col3 = '#FE1B59';
		break;

		case 'orange':
		$col1 = '#FCE6BC'; $col2 = '#FDF1DB'; $col3 = '#DE950C';
		break;

		case 'green':
		$col2 = '#EAFFD7'; $col1 = '#DAFFB9'; $col3 = '#008000';
		break;
	}


	print "
			<table cellspacing=0 cellpadding=0 width=$width>
			<tr>";	    	 
	$p = 1;
	$w = $width;
	foreach($tabs as $tab){
		if($p==1) $color = $col1;
		else $color = $col2;
		$ww = (int) ($width * 0.22);
		$www = (int) ($width * 0.21);
		print "<td align='center'
				  width=$ww style='padding-top:5px;padding-left:5px;padding-right:5px;padding-bottom:-5px'>
				  <span style='width:$www"."px;border-top:1px solid $col3;border-left:1px solid $col3;border-right:1px solid $col3;background:$color;padding:2px;color:$col3;cursor:pointer' id='spanm_$p'
				  onclick='showTab($p, this)'
				  >".$tab['caption']."</span></td>";
		$p++;
		$w-=$ww;
	}
	print "
			<script>
				function showTab(p, obj){
				  	for(i=1;i<=$p-1;i++){
					    $('tab_'+i).hide();					    
					    $('spanm_'+i).style.background = '$col2';
					}
					$('tab_'+p).show();
					obj.style.background = '$col1'
				}
			</script>
			";
	$p = $p + 1;
	//$w = $width/2;
	print "<td width=$w></td><tr>";
	print "<td colspan=$p style='border:1px solid $col3;background:$col1;padding:10px'>";
	$p = 1;
	foreach($tabs as $tab){
		if($p!=1){
			print "<div id='tab_$p' style='display:none'>";
		} else {
			print "<div id='tab_$p'>";
		}
		render_partial($tab['partial']);
		print "</div>";
		$p++;
	}
	print "<br></td><td width=30></td>";
	print "</table>";
}

function tr_break($x=''){
	static $l;
	if($x=='') {
		$l = 0;
		return;
	}
	if(!$l) $l = 1;
	else $l++;
	if(($l%$x)==0) {
		print "</tr><tr>";
	}
}

function br_break($x=''){
	static $l;
	if($x=='') {
		$l = 0;
		return;
	}
	if(!$l) $l = 1;
	else $l++;
	if(($l%$x)==0) {
		print "<br/>\n";
	}
}

function tr_color($colors){
	static $i;
	if(func_num_args()>1){
		$colors = get_params(func_get_args());
	}
	if(!$i) {
		$i = 1;
	}
	print "<tr bgcolor=\"{$colors[$i-1]}\">";
	if(count($colors)==$i) {
		$i = 1;
	} else {
		$i++;
	}
}

/**
 * Crea un Button que al hacer click carga
 * un controlador y una acción determinada
 *
 * @param string $caption
 * @param string $action
 * @param string $classCSS
 * @return HTML del Botón
 */
function button_to_action($caption, $action, $classCSS=''){
	return "<button class='$classCSS' onclick='window.location=\"".KUMBIA_PATH."$action\"'>$caption</button>";
}

/**
 * Crea un Button que al hacer click carga
 * con AJAX un controlador y una acción determinada
 *
 * @param string $caption
 * @param string $action
 * @param string $classCSS
 * @return HTML del Botón
 */
function button_to_remote_action($caption, $action, $classCSS=''){
	$opts = get_params(func_get_args());
	if(func_num_args()==2){
		$opts['action'] = $opts[1];
		$opts['caption'] = $opts[0];
	} else {
		if(!$opts['action']) $opts['action'] = $opts[1];
		if(!$opts['caption']) $opts['caption'] = $opts[0];
	}
	$code = "<button onclick='AJAX.execute({action:\"{$opts['action']}\", container:\"{$opts['update']}\", callbacks: { success: function(){{$opts['success']}}, before: function(){{$opts['before']}} } })'";
	unset($opts['action']);
	unset($opts['success']);
	unset($opts['before']);
	unset($opts['complete']);
	foreach($opts as $k => $v){
		if(!is_numeric($k)&&$k!='caption'){
			$code.=" $k='$v' ";
		}
	}
	$code.=">{$opts['caption']}</button>";
	return $code;
}

/**
 * Crea un select multiple que actualiza un container 
 * usando una accion ajax que cambia dependiendo del id
 * selecionado en el select
 * @param string $id
 * @return code
 */
function updater_select($id){
	$opts = get_params(func_get_args());
	if(func_num_args()==1){
		$opts['id'] = $id;
	}
	if(!$opts['id']) $opts['id'] = $opts[0];
	if(!$opts['container']) $opts['container'] = $opts['update'];
	$code = "
	<select multiple onchange='AJAX.viewRequest({
		action: \"{$opts['action']}/\"+selectedItem($(\"{$opts['id']}\")).value,
		container: \"{$opts['container']}\"	
	})' ";
	unset($opts['container']);
	unset($opts['update']);
	unset($opts['action']);
	foreach($opts as $k => $v){
		if(!is_numeric($k)){
			$code.=" $k='$v' ";
		}
	}
	$code.=">\n";
	return $code;
}

function text_field_with_autocomplete(){
	$value = get_value_from_action($name);
	$name = get_params(func_get_args());
	$hash = md5(uniqid());
	if(!$name['name']) $name['name'] = $name[0];
	if(!$name['after_update']) $name['after_update'] = "function(){}";
	if(!$name['id']) $name['id'] = $name['name'] ? $name['name'] : $name[0];
	if(!$name['message']) $name['message'] = "Consultando..";
	$GLOBALS['formRemoteObjs'][] = "'{$name['name']}'";
	$code = "<input type='text' id='{$name[0]}' name='{$name['name']}'";
	foreach($name as $key => $value){
		if(!is_numeric($key)&&$key!="action"&&$key!="after_update"){
			$code.="$key='$value' ";
		}
	}
	$code.= "/>
	<span id='indicator$hash' style='display: none'><img src='".KUMBIA_PATH."img/spinner.gif' alt='{$name['message']}'/></span>
	<div id='{$name[0]}_choices' class='autocomplete'></div>
	<script type='text/javascript'>
	// <![CDATA[		
	new Ajax.Autocompleter(\"{$name[0]}\", \"{$name[0]}_choices\", \$Kumbia.path+\"{$name['action']}\", { minChars: 2, indicator: 'indicator$hash', afterUpdateElement : {$name['after_update']}});
	// ]]>
	</script>
	";
	return $code;
}


#Other functions
function truncate($word, $number=0){
	if($number){
		return substr($word, 0, $number);
	} else {
		return rtrim($word);
	}
}

function highlight($sentence, $what){
	return str_replace($what, '<strong class="highlight">'.$what.'</strong>', $sentence);
}

function money($number){
	$number = my_round($number);
	return "$ ".number_format($number, 2, ",", ".");
}

function roundnumber($n, $d = 0) {
	$n = $n - 0;
	if ($d === NULL) $d = 2;

	$f = pow(10, $d);
	$n += pow(10, - ($d + 1));
	$n = round($n * $f) / $f;
	$n += pow(10, - ($d + 1));
	$n += '';

	if ( $d == 0 ):
	return substr($n, 0, strpos($n, '.'));
	else:
	return substr($n, 0, strpos($n, '.') + $d + 1);
	endif;
}

function my_round($number, $n=2){
	return ActiveRecord::static_select_one("round($number, $n)");
}


?>