var bln;

function showSubmenu (nu_menu) {
	var obj;
	
	obj = document.getElementById("submenu_" + nu_menu);
	
	if (obj) {
		obj.style.display = "inline";
		bln = true;
	}
} 

function hide (nu_menu) {
	var obj;
	
	if (!bln) {
		obj = document.getElementById("submenu_" + nu_menu);
		
		if (obj) {
			obj.style.display = "none";
		}				
	}
} 

function hideAll(numItens)
{
	for (i=1; i <= numItens; i++)
	{
		hide(i);
	}
}

function hideSubmenu (nu_menu) {
	bln = false;
	setTimeout("hide(" + nu_menu + ")", 800);
}	

function SubmitaComboBuscaLojas (obj)
{
	location.href="?estado=" + obj[obj.selectedIndex].value;
}

function NavegaComboBuscaLojas (obj)
{
	location.href=obj[obj.selectedIndex].value;
}

/********** Fale Conosco *******/

function ValidaFaleConosco (frm)
{	
	with(frm)
	{
		if (tx_assunto.value == "")
		{
			alert("Selecione o Assunto!");
			tx_assunto.focus();
			return false;
		}
		
		if (tx_produto.value == "")
		{
			alert("Selecione o Produto!");
			tx_produto.focus();
			return false;
		}
		
		if (divFormulario.style.display == '')
		{
			if (tx_nome.value == "")
			{
				alert("Digite o seu nome!");
				tx_nome.focus();
				return false;
			}
			
			if (in_cliente_fininvest[0].checked) 
			{
				if (tx_cpf.value == "")
				{
					alert("Digite seu CPF!");
					tx_cpf.focus();
					return false;
				}
				else if (!ValidaCPF (tx_cpf.value))
				{
					alert("CPF Inválido!");
					tx_cpf.focus();
					return false;
				}
			}
			
			if (tx_email_cliente.value == "")
			{
				alert("Digite seu email!");
				tx_email_cliente.focus();
				return false;
			}
			else if (!ValidaEmail (tx_email_cliente.value))
			{
				alert("Email inválido!");
				tx_email_cliente.focus();
				return false;
			}
			
			if (tx_ddd.value == "")
			{
				alert("Digite o DDD do Telefone!");
				tx_ddd.focus();
				return false;
			}
			else if (!SoNumeros(tx_ddd.value))
			{
				alert("DDD Inválido!");
				tx_ddd.focus();
				return false;
			}
			
			if (tx_telefone.value == "")
			{
				alert("Digite seu Telefone!");
				tx_telefone.focus();
				return false;
			}
			else if (!SoNumeros(tx_telefone.value))
			{
				alert("Telefone Inválido!");
				tx_telefone.focus();
				return false;
			}
			
			if (!( (in_cliente_fininvest[0].checked) || (in_cliente_fininvest[1].checked) ))
			{
				alert("Selecione a opção Cliente Fininvest (S/N)!");
				return false;
			} 
			
			if (!( (in_preferencia_atendimento[0].checked) || (in_preferencia_atendimento[1].checked) ))
			{
				alert("Selecione a preferência de atendimento (Telefone/Email) !");
				return false;
			}
			else
			{
				if (in_preferencia_atendimento[0].checked) 
				{
					if (tx_horario.value == "")
					{
						alert("Selecione o melhor horário para contato!");
						tx_horario.focus();
						return false;
					}
				}
			}

			if (tx_mensagem.value == "")
			{
				alert("Digite a mensagem!");
				tx_mensagem.focus();
				return false;
			}				
		}
		if (divOuvidoria.style.display == '')
		{
			//Verifica se código protocolo não é branco
			if(nrProtocolo.value == '')
			{
				alert("Preencha o campo Número do Protocolo!");
				nrProtocolo.focus();
				return false;
			}
			//Verifica se código imagem não é branco
			else if(txtCodigo.value == '')
			{
				alert("Preencha o campo Código!");
				txtCodigo.focus();
				return false;
			}	
			// Valida Codigo da imagem
			else if (validarCodigoImagem() == false)
			{
				alert("Código Inválido!");
				txtCodigo.focus();
				return false;
			}
			// Valida Protocolo	
			else 
			{
				var intTamanho = nrProtocolo.value.length;				
				if ((intTamanho == 7) || (intTamanho == 8) || (intTamanho == 9))
				{
					/*Faz a verificação do Numero do Protocolo*/					
					var blnProtocoloOk = ValidaElegibilidade(nrProtocolo.value);					
					if (!blnProtocoloOk)
					{
						document.forms[0].action += '?status=1'
						return true;
					}									
				}
				else
				{
					document.forms[0].action += '?status=1'
					return true;
				}
			}
		}						
		
		if (fg_Ouvidoria.value == '1')
		{
			if (divOuvidoria.style.display == '')
			{
				nmProtocolo.value = nrProtocolo.value;			
				fg_Ouvidoria.value = '0';
				//divOuvidoria.style.display = 'none';			
				divFormulario.style.display = '';						
				return false;			
			}
			else
			{
				divOuvidoria.style.display = '';
				return false;
			}
		}
		
		return true;
	}
}


function ValidaElegibilidade(nmProtocolo)
{	
	// ********************************************************************************
	// ************** Validação de Protocolo (Elegibilidade) **************************
	// ********************************************************************************	
	var strURL = window.location.href;
	var intIndex = strURL.indexOf('?');
	if (intIndex != -1)
	{
		strURL = strURL.substring(0,intIndex);
	}	
	if ((nmProtocolo.length != 7) && (nmProtocolo.length != 8) && (nmProtocolo.length != 9))
	{	
		window.location = strURL + '?status=1';
		return false;
	}
	else
	{
		var strRetorno, MR_IAPJS_CDTRAN, MR_IAPJS_CDCANA, MR_IAPJS_IDENTEMP, MR_IAPJS_IDENTNEG, MR_IAPJS_NUMEPARM
		MR_IAPJS_CDTRAN = 'UM8N'
		MR_IAPJS_CDCANA = '11'
		MR_IAPJS_IDENTEMP = '4' //(4) Fininvest
		MR_IAPJS_IDENTNEG = '6'
		MR_IAPJS_NUMEPARM = nmProtocolo		
		strRetorno = GetUrl('/ajax/Elegibilidade/ValidaProtocolo.asp?MR_IAPJS_CDTRAN=' + MR_IAPJS_CDTRAN + '&MR_IAPJS_CDCANA=' + MR_IAPJS_CDCANA + '&MR_IAPJS_IDENTEMP=' + MR_IAPJS_IDENTEMP + '&MR_IAPJS_IDENTNEG=' + MR_IAPJS_IDENTNEG + '&MR_IAPJS_NUMEPARM=' + MR_IAPJS_NUMEPARM + '');		
		strRetorno = strRetorno.split('|');
		if (strRetorno.length == 2)
		{
			var strStatus, strElegivel
			strStatus = strRetorno[0];
			strElegivel = strRetorno[1];				
			if (strStatus != '00')
			{
				alert('Erro ao validar o Protocolo.');
				return false;
			}
			else
			{
				if (strElegivel == 'N')
				{
					window.location = strURL + '?status=1';
					return false;
				}
				return true;
			}
		}
	}
	// ********************************************************************************
	// ********************************************************************************
	// ********************************************************************************
}

function validarCodigoImagem()
{
	var blnReturn;
	blnReturn = false;
	switch(document.FaleConosco.objImgCod.value)
	{
		case '01':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'brlm') blnReturn = true;
			break;
		case '02':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'c6hm') blnReturn = true;
			break;
		case '03':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'cxjf') blnReturn = true;
			break;
		case '04':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'd9df') blnReturn = true;
			break;
		case '05':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'e8x6') blnReturn = true;
			break;
		case '06':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'jb7f') blnReturn = true;
			break;
		case '07':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'p6wi') blnReturn = true;
			break;
		case '08':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'sfdc') blnReturn = true;
			break;
		case '09':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'vhdg') blnReturn = true;
			break;
		case '10':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'wh4c') blnReturn = true;
			break;
		case '11':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'wnff') blnReturn = true;
			break;
		case '12':
			if(document.FaleConosco.txtCodigo.value.toLowerCase() == 'xubg') blnReturn = true;
			break;			
	}	
	return blnReturn;
}



function ValidaFaleConoscoMacro (frm)
{
	with(frm)
	{
		if (tx_assunto.value == "")
		{
			alert("Selecione o Assunto!");
			tx_assunto.focus();
			return false;
		}
		
		if (tx_nome.value == "")
		{
			alert("Digite o seu nome!");
			tx_nome.focus();
			return false;
		}
		
		if (tx_numero_cartao.value == "")
		{
			alert("Digite o número do cartão!");
			tx_numero_cartao.focus();
			return false;
		}
		else if (!SoNumeros(tx_numero_cartao.value))
		{
			alert("Número do Cartão Inválido!");
			tx_numero_cartao.focus();
			return false;
		}
		
		if (in_cliente_pagueleve[0].checked) 
		{
			if (tx_cpf.value == "")
			{
				alert("Digite seu CPF!");
				tx_cpf.focus();
				return false;
			}
			else if (!ValidaCPF (tx_cpf.value))
			{
				alert("CPF Inválido!");
				tx_cpf.focus();
				return false;
			}
		}
		
		if (tx_email_cliente.value == "")
		{
			alert("Digite seu email!");
			tx_email_cliente.focus();
			return false;
		}
		else if (!ValidaEmail (tx_email_cliente.value))
		{
			alert("Email inválido!");
			tx_email_cliente.focus();
			return false;
		}
		
		if (tx_ddd.value == "")
		{
			alert("Digite o DDD do Telefone!");
			tx_ddd.focus();
			return false;
		}
		else if (!SoNumeros(tx_ddd.value))
		{
			alert("DDD Inválido!");
			tx_ddd.focus();
			return false;
		}
		
		if (tx_telefone.value == "")
		{
			alert("Digite seu Telefone!");
			tx_telefone.focus();
			return false;
		}
		else if (!SoNumeros(tx_telefone.value))
		{
			alert("Telefone Inválido!");
			tx_telefone.focus();
			return false;
		}
		
		if (!( (in_cliente_pagueleve[0].checked) || (in_cliente_pagueleve[1].checked) ))
		{
			alert("Selecione a opção Cliente Fininvest (S/N)!");
			return false;
		} 
		
		if (!( (in_preferencia_atendimento[0].checked) || (in_preferencia_atendimento[1].checked) ))
		{
			alert("Selecione a preferência de atendimento (Telefone/Email) !");
			return false;
		}
		else
		{
			if (in_preferencia_atendimento[0].checked) 
			{
				if (tx_horario.value == "")
				{
					alert("Selecione o melhor horário para contato!");
					tx_horario.focus();
					return false;
				}
			}
		}

		if (tx_mensagem.value == "")
		{
			alert("Digite a mensagem!");
			tx_mensagem.focus();
			return false;
		}
		
	}
}

function ValidaEmail(email) {
        var achou_ponto=false;
        var achou_arroba=false;
        var achou_caracter=false;

        for (var i=0; i<email.length; i++) {
                if (email.charAt(i)=="@")
                { 
                  if (email.charAt(i+1)==".")
                  	achou_arroba=false;
                  else
                    achou_arroba=true;
                }
                else if (email.charAt(i)==".") achou_ponto=true;
                else if (email.charAt(i)!=" ") achou_caracter=true;
        }

        if((email.charAt(0)=="W" || email.charAt(0)=="w") &&
           (email.charAt(1)=="W" || email.charAt(1)=="w") &&
           (email.charAt(2)=="W" || email.charAt(2)=="w") &&
           (email.charAt(3)=="."))
        {
            achou_ponto=false;
            achou_caracter=false;
        }
        if(email.charAt(email.length-1)==".")
        {
            achou_ponto=false;
        }	
        return (achou_ponto && achou_arroba && achou_caracter);
}

function ValidaCPF(s)
{
 var i;
 var c;
 x = 0;
 soma = 0;
 dig1 = 0;
 dig2 = 0;
 texto = "";
 numcpf1="";
 numcpf = "";

 for (i = 0; i < s.length; i++) {
 	 c = s.substring(i,i+1);
	 if (isdigit(c))
	 	numcpf = numcpf + c;
 }
	 	
 if (numcpf.length != 11) {
 	return false;
 }
	
 len = numcpf.length; x = len -1;
 for (var i=0; i <= len - 3; i++) {
  y = numcpf.substring(i,i+1);
  soma = soma + ( y * x);
  x = x - 1;
  texto = texto + y;
 }
 dig1 = 11 - (soma % 11);
 if (dig1 == 10) dig1=0 ;
 if (dig1 == 11) dig1=0 ;
 numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
 x = 11; soma=0;
 for (var i=0; i <= len - 2; i++) {
  soma = soma + (numcpf1.substring(i,i+1) * x);
  x = x - 1;
 }
 dig2= 11 - (soma % 11);
 if (dig2 == 10) dig2=0;
 if (dig2 == 11) dig2=0;
  if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
  return true;
 }
 return false;
}


function isdigit(c)
{
 if ((c >= '0') && (c <= '9'))
 	 return true;
 else  
 	 return false;
}


function SoNumeros(s)
{
 for (var i=0;i<s.length;i++) { 
 	  c = s.substring(i,i+1); 
	  if (!isdigit(c)) 
	  	  return false;	  
 }
 return true;
}