function formatar(src, mask,event) {
	 var i = src.value.length;
	 var saida = mask.substring(i,i+1);
	 var ascii = event.keyCode;
	 if (saida == "A") {
	  if ((ascii >=97) && (ascii <= 122)) { 
	  	event.keyCode -= 32;
	  }else {
	  	event.keyCode = 0;
	  }
	}else if (saida == "0") {
  if ((ascii >= 48) && (ascii <= 57)) { return }
  else { event.keyCode = 0 }
 } else if (saida == "#") {
  return;
 } else {
  src.value += saida;
  i += 1
  saida = mask.substring(i,i+1);
  if (saida == "A") {
   if ((ascii >=97) && (ascii <= 122)) { event.keyCode -= 32; }
   else { event.keyCode = 0; }
  } else if (saida == "0") {
   if ((ascii >= 48) && (ascii <= 57)) { return }
   else { event.keyCode = 0 }
  } else { return; }
 }
}
//alternar de campo
function autotab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
	destination.focus()
}

function formataValor(campo) {

	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;
	if ( tam <= 2 ){ 
		campo.value = vr ; }
 	if ( (tam > 2) && (tam <= 5) ){
		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 6) && (tam <= 8) ){
		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 9) && (tam <= 11) ){
 		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 12) && (tam <= 14) ){
 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 15) && (tam <= 18) ){
		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
}

// limpa todos os caracteres especiais do campo solicitado
function filtraCampo(campo){
	var s = "";
	var cp = "";
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) {  
		if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "."  && vr.substring(i,i + 1) != "," ){
		 	s = s + vr.substring(i,i + 1);}
	}
	campo.value = s;
	return cp = campo.value
}

function popup(url,destino,largura,altura) {
	esquerda = (screen.width - largura)/2;
	topo = (screen.height - altura)/2;
	if (topo > 0 ) {
		topo = topo - 3;
	}
	settings='width='+largura+',height='+altura+',top='+topo+',left='+ esquerda +',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no, modal=yes'; 			
	//window.open(url,destino,settings);
	//window.showModalDialog(pUrl, pArguments, pFeatures);
	if (window.showModalDialog) {
		settings=	"dialogWidth:" + largura + "px;dialogHeight:" + altura + "px;help:no;scroll:yes;status:no";
		return window.showModalDialog(url, self,settings);
	} else {
		try {
			netscape.security.PrivilegeManager.enablePrivilege( "UniversalBrowserWrite");
			window.open(url, destino,settings);
		}catch (e) {
			alert("Script n�o confi�vel, n�o � poss�vel abrir janela modal.");
		}
	}
}
function popupSimples(url,largura,altura) {
	esquerda = (screen.width - largura)/2;
	topo = (screen.height - altura)/2;
	if (topo > 0 ) {
		topo = topo - 3;
	}
	param = "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=" + largura + ",height=" + altura + ",top=" + topo + ",left=" + esquerda;
	window.open(url,"RELATORIO",param);
}

function Limpar(valor, validos) {
	  // retira caracteres invalidos da string
	  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;
}

//Formata o campo valor
function formataNumerico(campo) {
	valorAnterior = campo.value
	valorAtual = Limpar(campo.value, "0123456789");
	if (valorAnterior!=valorAtual) campo.value = Limpar(campo.value, "0123456789");
}


