/* 

    Document   : funcoes.js
    Created on : 06/01/2010, 08:36:10
    Author     : Acao Direta Web Studio
    Description: Funcoes - Content 
	
*/


// Link Externo - não use target="_blank" (Y)
    $(function(){
		$("a[rel=externo]").attr('target','_blank');
	});


// Esta função "chama" todas as funções usadas em um documento.
function init() {
	createExternalLinks();
}


// FORÇA LINKS A ABRIREM EM UMA NOVA JANELA
//******************************************************************************
// A W3C não aceita mais o atributo "target" em links (tag <a>) em doctypes XHTML 1.0 Strict
// A função abaixo faz com que todos os links que possuem rel="externo" no link, abram em outra janela conforme no exemplo abaixo:
// EXEMPLO:
//******************************************************************************
//<a href="index.php" title="Link de Exemplo" rel="externo">

/* Para chamar a função, insira no body  == <script type="text/javascript">createExternalLinks();</script>  */

function createExternalLinks() {
    if(document.getElementsByTagName) {
        var anchors = document.getElementsByTagName('a');
        for(var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if(anchor.getAttribute("href") && anchor.getAttribute('rel')=='externo') { // <-- É necessário inserir rel="externo" no link
                anchor.target = '_blank';
            }
        }
    }
}

//........................................................................
//........................................................................


// Esta função "chama" todas as funções usadas em um documento.
//========================================================
// Event Listener by Scott Andrew - http://scottandrew.com
// edited by Mark Wubben, <useCapture> is now set to false
//http://simon.incutio.com/archive/2004/05/26/addLoadEvent
//========================================================
function addEvent(obj, evType, fn){
    if(obj.addEventListener){
        obj.addEventListener(evType, fn, false);

        return true;

    } else if (obj.attachEvent){
        var r = obj.attachEvent('on'+evType, fn);

        return r;

    } else {
        return false;

    }
}

addEvent(window, "load", init);

function createOption(dados) {
   var option = document.createElement("option");
   var textNd = document.createTextNode(dados[1]);
   
   option.setAttribute("value", dados[0]);
   option.appendChild(textNd);
   
   return option;
}

function buscaAtividades(id_select, segmento) {
   //var string_url = "http://www.meucarrao.com.br/misc/busca-marcas/?tipo=" + idTipo;
   var string_url = "http://www.acaodireta.com.br/site/guerar/admin/ajax-atividades.php?segmento=" + segmento;
   //var string_url = "http://www.acaodireta.com.br/site/meucarrao/misc/busca-marcas/?tipo=" + idTipo;
   var option = document.createElement("option");
   
   select = document.getElementById(id_select);
   select.innerHTML = "";
      
   option.setAttribute("value", "");
   option.appendChild(document.createTextNode("Carregando..."));
   select.appendChild(option);
   
   $.ajax({type:'get', async:false, url:string_url, success:function(res) {
         res = eval(res);
         select.innerHTML="";

         for (var i=0; i<res.length; i++) {
            option = createOption(res[i]);
            select.appendChild(option);
         }
      }
   });
}

function mostraCondicoes()
{
	var divCondicoes = document.getElementById("condicoes");
	
	divCondicoes.style.display = "block";
}

function fechaCondicoes()
{
	var divCondicoes = document.getElementById("condicoes");
	
	divCondicoes.style.display = "none";
}

function mostraAcidentesPessoais()
{
	var divAcidentes = document.getElementById("acidentes");
	
	divAcidentes.style.display = "block";
}

function fechaAcidentesPessoais()
{
	var divAcidentes = document.getElementById("acidentes");
	
	divAcidentes.style.display = "none";
}

function mostraSeguroResidencial()
{
	var divSeguro = document.getElementById("seguro");
	
	divSeguro.style.display = "block";
}

function fechaSeguroResidencial()
{
	var divSeguro = document.getElementById("seguro");
	
	divSeguro.style.display = "none";
}

function mostraConsultaCadastro()
{
	var divConsultaCadastro = document.getElementById("consulta_cadastro");
	
	divConsultaCadastro.style.display = "block";
}

function fechaConsultaCadastro()
{
	var divConsultaCadastro = document.getElementById("consulta_cadastro");
	
	divConsultaCadastro.style.display = "none";
}

function mostraBeneficiosEmpresas()
{
	var divBeneficiosEmpresas = document.getElementById("beneficios_empresas");
	
	divBeneficiosEmpresas.style.display = "block";
}

function fechaBeneficiosEmpresas()
{
	var divBeneficiosEmpresas = document.getElementById("beneficios_empresas");
	
	divBeneficiosEmpresas.style.display = "none";
}

function mostraMore()
{
	var divMore = document.getElementById("more");
	
	divMore.style.display = "block";
}

function marcaAba1()
{
	$("ul.passos li, ul.botoes li").click(function() {
	
		$("ul.passos li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".passo_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return true;
	});
}

function marcaAba2()
{	
	var plano1 = document.getElementById("plano_1");
	var plano2 = document.getElementById("plano_2");
	var plano3 = document.getElementById("plano_3");
	var plano4 = document.getElementById("plano_4");
	var plano5 = document.getElementById("plano_5");
	
	$('ul.passos li, ul.botoes li').unbind('click');
	
	if((plano1.checked == false) && (plano2.checked == false) && (plano3.checked == false) && (plano4.checked == false) && (plano5.checked == false))
	{
		alert('Selecione um plano contratado!');
		return false;
	}
	else
	{
		$("ul.passos li, ul.botoes li").click(function() {
	
			$("ul.passos li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(".passo_content").hide(); //Hide all tab content
	
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return true;
		});
	}
}

function marcaAba3(form)
{
	msg = new Array();

	formElements = $(form).map(function(){ return $.makeArray(this.elements); });
	formElements.each(function(i){
		a = $(this);
		nomeObj = a.attr('name');
		idObj 	= a.attr('id');
		if (a.hasClass("required_email") ) {
			b=a.prev('input:first')
			if(!validaEmail(a.val())){
				a.val();
				a.addClass('campo_requer');
				b.addClass('campo_requer');
				msg.push('?   ' + a.attr('title'));
			} else {
				a.removeClass('campo_requer');
				b.removeClass('campo_requer');
			}
		}else if(a.hasClass("required")) {
			b=a.prev('input:first')
			if((a.val() == null) || (a.val() == "")){
				a.addClass('campo_requer');
				b.addClass('campo_requer');
				msg.push('?   ' + a.attr('title'));
			} else {
				a.removeClass('campo_requer');
				b.removeClass('campo_requer');
			}
		}
	});
	
	$('ul.passos li, ul.botoes li').unbind('click');
	
	if(document.getElementById("d1_nome").value != "")
	{
		if(document.getElementById("d1_nascimento").value == "")
		{
			msg = '1';
			alert('Digite a data de nascimento do dependente 1!');
		}
	}
	
	if(document.getElementById("d2_nome").value != "")
	{
		if(document.getElementById("d2_nascimento").value == "")
		{
			msg = '1';
			alert('Digite a data de nascimento do dependente 2!');
		}
	}
	
	if(document.getElementById("d3_nome").value != "")
	{
		if(document.getElementById("d3_nascimento").value == "")
		{
			msg = '1';
			alert('Digite a data de nascimento do dependente 3!');
		}
	}
	
	if(document.getElementById("d4_nome").value != "")
	{
		if(document.getElementById("d4_nascimento").value == "")
		{
			msg = '1';
			alert('Digite a data de nascimento do dependente 4!');
		}
	}
	
	if(document.getElementById("d5_nome").value != "")
	{
		if(document.getElementById("d5_nascimento").value == "")
		{
			msg = '1';
			alert('Digite a data de nascimento do dependente 5!');
		}
	}
	
	if(msg.length==0) {
		
		$("ul.passos li, ul.botoes li").click(function() {
	
			$("ul.passos li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(".passo_content").hide(); //Hide all tab content
	
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return true;
		});
	}
	else
	{
		alert('Verifique os erros informados:\n\n' + msg.join('\n'));
		return false;
	}
}

function valida_cpf()
{
	var cpf = document.getElementById("cpf").value;
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	if (cpf.length < 11)
	{
		alert('Digite um CPF válido!');
		document.getElementById("cpf").value = '';
	}
	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))
		{
			alert('Digite um CPF válido!');
			document.getElementById("cpf").value = '';
		}
		
		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))
		{
			alert('Digite um CPF válido!');
			document.getElementById("cpf").value = '';
		}
		
		return true;
	}
	else
	{
		alert('Digite um CPF válido!');
	    document.getElementById("cpf").value = '';
	}
}

function checkPlano1()
{
	var radioPlano1 = document.getElementById("plano_1");
	
	radioPlano1.checked = true;
}

function checkPlano2()
{
	var radioPlano2 = document.getElementById("plano_2");
	
	radioPlano2.checked = true;
}

function checkPlano3()
{
	var radioPlano3 = document.getElementById("plano_3");
	
	radioPlano3.checked = true;
}

function checkPlano4()
{
	var radioPlano4 = document.getElementById("plano_4");
	
	radioPlano4.checked = true;
}

function checkPlano5()
{
	var radioPlano5 = document.getElementById("plano_5");
	
	radioPlano5.checked = true;
}

function verificaAction()
{
	var formLoginAdmin = document.getElementById("formLoginAdmin");
	var adminOdonto = document.getElementById("adminOdonto");
	var adminRedesul = document.getElementById("adminRedesul");
	
	if(adminOdonto.checked)
	{
		formLoginAdmin.action = '/admin/verifica.php';
	}
	else
	{
		formLoginAdmin.action = 'http://www.redesulsaude.com.br/admin/login';
	}
}

function verificaPagamento()
{
	var pagamento_1 = document.getElementById("forma_pagamento_1");
	var p1_numero = document.getElementById("p1_numero");
	
	var pagamento_2 = document.getElementById("forma_pagamento_2");
	var p2_banco = document.getElementById("p2_banco");
	var p2_agencia = document.getElementById("p2_agencia");
	var p2_conta = document.getElementById("p2_conta");
	
	var pagamento_3 = document.getElementById("forma_pagamento_3");
	var p3_numero = document.getElementById("p3_numero");
	
	if(pagamento_1.checked)
	{
		if (p1_numero.value == '')
		{
			alert('Digite o número da conta!');
			return false;
		}
	}
	
	if(pagamento_2.checked)
	{
		if (p2_banco.value == '')
		{
			alert('Digite o nome do banco!');
			return false;
		}
		if (p2_banco.value == '')
		{
			alert('Digite o nome do banco!');
			return false;
		}
		if (p2_agencia.value == '')
		{
			alert('Digite o número da agência!');
			return false;
		}
		if (p2_conta.value == '')
		{
			alert('Digite o número da conta!');
			return false;
		}
	}
	
	if(pagamento_3.checked)
	{
		if (p3_numero.value == '')
		{
			alert('Digite o número do cartão!');
			return false;
		}
	}
	
	return true;
}

function mostraUm()
{
	var divUm = document.getElementById("um");
	var divDois = document.getElementById("dois");
	var divTres = document.getElementById("tres");
	
	divUm.style.display = "block";
	divDois.style.display = "none";
	divTres.style.display = "none";
}

function mostraDois()
{
	var divUm = document.getElementById("um");
	var divDois = document.getElementById("dois");
	var divTres = document.getElementById("tres");
	
	divUm.style.display = "none";
	divDois.style.display = "block";
	divTres.style.display = "none";
}

function mostraTres()
{
	var divUm = document.getElementById("um");
	var divDois = document.getElementById("dois");
	var divTres = document.getElementById("tres");
	
	divUm.style.display = "none";
	divDois.style.display = "none";
	divTres.style.display = "block";
}
