

function showOtros(){
					document.getElementById("otros").style.display ="block";
}
				
	
function hideOtros(){
					document.getElementById("otros").style.display ="none";
}
	

function hideOrShow(id){
				if (document.getElementById(id).style.display =="none") {
					document.getElementById(id).style.display = "block";
				} else {
					document.getElementById(id).style.display = "none";
				}
}
	
//
// Functions for Encuestarea WebSite
// (c) Cometa Technologies, 2006
//

function paginate(formName,page,pageField,end,endField){
	document.getElementById(pageField).value=page;
	document.getElementById(endField).value=end;
	document.getElementById(formName).submit();
}


function emptyOthers (name, hidden) {
	 if  ( (document.getElementsByName(name).item(0)) && (document.getElementsByName(name).item(0).value != "") ) {
	 		if (document.getElementsByName(hidden).item(0)) {
	 			document.getElementsByName(hidden).item(0).value = document.getElementsByName(name).item(0).value;
	 		}
			document.getElementsByName(name).item(0).value = "";
	 }
}

function recoverOthers (target, name) {
	if (document.getElementsByName(name).item(0).value!= "") {
		target.value = document.getElementsByName(name).item(0).value;
	}
}

//
// Method to change URL to the given one by parameter
//
function changeTo(urlToMove) {
	window.location.href = urlToMove;
}

//
// Method to change URL to the given by parameter
// and submiting value of select object
function changeToOnSelect(urlToMove,paramName,selectListName) {
	
	var selectObj = document.getElementById(selectListName);
	var value = selectObj.options[selectObj.selectedIndex].value;
	window.location.href = urlToMove + paramName + '=' + value;
}

//
// Generic method to submit a form
//
function submitForm(formName) {
	document.getElementById(formName).submit();
}

function submitFormTo (formName, target) {
	document.getElementById(formName).action = target;
	document.getElementById(formName).submit();
}


function enableDisableElement(element){
	document.getElementById(element).disabled = !document.getElementById(element).disabled;
}

//
// Generic method to change checkbox state
//
function canvia(obj){
	obj.disabled=!obj.disabled;
}

function canviaLinkHref(linkId, url) {
	document.getElementById(linkId).href = url;
}

function canviaLinkHref2(linkId, linkId2, url) {
	document.getElementById(linkId).href = url;
	document.getElementById(linkId2).href = url;
}

function canviaLinkHref3(radioId, varname, url) {
	val = document.getElementById(radioId).value;
	alert(radioId);
	finalurl = url+'?'+varname+'='+val;
	alert(finalurl);
// changeTo(finalurl);
}

//
// Function to go to next page of results
//
function nextPage(formName){
	form = document.forms[formName];
	form.navigation.value = 'next';
	form.submit();
}

//
// Function to go to previous page of results
//
function previousPage(formName){
	form = document.forms[formName];
	form.navigation.value = 'previous';
	form.submit();
}

//
// Function to go to a given page of results
//
function goPage(formName,numField){
	form = document.forms[formName];
	var numberFormField = document.getElementById(numField);
	form.navigation.value = numberFormField.value;
	form.submit();
}

//
// Function to change number of displayed elements.
//
function changeDisplayedElements(formName){
	form = document.forms[formName];
	form.actualPage.value = 1;
	form.submit();
}

//
// Open alert window
//
function showMessage(message){
	alert(message);
}

//
// Open a popUp
//

function popup (URL,w,h){
	var title = 'popup';
	
	if (w<=0) w='650';
	if (h<=0) h='400';
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	
	parametres = "width=" + w + ", height=" + h + ",top=" + wint + ",left=" + winl + ", scrollbars=yes, menubar=no, location=no, resizable=yes";
	window.open(URL,title,parametres);
}

function changeQuestion(dropDownList){
            var url = "CopyMoveQuest.do?questionid=" + dropDownList.selectedOrigin.value+"&testid=" +dropDownList.testid.value;
            window.location = url;
}

function alertar(msg){
	alert(msg);
}

function submitTo(form, destaction){
	form.action = destaction;
	form.submit();
}

function enableDisable(enableElement,disableElement){
	document.getElementById(enableElement).disabled = true;
	document.getElementById(disableElement).disabled = false;
}

function enableDisable2(enableElement,disableElement){
	document.getElementById(enableElement).disabled = !document.getElementById(enableElement).disabled;
	document.getElementById(disableElement).disabled = !document.getElementById(disableElement).disabled;
}

function enableElement(element){
	document.getElementById(element).disabled = '';
}

function disableElement(element){
	document.getElementById(element).disabled = 'true';
}

//
// Popup to confirm action
// msg : message to show
// url : if user confirms action, this is the url to redirect.
function confirmationMsg(msg,url){
	var answer = confirm(msg);
	if (answer){
		window.location = url;
	}
}

//
// Popup to confirm action and perform action based on <SELECT> selection.
// msg : message to show
// url : if user confirms action, this is the url to redirect.
function confirmationMsgOnSelect(msg,urlToMove,paramName,selectListName) {
	
	var answer = confirm(msg);
	if (answer){
		var selectObj = document.getElementById(selectListName);
		var value = selectObj.options[selectObj.selectedIndex].value;
		window.location.href = urlToMove + paramName + '=' + value;
	}
}

//
// Popup to confirm action. It asks for two confirmation.
// Function used in common cancel or delete operations.
// msg1 : first message to show
// msg2 : second message to show
// url : if user confirms action, this is the url to redirect.
function doubleConfirmationMsg(msg1, msg2,url){
	var answer = confirm(msg1);
	if (answer){
		answer = confirm(msg2)
		if(answer){
			window.location = url;
		}
	}
}

//
// Set hidden parameter and submit form. Value of hidden parameter
// is value from a select element.
//
function setHiddenAndSubmit(value,hiddenName,formName){

	var hiddenObj = document.getElementById(hiddenName);
	hiddenObj.value = value;
	document.getElementById(formName).submit();
}



// **********************************************************************
function validarIdentificacionFiscal(cif) {
	
	if(cif!=""){
		par = 0
		non = 0
		letras="ABCDEFGHKLMNPQS"
		let=cif.charAt(0)
		
		if (!isNaN(let)){
		  nif=cif
		  if(!validarNif(nif))
		  	return false
		}

		if (cif.length!=9){
			alert('El Cif debe tener 9 digitos')
			return false
		}

		if (letras.indexOf(let.toUpperCase())==-1){
			alert("El comienzo del Cif no es valido")
			return false;
		}

		for (zz=2;zz<8;zz+=2){
			par = par+parseInt(cif.charAt(zz))
		}
		
		for (zz=1;zz<9;zz+=2){
			nn = 2*parseInt(cif.charAt(zz));
			if (nn > 9) nn = 1+(nn-10);
			non = non+nn;
		}
		
		parcial = par + non;
		control = (10 - ( parcial % 10));
		if (control==10) control=0;
		if (control!=cif.charAt(8)){
			alert("El Cif no es valido");
			return false
		}
	}
	return true
}

function validarNif(abc){
	dni=abc.substring(0,abc.length-1);
	let=abc.charAt(abc.length-1);
	if (!isNaN(let)){
		alert('Falta la letra del NIF');
		return false;
	} else {
		cadena="TRWAGMYFPDXBNJZSQVHLCKET";
		posicion = dni % 23;
		letra = cadena.substring(posicion,posicion+1);
		if (letra!=let.toUpperCase()){
			alert("Nif no valido");
			return false;
		}
	}
	return true
}

//
// Function to only accept DELETE(keycode=46) or BACKSPACE(keycode=8)
// keys on a keypress event, and delete field content.
//
function onlyDelete(e,field){
	if (e.keyCode==46 || e.keyCode==8)
		document.getElementById(field).value='';
	return false;
}





// **********************************************************************
function submitIf(formName,fieldToCheck,invalidValue,msg){

	var form = document.getElementById(formName);
	if(document.getElementById(fieldToCheck).value!==invalidValue){
		form.submit();
	} else {
		alert(msg);
	};
}

// **********************************************************************
function checkIfConfirmation(msg,elementToCheck){
	alert(msg);
	document.getElementById(elementToCheck).value = "true";
}

function checkIfEditing(msg,changedField){
	var isEditing = document.getElementById('editMode');
	var yaAvisado = document.getElementById('avisado');
	var tieneRespuestas = document.getElementById('hasCollectedAnswers');
	if(tieneRespuestas=='true' && isEditing.value=='true' && yaAvisado.value=='false'){
		yaAvisado.value = 'true';
		checkIfConfirmation(msg,changedField);
	}
	document.getElementById(changedField).value = "true";
}

function alertIfHasAnswers (msg){
	var tieneRespuestas = document.getElementsByName('hasCollectedAnswers').item(0);

	if(tieneRespuestas.value=='true'){
	  alert(msg);
	}
	return true;
}

function isNumeric(currentItem){
	if (currentItem.value!="");
	{
		var isFound = /^-?\d+$/.test(currentItem.value);
		if (!isFound){
			currentItem.value= "";	
			alert ("El valor debe ser numérico");
			currentItem.focus();
		}
		
	}
}


// Submit a form if fieldToCheck is true, and user confirms twice.
function ifTrueDoubleConfirmation(msg1,msg2,formToSubmit,fieldToCheck,valueToCheck){
	var field = document.getElementById(fieldToCheck);
	if(field.value==valueToCheck){
		var answer = confirm(msg1);
		if (answer){
			answer = confirm(msg2)
			if(answer){
				document.getElementById(formToSubmit).submit();
			}
		}
	} else {
		document.getElementById(formToSubmit).submit();
	}
}

// Submit form if user confirms action
function submitFormIfConfirmation(msg,formName) {
	if(confirm(msg)){
		document.getElementById(formName).submit();
	}
}

// Submit form if user confirms action
function gotoIfConfirmation(msg,ref) {
	if(confirm(msg)){
		changeTo (ref);
	}
}

// ********************************************************
// Switch between INSERT and KEEP/MODIFY/DELETE options
// in question types 1,2,4 and 5.
// ********************************************************
function switchOptions(){
	// Recuperamos estado actual
	// Intercambiamos estados solo si no se ha realizado ningun cambio
	// en las opciones de respuesta. Si no, dejamos el estado que tengamos.
	var isEditing = document.getElementById('editMode');
	var yaAvisado = document.getElementById('avisado');
	if(isEditing.value=='false'){
		// Si estamos insertando no se produce nunca un cambio
	} else {
		// Si estamos en edicion, solo cambiaremos de panel
		document.getElementById('insertOpts').style.display = "inline";
		document.getElementById('updateOpts').style.display = "none";
		document.getElementById('insertOpts').style.visibility = "visible";
		document.getElementById('updateOpts').style.visibility = "hidden";
	}
}

function valueIsPositiveInteger(field,literal){
	var value = parseInt(document.getElementById(field).value);
	if(value<0){
		alert("Campo " + literal + " tiene que ser positivo");
		return false;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function trim(myString) {
	if(myString == null){
		return null;
	}
	else{
		return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
	}
}


// *********************************************************
// Function to assure only one radio button per column is
// selected. This function has a variable number of
// parameters. The first is the form
// The second one must to be the name of actual
// radio buttons group. The rest is all the names of all
// groups to check.
// *********************************************************
function oneOptionPerColumn(){

	// Recuperamos los parametros
	var form = arguments[0];
	var actual = arguments[1];
	var numElements = arguments.length;
	
	// Recuperamos la posicion del elemento que
	// se ha seleccionado.
	var selectedGroup = form[actual];
	var selectedPosition = 0;
	for(i=0;i<selectedGroup.length;i++){
		if(selectedGroup[i].checked){
			pos = i;
		}
	}
	
	// Recorremos todos los grupos y deseleccionamos
	// la misma posicion indicada.
	for(i=2;i<numElements;i++){
		if(arguments[i]!=actual){
			var group = form[arguments[i]];
			group[pos].checked = false;
		}
	}
}

    

function addFavorites() {
	if (window.sidebar&&window.sidebar.addPanel)
		window.sidebar.addPanel("e-encuesta: Encuestas on-line","http://www.e-encuesta.com","");
	else
		window.external.AddFavorite("http://www.e-encuesta.com"," e-encuesta: Encuestas on-line");
}



function validateEmail(addr,man,db) {
		if (addr == '' && man) {
		   if (db) alert('El campo email es obligatorio');
		   return false;
		}
		if (addr == '') return true;
		var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
		for (i=0; i<invalidChars.length; i++) {
		   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		      if (db) alert('El email contiene caracteres no válidos');
		      return false;
		   }
		}
		for (i=0; i<addr.length; i++) {
		   if (addr.charCodeAt(i)>127) {
		      if (db) alert("El email contiene caracteres no válidos");
		      return false;
		   }
		}
		
		var atPos = addr.indexOf('@',0);
		if (atPos == -1) {
		   if (db) alert('El email debe contener el símbolo @');
		   return false;
		}
		if (atPos == 0) {
		   if (db) alert('El email no puede comenzar con el símbolo @');
		   return false;
		}
		if (addr.indexOf('@', atPos + 1) > - 1) {
		   if (db) alert('El email no puede contener más de una @');
		   return false;
		}
		if (addr.indexOf('.', atPos) == -1) {
		   if (db) alert('El email debe contener un punto en el dominio');
		   return false;
		}
		if (addr.indexOf('@.',0) != -1) {
		   if (db) alert('El email no puede contener un punto seguido de @');
		   return false;
		}
		if (addr.indexOf('.@',0) != -1){
		   if (db) alert('El email no puede contener un punto precedido de @');
		   return false;
		}
		if (addr.indexOf('..',0) != -1) {
		   if (db) alert('El email no puede contener dos puntos seguidos');
		   return false;
		}
		var suffix = addr.substring(addr.lastIndexOf('.')+1);
		suffix = suffix.toLowerCase();
	
		if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
		   if (db) alert('Extendión inválida de email');
		   return false;
		}
		return true;
}

function findIndex(array, element){
	for(var i = 0; i< array.length;i++){
		if(array[i] == element){
			return i;
		}
	}
	return -1;
}
