function validateElement () {
  // Comprueba que el control de formulario ,tipo text,textArea, sobre el que se 
  // lanza esta funcion sea correcto, esto es, comprueba si es o no obligatorio pbNotNull, 
  // que no exceda de una longitud maxima, y que el valor que almacena sea valido para el tipo 
  // number,varchar,decimal,date,hour
  
  
  if (this.type.toUpperCase().indexOf("SELECT") != -1) 
  {
  	// Validamos el select
  	  if ( !this.canBeNull ) 
  	  {
	      // Field no puede ser nulo
		  //MODIFICADO NACHO- LISTA DE PAISES
	      if (( this.selectedIndex == -1 ))// || (this[this.selectedIndex].value == -1 || this[this.selectedIndex].value==""))
	      {
			     alert("Please select a value from the list (" + this.label + ")");
			     this.focus();
			     return false;
		    }
	    }
	  // En este punto, el elemento select esta validado correctamente
	  return true;
  }
  
  if (this.type.toUpperCase().indexOf("HIDDEN") != -1) 
  {
  	// Validamos el select
  	  if ( !this.canBeNull ) 
  	  {
	      // Field no puede ser nulo
	      if (( this.value == "" ))
	      {
			     alert("Please select a value from the obligatory fields (" + this.label + ")");
			     //this.focus();
			     return false;
		    }
	    }
	  // En este punto, el elemento select esta validado correctamente
	  return true;
  }


  var controlValue = "";
  var checkValue = "";
  controlValue = this.value;

  // Comprobamos que el control existe
  var re = /\s/gi;
  checkValue = this.value.replace(re,'');
  
  
   
  if ( !this.canBeNull ) 
  {
    // Field no puede ser nulo
    if ( checkValue.length <= 0 ) 
    {
			alert("Field must be filled (" + this.label + ")");
			this.select();
			this.focus();
			return false;
   	}
  }
  else 
  {
  	// Field puede ser nulo
  	if (checkValue.length <= 0) return true; // Field esta vacio: No hacemos mas comprobaciones.
  }
  
  // Comprobando caracteres especiales de HTML
  if (!containsHTMLCode(this)) {
  	return false;
  }
  
  
  var bError = false;

  
  switch ( this.storageType ) {
    case "number" :  
		// Field debe ser un numero
		if (this.maxValue!=undefined && controlValue>this.maxValue) {
			//Supera el valor maximo
			alert("Maximum value of field exceeded (" + this.label + "): " + this.maxValue);
			bError = true;
			break;
		}
		if ( controlValue.length > this.maxStorageLength ) {
			// Excede la longitud maxima
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxStorageLength);
			bError = true;
			break;
		}
		
		// Comprobamos la longitud mínima dField
		if ( controlValue.length < this.minStorageLength ) {
			// No alcanza la longitud minima
			alert("Minimum length of field not reached (" + this.label + "): " + this.minStorageLength);
			bError = true;
			break;
		}
			
		var mivalue=parseInt (controlValue);
		// Testeamos que sea un numero valido
		if ( isNaN (controlValue) ) {
			alert("Field  (" + this.label + ") is not a number");
			bError = true;
			break;
		}
		// Testeamos que no tenga decimales
		if (String(mivalue) != String(controlValue)) {
			alert("Field  (" + this.label + ") is not a whole number");
			bError = true;
			break;
		}
		
    break;
	
	case "positive" :  
		// Field debe ser un numero
		if (this.maxValue!=undefined && controlValue>this.maxValue) {
			//Supera el valor maximo
			alert("Maximum value of field exceeded (" + this.label + "): " + this.maxValue);
			bError = true;
			break;
		}
		if ( controlValue.length > this.maxStorageLength ) {
			// Excede la longitud maxima
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxStorageLength);
			bError = true;
			break;
		}
		
		// Comprobamos la longitud mínima dField
		if ( controlValue.length < this.minStorageLength ) {
			// No alcanza la longitud minima
			alert("Minimum length of field not reached (" + this.label + "): " + this.minStorageLength);
			bError = true;
			break;
		}
			
		var mivalue=parseInt (controlValue);
		// Testeamos que sea un numero valido
		if ( isNaN (controlValue) ) {
			alert("Field (" + this.label + ") is not a number ");
			bError = true;
			break;
		}
		// Testeamos que no tenga decimales
		if (String(mivalue) != String(controlValue)) {
			alert("Field (" + this.label + ") is not a whole number");
			bError = true;
			break;
		}
		
		if (mivalue<1) {
			alert("Field (" + this.label + ") is not a positive number");
			bError = true;
			break;
		}
		
    break;
    	
    case "varchar" :
		// Field debe ser alfanumerico
		if ( controlValue.length > this.maxStorageLength ) {
			// Excede la longitud maxima
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxStorageLength);
			bError = true;
			break;
		}
		
		// Comprobamos la longitud mínima dField
		if ( controlValue.length < this.minStorageLength ) {
			// No alcanza la longitud minima
			alert("Minimum length of field not reached (" + this.label + "): " + this.minStorageLength);
			bError = true;
			break;
		}
		break;
		
    case "email" :
		// Field debe ser alfanumerico
		if ( controlValue.length > this.maxStorageLength ) {
			// Excede la longitud maxima
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxStorageLength);
			bError = true;
			break;
		}
		
		// Comprobamos la longitud mínima dField
		if ( controlValue.length < this.minStorageLength ) {
			// No alcanza la longitud minima
			alert("Minimum length of field not reached (" + this.label + "): " + this.minStorageLength);
			bError = true;
			break;
		}
		
		// the following expression must be all on one line...
		var goodEmail = controlValue.match(/\b(^(\S+@).+((\..{2,4}))$)\b/gi);
		goodEmail = goodEmail && (controlValue.indexOf(',') == -1);
		if (!goodEmail){
		   alert('Please enter a valid email address');
		   bError = true;
		   break;
    }
		
		break;
		
		
    case "RestrictedVarchar" :
		// Field debe ser alfanumerico
		
		if ( controlValue.length > this.maxStorageLength ) {
			// Excede la longitud maxima
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxStorageLength);
			bError = true;
			break;
		}
		
		// Comprobamos la longitud mínima dField
		if ( controlValue.length < this.minStorageLength ) {
			// No alcanza la longitud minima
			alert("Minimum length of field not reached (" + this.label + "): " + this.minStorageLength);
			bError = true;
			break;
		}
		
	  // Sólo caracteres alfanuméricos
	  re = /[^a-zA-z0-9áéíóúÁÉÍÓÚ\\\-\/\._\s]/gi;
	  
	  if (controlValue.search(re) >= 0) {
		alert ("La secuencia de caracteres \""+ controlValue.match(re) + "\" no está permitida (" + this.label + ").");
  	bError = true;
		break;
	  }
		
		
		break;

	 case "decimal" :
		// Field debe ser float
		if (this.maxValue!=undefined && controlValue>this.maxValue) {
			//Supera el valor maximo
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxValue);
			bError = true;
			break;
		}
		if ( controlValue.length > this.maxStorageLength + 1 ) {
			// Excede la longitud maxima 
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxStorageLength);
			bError = true;
			break;
		}
		// Comprobamos la longitud mínima dField
		if ( controlValue.length < this.minStorageLength -1 ) {
			// No alcanza la longitud minima
			alert("Minimum length of field not reached (" + this.label + "): " + this.minStorageLength);
			bError = true;
			break;
		}
		
		var mivalue=parseFloat (controlValue);
		// Testeamos que sea un numero valido
		if ( isNaN (controlValue) ) {
			alert("Field (" + this.label + ") is not a decimal number");
			bError = true;
			break;
		}
		
		break;
    case "decimal-positive" :
		// Field debe ser float
		if (this.maxValue!=undefined && controlValue>this.maxValue) {
			//Supera el valor maximo
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxValue);
			bError = true;
			break;
		}
		if ( controlValue.length > this.maxStorageLength + 1 ) {
			// Excede la longitud maxima 
			alert("Maximum length of field exceeded (" + this.label + "): " + this.maxStorageLength);
			bError = true;
			break;
		}
		// Comprobamos la longitud mínima dField
		if ( controlValue.length < this.minStorageLength -1 ) {
			// No alcanza la longitud minima
			alert("Minimum length of field not reached (" + this.label + "): " + this.minStorageLength);
			bError = true;
			break;
		}
		
		var mivalue=parseFloat (controlValue);
		// Testeamos que sea un numero valido
		if ( isNaN (controlValue) ) {
			alert("Field (" + this.label + ") is not a decimal number");
			bError = true;
			break;
		}

		if (mivalue<0) {
			alert("Field (" + this.label + ") is not a positive number");
			bError = true;
			break;
		}
		break;
    case "date" :
		// Field debe ser una fecha
		if (controlValue.length != 10) {
			// La fecha debe ser de 10 caracteres
			alert ("Incorrect date format (" + this.label + ") incorrecto : (dd/mm/aaaa)");
			bError = true;
			break;
		}
		if ((controlValue.indexOf("/") !=2) || (controlValue.lastIndexOf("/") !=5)) {
			// Los separadores de la fecha no son correctos
			alert ("Incorrect date format (" + this.label + ") : (dd/mm/aaaa)");
			bError = true;
			break;
		}
		if (isNaN(controlValue.substring(6,10))) {
			// El año no es un numero
			alert ("Year incorrect (" + this.label + ")");
			bError = true;
			break;
		} 
		if (isNaN(controlValue.substring(3,5))) {
			// El mes no es un numero
			alert ("Month incorrect (" + this.label + ")");
			bError = true;
			break;
		}
		if ((parseInt(eval(controlValue.substring(3,5)))>12) || (parseInt(eval(controlValue.substring(3,5)))<1)) {
			// El numero de mes no es correcto
			alert ("The month should be between 1 and 12. (" + this.label + ")");
			bError = true;
			break;
		}
		if (isNaN(controlValue.substring(0,2))) {
			// El dia no es un numero
			alert ("Day incorrect (" + this.label + ")");
			bError = true;
			break;
		}
		if ((parseInt(eval(controlValue.substring(0,2)))>31) || (parseInt(eval(controlValue.substring(0,2)))<1)) {
			// El numero de dia no es correcto
			alert ("The day should be between 1 and 31  (" + this.label + ")");
			bError = true;
			break;
		}
		
		// Testeamos los años bisiestos
		var nDia=parseInt(eval(controlValue.substring(0,2)));
		var nMes=parseInt(eval(controlValue.substring(3,5))) -1 ;
		var nAnio=parseInt(eval(controlValue.substring(6,10)));
		var check_fecha = new Date (nAnio,nMes,nDia);
		if (nMes != check_fecha.getMonth()) {
			alert ("Date incorrect (" + this.label + ")");
			bError = true;
			break;
		}
    	break;
	case "monthy" :
		// Field debe ser una fecha
		if (controlValue.length != 7) {
			// La fecha debe ser de 10 caracteres
			alert ("Incorrect date format (" + this.label + ") : (mm/aaaa)");
			bError = true;
			break;
		}
		if ((controlValue.indexOf("/") !=2)) {
			// Los separadores de la fecha no son correctos
			alert ("Incorrect date format (" + this.label + ") : (mm/aaaa)");
			bError = true;
			break;
		}
		if (isNaN(controlValue.substring(3,7))) {
			// El año no es un numero
			alert ("Year incorrect (" + this.label + ")");
			bError = true;
			break;
		} 
		if (isNaN(controlValue.substring(0,2))) {
			// El mes no es un numero
			alert ("Month incorrect (" + this.label + ")");
			bError = true;
			break;
		}
		if ((parseInt(eval(controlValue.substring(0,2)))>12) || (parseInt(eval(controlValue.substring(0,2)))<1)) {
			// El numero de mes no es correcto
			alert ("The month should be between 1 and 12. (" + this.label + ")");
			bError = true;
			break;
		}
		
		// Testeamos los años bisiestos
		/*var nDia=parseInt("01");
		var nMes=parseInt(eval(controlValue.substring(0,2))) -1 ;
		var nAnio=parseInt(eval(controlValue.substring(3,7)));
		var check_fecha = new Date (nAnio,nMes,nDia);
		if (nMes != check_fecha.getMonth()) {
			alert ("El dia " + controlValue.substring(0,2) + " no existe para el mes " + controlValue.substring(3,5));
			bError = true;
			break;
		}*/
    	break;
    case "hour" :
		// El texto debe ser un campo de hora
		if (controlValue.length <4) {
			// La hora debe tener una longitud minima de 4 caracteres
			alert ("Incorrect hour format : hh:mm ó h:mm (" + this.label + ")");
			bError = true;
			break;
		}
		if (controlValue.lastIndexOf(":") != controlValue.length - 3) {
			// El separador de hora no esta en la posicion correcta
			alert ("Incorrect hour format : hh:mm ó h:mm (" + this.label + ")");
			bError = true;
			break;
		}
		if (controlValue.lastIndexOf(":") > 2) {
			// El separador de hora no esta en la posicion correcta
			alert ("Incorrect hour format : hh:mm ó h:mm (" + this.label + ")");
			bError = true;
			break;
		}
		
		if (isNaN(controlValue.substring(0,controlValue.lastIndexOf(":")))) {
			// Las horas deben ser un numero
			alert ("Number of hours incorrect (" + this.label + ")");
			bError = true;
			break;
		}
		
		if (isNaN(controlValue.substring(controlValue.lastIndexOf(":")+1))) {
			// Los minutos deben ser un numero
			alert ("Number of minutes incorrect (" + this.label + ")");
			bError = true;
			break;
		}
		
		// Testeamos el numero de horas y minutos en un rango horario si este puede serlo
		var nMinutos = parseInt(controlValue.substring(controlValue.lastIndexOf(":")+1));
		if (controlValue.length <= 5) {
			var nHoras = parseInt(controlValue.substring(0,controlValue.lastIndexOf(":")));
			// Si la hora es de un rango horario debe estar entre 0 y 23
			
			if (nHoras < 0 || nHoras > 23) {
				alert("The number of hours should be between 0 and 23");
				bError = true;
				break;
			}
		}
		if (nMinutos < 0 || nMinutos > 59) {
			// Los minutos no pueden exceder de 59
			alert("The number of minutes should be between 0 and 59 (" + this.label + ")");
			bError = true;
			break;
		}
		break;
  }

  if (bError) {
	// Error: posicionamos el cursor en Field y lo seleccionamos para que lo modifiquen
	this.select();
	//this.focus();
	return false;
  }
  else 
	return true;
}

//_________________________________________________________________________________________//

function containsHTMLCode(oControl) {
	
	

  var sValue = oControl.value;
  // checking "
  if (sValue.indexOf('"') != -1) {
		alert('Character \" not allowed (' + this.label + ')');
		oControl.select();
		oControl.focus();
		return false;
  }
  
  // checking "
  if (sValue.indexOf('<') != -1) {
		alert('Character < not allowed (' + this.label + ')');
		oControl.select();
		oControl.focus();
		return false;
  }

  if (sValue.indexOf('>') != -1) {
		alert('Character < not allowed (' + this.label + ')');
		oControl.select();
		oControl.focus();
		return false;
  }


  // checking "
  if (sValue.toUpperCase().indexOf('&QUOT') != -1) {
		alert ("La secuencia de caracteres \""+ sValue + "\" no está permitida (" + this.label + "): Es una secuencia de escape que usa la página HTML");
		oControl.select();
		oControl.focus();
		return false;
  }

  // checking "
  if (sValue.toUpperCase().indexOf('&AMP') != -1) {
		alert ("La secuencia de caracteres \""+ sValue + "\" no está permitida (" + this.label + "): Es una secuencia de escape que usa la página HTML");
		oControl.select();
		oControl.focus();
		return false;
  }




  // checking &
  re = /&[a-zA-z]*;/gi;
  
  if (sValue.search(re) >= 0) 
  {
	alert ("La secuencia de caracteres \""+ sValue.match(re) + "\" no está permitida (" + this.label + "): podría ser una secuencia de escape que usa la página HTML");
	oControl.select();
	oControl.focus();
	return false;
  }
  
  return true;
}
