function VerificarFormPrincipal() 
{
	if (dados_em_branco())
	{		
		var strCpfCnpj = document.formulario.cpf1.value + document.formulario.cpf2.value;		
		if (isCPF(strCpfCnpj) || isCNPJ(strCpfCnpj))
		{
			if (isEmail(document.formulario.email.value) == false)
			{
				alert("E-Mail Inválido !");	
				document.formulario.email.focus();
				document.formulario.email.select();
				return false;
			}
		}
		else
		{
			alert("CPF / CNPJ Inválido !");	
			document.formulario.cpf1.focus();
			document.formulario.cpf1.select();
			return false;		
		}	
	}
	else
	{
		return false;
	}
}

function VerificarFormPrincipalPJ() 
{
	if (dados_em_brancoPJ())
	{
		if (isEmail(document.formulario.email.value) == false)
		{
			alert("E-Mail Inválido !");	
			document.formulario.email.focus();
			document.formulario.email.select();
			return false;
		}
	}
	else
	{
		return false;
	}
}

function isEmail(email)
{
    var reEml = /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/;
    if (! reEml.test(email)) 
    {
        return false;
	}
	return true;
}

function isCPF(cpf)
{
    if (cpf.length < 11)
    {
        return false;
    }
    for (i = 0; i <= 10; i++) 
	{
	    c = cpf.charAt(i);	    
		if(! (c >= "0") && (c <= "9")) 
		{
			return false;
		}
		varjsSoma = 0;
		for ( j = 0; j < 9; j++ ) 
		{
			varjsSoma += (10 - j) * ( eval(cpf.charAt(j)) );
		}
		vdigito = 11 - (varjsSoma % 11);
		if ( (varjsSoma % 11) < 2 ) vdigito = 0;
		if ( eval( cpf.charAt(9) ) != vdigito ) 
		{
			return false;
		}
		varjsSoma = 0;
		for ( k = 0; k < 9; k++ ) 
		{
			varjsSoma += (11 - k) * ( eval(cpf.charAt(k)) );
		}
		varjsSoma += 2 * ( eval(cpf.charAt(9)) );
		vdigito = 11 - (varjsSoma % 11);
		if ((varjsSoma % 11) < 2 ) vdigito = 0;
		if (eval(cpf.charAt(10)) != vdigito) 
		{
			return false;
		}
	}
	return true;
}
function isCNPJ(CNPJ) 
{
    var erro = "";
    if(CNPJ.length < 14)
    {
        erro += "CNPJ deve conter 14 dígitos\n";
    }
    var nonNumbers = /\D/;
    if (nonNumbers.test(CNPJ)) 
    {
        erro += "A verificação de CNPJ suporta apenas números\n"; 
    }
    var a = [];
    var b = new Number;             
    var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
    for (i=0; i<12; i++)
    {
        a[i] = CNPJ.charAt(i);
        b += a[i] * c[i+1];
    }
    if ((x = b % 11) < 2) 
    { 
        a[12] = 0;
    } 
    else 
    { 
        a[12] = 11-x;
    }
    b = 0;
    for (y=0; y<13; y++) 
    {
        b += (a[y] * c[y]); 
    }
    if ((x = b % 11) < 2) 
    { 
        a[13] = 0; 
    } 
    else 
    { 
        a[13] = 11-x; 
    }
    if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13]))
    {
        erro +="Dígito verificador com problema\n";
    }
    if (erro.length > 0)
    {
        return false;
    }     
    return true;
}

function EhNumero(valor,texto,fieldName)
{
    for (i=0; i < valor.length; i++) {
		if (isNaN(parseInt(valor.charAt(i)))) {
		    alert("Erro: O campo " + texto + " tem que ser numérico.");
		    eval("document.formulario." + fieldName + ".focus()");
		    eval("document.formulario." + fieldName + ".select()");
		    return false;
		}
    }
    return true;
}

function dados_em_branco() {
	if (soBrancosTipoEmail() &&
		soBrancosCliente() &&
		soBrancos(document.formulario.nome.value,"Nome",'nome') &&
		soBrancosCPF() &&		
		soBrancos(document.formulario.email.value,"E-mail",'email') &&
	    soBrancos(document.formulario.comentario.value,"Comentários",'comentario')){
		return true;
	}
	else { return false; }
}

function dados_em_brancoPJ() {
	if (soBrancosTipoEmail() &&		
		soBrancos(document.formulario.nome.value,"Nome",'nome') &&		
		soBrancos(document.formulario.RazaoSocial.value,"Razão Social",'RazaoSocial') &&		
		soBrancos(document.formulario.HomePage.value,"Home Page",'HomePage') &&		
		soBrancos(document.formulario.email.value,"E-mail",'email') &&
	    soBrancos(document.formulario.comentario.value,"Comentários",'comentario')){
		return true;
	}
	else { return false; }
}

function soBrancos(valor,texto,fieldName)
{
    for (i=0; i<valor.length; i++) {
		if (valor.charAt(i) != ' ') {
		    return true
		}
    }
    alert("O preenchimento do campo " + texto + " é obrigatório.")
    eval("document.formulario." + fieldName + ".focus()");
    eval("document.formulario." + fieldName + ".select()");
    return false
}	

function soBrancosTipoEmail()
{
	var tipoEmail = document.formulario.selTipoEmail.value;
	if (tipoEmail == "")
	{
		alert("O preenchimento do campo Tipo de E-mail é obrigatório.")
		document.formulario.selTipoEmail.focus();		
		return false;	
	}	
	return true;
}   

function soBrancosCliente()
{
	var inCliente = document.formulario.in_Cliente
	if ((inCliente[0].checked == false) && (inCliente[1].checked == false))
	{
		alert("Selecione a opção Cliente Hipercard (S/N)!")				
		return false;			
	}
	return true;
}  

function soBrancosCPF()
{
	texto = document.formulario.cpf1.value;
	digito = document.formulario.cpf2.value;
	nivel = 1;
	for (i=0; i<texto.length; i++) {
		if (texto.charAt(i) != " ") {
			nivel = 2;
			for (j=0; j<digito.length; j++) {
				if (digito.charAt(j) != " ") {
					return true;
				}
			}
		}
	}
    alert("O preenchimento do campo CPF é obrigatório.")
	eval("document.formulario.cpf" + nivel + ".focus()");
	eval("document.formulario.cpf" + nivel + ".select()");
	return false;
}	

function VerificaCampos()
{
	var frm   = eval("document.formulario");
	
	if (divFormulario.style.display == '')
	{		
		if (VerificarFormPrincipal() == false)
		{
			return false;
		}
	}
	
	if (divOuvidoria.style.display == '')
	{
		
		//Verifica se código protocolo não é branco
		if(frm.nrProtocolo.value == '')
		{
			alert("Preencha o campo Número do Protocolo!");
			frm.nrProtocolo.focus();
			return false;
		}
		//Verifica se código imagem não é branco
		else if(frm.txtCodigo.value == '')
		{
			alert("Preencha o campo Código!");
			frm.txtCodigo.focus();
			return false;
		}	
		//Valida Imagem
		else if (validarCodigoImagem() == false)
		{
			alert("Código Inválido!");
			frm.txtCodigo.focus();
			return false;
		}
		//Faz a verificação do Número do Protocolo
		else 
		{
			var nrProtocolo = frm.nrProtocolo.value;						
			var blnProtocoloOk = ValidaElegibilidade(nrProtocolo);
			if (!blnProtocoloOk)
			{				
				frm.action += '?status=1';
				return true;
			}
		}
		
	}
	
	if (frm.fg_Ouvidoria.value == '1')
	{
		frm.nmProtocolo.value = frm.nrProtocolo.value;
		frm.fg_Ouvidoria.value = '0';
		showFormPricipal(true);
		return false;
	}
	
	//frm.submit();	
	return true;
}

function VerificaCamposPJ()
{
	var frm   = eval("document.formulario");
	
	if (divFormulario.style.display == '')
	{		
		if (VerificarFormPrincipalPJ() == false)
		{
			return false;
		}
	}
	
	if (divOuvidoria.style.display == '')
	{
		//Verifica se código protocolo não é branco
		if(frm.nrProtocolo.value == '')
		{
			alert("Preencha o campo Número do Protocolo!");
			frm.nrProtocolo.focus();
			return false;
		}
		//Verifica se código imagem não é branco
		else if(frm.txtCodigo.value == '')
		{
			alert("Preencha o campo Código!");
			frm.txtCodigo.focus();
			return false;
		}	
		// Valida Codigo da imagem
		else if (validarCodigoImagem() == false)
		{
			alert("Código Inválido!");
			frm.txtCodigo.focus();
			return false;
		}
		//Faz a verificação do Numero do Protocolo
		else
		{
			var nrProtocolo = frm.nrProtocolo.value;						
			var blnProtocoloOk = ValidaElegibilidade(nrProtocolo);
			if (!blnProtocoloOk)
			{		
				frm.action += '?status=1'; 		
				return true;
			}
		}
		
				
	}
	
	if (frm.fg_Ouvidoria.value == '1')
	{
		frm.nmProtocolo.value = frm.nrProtocolo.value;
		frm.fg_Ouvidoria.value = '0';
		showFormPricipal(true);
		return false;
	}
	
	frm.submit();	
}

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)
	{
		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 = '6' //(6) Hipercard
		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 Voltar()
{	
	var strURL = window.location.href;
	var intIndex = strURL.indexOf('?');
	if (intIndex != -1)
	{
		strURL = strURL.substring(0,intIndex);
	}	
	window.location = strURL;	
}

function EnviarReclamacao()
{
	var strURL = window.location.href;	
	strURL = strURL.substring(0,strURL.indexOf('?'));
	window.location = strURL + "?te=reclamacao";
}

function AjustaFormularioTE()
{
	var objTipoEmail = document.getElementById('selTipoEmail');
	if (objTipoEmail != null)
	{
		objTipoEmail.value = 'Reclamação';	
		TrocaFormulario(objTipoEmail.value);	
	}
}

function ShowHint(strLayer)
{
	var objL = document.getElementById(strLayer);
	objL.style.visibility = 'visible';
}
function HideHint(strLayer)
{
	var objL = document.getElementById(strLayer);
	objL.style.visibility = 'hidden';
}

function AjustarForm(value)
{				
	if (value.toLowerCase() == 'ouvidoria')
	{
		showFormOuvidoria(true);
		showFormPricipal(false);
	}
	else
	{
		showFormOuvidoria(false);
		showFormPricipal(true);
	}
	if (value.toLowerCase() == '')
	{
		document.forms[0].submit();
	}
}

function AjustaFormOuvidoria()
{
	showFormPricipal(false);
	showFormOuvidoria(true);	
}

function ConfigurarForm()
{
	showFormPricipal(true);
	showFormOuvidoria(false);
}

function showFormOuvidoria(blnMostrar)
{				
	var objdivOuvidoria = document.getElementById('divOuvidoria');		
	if (objdivOuvidoria != null)
	{
		if (blnMostrar)
		{
			objdivOuvidoria.style.display = '';						
			document.formulario.fg_Ouvidoria.value = '1';
			document.formulario.selTipoEmail.value = 'Ouvidoria';
		}
		else
		{
			objdivOuvidoria.style.display = 'none';
			document.formulario.fg_Ouvidoria.value = '0';
		}
	}
}
function showFormPricipal(blnMostrar)
{
	var objdivFormulario = document.getElementById('divFormulario');
	if (objdivFormulario != null)
	{
		if (blnMostrar)
		{
			objdivFormulario.style.display = '';
		}
		else
		{
			objdivFormulario.style.display = 'none';
		}
	}
}

function getKey(evt) 
{
	//return evt?(evt.keyCode?evt.keyCode:(evt.which?evt.which:evt.charCode)):null;
	var e = evt? evt : window.event; 
	if(!e) return; 
	var key = 0; 
	key = e.charCode; 
	if (typeof(key) == 'undefined') 
	{ 
		key = e.keyCode; 
		if (typeof(key) == 'undefined') 
		{ 
			key = e.which; 
		} 
	} 
	return key;
}		

function soNum(e) 
{
	var tecla = getKey(e);
	var strTecla = String.fromCharCode(tecla);
	var reNum = /^(\d)+$/;	
	if (! reNum.test(strTecla) && tecla != 0) 
	{
		return false;
	}
	return true;	
}

function validarCodigoImagem()
{
	var blnReturn;
	blnReturn = false;
	switch(document.formulario.objImgCod.value)
	{
		case '01':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'brlm') blnReturn = true;
			break;
		case '02':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'c6hm') blnReturn = true;
			break;
		case '03':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'cxjf') blnReturn = true;
			break;
		case '04':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'd9df') blnReturn = true;
			break;
		case '05':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'e8x6') blnReturn = true;
			break;
		case '06':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'jb7f') blnReturn = true;
			break;
		case '07':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'p6wi') blnReturn = true;
			break;
		case '08':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'sfdc') blnReturn = true;
			break;
		case '09':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'vhdg') blnReturn = true;
			break;
		case '10':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'wh4c') blnReturn = true;
			break;
		case '11':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'wnff') blnReturn = true;
			break;
		case '12':
			if(document.formulario.txtCodigo.value.toLowerCase() == 'xubg') blnReturn = true;
			break;			
	}	
	return blnReturn;
}

function TrocaFormulario(value)
{
	if (value.toUpperCase() == 'OUVIDORIA')
	{
		showFormPricipal(false);
		showFormOuvidoria(true);
	}
	else
	{
		showFormPricipal(true);
		showFormOuvidoria(false);
	}
}
