function OnlyNumbers(evt) 
{
    evt = evt ? evt : window.event;
    var charCode = evt.which ? evt.which : evt.keyCode;
    if ((charCode > 31 && (charCode < 48 || charCode > 57)) &&
        charCode != 177) {
        alert("Utilize somente números");
        return false;
    }
    return true;
}
function mascara(e, src, mask) 
{
    if (window.event) {
        var _TXT = e.keyCode;
    } else if (e.which) {
        var _TXT = e.which;
    }
    if (_TXT > 47 && _TXT < 58) {
        var i = src.value.length;
        var saida = mask.substring(0, 1);
        var texto = mask.substring(i);
        if (texto.substring(0, 1) != saida) {
            src.value += texto.substring(0, 1);
        }
        return true;
    } else {
        if (_TXT != 8) {
            return false;
        } else {
            return true;
        }
    }
}
function verificaOutros(src)
	{
	if(src.value == 4)
		{
		document.getElementById('outrohidden').style.visibility = "visible";
		}
	else
		{
		document.getElementById('outrohidden').style.visibility = "hidden";	
		document.getElementById('outro').value = "";	
		}
	}

function validaCpf(frmCpf) 
{
	if(frmCpf.txtCpf.value == '')
	{
	alert("Digite o seu CPF!");
    frmCpf.txtCpf.focus();
    return false;	
	}
}

var checkdate = function(value)
		{
			var date = new Array();
			
			value = value.replace(/\D/g,"/");
			
			var validDate = function(day, month, year)
			{
		        return day < 1 || day > 31 ? false 
						: month < 1 || month > 12 ? false
						: /4|6|9|11/.test(month) && day == 31 ? false
						: (month == 2 && (day > ((year = !(year % 4) && (year % 1e2) || !(year % 4e2)) ? 29 : 28)) ? 1 + !!year : 0) > 0 ? false
						: true;
			};
			
			if(/^([0-9]{1,2})[\/]([0-9]{1,2})[\/]([0-9]{2,4})$/.test(value))
			{
				date = value.split("/");
				return validDate(parseInt(date[0],10),parseInt(date[1],10),parseInt(date[2],10));
			}
			else
				return false;
		};

function validaForm(theForm) 
	{
	if((theForm.turnomanha.checked == false) && (theForm.turnonoite.checked == false))
		{
		alert("Preencha corretamente o Turno!");
        theForm.turnomanha.focus();
        return false;	
		}
    if ((theForm.cpf.value == "") || (theForm.cpf.value.length < 14))
		{
		alert("Preencha corretamente seu CPF!");
		theForm.cpf.focus();
		return false;
		}
	if (theForm.nome.value == "")
		{
		alert("Preencha corretamente o Nome!");
		theForm.nome.focus();
		return false;
		}
	if ((theForm.endereco.value == "") || (theForm.endereco.value.length < 10))
		{
		alert("Preencha corretamente o Endereço!");
		theForm.endereco.focus();
		return false;
		}
	if (theForm.bairro.value == "")
		{
		alert("Preencha corretamente o Bairro!");
		theForm.bairro.focus();
		return false;
		}
	if (theForm.bairro.value == "")
		{
		alert("Preencha corretamente o Bairro!");
		theForm.bairro.focus();
		return false;
		}
	if ((theForm.cep.value == "") || (theForm.cep.value.length < 9))
		{
		alert("Preencha corretamente o CEP!");
		theForm.cep.focus();
		return false;
		}
	if ((theForm.cidade.value == "") || (theForm.cidade.value.length < 3))
		{
		alert("Preencha corretamente a Cidade!");
		theForm.cidade.focus();
		return false;
		}
	if (theForm.uf.value == "")
		{
		alert("Preencha corretamente o Estado!");
		theForm.uf.focus();
		return false;
		}
	if ((theForm.dddresidencial.value == "") || (theForm.dddresidencial.value.length < 2))
		{
		alert("Preencha corretamente o Telefone!");
		theForm.dddresidencial.focus();
		return false;
		}
	if ((theForm.numeroresidencial.value == "") || (theForm.numeroresidencial.value.length < 9))
		{
		alert("Preencha corretamente o Telefone!");
		theForm.numeroresidencial.focus();
		return false;
		}
		
	var padrao	= /^(([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?)$/;

	if ((theForm.email.value == "") || !(padrao.test(theForm.email.value)))
	{
		alert("Preencha corretamente o E-mail!");
		theForm.email.focus();
		return false;
	}
	if ((theForm.rg.value == "") || (theForm.rg.value.length < 7))
	{
		alert("Preencha corretamente o RG!");
		theForm.rg.focus();
		return false;
	}
	if ((theForm.datanascimento.value == "") || (theForm.datanascimento.value.length < 10))
	{
		alert("Preencha corretamente a Data de Nascimento!");
		theForm.datanascimento.focus();
		return false;
	}
	if (!checkdate(theForm.datanascimento.value))
	{
		alert("Data de Nascimento inválida!");
		theForm.datanascimento.focus();
		return false;
	}
	if((theForm.radIndicacaoSim.checked == false) && (theForm.radIndicacaoNao.checked == false))
	{
		alert("Preencha corretamente o campo de Indica褯 ção de Curso!");
		theForm.turnomanha.focus();
		return false;	
	}
	if (theForm.radIndicacaoSim.checked == true) 
		{
			if ((theForm.nomeIndicado.value == "") || (theForm.emailIndicado.value == ""))
			{
				alert("Preencha corretamente os dados de Nome e E-mail de quem indicou!");
				theForm.nomeIndicado.focus();
				return false;				
			}
			if ((theForm.emailIndicado.value != "") && !(padrao.test(theForm.emailIndicado.value)))
			{
				alert("Preencha corretamente o e-mail de quem indicou!");
				theForm.emailIndicado.focus();
				return false;
			}	
		}		
	if (theForm.tipoconhecimentounidade.value == "")
	{
		alert("Preencha corretamente a pergunta 01!");
		theForm.tipoconhecimentounidade.focus();
		return false;
	}
	if (theForm.tipomotivounidade.value == "")
	{
		alert("Preencha corretamente a pergunta 02!");
		theForm.tipomotivounidade.focus();
		return false;
	}
	if (theForm.tipomotivounidade.value == "4")
	{
		if (theForm.outro.value == "")
		{
			alert("Preencha corretamente o Outro Motivo!");
			theForm.outro.focus();
			return false;
		}
	}
	if((theForm.necessidadeespecialsim.checked == false) && (theForm.necessidadeespecialnao.checked == false))
	{
		alert("Preencha corretamente a pergunta 03!");
		theForm.necessidadeespecialsim.focus();
		return false;	
	}
	if (theForm.necessidadeespecialsim.checked == true) 
		{
		if ((theForm.NecessidadeEspecialQual.value == ""))
			{
				alert("Preencha corretamente os dados de sua necessidade especial!");
				theForm.NecessidadeEspecialQual.focus();
				return false;				
			}	
		}				
		
		
	if(theForm.aceito.checked == false)
	{
		alert("É necessário ler e aceitar o Edital de Abertura do Processo Seletivo!");
		theForm.aceito.focus();
		return false;	
	}
	if(theForm.declaracaoEnsinoMedio.checked == false)
	{
		alert("É necessário a confirmação que você está estudando, ou tenha concluído o Ensino Médio!");
		theForm.declaracaoEnsinoMedio.focus();
		return false;	
	}
	theForm.gravar.disabled = true;
	loading('Enviando Email e Gravando dados....<br/>Aguarde...');
	return true;
	
	}
function validaForm2(theForm) 
{

	if ((theForm.nome.value == "") || (theForm.nome.value.length < 2))
	{
		alert("Você precisa digitar o nome!");
		theForm.nome.focus();
		return false;
	}
	if ((theForm.ddd.value == "") || (theForm.ddd.value.length < 2))
	{
		alert("Você precisa digitar o telefone!");
		theForm.ddd.focus();
		return false;
	}
	if ((theForm.telefone.value == "") || (theForm.telefone.value.length < 8))
	{
		alert("Você precisa digitar o telefone!");
		theForm.telefone.focus();
		return false;
	}
	
	var padrao	= /^(([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?)$/;

	if ((theForm.email.value == "") || !(padrao.test(theForm.email.value)))
	{
		alert("Preencha corretamente o E-mail!");
		theForm.email.focus();
		return false;
	}
	if ((theForm.comentario.value == "") || (theForm.comentario.value.length < 5))
	{
		alert("Você precisa digitar uma mensagem!");
		theForm.comentario.focus();
		return false;
	}
	
	theForm.enviar.disabled = true;
	loading('Enviando Email...<br/>Aguarde...');
	return true;
	
}

//verifica se um cpf é válido
function cpfValido(valor) 
{
	cpf = valor;
	if (cpf.length < 11) 
	{
		return false;
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf)) 
	{
		return false;
	}
	if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
	{
		return false
	}
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++)
	{
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] * --c);
	}
	if ((x = b % 11) < 2) 
	{
		a[9] = 0;
	}
	else
	{
		a[9] = 11-x;
	}
	b = 0;
	c = 11;
	for (y=0; y<10; y++)
		b += (a[y] * c--);
	if ((x = b % 11) < 2)
	{
		a[10] = 0;
	}
	else
	{
		a[10] = 11-x;
	}
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]))
	{
		return false;
	}
	return true;
}

function validarFrmCpf()
{
	var formulario = "#frmCpf";
	
	if($(formulario).length > 0)
	{
		$(formulario).submit(function(){
			var expreg = /[ .\/\-]+/g;
			var valor = $("#txtCpf").val();
			valor = valor.replace(expreg, "");
			if ($("#txtCpf").val().length > 0 && !/_/.test($("#txtCpf").val()))
			{
				var result = cpfValido(valor);
				if(!result)
				{
					alert("Digite um CPF válido!");
				}
			}
			else
			{
				alert("Digite o CPF!");
				result = false;
			}
			return result;
		});
	}
}

function loading(msg) {
    document.getElementById("carregando").innerHTML = msg;
    document.getElementById("carregando").style.visibility = "visible";
	hiddenSelects();
    expand();
}
var root = 'http://ci/fgvfaculdade/';
function hiddenSelects()
	{
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	if ((browser=="Microsoft Internet Explorer") && (version<=6))
		{
		if(document.createStyleSheet) 
			{
				document.createStyleSheet('comum/css/hidden.css');
			}
			
			else 
			{
				var styles = "@import url( '" + root + "/comum/css/hidden.css ');";
				var newSS=document.createElement('link');
				newSS.rel='stylesheet';
				newSS.href='data:text/css,'+escape(styles);
				document.getElementsByTagName("head")[0].appendChild(newSS);
			}
		}		
	}

var current = 0;
var current2 = 300;

function hidde(src)
	{
	src.style.visibility = "hidden";
	}
function expand() 
{
    if (current <= 300) 
	{
        current += 15;
        current2 += 10;
        setTimeout(expand, 1);
    }
	document.getElementById("carregando").style.height = current + "px";
    document.getElementById("carregando").style.width = current2 + "px";
    document.getElementById("carregando").style.marginLeft = "-" + current2 / 2 + "px";
    document.getElementById("carregando").style.marginTop = "-" + current / 2 + "px";
}

function SegundaVia()
{
	var formulario = "#frmSegundaVia";
	
	if($(formulario).length > 0)
	{		
		$(formulario).submit(function(){
			var expreg = /[ .\/\-]+/g;
			var valor = $("#txtCpf").val();
			valor = valor.replace(expreg, "");
			if ($("#txtCpf").val().length > 0 && !/_/.test($("#txtCpf").val()))
			{
				var result = cpfValido(valor);
				if(!result)
				{
					alert("Digite um CPF válido!");
				}
			}
			else
			{
				alert("Digite o CPF!");
				result = false;
			}
			return result;
		});
	}
}

