
function popup(idioma) {
	window.open(idioma+'.html','Idioma','WIDTH=480,HEIGHT=300');
}

function showErros() {
	if(document.getElementById("listaErros").style.display=="block") {
		document.getElementById("listaErros").style.display = "none";
		document.getElementById("detalhes").innerHTML = "Exibir detalhes";
	}else{
		document.getElementById("listaErros").style.display = "block";
		document.getElementById("detalhes").innerHTML = "Ocultar detalhes";
	}
}

function addParticipante() {
	document.forms[0].action = "addParticipante";
	document.forms[0].submit();
}

function removerParticipante(n) {
	document.getElementById("_action_removeParticipante").value = "Remover #"+n;
	document.forms[0].submit();
}

function mesmoParticipante(obj) {
	if(obj.checked)
		document.getElementById("divPart0").style.display = "none";
	else
		document.getElementById("divPart0").style.display = "block";
}


function validaFormPF() {
	// validando campos em branco
	var camposTmp;
	var msgCamposTmp;
	var obj;
	
	// nome
	if(campoVazio("nome","Nome"))
		return false;

	// rg
	if(campoVazio("rg","RG"))
		return false;

	// cpf
	if(!valida_cpf(document.getElementById('cpf').value)) {
		alert("O CPF informado não é válido.");
		document.getElementById('cpf').focus();
		return false;
	}

	// Data nascimento
	if(campoVazio("nascimento","Data de Nascimento"))
		return false;

	//sexo
	radios = document.getElementsByName("custom[sexo]");
	var tmp = 1;
	var i;

	for(i=0;i<radios.length;i++) {
		if(radios[i].name=="custom[sexo]")
			if(radios[i].checked)
				tmp++;
	}

	if(tmp==1) {
		alert("Você deve selecionar a opção Sexo");
		return false;
	}
	
	
	// endereco, numero, bairro, cidade
	camposTmp = new Array("endereco","numero","bairro","cidade");
	msgCamposTmp = new Array("Endereço","Número","Bairro","Cidade");
	for(var i in camposTmp) {
		if(IsNumber(i))
			if(campoVazio(camposTmp[i],msgCamposTmp[i])) {		
				return false;
			}
	}
	
	// estado
	if(selectVazio("estado","Estado"))
		return false;
	
	// cep
	obj = document.getElementById("cep");
	if(!IsNumber(obj.value)) {
		alert("O campo CEP deve ser preenchido e conter apenas números.");
		obj.value="";
		obj.focus();
		return false;
	}
		
	// ddd, telefone, celular, email
	camposTmp = new Array("dddFixo","telefoneFixo","dddCelular", "telefoneCelular", "email");
	msgCamposTmp = new Array("DDD do telefone","Telefone","DDD do celular", "Celular","E-mail");
	for(var i in camposTmp) {
		if(IsNumber(i))
			if(campoVazio(camposTmp[i],msgCamposTmp[i])) {	
				return false;
			}
	}
	
	// campo de acordo
	if(!document.getElementById("deacordo").checked) {
		alert('Você deve estar de acordo com as condições de pagamento e políticas de cancelamento do evento.');
		document.getElementById("deacordo").focus();
		return false;	
	}
	return true;
}

function validaFormPJ() {
	// validando campos em branco
	var camposTmp;
	var msgCamposTmp;
	var obj;
	
	// razao, fantasia, atividade
	camposTmp = new Array("fantasia","empresa","atividade");
	msgCamposTmp = new Array("Nome Fantasia","Razão Social","Atividade da Empresa");
	for(var i in camposTmp) {
		if(IsNumber(i))
			if(campoVazio(camposTmp[i],msgCamposTmp[i])) {		
				return false;
			}
	}
	
	// cnpj
	if(!cgc(document.getElementById('cnpj').value)) {
		alert("O CNPJ informado não é válido.");
		document.getElementById('cnpj').focus();
		return false;
	}
	
	// endereco, numero, bairro, cidade
	camposTmp = new Array("endereco","numero","bairro","cidade");
	msgCamposTmp = new Array("Endereço","Número","Bairro","Cidade");
	for(var i in camposTmp) {
		if(IsNumber(i))
			if(campoVazio(camposTmp[i],msgCamposTmp[i])) {		
				return false;
			}
	}
	
	// estado
	if(selectVazio("estado","Estado"))
		return false;
	
	// cep
	obj = document.getElementById("cep");
	if(!IsNumber(obj.value)) {
		alert("O campo CEP deve ser preenchido e conter apenas números.");
		obj.value="";
		obj.focus();
		return false;
	}
	
	// pais
	if(selectVazio("pais","País"))
		return false;
	
	// Responsavel pela nota
	// nome
	if(campoVazio("nome","Nome do Responsável"))
		return false;

	// rg
	if(campoVazio("rg","RG"))
		return false;
	
	// cpf
	if(!valida_cpf(document.getElementById('cpf').value)) {
		alert("O CPF informado não é válido.");
		document.getElementById('cpf').focus();
		return false;
	}

	// Data nascimento
	if(campoVazio("nascimento","Data de Nascimento"))
		return false;
	
	//sexo
	radios = document.getElementsByName("custom[sexo]");
	var tmp = 1;
	var i;

	for(i=0;i<radios.length;i++) {
		if(radios[i].name=="custom[sexo]")
			if(radios[i].checked)
				tmp++;
	}
	if(tmp==1) {
		alert("Você deve selecionar a opção Sexo");
		return false;
	}

	// cargo, departamento, telefone, celular e-mail
	camposTmp = new Array("cargo","departamento","dddFixo","telefoneFixo","dddCelular","telefoneCelular","email");
	msgCamposTmp = new Array("Cargo","Departamento","DDD do telefone","Telefone","DDD do celular","Celular","E-mail");
	for(var i in camposTmp) {
		if(IsNumber(i))
			if(campoVazio(camposTmp[i],msgCamposTmp[i])) {	
				return false;
			}
	}
	
	
	//-------------------- validando dados dos participante
	var totalParticipantes = document.getElementById('totalParticipantes').value;
	var participante;
	if(document.getElementById("responsavelParticipa").checked)
		participante=1;
	else
		participante = 0;
		
	var numero;
	for(participante;participante<totalParticipantes;participante++) {
		numero = participante+1;
		// nome
		if(campoVazio("participantes["+participante+"].nome","Nome do "+numero+"º participante"))
			return false;
		
		// rg
		if(campoVazio("participantes["+participante+"].rg","RG do "+numero+"º participante"))
			return false;
		
		// cpf
		if(!valida_cpf(document.getElementById('participantes['+participante+'].cpf').value)) {
			alert("O CPF do "+numero+"º participante não é válido.");
			document.getElementById('participantes['+participante+'].cpf').focus();
			return false;
		}
		
		// Data nascimento
		if(campoVazio('participantes['+participante+'].nascimento',"Data de Nascimento do "+numero+"º participante"))
			return false;
		
		//sexo
		radios = document.getElementsByName("participantes["+participante+"].sexo");
		var tmp = 1;
		var i;
	
		for(i=0;i<radios.length;i++) {
			if(radios[i].name=="participantes["+participante+"].sexo")
				if(radios[i].checked)
					tmp++;
		}
	
		if(tmp==1) {
			alert("Você deve informar o Sexo do "+numero+"º participante");
			return false;
		}
		
		// cargo, departamento, telefone, celular, email
		camposTmp = new Array('participantes['+participante+'].cargo','participantes['+participante+'].departamento','participantes['+participante+'].dddFixo', 'participantes['+participante+'].telefoneFixo','participantes['+participante+'].dddCelular', 'participantes['+participante+'].telefoneCelular', 'participantes['+participante+'].email');
		msgCamposTmp = new Array("Cargo do "+numero+"º participante","Departamento do "+numero+"º participante","DDD do "+numero+"º participante","Telefone do "+numero+"º participante","DDD do Celular do "+numero+"º participante","Celular do "+numero+"º participante", "E-mail do "+numero+"º participante");
		for(var i in camposTmp) {
			if(IsNumber(i))
				if(campoVazio(camposTmp[i],msgCamposTmp[i])) {		
					return false;
				}
		}		
		
	}
	
	
	//-------------------- validando endereco para emissao da nota
	// Verifica se e o mesmo endereço e copia os valores dos campos
	/*
	if(document.getElementById("mesmo_enderecoS").checked==true) {
		document.getElementById("enderecoNota").value = document.getElementById("endereco").value;
		document.getElementById("numeroNota").value = document.getElementById("numero").value;
		document.getElementById("complementoNota").value = document.getElementById("complemento").value;
		document.getElementById("bairroNota").value = document.getElementById("bairro").value;
		document.getElementById("cidadeNota").value = document.getElementById("cidade").value;
		document.getElementById("estadoNota").options[document.getElementById("estado").selectedIndex].selected = true;
		document.getElementById("cepNota").value = document.getElementById("cep").value;
		document.getElementById("paisNota").options[document.getElementById("pais").selectedIndex].selected = true;
		
		document.getElementById("nomeNota").value = document.getElementById("nome").value;
		document.getElementById("cargoNota").value = document.getElementById("cargo").value;
		document.getElementById("departamentoNota").value = document.getElementById("departamento").value;
		document.getElementById("dddNota").value = document.getElementById("dddFixo").value;
		document.getElementById("telefoneNota").value = document.getElementById("telefoneFixo").value;
		document.getElementById("ramalNota").value = document.getElementById("ramalFixo").value;
		document.getElementById("emailNota").value = document.getElementById("email").value;
		
		
		radios = document.getElementsByName("custom[sexo]");
		radiosNota = document.getElementsByName("custom[sexoNota]");
		var tmp = 1;
		var i;
	
		for(i=0;i<radios.length;i++) {
			if(radios[i].name=="custom[sexo]")
				if(radios[i].checked) {
					for(var j=0;j<radiosNota.length;j++) {
						if(radiosNota[j].name=="custom[sexoNota]")
							if(radiosNota[j].value == radios[i].value) {
								radiosNota[j].checked = true;
								break;
							}
					}
				}
		}
	}
	
	//-------------------- endereço da nota diferente
	if(document.getElementById("mesmo_enderecoN").checked==true) {
		// endereco, numero, bairro, cidade
		camposTmp = new Array("enderecoNota","numeroNota","bairroNota","cidadeNota");
		msgCamposTmp = new Array("Endereço","Número","Bairro","Cidade");
		for(var i in camposTmp) {
			if(IsNumber(i))
				if(campoVazio(camposTmp[i],msgCamposTmp[i])) {		
					return false;
				}
		}
		
		// estado
		if(selectVazio("estadoNota","Estado"))
			return false;
		
		// cep
		obj = document.getElementById("cepNota");
		if(!IsNumber(obj.value)) {
			alert("O campo CEP deve ser preenchido e conter apenas números.");
			obj.value="";
			obj.focus();
			return false;
		}
		
		// pais
		if(selectVazio("paisNota","País"))
			return false;
	
		// nome
		if(campoVazio("nomeNota","Nome do Responsável"))
			return false;
			
		//sexo
		radios = document.getElementsByName("custom[sexoNota]");
		var tmp = 1;
		var i;
	
		for(i=0;i<radios.length;i++) {
			if(radios[i].name=="custom[sexoNota]")
				if(radios[i].checked)
					tmp++;
		}
		if(tmp==1) {
			alert("Você deve selecionar a opção Sexo");
			return false;
		}
		
		// cargo, departamento, telefone, email
		camposTmp = new Array("cargoNota","departamentoNota","dddNota","telefoneNota","emailNota");
		msgCamposTmp = new Array("Cargo","Departamento","DDD do telefone","Telefone","E-mail");
		for(var i in camposTmp) {
			if(IsNumber(i))
				if(campoVazio(camposTmp[i],msgCamposTmp[i])) {	
					return false;
				}
		}
	}
	*/
	
	//-------------------- campo de acordo
	if(!document.getElementById("deacordo").checked) {
		alert('Você deve estar de acordo com as condições de pagamento e políticas de cancelamento do evento.');
		document.getElementById("deacordo").focus();
		return false;	
	}
	document.forms[0].submit();
}

function desativaEnter (event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		validaFormPJ();
		return false;
	}
}


function campoVazio(campo,label) {
	var obj = document.getElementById(campo);
	if(obj.value == "") {
		alert("O campo "+label+" deve ser preenchido.");
		obj.focus();
		return true;
	}
	return false;
}

function selectVazio(campo,label) {
	var obj = document.getElementById(campo);
	if(obj.options[obj.selectedIndex].value==""){
		alert("Você deve selecionar um valor para o campo "+label);
		obj.focus();
		return true;
	}
	return false;
}

function outroEnderecoNota(obj) {
	if(obj.checked) {
		if(obj.value == "S")
			document.getElementById("tblOutroEnderecoNota").style.display = "none";
		else
			document.getElementById("tblOutroEnderecoNota").style.display = "block";
	}
}


// --- Fun&ccedil;&atilde;o de Verifica&ccedil;&atilde;o de n&uacute;meros inteiros ---
function IsNumber(x) {	
	x = '1' + x
	return x!='1'?(parseInt(x).toString().length==x.length?parseInt(x).toString().substring(1,parseInt(x).toString().length):NaN):NaN
}

// cgc(cnpj)
// Verifica&ccedil;&atilde;o de CNPJ
function cgc(cnpj) {
	if (cnpj.length != 14) {
		return false;
	}
	if(!IsNumber(cnpj))
		return false;
		
	m2 = 2;
	soma1 = 0;
	soma2 = 0;
	for (i=11;i>=0;i--) {
		val = eval(cnpj.charAt(i));
		m1 = m2;
		if (m2<9) {
			m2 = m2+1;
		} else {
			m2 = 2;
		}
		soma1 = soma1 + (val * m1);
		soma2 = soma2 + (val * m2);
	}
	soma1 = soma1 % 11;
	if (soma1 < 2) {
		d1 = 0;
	} else {
		d1 = 11 - soma1;
	}
	soma2 = (soma2 + (2 * d1)) % 11
	if (soma2 < 2) {
		d2 = 0;
	} else {
		d2 = 11 - soma2;
	}
	if ((d1==cnpj.charAt(12)) && (d2==cnpj.charAt(13))) {
		return true;
	} else {
		return false;
	}
}

// valida_cpf
function valida_cpf(cpf) {
    var numeros, digitos, soma, i, resultado, digitos_iguais;
    digitos_iguais = 1;
    if (cpf.length < 11)
          return false;
    for (i = 0; i < cpf.length - 1; i++)
          if (cpf.charAt(i) != cpf.charAt(i + 1))
                {
                digitos_iguais = 0;
                break;
                }
    if (!digitos_iguais)
          {
          numeros = cpf.substring(0,9);
          digitos = cpf.substring(9);
          soma = 0;
          for (i = 10; i > 1; i--)
                soma += numeros.charAt(10 - i) * i;
          resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
          if (resultado != digitos.charAt(0))
                return false;
          numeros = cpf.substring(0,10);
          soma = 0;
          for (i = 11; i > 1; i--)
                soma += numeros.charAt(11 - i) * i;
          resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
          if (resultado != digitos.charAt(1))
                return false;
          return true;
          }
    else
          return false;
}

function formatar(campo,teclapres) { 
	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
	if( tecla != 9 && tecla != 8 ){
		if( tam > 2 && tam < 5 )
			campo.value = vr.substr( 0, tam - 2 ) + '/' + vr.substr( tam - 2, tam );
		if( tam >= 5 && tam <= 10 )
			campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function soNumeros(v){
    return v.replace(/\D/g,"")
}

function data(v){
	v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
	v=v.replace(/(\d{2})(\d)/,"$1/$2") //Coloca hífen entre o quarto e o quinto dígitos
	v=v.replace(/(\d{2})(\d)/,"$1/$2") //Coloca hífen entre o quarto e o quinto dígitos
	return v
}
