function post() {
    __doPostBack('__Page', 'Post');
}

function FechaModal() { Shadowbox.close(); }

// Modal shadow box
function ModalBox(url, width, height, tituloJanela) {
    Shadowbox.open({
        player: 'iframe',
        content: url,
        width: width,
        height: height,
        title: tituloJanela
    });
}

// Abre Tela de Pagamento
function AbrePagamento(url, nome_relatorio) {
    Shadowbox.open({
        player: 'iframe',
        content: url,
        title: nome_relatorio
    });
}

// Função para congelar a tela no momento do processamento
function showLoading() { $('#divLoading').show(); }
function hideLoading() { $('#divLoading').hide(); }

function maskIt(w, e, m, r, a) {

    // Cancela se o evento for Backspace
    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

    // Variáveis da função
    var txt = (!r) ? w.value.replace(/[^\d]+/gi, '') : w.value.replace(/[^\d]+/gi, '').reverse();
    var mask = (!r) ? m : m.reverse();
    var pre = (a) ? a.pre : "";
    var pos = (a) ? a.pos : "";
    var ret = "";

    if (code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g, '').length) return false;

    // Loop na máscara para aplicar os caracteres
    for (var x = 0, y = 0, z = mask.length; x < z && y < txt.length; ) {
        if (mask.charAt(x) != '#') {
            ret += mask.charAt(x); x++;
        } else {
            ret += txt.charAt(y); y++; x++;
        }
    }

    // Retorno da função
    ret = (!r) ? ret : ret.reverse()
    w.value = pre + ret + pos;
}

// Novo método para o objeto 'String'
String.prototype.reverse = function() {
    return this.split('').reverse().join('');
};

//valida telefone
function ValidaTelefone(tel) {
    exp = /\(\d{2}\)\ \d{4}\-\d{4}/
    if (!exp.test(tel.value)) {
        alert('Numero de Telefone Invalido!');
        tel.value = '';
        tel.focus();
    }
}

//valida CEP
function ValidaCep(cep) {
    exp = /\d{2}\.\d{3}\-\d{3}/
    if (!exp.test(cep.value)) {
        //alert('Numero de Cep Invalido!');
        //cep.value = '';
        //cep.focus();        
    }
}

//valida data
function ValidaData(data) {
    exp = /\d{2}\/\d{2}\/\d{4}/
    if (!exp.test(data.value))
        alert('Data Invalida!');
}

//valida o CPF digitado
function ValidarCPF(Objcpf) {

    var cpf = Objcpf.value;

    if (cpf.toString() != '')
    {
        exp = /\.|\-/g
        cpf = cpf.toString().replace(exp, "");
        var digitoDigitado = eval(cpf.charAt(9) + cpf.charAt(10));
        var soma1 = 0, soma2 = 0;
        var vlr = 11;

        for (i = 0; i < 9; i++) {
            soma1 += eval(cpf.charAt(i) * (vlr - 1));
            soma2 += eval(cpf.charAt(i) * vlr);
            vlr--;
        }
        soma1 = (((soma1 * 10) % 11) == 10 ? 0 : ((soma1 * 10) % 11));
        soma2 = (((soma2 + (2 * soma1)) * 10) % 11);

        var digitoGerado = (soma1 * 10) + soma2;
        if (digitoGerado != digitoDigitado) {
            alert('CPF Invalido!');

            Objcpf.value = '';
            Objcpf.focus();
        }        
    }        
}

//valida numero inteiro com mascara
function mascaraInteiro() {
    if (event.keyCode < 48 || event.keyCode > 57) {
        event.returnValue = false;
        return false;
    }
    return true;
}

//valida o CNPJ digitado
function ValidarCNPJ(ObjCnpj) {
    var cnpj = ObjCnpj.value;
    var valida = new Array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
    var dig1 = new Number;
    var dig2 = new Number;

    exp = /\.|\-|\//g
    cnpj = cnpj.toString().replace(exp, "");
    var digito = new Number(eval(cnpj.charAt(12) + cnpj.charAt(13)));

    for (i = 0; i < valida.length; i++) {
        dig1 += (i > 0 ? (cnpj.charAt(i - 1) * valida[i]) : 0);
        dig2 += cnpj.charAt(i) * valida[i];
    }
    dig1 = (((dig1 % 11) < 2) ? 0 : (11 - (dig1 % 11)));
    dig2 = (((dig2 % 11) < 2) ? 0 : (11 - (dig2 % 11)));

    if (((dig1 * 10) + dig2) != digito) {
        alert('CNPJ Invalido!');

        ObjCnpj.value = '';
        ObjCnpj.focus();
        
        return false;
    }

    return true;      
}
function ValidarMAIL(mail) {
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if (typeof (mail) == "string") {
        if (er.test(mail)) { return true; }
    } else if (typeof (mail) == "object") {
    if (er.test(mail.value)) {            
            return true;
        }
    } else {
    alert('Email Invalido!');
    mail.value = '';
    mail.focus();
        return false;
    }
}
function handleEnter(field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        var i;
        for (i = 0; i < field.form.elements.length; i++)
            if (field == field.form.elements[i])
            break;
        i = (i + 1) % field.form.elements.length;
        field.form.elements[i].focus();
        return false;
    }
    else
        return true;
}
function Tecla(e) {
    if (document.all) // Internet Explorer
        var tecla = event.keyCode;
    else if (document.layers) // Nestcape
        var tecla = e.which;
    if (tecla > 47 && tecla < 58) // numeros de 0 a 9
        return true;
    else {
        if (tecla != 8) // backspace
            event.keyCode = 0;
        //return false;
        else
            return true;
    }
}

function changeColor(obj) {
    var tr = obj.parentNode.parentNode; // acrescentar ou retirar parentNode de acordo com seu HTML
    tr.style.backgroundColor = (obj.checked) ? '#D4BFFF' : '';
}

function SelecionaTodosChecks(spanChk) {

    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ?
        spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    elm = theBox.form.elements;


    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox" &&
              elm[i].id != theBox.id) {
            if (elm[i].checked != xState)
                elm[i].click();
        }
}

function mostraOculta(objDiv) {
    objDiv = objDiv.parentNode.getElementsByTagName('div')[0];
    if (objDiv.style.display == 'none') {
        qobjDivuem.style.display = 'block';
    } else {
        objDiv.style.display = 'none';
    }
}

// Recupera a numeração selecionada e marca como selecionada
function RecuperaGrade(numero) {
   
    if (document.getElementById("ContentPlaceHolder_hdnGrade").value == numero) {
        document.getElementById("numeracao_" + numero).className = "num-disponivel";
        document.getElementById("numeracao_" + numero).style.backgroundImage = "";
        document.getElementById("ContentPlaceHolder_hdnGrade").value = "";
    } else {
        if (document.getElementById("ContentPlaceHolder_hdnGrade").value == "") {
            document.getElementById("numeracao_" + numero).style.backgroundImage = "url(/image/img-num-selecionado.gif)";
            document.getElementById("ContentPlaceHolder_hdnGrade").value = numero;
        } else {
            document.getElementById("numeracao_" + document.getElementById("ContentPlaceHolder_hdnGrade").value).style.backgroundImage = "";
            document.getElementById("numeracao_" + numero).style.backgroundImage = "url(/image/img-num-selecionado.gif)";
            document.getElementById("ContentPlaceHolder_hdnGrade").value = numero;
        }
    }

}

function ExibeMsg(msgErro) {

    $('#msgErro').html(msgErro);
    $('#boxErro').show(500);

    if (typeof (tempo) != 'undefined') {
        clearTimeout(tempo);
    }

    tempo = setTimeout("$('#boxErro').hide(500);", 8000);
}

// Seleciona imagem Thumb
function ChangeImage(numero) {
    thumbs = new Array(8);
    thumbs[1] = document.thumb_1;
    thumbs[2] = document.thumb_2;
    thumbs[3] = document.thumb_3;
    thumbs[4] = document.thumb_4;
    thumbs[5] = document.thumb_5;
    thumbs[6] = document.thumb_6;
    thumbs[7] = document.thumb_7;
    thumbs[8] = document.thumb_8;
    aux = document.foto_principal.src;
    document.foto_principal.src = thumbs[numero].src;
    document.link_zoom.href = thumbs[numero].src;
    thumbs[numero].src = aux;
}
