isOpera = (navigator.userAgent.toLowerCase().indexOf('opera')>-1);
isKonqueror = (navigator.userAgent.toLowerCase().indexOf('konqueror')>-1);
isIE  = ((!isOpera)&&(navigator.userAgent.toLowerCase().indexOf('msie')>-1));
isIE7 = ((!isOpera)&&(navigator.userAgent.toLowerCase().indexOf('msie 7')>-1));
isIE8 = ((!isOpera)&&(navigator.userAgent.toLowerCase().indexOf('msie 8')>-1));
isIE6 = ((isIE)&&(!isIE7)&&(!isIE8));

function SomenteNumero(even){
	
	if(document.all){
		tecla = event.keyCode;
	}else{
		tecla = even.which
	}
	
	if (tecla != 8 && tecla != 0){
		if (tecla < 48 || tecla > 57 ){	
			/*alert("Somente permitido digitar numeros!");	*/
			return false;
		}
	}
	
}

/**** PREENCHE DADOS DA TA *******/
function preenche_dados(ta, fantasia, razao_social, cnpj, endereco, cep, uf, fone, fax, funcionarios, ie, cidade){
	var f = document.frmEvento;
	
	f.TermoAdesao.value = ta;
	f.TermoAdesao.setAttribute('readOnly','readonly');
	f.Empresa.value = fantasia;
	f.Empresa.setAttribute('readOnly','readonly');
	f.NroFuncionarios.value = funcionarios;
	f.NroFuncionarios.setAttribute('readOnly','readonly');
	f.Endereco.value = endereco;
	f.Endereco.setAttribute('readOnly','readonly');
	f.Cep.value = cep;
	f.Cep.setAttribute('readOnly','readonly');
	f.Cidade.value = cidade;
	f.Cidade.setAttribute('readOnly','readonly');
	f.Estado.value = uf;
	f.Estado.setAttribute('readOnly','readonly');
	f.Fone.value = fone;
	f.Fone.setAttribute('readOnly','readonly');
	f.Fax.value = fax;
	f.Fax.setAttribute('readOnly','readonly');
	f.NFemNome.value = razao_social;
	f.NFemNome.setAttribute('readOnly','readonly');
	//f.Cnpj.value = cnpj;
	//f.Cnpj.disabled = true;
	f.InscEstadual.value = ie;
	f.InscEstadual.setAttribute('readOnly','readonly');
	//document.getElementById('Organizacoes').checked = true;
	
	f.Emailinsc.value = "";
	f.Emailinsc.removeAttribute('readOnly');
	f.participante_1.value = "";
	f.participante_1.removeAttribute('readOnly');
}

function preenche_dados2(nome, email, endereco, cep, estado, fone, cidade){
	var f = document.frmEvento;
	f.NFemNome.value = nome;
	f.NFemNome.setAttribute('readOnly','readonly');
	f.Endereco.value = endereco;
	f.Endereco.setAttribute('readOnly','readonly');
	f.Cep.value = cep;
	f.Cep.setAttribute('readOnly','readonly');
	f.Cidade.value = cidade;
	f.Cidade.setAttribute('readOnly','readonly');
	f.Estado.value = estado;
	f.Estado.setAttribute('readOnly','readonly');
	f.Emailinsc.value = email;
	f.Emailinsc.setAttribute('readOnly','readonly');
	f.Fone.value = fone;
	f.Fone.setAttribute('readOnly','readonly');
	f.participante_1.value = nome;
	f.participante_1.setAttribute('readOnly','readonly');
	
	f.TermoAdesao.value = "";
	f.TermoAdesao.removeAttribute('readOnly');
	f.Empresa.value = "";
	f.Empresa.removeAttribute('readOnly');
	f.NroFuncionarios.value = "";
	f.NroFuncionarios.removeAttribute('readOnly');
	f.Fax.value = "";
	f.Fax.removeAttribute('readOnly');
	f.InscEstadual.value = "";
	f.InscEstadual.removeAttribute('readOnly');
}
/********************/

/* MUDA TIPO DA PESSOA */

function selTipoPessoa() {
	if ( document.getElementById('tpf').checked ) {
		document.getElementById('tpPessoa').innerHTML = "CPF*:";
		document.getElementById('pessoaEscolhida').value = 1;
		document.getElementById('Cnpj').setAttribute('maxLength', 11);
                if(navigator.appName == 'Microsoft Internet Explorer'){
                    document.getElementById('campo_pj').style.display = "block";
                }else{
                    document.getElementById('campo_pj').style.display = "table-row";
                }
	}
	else if ( document.getElementById('tpj').checked ) {
		document.getElementById('tpPessoa').innerHTML = "CNPJ*:";
		document.getElementById('pessoaEscolhida').value = 2;
		document.getElementById('Cnpj').setAttribute('maxLength', 14);
                document.getElementById('campo_pj').style.display = "none";
	}	
}

function adiciona_span(aux) {
	var inc = parseInt(aux) + 1;
	
	document.getElementById('plus_father').innerHTML = '';
	document.getElementById('plus_father').innerHTML = "<input type=\"button\" name=\"add_campo_nome\" id=\"add_campo_nome\" onclick=\"adiciona_span("+inc+");\" value=\"Novo Participante\" style=\"font-size:9px; padding:1px;\">";
	
	var temp = document.getElementById('first_inc').value;
	temp++;
	
	var adicionais = new Array();
	for (var i=temp; i<=aux; i++) {
		if (document.getElementsByName("participante_" + i)[0] != undefined) {
			adicionais[i] = document.getElementsByName("participante_" + i)[0].value;
		}
	}

	var span = document.getElementById('spanx').innerHTML + "<span id='spanz_" + inc + "'><input type=\"text\" name=\"participante_"+ inc +"\" id=\"participante_"+ inc +"\" maxlength=\"255\" style=\"width:266px; margin-top:5px\" />&nbsp;&nbsp;<img src=\"img/publish_x.gif\" border=\"0\" onclick=\"limpa_span(" + inc + ");\" /><br /></span>";
	document.getElementById('spanx').innerHTML = span;

	for (var j in adicionais) {
		document.getElementsByName("participante_" + j)[0].value = adicionais[j];
	}

	document.getElementById('nspans').value = aux;
	
	var num_partic = parseInt(document.getElementById('num_particpantes').value);
	document.getElementById('num_particpantes').value = num_partic + 1 ;
	
	defineValor();
}

function limpa_span(aux) {
	var id = 'spanz_' + aux;
	document.getElementById(id).innerHTML = '';
	document.getElementById(id).style.visibility = 'hidden';
	
	var num_partic = parseInt(document.getElementById('num_particpantes').value);
	document.getElementById('num_particpantes').value = num_partic - 1 ;
	
	defineValor();
}

/* =============================================================== */

function digitaNumero(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	/*if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
		
	return true;*/
	if ((charCode >= 48 && charCode <= 57) || charCode == 8 || charCode == 9 || charCode == 13)
		return true;
		
	return false;
}

/* =============================================================== */

function isLetterKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode >= 48 && charCode <= 57)
 return false;

 return true;
}
	
/* =============================================================== */
	
function limpa_string(S) {
	// Deixa só os dígitos
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
   	for (var i=0; i<S.length; i++){
      digito = S.charAt(i);
      if (Digitos.indexOf(digito)>=0){temp=temp+digito}
   	}
   return temp
}

/* =============================================================== */

function valida_CPF(s) {
	var i;
	s = limpa_string(s);
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(10-i);
	}
        if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)
	{
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
   
   return true;
}

/* =============================================================== */

function valida_CGC(s)
{
	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
        if (d1 == 0) return false;
        d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)
	{
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
        return true;
}

/* =============================================================== */

function limpaForm() {
    document.frmEvento.reset();
    selTipoPessoa();
}

function validaForm(f) {
	visita = false;
	intVisita = 0;
	if (!testaVazio(f.participante_1, 'seu nome')){
		return false;
	}
	//if (!testaVazio(f.Empresa, 'o nome da Empresa')) return false;
	if (!testaEmail(f.Emailinsc)) return false;
	//if (!testaVazio(f.Cargo, 'o Cargo')) return false;
	//if (!testaVazio(f.NroFuncionarios, 'o Nº de Funcionários')) return false;
	if (!testaVazio(f.Endereco, 'o Endereco')) return false;
	if (!testaVazio(f.Cep, 'o CEP')) return false;
	if (!testaVazio(f.Cidade, 'a Cidade')) return false;
	if (!testaVazio(f.Estado, 'a UF')) return false;
	if (!testaVazio(f.Fone, 'o Telefone')) return false;
	//if (!validaRadio(f,'Participacao', 'uma forma de participação no evento')) return false;
	//if (f.formaParticipacao.value != "0"){
		//for(i = 0; i < f.visitaTecnica.length; i++){
			//visita = (f.visitaTecnica[i].checked || visita);
			//if(f.visitaTecnica[i].checked){
				//intVisita = i;
			//}
		//}
		//if(!visita){
			//alert("Escolha uma organização para realizar a visita técnica");
			//f.visitaTecnica[0].focus();
			//return false;
		//}
	//}
	if (!validaRadio(f,'Categoria','uma categoria')) return false;
	
	if (!validaRadio(f,'ci','os dias em que deseja participar do Congresso Internacional')) return false;
	
	if (f.TipoPessoa[0].checked) { 
	
    	//Pessoa Física
    	if (!testaVazio(f.NFemNome, 'o Nome para a Nota Fiscal')) return false;
    	if (!testaVazio(f.Cnpj, 'o CPF da Nota Fiscal')) return false;
    	//Verifica se CPF está correto
    	if (!valida_CPF(f.Cnpj.value)) {
    		alert("CPF inválido. Digite novamente.");		
    		f.Cnpj.focus();
    		return false;
    	}
	
	}if (f.TipoPessoa[1].checked) { 
		//Pessoa Jurídica
		if (!testaVazio(f.NFemNome, 'a Razao Social/Nome Fantasia')) return false;
		if (!testaVazio(f.Cnpj, 'o CPF/CNPJ')) return false;
		//Verifica se CGC está correto
		if (!valida_CGC(f.Cnpj.value)) {
			alert("CPF/CNPJ inválido. Digite novamente.");
			f.Cnpj.focus();
			return false;
		}
	}
	
	if (document.getElementById('Total').value.substr(0,1)!="0"){
	    if (!validaRadio(f,'forma_pagto','uma forma de pagamento')) return false;
	}

	return true;
}


/* =============================================================== */

function exibeObsM() {
	document.getElementById('obs_m').style.display = 'block';	
}

function apagaObs() {
	document.getElementById('obs_m').style.display = 'none';	
}

function Especificacategoria(vlr){
    document.getElementById('obs_congressista').style.display = "none";
    document.getElementById('obs_estudante').style.display = "none";
    document.getElementById('obs_patrocinador').style.display = "none";
    document.getElementById('obs_mpe').style.display = "none";
    
    switch (vlr) {
        case "ESTD":
            document.getElementById('obs_estudante').style.display = "block";
        break;
        case "CONG":
            document.getElementById('obs_congressista').style.display = "block";
        break;
        case "PTAP":
            document.getElementById('obs_patrocinador').style.display = "block";
        break;
        case "ESPM":
            document.getElementById('obs_mpe').style.display = "block";
        break;
    }
    
	document.getElementById('categoria_selecionada').value = vlr;
}

function EspecifiCI(vlr){
	document.getElementById('ci_selecionado').value = vlr;
}

function EspecificaSeminario(id){
	document.getElementById('seminario_selecionado').value = id;
}
function EspecificaSeminario2(id){
	document.getElementById('seminario2_selecionado').value = id;
}
function EspecificaSeminarioManha(id){
	document.getElementById('seminario_manha_selecionado').value = id;
}

function EspecificaSeminarioTarde(id){
	document.getElementById('seminario_tarde_selecionado').value = id;
}

function EspecificaVisita(id){
	document.getElementById('visita_selecionada').value = id;
}


var ValorComite = 0.00;
var ValorEspecial = 0.00;
var ValorPatrocinador1Dia = 0.00;
var ValorPatrocinador2Dias = 0.00;
var ValorEstudante = 50.00;
var ValorFundatec = 40.00;
var ValorExaminador = 50.00;
var ValorGesPublica = 330.00;
var ValorCongressista1Dia = 330.00;
var ValorSebrae = 347.00;
var ValorCongressista2Dias = 495.00;
var ValorEspecialMPE1Dia = 300.00;
var ValorEspecialMPE2Dias = 315.00;
var Valor1Visita = 110;
var ValorSeminario = 265.00;
var ValorSeminario2 = 0.00;
var SeminarioGratuito = '2903';
var SeminarioSaude = '2902'


function defineValor(){
    var categoria = document.getElementById('categoria_selecionada').value;
    switch (categoria) {
        case 'CMTS':
            calculaTotal(ValorComite);
            break;
        case 'PTAP':
            calculaTotal(ValorPatrocinador1Dia, ValorPatrocinador2Dias);
            break;
        case 'ESP':
            calculaTotal(ValorEspecial);
            break;
        case 'ESPM':
            calculaTotal(ValorEspecialMPE1Dia, ValorEspecialMPE2Dias);
            break;
        case 'GPUB':
            calculaTotal(ValorGesPublica);
            break;
        case 'ESTD':
            calculaTotal(ValorEstudante);
            break;
        case 'EXA':
            calculaTotal(ValorExaminador);
            break;
        case 'CONG':
            calculaTotal(ValorCongressista1Dia, ValorCongressista2Dias);
            break;
        case 'SEBRAE':
            calculaTotal(ValorSebrae);
            break;
        case 'FUNDATEC':
            calculaTotal(ValorFundatec);
            break;
    }

}

function calculaTotal(valor1Dia, valor2Dias) {
    if (valor2Dias==undefined) {
        valor2Dias = valor1Dia;
    }
    
    var CI = document.getElementById('ci_selecionado').value;
    var Categoria = document.getElementById('categoria_selecionada').value;
	var Seminario = document.getElementById('seminario_selecionado').value;
        var Seminario2 = document.getElementById('seminario2_selecionado').value;
	var Visita = document.getElementById('visita_selecionada').value;
	
	var Total = 0.00;
	
	var Participantes = contaParticipantes();
	var ParticipantesCongresso = Participantes;
	
	if (Categoria=="CONG" && Participantes > 9) {
        ParticipantesCongresso = Participantes - 1;
	}
	
	if (Categoria=="PTAP") {
        ParticipantesCongresso = Math.max(0, Participantes-5);
	}
	
	if (CI > 0 && CI != "N") {
		if (CI != 3) {
			Total = (CI==1 && Categoria=="GPUB") ? 0 : valor1Dia;
		} else {
			Total = valor2Dias;
		}
		Total = Total * ParticipantesCongresso;
	}
	
	if (Seminario > 0 && Seminario != "N") {
                switch(Seminario){
                    case '2897':
                        ValorSeminario = 20.00;
                        break;
                    default:
                        ValorSeminario = 265.00;
                }
		Total = parseFloat(Total + (ValorSeminario * Participantes));
	}
        if (Seminario2 > 0 && Seminario2 != "N") {
            switch(Seminario2){
                case '2902':
                    ValorSeminario2 = 130.00;
                    break;
                case '2903':
                    ValorSeminario2 = 0.00;
                    break;
            }
            Total = parseFloat(Total + (ValorSeminario2 * Participantes));
        }

	
	if (Visita > 0 && Visita != "N") {
	    Total = parseFloat(Total + (Valor1Visita * Participantes));
	}	
	showTotal(Total);
}

function contaParticipantes(){
	return parseInt(document.getElementById('num_particpantes').value);
}

/*
function CalculaVisitas() {
	check = document.forms[0].elements["visitas"];
	VlrVisita = 0.00;
    for(x = 0; x < check.length; x++) { 
        if (check[x].checked) { 
            VlrVisita = VlrVisita + Valor1Visita;
        } 
    }
    return VlrVisita;
}*/

function showTotal(Total) {
    if (Total>=0) {
        if(navigator.appName == 'Microsoft Internet Explorer'){
            document.getElementById('forma-pgto').style.display = "inline";
        }else{
            document.getElementById('forma-pgto').style.display = "table-row";
        }
        
    } else {
        document.getElementById('forma-pgto').style.display = "none";
    }
    document.getElementById('Total').value = Total + ',00';
    if(Total <= 0.00){
        document.getElementById('forma-pgto-opcoes').style.display = 'none';
    }else{
        if(navigator.appName == 'Microsoft Internet Explorer'){
            document.getElementById('forma-pgto-opcoes').style.display = "inline";
        }else{
            document.getElementById('forma-pgto-opcoes').style.display = "table-row";
        }
    }
}


/************************************************************************/

function execClick(){
    var value;
    alert(getElementById("Fundatec").checked);
    if(getElementById("Especial").checked){
        value = "ESP";
    }else if(getElementById("EspecialMPE").checked){
        value = "ESPM";
    }else if(getElementById("GestaoPublica").checked){
        value = "GPUB";
    }else if(getElementById("Examinador").checked){
        value = "ESTD";
    }else if(getElementById("Sebrae").checked){
        value = "SEBRAE";
    }else if(getElementById("Fundatec").checked){
        value = "FUNDATEC";
    }
    Especificacategoria(value);
    defineValor();
}

/************************************************************************/
