﻿function Abrir(lugar) {
    open(lugar, "_self");
}

function Voltar() {
    history.back();
}

function Logar() {
    usuario = document.dadosLogin.usuario.value;
    senha = document.dadosLogin.senha.value;

    if (usuario != "" && senha != "") {
        document.dadosLogin.submit();
    } else {
        alert("Preencha os campos obrigatórios");
    }
}

function Imprimir(fram, tipo) {
    if (fram != null) {
        if (tipo == null) {
            top.document.frames[fram].focus();
        } else {
            parent.document.frames[fram].focus();
        }
    }
    window.print();
}

function Checando(obj, func) {
    if (obj.checked == true) {
        var check = true;
        obj.title = "Desmarcar Todos";
    } else {
        var check = false;
        obj.title = "Marcar Todos";
    }
    x = document.getElementsByTagName("input");

    for (i = 0; i < x.length; i++) {
        nome = x[i].name;
        if (x[i].type == "checkbox" && nome.indexOf("CHK_") != -1) {
            if (x[i].checked != check) {
                x[i].checked = check;
                if (func != null) {
                    eval(func);
                }
            }
        }
    }
}

function Apagar(pagina, uniqkey, valuniq, vars, redirect) {
    if (vars == null || vars == "") {
        vars = "";
    }
    if (confirm("Tem certeza de que deseja excluir este cadastro?")) {
        open(pagina + "?act=delete&" + uniqkey + "=" + valuniq + vars + "&redirect=" + escape(redirect), "_self");
    }
}

function ApagarSelecionados(pagina, uniqkey, vars, redirect) {
    if (vars == null || vars == "") {
        vars = "";
    }
    var str = "";
    x = document.getElementsByTagName("input");

    for (i = 0; i < x.length; i++) {
        nome = x[i].name;
        if (x[i].type == "checkbox" && nome.indexOf("CHK_") != -1 && x[i].checked == true) {
            str += "&" + uniqkey + "=" + nome.replace("CHK_", "");
        }
    }

    // Rotina para não trazer duas vezes dois valores
    if (redirect.indexOf(vars) != -1) {
        redirect = redirect.replace(vars, null);
    }

    if (str == "") {
        alert("Selecione ao menos um registro");
    } else {
        if (confirm("Tem certeza de que deseja excluir este(s) cadastro(s)?")) {
            open(pagina + "?act=del" + str + vars + "&redirect=" + redirect, "_self");
        }
    }
}

function ApagarSelecionados2(xnome) {
    var str = "";
    x = document.getElementsByTagName("input");
    
    for (i = 0; i < x.length; i++) {
        nome = x[i].name;
        if (x[i].type == "checkbox" && nome.indexOf("CHK_") != -1 && x[i].checked == true) {
            str += "&" + xnome + "=" + nome.replace("CHK_", "");
        }
    }
    
    if (str == "") {
        alert("Selecione ao menos um registro");
    } else {
        if (confirm("Tem certeza de que deseja excluir este(s) cadastro(s)?")) {
            open("[thispage1]?act=del" + str + "&redirect=[thispage]", "_self");
        }
    }
}

function SelectInputs(nome, num) {
    obj = xGetElementById(nome);
    len = xGetElementById(nome).length;
    //alert(obj.checked);
}

function Loading(num) {
    if (num != null) {
        Tamanho(num);
    }
    Init();
}

function Tamanho(numero) {
	var cont = 1;
	var linhas = 0;
	
	if (xGetElementById("TR_Linha")) {
		obj = document.getElementsByTagName("tr");
		
		for (i=0; i<obj.length; i++) {
			if (obj[i].id == "TR_Linha") {
				linhas++;
			}
		}
	} else {
		linhas = 1;
	}

	if (xGetElementById("V1")) {
	    for (i = 1; i <= numero * linhas; i++) {
	        xGetElementById("V" + i).width = xGetElementById("T" + cont).clientWidth;

	        if (cont % numero == 0) {
	            cont = 1;
	        } else {
	            cont++;
	        }
	    }
	}
}

function AbreGrupo(nome) {
    if (document.all["TBH_" + nome].style.display == "none") {
        document.all["TBH_" + nome].style.display = "";
        document.images["IMG_" + nome].src = eval("menos.src");
    } else {
        document.all["TBH_" + nome].style.display = "none";
        document.images["IMG_" + nome].src = eval("mais.src");
    }
}

function doLink(lugar, targ) {
    open(lugar, targ);
}

function xGetElementById(e) {
	if (typeof(e) != 'string') return e;
	if (document.getElementById) e = document.getElementById(e);
	else if (document.all) e = document.all[e];
	else e = null;
	return e;
}

function isURL(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
    return regexp.test(s);
}

function EmailValido(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(email)) {
        return true;
    } else {
        return false;
    }
}

function FormatCPF(cpf) {
    if (cpf != "") {
        cpf = cpf.substring(0, 3) + "." + cpf.substring(3, 6) + "." + cpf.substring(6, 9) + "-" + cpf.substring(9, 11);
        return (cpf);
    } else {
        return ("");
    }
}

function FormatRG(rg) {
    if (rg != "") {
        rg = rg.substring(0, 2) + "." + rg.substring(2, 5) + "." + rg.substring(5, 8) + "-" + rg.substring(8, 9);
        return (rg);
    } else {
        return ("");
    }
}

function Submitando() {
    event.cancelBubble = true;
    event.returnValue = false;
}

function Limpar() {
    document.dados.reset();
}

function LimparCampo(formulario, campo) {
    document[formulario][campo].value = "";
}

function img_on(nome) {
    if (memo != null) {
        document.images[memo].src = eval(memo + "off.src");
    }
    document.images[nome].src = eval(nome + "on.src");
}

function img_off(nome) {
    if (memo != null) {
        document.images[memo].src = eval(memo + "on.src");
    }
    document.images[nome].src = eval(nome + "off.src");
}

function Lupinha(pagina, filtros, filtros2, filtros3, filtros4, targt) {
    var plus, plus1, plus2;

    pagina = pagina.toString();
    if (filtros != null) {
        filtros = filtros.toString();
    } else {
        filtros = "";
    }
    if (filtros2 != null) {
        filtros2 = filtros2.toString();
        if (filtros != "") {
            plus = "&";
        } else {
            plus = "";
        }
    }
    if (filtros3 != null) {
        xfiltros3 = eval(filtros3);
        plus1 = xfiltros3.toString();
    } else {
        plus1 = "";
    }
    if (filtros4 != null) {
        xfiltros4 = eval(filtros4);
        if (xfiltros4 != false) {
            plus2 = xfiltros4;
        } else {
            plus2 = "";
            return;
        }
    } else {
        plus2 = "";
    }
    page = pagina + "?" + filtros + plus + filtros2 + plus1 + plus2;
    w = 600; h = 450;
    x = (screen.availWidth - w) / 2;
    y = (screen.availHeight - h) / 2;
    if (targt == null) {
        JP = window.open(page, "janela1", "toolbar=0, status=1, width=" + w + ", height=" + h + ", left=" + x + ", top=" + y + ", menubar=0, resizable=0, fullscreen=0, scrollbars=0");
        JP.focus();
    } else {
        open(page, targt);
    }
}

function LimparCampos(campos) {
    campos = campos.split(",");
    for (i = 0; i < campos.length; i++) {
        document.dados[campos[i]].value = "";
    }
}

function SoNumeros(e) {
    if (IsIE()) {
        var e = window.event;
        code = e.keyCode;
    } else {
        code = e.which;
    }

    if (!e.shiftKey) {
        if ((code < 48 || code > 57) && (code < 96 || code > 105) &&
			(code != 8 && code != 9 && code != 35 && code != 36 && code != 37 &&
			code != 38 && code != 39 && code != 40 && code != 46 && code != 13)) {
            if (IsIE()) {
                e.cancelBubble = true;
                e.returnValue = false;
            } else {
                e.preventDefault();
            }
        }
    } else {
        if (IsIE()) {
            e.cancelBubble = true;
            e.returnValue = false;
        } else {
            e.preventDefault();
        }
    }
}

function CampoDinheiro(e) {
    if (IsIE()) {
        var e = window.event;
        code = e.keyCode;
    } else {
        code = e.which;
    }

    if (!e.shiftKey) {
        if ((code < 48 || code > 57) && (code < 96 || code > 105) &&
			(code != 8 && code != 9 && code != 35 && code != 36 && code != 37 &&
			code != 38 && code != 39 && code != 40 && code != 46 && code != 188)) {
            if (IsIE()) {
                e.cancelBubble = true;
                e.returnValue = false;
            } else {
                e.preventDefault();
            }
        }
    } else {
        if (IsIE()) {
            e.cancelBubble = true;
            e.returnValue = false;
        } else {
            e.preventDefault();
        }
    }
}

function Pula(campo, proximo) {
    if (campo.value.length == campo.maxLength) {
        document.dados[proximo].focus();
    }
}

function CampoData() {
    code = event.keyCode;
    if (!event.shiftKey) {
        if ((code < 48 || code > 57) && (code < 96 || code > 105) &&
			(code != 8 && code != 9 && code != 35 && code != 36 && code != 37 &&
			code != 38 && code != 39 && code != 40 && code != 46 && code != 191 &&
			code != 111)) {
            event.cancelBubble = true;
            event.returnValue = false;
        }
    } else {
        event.cancelBubble = true;
        event.returnValue = false;
    }
}

function SemCaractere(caracs) {
    code = event.keyCode;
    if (caracs != null) {
        caracs = caracs.split(",");
        for (i = 0; i < caracs.length; i++) {
            if (code == caracs[i]) {
                event.cancelBubble = true;
                event.returnValue = false;
            }
        }
    }
}

function FormatHora(campo) {
    nome = campo.name;
    codigo = event.keyCode;
    if (!event.shiftKey) {
        if ((codigo < 48 || codigo >= 58) &&
			(codigo < 96 || codigo >= 106) && codigo != 46 &&
			codigo != 45 && codigo != 35 && codigo != 36 &&
			codigo != 37 && codigo != 39 && codigo != 8 &&
			codigo != 9) {
            event.cancelBubble = true;
            event.returnValue = false;
        }
    } else {
        event.cancelBubble = true;
        event.returnValue = false;
    }

    if (codigo != 46 && codigo != 8) {
        obj = document.dados[nome].value;
        tamanho = obj.length;
        if (tamanho == 2) {
            document.dados[nome].value += ":";
        }
    }
}

function FormatNota(campo) {
    nome = campo.name;
    codigo = event.keyCode;
    if (!event.shiftKey) {
        if ((codigo < 48 || codigo >= 58) &&
			(codigo < 96 || codigo >= 106) && codigo != 46 &&
			codigo != 45 && codigo != 35 && codigo != 36 &&
			codigo != 37 && codigo != 39 && codigo != 8 &&
			codigo != 9) {
            event.cancelBubble = true;
            event.returnValue = false;
        }
    } else {
        event.cancelBubble = true;
        event.returnValue = false;
    }

    if (codigo != 46 && codigo != 8) {
        obj = document.dados[nome].value;
        tamanho = obj.length;
        if (tamanho == 2) {
            document.dados[nome].value += ",";
        }
    }
}

function CNPJ(campo) {
    nome = campo.name;
    codigo = window.event.keyCode;
    if ((codigo < 48 || codigo >= 58) &&
		(codigo < 96 || codigo >= 106) && codigo != 46 &&
		codigo != 45 && codigo != 35 && codigo != 36 &&
		codigo != 37 && codigo != 39 && codigo != 8 &&
		codigo != 9) {
        window.event.returnValue = false;
        window.event.keyCode = 0;
    }

    if (codigo != 46 && codigo != 8) {
        cnpj = document.dados[nome].value;
        tamanho = cnpj.length;
        if (tamanho == 3) {
            document.dados[nome].value += ".";
        } else if (tamanho == 7) {
            document.dados[nome].value += ".";
        } else if (tamanho == 11) {
            document.dados[nome].value += "/";
        } else if (tamanho == 16) {
            document.dados[nome].value += "-";
        }
    }
}

function CPF(campo) {
    nome = campo.name;
    codigo = window.event.keyCode;
    if ((codigo < 48 || codigo >= 58) &&
		(codigo < 96 || codigo >= 106) && codigo != 46 &&
		codigo != 45 && codigo != 35 && codigo != 36 &&
		codigo != 37 && codigo != 39 && codigo != 8 &&
		codigo != 9) {
        window.event.returnValue = false;
        window.event.keyCode = 0;
    }

    if (codigo != 46 && codigo != 8) {
        cpf = document.dados[nome].value;
        tamanho = cpf.length;
        if (tamanho == 3) {
            document.dados[nome].value += ".";
        } else if (tamanho == 7) {
            document.dados[nome].value += ".";
        } else if (tamanho == 11) {
            document.dados[nome].value += "-";
        }
    }
}

function Ocorrencias(campo, txt) {
    var contador = 0;
    if (campo != "") {
        tam = campo.length;
        tam2 = txt.length;
        for (i = 0; i < tam; i++) {
            if (campo.substring(i, tam2 + i) == txt) {
                contador++;
            }
        }
    }
    return contador;
}

function IsNumeric(strString) {
    if (strString.toString() != "") {
        var strValidChars = "0123456789.";
        var strChar;
        var blnResult = true;

        for (i = 0; i < strString.length && blnResult == true; i++) {
            strChar = strString.charAt(i);
            if (strValidChars.indexOf(strChar) == -1) {
                blnResult = false;
            }
        }
        return blnResult;
    } else {
        return false;
    }
}

function replace2(str, valor1, valor2) {
    var str2 = "";
    for (q = 0; q < str.length; q = q + valor1.length) {
        if (str.substring(q, q + valor1.length) == valor1) {
            str2 += valor2;
        } else {
            str2 += str.substring(q, q + valor1.length);
        }
    }
    return str2;
}

function TiraHTML(txt) {
    var novo_txt = "";
    var sinal_maior = false;
    var quebra = new RegExp("\n", "gi");

    if (txt != "") {
        sinal_maior = false;

        for (i = 0; i < txt.length; i++) {
            if (txt.substring(i, i + 1) != "<" && sinal_maior == false) {
                novo_txt += txt.substring(i, i + 1);
            } else if (txt.substring(i, i + 1) == "<") {
                sinal_maior = true;
            } else if (txt.substring(i, i + 1) == ">") {
                sinal_maior = false;
            }
        }
        novo_txt = novo_txt.replace(quebra, "<br>");

        return novo_txt;
    }
}

function IsIE() {
    if (document.all) {
        return true;
    } else {
        return false;
    }
}

function Calendar2(nome, xform, xobj, xdiv, func) {
    if (xGetElementById("calendario").style.display != "") {
        formu = xform.name;
        
        if (document[formu][xobj]) {
            obj = document[formu][xobj].name;
        } else if (document[formu]["Master$Formulario2$" + xobj]) {
            obj = document[formu]["Master$Formulario2$" + xobj].name;
        }
        
        if (xGetElementById(xdiv)) {
            div = xdiv;
        } else {
            div = "Master_" + xdiv;
        }
        
        if (document[formu][obj].value != "") {
            data = document[formu][obj].value;
            dia = data.substring(0, data.indexOf("/"));
            mes = data.substring(data.indexOf("/") + 1, data.lastIndexOf("/"));
            ano = data.substring(data.lastIndexOf("/") + 1, data.length);
            //document.calendario.document.location = "inc_calendario.asp?dia=" + dia + "&mes=" + mes + "&ano=" + ano + "&objeto=" + obj + "&formulario=" + formu;
        } else {
            //document.calendario.document.location = "inc_calendario.asp?objeto=" + obj + "&formulario=" + formu;
        }

        DesapareceSels();
        
        if (div != null) {
            scrolltop = xGetElementById(div).scrollTop;
            scrollleft = xGetElementById(div).scrollLeft;
            altura = xGetElementById(div).scrollHeight;
            largura = xGetElementById(div).scrollWidth;
        } else {
            scrolltop = document.body.scrollTop;
            scrollleft = document.body.scrollLeft;
            altura = document.body.scrollHeight;
            largura = document.body.scrollWidth;
        }
        eval(nome).style.position = "absolute";
        if (eval(nome).offsetLeft + 201 > largura) {
            x = eval(nome).offsetLeft - (scrollleft + (eval(nome).offsetLeft + 201 - largura));
        } else {
            x = eval(nome).offsetLeft - scrollleft;
        }
        //y = eval(nome).offsetTop + 20;
        if (eval(nome).offsetTop + 214 > altura) {
            y = eval(nome).offsetTop - (scrolltop + (eval(nome).offsetTop + 214 - altura));
        } else {
            y = eval(nome).offsetTop - scrolltop;
        }
        if (div != null) {
            xGetElementById(div).style.position = "absolute";
            if (IsIE()) {
                posdiv = parseInt(xGetElementById(div).offsetTop);
            } else {
                posdiv = 0;
            }
            xGetElementById(div).style.position = "static";
            y = y + posdiv + 19;
        }
        //y = y - scrolltop;
        eval(nome).style.position = "static";
        xGetElementById("calendario").style.visibility = "visible";
        xGetElementById("calendario").style.left = x;
        xGetElementById("calendario").style.top = y;
        xGetElementById("calendario").style.display = "";
        
        if (IsIE()) {
            //document.Form1.objeto.value = obj;
            document.calendario.document.Form1.form.value = formu
            document.calendario.document.Form1.objeto.value = obj;
        } else {
            xGetElementById("calendario").contentWindow.document.Form1.form.value = obj;
            xGetElementById("calendario").contentWindow.document.Form1.objeto.value = obj;
            //xGetElementById("calendario").contentWindow.document.cal.objeto.value = document[formu][obj].name;
        }
        if (func != null) {
            if (IsIE()) {
                //xGetElementById("calendario").document.cal.func.value = func;
            } else {
                //xGetElementById("calendario").contentWindow.document.cal.func.value = func;
            }
        }
    } else {
        xGetElementById("calendario").style.display = "none";
        ApereceSels();
    }
}

function DesapareceCalendario() {
    xGetElementById("calendario").style.display = "none";
    ApereceSels();
}

function DesapareceSels() {
    elem = document.getElementsByTagName("select");
    total = document.getElementsByTagName("select").length;
    for (i = 0; i < total; i++) {
        eval(elem[i]).style.visibility = "hidden";
    }

    iframes = document.getElementsByTagName("iframe");
    for (i = 0; i < iframes.length; i++) {
        eval(iframes[i]).style.visibility = "hidden";
    }
}

function ApereceSels() {
    elem = document.getElementsByTagName("select");
    for (i = 0; i < elem.length; i++) {
        eval(elem[i]).style.visibility = "visible";
    }

    iframes = document.getElementsByTagName("iframe");
    for (i = 0; i < iframes.length; i++) {
        eval(iframes[i]).style.visibility = "visible";
    }
}

function Trim(txt) {
    var i = 0;
    while (txt.charAt(i) == " ") {
        i = i + 1;
    }
    var j = txt.length - 1
    while (txt.charAt(j) == " ") {
        j = j - 1;
    }
    return txt.substr(i, j - i + 1)
}

function FormataCampo(campo) {
    if (campo) {
        vax = campo.value;
        vax = Formata2(vax);
        vax = parseFloat(vax);
        if (isNaN(vax) == false) {
            campo.value = Formata(vax, '');
        } else {
            campo.value = "0,00";
        }
    }
}

function Formata(numero, tipo) {
    if (numero != "") {
        numero = numero.toString();
        numero = replace2(numero, " ", "");
        numero = Trim(numero);

        num = ""; num2 = ""; sinal = "";
        count = 0; jafoi = false;
        if (numero < 0) {
            sinal = "-";
            numero = numero.toString();
            numero = numero.replace("-", "");
        } else {
            numero = numero.toString();
        }
        tamanho = numero.length;
        //		if (numero.indexOf(",") != -1 && (numero.indexOf(".") == -1)) {
        //			numero = replace2(numero, ".", "");
        //			numero = replace2(numero, ",", ".");
        //			numero = parseFloat(numero);
        //			numero = numero.toString();
        //		}

        if (numero.indexOf(".") != -1) {
            for (j = tamanho; j > 0; j--) {
                if (jafoi == true) {
                    if (count % 3 == 0) {
                        if (count != 0) {
                            num += ".";
                        } else {
                            num += ":";
                        }
                    }
                    count++;
                }
                if (numero.substring(j, j - 1) == ".") {
                    jafoi = true;
                } else {
                    num += numero.substring(j, j - 1);
                }
            }
            if (num.indexOf(":") == 1) {
                num = "0" + num;
            }
        } else {
            for (j = tamanho; j > 0; j--) {
                num += numero.substring(j, j - 1);
                count++;
                if (count % 3 == 0 && j != 1) {
                    if (count != 0) {
                        num += ".";
                    } else {
                        num += ":";
                    }
                }
            }

            //			if (num.substring(0, 1) == ".") {
            //				num = num.replace(".", "");
            //			}
            num = "00," + num;
        }

        for (j = num.length; j > 0; j--) {
            num2 += num.substring(j, j - 1);
        }
        num2 = num2.replace(":", ",");

        if (num2.indexOf(",") != -1) {
            diferenca = num2.length - (num2.indexOf(",") + 1);
            if (diferenca > 2) {
                num2 = num2.substring(0, num2.indexOf(",") + 3);
            }
        }

        if (tipo == "P") {
            num2 = sinal + num2 + "%";
        } else if (tipo == "R") {
            num2 = "R$ " + sinal + num2;
        } else {
            num2 = sinal + num2;
        }
        return num2;
    } else {
        if (tipo == "P") {
            num2 = "0,00%";
        } else if (tipo == "R") {
            num2 = "R$0,00";
        } else {
            num2 = "0,00";
        }
        return num2;
    }
}

function Formata2(valor) {
    if (valor != "" && valor != null) {
        if (valor.indexOf(",") != -1) {
            valor = replace2(valor, ".", "");
            valor = replace2(valor, ",", ".");
        }
        return valor;
    } else {
        return "";
    }
}

function criaAJAX() {
    var xmlhttp = false;
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
        try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (E) { xmlhttp = false; }
    }
    if (!xmlhttp) xmlhttp = new XMLHttpRequest();
    return xmlhttp;
}

function XMLParser() {
    var xmlDoc, ie;
    
    this.create = function() {
        var pser = null;
        if (window.ActiveXObject) {
            pser = new ActiveXObject("Microsoft.XMLDOM");
            pser.async = false;
            this.ie = true;
        } else if (document.implementation && document.implementation.createDocument) {
            pser = document.implementation.createDocument("", "", null);
            this.ie = false;
        }
        return pser;
    }
    
    this.load = function(file) {
        this.xmlDoc.load(file);
        if (this.ie)
            this.callback();
        else
            this.xmlDoc.onload = this.callback;
    }
    
    this.xmlDoc = this.create();
    
    this.callback = function() {
        alert("Funçao de callback não definida");
    };
}

function basicAJAX(page, data) {
    var i, retorno, data_qtt = 0;
    if (data != undefined && data.length > 0) {
        data_qtt++;
        for (i = 0; i < data.length; i++) if (data.substr(i, 1) == '&') data_qtt++;
    }
    ajax.open("POST", page, true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", data_qtt);
    ajax.onreadystatechange = function() { if (ajax.readyState == 4) return (ajax.responseText); }
    ajax.send(data);
}
