addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

function tabAutom(quem)
{
	if ( ( quem.value.length == quem.maxLength ) ) {
		var i=0, j=0, indice=-1;
		// Localiza em qual form está o input no documento
		for (i=0; i<document.forms.length; i++) {
			for (j=0; j<document.forms[i].elements.length; j++) {
				if (document.forms[i].elements[j].name == quem.name) {
					indice=i;
					break;
				}
			}
			if (indice != -1)
				 break;
		}

		// Localiza o próximo input no documento
		for (i=0; i<document.forms[indice].elements.length; i++)
		{
			if (document.forms[indice].elements[i].getAttribute("tabindex") == quem.getAttribute("tabindex") + 1)
			{
				document.forms[indice].elements[i].focus();
			}
		}
	}
}

linha = String.fromCharCode(13);

function FuncaoJS() {
}

function ProtocoloIncorreto(strErro) {
	alert(strErro);
}

function abrir(url,janela,larg,alt,scroll,pos1,pos2){
	window.open(url,janela,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scroll+",resizable=no,copyhistory=no,top="+pos1+",left="+pos2+",screenY="+pos1+",screenX="+pos2+",width="+larg+",height="+alt);
}
function impressao(id) {
	abrir("conteudo-impressao.asp?id="+id,"impressao",600,500,"yes",(screen.width/2)-600,(screen.height/2)-500);
}

function imprimir() {
	document.getElementById("btprint").style.display = "none";
	print();
	document.getElementById("btprint").style.display = "block";
}

function impressaoprotocolo(param){
	abrir("protocolos-imprimir.asp?p="+param,"impressao",650,550,"yes",(screen.width/2)-600,(screen.height/2)-500);
}

function consultaProtocolo(quem, texto, acao) {
	if (acao == "Click" || acao == "Down") {
		if(quem.value == quem.getAttribute("v")) quem.value = "";
	}
	if (acao == "Out" || acao == "Change") {
		if(quem.value.length == 0) quem.value = quem.getAttribute("v");
	}
}

function mascaraConsulta() {
	var r = new Restrict("frmRCPJRJ");

	r.field.txtData = "\\d/";
    r.mask.txtData = "##/##/####";
    
	r.field.txtProtocolo = "\\d.";
    r.mask.txtProtocolo = "########";

	r.onKeyRefuse = function(o, k){
        o.style.backgroundColor = "#fdc";
    }
    r.onKeyAccept = function(o, k){
        if(k > 30)
            o.style.backgroundColor = "#FFF";
    }
    r.start();
	
	var f = document.forms[0];
	addEvent( f, 'submit', function() { return enviarConsultaProtocolo() } )
	
	var inputs = document.getElementsByTagName("input");
	for(var i in inputs) {
 		if(inputs[i].type == "text") {
			addEvent( inputs[i], 'blur', function() { this.style.backgroundColor = "#FFF"; } );
			addEvent( inputs[i], 'keyup', function() { tabAutom(this) } );
		}
	}
}

function enviarConsultaProtocolo() {
	var erros = "";
	with(document.forms[0]) {
		if ( (txtData.value == "") || (txtData.value == "Data do Protocolo") ) {
			erros += "A data do protocolo não foi preenchida.\n";
		} 
		else if ( (txtProtocolo.value == "") || (txtProtocolo.value == "Número do Protocolo") ) {
			erros += "O número do protocolo não foi preenchido.\n";
		}
			
		if (erros != "") {
			alert("Para enviar, corrija o(s) erro(s) abaixo: \n\n" + erros + "\n");
			return false;
		}
		else {
			var d = txtData.value.substr(0,2);
			var m = txtData.value.substr(3,2);
			var y = txtData.value.substr(6,4);
			var p = "1" + y + m + d + txtProtocolo.value;
			action = "protocolos.asp?p=" + p;
			submit();
		}
	}
}