function trim(cadena) {
	if(!cadena || !cadena.length)
		return cadena;

	for(i=0; i< cadena.length; ) {
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1) {
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
        
        return cadena;
}

function printDate($date, $form, $format, $default, $hide_time) {
	if(!$form)
		$form = 0;

	if(!$format)
		$format = 1;

	if(!$default)
		$default = '&nbsp;';
		
	// $fecha viene en 'AAAA-MM-DD'
	$res = $default;

	switch($format) {
		case 3:
			$meses = {"01": "01","02": "02","03": "03","04": "04","05": "05","06": "06","07": "07","08": "08","09": "09","10": "10","11": "11","12": "12"};
			break;

		case 2:
			$meses = {"01": "Ene","02": "Feb","03": "Mar","04": "Abr","05": "May","06": "Jun","07": "Jul","08": "Ago","09": "Sep","10": "Oct","11": "Nov","12": "Dic"};
			break;

		case 1: default:
			$meses = {"01": "Enero","02": "Febrero","03": "Marzo","04": "Abril","05": "Mayo","06": "Junio","07": "Julio","08": "Agosto","09": "Septiembre","10": "Octubre","11": "Noviembre","12": "Diciembre"};
			break;
	}

	if(($aux = /(\d{4})-(\d{2})-(\d{2})( \d{2}:\d{2}:\d{2})?/.exec($date))) {
		if($aux[1] == "0000")
			return $default;

		if($format != 3)
			$aux[3] = parseInt($aux[3], 10);

		switch($form) {
			case 1:
				$res = ($aux[3] != '00' ? $aux[3] + " / " : '') + ($aux[2] != '00' ? $meses[$aux[2]] + " / " : '') + $aux[1];
				break;
			case 2: 
				$res = $aux[3] + "-" + $meses[$aux[2]] + "-" + $aux[1];
				break;
			case 0: default:
				$res = $meses[$aux[2]] + " " + $aux[3] + ", " + $aux[1];
		}
		
		if($aux[4] && !$hide_time) {
			$res += " " + $aux[4];
		}
	}

	return $res;
}

function printSize($size) {
	$i = 0;
	$iec = [ 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ];
	while (($size / 1024) > 1) {
		$size = $size / 1024;
		$i++;
	}
	
	if($size == undefined) {
		return "0 B";
	}

	$tam = $size.toString().length;
	if($i > 0)		
		$tam = $size.toString().indexOf('.') + 3;

	return $size.toString().substring(0, $tam) + ' ' + $iec[$i];
}

function __parametersObjectToURL(parameters) {
        if(!parameters)
                return '';

        var str_parameters = '';
        for(var a in parameters)
                str_parameters += '&' + a + '=' + parameters[a];

        return str_parameters;
}

function modTT780(mod, parameters, complete) {
        var async = !complete ? false : true;

        var str_parameters = __parametersObjectToURL(parameters);

        // ... para evitar el cache en ajax
        var token = '';
        for(var i = 0; i < 10; i++)
            token += Math.ceil(9 * Math.random())

        var a_data = $.ajax({
                type: "GET",
                url: "index.php",
                data: "js=true&mod=" + mod + str_parameters + '&' + token,
                async: async,
                complete: complete
        });

        if(!complete)
                return a_data.responseText;

        return a_data;
}

function boxTT780(mod, parameters, options, page) {
		if(!page)
			page = "index.php";

        var str_parameters = __parametersObjectToURL(parameters);

        // opciones para mwindow
        var opt = {
			title: options.title ? options.title : 400,
			width: options.width ? options.width : 400,
			height: options.height ? options.height : 400
        };

		if(options.onClose)
			mWindow.onClose = options.onClose;

		if(options.onOpen)
			mWindow.onOpen = options.onOpen;

		if(options.onLoad)
			mWindow.onLoad = options.onLoad;

        var controls = '';
        if(options.buttons) {
                for(var i = 0; i < options.buttons.length; i++)
                        controls += '<input type="button" value="' + options.buttons[i].name + '" id="mw' + options.buttons[i].name + '">';

                controls = '<div class="controles">' + controls + '</div>';
        }

        mWindow.open('<iframe name="mwFrame" id="mwFrame" border="0" frameborder="0" width="' + opt.width + '" height="' + (opt.height - 36) + '" src="' + page + '?mod=' + mod + str_parameters + '"></iframe>' + controls, opt);
		
		window.mWindowClose = function () {
			mWindow.close();
		}
		
		window.mWindowResize = function (w, h) {
			$('#mwFrame').css('width', (w - 6) + 'px');
			$('#mwFrame').css('height', (h - 36) + 'px');
			
			$('#mWindow .redondeado_content').css('width', (w - 2) + 'px');
			$('#mWindow .redondeado_content').css('height', (h - 8) + 'px');

			$('#mWindow').css('width', w + 'px');
			$('#mWindow').css('height', h + 'px');
			
			$('#mWindow').css('margin-left', (-1 * Math.ceil(w / 2)) + 'px');
			$('#mWindow').css('margin-top', (-1 * Math.ceil(h / 2)) + 'px');

		}
}


////////////////////////////////////////////////////////////////////////////////

function __intentar(value) {
	try {
		return eval(value);
	}
	catch(e) {
		alert(e + ":\n" + value);
		return null;
	}
}

function is_integer(value) {
	if(value == '' || parseInt(value, 10) == NaN)
		return 0;

	return __intentar(value);
}

function is_text(value) {
	if(value == '' || value == 'null' || value === null)
		return '';

	return __intentar(value);
}

function is_hash(value) {
	return eval('(' + value + ')');
	

}

