
/*
Desenvolvido por Rodrigo Sieiro
http://www.sharpcube.com
*/

function OnlyNumbers(evt)
{
    var k = (evt.keyCode)? evt.keyCode : evt.which;
    
	if (k < 32)
	{
		return true;
	}

	if ((String.fromCharCode(k) < '0') || (String.fromCharCode(k) > '9'))
		return false;
	else
		return true;
}

function VerificaCPF (cpf) 
{
    cpf = cpf.replace(/\./g, "").replace(/-/g, "");
    
	if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") return false;
	add = 0;
	for (i=0; i < 9; i ++) add += parseInt(cpf.charAt(i)) * (10 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11) rev = 0;
	if (rev != parseInt(cpf.charAt(9))) return false;
	add = 0;
	for (i = 0; i < 10; i ++) add += parseInt(cpf.charAt(i)) * (11 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11) rev = 0;
	if (rev != parseInt(cpf.charAt(10))) return false;

	return true;
}

function ValidaForm()
{
	var elm;
	var errorList = "";

	// Documento
	elm = $('#documento');
	if (elm.val() == "")
	{
		elm.addClass("error");
		errorList += "<li>" + "Please fill your ID correctly." + "</li>";
	}
	else
	{
		// Valido o CPF
		if ($("#tipoCPF")[0].checked && !VerificaCPF(elm.val()))
		{
			elm.addClass("error");
			errorList += "<li>" + "This CPF number is invalid." + "</li>";
		}
		else
		{
			elm.removeClass("error");
		}
	}

	// Nome
	elm = $('#nome');
	if (elm.val() == "")
	{
		elm.addClass("error");
		errorList += "<li>" + "Please fill your Name correctly." + "</li>";
	}
	else
	{
		elm.removeClass("error");
	}

	// E-Mail
	elm = $('#email');
	if (elm.val() == "")
	{
		elm.addClass("error");
		errorList += "<li>" + "Please fill your e-mail address correctly." + "</li>";
	}
	else
	{
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
		// Verifico se o e-mail é válido
		if (!filter.test(elm.val()))
		{
			elm.addClass("error");
			errorList += "<li>" + "The e-mail you typed is invalid." + "</li>";
		}
		else
		{
			elm.removeClass("error");
		}
	}

	// Confirmação E-Mail
	elm = $('#emailConf');
	if (elm.val() != $('#email').val())
	{
		elm.addClass("error");
		errorList += "<li>" + "The e-mail confirmation field must match the e-mail you typed." + "</li>";
	}
	else
	{
		elm.removeClass("error");
	}

	// Forma de Inscrição
	elm = $('#forma');
	if (elm.val() == "" || elm.val() == "0")
	{
		elm.addClass("error");
		errorList += "<li>" + "Please select your registration type." + "</li>";
	}
	else
	{
		elm.removeClass("error");
	}

	// Verifico se tem Trabalho
	if ($('#forma').val() == "2")
	{
		// Título
		elm = $('#titulo');
		if (elm.val() == "")
		{
			elm.addClass("error");
			errorList += "<li>" + "Please fill your paper title correctly." + "</li>";
		}
		else
		{
			elm.removeClass("error");
		}

		// Tipo de Atividade
		elm = $('#tipoAtividade');
		if (elm.val() == "" || elm.val() == "0")
		{
			elm.addClass("error");
			errorList += "<li>" + "Please fill your type of presentation correctly." + "</li>";
		}
		else
		{
			elm.removeClass("error");
		}

		// Resumo
		elm = $('#resumo');
		if (elm.val() == "")
		{
			elm.addClass("error");
			errorList += "<li>" + "Please fill your paper abstract correctly." + "</li>";
		}
		else
		{
			elm.removeClass("error");
		}

		// Senha
		elm = $('#senha');
		if (elm.val() == "")
		{
			elm.addClass("error");
			errorList += "<li>" + "Please type a password for future system access." + "</li>";
		}
		else
		{
			elm.removeClass("error");
		}

		// Confirmação Senha
		elm = $('#senhaConf');
		if (elm.val() != $('#senha').val())
		{
			elm.addClass("error");
			errorList += "<li>" + "The password confirmation field must match the password you typed." + "</li>";
		}
		else
		{
			elm.removeClass("error");
		}
	}

	// Se teve erro, mostra
	if (errorList != "")
	{
		$("#listaErros").html(errorList);
		$("#errorForm").show();
		$.scrollTo(0, 800);
		return false;
	}

	$("#formDados").submit();
	return true;
}

function MudouForma()
{
	if ($('#forma').val() == "2")
	{
		$("#divTrabalho").show();
	}
	else
	{
		$("#divTrabalho").hide();
	}
}

function AlterarIdioma(atual, novo)
{
	var url = window.location.href;
	url = url.replace(atual, novo);

	window.location = url;
}