function ajustar_data(input, evento)
		{
         var BACKSPACE=  8; 
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37;          
         var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 

		if ((tecla<48)||(tecla>57)){
			evento.returnValue =false;
			return false;
		} else { 
			if((input.value.length==2)||(input.value.length==5)){
			    input.value=input.value + "/" ;
			}
		}
 return true;				
	}
	
function ajustar_hora(input, evento)
		{
         var BACKSPACE=  8; 
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37;          
         var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 

		if ((tecla<48)||(tecla>57)){
			evento.returnValue =false;
			return false;
		} else { 
			if((input.value.length==2)||(input.value.length==2)){
			    input.value=input.value + ":" ;
			}
		}
 return true;				
	}
function counterUpdate(campo,total,conta) {

    field = document.getElementById(campo);
    txt_field = document.getElementById(conta);
    txt_field.innerHTML = field.value.length;

if(field.value.length = total)
	field.value = field.value.substring(0, total);
}

// ### ABRE UMA NOVA JANELA
function AbrirJanelaCentralizada(mypage,myname,w,h,scroll){
var win = null;
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
if(win.window.focus){win.window.focus();}
}

function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      
          

// ### DESABILITA A DIGITAÇÃO DE LETRAS
function IsNumTel(obj,event)
{
    var str = obj.value;
    var Tecla = event.which;
    if(Tecla == null)
        Tecla = event.keyCode;

    //se nao for número nem parentesis ou espaço
    if ( Tecla < 48 || Tecla > 59) {
        event.returnValue = false;
	alert("Só deve ser digitados numeros inteiros!")
        return false;
    }
    event.returnValue = true;
    return true;
}

// ### DESABILITA O ENTER NOS CONTROLES
function desabilita()
{ 
  var tecla = event.keyCode;
  if ((tecla == 13))
   { return false;}
 
  return tecla;
}


// ### DESABILITA O BOTÃO DIREITO DO MOUSE
function Disable_Direito()
{
 
  if (event.button==2) 
    { alert('Opcao desabilitada.'); }
}



// ### FOCO NO CONTROLE
function Foco(campo)
{ campo.focus(); }

function SelecionaTexto(campo)
{ campo.select(); }

// ### VERIFICA SE O CARACTER É UM NÚMERO OU LETRA
function VerificaSenha(e)
{
  // 48 a 57 (0,1,2,3,4,56,7,8,9)
  // 8 (Backspace)
  // 0 (null)
  // 65 a 90  (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,X,Y,W,Z)
  // 97 A 122 (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,x,y,w,z)

  var NS = (navigator.appName == "Netscape")
  var Digit = parseInt(eval(((NS)?"e.which":"e.keyCode")))
	
  if (!(Digit>47 && Digit<58 || Digit == 8 || Digit == 0 || Digit>64 && Digit<91 || Digit>96 && Digit<123))
  { return false; }
}

// ### VERIFICA SE O CARACTER É UM NÚMERO
function VerificaNumeros(e)
{
  //- 48 a 57 (0,1,2,3,4,56,7,8,9)
  //- 8 (Backspace)
  //- 0 (null)

  // Internet Explorer
  if (document.all) 
    var tecla = event.keyCode;
  //Nestcape
  else if(document.layers) 
    var tecla = e.which;
  
  if (tecla>47&&tecla<58) 
      return true;
  else
  { if (tecla != 8)
      event.keyCode = 0; 
    else
      return true;
  }
}



// ### CARACTERES MAIÚSCULOS
function maiuscula(campo)
{
 var str = campo.value;
     campo.value = str.toUpperCase();
}


// ### CARACTERES MINÚSCULOS
function minuscula(campo)
{
 var str = campo.value;
    campo.value = str.toLowerCase();
}



// ### CONTA CARECTERES DO CAMPO. TRAVA DIGITAÇÃO QDO ALCANÇAR TAMANHO MÁXIMO
function ContadorTexto(campo,campocont,limite)
{
  if (campo.value.length > limite)
    campo.value = campo.value.substring(0,limite);
  else
    campocont.value = limite - campo.value.length;
}



// ### MUDA A COR DO CAMPO COM FOCUS
// ### EXEMPLO: <INPUT maxLength="9" name="cpf1" size="9" onblur="FuncBlur(this);" // ### onkeyup="exibeValor(this, 6, 0)" onfocus="stopTabCheck(this); FuncFocus(this);">

function FuncBlur(campo)
{ campo.style.backgroundColor = '#FFFFFF'; }	

function FuncFocus(campo)
{ campo.style.backgroundColor = '#FFFFC0' }



// ### TABULACAO DE CAMPOS
var checarTabulacao=true;
function stopTabCheck(nomeCampo)
{ checarTabulacao=false; }

function startTabCheck()
{ checarTabulacao=true; }

function exibeValor(nomeCampo, lenCampo)
{
	if ((nomeCampo.value.length == lenCampo) && (checarTabulacao))
	{
		var i=0;
		for (i=0; i < document.forms[0].elements.length; i++)
		{
			if (document.forms[0].elements[i].name == nomeCampo.name)
			{
				while ((i+1) < document.forms[0].elements.length)
				{		
					if (document.forms[0].elements[i+1].type != "hidden")
					{
						document.forms[0].elements[i+1].focus();
						break;
					}	
					i++;
				}
				checarTabulacao=false;
				break;
			}
		}
	}
}



// ### MUDAR COR DA LINHA SELECIONADA DO GRID
var cor;

function selecionaGrid( elemento )
{
cor=elemento.style.backgroundColor;
elemento.style.backgroundColor = "#BFBFBF";
}


function deSelecionaGrid( elemento)
{
elemento.style.backgroundColor = cor; 
}


function SairSistema()
{
    window.close();
}
function Excluindo()
{
    return confirm('Confirma a exclusao do registro?')
}
function Saindo()
{
    return confirm('Deseja salvar antes de sair?')
}
	
function Esvazia(valor_campo){
   
if (valor_campo.value == valor_campo.defaultValue)
	valor_campo.value='';
}

function Padrao(valor_campo){

if (valor_campo.value == '')
	valor_campo.value = valor_campo.defaultValue;
}
 
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
      var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

      if(document.all) {
        nTecla = evtKeyPress.keyCode; }
      else if(document.layers) {
        nTecla = evtKeyPress.which;
      }

      sValue = objForm[strField].value;

      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( " ", "" );
      sValue = sValue.toString().replace( " ", "" );
      fldLen = sValue.length;
      mskLen = sMask.length;

      i = 0;
      nCount = 0;
      sCod = "";
      mskLen = fldLen;

      while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
          sCod += sMask.charAt(i);
          mskLen++; }
        else {
          sCod += sValue.charAt(nCount);
          nCount++;
        }

        i++;
      }

      objForm[strField].value = sCod;

      if (nTecla != 8) { // backspace
        if (sMask.charAt(i-1) == "9") { // apenas números...
          return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
        else { // qualquer caracter...
          return true;
        } }
      else {
        return true;
      }
}

	function txbkd()
	{
		var p = window.event.srcElement.cbx.lst;
		
		if (window.event.keyCode == 9)
		{
			phide(p);
			return true;
		}
		
		if (window.event.keyCode == 13)
		{
			if (p.selectedItem != null)
			{
				p.cbx.txb.value = p.selectedItem.innerText;
				//itemDeselect(p.selectedItem);
				phide(p);
				window.event.returnValue = false;
				window.event.srcElement.select();
				
				__combobox_invokepostback(p.cbx);
					
				return false;
			}
		}
		
		if (window.event.keyCode == 40 || window.event.keyCode == 38)
		{
				if (p.selectedItem != null && p.style.visibility == 'visible')
				{
					var index = p.selectedIndex;
					itemDeselect(p.selectedItem);
					var dir = window.event.keyCode == 40 ? 1 : -1;
					if (index + dir >= 0 && index + dir < p.childNodes.length )
					{
						itemSelect(p.childNodes[index + dir]);
					}
				}
				else
				{
					p.style.visibility = 'visible';
					if (0 < p.childNodes.length)
					{
						itemSelect(p.childNodes[0]);
					}
				}
		}
		else if (window.event.keyCode == 27)
		{
			phide(p);
		}
	}
	
	function itemFocus()
	{
	}
	
	function getSelectedItem(comboBox)
	{
		var cbx = _cbx(comboBox);
		var li = cbx.lst.childNodes;
		for (var i = 0; i < li.length; i++)
			if (li[i].isSelected) return li[i];
	}
	
	function txbku()
	{
		var ev = window.event;
		var e = window.event.srcElement;
		var c = e.cbx;
		var p = c.lst;
		var txt = e.value;
		if (ev.keyCode != 38 && ev.keyCode != 40 && e.keyCode != 37 && ev.keyCode != 39 && ev.keyCode != 9 && ev.keyCode != 13)
		{
			itemDeselect(p.selectedItem);
			if (txt != '' && e.cbx.autocomplete)
			{
				var j = 0;
				if (c.cases)
				{
					for (var i = 0; i < p.items.length; i++) if (p.items[i].indexOf(txt) == 0) j++;
				}
				else
				{
					for (var i = 0; i < p.items.length; i++) if (p.items[i].toLowerCase().indexOf(txt.toLowerCase()) == 0) j++;
				}
				
				var selectedList = new Array(j);
				
				j = 0;
				if (c.cases)
				{
					for (var i = 0; i < p.items.length; i++)
						if (p.items[i].indexOf(txt) == 0)
							selectedList[j++] = p.items[i];
				}
				else
				{
					for (var i = 0; i < p.items.length; i++)
						if (p.items[i].toLowerCase().indexOf(txt.toLowerCase()) == 0)
							selectedList[j++] = p.items[i];
				}
				
				var fireList = (j != 0 ? selectedList : null);
				
				popItems(e.cbx, fireList);
				
				// set the textbox value & selection
				if (((ev.keyCode >= 48 && ev.keyCode <= 57) || (ev.keyCode >= 65 && ev.keyCode <= 90)) && j != 0)
				{
					for (var i = 0; i < p.items.length; i++)
					{
						if ((!c.cases && ((p.items[i].toLowerCase().indexOf(txt.toLowerCase()) == 0))) || (p.items[i].indexOf(txt) == 0))
						{
							e.value = p.items[i];
							var tr = e.createTextRange();
							tr.moveStart('character', txt.length);
							tr.select();
							break;
						}
					}
				}
			}
			else
			{
				popItems(e.cbx, null);
			}
		}
	}
	
	function txblur()
	{
		var e = window.event.srcElement;
		var p = e.cbx.lst;
		var b = e.cbx.btn;
		if (!p.ison && !b.ison)
		{
			phide(p);
			//p.style.visibility = 'hidden';
		}//itemDeselect(p.selectedItem);
		//itemsDeselect(p);
	}
	
	function pmr()
	{
		var d = window.event.srcElement;
		d.ison = true;
	}
	
	function pmt()
	{
		var d = window.event.srcElement;
		d.ison = false;
	}

	function mr()
	{
		itemSelect(window.event.srcElement);
	}
	
	function itemSelect(i)
	{
		if (i != null)
		{
			i.style.backgroundColor = 'buttonshadow';
			i.style.color = 'window';
			
			i.isSelected = true;
			i.parentElement.cbx.lst.ison = true;
			i.parentElement.selectedItem = i;
			i.parentElement.selectedIndex = i.itemIndex;
		}
	}
	
	function itemDeselect(i)
	{
		if (i != null)
		{
			i.style.backgroundColor = 'window';
			i.style.color = 'windowtext';
			
			i.isSelected = false;
			i.parentElement.cbx.lst.ison = false;
			i.parentElement.selectedItem = null;
			i.parentElement.selectedIndex = -1;
		}
	}
	
	function itemsDeselect(p)
	{
		for (var i = 0; i < p.subitems.length; i++)
		{
			itemDeselect(p.childNodes[i]);
		}
	}
	
	function phide(p)
	{
		if (p.style.visibility == 'visible')
			p.style.visibility = 'hidden';
		itemsDeselect(p);
	}
	
	function mt()
	{
		itemDeselect(window.event.srcElement);
	}
	
	function getRealIndex(comboBox)
	{
		var l = comboBox.lst;
		var t = comboBox.txb;
		
		for (i = 0; i < l.items.length; i++)
			if (l.items[i] == t.value)
				return i;
		return -1;
	}
	
	function mc()
	{
		var d = window.event.srcElement;
		var l = d.parentElement;
		var c = l.cbx;
		var t = c.txb;
		
		t.value = d.innerText;
		if (c.autopostback)
		{
			__combobox_postback(c, getRealIndex(c));
		}
		else
		{
			phide(l);
			t.focus();
			t.select();
		}
	}
	
	function _cbx(comboBox)
	{
		return document.getElementById(comboBox);
	}
	
	function clearlst(lst)
	{
		lst.innerHTML = '';
	}
	
	function popItems(comboBox, list, itin)
	{	
		var p = comboBox.lst;
		
		if (itin)
		{
			p.items = list;
		}
		p.subitems = (list == null ? p.items : list);
		
		clearlst(p);
		
		for (var c = 0; c < p.subitems.length; c++)
		{
			var ps = p.style;
			var i = document.createElement('div');
			
			i.id = 'item' + p.childNodes.length;
			
			//if (itin)
				i.itemIndex = c;
			
			i.style.cursor = 'default';
			i.style.whitespace = 'default';
			
			
			i.bgcolor = '#000000';
			i.innerText = p.subitems[c];
			
			i.style.fontFamily =  comboBox.txb.style.fontFamily;
			i.style.fontSize = comboBox.txb.style.fontSize;
			
			i.style.width = isNaN(parseInt(comboBox.txb.style.width, 10)) ? '200px' : comboBox.txb.style.width;
			//i.style.height = '24px';
			
			i.style.verticalAlign = 'middle';
			
			//i.innerHTML = "<table><tr><td valign = 'center'>" + p.subitems[c] + "</td></tr></table>";
			
			i.onmouseover = mr;
			i.onmouseout = mt;
			i.onclick = mc;
			
			p.appendChild(i);
		}
	}
	
	function pstyle(p)
	{
		var ps = p.style;
		ps.position = 'absolute';
		ps.width = p.cbx.style.width;
		var iHeight = p.cbx.offsetHeight - 5;
		ps.height = (p.maxDropDownItems * iHeight) + 2 + 'px';
		
		ps.top = p.cbx.style.position != 'absolute' ? getElementPosition(p.cbx).top + p.cbx.offsetHeight + 2 : p.cbx.offsetHeight + 1;//+ 'px';
	
		ps.border = '1px solid #000000';
		ps.backgroundColor = '#ffffff';
	}

	function getElementPosition(el)
	{
		var c = el, l = 0, t = 0;
		for (; c.offsetParent.offsetParent; c = c.offsetParent)
		{
			l += c.offsetLeft;
			t += c.offsetTop;
		}
		return {left: l, top: t};
	}
	
	function flip()
	{
		var e = window.event.srcElement;
		var t = e.cbx.txb;
		var p = e.cbx.lst;
		var ps = p.style;
		
		if (ps.visibility == 'hidden')
		{
			ps.visibility = 'visible';
			t.focus();
		}
		else
		{
			phide(p);
		}
	}
	
	function __combobox_invokepostback(comboBox)
	{
		if (comboBox.autopostback)
			__combobox_postback(comboBox, getRealIndex(comboBox));
	}
	
	function initCombo(comboBox, _items, apb, autocomplete, cases, maxDropDownItems)
	{
		var e = document.getElementById(comboBox);
		e.lst = e.childNodes[0];
		e.txb = e.childNodes[1].rows[0].cells[0].childNodes[0];
		e.btn = e.childNodes[1].rows[0].cells[1];
		e.btn.cbx = e.lst.cbx = e.txb.cbx = e;
		e.lst.maxDropDownItems = maxDropDownItems;
		
		if (e.autocomplete = autocomplete)
			e.cases = cases;
		
		e.autopostback = apb;
		
		pstyle(e.lst);
		
		popItems(e, _items, true);
	}
	function somente_numero(campo){
	var digits="0123456789"
	var campo_temp 
	for (var i=0;i<campo.value.length;i++){
	  campo_temp=campo.value.substring(i,i+1)	
	  if (digits.indexOf(campo_temp)==-1){
		    campo.value = campo.value.substring(0,i);
		    break;
	   }
	}
}