function ById(s) {
	return document.getElementById(s);
}

function $_(a){
    return document.getElementById(a);
}

function TrimJS(campo){
    return campo.replace(/^\s+|\s+$/g, "");
}

var formatoData = 'yyyy-mm-dd';
var mensagemErro = "Campos em destaque contém valores inválidos ou sem preenchimento";
var mensagemErroEmail = "E-mail inválido";

(function($) {

    $.fn.validate = function(args) {
        

        /* Load the default options. */
        var options = $.extend({}, $.fn.validate.defaults, args);
        var jQ = jQuery;
        return this.each(function() {
            /***** Plugin Goes Here *********/
            jQ(this).submit(function(){
                var valid=true;
                var validEmail=true;
                jQ(this).find(".validate").each(function(){
                    var el = jQ(this);
                    var filtro = false;
                    if('filtro_' == el[0].name.substr(0, 7)){
                        filtro = true;
                        el[0].setAttribute("style", "border:1px solid;color:#FF0000");
                        //el[0].parentNode.setAttribute("style", "");
                    }else{
                        el[0].setAttribute("style", "");
                        //el[0].parentNode.setAttribute("style", "");
                    }

                    if(el.hasClass("email")) {
                        if(!valid_email(el.val())) {
                            add_error(el,filtro);
                            validEmail=false;
                        }
                    }
                    if(el.hasClass("date")) {
                        if(!valid_date(el.val(), formatoData)) {
                            add_error(el,filtro);
                            valid=false;
                        }
                    }
                    if(el.hasClass("number")) {
                        if(!valid_number(el.val())) {
                            add_error(el,filtro);
                            valid=false;
                        }
                    }
                    if(el.hasClass("required")) {
                        if(!valid_required(el.val())) {
                            add_error(el,filtro);
                            valid=false;
                        }
                    }
                });
                if(valid == false){
                    if($("#mensagem_erro")){
                        $("#mensagem_erro").attr("style", "display: block");
                        if(valid == false){
                            $("#mensagem_erro").html(mensagemErro);
                        }
                        if(validEmail == false){
                            $("#mensagem_erro").html(mensagemErroEmail);
                            valid=false;
                        }
                    }
                    var topo = document.URL+"#";
                    document.location.href = topo;
                }
                return valid;
            });

        });

    };

    //DESTACA O CAMPO COM ERRO
    function add_error(el,filtro) {
        if(!filtro){
            //el[0].parentNode.setAttribute("style", "background-color:#F8E5E5");
            el[0].setAttribute("style", "border: 1px solid; color:#ff6d6d" );
        }else{
            //el[0].parentNode.setAttribute("style", "");
            el[0].setAttribute("style", "");
        }
    }

    function valid_email(email) {
        var email_pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(email.match(email_pattern)) return true;
        if(email == ''){
            return true;
        }
        return false;
    }

    function valid_number(number) {
        var number_pattern = /^([0-9\s])+$/;
        if(number.match(number_pattern)){
            return true;
        }
        if(number == ''){
            return true;
        }
        return false;
    }

    function valid_required(val) {
        if(val.length>0) return true;
        return false;
    }

    function valid_date(date_passed, date_format) {
        var date_pattern = null;
        if(date_format == "m/d/Y") date_pattern  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
        if(date_format == "m-d-Y") date_pattern  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
        if(date_format == "d/m/Y") date_pattern  = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
        if(date_format == "d-m-Y") date_pattern  = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
        if(date_passed.match(date_pattern)) return true;
        if(date_passed == ''){
            return true;
        }
        return false;
    }

})(jQuery);

function mudaAction(id,action){
    var el = document.getElementById(id);
    el.setAttribute("action", action);
    el.submit();
}

//VALIDACAO DO FORMULARIO -  DEVE CONTER A CLASS "validate" no objeto FORM
$(document).ready(function() {
    $("form.validate").validate();
    $("#dataVistoria").setMask('date');
    $("#horaVistoria").setMask('time');
    $("#telefoneVistoria").setMask('phone');
    $("#dataReclamacoes").setMask('date');

    //Curriculo
    $("#telefone").setMask('phone');
    $("#celular").setMask('phone');
    $("#cep").setMask('cep');
    $("#data_nascimento").setMask('date');
    $("#ultima_data_inicio").setMask('date');
    $("#ultima_data_termino").setMask('date');
    $("#penultima_data_inicio").setMask('date');
    $("#penultima_data_termino").setMask('date');
    $("#antepenultima_data_inicio").setMask('date');
    $("#antepenultima_data_termino").setMask('date');
    $("#cpf").setMask('99999999999');

$("#formNewsLetter").bind("submit", function(){
    if(($("#news_nome").attr("value") == "")||($("#news_nome").attr("value") == "nome")){
        alert("Preencha o campo nome");
        $("#news_nome").focus();
        return false;
    }
    if(($("#news_email").attr("value") == "")||($("#news_email").attr("value") == "e-mail")){
        alert("Preencha o campo e-mail");
        $("#news_email").focus();
        return false;
    }
});

});

function validaCodigo(val, id, formulario){
    if (TrimJS(val) == '') {
        alert("O campo código de atendimento é obrigatório!");
        $_(id).value = '';
        $_(id).focus();
    }
    else {
        $_(formulario).submit();
    }
}

function mostraAssistencia(valor,id,idMsg){
    if(valor == 12){
        $_(id).style.display = "block";
        $_(idMsg).style.display = "none";
        $("#proprietarioVistoria").attr("class", "validate required");
        $("#empreendimentoVistoria").attr("class", "validate required");
        $("#dataVistoria").attr("class", "validate required");
        $("#horaVistoria").attr("class", "validate required");
        $("#telefoneVistoria").attr("class", "validate required");
        $("#unidadeVistoria").attr("class", "validate required");
        $("#mensagem").attr("class", "");
        $("#form_contato").attr("style", "background:url(./imagens/bg_contatomaior.png) no-repeat; height:827px;");
        $("#content .conteudo").attr("style", "height:847px; position:relative");
    }
    else{
        $("#form_contato").attr("style", "background:url(./imagens/bg_contato.png) no-repeat;height:744px;");
        $_(id).style.display = "none";
        $_(idMsg).style.display = "block";
        $("#proprietarioVistoria").attr("class", "");
        $("#empreendimentoVistoria").attr("class", "");
        $("#dataVistoria").attr("class", "");
        $("#horaVistoria").attr("class", "");
        $("#telefoneVistoria").attr("class", "");
        $("#unidadeVistoria").attr("class", "");
        $("#mensagem").attr("class", "");
        $("#content .conteudo").attr("style", "height:497px; position:relative");
    }
}

function exibirIndique() {
    $("#form_indique").attr("class", "envie show");
}

function FecharIndique() {
    $("#form_indique").attr("class", "envie hidden");
	return false;
}


