$(document).ready(function () {
    $('.slideshow').cycle({
        fx: 'fade',
        speed: 500,
        timeoutFn: timerSlide,
        pager: '#nav'
    });

    //center nav
    var cp = $("#banner_nav_container").width() / 2;
    var na = $("#banner_nav").width() / 2;
    $("#banner_nav_container").css("padding-left", (cp - na) + "px");

    $(".megamenu").megamenu();
    $('.abas').tabs();

    //mascaras
    $(".mk_data").mask("99/99/9999");
    $(".mk_fone").mask("(99) 9999-9999");
    $(".mk_cep").mask("99999-999");
    $(".mk_cpf").mask("999.999.999-99");
    $(".mk_cnpj").mask("99.999.999/9999-99");

    //dialog
    $(".janela").click(function (e) {
        e.preventDefault();
        var $this = $(this);
        var horizontalPadding = 30;
        var verticalPadding = 30;
        $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
            title: ($this.attr('title')) ? $this.attr('title') : 'Port',
            autoOpen: true,
            width: 400,
            height: 500,
            modal: true,
            resizable: true,
            autoResize: true
        }).width(400 - horizontalPadding).height(400 - verticalPadding);
    });
});

function timerSlide(currElement, nextElement, opts, isForward) {
    var index = opts.currSlide;
    var tempo = $(".slide:eq(" + index + ") a.tempo").attr("name");
    return parseInt(tempo);
}

 function validaCPF(source, args) {
   var cpf = $(".cpf").val();
   cpf = cpf.replace("-", "");
   cpf = cpf.replace("-", "");
   cpf = cpf.replace(".", "");
   cpf = cpf.replace(".", "");
   cpf = cpf.replace("/", "");
   cpf = cpf.replace("/", "");
   cpf = cpf.replace("(", "");
   cpf = cpf.replace("(", "");
   cpf = cpf.replace(")", "");
   cpf = cpf.replace(")", "");
   cpf = cpf.replace(" ", "");
   cpf = cpf.replace(" ", "");
    args.IsValid = cpfCNPJ(cpf);
    return;
    }

function cpfCNPJ(theCPF) {
    if (theCPF == "") {
        alert("Informe o CPF ou CNPJ");
        theCPF.focus();
        return (false);
    }

    if (((theCPF.length == 11) && (theCPF == 11111111111) || (theCPF == 22222222222) || (theCPF == 33333333333) || (theCPF == 44444444444) || (theCPF == 55555555555) || (theCPF == 66666666666) || (theCPF == 77777777777) || (theCPF == 88888888888) || (theCPF == 99999999999) || (theCPF == 00000000000))) {
        alert("CPF/CNPJ incorreto");
        theCPF.focus();
        return (false);
    }

    if (!((theCPF.length == 11) || (theCPF.length == 14))) {
        alert("CPF/CNPJ incorreto.");
        theCPF.focus();
        return (false);
    }

    var checkOK = "0123456789";
    var checkStr = theCPF;
    var allValid = true;
    var allNum = "";
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i);
        for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break;
    if (j == checkOK.length) {
        allValid = false;
        break;
    }
    allNum += ch;
}
if (!allValid) {
    alert("Favor preencher somente com numeros o campo CPF/CNPJ.");
    theCPF.focus();
    return (false);
}
var chkVal = allNum;
var prsVal = parseFloat(allNum);
if (chkVal != "" && !(prsVal > "0")) {
    alert("CPF zerado !");
    theCPF.focus();
    return (false);
}

if (theCPF.length == 11) {
    var tot = 0;

    for (i = 2; i <= 10; i++)
        tot += i * parseInt(checkStr.charAt(10 - i));

    if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(9))) {
        alert("CPF/CNPJ incorreto.");
        theCPF.focus();
        return (false);
    }

    tot = 0;

    for (i = 2; i <= 11; i++)
        tot += i * parseInt(checkStr.charAt(11 - i));

    if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(10))) {
        alert("CPF/CNPJ incorreto.");
        theCPF.focus();
        return (false);
    }
}
else {
    var tot = 0;
    var peso = 2;

    for (i = 0; i <= 11; i++) {
        tot += peso * parseInt(checkStr.charAt(11 - i));
        peso++;
        if (peso == 10) {
            peso = 2;
        }
    }

    if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(12))) {
        alert("CPF/CNPJ incorreto.");
        theCPF.focus();
        return (false);
    }

    tot = 0;
    peso = 2;

    for (i = 0; i <= 12; i++) {
        tot += peso * parseInt(checkStr.charAt(12 - i));
        peso++;
        if (peso == 10) {
            peso = 2;
        }
    }

    if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(13))) {
        alert("CPF/CNPJ  incorreto.");
        theCPF.focus();
        return (false);
    }
}
return (true);
}





