/* Version 0.27 */

/**   * Função Principal
 * @param w - O elemento que será aplicado (normalmente this).
 * @param e - O evento para capturar a tecla e cancelar o backspace.
 * @param m - A máscara a ser aplicada.
 * @param r - Se a máscara deve ser aplicada da direita para a esquerda. Veja Exemplos.
 * @param a -
 * @returns null   */
function maskIt(w,e,m,r,a){
    // Cancela se o evento for Backspace
  if(e!='undefined'){
    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
  }else{
  	var code = 0;
  }
    // Variáveis da função
  var txt  = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();
  var mask = (!r) ? m : m.reverse();
  var pre  = (a ) ? a.pre : "";
  var pos  = (a ) ? a.pos : "";
  var ret  = "";

  if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;

  // Loop na máscara para aplicar os caracteres
  for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
      if(mask.charAt(x)!='#'){
          ret += mask.charAt(x); x++;
      } else{
          ret += txt.charAt(y); y++; x++;
      }
  }
    // Retorno da função
  ret = (!r) ? ret : ret.reverse();
         w.value = pre+ret+pos;
         
}

function abrirOrganogramaPopup(url){
	open(url,"Organograma DPRF",'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=700,height=500');
}

/**
 * Tirar espaçoes
 */
String.prototype.trim = function()
{
return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function tirarEspaco(campo){
	campo.value = campo.value.trim();
}

// Novo método para o objeto 'String'
String.prototype.reverse = function(){
  return this.split('').reverse().join('');
};


/**   * Exemplos
 * CPF: <input type="text" name="cpf" onkeyup="maskIt(this,event,'###.###.###-##')" />
 * TELEFONE: <input type="text" name="fone" onkeyup="maskIt(this,event,'(##)####-####')" />
 * <INPUT TYPE="Text" NAME="dinheiro" SIZE="40" onkeyup="maskIt(this,event,'###.###.###,##',true,{pre:'R$',pos:''})">
 * GRAUS: <input type="text" name="graus" onkeyup="maskIt(this,event,'###,#',true,{pre:'',pos:'º'})" />
 *
 *
 */
// Sub Nav Script

var currentSubMenu = "";
var currentMenuItem = "";
var menuItemNormal = "";

function setCurrentMenuItem(item, normal) {
  if(currentMenuItem != MM_findObj(item)) {
    currentMenuItem.src = menuItemNormal;
    currentMenuItem = MM_findObj(item);
    menuItemNormal = normal;
  }
}

function toggleList(id) {
  list = document.getElementById(id);
  if (list.style.display == "inline") {
    list.style.display = "none";
  } else {
    if(currentSubMenu != "") {
      prev = document.getElementById(currentSubMenu);
      prev.style.display = "none";
    }
    list.style.display = "inline";
  }
  currentSubMenu = id;
}

function writeDiv(id) {
  if (id == currentMenuItem) {
    document.write("<div id=\"" + id + "\" style=\"display:inline\">");
  } else {
    document.write("<div id=\"" + id + "\" style=\"display:none\">");
  }
}

// Swap Textbox Form Element Contact

function setBgImageContact(element,imageFile) {
  element.style.backgroundImage="url("+imageFile+")";
  element.style.color="#000000";
}

function setBgImageOverContact(element,imageFile) {
  element.style.backgroundImage="url("+imageFile+")";
  element.style.color="#FFFFFF";
}

function imageon(imgName)
{
	if (document.images)
		document[imgName].src = eval(imgName + 'on.src');
}

function imageoff(imgName)
{
	if (document.images)
		document[imgName].src = eval(imgName + 'off.src');

}

function navBG(id, which)
{
	if (which == "off")
		document.getElementById(id).style.backgroundImage = "url(/images/navsub_bg_off.gif)";
	else
		document.getElementById(id).style.backgroundImage = "url(/images/navsub_bg_on.gif)";
}

//Open Browser Window

function OpenBrWindow(theURL,winName,features, myWidth, myHeight, isCenter) { //v3.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2; 
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  }
  window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function showHtml() { //v6.0
  var i,p,v,obj,args=showHtml.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function somenteNumeros(campo){
     var valor = "";
	 var teste = campo.value.toUpperCase();
   for (var i = 1;i<=teste.length;i++) {
      if(!isNaN(teste.charAt(i-1))) {
         valor += teste.charAt(i-1);
	  }
   }
   campo.value=valor;
}