﻿// JScript File
function EnviarFormContato()
{
    //Verificando se os campos estão corretamente preenchidos.
    if(EmptyField(document.getElementById('assunto')))
    {
        alert('Por favor informe o nome para contato.');  
        document.getElementById('assunto').focus();
        return false;
    } 
    
    
    if(EmptyField(document.getElementById('nome')))
    {
        alert('Por favor informe o nome para contato.'); 
        document.getElementById('nome').focus(); 
        return false;
    }
    
    if(document.getElementById('estado').value == '0')
    {
        alert('Por favor o estado em que reside.'); 
        document.getElementById('estado').focus(); 
        return false;
    }
    if(document.getElementById('cidade').value == '0')
    {
        alert('Por favor a cidade em que reside.');  
        document.getElementById('cidade').focus();
        return false;
    }
    
    if(EmptyField(document.getElementById('email')))
    {
        alert('Por favor informe o e-mail para contato.'); 
        document.getElementById('email').focus(); 
        return false;
    }
    else
    {
        if(!valida_email(document.getElementById('email')))
        {
            //alert('E-mail Inválido.'); 
            document.getElementById('email').focus(); 
            return false;
        }
    }
    
    if(EmptyField(document.getElementById('fone')))
    {
        alert('Por favor informe o Telefone/Fax para contato.'); 
        document.getElementById('fone').focus(); 
        return false;
    }
    else
    {
        if(!valida_telefone(document.getElementById('fone')))
        {
            //alert('Telefone/Fax em formato inválido.Por favor siga o seguinte formato (XX) XXXX-XXXX.');  
            document.getElementById('fone').focus();
            return false;
        }
    }
    
    if(document.getElementById('departamento').value == "0")
    {
        alert('Por favor informe o departamento no qual deseja enviar o contato.'); 
        document.getElementById('departamento').focus(); 
        return false;
    }
    if(EmptyField(document.getElementById('comentario')))
    {
        alert('Por favor informe o comentário do contato.'); 
        document.getElementById('comentario').focus(); 
        return false;
    }
    
    if (!CaptchImg())
	{
	    return false;
	}
    
    //Caso tenha chegado até aqui, envia-se o formulário de contato.
    var assunto_ = document.getElementById('assunto').value;
    var nome_ = document.getElementById('nome').value;
    
    var estado_ = document.getElementById('estado').options[document.getElementById('estado').selectedIndex].text;
    var cidade_ = document.getElementById('cidade').options[document.getElementById('cidade').selectedIndex].text;
    
    
    var email_ = document.getElementById('email').value;
    var fone_ = document.getElementById('fone').value;
    var departamento_ = document.getElementById('departamento').options[document.getElementById('departamento').selectedIndex].value;
    
    var comentario_ = document.getElementById('comentario').value;
    
    if(!gestao_contatos.EnviarFormularioContato(assunto_, nome_, estado_,cidade_, email_, fone_,departamento_, comentario_).value)
    {
        document.getElementById('tb_erro').className = "error";
        document.getElementById('tb_sucesso').className  = "success hidden";
        document.getElementById('form_contato').style.display = 'none';
    }
    else
    {
		window.scrollTo(0,0);	
        document.getElementById('tb_erro').className = "error hidden";
        document.getElementById('tb_sucesso').className  = "success";
        document.getElementById('form_contato').style.display = 'none';
    }
}


function VerificaCodigo()
{

    if(EmptyField(document.getElementById('codigo_contato')))
    {
        alert('Para poder visualizar seu contato, por favor informe o número de seu atendimento.');  
        document.getElementById('codigo_contato').focus();
        return false
    }
    
    var codigo_ = document.getElementById('codigo_contato').value;
    
    if(!gestao_contatos.LoginContato(codigo_).value)
    {
        alert('O código de atendimento informado, não consta em nossos cadastros. Por favor verifique a digitação e tente novamente.');  
        document.getElementById('codigo_contato').focus();
        return false;
    }
    else
    {
        window.location = 'gestao_contatos_mensagens.aspx';
    }
}


//Função assincrona do populaCidade.
function retorno_populaCidade(ret){
    var dt = ret.value;
    var cidade = document.getElementById('cidade');
    cidade.options.length = 0;
	cidade.options[cidade.options.length] = new Option("Selecione uma Cidade","0");            
    for(var i = 0; i < dt.length; i++)
    {                
        cidade.options[cidade.options.length] = new Option(dt[i].Nome, dt[i].Codigo_Cidades);
    }
    document.getElementById('img_cidade').style.display = "none";
}

//Metodo responsável por popular as cidades de acordo com o estado selecionado.
function populaCidade(value)
{
    document.getElementById('img_cidade').style.display = "";
    gestao_contatos.PopulaCidade(value,retorno_populaCidade);   
}

//Função para ao se dá um Enter cair sobre o evento de submit da pagina de contato...
function EnterContato(evt)
{
    //Verificando se o que é digitado é somente campos numéricos
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if(charCode==13)
    {
         document.getElementById('bt_enviarContato').onclick();		
         return false;
    }
    return true;
}

function stripHTML(textFull) 
{
  if(textFull != null &&  textFull != ''){
          textFull = textFull.replace(/[ºª¦£]/g, '');
          textFull = textFull.replace(/[;<>()+÷|¦&$!?*¬ªº°_/\\%@#=`¨´''~^{}]/g, '');
          textFull = textFull.replace(/\]|\[|-|"/g, '');
        } 
        
         document.getElementById("comentario").value = textFull; 
}


function CaptchImg()
{
    if(document.getElementById("valorInformado").value == '')
    {
        alert('Digite o código de validação.');
        document.getElementById("valorInformado").focus();
    }
    else
    {
        if(gestao_contatos.CaptchImg(document.getElementById("valorInformado").value).value)
        {
            return true
        }
        else
        {
            alert('Código inválido.');
            document.getElementById("valorInformado").value = '';
            document.getElementById("valorInformado").focus();
        }   
    }
    return false;
}

