/* efecto animación fade in página */
$(document).ready(function(){
    $("body").fadeIn(700);
});
	
/* Validación formulario de contacto */

function validar_formulario() {
		/* Campos obligatorios */
		var f= document.datos;
			if  (f.nombre.value== "" )
				 {
					  f.nombre.style.border="solid 1px red"; f.nombre.value="Campo obligatorio";
					  return;
				 }
		if  (f.email.value== "" )
				 {
					  f.email.style.border="solid 1px red"; f.email.value="Campo obligatorio";
					  return;
				 }
				 
		if  (f.mensaje.value== "" )
				 {
					  f.mensaje.style.border="solid 1px red"; f.mensaje.value="Introduce tu mensaje";
					  return;
				 }
	
/* Comprueba si la direccion de email tiene @ y un punto detras de la @ */		
		var email= f.email.value;
		var posicion_a=email.indexOf("@");
			if (posicion_a == -1) 
			{
				f.email.style.border="solid 1px red";
				f.email.value="Campo obligatorio";
				return;
				
			}else{
				var dominio=email.substring(posicion_a);
				if (dominio.indexOf (".") == -1)
					{
						f.email.style.border="solid 1px red";
						f.email.value="Campo obligatorio";
						return;
					}
				}
				
		alert ("Su mensaje ha sido enviado con éxito");
				
		f.submit();
	}


// ********************************************************************************************
// Funciones para realizar operaciones con forms
// ********************************************************************************************

// Remove first and last spaces of a string value
function trim(str){ return str.replace(/^\s*|\s*$/g,""); }

// Check all conditions from form
function checkAll(type){
	switch (type){
		case 1:	return (checkMandatory() && checkIsNumeric() && checkMaxLength() && checkMinLength()); break;
		case 2: return confirm(msg[7]); break;
		case 3: return confirm(msg[8]); break;		
	}
}

// Check all mandatory form fields
function checkMandatory(){
	if (mandatoryFields != null && mandatoryFields.length > 0){
    	for (i = 0; i < mandatoryFields.length; i++){
	        field = document.getElementById(mandatoryFields[i]);
	        if (field != null){
	        	if (field.type.toLowerCase() == 'text' || 
		        	field.type.toLowerCase() == 'password' ||
	        		field.tagName.toLowerCase() == 'textarea'){
			        if (trim(field.value).length == 0){ alert(msg[0]); field.focus(); return false; }
			        
				} else if (field.tagName.toLowerCase() == 'select'){
			        if (field.selectedIndex == 0){ alert(msg[0]); field.focus(); return false; }				
				}
	        }
		}
	}
	
	return true;
}

// Check the max length form fields
function checkMaxLength(){
	if (maxLengthFields != null && maxLengthFields.length > 0){
    	for (i = 0; i < maxLengthFields.length; i++){
    		element = maxLengthFields[i];
    		if (element != null && element.length == 2){
		        field = document.getElementById(element[0]);
		        if (field != null && field.value.length > element[1]){ alert(msg[1]); field.focus(); return false; }
		    }
		}
	}
	
	return true;
}

// Check the max length form fields
function checkMinLength(){
	if (minLengthFields != null && minLengthFields.length > 0){
    	for (i = 0; i < minLengthFields.length; i++){
    		element = minLengthFields[i];
    		if (element != null && element.length == 2){
		        field = document.getElementById(element[0]);
		        if (field != null && field.value.length < element[1]){ alert(msg[10].replace("{0}", element[1])); field.focus(); return false; }
		    }
		}
	}
	
	return true;
}

// Check if a field is numeric
function checkIsNumeric(){
	if (isNumericFields != null && isNumericFields.length > 0){
    	for (i = 0; i < isNumericFields.length; i++){
	        field = document.getElementById(isNumericFields[i]);
	        if (field != null){
		        if (isNaN(field.value)){ alert(msg[2]); field.focus(); return false; }
		    }
		}
	}
	
	return true;
}

// Check if a field is a date
function checkIsDate(){
	if (isDateFields != null){
    	for (i = 0; i < isDateFields.length; i++){
	        field = document.getElementById(isDateFields[i]);
	        if (field != null){
		        if(!checkDate(field)) return false;
		    }
		}
	}
	
	return true;
}

// Check if a field is an hour
function checkIsHour(){
	if (isHourFields != null){
    	for (i = 0; i < isHourFields.length; i++){
	        field = document.getElementById(isHourFields[i]);
	        if (field != null){
		        if(!checkHour(field)) return false;
		    }
		}
	}
	
	return true;
}

// Check the content of a date field
//		- checkToday == 0: do nothing
//		- checkToday == -1: check date before current day
//		- checkToday == 1: check date after current day
function checkDate(field, checkToday){

	var aDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	try{
		if(field.value != ""){
			var s = field.value.split("/");
			var today = new Date();
			var day, month, year;
			
			if (s.length == 3){
				day = s[0];
				month = s[1] - 1;
				year = s[2];
				
			} else if (s.length == 1){			
				day = field.value.substr(0,2);
				monthStr = field.value.substr(2,2);
				month = parseInt(field.value.substr(2,2), 10) - 1;
				year = field.value.substr(4);
				if (field.value.length == 6)	year = "20" + year;	
				field.value = day + "/" + monthStr + "/" + year;
						
			} else {
				throw 0;
			}

			aDays[1] = (((parseInt(year, 10) % 4 == 0) && ( (!(parseInt(year, 10) % 100 == 0)) || (parseInt(year, 10) % 400 == 0))) ? 29 : 28 );

			if (parseInt(month, 10) < 0 || parseInt(month, 10) > 11) throw 0;
			if (parseInt(day, 10) < 1 || parseInt(day, 10) > parseInt(aDays[month], 10)) throw 0;

			if (isNaN(day) || isNaN(month) || isNaN(year)) throw 0;
			
			var date = new Date();
			date.setDate(day);
			date.setMonth(month);
			date.setFullYear(year);
			
			if (checkToday > 0 && today > date) throw 1;
			if (checkToday < 0 && today < date) throw 2;  
		}
		
		return true;
		
	} catch (ex){
		if(ex == 1) alert(msg[3]);
		else if(ex == 2) alert(msg[4]);
		else alert(msg[5]);
		field.value = "";
		field.focus();
		
		return false;
	}
}

// Check the format of an hour field
function checkHour(field){
	try {		
		var value = field.value;
		if (value.length == 0) return true;
		var aBase = value.split(":");
		if (aBase.length != 2) throw "error";
		var hour = aBase[0];
		var minuts = aBase[1];
		if (isNaN(hour)) throw "error";
		if (isNaN(minuts)) throw "error";
		hour = parseInt(hour, 10);
		minuts = parseInt(minuts, 10);
		if(hour < 0 || hour > 23) throw "error";
		if(minuts < 0 || minuts > 59) throw "error";
		
		return true;
		
	} catch (ex){
		alert(msg[6]);
		field.focus();
		field.select();
		return false;
	}
}


// Check a valid Fiscal Number (NIF)
function checkNIF(field){
	try {
		var value = field.value;
		if (value.length == 0) return true;

		var regexp1 = new RegExp(/\d{8}[a-z]{1}/i);			// NIF
		var regexp2 = new RegExp(/[a-z]{1}\d{8}[a-z]{1}/i);	// NIE	
		
		if (regexp1.test(value) || regexp2.test(value)){
			field.value = value.toUpperCase();
		} else {
			throw "error";
		}
		
		
	} catch (ex){
		alert(msg[11]);
		field.focus();
		field.select();
		return false;		
	}
}

// Set the option of a select (id) with de value passed
function setSelectIndex(id, value){
	var select = document.getElementById(id);
	if (select != null){
		var selectedIndex = 0;
		for (i == 0; i < select.length; i++){
			if (value == select.options[i].value) selectedIndex = i;
		} 
		select.selectedIndex = selectedIndex;
	}
}

// ********************************************************************************************



			

