﻿function post() {
    __doPostBack('__Page', 'Post');
}

//Seta MaxLength em TextBox MultiLine
function MaxLength(campo, tamanho) {
    if (campo.value.length >= tamanho) {
        alert('Atenção: A Quantidade máxima de ' + tamanho + ' caracteres excedeu.');
        campo.value = campo.value.substring(0, tamanho)
    }
}

//Mensagem de erro customizada, abre a shadowBox
function Mensagem(descricaoErro, caminho) {

    var msg;

    msg = '<div style="background-color: #fff; margin:5px; color: #000; padding:10px;font-size:10pt;font-family:Tahoma; height: 170px; text-align: justify;">' +
          '<table width="100%" border="0" celpadding="0" celspacing="0">' +
          '<tr>' +
          '<td><img src="' + caminho + 'js/images/alert.gif" alt="" /></td>' +
          '<td style="padding-left: 10px;">' +
          descricaoErro +
          '</td>' +
          '</tr>' +
          '</table>' +
          '</div>';

    Shadowbox.open({
        player: 'html',
        content: msg,
        height: 200,
        width: 400,
        title: 'Atenção'
    });
}

var popup = null;
// Validação do browser
var isNav4, isNav, isIE;
if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
    isNav = (navigator.appName == "Netscape") ? true : false;
    isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
}
if (navigator.appName == "Netscape") {
    isNav4 = (parseInt(navigator.appVersion.charAt(0)) == 4);
}

//*** Modal shadow box
function ModalBox(url, width, height, tituloJanela) {

    Shadowbox.open({
        player: 'iframe',
        content: url,
        width: width,
        height: height,
        title: tituloJanela
    });

}

// ***************** Poup usada nos métodos de pesquisa **************************
function PopupGlobal(sURL, width, height, parametro) {
    var popup = null;
    var url = null;
    if (parametro != null) { url = sURL + parametro; } else { url = sURL; }
    if (popup != null && !popup.closed) { popup.focus(); } else {
        LeftPosition = (screen.width) ? (screen.width - width) / 2 : 0;
        TopPosition = (screen.height) ? (screen.height - height) / 2 : 0;
        settings = 'height=' + height + ',width=' + width + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=yes';
        popup = window.open(url, 'Global', settings);
        popup.focus();
    }
}
function openlink(URL, w, h) {
    LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
    TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
    settings = 'toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable=0,width=' + w + ',height=' + h + ',left=' + LeftPosition + ',top=' + TopPosition + ''
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "',settings);");
}
function tamanhoJanela(width, height, top, left) {
    this.dlgFeatures = "dialogHeight:" + height + "px;" +
  	  					"dialogWidth:" + width + "px;" +
  	  					"status:no;" +
  	  					"help:no;" +
  	  					"center:yes;" +
  	  					"edge:sunken;" +
  	  					"scroll:no";

}


function setValorBairro(campos, post_pagina_princ) {
    var array_id = campos.split('*');

    for (i = 0; i < array_id.length; i++) {
        window.parent.document.getElementById(array_id[i]).value = array_id[i + 1];
        i = i + 1;
    }

    if (post_pagina_princ == 'true') { window.parent.post(); }

    //window.close();    
}

//Seta o campos da página principal
function setValorUsuarioGlobal(campos, post_pagina_princ) {
    var array_id = campos.split('*');

    for (i = 0; i < array_id.length; i++) {
        window.parent.document.getElementById(array_id[i]).value = array_id[i + 1];
        i = i + 1;
    }

    if (post_pagina_princ == 'true') { window.parent.post(); }

    //window.close();    
}

//Seta o campos da página principal
function setValorPesquisa(campos, post_pagina_princ) {
    var array_id = campos.split('*');

    for (i = 0; i < array_id.length; i++) {
        window.opener.document.getElementById(array_id[i]).value = array_id[i + 1];
        i = i + 1;
    }

    if (post_pagina_princ == 'true') { window.opener.post(); }

    window.close();
}
//Seta o campos da página principal
function setValorPesquisaBox(campos, post_pagina_princ) {
    var array_id = campos.split('*');

    for (i = 0; i < array_id.length; i++) {
        window.parent.document.getElementById(array_id[i]).value = array_id[i + 1];
        i = i + 1;
    }

    if (post_pagina_princ == 'true') { window.parent.post(); }

    window.parent.FechaModal();
}

function FechaModal() { Shadowbox.close(); }

function AbreRelatorio(url, nome_relatorio) {
    Shadowbox.open({
        player: 'iframe',
        content: url,
        //width:      450,
        //height:     300,
        title: nome_relatorio
    });
}

/* Para usar radio button em Repeater */
function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

function formatar(src, mask) {
    var i = src.value.length;
    var saida = mask.substring(0, 1);
    var texto = mask.substring(i)
    if (texto.substring(0, 1) != saida) {
        src.value += texto.substring(0, 1);
    }
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode

    if ((charCode > 47 && charCode < 58) || (charCode > 95 && charCode < 106))
        return true;
    else {
        if (charCode == 8 || charCode == 9 || charCode == 46 || (charCode >= 37 && charCode <= 40)) // backspace, delete, setas, tab;
            return true;
        else
            return false;
    }

}

//valida o CPF digitado
function ValidarCPF(Objcpf) {
    //alert(Objcpf.value);
    var CPF = Objcpf.value;

    var wVr, wTam, wSoma, wSoma2, i, wDig1, wDig2,
		wVETOR_CC = new Array(11),
		wVETOR_PESO = new Array(11),
		Erro = false;

    wVr = "";

    for (i = 0; i < CPF.length; i++) {
        if (!isNaN(parseInt(CPF.charAt(i)))) {
            wVr = wVr + CPF.charAt(i);
        }
    }

    wTam = wVr.length + 1;

    if (wTam < 11) {
        alert("Faltam dígitos no CPF. Por favor, tente novamente.");
        return false;
    }


    if (wVr == '11111111111' || wVr == '22222222222' || wVr == '33333333333' || wVr == '44444444444' || wVr == '55555555555' ||
		wVr == '66666666666' || wVr == '77777777777' || wVr == '88888888888' || wVr == '99999999999') {
        alert("CPF Inválido. Por favor, tente novamente.");
        Objcpf.value = '';
        Objcpf.focus();
        return false;
    }

    wSoma = 0;
    wSoma2 = 0;

    for (i = 0; i < 11; i++) {
        wVETOR_CC[i] = wVr.charAt(i);
        wVETOR_PESO[i] = i + 2;
    }

    i = 0;
    while (i < 9) {
        i++;

        if (i < 10) {
            wSoma += wVETOR_CC[9 - i] * wVETOR_PESO[i - 1];
        }

        wSoma2 += wVETOR_CC[10 - i] * wVETOR_PESO[i - 1];
    }

    wDig1 = (wSoma * 10) % 11;
    wDig2 = (wSoma2 * 10) % 11;

    if (wDig1 == 10) {
        wDig1 = 0;
    }

    if (wDig2 == 10) {
        wDig2 = 0;
    }

    if (parseInt(wVr.charAt(9)) != wDig1 || parseInt(wVr.charAt(10)) != wDig2) {
        alert("CPF inválido. Por favor, tente novamente.");
        Objcpf.value = '';
        Objcpf.focus();
        return false;
    }

    return true;

}

//valida se digitou alguma coisa em pesquisa, diferente de TAB
//Limpando o controle hidden
function LimpaHiddenPesquisa(objTxt, objHdf) {

    var charCode = (objTxt.which) ? objTxt.which : event.keyCode

    if (charCode != 9) { //TAB
        objHdf.value = '';
    }

}

//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();
    }
}
function Limpar(valor, validos) {
    var result = "";
    var aux;
    for (var i = 0; i < valor.length; i++) {
        aux = validos.indexOf(valor.substring(i, i + 1));
        if (aux >= 0) {
            result += aux;
        }
    }
    return result;
}
function MascaraCNPJ(cnpj) {
    if (mascaraInteiro(cnpj) == false) {
        event.returnValue = false;
    }
    return formataCampo(cnpj, '00.000.000/0000-00', event);
}
function formataCampo(campo, Mascara, evento) {
    var boleanoMascara;

    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace(exp, "");

    var posicaoCampo = 0;
    var NovoValorCampo = "";
    var TamanhoMascara = campoSoNumeros.length; ;

    if (Digitato != 8) { // backspace 
        for (i = 0; i <= TamanhoMascara; i++) {
            boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/"))
            boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                TamanhoMascara++;
            } else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
            }
        }
        campo.value = NovoValorCampo;
        return true;
    } else {
        return true;
    }
}

function MascaraCep(cep) {
    if (mascaraInteiro(cep) == false) {
        event.returnValue = false;
    }
    return formataCampo(cep, '00.000-000', event);
}

function MascaraMesAno(anomes) {
    if (mascaraInteiro(anomes) == false) {
        event.returnValue = false;
    }
    return formataCampo(anomes, '00/0000', event);
}

function MascaraData(anomes) {
    if (mascaraInteiro(anomes) == false) {
        event.returnValue = false;
    }
    return formataCampo(anomes, '00/00/0000', event);
}

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();
    }
}


