﻿// JScript File
function EnviarFormContato()
{
    var sucesso = true;
    
    // Define os estilos padrões
    document.getElementById('nome').className = 'campotxt';
    document.getElementById('erronome').innerHTML = '';
    
    document.getElementById('estado').className = 'campotxt';
    document.getElementById('erroestado').innerHTML = '';

    document.getElementById('cidade_drop_lista').className = 'campotxt';
    document.getElementById('errocidade').innerHTML = '';
    
    document.getElementById('email').className = 'campotxt';
    document.getElementById('erroemail').innerHTML = '';
    
    document.getElementById('fone').className = 'campotxt';
    document.getElementById('errofone').innerHTML = '';

    document.getElementById('comentario').className = 'campotxt';
    document.getElementById('errocomentario').innerHTML = '';

    document.getElementById('erroimoveis_selecionados').innerHTML = '';
    
    if(EmptyField(document.getElementById('nome')))
    {
        sucesso = false;
        document.getElementById('nome').className = 'campotxt erro';
        document.getElementById('erronome').innerHTML = 'Por favor, informe o nome para contato.';  
    }
    
    
    if(document.getElementById('estado').value == '0')
    {
        sucesso = false;
        document.getElementById('estado').className = 'campotxt erro';
        document.getElementById('erroestado').innerHTML = 'Por favor, o estado em que reside.';  
    }
    if (document.getElementById('cidade_drop_lista').value == '0')
    {
        sucesso = false;
        document.getElementById('cidade_drop_lista').className = 'campotxt erro';
        document.getElementById('errocidade').innerHTML = 'Por favor, a cidade em que reside.';  
    }
    
       
    if(EmptyField(document.getElementById('email')))
    {
        sucesso = false;
        document.getElementById('email').className = 'campotxt erro';
        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 = 'campotxt erro';
            document.getElementById('erroemail').innerHTML = 'E-mail Inválido.';  
        }
    }
    
    if(EmptyField(document.getElementById('fone')))
    {
        sucesso = false;
        document.getElementById('fone').className = 'campotxt erro';
        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 = 'campotxt erro';
            document.getElementById('errofone').innerHTML = 'Telefone/Fax em formato inválido.Por favor siga o seguinte formato (XX) XXXX-XXXX.';  
        }
    }

    if (EmptyField(document.getElementById('imoveis_selecionados'))) 
    {
        sucesso = false;
        document.getElementById('erroimoveis_selecionados').innerHTML = 'Você deve selecionar ao menos um imóvel para entrar em contato.';  
    }
    
    if(EmptyField(document.getElementById('comentario')))
    {
        sucesso = false;
        document.getElementById('comentario').className = 'campotxt erro';
        document.getElementById('errocomentario').innerHTML = 'Por favor, informe o comentário do contato.';  
    }
    
    if (!sucesso) {
        return false;
    }

    var horario_contatar = ""
    for (var i = 0; i < document.Form.horario_contato.length; i++) {
        if (document.Form.horario_contato[i].checked) {
            if (i == (document.Form.horario_contato.length - 1))
                horario_contatar += document.Form.horario_contato[i].value;
            else
                horario_contatar += document.Form.horario_contato[i].value + ", ";
        }
    }

    var forma_contatar = "";
    for (var i = 0; i < document.Form.forma_contato.length; i++) {
        if (document.Form.forma_contato[i].checked) {
            if (i == (document.Form.forma_contato.length - 1))
                forma_contatar += document.Form.forma_contato[i].value;
            else
                forma_contatar += document.Form.forma_contato[i].value + ", ";

        }
    }

    
    //Caso tenha chegado até aqui, envia-se o formulário de contato.
    var nome_ = document.getElementById('nome').value;

    var cidade_ = document.getElementById('cidade_drop_lista').options[document.getElementById('cidade_drop_lista').selectedIndex].text;
    var estado_ = document.getElementById('estado').options[document.getElementById('estado').selectedIndex].text; 
        
    var email_ = document.getElementById('email').value;
    var fone_ = document.getElementById('fone').value;

    var comentario_ = document.getElementById('comentario').value;
    var lista_imoveis = document.getElementById('imoveis_selecionados').value;

    if (!lista_interesse_contato.EnviarFormularioContato(nome_, estado_, cidade_, email_, fone_, comentario_, lista_imoveis, horario_contatar, forma_contatar).value)
    {
        document.getElementById('erro_contato').style.display = "";
        document.getElementById('mapa_contato').style.display = "none";
        document.getElementById('form_contato').style.display = "none";
        document.getElementById('tabela_interesse').style.display = "none";
    }
    else
    {
        document.getElementById('sucesso_contato').style.display = "";
        document.getElementById('mapa_contato').style.display = "none";
        document.getElementById('form_contato').style.display = "none";
        document.getElementById('tabela_interesse').style.display = "none";
    }
}


//Função assincrona do populaCidade.
function retorno_populaCidade(ret){
    var dt = ret.value;
    var cidade = document.getElementById('cidade_drop_lista');
    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 = "";
    lista_interesse_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 marcarTodosImoveis(id, objeto) {
    $('#' + id + '  input:checkbox').each(function () {
        if (objeto.checked) {
            $(this).parent("span").addClass("checked");
            $(this).attr('checked', true);
            marcaElemento(this);
        } else {
            $(this).parent("span").removeClass("checked");
            $(this).attr('checked', false);
            marcaElemento(this);
        }
    });
}

function marcaElemento(elemento) {
    if ($(elemento).attr('checked')) {

        $(elemento).parent('td').parent('tr').addClass('ativo');
        $(elemento).parent('span').parent('div').parent('td').parent('tr').addClass('ativo');

        var sel_imovel = $('#imoveis_selecionados').val();
        var imovel = ';' + $(elemento).val();
        sel_imovel = sel_imovel.replace(imovel, '');
        $('#imoveis_selecionados').val(sel_imovel);

        $('#imoveis_selecionados').val($('#imoveis_selecionados').val() + ';' + $(elemento).val());
    }
    else {

        $(elemento).parent('td').parent('tr').removeClass('ativo');
        $(elemento).parent('span').parent('div').parent('td').parent('tr').removeClass('ativo');
        var sel_imovel = $('#imoveis_selecionados').val();
        
        var imovel = ';' + $(elemento).val();
        sel_imovel = sel_imovel.replace(imovel, '');

        $('#imoveis_selecionados').val(sel_imovel);
    }
}

