// JavaScript Document

// Función para seleccionar todos los checkboxes de un formulario
function selectAll() {
 d = document.forms[0];
 for (i = 0; i < d.elements.length; i++) {
   if (d.elements[i].type == "checkbox") {
     d.elements[i].checked = true;
   }
 }
}


// Función para desseleccionar todos los checkboxes de un formulario
function deselectAll() {
 d = document.forms[0];
 for (i = 0; i < d.elements.length; i++) {
   if (d.elements[i].type == "checkbox") {
     d.elements[i].checked = false;
   }
 }
}

// Función para motrar y ocultar divs
function show_hide_div(id) {
	//alert (document.getElementById(id).style.display);
	if (document.getElementById(id).style.display == "none" || !document.getElementById(id).style.display) document.getElementById(id).style.display='block';
	else document.getElementById(id).style.display='none';
}


// Función para actualizar dinámicamente los campos de un formulario de estructura xml
/*function struc_update_info (struc_id) {
	
	formulario = document.forms[0];
	
	// Se borra el valor de los campos antes de actualizarlos 
	for (i=0; i<formulario.elements.length; i++) if (formulario.elements[i].id.substr(0,nom_campo.length+1) == "_"+nom_campo) formulario.elements[i].value = "";
	
	// Se actualizan los campos de formulario
	for (var campo in xml_vars_rel) {
		
		campo_id = '_'+nom_campo+'_'+xml_vars_rel[campo];
		
		if (!isset(document.getElementById(campo_id))) continue;

		document.getElementById(campo_id).value = xml_vars[struc_id][campo];
		
		if (document.getElementById(campo_id).value == 'undefined') document.getElementById(campo_id).value = "";
		
		// Especial para borrado, si el campo es el selector de borrado y no se tienen permisos, se inhabilita el campo
		if (xml_vars_rel[campo] == 'privilegioborrar' && grupos_privilegioborrar<xml_vars[struc_id][campo]) document.getElementById(campo_id).disabled = true;
	
	}
}*/


// Función para aplicar función a un array de manera recursiva
function recursive_array_walk (array_,function_) {
	
	for (var child in array_.childNodes) {
		
		eval (function_);

		if (array_.childNodes[child].childNodes) recursive_array_walk (array_.childNodes[child],function_);
	}
}


// Función para actualizar dinámicamente los campos de un formulario de estructura motree xml
function struc_update_info_motree (struc_id) {

	if (struc_id != id_edit && isset(document.getElementById('span_'+struc_id)) && old_id_struc != struc_id) tag = "span_";
	else {
		if (isset(document.getElementById('input_'+struc_id))) desEditIt();								  	
		return;
	}
	
	old_id_struc = struc_id;

	// Se desmarca el elemento anterior
	if (id_estructural && isset(document.getElementById('span_'+id_estructural))) {

		document.getElementById(id_estructural).className = 'structure_li';

		document.getElementById(tag+id_estructural).className = 'structure_span';

	}
	
	document.getElementById(struc_id).className = 'structure_li_marked';
		
	document.getElementById(tag+struc_id).className = 'structure_span_marked';
	
	id_estructural = struc_id;
	
	formulario = document.forms[0];
	
	// Se borra el valor de los campos antes de actualizarlos 
	for (i=0; i<formulario.elements.length; i++) if (formulario.elements[i].id.substr(0,nom_campo.length+1) == "_"+nom_campo) if (formulario.elements[i].type != 'checkbox' && formulario.elements[i].getTag() != 'select') {
			formulario.elements[i].value = "";
	}
	
	
	// Se actualizan los campos de formulario
	for (var campo in xml_vars_rel) {
		
		campo_id = '_'+nom_campo+'_'+xml_vars_rel[campo];
		
		if (!isset(document.getElementById(campo_id))) continue;

		// Si es un checkbox
		if (document.getElementById(campo_id).type == 'checkbox') document.getElementById(campo_id).checked = (xml_vars[struc_id][campo] == 'true'?true:false);
		else document.getElementById(campo_id).value = xml_vars[struc_id][campo];
		
		if (document.getElementById(campo_id).value == 'undefined') document.getElementById(campo_id).value = "";
		
		// Especial para borrado, si el campo es el selector de borrado y no se tienen permisos, se inhabilita el campo
		if (xml_vars_rel[campo] == 'privilegioborrar' && grupos_privilegioborrar<xml_vars[struc_id][campo]) document.getElementById(campo_id).disabled = true;
	
	}

}


// Función para deshabilitar los camos de un formulario
function disable_form (bool) {
	
	formulario = document.forms[0];
	
	for (i=0; i<formulario.elements.length; i++) formulario.elements[i].disabled = bool;
	
}


// Función para ocultar y mostrar elementos
function show_hide (element_id,control_element_id,frase_on,frase_off) {
	
	obj = document.getElementById (element_id);
	
	control_obj = document.getElementById (control_element_id);
	
	if (obj.style.display == 'block' && element_id != control_element_id) {
		obj.style.display = 'none';
		control_obj.firstChild.nodeValue = frase_on; 
	}
	else if (obj.style.display == 'none' && element_id != control_element_id) {
		obj.style.display = 'block';
		control_obj.firstChild.nodeValue = frase_off;
	} else if (obj.style.display == '' || element_id == control_element_id) {
		obj.style.display = 'block';
		control_obj.firstChild.nodeValue = frase_on; 
	}
}



// Función isset
function isset(variable_name) {
	try {
		 if (typeof(eval(variable_name)) != 'undefined')
		 if (eval(variable_name) != null)
		 return true;
	 } catch(e) { }
	return false;
}

// Función include
function include(file_path) {
	window.addEvent('domready', function() {
		var j = document.createElement("script");
		j.type = "text/javascript";
		j.src = file_path;
		document.body.appendChild(j);
	});
}

// Función para evitar el envío de un formulario mediante enter
function get_disable_enter (e) {
	if (!isset ('permit_enter_submit')) return true;
	else return e.keyCode!=13;			
}

function get_img_size(src) {
	
	var newImg = new Image();
	
	newImg.src = src;
	
	var height = newImg.offsetHeight;
	
	var width = newImg.offsetWidth;
	
	
	imgSize = new Array();
	
	imgSize[0] = width;
	imgSize[1] = height;
	
	return imgSize;
	
}

function serialize (element, options){
    var str = [];
    var key = encodeURIComponent(options.key || element.id);
    var elements = $A(element.getElements(options.tag || ''));
    elements.each(function(el, i) {
        str.push(key+'['+i+']='+el.id);
    });
    return str.join('&');
}





// Función que recupera la posición del ratón
/*function getMousePos(e) {
	var mouseposx = 0;
	var mouseposy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		mouseposx = e.pageX;
		mouseposy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		mouseposx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		mouseposy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

}*/



