/**
 * Función que reemplaza la ñ por la n de una cadena dada.
 *
 * @param {String} cadena Cadena a reemplazar.
 * @return La cadena reemplazada.
 * @type {String}
 */
function replaceChars(cadena) {
	var resultado = "" + cadena;
	var out = String.fromCharCode(209).toLowerCase();
	var add = "N";
	
	while (resultado.indexOf(out) > -1) {
		var pos = resultado.indexOf(out);
		resultado = "" + (resultado.substring(0, pos) + add + resultado.substring((pos + out.length), resultado.length));
	}
	return resultado
}

/**
 * Función para esconder o mostrar un elemento HTML.
 *
 * @param {String} id Identificador del elemento a esconder/mostrar.
 * @param {boolean} ver True si se quiere mostrar, false si se quiere ocultar.
 * @return
 * @type void
 */
function verFila(id,ver) {
	var dis = ver ? '' : 'none';
	var fila = document.getElementById(id);
	fila.style.display = dis;
}

/**
 * Función que mira si una cadena es vacio (contando los espacios en blanco como vacío.
 *
 * @param {String} cadena Cadena a mirar.
 * @return True si el campo es vacío y false en caso contrario.
 * @type {boolean}
 */
function campoVacio(cadena) {  
	for ( i = 0; i < cadena.length; i++ ) {
		if ( cadena.charAt(i) != " " ) {  
			return false;
		}
	}
	return true;
}

/**
 * Adds an option to a select and remove the last options.
 *
 * @param {String} selectId the id of the select tag to add the option.
 * @param {String} text option text.
 * @param {String} value option value; if the method parameter is not passed, an empty value is assumed.
 * @return
 * @type void
 */
function addOption(selectId, text, value) {

	var option = document.createElement('option');

	option.text = text;
	option.value = (value != 'undefined' ? value : '');

	var select = document.getElementById(selectId);
	
    try {
		select.add(option, null); // standards compliant
	} catch (ex) {
		select.add(option); // IE only
	}
}

/**
 * Insert all options in the select tag.
 *
 * @param {Ajax.Request} request AJAX prototype request. 
 *		If you don't use the json array data then evaluate the renderedText property of the AJAX request instead.
 * @param {Array} json the array with the options generated in the ajax callback function.
 * @param {int} selectedId the id of the option selected.
 * @param {Array} staticOptions the array with the static options.
 * @param {String} selectId the id of the select tag to add the loading option.
 * @return
 * @type void
 */
function updateSelect(request, selectedId, staticOptions, selectId) {

	var select = document.getElementById(selectId);
	
	select.length = 0;
	
	var responses = eval('(' + request.responseText + ')');
	
	var resSize = responses.length;
	
	var staticSize = staticOptions.length;
	
	// Create static options
	for (var i = 0; i < staticSize; i++) {
		var option = document.createElement('option');

		option.value = staticOptions[i][0];
		option.text = staticOptions[i][1];
		
		try {
			select.add(option,null); // standards compliant
		}
		catch (ex) {
			select.add(option); // IE only
		}	
	}
	
	// Create all other options
	for (var i = 0; i < resSize; i++) {
		var option = document.createElement('option');

		option.value = responses[i][0];
		option.text = responses[i][1];
		
		try {
			select.add(option,null);
		}
		catch (ex) {
			select.add(option);
		}
	}
	
	for (var i = 0; i < select.length; i++) {
		if (select.options[i].value == selectedId) {
			select.selectedIndex = i;
		}
	}
	
	return;
}