﻿// JScript File
function EnviarFormContato()
{
    var sucesso = true;
    
    // Define os estilos padrões

    
    document.getElementById('nome').className = 'txtfield';
    document.getElementById('erronome').style.display = 'none';
    
    document.getElementById('pais').className = 'txtfield';
    document.getElementById('erropais').style.display = 'none';
    
    document.getElementById('estado').className = 'txtfield';
    document.getElementById('erroestado').style.display = 'none';
    
    document.getElementById('cidade').className = 'txtfield';
    document.getElementById('errocidade').style.display = 'none';
    
    document.getElementById('pais_estado').className = 'txtfield';
    document.getElementById('erropais_estado').style.display = 'none';
    
    document.getElementById('pais_cidade').className = 'txtfield';
    document.getElementById('erropais_cidade').style.display = 'none';

    document.getElementById('email').className = 'txtfield';
    document.getElementById('erroemail').style.display = 'none';
    
    document.getElementById('fone').className = 'txtfield';
    document.getElementById('errofone').style.display = 'none';

    document.getElementById('departamento').className = 'txtfield';
    document.getElementById('errodepartamento').style.display = 'none';

    document.getElementById('assunto').className = 'txtfield';
    document.getElementById('erroassunto').style.display = 'none';
    
    document.getElementById('comentario').className = 'txtfield';
    document.getElementById('errocomentario').style.display = 'none';
    
    
    //Verificando se os campos estão corretamente preenchidos.
    
    if(EmptyField(document.getElementById('nome')))
    {
        sucesso = false;
        document.getElementById('nome').className = 'txtfield error';
        document.getElementById('erronome').style.display = '';
        document.getElementById('erronome').innerHTML = 'Por favor, informe o nome para contato.';  
    }
    
    if(document.getElementById('pais').value == '0')
    {
        sucesso = false;
        document.getElementById('pais').className = 'txtfield error';
        document.getElementById('erropais').style.display = '';
        document.getElementById('erropais').innerHTML = 'Por favor, informe o país em que reside.';  
    }
    if(document.getElementById('pais').value == '32')
    {
        if(document.getElementById('estado').value == '0')
        {
            sucesso = false;
            document.getElementById('estado').className = 'txtfield error';
            document.getElementById('erroestado').style.display = '';
            document.getElementById('erroestado').innerHTML = 'Por favor, informe o estado em que reside.';  
        }
        if(document.getElementById('cidade').value == '0')
        {
            sucesso = false;
            document.getElementById('cidade').className = 'txtfield error';
            document.getElementById('errocidade').style.display = '';
            document.getElementById('errocidade').innerHTML = 'Por favor, informe a cidade em que reside.';  
        }
    }
    else
    {
        if(EmptyField(document.getElementById('pais_estado')))
        {
            sucesso = false;
            document.getElementById('pais_estado').className = 'txtfield error';
            document.getElementById('erropais_estado').style.display = '';
            document.getElementById('erropais_estado').innerHTML = 'Por favor, informe o estado em que reside.';  
        }
        if(EmptyField(document.getElementById('pais_cidade')))
        {
            sucesso = false;
            document.getElementById('pais_cidade').className = 'txtfield error';
            document.getElementById('erropais_cidade').style.display = '';
            document.getElementById('erropais_cidade').innerHTML = 'Por favor, informe a cidade em que reside.';  
        }
    }
    
    
    if(EmptyField(document.getElementById('email')))
    {
        sucesso = false;
        document.getElementById('email').className = 'txtfield error';
        document.getElementById('erroemail').style.display = '';
        document.getElementById('erroemail').innerHTML = 'Por favor, informe o e-mail para contato.';  
    }
    else
    {
        if(!valida_email(document.getElementById('email')))
        {
            sucesso = false;
            document.getElementById('email').className = 'txtfield error';
            document.getElementById('erroemail').style.display = '';
            document.getElementById('erroemail').innerHTML = 'E-mail inválido.';  
        }
    }
    
    if(EmptyField(document.getElementById('fone')))
    {
        sucesso = false;
        document.getElementById('fone').className = 'txtfield error';
        document.getElementById('errofone').style.display = '';
        document.getElementById('errofone').innerHTML = 'Por favor, informe o Telefone/Fax para contato.';  
    }
    else
    {
        if(!valida_telefone(document.getElementById('fone')))
        {
            sucesso = false;
            document.getElementById('fone').className = 'txtfield error';
            document.getElementById('errofone').style.display = '';
            document.getElementById('errofone').innerHTML = 'Telefone/Fax em formato inválido.Por favor, siga o seguinte formato (XX) XXXX-XXXX.';  
        }
    }
    
//    if(document.getElementById('departamento').value == "0")
//    {
//        sucesso = false;
//        document.getElementById('departamento').className = 'txtfield error';
//        document.getElementById('errodepartamento').style.display = '';
//        document.getElementById('errodepartamento').innerHTML = 'Por favor, informe o departamento no qual deseja enviar o contato.';  
//    }
    
    if(document.getElementById('assunto').value == '0')
    {
        sucesso = false;
        document.getElementById('assunto').className = 'error';
        document.getElementById('erroassunto').style.display = '';
        document.getElementById('erroassunto').innerHTML = 'Por favor, informe o assunto do contato.';  
    }
    
    if(EmptyField(document.getElementById('comentario')))
    {
        sucesso = false;
        document.getElementById('comentario').className = 'txtfield error';
        document.getElementById('errocomentario').style.display = '';
        document.getElementById('errocomentario').innerHTML = 'Por favor, informe o comentário do contato.';  
    }
    
    if (!sucesso) {
        alert('Alguns campos não foram preenchidos da forma devida. Por favor, verifique os campos marcados em vermelho.'); 
        return false;
    }
    
    //Caso tenha chegado até aqui, envia-se o formulário de contato.
    var assunto_ = document.getElementById('assunto').options[document.getElementById('assunto').selectedIndex].text;
    var nome_ = document.getElementById('nome').value;
    
    var pais_ =  document.getElementById('pais').options[document.getElementById('pais').selectedIndex].text;
    var estado_ = "";
    var cidade_ = "";
    
    
    if(document.getElementById('pais').value == '32')
    {
        cidade_ = document.getElementById('cidade').options[document.getElementById('cidade').selectedIndex].text;   
        estado_ = document.getElementById('estado').options[document.getElementById('estado').selectedIndex].text;    
    }
    else
    {
        cidade_ = document.getElementById('pais_cidade').value;   
        estado_ = document.getElementById('pais_estado').value;    
    }
    
    
    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;
    
    document.body.style.cursor = 'wait';
    
    if(!gestao_contatos.EnviarFormularioContato(assunto_, nome_, pais_, estado_,cidade_, email_, fone_,departamento_, comentario_).value)
    {
        document.getElementById('erro_contato').style.display = "";
        document.getElementById('form_contato').style.display  = "none";
    }
    else
    {
        gestao_contatos.EnviarConfirmacaoModerador(nome_,email_);
        
        document.getElementById('sucesso_contato').style.display = "";
        document.getElementById('form_contato').style.display  = "none";
    }
    document.body.style.cursor = 'default';
    
}


function VerificaCodigo()
{
    var sucesso = true;
    
    // Define os estilos padrões
    document.getElementById('codigo_contato').className = 'txtfield';
    document.getElementById('errocodigo_contato').style.display = 'none';
    
    if(EmptyField(document.getElementById('codigo_contato')))
    {
        sucesso = false;
        document.getElementById('codigo_contato').className = 'txtfield error';
        document.getElementById('errocodigo_contato').style.display = '';
        document.getElementById('errocodigo_contato').innerHTML = 'Para poder visualizar seu contato, por favor, informe o número de seu atendimento';  
    }
    
    if (!sucesso) {
        alert('Alguns campos não foram preenchidos da forma devida. Por favor, verifique os campos marcados em vermelho.'); 
        return false;
    }
    
    
    
    var codigo_ = document.getElementById('codigo_contato').value;
    
    if(!gestao_contatos.LoginContato(codigo_).value)
    {
        document.getElementById('codigo_contato').className = 'txtfield error';
        document.getElementById('errocodigo_contato').style.display = '';
        document.getElementById('errocodigo_contato').innerHTML = 'O código de atendimento informado, não consta em nossos cadastros. Por favor, verifique a digitação e tente novamente.';  
    }
    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_Cidade);
    }
    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
//param:evt = Evento do keyPress
//param:botao = Id do botão que dá o submit na página
function EnterContato(evt,botao)
{
    //Verificando se o que é digitado é somente campos numéricos
    var charCode = (evt.which) ? evt.which : evt.keyCode;
  
    if(charCode==13)
    {
         document.getElementById(botao).onclick();		
         return false;
    }
    return true;
}


function verificaPais(value)
{
    if(value == 0)
    {
        document.getElementById('div_brasil').style.display = 'none';
        document.getElementById('div_outro_pais').style.display = 'none';
    }
    else if(value == 32)
    {
        document.getElementById('div_brasil').style.display = '';
        document.getElementById('div_outro_pais').style.display = 'none';
    }
    else
    {
        document.getElementById('div_brasil').style.display = 'none';
        document.getElementById('div_outro_pais').style.display = '';
    }
}