//<|fnc:getAncestorNode;data:2005.08.26;autor:Leandro N. Camargo|>
function getAncestorNode(obj,lvl) {
	for(var i=0;i<lvl;i++) {
		if(!obj.parentNode) return false;
		obj = obj.parentNode;
	}
	return obj;
}
//<|@fnc:getAncestorNode|>
//<|fnc:getCloserAncestorElement;data:2005.08.26;autor:Leandro N. Camargo|>
function getCloserAncestorElement(a,b) { // pega o mais proximo elemento 'b' ancestral de 'a'
	a=typeof a=="string"?crossObj(a):a;if(!a)return false;b=b.toLowerCase();
	for(var c=false;(a=a.parentNode).nodeName!='BODY';a.nodeName.toLowerCase()==b?(c=!c?a:c):null);
	return c;
}
//<|@fnc:getCloserAncestorElement|>
function abreJan(url,largura,altura) {
	if( !largura ) largura=760;
	if( !altura ) altura=550;
	window.open(url,"imagem","resizable=no,toolbar=no,status=no,top=" + (screen.height - altura)/2 +",left=" + (screen.width - largura)/2 + ",menubar=no,scrollbars=auto,width=" + largura + ",height=" + altura);
}
function window_random_open( u, w, h, f, wn ) { // u : URL; w : width; h : height; f : frequência; wn : window name;
	if( !w ) w = 760;
	if( !h ) h = 550;
	if( !f || f < 0 || f > 1 ) return false;
	if( Math.random() <= f ) window.open( u, wn, "resizable=no,toolbar=no,status=no,top=" + (screen.height - h)/2 +",left=" + (screen.width - w)/2 + ",menubar=no,scrollbars=auto,width=" + w + ",height=" + h);
}
function getClassNameFrom(/*cN[,tagnames]**/)
{
	if(!document.getElementsByTagName)return false;var args=getClassNameFrom.arguments;var n_args=args.length;
	if(n_args<1) return false;
	var cN=args[0];var tagnames=new Array();

	if(n_args==1) tagnames=['*'];
	else
	{
		for(var i=0;i<n_args-1;i++) tagnames[i]=args[i+1];
	}
	var arrReturn=new Array();
	var strrepl="aaa";
	cN=cN.replace(/\-/g,strrepl);
	var m=new RegExp("(\s+"+cN+")|("+cN+"\s+)|(\s+"+cN+"\s+)|([^\-_a-zA-Z0-9]*"+cN+"[^\-_a-zA-Z0-9]*)");
	var t1=tagnames.length;
	for(var i=0;i<t1;i++)
	{
		var currElems=document.getElementsByTagName(tagnames[i]);
		var t2=currElems.length;
		for(var j=0;j<t2;j++)
		{
			var strtemp=currElems[j].className;strtemp=strtemp.replace(/\-/g,strrepl);
			if(strtemp.match(m)) arrReturn[arrReturn.length]=currElems[j];
		}
	}
	return arrReturn;
}
function getCloserAncestorElement(a,b) { // pega o mais proximo elemento 'b' ancestral de 'a'
	a=typeof a=="string"?crossObj(a):a;if(!a)return false;b=b.toLowerCase();
	for(var c=false;(a=a.parentNode).nodeName!='BODY';a.nodeName.toLowerCase()==b?(c=!c?a:c):null);
	return c;
}
function addSTPB(a,b,c,d) // add Submit To Popup Button
//a={string} class name
//b={string} nome do elemento a ser buscado
//c={string} nome da janela
//d={string} parametros do popup
{
	if( !( a = getClassNameFrom(a,b) ) )return false;
	for( var t = a.length, e; t; ) {
		a[--t].t = t;
		if( ( e = getCloserAncestorElement(a[t],'form') ) ) {
			e.target = c;
			e.onsubmit = function() { var jan = window.open( '', c, d ); }
		}
	}
}
Array.prototype.has = function() {
	var a, t = ( a = this.has.arguments ).length, t2 = this.length;
	for( var i = 0; i < t2; i++ ) {
		for( var j = 0; j < t; j++ ) {
			if( a[j] == this[i] ) return a[j];
		}
	}
}
function crossObj(/*string ID [,stringId]+*/)
{
	var args = crossObj.arguments;
	var n_args = args.length;
	if(!n_args) return 0;
	
	if(n_args == 1)
	{
		if(document.getElementById) return document.getElementById(args[0]);
		else if(document.all) return document.all[args[0]];
		else return 0;
	}
	else
	{
		var objCollec = new Array(n_args);

		for(var i = 0; i < n_args; i++)
		{
			if(document.getElementById) objCollec[i] = document.getElementById(args[i]);
			else if(document.all) objCollec[i] = document.all[args[i]];
			else objCollec[i] = 0;
		}
	}
}
//<|fnc:mascaracpfcnpj;data:2005.12.07;autor:Rodolfo Alencar|>
function mascaracpfcnpj(S,campo){
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
	var dig = "";
	var espaco;
	var numero;
	numero = 0;
	espaco = 0;
	S = limpa_string(S);	
	for (var i=0; i<S.length; i++){
		numero = numero + 1;
		digito = S.charAt(i);
		if (i<=13) {
			if (Digitos.indexOf(digito)>=0){temp = temp + digito}
			if (numero == 2 ){temp = "-" + S.substring(S.length-2,S.length)}
			if (numero == 3 ){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) +"-"+ S.charAt(1) + S.charAt(2)}}
			if (numero == 4 ){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) + S.charAt(1) + "-" + S.charAt(2) + S.charAt(3)}}
			if (numero == 5 ){if (Digitos.indexOf(digito)>=0){temp = "."+S.charAt(0) + S.charAt(1) + S.charAt(2) + "-" + S.charAt(3) + S.charAt(4)}}
			if (numero == 6 ){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) +"."+ S.charAt(1) + S.charAt(2) + S.charAt(3) + "-" + S.charAt(4) + S.charAt(5)}}
			if (numero == 7 ){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) + S.charAt(1) +"."+ S.charAt(2) + S.charAt(3) + S.charAt(4) + "-" + S.charAt(5)+ S.charAt(6)}}
			if (numero == 8 ){if (Digitos.indexOf(digito)>=0){temp = "."+S.charAt(0) + S.charAt(1) + S.charAt(2) +"."+ S.charAt(3) + S.charAt(4) + S.charAt(5) + "-" + S.charAt(6)+ S.charAt(7)}}
			if (numero == 9 ){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) +"."+ S.charAt(1) + S.charAt(2) + S.charAt(3) +"."+ S.charAt(4) + S.charAt(5) + S.charAt(6) + "-" + S.charAt(7)+ S.charAt(8)}}
			if (numero == 10){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) + S.charAt(1) +"."+ S.charAt(2) + S.charAt(3) + S.charAt(4) +"."+ S.charAt(5) + S.charAt(6) + S.charAt(7) + "-" + S.charAt(8)+ S.charAt(9)}}
			if (numero == 11){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) + S.charAt(1) + S.charAt(2) +"."+ S.charAt(3) + S.charAt(4) + S.charAt(5) +"."+ S.charAt(6) + S.charAt(7) + S.charAt(8) + "-" + S.charAt(9) + S.charAt(10)}}
			if (numero == 12){if (Digitos.indexOf(digito)>=0){temp = "." + S.charAt(0) + S.charAt(1) + S.charAt(2) + "." + S.charAt(3) + S.charAt(4) + S.charAt(5) + "/" +  S.charAt(6) + S.charAt(7) + S.charAt(8) + S.charAt(9) + "-" + S.charAt(10) + S.charAt(11)}}
			if (numero == 13){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) + "." +  S.charAt(1) + S.charAt(2) + S.charAt(3) +"."+ S.charAt(4) + S.charAt(5) + S.charAt(6) +"/"+ S.charAt(7) + S.charAt(8) + S.charAt(9) + S.charAt(10) +"-"+ S.charAt(11) + S.charAt(12)}}
			if (numero == 14){if (Digitos.indexOf(digito)>=0){temp = S.charAt(0) + S.charAt(1) + "." + S.charAt(2) + S.charAt(3) + S.charAt(4) + "." + S.charAt(5) + S.charAt(6) + S.charAt(7) + "/" + S.charAt(8) + S.charAt(9) + S.charAt(10) + S.charAt(11) +"-"+ S.charAt(12) + S.charAt(13)}}
		}
	}
	//document.formulario.CGC.value = temp;
	campo.value = temp;
}
//<|@fnc:mascaracpfcnpj|>
//<|fnc:limpa_string;data:2005.12.07;autor:Rodolfo Alencar|>
function limpa_string(S){
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
	for (var i=0; i<S.length; i++){
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0){temp=temp+digito}
	}
	return temp;
}
//<|@fnc:limpa_string|>