// 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('estado').className = 'txtfield';
    document.getElementById('erroestado').style.display = 'none';
    
    document.getElementById('cidade').className = 'txtfield';
    document.getElementById('errocidade').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('nome')))
    {
        sucesso = false;
        document.getElementById('nome').className = 'error';
        document.getElementById('erronome').style.display = '';
        document.getElementById('erronome').innerHTML = 'Por favor informe o nome para contato.';  
    } 
    
    if(EmptyField(document.getElementById('email')))
    {
        sucesso = false;
        document.getElementById('email').className = '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 = 'error';
            document.getElementById('erroemail').style.display = '';
            document.getElementById('erroemail').innerHTML = 'E-mail Inválido.';  
        }
    }
    
    if(!EmptyField(document.getElementById('fone')))
    {
        if(!valida_telefone(document.getElementById('fone')))
        {
            sucesso = false;
            document.getElementById('fone').className = '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 = '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 nome_ = document.getElementById('nome').value;
    
    var pais_ = "";
    var estado_ = "";
    var cidade_ = "";
    var pais_ = "";
    
    if(document.getElementById('pais').value != "0" )
    {
        pais_ = document.getElementById('pais').options[document.getElementById('pais').selectedIndex].text;
    }
    
    
    if(document.getElementById('pais').value == 32)
    {
        if(document.getElementById('estado').value != "0" )
        {
            estado_ = document.getElementById('estado').options[document.getElementById('estado').selectedIndex].text;
        }
    
        if(document.getElementById('cidade').value != "0" )
        {
            cidade_ = document.getElementById('cidade').options[document.getElementById('cidade').selectedIndex].text;
        }
    }
    else
    {
        estado_ = document.getElementById('pais_estado').value;
        cidade_ = document.getElementById('pais_cidade').value;
    }
    
    var email_ = document.getElementById('email').value;
    var fone_ = document.getElementById('fone').value;
    var comentario_ = document.getElementById('comentario').value;
    
    if(!contato.EnviarFormularioContato(nome_,pais_, estado_, cidade_, email_, fone_,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";
    }
}

//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 = "";
    contato.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_outro_pais').style.display = 'none';
        document.getElementById('div_brasil').style.display = 'none';
    }
    else if(value == '32')//Brazil
    {
        document.getElementById('div_outro_pais').style.display = 'none';
        document.getElementById('div_brasil').style.display = '';
    }
    else
    {
        //Habilitando a pessoa a inserir manualmente o seu estado e cidade.
        document.getElementById('div_outro_pais').style.display = '';
        document.getElementById('div_brasil').style.display = 'none';
    }
}
