function validarFicha(formulario)
{
	formulario =  eval("document."+formulario)
	
	
	estado = 'true'
	// valido los tipos text y textarea
      for (j=0;j<formulario.elements.length && estado == 'true';j++){
      	// valido los input
      	
	     	if ((formulario.elements[j].type=='text') || (formulario.elements[j].type=='password') || (formulario.elements[j].type=='file') ){
      		estado = validaInput(formulario.elements[j])
	     		if (estado!="true")
      			formulario.elements[j].focus()
      	}
      	if (formulario.elements[j].type=='hidden'){
      		estado = validaHidden(formulario.elements[j])
      	}
      	if (formulario.elements[j].type=='textarea'){
      	 	estado = validaTextArea(formulario.elements[j])
	     		if (estado!="true")
      			formulario.elements[j].focus()
      	}
		if (formulario.elements[j].type=='select-one'){
      	 	estado = validaCombo(formulario.elements[j])
	     	if (estado!="true")
      			formulario.elements[j].focus()
      	}
      }
      return estado
      
}

function validaInput(elemento)
{
	if ((elemento.requerido=='si') && (elemento.value=='')){
	
		if (elemento.tipovalidacion!='fecha'){
			alert("Debe rellenar el campo " + elemento.titulo )
			return 0;
		}else{
			alert("Debe rellenar la fecha de " + elemento.titulo)
			return 0;
		}
	}	
		
	

	if ((elemento.requerido=='si') || (elemento.value!='')){
		switch (elemento.tipovalidacion)
		{
			case "email":
				
			      email=elemento.value
				if (email.length < 5){
					alert("Por favor, rellene el " + elemento.titulo +" correctamente");
		   			return 'false'
		   		}
		   		// si tiene un blanco o una #
				if ((email.indexOf("#") !=-1) || (email.indexOf(" ") !=-1) ) {
					alert("Por favor, rellene el " + elemento.titulo +" correctamente");
		   			return 'false'
				}
				//que tenga arroba y punto
		   		if ((email.indexOf("@") ==-1) || (email.indexOf(".") ==-1))   {
					alert("Por favor, rellene el " + elemento.titulo +" correctamente");
		   			return 'false'
				}
				break;
			case "euro":
				if (isNaN(Number(elemento.value))){
					alert("El campo '" + elemento.titulo + "' debe ser un número. Formato num.num");
					return 'false'
				}
				elemento.value = Number(elemento.value) * 100
				break;
			case "fecha":
			        
				fecha= new Date(elemento.value)

		      if (!isNaN(fecha)){
	
			if (elemento.value.substr(2,1)!= "/") {
				alert ("Debe introducir una fecha en formato dd/mm/yyyy")
				return 0;
			}	
		        if (elemento.value.substr(5,1)!= "/"){
		        	alert ("Debe introducir una fecha en formato dd/mm/yyyy")
		         	return 0;
		        } 	
		      
					dia= elemento.value.substr(0,2)
					mes= elemento.value.substr(3,2)
					anio=elemento.value.substr(6,4)
			
				        var daysInMonth = makeArray(12);
					daysInMonth[1] = 31;
					daysInMonth[2] = 29;
					daysInMonth[3] = 31;
					daysInMonth[4] = 30;
					daysInMonth[5] = 31;
					daysInMonth[6] = 30;
					daysInMonth[7] = 31;
					daysInMonth[8] = 31;
					daysInMonth[9] = 30;
					daysInMonth[10] = 31;
					daysInMonth[11] = 30;
					daysInMonth[12] = 31;
					
								
					if (!(mes > 0 && mes < 13))
					{
					alert ("Introduzca un valor para el mes entre 1 y 12");
					return 0;
					}
					if (!(dia > 0 && dia < 32))
					{
					alert ("Introduzca un valor para el día entre 1 y 31");
					return 0;
					}
					//parseInt() para quitar el 0 de delante
								
					if (dia > daysInMonth[parseInt(mes)])
					{
					alert ("Introduzca un valor para el día correcto")
					return 0;
					}
					if (!(anio > 1889 && anio < 2199))
					{
					alert ("Introduzca un valor para el año correcto");
					return 0;
					}
					if ((mes=="02") && dia > daysInFebruary(anio))
					{
					alert ("Fecha incorrecta para febrero")
					return 0;
					}
					
					}
				else
				  alert ("Debe introducir la fecha de " +  elemento.titulo  + " en formato dd/mm/yyyy")

				break;
			case "numerico":
				if (isNaN(Number(elemento.value))){
					alert("El campo '" + elemento.titulo + "' debe ser un número.");
					return 'false'
				}
				break;
			case "entero":
				var oRegExp = new RegExp("[^0-9]","i");
				if(oRegExp.test(elemento.value)){
					alert("El campo'" + elemento.titulo + "' debe ser un número entero. Formato num");
					return 'false';
				}
				break;
			case 	"alfanumerico":
				break;
			case 	"imagen":
				break;
			case 	"adjunto":
				break;
			case 	"documento":
				break;
			default:
				//alert(elemento.tipovalidacion)
		}
	}
	return 'true'
}

function validaTextArea(elemento)
{
	if ((elemento.requerido=='si') && (elemento.value==''))
	{
		alert("Debe rellenar el campo " + elemento.titulo )
		return 'false'
	}
	if ((elemento.max_car!='') && ((elemento.value).length>elemento.max_car))
	{
		alert("La longitud del campo " + elemento.titulo + " no puede sobrepasar los " + elemento.max_car +" caracteres" )
		return 'false'
	}
	return 'true'
}

function validaCombo(elemento){
	if (elemento.requerido=='si'){
		if(elemento.value==0){
			alert("Por favor, rellene el " + elemento.titulo);
			return 'false';
		}
	
	}
	return 'true';
}

function validaHidden(elemento)
{
	if (elemento.requerido=='si')
	{
		switch (elemento.tipovalidacion)
		{
		case 	"contenido":
	    		if (eval(formulario+"."+elemento.name+"_contenido.value")=="")
	    		{
	    			alert("Por favor, rellene el " + elemento.titulo + " correctamente");
	    			return 'false'
	    		}
			break;
		case 	"adjunto":
			if (elemento.value==elemento.valorNeutro)
			{
	    			alert("Por favor, adjunte el elemento " + elemento.titulo );
	    			return 'false'
			}
		case 	"editor":
			if (elemento.value=="")
			{
	    			alert("Por favor, rellene el texto " + elemento.titulo );
	    			return 'false'
			}


		}
	}
	return 'true'
}

function makeArray(n) {
			   for (var i = 1; i <= n; i++) {
			      this[i] = 0;
			   } 
			   return this;
			}
			
function daysInFebruary(anio) {
			    return (anio % 4 == 0 && (!(anio % 100 == 0)
			 || (anio % 400 == 0)) ? 29 : 28);
			}

//////////////////////////////////////////////////////////////////////////////////////////