// JavaScript Document
jQuery(document).ready(function ($) {

    //Numeric Mask
    $('#ctl00_cph_txtTelefoneFixo').mask('(99) 9999-9999');
    $('#ctl00_cph_txtTelefoneCelular').mask('(99) 9999-9999');
    $('#ctl00_cph_txtTelefoneVT').mask('(99) 9999-9999');
    $('#ctl00_cph_txtTelefone').mask('(99) 9999-9999');
    $('#ctl00_cph_txtCPF').mask('999.999.999-99');
    $('#ctl00_cph_txtCNPJ').mask('99.999.999/9999-99');
});

//EXPAND MAPA
$(document).ready(function () {
    $(".mapa_imoevis").hide();
    $("#abrirmapa").click(function () {
        $(this).next(".mapa_imoevis").slideToggle(200);
    });
    var open = false;
    $('.bt_prestar').click(function () {
        if (!open) {
            $('.form_prestar').show();
            open = true;
        } else {
            $('.form_prestar').hide();
            open = false;
        }
    });
});

//retira espaçoes à esquerda e direita
String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}
//retira espaçoes à esquerda
String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
}
//retira espaçoes à direita
String.prototype.rtrim = function () {
    return this.replace(/\s+$/, "");
}

// Accordion Menu
function initMenus() {
    $('#menu ul').hide();
    $.each($('ul#menu'), function () {
        $('#' + this.id + '.expandfirst ul:first').show();
    });

    $('#menu li a').click(
		function () {
		    var checkElement = $(this).next();
		    var parent = this.parentNode.parentNode.id;

		    if ($('#' + parent).hasClass('noaccordion')) {
		        $(this).next().slideToggle('normal');
		        return false;
		    }
		    if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		        if ($('#' + parent).hasClass('collapsible')) {
		            $('#' + parent + ' ul:visible').slideUp('normal');

		        }

		        return false;
		    }
		    if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		        $('#' + parent + ' ul:visible').slideUp('normal');
		        checkElement.slideDown('normal');
		        return false;
		    }
		}
	);
}

$(document).ready(function () {
    //Patch para fechar
    $('#menu li a').bind('click', function () {
        if (($(this).next().is('ul')) && ($(this).next().is(':visible'))) {
            $(this).next().slideToggle('normal');
        }
    });

    //Call Menu
    initMenus();

    //Tooltip
    $('#linkTooltip').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        extraClass: "right"
    });

    //jqDock
    var opts = {
        align: 'middle',
        size: 64,
        distance: 100,
        coefficient: 1.5,
        labels: 'bc',
        duration: 150//,
        //source: function(i){ return this.src.replace(/gif$/,'png'); }
    };

    $('#dock').jqDock(opts);

    /*
    // Dock Menu
    $('#dockMenu1').Fisheye({
    maxWidth: 15,
    items: 'a',
    itemsText: 'span',
    container: '.dock-container2',
    itemWidth: 60,
    proximity: 80,
    alignment : 'right',
    valign: 'bottom',
    halign : 'right'
    });


    //Fisheye
    $('#fisheye').Fisheye(
    {
    maxWidth: 50,
    items: 'a',
    itemsText: 'span',
    container: '.fisheyeContainter',
    itemWidth: 40,
    proximity: 90,
    halign : 'center'
    }
    )
    */
    //Fancybox
    $('a.fancy').fancybox({});

    //Migalha
    var qtd = $('#fixedBar ul li').length;
    var tag = '';
    for (i = 1; i < qtd; i++) {
        tag += 'ul ';
        var con = $('#fixedBar ul li:eq(' + i + ')').text().ltrim();
        con = con.substring(0, con.length - 2);
        if (i < qtd - 1) {
            $(tag + 'li:contains(' + con + ')').children().next()
		.css({ overflow: 'visible', visibility: 'visible', display: 'block' });
            //alert(i);
        } else {
            //alert(con);
            $('#menu a:contains(' + con + ')').addClass('submenu1');
        }
    }

    // carousel Imoveis Entregues
    jQuery('#mycarousel').jcarousel();

    // Listagem de Agenda do Mes / Show > Hide detalhes
    $('ul.search').hide();
    $('#busca h3').click(function () {
        $(this).next().slideToggle('fast');
    });

    // Listagem de Agenda do Mes / Show > Hide detalhes
    $('div#colapse').hide();
    $('h4#titShowHide').click(function () {
        $(this).next().slideToggle('fast');
    });

    $('div#colapseIngles').hide();
    $('h4#titShowHideIngles').click(function () {
        $(this).next().slideToggle('fast');
    });

    // Listagem de Agenda do Mes / Show > Hide detalhes
    /*$('div#patrimarcolapse').hide();
    $('h4#patrimarShowHide').click(function() {
    $(this).next().slideToggle('fast');
    });*/

    // Listagem de Agenda do Mes / Show > Hide detalhes
    //$('div.patrimarcolapse p').hide();
    $('div.parceiroColapse').hide();
    $('h4.tit_parceiros').click(function () {
        $(this).next().slideToggle('fast');
    });



    //Link Class
    $('p.link a').each(function () {
        $('a[href$="doc"]').addClass('linkDOC');
        $('a[href$="pdf"]').addClass('linkPDF');
        $('a[href$="zip"]').addClass('linkZIP');
        $('a[href$="xls"]').addClass('linkXLS');
        $('a[href$="ppt"]').addClass('linkPPT');
        $('a[href$="jpg"]').addClass('linkJPG');
    });
});

function UrlBusca() {
    var strCidade = "", strBairro = "";
    var msg = "";
    if (document.getElementById("ctl00_ddlEstados").value == "0") {
        msg += "<p>Selecione um Estado.</p>";
    }
    if (document.getElementById("ctl00_ddlCidades").value == "0") {
        msg += "<p>Selecione uma Cidade.</p>";
    }
    else {
        strCidade = document.getElementById("ctl00_ddlCidades").value;
    }

    if (document.getElementById("ctl00_ddlBairros").value != "0") {
        strBairro = document.getElementById("ctl00_ddlBairros").value;
    }

    if (msg != "") {
        abrir("<h2 class='Aamerelo Aalerta'>Aten&ccedil;&atilde;o!</h2>" + msg);
        return false;
    }
    else {
        window.open("http://loja.mrv.com.br/busca?w=www.mrv.com.br&b=hotsite&s=porquemrv&cidadeComercial=" + strCidade + "&bairroComercial=" + strBairro);
    }
}


function ValidaBusca() {

    if (document.getElementById('ctl00_txtBusca').value == "" || document.getElementById('ctl00_txtBusca').value == "Digite uma palavra-chave") {

        document.getElementById('ctl00_txtBusca').value = "Digite uma palavra-chave";
        return false;
    }

}

//function gravaCookie(id, sigla, nome){
//    document.getElementById("mapa").style.display = 'none';
//    location.href = 'http://imoveis.mrv.com.br/default.aspx?s='+sigla;
//}

function gravaCookie(id, sigla, nome, url) {
    document.getElementById("mapa").style.display = 'none';
    location.href = 'http://imoveis.mrv.com.br/' + url;
}

//mascaras
function mask(isNum, event, field, mask, maxLength) {
    var keyCode;
    if (event.srcElement)
        keyCode = event.keyCode;
    else if (event.target)
        keyCode = event.which;
    var maskStack = new Array();
    var isDynMask = false;
    if (mask.indexOf('[') != -1)
        isDynMask = true;
    var length = mask.length;
    for (var i = 0; i < length; i++)
        maskStack.push(mask.charAt(i));
    var value = field.value;
    var i = value.length;
    if (keyCode == 0 || keyCode == 8)
        return true;
    //código adaptado para aceitar X (maiúsculo) ou x (minúsculo), além de números
    if (isNum && (keyCode < 48 || keyCode > 57) && (keyCode != 88) && (keyCode != 120))
        return false;
    if (!isDynMask && i < length) {
        if (maskStack.toString().indexOf(String.fromCharCode(keyCode)) != -1 && keyCode != 8) {
            return false;
        } else {
            if (keyCode != 8) {
                if (maskStack[i] != '#') {
                    var old = field.value;
                    field.value = old + maskStack[i];
                }
            }
            if (autoTab(field, keyCode, length)) {
                if (!document.layers) {
                    return true;
                } else if (keyCode != 8) {
                    field.value += String.fromCharCode(keyCode);
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        }
    } else if (isDynMask) {
        var maskChars = "";
        for (var j = 0; j < maskStack.length; j++)
            if (maskStack[j] != '#' && maskStack[j] != '[' && maskStack[j] != ']')
                maskChars += maskStack[j];
        var tempValue = "";
        for (var j = 0; j < value.length; j++) {
            if (maskChars.indexOf(value.charAt(j)) == -1)
                tempValue += value.charAt(j);
        }
        value = tempValue + String.fromCharCode(keyCode);
        if (maskChars.indexOf(String.fromCharCode(keyCode)) != -1) {
            return false;
        } else {
            var staticMask = mask.substring(mask.indexOf(']') + 1);
            var dynMask = mask.substring(mask.indexOf('[') + 1, mask.indexOf(']'));
            var realMask = new Array;
            if (mask.indexOf('[') == 0) {
                var countStaticMask = staticMask.length - 1;
                var countDynMask = dynMask.length - 1;
                for (var j = value.length - 1; j >= 0; j--) {
                    if (countStaticMask >= 0) {
                        realMask.push(staticMask.charAt(countStaticMask));
                        countStaticMask--;
                    }
                    if (countStaticMask < 0) {
                        if (countDynMask >= 0) {
                            if (dynMask.charAt(countDynMask) != '#') {
                                realMask.push(dynMask.charAt(countDynMask));
                                countDynMask--;
                            }
                        }
                        if (countDynMask == -1) {
                            countDynMask = dynMask.length - 1;
                        }
                        realMask.push(dynMask.charAt(countDynMask));
                        countDynMask--;
                    }
                }
            }
            var result = "";
            var countValue = 0;
            while (realMask.length > 0) {
                var c = realMask.pop();
                if (c == '#') {
                    result += value.charAt(countValue);
                    countValue++;
                } else {
                    result += c;
                }
            }
            field.value = result;
            if (maxLength != undefined && value.length == maxLength) {
                var form = field.form;
                for (var i = 0; i < form.elements.length; i++) {
                    if (form.elements[i] == field) {
                        field.blur();
                        //if alterado para quando a máscara for utilizada no último campo, não dê mensagem de erro quando tentar colocar o foco no "Salvar"
                        //if (form.elements[i + 1] != null)           
                        if ((form.elements[i + 1] != null) && (form.elements[i + 1].name != "METHOD"))
                            form.elements[i + 1].focus();
                        break;
                    }
                }
            }
            return false;
        }
    } else {
        return false;
    }
    function autoTab(field, keyCode, length) {
        var i = field.value.length;
        if (i == length - 1) {
            field.value += String.fromCharCode(keyCode);
            var form = field.form;
            for (var i = 0; i < form.elements.length; i++) {
                if (form.elements[i] == field) {
                    field.blur();
                    //if alterado para quando a máscara for utilizada no último campo, não dê mensagem de erro quando tentar colocar o foco no "Salvar"
                    //if (form.elements[i + 1] != null)
                    if ((form.elements[i + 1] != null) && (form.elements[i + 1].name != "METHOD"))
                        form.elements[i + 1].focus();
                    break;
                }
            }
            return false;
        } else {
            return true;
        }
    }
}

//Validar o formulário
function ValidaFormulario() {
    var errTelRes = 0, errTelCel = 0;
    var errTelResIndicado = 0, errTelCelIndicado = 0;
    var reDigits = /\d+$/;
    var reDigtsInvalidos = RegExp("@|\'|\"|[\|!|@|#|$|%|¨|&|*|(|)|:|;|+|-|.|=|/|,|?|>|\||{|}|<|\'|\"]+$");
    var reEmail = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

    eval("reDigits = " + reDigits);
    eval("reDigtsInvalidos = " + reDigtsInvalidos);
    eval("reEmail = " + reEmail);

    if (document.getElementById("ctl00_cph_pnlIndicacao")) {


        //valida o campo nome		
        if (document.getElementById("ctl00_cph_txtNome").value.ltrim() == null || document.getElementById("ctl00_cph_txtNome").value.ltrim() == "") {
            document.getElementById("ctl00_cph_spnNome").innerHTML = "Preencha o Campo Nome.";
            return false;
        }
        else {
            if (reDigits.test(document.getElementById("ctl00_cph_txtNome").value.ltrim())) {
                document.getElementById("ctl00_cph_spnNome").innerHTML = "Nome Invalido.";
                document.getElementById("ctl00_cph_txtNome").value = "";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnNome").innerHTML = "";
            }

            if (reDigtsInvalidos.test(document.getElementById("ctl00_cph_txtNome").value.ltrim())) {
                document.getElementById("ctl00_cph_txtNome").value = "";
                document.getElementById("ctl00_cph_spnNome").innerHTML = "Nome Invalido.";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnNome").innerHTML = "";
            }

            if (reEmail.test(document.getElementById("ctl00_cph_txtNome").value.ltrim())) {
                document.getElementById("ctl00_cph_txtNome").value = "";
                document.getElementById("ctl00_cph_spnNome").innerHTML = "Nome Invalido.";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnNome").innerHTML = "";
            }
        }

        //valida o campo e-mail
        if (document.getElementById("ctl00_cph_txtEmail").value.ltrim() == null || document.getElementById("ctl00_cph_txtEmail").value.ltrim() == "") {
            document.getElementById("ctl00_cph_spnEmail").innerHTML = "Preencha o Campo E-mail.";
            return false;
        }
        else {
            if (reEmail.test(document.getElementById("ctl00_cph_txtEmail").value.ltrim())) {
                document.getElementById("ctl00_cph_spnEmail").innerHTML = "";
            }
            else {
                document.getElementById("ctl00_cph_spnEmail").innerHTML = "E-mail invalido.";
                return false;
            }
        }

        if (document.getElementById("ctl00_cph_txtCpf").value.ltrim() == null || document.getElementById("ctl00_cph_txtCpf").value.ltrim() == "") {
            document.getElementById("ctl00_cph_spnCpf").innerHTML = "Preencha o campo de CPF.";
            return false;
        }
        else if (ValidaCPF('ctl00_cph_txtCpf') == false) {

            document.getElementById("ctl00_cph_spnCpf").innerHTML = "Campo de CPF invalido.";
            return false;
        }
        else {
            document.getElementById("ctl00_cph_spnCpf").innerHTML = "";
        }

        //valida o campo Empreendimento		
        if (document.getElementById("ctl00_cph_txtEmpreendimento").value.ltrim() == null || document.getElementById("ctl00_cph_txtEmpreendimento").value.ltrim() == "") {
            document.getElementById("ctl00_cph_spnEmpreendimento").innerHTML = "Preencha o Campo Empreendimento.";
            return false;
        }
        else {
            if (reDigits.test(document.getElementById("ctl00_cph_txtEmpreendimento").value.ltrim())) {
                document.getElementById("ctl00_cph_txtEmpreendimento").value = "";
                document.getElementById("ctl00_cph_spnEmpreendimento").innerHTML = "Empreendimento Invalido.";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnEmpreendimento").innerHTML = "";
            }

            if (reDigtsInvalidos.test(document.getElementById("ctl00_cph_txtEmpreendimento").value.ltrim())) {
                document.getElementById("ctl00_cph_txtEmpreendimento").value = "";
                document.getElementById("ctl00_cph_spnEmpreendimento").innerHTML = "Empreendimento Invalido.";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnEmpreendimento").innerHTML = "";
            }

            if (reEmail.test(document.getElementById("ctl00_cph_txtEmpreendimento").value.ltrim())) {
                document.getElementById("ctl00_cph_txtEmpreendimento").value = "";
                document.getElementById("ctl00_cph_spnEmpreendimento").innerHTML = "Empreendimento Invalido.";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnEmpreendimento").innerHTML = "";
            }
        }

        //valida o campo Bloco		
        if (document.getElementById("ctl00_cph_txtBloco").value.ltrim() == null || document.getElementById("ctl00_cph_txtBloco").value.ltrim() == "") {
            document.getElementById("ctl00_cph_spnBloco").innerHTML = "Preencha o Campo Bloco.";
            return false;
        }

        //valida o campo Unidade		
        if (document.getElementById("ctl00_cph_txtUnidade").value.ltrim() == null || document.getElementById("ctl00_cph_txtUnidade").value.ltrim() == "") {
            document.getElementById("ctl00_cph_spnUnidade").innerHTML = "Preencha o Campo Unidade.";
            return false;
        }
        else {
            if (!reDigits.test(document.getElementById("ctl00_cph_txtUnidade").value.ltrim())) {
                document.getElementById("ctl00_cph_txtUnidade").value = "";
                document.getElementById("ctl00_cph_spnUnidade").innerHTML = "Unidade Invalida.";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnUnidade").innerHTML = "";
            }

            if (reDigtsInvalidos.test(document.getElementById("ctl00_cph_txtUnidade").value.ltrim())) {
                document.getElementById("ctl00_cph_txtUnidade").value = "";
                document.getElementById("ctl00_cph_spnUnidade").innerHTML = "Unidade Invalida.";
                return false;
            }
            else {
                document.getElementById("ctl00_cph_spnUnidade").innerHTML = "";
            }
        }

        //verifica se um dos telefones estão preenchidos
        //residencial		
        if ((document.getElementById("ctl00_cph_txtTelDDD").value.ltrim() == "" || document.getElementById("ctl00_cph_txtTelNum").value.ltrim() == "") || (document.getElementById("ctl00_cph_txtTelNum").value.length != 9 && document.getElementById("ctl00_cph_txtTelNum").value.length > 0)) {
            errTelRes = 1;
            document.getElementById("ctl00_cph_txtTelDDD").value = "";
            document.getElementById("ctl00_cph_txtTelNum").value = "";
        }

        //Celular
        if ((document.getElementById("ctl00_cph_txtCelDDD").value.ltrim() == "" || document.getElementById("ctl00_cph_txtCelNum").value.ltrim() == "") || (document.getElementById("ctl00_cph_txtCelNum").value.length != 9 && document.getElementById("ctl00_cph_txtCelNum").value.length > 0)) {
            errTelCel = 1;
            document.getElementById("ctl00_cph_txtCelDDD").value = "";
            document.getElementById("ctl00_cph_txtCelNum").value = "";
        }

        //soma os valores se for menor que 2, um telefone está preenchido
        if ((errTelRes + errTelCel) == 2) {
            document.getElementById("ctl00_cph_spnTelefone").innerHTML = "Preencha no minimo um telefone com o ddd.";
            return false;
        }
        else {
            document.getElementById("ctl00_cph_spnTelefone").innerHTML = "";
        }

    }

    //valida o campo nome do indicado		
    if (document.getElementById("ctl00_cph_txtNomeIndicado").value.ltrim() == null || document.getElementById("ctl00_cph_txtNomeIndicado").value.ltrim() == "") {
        document.getElementById("ctl00_cph_spnNomeIndicado").innerHTML = "Preencha o Campo Nome do Indicado.";
        return false;
    }
    else {
        if (reDigits.test(document.getElementById("ctl00_cph_txtNomeIndicado").value.ltrim())) {
            document.getElementById("ctl00_cph_txtNomeIndicado").value = "";
            document.getElementById("ctl00_cph_spnNomeIndicado").innerHTML = "Nome do Indicado Invalido.";
            return false;
        }
        else {
            document.getElementById("ctl00_cph_spnNomeIndicado").innerHTML = "";
        }

        if (reDigtsInvalidos.test(document.getElementById("ctl00_cph_txtNomeIndicado").value.ltrim())) {
            document.getElementById("ctl00_cph_txtNomeIndicado").value = "";
            document.getElementById("ctl00_cph_spnNomeIndicado").innerHTML = "Nome do Indicado Invalido.";
            return false;
        }
        else {
            document.getElementById("ctl00_cph_spnNomeIndicado").innerHTML = "";
        }

        if (reEmail.test(document.getElementById("ctl00_cph_txtNomeIndicado").value.ltrim())) {
            document.getElementById("ctl00_cph_txtNomeIndicado").value = "";
            document.getElementById("ctl00_cph_spnNomeIndicado").innerHTML = "Nome do Indicado Invalido.";
            return false;
        }
        else {
            document.getElementById("ctl00_cph_spnNomeIndicado").innerHTML = "";
        }
    }

    //valida o campo e-mail
    if (document.getElementById("ctl00_cph_txtEmailIndicado").value.ltrim() == null || document.getElementById("ctl00_cph_txtEmailIndicado").value.ltrim() == "") {
        document.getElementById("ctl00_cph_spnEmailIndicado").innerHTML = "Preencha o Campo E-mail do indicado.";
        return false;
    }
    else {
        if (reEmail.test(document.getElementById("ctl00_cph_txtEmailIndicado").value.ltrim())) {
            document.getElementById("ctl00_cph_spnEmailIndicado").innerHTML = "";
        }
        else {
            document.getElementById("ctl00_cph_spnEmailIndicado").innerHTML = "E-mail invalido.";
            return false;
        }
    }


    //Valida o campo Estado do indicado
    if (document.getElementById("ctl00_cph_ddlEstados").value == 0) {
        document.getElementById("ctl00_cph_spnEstadoIndicado").innerHTML = "Preencha o campo Estado.";
        return false;
    }
    else {
        document.getElementById("ctl00_cph_spnEstadoIndicado").innerHTML = ""
    }

    //Valida o campo Cidade do indicado
    if (document.getElementById("ctl00_cph_ddlCidades").value == 0) {
        document.getElementById("ctl00_cph_spnCidadeIndicado").innerHTML = "Preencha o campo Cidade do indicado.";
        return false;
    }
    else {
        document.getElementById("ctl00_cph_spnCidadeIndicado").innerHTML = "";
    }
    //verifica se um dos telefones estão preenchidos
    //residencial do indicado		
    if ((document.getElementById("ctl00_cph_txtTelDDDIndicado").value.ltrim() == "" || document.getElementById("ctl00_cph_txtTelNumIndicado").value.ltrim() == "") || (document.getElementById("ctl00_cph_txtTelNumIndicado").value.length != 9 && document.getElementById("ctl00_cph_txtTelNumIndicado").value.length > 0)) {
        errTelResIndicado = 1;
        document.getElementById("ctl00_cph_txtTelDDDIndicado").value = "";
        document.getElementById("ctl00_cph_txtTelNumIndicado").value = "";
    }

    //Celular do indicado
    if ((document.getElementById("ctl00_cph_txtCelDDDIndicado").value.ltrim() == "" || document.getElementById("ctl00_cph_txtCelNumIndicado").value.ltrim() == "") || (document.getElementById("ctl00_cph_txtCelNumIndicado").value.length != 9 && document.getElementById("ctl00_cph_txtCelNumIndicado").value.length > 0)) {
        errTelCelIndicado = 1;
        document.getElementById("ctl00_cph_txtCelDDDIndicado").value = "";
        document.getElementById("ctl00_cph_txtCelNumIndicado").value = "";
    }

    //soma os valores se for menor que 2, um telefone está preenchido
    if ((errTelResIndicado + errTelCelIndicado) == 2) {
        document.getElementById("ctl00_cph_spnTelIndicado").innerHTML = "Preencha no minimo um telefone com o ddd do indicado.";
        return false;
    }
    else {
        document.getElementById("ctl00_cph_spnTelIndicado").innerHTML = "";
    }

    if (document.getElementById("ctl00_cph_ddlCidadeInteresse").value == 0) {
        document.getElementById("spnCidadeInteresse").innerHTML = "Selecione uma Cidade de Interesse.";
        return false;
    }
    else {
        document.getElementById("spnCidadeInteresse").innerHTML = "";
    }


}

//Validador de CPF
function ValidaCPF(Campo) {
    CNUMB = document.getElementById(Campo).value;
    x = CNUMB;
    x = ClearStr(x, '-');
    x = ClearStr(x, '/');
    x = ClearStr(x, ',');
    x = ClearStr(x, '.');
    x = ClearStr(x, '(');
    x = ClearStr(x, ')');
    x = ClearStr(x, ' ');
    x = x.split('');
    todosIguais = 0;
    for (var i = 0, len = x.length; i < len; ++i) {
        j = 0;
        while (j < len) {
            if (x[i] != x[j])//primeiro igual ao segundo, passa
            {
                todosIguais = 0;
                break;
            }
            todosIguais = 1;
            ++j;
        }
    }

    if (todosIguais == 0) {

        if (Verify(CNUMB, 'CPF') == false) {
            document.getElementById("ctl00_cph_spnCpf").innerHTML = "Cpf Inv&aacute;lido!";
            return false;
        } else {
            return true;
        }
    } else {
        document.getElementById("ctl00_cph_spnCpf").innerHTML = "Cpf Inv&aacute;lido!";
        return false;
    }
}
function ClearStr(str, charo) {
    while ((cx = str.indexOf(charo)) != -1) {
        str = str.substring(0, cx) + str.substring(cx + 1);
    }
    return (str);
}
function ParseNumb(c) {
    c = ClearStr(c, '-');
    c = ClearStr(c, '/');
    c = ClearStr(c, ',');
    c = ClearStr(c, '.');
    c = ClearStr(c, '(');
    c = ClearStr(c, ')');
    c = ClearStr(c, ' ');
    if ((parseFloat(c) / c != 1)) {
        if (parseFloat(c) * c == 0) {
            return (c);
        }
        else {
            return (0);
        }
    }
    else {
        return (c);
    }
}
function Verify(CNUMB, CTYPE) {
    CNUMB = ParseNumb(CNUMB)
    if (CNUMB == 0) {
        return (false);
    }
    else {
        g = CNUMB.length - 2;
        if (TestDigit(CNUMB, CTYPE, g)) {
            g = CNUMB.length - 1;
            if (TestDigit(CNUMB, CTYPE, g)) {
                return (true);
            }
            else {
                return (false);
            }
        }
        else {
            return (false);
        }
    }
}

function TestDigit(CNUMB, CTYPE, g) {
    var dig = 0;
    var ind = 2;
    for (f = g; f > 0; f--) {
        dig += parseInt(CNUMB.charAt(f - 1)) * ind;
        if (CTYPE == 'CNPJ')
        { if (ind > 8) { ind = 2 } else { ind++ } }
        else
        { ind++ }
    }
    dig %= 11;
    if (dig < 2) {
        dig = 0;
    }
    else {
        dig = 11 - dig;
    }
    if (dig != parseInt(CNUMB.charAt(g))) {
        return false;
    }
    else {
        return (true);
    }
}

//somente numeros
function getNumeros(isNum, event, field) {
    var keyCode;
    if (event.srcElement)
        keyCode = event.keyCode;
    else if (event.target)
        keyCode = event.which;

    if (keyCode == 0 || keyCode == 8)
        return true;
    if (isNum && (keyCode < 48 || keyCode > 57))
        return true;
    else
        return false;
}

function validaCompraTerreno() {

    if (document.getElementById("ctl00_cph_ddlEstados").value == "0") {
        document.getElementById("spnEstado").innerHTML = "<br />Selecione um estado.";
        return false;
    }

    if (document.getElementById("ctl00_cph_ddlRegiao")) {
        if (document.getElementById("ctl00_cph_ddlRegiao").value == "0") {
            document.getElementById("spnRegiao").innerHTML = "<br />Selecione uma regi&atilde;o.";
            return false;
        }
    }
}

function PopChatDT(url, cidade, bairro, origem) {
    var popchat = window.open("http://www.mrv.com.br/corretores/chat/" + url + "?cidade=" + cidade + "&bairro=" + bairro + "&cod=" + (new Date().getTime()) + "&origem=" + origem, "Pop_Chat", "top=25,left=50,width=505,height=590,scrollbars=no,resizable=yes");
    popchat.focus();
}

$(document).ready(function () {
    $('.menu_topo li , .menu_topoen li').hover(function () {
        $('.menu_topo li a ,.menu_topoen li a').removeClass('active');
        menuOption = $(this).attr('class');
        //        $('.conteudo').removeClass('imoveisAVenda').removeClass('investidores').removeClass('institucional').removeClass('clientes');
        //        $('.conteudo').addClass(menuOption);
        $(this).find('a').addClass('active');
    })
})


var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

function VerificaEmail(stremail) {
    if (reEmail.test(stremail)) {
        return true;
    } else if (stremail != null && stremail != "") {
        return false;
    }
}

function validarEmail(source, args) {
    //    validação de E-mail
    if ($('input[id*="txtEmail"').val() != "") {
        if (!VerificaEmail($('input[id*="txtEmail"]').val())) {
            alert("Campo E-mail Invalido!");
            args.IsValid = false;
        }
    }
}

function ValidarCpf(source, args) {
    var cpf = $('input[id*="txtCPF"]').val().replace(".", "").replace(".", "");
    var cpfReal = $('input[id*="txtCPF"]').val().replace(".", "").replace(".", "").replace("-", "");
    var validacao = true;

    if (cpfReal.length == 11) {

        $.each(cpfReal, function (i, val) {
            $.each(cpfReal, function (j, val) {
                if (i != j) {
                    if (cpfReal.substr(i, 1) != cpfReal.substr(j, 1)) {
                        validacao = false;
                    }
                }
            });
        });

        if (!validacao) {
            cpf = cpf.substring(0, cpf.indexOf("-"));
            cpf += somaCpf(cpf, 10);
            cpf += somaCpf(cpf, 11);

            if (cpf != cpfReal) {
                alert("Campo CPF Invalido!");
                args.IsValid = false;
            }
        }
        else {
            alert("Campo CPF Invalido!");
            args.IsValid = false;
        }
    }
    else {
        alert("Campo CPF Invalido!");
        args.IsValid = false;
    }
}

function somaCpf(cpf, quant) {

    var soma = 0, cont = 0, mult = 0, num = 0, resto = 0;
    var arr = new Array(11, 10, 9, 8, 7, 6, 5, 4, 3, 2);

    if (quant == 11) {
        cont = 0;
    }
    else if (quant == 10) {
        cont = 1;
    }

    for (i = 0; i < (quant - 1); i++) {
        mult = arr[i + cont] * parseInt(cpf.substr(i, 1));
        soma += parseInt(mult);
    }

    resto = soma % 11;

    if (resto > 1) {
        num = 11 - resto;
    }
    else {
        num = 0;
    }

    return num
}

function ValidarCnpj(source, args) {
    var cnpj = $('input[id*="txtCNPJ"]').val().replace(".", "").replace(".", "");
    var cnpjReal = $('input[id*="txtCNPJ"]').val().replace(".", "").replace(".", "").replace("/", "").replace("-", "");

    var validacao = true;

    if (cnpjReal.length == 14) {

        $.each(cnpjReal, function (i, val) {
            $.each(cnpjReal, function (j, val) {
                if (i != j) {
                    if (cnpjReal.substr(i, 1) != cnpjReal.substr(j, 1)) {
                        validacao = false;

                    }
                }
            });
        });
        if (!validacao) {
            cnpj = cnpj.replace("/", "");
            cnpj = cnpj.substring(0, cnpj.indexOf("-"));
            cnpj += somaCnpj(cnpj, 13);
            cnpj += somaCnpj(cnpj, 14);

            if (cnpj != cnpjReal) {
                alert("Campo CNPJ Invalido!");
                args.IsValid = false;
            }
        }
        else {
            alert("Campo CNPJ Invalido!");
            args.IsValid = false;
        }
    }
    else {
        alert("Campo CNPJ Invalido!");
        args.IsValid = false;
    }
}

function somaCnpj(cnpj, quant) {

    var soma = 0, cont = 0, mult = 0, num = 0, resto = 0;
    var arr = new Array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);

    if (quant == 14) {
        cont = 0;
    }
    else if (quant == 13) {
        cont = 1;
    }

    for (i = 0; i < (quant - 1); i++) {
        mult = arr[i + cont] * parseInt(cnpj.substr(i, 1));
        soma += mult;

    }

    resto = soma % 11;

    if (resto > 1) {
        num = 11 - resto;
    }
    else {
        num = 0;
    }

    return num
}

$(document).ready(function () {
    $('#ctl00_cph_btnEnviar').click(function () {
        $('#ctl00_cph_pnlFormulario').each(function () {
            if ($('span[id*="rfv"]').attr('visibility', 'visible')) {
                $('#divMsg').html('<b>*</b> - Campos Obrigat&oacute;rios');
                return false;
            }
        });
    });
});
