﻿// JScript File
function EnviarFormContato()
{    
    var sucesso = true;
    
    // Define os estilos padrões
    document.getElementById('assunto').className = 'txtfield';
    document.getElementById('erroassunto').style.display = 'none';
    
    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('comentario').className = 'txtfield';
    document.getElementById('errocomentario').style.display = 'none';
    
    
    //Verificando se os campos estão corretamente preenchidos.
    if(EmptyField(document.getElementById('assunto')))
    {
        sucesso = false;
        document.getElementById('assunto').className = 'txtfield error';
        document.getElementById('erroassunto').style.display = '';
        document.getElementById('erroassunto').innerHTML = 'Por favor informe o nome para contato.';  
    } 
    
    
    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 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 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 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 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(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 novamente.'); 
        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 pais_ =  document.getElementById('pais').options[document.getElementById('pais').selectedIndex].text;
    var estado_ = "";
    var cidade_ = "";
    var departamento_ = 1;    
    
    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 comentario_ = document.getElementById('comentario').value;
    
    if(!exportacao.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
    {
        document.getElementById('sucesso_contato').style.display = "";
        document.getElementById('form_contato').style.display  = "none";
    }
}


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(!exportacao.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 = 's_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 = "";
    exportacao.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 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_brasil').style.visibility = 'visible';
        document.getElementById('div_outro_pais').style.visibility = 'hidden';
    }
    else
    {
        document.getElementById('div_brasil').style.visibility = 'hidden';
        document.getElementById('div_outro_pais').style.display = '';
        document.getElementById('div_outro_pais').style.visibility = 'visible';
    }
}