/*
	function highlight (obj)
	function dehighlight (obj)
	function check_empty_field (element)
	function check_number (element)
	function check_text (element)
	function check_mail (element)
	function check_select (element)
	function check_contacto (form)
	function check_busca_vehiculos ()
*/

function highlight (obj) {
	obj.style.backgroundColor='#ffffcc';
}
function dehighlight (obj) {
	obj.style.backgroundColor='#f7f7f7';
}

// funcion que comprueba campos que los campos obligatorios no están vacios
function check_empty_field (element)
{
	var elemento = eval('document.getElementById("' + element + '")');
	if (elemento.value.length==0)
	{
		empty_message = "- " + element + "\n" + empty_message;
		elemento.focus();
		elemento.select();
	}
}

// funcion que comprueba si los campos numéricos sólo contienen números
function check_number (element)
{
	var elemento = eval('document.getElementById("' + element + '")');
	longitud_elemento = elemento.value.length;
	valor_elemento = elemento.value;
	if (longitud_elemento!=0)
	{
		// comprobamos que el texto introducido es un número
		if ( isNaN(valor_elemento) == true )
		{
			number_message = "- " + element + "\n" + number_message;
			elemento.focus();
			elemento.select();
		}
	}
}

// funcion que comprueba si un campo de texto sólo contiene letras
function check_text (element)
{
	ok = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + " abcdefghijklmnñopqrstuvwxyzáéíóú";
	var elemento = eval('document.getElementById("' + element + '")');
	cad_nombre=elemento.value;
	for(i=0; i < cad_nombre.length ;i++)
	{
		if(ok.indexOf(cad_nombre.charAt(i))<0)
		{
			text_message = "- " + element + "\n" + text_message;
			elemento.focus();
			elemento.select();
			return;
		}	
	}
}

// funcion que comprueba si un campo de email tiene el formato adecuado
function check_mail (element)
{
	var elemento = eval('document.getElementById("' + element + '")');
	length_mail = 0;
	length_mail = elemento.value.length;
	length_mail = length_mail - 1;
	if ( (elemento.value.indexOf ('@', 0) < 1) || (elemento.value.length < 5) 
		|| (elemento.value.lastIndexOf(".") < 2) || (elemento.value.charAt(length_mail) == "." ) )
	{
		email_message = "- Formato de EMAIL incorrecto\n" + email_message;
		elemento.focus();
		elemento.select();
		return;
	}
}

// funcion que comprueba si un campo tipo select ha sido seleccionado
function check_select (element)
{
	var elemento = eval('document.getElementById("' + element + '")');
	if (elemento.value==0)
	{
		empty_message = "- " + element + "\n" + empty_message;
		elemento.focus();
	}
}

// Comprueba q todos los campos del formulario presupuesto han sido rellenados y están correctos
function check_contacto(form)
{
	empty_message = "";
  text_message = "";
	email_message = "";
	number_message = "";
	var formulario = eval('document.getElementById("' + form + '")');
	
  // Comprobamos si los campos obligatorios han sido rellenados
	check_empty_field('Email');
	check_empty_field('Apellidos');
	check_empty_field('Nombre');
	
  if (empty_message != "") {
    empty_message = "Los siguientes campos son obligatorios:\n\n" + empty_message;
    alert(empty_message);
    return;
  }

	// Comprobamos que los campos obligatorios tienen el formato adecuado
	check_text('Apellidos');
	check_text('Nombre');
  if (text_message != "") {
    text_message = "Los siguientes campos solo aceptan letras:\n\n" + text_message;
    alert(text_message);
    return;
  }

	// Comprobamos que el campo email está correcto
	check_mail('Email');
	if (email_message != "") {
		alert(email_message);
    return;
	}
	
	// Si los campos opcionales numericos han sido rellenados, comprobamos que sean correctos
	if (formulario.Telefono.value.length!=0 
	 || formulario.Fax.value.length!=0) {
		check_number('Fax');
		check_number('Telefono');
		if (number_message != "") {
			number_message = "El valor de siguientes campos son incorrectos:\n\n" + number_message;
			alert(number_message);
			return;
	  }
	}

  formulario.submit();
}

// Comprobamos que se ha seleccionado algun criterio de búsqueda y si es así enviamos el formulario
function check_busca_vehiculos ()
{
	if (document.getElementById("categoria").value=="" && document.getElementById("marca").value=="")
	{
		alert("Debe seleccionar algun criterio de busqueda");
		return false;
	}
	document.frm_busqueda_vehiculos.submit();
}

// funcion que comprueba el formulario de la NEWSLETTER antes de enviarlo
function enviar_newsletter()
{
	error_message = "";
	
	if (document.getElementById("email").value.length==0)
	{
		error_message = "- email\n" + error_message;
		document.getElementById("email").focus();
		document.getElementById("email").select();
	}

	if (document.getElementById("nombre").value.length==0)
	{
   	error_message = "- nombre\n" + error_message;
   	document.getElementById("nombre").focus();
		document.getElementById("nombre").select();
  }

	if (error_message != "")
	{
		error_message = "Los siguientes campos son obligatorios:\n\n" + error_message;
		window.alert(error_message);
		return;
	}
	
	length_mail = document.getElementById("email").value.length;
	length_mail = length_mail - 1;
	if ( (document.getElementById("email").value.indexOf ('@', 0) < 1) || (document.getElementById("email").value.length < 5) 
		|| (document.getElementById("email").value.lastIndexOf(".") < 2) || (document.getElementById("email").value.charAt(length_mail) == "." ) )
	{
   	alert("Escriba una dirección de correo válida en el campo \"E-mail\".");
		document.getElementById("email").focus();
		document.getElementById("email").select();
   	return;
  }
    
	enviar_mail();

}
