// JavaScript Document
//rellenar los cuadros de texto 
 function seleccionar()
{
	this.style.background='#90EE90';
}
function cambio()
{
	this.style.background='#FFFFFF';
}


//validar correo
function email()
{
valor = document.getElementById("correo").value;

if( !(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(valor)) ) {

  return false;
  
}
else
{
	return true;
}
}

//validar telefono
function telefono()
{
valor = document.getElementById("tfno").value;
if( !(/^\d{9}$/.test(valor)) ) {
	
  return false;
}
else 
{
	return true;
}
}
window.onclick=function ()
{
	document.getElementById("correo").onchange=email;
	document.getElementById("tfno").onchange=telefono;
	
}
//controlor el text area 
function limita(elEvento, maximoCaracteres) {
  var elemento = document.getElementById("texto");
 
  // Obtener la tecla pulsada 
  var evento = elEvento || window.event;
  var codigoCaracter = evento.charCode || evento.keyCode;
  // Permitir utilizar las teclas con flecha horizontal
  if(codigoCaracter == 37 || codigoCaracter == 39) {
    return true;
  }
 
  // Permitir borrar con la tecla Backspace y con la tecla Supr.
  if(codigoCaracter == 8 || codigoCaracter == 46) {
    return true;
  }
  else if(elemento.value.length >= maximoCaracteres ) {
    return false;
  }
  else {
    return true;
  }
}
 
function actualizaInfo(maximoCaracteres) {
  var elemento = document.getElementById("texto");
  var info = document.getElementById("info");
 
  if(elemento.value.length >= maximoCaracteres ) {
    info.innerHTML = "Máximo "+maximoCaracteres+" caracteres";
  }
  else {
    info.innerHTML = "Puedes escribir hasta "+(maximoCaracteres-elemento.value.length)+" caracteres adicionales";
  }
}

function validarnombre()
{
// NO DEJAR UN CAMPO SIN RELLENAR O BIEN A ESPACIOS EN BLANCO
valor = document.getElementById("nombre").value;
if( valor == null || valor.length == 0 || /^\s+$/.test(valor) ) {

  return false;
}
else 
{
	return true;
}
}

function validaremail()
{
// NO DEJAR UN CAMPO SIN RELLENAR O BIEN A ESPACIOS EN BLANCO
valor = document.getElementById("correo").value;
if( valor == null || valor.length == 0 || /^\s+$/.test(valor) ) {
  return false;
}
else 
{
	return true;
}
}

function validartelefono()
{
// NO DEJAR UN CAMPO SIN RELLENAR O BIEN A ESPACIOS EN BLANCO
valor = document.getElementById("tfno").value;
if( valor == null || valor.length == 0 || /^\s+$/.test(valor) ) {
  return false;
}
else 
{
	return true;
}
}



window.onload=function()
{
	var formulario=document.getElementById("formulario");
	var camposInput=formulario.getElementsByTagName("input");
	for(var i=0; i<camposInput.length; i++)
		if(camposInput[i].type=="text")
		{
			camposInput[i].onfocus=seleccionar;
			//camposInput[i].onchange=cambio;
			camposInput[i].onblur=cambio;
		}
	document.getElementById("enviar").onclick=validar;
}
function validar()
{
if(!(validarnombre()))
 {
	alert("Introduzca un nombre o empresa");
	return false;
 }

 else
 {
	if (!(validaremail())) 
	{
		if (!(validartelefono()))
		{
		alert("Introduzca un e-mail válido o teléfono");
		return false;
		}
	    else
	    {
		 if (!(telefono()))
	    {
			alert("El teléfono no es correcto");
			return false;
		}
		else
		{
	    // document.getElementById("formulario").submit();
		 alert("El formulario ha sido enviado a FP Burgos");
		 return true;
		}
		}
	}
else
{
	if (!(email()))
	    {
			alert("El e-mail no es correcto");
			return false;
		}
		else
		{
		//document.getElementById("formulario").submit();
		 alert("El formulario ha sido enviado a FP Burgos");
		 return true;
		}
}
}
}
