// JavaScript Document

function validaForm(f) {
	
	if ( f.nombre.value == "" ) {
		alert("Tu nombre es obligatorio.")
		f.nombre.focus()
		return false
	}
	
	if ( f.telefono.value == "" ) {
		alert("El teléfono es obligatorio.")
		f.telefono.focus()
		return false
	}
	
	
			
	//Comprueba que se introduzcan los telefonos
		
	var tamTelf = 9
	if ( f.telefono.value!="" && (isNaN(f.telefono.value) || f.telefono.value.length != tamTelf) ) {
		  alert("Por favor, escribe los " + tamTelf + " d&iacute;gitos de tu teléfono fijo.")
		  f.telefono.focus()
		  return false
	}
	
     if ( f.provincia.value == "" ) {
		alert("Debes seleccionar una provincia")
		f.provincia.focus()
		return false
	}
	
	
	if( f.email.value.length == 0 || !validamail(f.email.value) ) {
		alert("El e-mail parece incorrecto.");
	    f.email.focus()
		return false
    }
	
	if ( f.articulo.value == "" ) {
		alert("El articulo es obligatorio.")
		f.articulo.focus()
		return false
	}
	
	if ( f.marca.value == "" ) {
		alert("La marca es obligatoria.")
		f.marca.focus()
		return false
	}
	
	if ( f.modelo.value == "" ) {
		alert("El modelo es obligatorio.")
		f.modelo.focus()
		return false
	}
	
	if ( f.ano.value == "" ) {
		alert("El año es obligatorio.")
		f.ano.focus()
		return false
	}

    if ( f.mensaje.value == "" ) {
		alert("El mensaje es obligatorio.")
		f.mensaje.focus()
		return false
	}
	
					
	// Todo OK, enviamos el formulario
	return true
}


function validamail(email){

	// Mínimo de 5 caracteres
	if (email.length < 5)
		return false
		
	// Cadena de caracteres no permitidos
	var iChars = "+*|,\":<>[]{}`';()&$#% ";	
	
	// Primero comprobamos que en el email no haya algún 
	// caracter no permitido
	var eLength = email.length;	
	for (var i=0; i < eLength; i++)	{		
		if (iChars.indexOf(email.charAt(i)) != -1)
			return false
	}	
	
	// Comprobamos que la @ tenga algún caracter delante y alguno detrás
	var atIndex = email.lastIndexOf("@");	
	if(atIndex < 1 || (atIndex == eLength - 1))
		return false

	// Comprobamos que exista '.' a partir del cuarto carácter, pero
	// que no acabé en '.'
	var pIndex = email.lastIndexOf(".");	
	if(pIndex < 3 || (pIndex == eLength - 1))	
		return false;	

	// Por último, comprobamos que el punto esté detrás de la @
	if(atIndex > pIndex)	
		return false	

	return true
}


function isValidDate(dateStr) {
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (dateStr==""){
return true
}
if (matchArray == null) {
alert("El formato de la Fecha debe ser dd/mm/aa")
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[1];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("El mes debe estar entre 1 y 12.");
return false;
}
if (day < 1 || day > 31) {
alert("El D&iacute;a debe estar entre 1 y 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Este mes no tiene 31 d&iacute;as!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("Febrero " + year + " no tiene " + day + " dias!");
return false;
}
}
if (year < 07) {
alert("El año debe ser mayor o igual a 2007")
return false
}

var daynew = new Date();

/*if (year > daynew.getYear()) {
alert("La fecha no puede ser superior a la actual")
return false
}

if (month > (daynew.getMonth()+1) && year == daynew.getYear()) {
alert("La fecha no puede ser superior a la actual")
return false
}

if (day > (daynew.getDate()) && month == (daynew.getMonth()+1) && year == daynew.getYear()) {
alert("La fecha no puede ser superior a la actual")
return false
}
*/

return true; // date is valid
}
