// Javascript para el formulario de valoracion.

function MarcarCampoComment(IDcampo)
{
    var x = 1;
    var ctl = document.getElementById("campo" + x);
    while (ctl != null)
    {
        if (x == IDcampo)
        {
            ctl.className = "txt_sel";
        }
        else
        {
            ctl.className = "txt";
        }
        x++;
        ctl = document.getElementById("campo" + x);
    }
}

function ValidarFormComment()
{
	error = false
    if(document.getElementById("campo1").value == "")
	{
		InvalidarCampoComment("1");
		error = true;
	}
	
	if(document.getElementById("campo3").value == "")
	{
		InvalidarCampoComment("3");
		error = true;
	}
	
	if(document.getElementById("campo4").value == "")
	{
		InvalidarCampoComment("4");
		error = true;
	}
	
	
	err_f=error;
	
	validarMailComment();
	
	if (err_f==true)error=true;
	
	if(error == true)
	{
		return (false);
	} else {
		return (true);
	}
}

function validarMailComment() {
	error = false;
	email = document.getElementById("campo2").value;
	if (email==null)
	{
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	}
	if (email.length==0)
	{
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	}
	if (! caracteresValidosComment(email))
	{  // verifica que todos los caracteres sean validos
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	}
	if (email.indexOf("@") < 1)
	{ //  debe conteneder @, y no debe ser el primer caracter
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	} else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // el ultimo punto debe estar despues de @
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	} else if (email.indexOf("@") == email.length) {  // @ no debe ser el ultimo caracter
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	} else if (email.indexOf("..") >=0) { // dos puntos de seguido no son correctos
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	} else if (email.indexOf(".") == email.length) {  // punto no puede ser el caracter final
		error = true;
		mensajeEmailComment("error");
		InvalidarCampoComment("2");
	} else {
		mensajeEmailComment("ok");
	}
	if (email == "")
	{
		error = true;
		mensajeEmailComment("error2");
		InvalidarCampoComment("2");
	}
		
	return error;
}

function mensajeEmailComment(tipo) {
	msg = document.getElementById("vacioCampo2");
	if(tipo == "error")	{
		msg.style.color = "#ff0000";
		msg.innerHTML = "Dirección de E-mail no válida";
	} else if(tipo == "ok") {
		msg.style.display = "none";
	} else if(tipo == "error2") {
		msg.style.color = "#000000";
		msg.innerHTML = "Obligatorio";
	}
}
	
function caracteresValidosComment(email) {
  var valido = true;
  var permitidos = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
	var letra = email.charAt(i).toLowerCase();
	if (permitidos.indexOf(letra) != -1)
	  continue;
	valido = false;
	break;
  }
  return valido;
}

function InvalidarCampoComment(id)
{
	document.getElementById("campo" + id).className = "txt_error";
}

function VerificarCampoComment(id)
{
	if(document.getElementById("campo" + id).value != "")
	{
		document.getElementById("vacioCampo" + id).style.display = "none";
	} else {
		document.getElementById("vacioCampo" + id).style.display = "inline";
	}
}
