/* ----------- funciones de utilidades ------------ */

function isEmpty(cadena) { 
    return (cadena == null) || (cadena.length == 0); 
}

/* elimina espacios de la izquierda */
var whitespace = "\n\r\t ";
function ltrim(cadena){
    if(isEmpty(cadena)) return "";
    var left = 0; var i = 0;
    while((i < cadena.length) && (whitespace.indexOf(cadena.charAt(i++))!= -1))
        left++;
        
    return cadena.substring(left, cadena.length);
}

/* elimina espacios de la derecha */
function rtrim(cadena){
    if(isEmpty(cadena)) return "";
    var right = 0; var k = 0;
    k = cadena.length-1;
    while((k >= 0) && (whitespace.indexOf(cadena.charAt(k--))!= -1)) 
        right++;
        
    return cadena.substring(0, cadena.length - right);
}

/* elimina los espacios a la izquierda y la derecha */
function trim(cadena){
    return ltrim(rtrim(cadena));
}


/* muestra la capa indicando su id (deberia ser un div) */
function mostrarCapa(obj){
	if(document.getElementById(obj) != null){
	    var el = document.getElementById(obj);
	    el.style.display = "block";
	} else {
		alert("Error: no se puede mostrar la capa con id '" + obj + "' porque no existe.");
	}
}

/* oculta la capa indicando su id (deberia ser un div) */
function ocultarCapa(obj){
	if(document.getElementById(obj) != null){
	    var el = document.getElementById(obj);
	    el.style.display = "none";
	} else {
		alert("Error: no se puede ocultar la capa con id '" + obj + "' porque no existe.");
	}
}

/* cambia la visibilidad de una la capa indicando su id (deberia ser un div) */
function cambiarVisibilidadCapa(obj){
	if(document.getElementById(obj) != null){
	    var el = document.getElementById(obj);
	    if(el.style.display != "block"){
	        el.style.display = "block";
		} else{
			el.style.display = "none";
		}
	} else {
		alert("Error: no se puede cambiar la visibilidad de la capa con id '" + obj + "' porque no existe.");
	}
}

/* Funcion para mostrar los elementos ocultos de un contenido */
function muestra(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("contenido").getElementsByTagName("div");
		if(document.getElementById(obj) != null){
			if(el.style.display != "block"){
				for (var i=0; i<ar.length; i++){
					if (ar[i].className=="subcontenido")
					ar[i].style.display = "none";
				}
				el.style.display = "block";
			} else{
				el.style.display = "none";
			}
		} else {
			alert("Error: no se puede mostrar el contenido de la capa con id '" + obj + "' porque no existe.");
		} 
	}
}

/* Cambia el valor marcado de un campo select sabiendo el valor (value) de esa opcion */
function cambiar_select(id_select, valor){
	if(document.getElementById(id_select) != null){
		var ob_select = document.getElementById(id_select);
		for(i=0; i < ob_select.length; i++){
			if(ob_select.options[i].value == valor){
				ob_select.options[i].selected = true;
			} 
		}
	} else {
		alert("Error: no se puede cambiar el valor del select con id '" + id_select + "' porque no existe.");
	} 
}

function include(file_path){
	var j = document.createElement("script");
	j.type = "text/javascript";
	j.src = file_path;
	document.body.appendChild(j);
}

function include_once(file_path){
	var sc = document.getElementsByTagName("script");
	for (var x in sc)
		if (sc[x].src != null && sc[x].src.indexOf(file_path) != -1) return;
	include(file_path);
}

function include_texto(texto){
	var j = document.createElement("script");
	j.type = "text/javascript";
	j.text = texto;
	document.body.appendChild(j);
}

/**
 * Esta funcion usa la libreria mootools
 */
var resaltar_en_accion = false;
function resaltar(id){
	if(!resaltar_en_accion){
		resaltar_en_accion = true;
		var color_anterior = $(id).getStyle('background-color');
		var efecto = $(id).effect('background-color', {duration: 250});
		 
		efecto.start('#fff').chain(function() {
			efecto.start(color_anterior).chain(function() {
				efecto.start('#fff').chain(function() {
					efecto.start(color_anterior).chain(function() {
						efecto.start('#fff').chain(function() {
							efecto.start(color_anterior).chain(function() {
								efecto.start('#fff').chain(function() {
									efecto.start(color_anterior).chain(function() {
										efecto.start('#fff').chain(function() {
											efecto.start(color_anterior).chain(function() {
												resaltar_en_accion = false;
											});
										});
									});
								});
							});
						});
					});
				});
			});
		});
	} /* fin if */
}


function pestana_on(id){
	$(id).effect('opacity', {duration: 300, transition: Fx.Transitions.Cubic.easeOut}).start(1);
	
	//var efecto = $(id).effects({duration: 300, transition: Fx.Transitions.Cubic.easeOut});
	//efecto.start({
	//	'opacity': 1
	//});
}

function pestana_off(id){
	$(id).effect('opacity', {duration: 300, transition: Fx.Transitions.Cubic.easeIn}).start(0.5);
}

function menu_on(id){
	var efecto = $(id).effects({duration: 300, transition: Fx.Transitions.Cubic.easeOut});
	efecto.start({
		'margin-left': 8
	});
}

function menu_off(id){
	var efecto = $(id).effects({duration: 300, transition: Fx.Transitions.Cubic.easeIn});
	efecto.start({
		'margin-left': 0
	});
}

/* funcion onload del body, volver a definir donde se necesite */
function mensaje_growl_smoke(titulo,texto){
	Growl.Smoke({
	    title: titulo,
	    text: texto
	});
}

function mensaje_growl_bezel(titulo,texto){
	Growl.Bezel({
	    title: titulo,
	    text: texto
	});
}


/* funcion onload del body, volver a definir donde se necesite */
function funcion_onload(){
	
}

/**
 * Pasa un array de javascript a string serializable para luego convertirlo a php
 * Se unserializa con:
 *	$my_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
 */
function js_array_a_php_array(a){ 
    var a_php = ""; 
    var total = 0;
	for(var key in a){ 
        ++total; 
        a_php = a_php + "s:" + 
                String(key).length + ":\"" + String(key) + "\";s:" + 
                String(a[key]).length + ":\"" + String(a[key]) + "\";"; 
    } 
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php; 
}

/* Sube el scroll al inicio de la pagina */
function scroll_arriba(){
	window.scrollTo(0,0);
}

/* Sube el scroll al inicio de la pagina */
function scroll_abajo(){
	window.scrollTo(0,100000);
}



/**/
