/*-----------------------------------------------------------------*
 | funcao para mostra/limpar nomes dentro da imputext                                  |
 *-----------------------------------------------------------------*/

function limpar (objeto, msg) {
        if (objeto.value == msg)objeto.value = '';
}

function mostrar (objeto, msg) {
        if (objeto.value == '')objeto.value = msg;
}
/*-----------------------------------------------------------------*
 | funcao para mostra/ocultar div                                  |
 *-----------------------------------------------------------------*/

function mostra(e) {
    if (document.getElementById(e).style.display == 'none') {
        document.getElementById(e).style.display = 'block';
    } else {
        document.getElementById(e).style.display = 'none';
    }
}

/*-----------------------------------------------------------------*
 | funcao para adicionar aos favoritos                             |
 *-----------------------------------------------------------------*/

function addFav(){
    var url        = "http://www.berimbau.com.br/";
    var title    = "Berimbau Comunicação";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

/*-----------------------------------------------------------------*
 | AJAX DOS representanteS                             |
 *-----------------------------------------------------------------*/
 
 var HttpReq = null;
var dest_combo = null;

function ajaxComboBox(url, comboBox){
   dest_combo = comboBox;
   var indice = document.getElementById('estado').selectedIndex;
   var nome_estado = document.getElementById('estado').options[indice].getAttribute('value');
   url = url + '?estado=' + nome_estado;
   if (document.getElementById) { //Verifica se o Browser suporta DHTML.
       if (window.XMLHttpRequest) {
           HttpReq = new XMLHttpRequest();
           HttpReq.onreadystatechange = XMLHttpRequestChange;
           HttpReq.open("GET", url, true);
           HttpReq.send(null);
       } else if (window.ActiveXObject) {
           HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
           if (HttpReq) {
               HttpReq.onreadystatechange = XMLHttpRequestChange;
               HttpReq.open("GET", url, true);
               HttpReq.send();
           }
       }
   }
}

function XMLHttpRequestChange() {
   if (HttpReq.readyState == 4 && HttpReq.status == 200){
       var result = HttpReq.responseXML;
       var representante = result.getElementsByTagName("nome");
       document.getElementById('representante').innerHTML = "";
       for (var i = 0; i < representante.length; i++) {
           new_opcao = create_opcao(representante[i]);
           document.getElementById('representante').appendChild(new_opcao);
       }
   }
}

function create_opcao(representante) { 
   var new_opcao = document.createElement("option"); 
   var texto = document.createTextNode(representante.childNodes[0].data); 
   new_opcao.setAttribute("value",representante.getAttribute("id")); 
   new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
   return new_opcao; // Retorna a nova OPTION.
}

/*-----------------------------------------------------------------*
 | FUNCOES COLOCADAS AUTOMATICAMENTE PELO DREAMWEAVER              |
 *-----------------------------------------------------------------*/

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

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 MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/*-----------------------------------------------------------------*
 | mOvr    troca a cor de fundo da celula de uma tabela            |
 *-----------------------------------------------------------------*/

function mOvr(src,clrOver) {
  if (!src.contains(event.fromElement)) {
    src.style.cursor = 'hand';
    src.bgColor = clrOver;
  }
}

/*-----------------------------------------------------------------*
 | mOut    volta a cor original de fundo da celula de uma tabela   |
 *-----------------------------------------------------------------*/

function mOut(src,clrIn) {
  if (!src.contains(event.toElement)) {
    src.style.cursor = 'default';
    src.bgColor = clrIn;
  }
}
/*-----------------------------------------------------------------*
 | ContidoNoDominio    Retorna True se a String dada só contiver   |
 |                     caracteres do domínio dado                  |
 *-----------------------------------------------------------------*/
function ContidoNoDominio(StrDado, Dominio)
	{
	var i, j;
	
	if (StrDado == "") return false;
	
	for (i=0; i<StrDado.length; i++)
		{
		for (j=0; j<Dominio.length; j++)
			{
			if (StrDado.substr(i,1) == Dominio.substr(j,1)) break;
			}
		if (j >= Dominio.length) return false;
		}
	return true
	}
			
/*-----------------------------------------------------------------*
 | ContemDominio    Retorna True se a String dada contiver algum   |
 |                  caractere do domínio dado                      |
 *-----------------------------------------------------------------*/
function ContemDominio(StrDado, Dominio)
	{
	var i, j;
	
	if (StrDado != "")
		{
		for (i=0; i<StrDado.length; i++)
			{
			for (j=0; j<Dominio.length; j++)
				{
				if (StrDado.substr(i,1) == Dominio.substr(j,1)) return true;
				}
			}
		}
		
	return false;
	}
			
/*-----------------------------------------------------------------*
 | IsStrNum     Retorna True se a String dada só contiver números  |
 |                                                                 |
 *-----------------------------------------------------------------*/
function IsStrNum(Dado)
	{
	return ContidoNoDominio(Dado, " 0123456789");
	}
	
/*-----------------------------------------------------------------*
 | IsStrInt	    Retorna True se a String dada só contiver          |
 |              Caracteres para Número inteiro                     |
 *-----------------------------------------------------------------*/
function IsStrInt(Dado)
	{
	return ContidoNoDominio(Dado, " +-0123456789");
	}
			
/*-----------------------------------------------------------------*
 | IsStrFloat   Retorna True se a String dada só contiver          |
 |              Caracteres para Número em Ponto-Flutuante          |
 *-----------------------------------------------------------------*/
function IsStrFloat(Dado)
	{
	return ContidoNoDominio(Dado, " +-0123456789Ee,.");
	}

/*-----------------------------------------------------------------*
 | IsStrCurr    Retorna True se a String dada só contiver          |
 |              Caracteres para Número Currency                    |
 *-----------------------------------------------------------------*/
function IsStrCurr(Dado)
	{
	return ContidoNoDominio(Dado, " +-0123456789,.");
	}

/*-----------------------------------------------------------------*
 | IsStrData    Retorna True se a String dada for uma data válida  |
 |              no formato DD/MM/AAAA                              |
 *-----------------------------------------------------------------*/
function IsStrData(Dado)
	{
    var DiasMes = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
    var Dia, Mes, Ano;
    var Result = false;

	// Pré-analisa o String:
	if (Dado != "")
		{
		if ((Dado.length == 10) && (Dado.substr(2,1) == "/") && (Dado.substr(5,1) == "/"))
			{
			// Levanta Campos:
			if (IsStrNum(Dado.substr(0,2))) Dia = Dado.substr(0,2);
			if (IsStrNum(Dado.substr(3,2))) Mes = Dado.substr(3,2);
			if (IsStrNum(Dado.substr(6,4))) Ano = Dado.substr(6,4);

			// Analisa Ano e Mês:
			if ((Ano > 0) && (Mes >= 1) && (Mes <= 12))
				{
				// Analisa Dia:
				if ((Dia >= 1) && (Dia <= DiasMes[Mes - 1]))
					{
					// Analisa os casos não-bissextos:
					if ((Mes == 2) && ((Ano%4 != 0) || (Ano%100 == 0) && (Ano%400 != 0)))
						{
						if (Dia <= 28) Result = true;
						}
					else
						{
						Result = true;
						}
					}
				}
			}
		}		
	return Result;
	}	

/*-----------------------------------------------------------------*
 | IsStrHora    Retorna True se a String dada for uma data válida  |
 |              no formato HH:MM ou HH:MM:SS                       |
 *-----------------------------------------------------------------*/
function IsStrHora(Dado)
	{
    var Hor, Min, Seg;
    var Result = false;

	// Pré-analisa o String:
	if (Dado != "")
		{
		if (((Dado.length == 5) || (Dado.length == 8)) && (Dado.substr(2,1) == ":"))
			{
			// Levanta Campos:
			if (IsStrNum(Dado.substr(0,2))) Hor = Dado.substr(0,2); else Hor = -1;
			if (IsStrNum(Dado.substr(3,2))) Min = Dado.substr(3,2); else Min = -1;

			// Analisa a Hora:
			if ((Hor >= 0) && (Hor <= 23))
				{
				// Analisa o Minuto:
				if ((Min >= 0) && (Min <= 59))
					{
					// Verifica se tem segundo:
					if (Dado.length == 8)
						{
						// Pré-analisa:
						if (Dado.substr(5,1) == ":")
							{
							// Levanta e verifica segundos:
							if (IsStrNum(Dado.substr(6,2))) Seg = Dado.substr(6,2); else Seg = -1;
							if ((Seg >= 0) && (Seg <= 59))
								{
								Result = true;
								}
							}
						}
					else
						{
						Result = true;
						}				
					}
				}
			}
		}
	return Result;
	}

/*-----------------------------------------------------------------*
 | SeparaNomeArq       Separa o Nome de Arquivo do Path completo   |
 |                                                                 |
 *-----------------------------------------------------------------*/
function SeparaNomeArq(PathDado)
	{
	var i
	
	if (PathDado.length == 0) return "";
	
	for (i=PathDado.length-1; i>=0; i--)
		{
		if (PathDado.substr(i,1) == "\\" || PathDado.substr(i,1) == ":")
			{
			return PathDado.substr(i + 1);
			}
		}
	return PathDado;				
	}

/*-----------------------------------------------------------------*
 | StrD      Acerta a String na Largura dada com                   |
 |           Alinhamento à Direita:                                |
 *-----------------------------------------------------------------*/
function StrD(Dado, Larg)
	{    
    var Result;
	var i;

    if (Dado.length >= Larg)             
        {
        Result = Dado.substr(Dado.length - Larg,Larg);             
		}
    else
		{
		Result = "";
		for (i=Larg-Dado.length; i>0; i--)
			{
			Result = Result + " ";
			}
        Result = Result + Dado;
		}
	return Result;
	}

/*-----------------------------------------------------------------*
 | StrE      Acerta a String na Largura dada com                   |
 |           Alinhamento à Esquerda:                               |
 *-----------------------------------------------------------------*/
function StrE(Dado, Larg)
	{    
    var Result;
	var i;

    if (Dado.length >= Larg)             
        {
        Result = Dado.substr(0,Larg);             
		}
    else
		{
		Result = Dado;
		for (i=Larg-Dado.length; i>0; i--)
			{
			Result = Result + " ";
			}
		}
	return Result;
	}

/*-----------------------------------------------------------------*
 | StrNum    Retorna o valor Numérico em String Dado,              |
 |           formatado na Largura dada:                            |
 *-----------------------------------------------------------------*/
function StrNum(Dado, Larg)
  {
  var Result, sDado, i;

  sDado = Dado.toString();
  if (sDado.length >= Larg)       
    {
    Result = sDado.substr(sDado.length - Larg,Larg);       
    }
  else
    {
    Result = "";
    for (i=Larg-sDado.length; i>0; i--)
      {
      Result = Result + "0";
      }
    Result = Result + sDado.toString();
    }
  return Result;
  }

/*-----------------------------------------------------------------*
 | PassaDominio        Retorna a String dada, somente com os       |
 |                     caracteres do domínio dado                  |
 *-----------------------------------------------------------------*/
function PassaDominio(StrDado, Dominio)
	{
	var i, j, c;
	var Result;
	
	Result = "";
	for (i=0; i<StrDado.length; i++)
		{
		c = StrDado.substr(i,1);
		for (j=0; j<Dominio.length; j++)
			{
			if (c == Dominio.substr(j,1)) break;
			}
		if (j < Dominio.length)
			{
			Result = Result + c;
			}
		}
	return Result;
}

			
/*-----------------------------------------------------------------*
 | BloqueiaDominio     Retorna a String dada retirando os          |
 |                     caracteres do domínio dado                  |
 *-----------------------------------------------------------------*/
function BloqueiaDominio(StrDado, Dominio)
	{
	var i, j;
	
	Result = "";
	for (i=0; i<StrDado.length; i++)
		{
		c = StrDado.substr(i,1);
		for (j=0; j<Dominio.length; j++)
			{
			if (c == Dominio.substr(j,1)) break;
			}
		if (j >= Dominio.length)
			{
			Result = Result + c;
			}
		}
	return Result;
	}

/*******************************************************************
 *  Funções de Filtro para uso com Text-Boxes:                     *
 *  Utilize-as sob os eventos OnKeyUp e OnChange simultaneamente.  *
 *******************************************************************/

function FiltroNum(Objeto)
	{
	Objeto.value = PassaDominio(Objeto.value, "0123456789");
	}
	
function FiltroInt(Objeto)
	{
	Objeto.value = PassaDominio(Objeto.value, "0123456789+-");
	}
			
function FiltroCurr(Objeto)
	{
	Objeto.value = PassaDominio(Objeto.value, "0123456789+-,.");
	}

function FiltroFloat(Objeto)
	{
	Objeto.value = PassaDominio(Objeto.value, "0123456789+-Ee,.");
	}

function FiltroData(Objeto)
	{
	Objeto.value = PassaDominio(Objeto.value, "0123456789/");
	}

function FiltroHora(Objeto)
	{
	Objeto.value = PassaDominio(Objeto.value, "0123456789:");
	}

function FiltroUp(Objeto)
	{
	Objeto.value = Objeto.value.toUpperCase();
	}



//limita tamanho para textarea
//-----------------------------------------------------------------------
function fMaxTamCampo(TamMax,ValCampo)
{	
	Campo = ValCampo.value
	TamanhoCampo = Campo.length
	if (TamanhoCampo > TamMax)
		{ 	
		ValorCampo = Campo.substring(0,TamMax)	
		ValCampo.value = ValorCampo		
		alert("O limite máximo do campo é de " +TamMax+ " caracteres.")
	}	
}
//-----------------------------------------------------------------------


function ValidaEMail(EMail)
  {
  if (EMail.indexOf("@") < 0) return false;
  if (EMail.indexOf(".") < 0) return false;
  if (ContemDominio(EMail, " ;,:/$!#%^&*()+[]{}|\\~`'\"")) return false;
  return true;
  }

/*-----------------------------------------------------------------*
 | isNumber		  Retorna True se o String dada for um número      |
 |                com casas decimais dadas.                        |
 *-----------------------------------------------------------------*/
function isNumber(sNumero, iDecimais)
  {
  var bRet
  var i
  bRet = true
  if (iDecimais > 0)
    {
    if (sNumero.length < iDecimais + 2 || (sNumero.indexOf(".", 0) == -1 && sNumero.indexOf(",", 0) == -1))
      bRet = false
    }
  if (bRet)
    {
    i = 0
    while(i < sNumero.length && bRet)
      {
      if (iDecimais > 0)
        {
        if (i == sNumero.length - (iDecimais + 1))
          {
          if (sNumero.charAt(i) != "." && sNumero.charAt(i) != ",")
            bRet = false
          }
        else
          {
          if (sNumero.charAt(i) < "0" || sNumero.charAt(i) > "9")
            bRet = false
          }
        }
      else
        {
        if (sNumero.charAt(i) < "0" || sNumero.charAt(i) > "9")
          bRet = false
        }
      i++
      }
    }
  return bRet
  }

/*
 Nome........: FmtData
 Descricao...: Insere a máscara de data no campo
 Paramentros.: Dado
 Retorno.....: Retorna o conteúdo formatado em dd/mm/yyyy, não permitindo a digitação
               de caracteres alfa 
*/

function FmtData_old(Dado)
  {
   	var Result = Dado;

	 	for (i=1; i<=Dado.length; i++)
		{			
			if (i == 2)
			{	
				Result = Dado.substr(0, 2) + "/" + Dado.substr(2, i);
			}
			if (i >= 4)
			{
				Result = Dado.substr(0, 2) + "/" + Dado.substr(2, 2) + "/" + Dado.substr(4, 4);
			}
		}
   return Result;
  }

function FmtData(Dado)
  {
  var Result = Dado;
  var l = Dado.length;

  if((l > 2) && (l < 5))
    {
	Result = Dado.substr(0, 2) + "/" + Dado.substr(2, 2);
    }
  if(l >= 5)
    {
	Result = Dado.substr(0, 2) + "/" + Dado.substr(2, 2) + "/" + Dado.substr(4, 4);
	}
  return Result;
  }


/*
 Nome........: FmtDataMesAno
 Descricao...: Insere a máscara de data no campo
 Paramentros.: Dado
 Retorno.....: Retorna o conteúdo formatado em mm/yyyy, não permitindo a digitação
               de caracteres alfa 
*/
function FmtDataMesAno(Dado)
  {
   	var Result = Dado;

	 	for (i=1; i<=Dado.length; i++)
		{			
			if (i == 2)
			{	
				Result = Dado.substr(0, 2) + "/" + Dado.substr(2, i);
			}
			if (i > 2)
			{
				Result = Dado.substr(0, 2) + "/" + Dado.substr(2, 4);
			}
		}
   return Result;
  }
  
  /*
 Nome........: FmtHora
 Descricao...: Insere a máscara de hora no campo
 Paramentros.: Dado
 Retorno.....: Retorna o conteúdo formatado em hh:mm, não permitindo a digitação
               de caracteres alfa 
*/

function FmtHora(Dado)
  {
   	var Result = Dado;

	 	for (i = 1; i <= Dado.length; i++)
		{

			if (i >= 3)
			{
				Result = Dado.substr(0, 2) + ":" + Dado.substr(2, 2);
			}
		}
   return Result;
  }
  
var bPula = true; 
  
function fAtuPulaBlur()
{
	bPula = false;	
}
function fAtuPulaKeyPress()
{
	bPula = true;
	return;
}


function fPulaCampo(campo1,campo2,iTam,tpcampo)
	{
		if(bPula)
		{
			
			if (tpcampo == "data")
				{
			    	Filtro(campo1,'data');
				}
				if (tpcampo == "hora")
				{
			    	Filtro(campo1,'hora');
				}
			if (campo1.value.length >= iTam)
			{ 
				campo2.focus(); 
			}
				
		}
		
		return;
		
	}


/*-----------------------------------------------------------------*
 | CalculaDigitoMod11(Dado, NumDig, LimMult)					   |
 |    Retorna o(s) NumDig Dígitos de Controle Módulo 11 do	   	   |
 |	  Dado, limitando o Valor de Multiplicação em LimMult:         |
 |		    													   |
 |		    Números Comuns::   			iDigSaida	 iCod          |
 |			  CGC					  	  2			   9		   |
 |   		  CPF					  	  2			  12		   |
 |			  C/C,Age - CAIXA			  1 		   9           |
 |			  habitação/bloqueto		  1 		   9           |
 *-----------------------------------------------------------------*/
function CalculaDigitoMod11(Dado, NumDig, LimMult)
  {
  var Mult, Soma, i, n;
    
  for(n=1; n<=NumDig; n++)
    {
    Soma = 0;
    Mult = 2;
    for(i=Dado.length-1; i>=0; i--)
      {
      Soma += (Mult * parseInt(Dado.charAt(i)));
      if(++Mult > LimMult) Mult = 2;
      }
    Dado += ((Soma * 10) % 11) % 10;
    }
  return Dado.substr(Dado.length-NumDig, NumDig);
  }

function CalculaDigitoMod10(sValor)
{
 mult = 2
 soma = 0
 str = new String()
 sValor = sZapDummy(sValor)

 for (t=sValor.length;t>=1;t--)
 {
 str = (mult*parseInt(sMid(sValor,t,1))) + str
 mult--
 if (mult<1) mult = 2
 }

 for (t=1;t<=str.length;t++)
 soma = soma + parseInt(sMid(str,t,1))
 soma = soma % 10
 if (soma != 0)
   soma = 10 - soma
 str = soma   //casting
 return str
}


function Mod10(valor)
{
  val = valor.substring( 0 , valor.length - 1 )
  dig = valor.substring( valor.length - 1 , valor.length )
  str = new String
  fator = 2
  soma = 0
  for(i = val.length; i > 0; i--)
  {
  str = fator * parseInt(val.substring(i, i - 1)) + str
  fator--
  if(fator < 1) fator = 2
  }
  for(i = 0; i < str.length; i++) soma = soma + parseInt(str.charAt(i))
  soma = soma % 10
  if(soma != 0) soma = 10 - soma
  str = soma //aqui existe uma espécie de "casting" (conversão)
  return str == dig
}



function isBissexto(iAno)
{
  var bRet
  bRet = false
  if (iAno % 4 == 0 && (iAno % 100 !=0 || iAno % 400 ==0 ))
    bRet = true
  return bRet
}


function isDate(sData)
{
  var bRet
  var i
  bRet = true
  if (sData.length != 10)
    bRet = false
  if (bRet)
  {
    i = 0
    while (i < sData.length && bRet)
    {
      if (i == 2 || i == 5)
      {
        if (sData.charAt(i) != "/")
          bRet = false
      }
      else
      {
        if (!isNumber(sData.charAt(i), 0))
          bRet = false
      }
      i++
    }
  }
  if (bRet)
  {
    iDia = parseInt(sData.substring(0, 2), 10)
    iMes = parseInt(sData.substring(3, 5), 10)
    iAno = parseInt(sData.substring(6, 10), 10)
    if (iMes < 1 || iMes > 12)
      bRet = false
    if (iAno < 1)
      bRet = false
  }
  if (bRet)
  {
    if (iMes == 1 || iMes == 3 || iMes == 5 || iMes == 7 || iMes == 8 || iMes == 10 || iMes == 12)
    {
      if (iDia < 1 || iDia > 31)
        bRet = false
    }
    if (iMes == 2)
    {
      if (isBissexto(iAno))
      {
        if (iDia < 1 || iDia > 29)
          bRet = false
      }
      else
      {
        if (iDia < 1 || iDia > 28)
          bRet = false
      }
    }
    if (iMes == 4 || iMes == 6 || iMes == 9 || iMes == 11)
    {
      if (iDia < 1 || iDia > 30)
        bRet = false
    }
  }
  return bRet
}


function sRight(sExpressao,iNumeros)
{
  if (sExpressao.length >= iNumeros ) return sExpressao.substring(sExpressao.length - iNumeros,sExpressao.length)
}

function sLeft(sExpressao,iNumeros)
{
  if (sExpressao.length >= iNumeros ) return sExpressao.substring(0,iNumeros)
}

function sMid(sExpressao,iNumeros,iTamanho)
{
  var aux = new String()
  if ((sExpressao.length >= iNumeros) && (iNumeros >=0) && (iTamanho > 0))
  {
    iNumeros--
    aux = sExpressao.substring(iNumeros,iNumeros+iTamanho)
  }
    return aux
}

function sZapDummy(sStringNum)
{
  var sAux = new String()
  for (t=1;t<=sStringNum.length;t++)
  if (!isNaN(parseInt(sMid(sStringNum,t,1))))
    sAux = sAux + sMid(sStringNum,t,1)
  return sAux
}

function Filtro(Objeto,tpCampo) {
    if (navigator.appName.substr(0,9) != "Microsoft") {
        return;
    }

    if (window.event.keyCode == 37 || window.event.keyCode == 8
            || window.event.keyCode == 36 || window.event.keyCode == 46
            || window.event.keyCode == 16 || window.event.keyCode == 9)
        return;

    if (window.event.keyCode == 111 || window.event.keyCode == 191) {
	    if (tpCampo == "data") {
		    if (Objeto.value.length == 3 || Objeto.value.length == 6) {
			    return;
		    }
		    if (Objeto.value.length == 4 || Objeto.value.length == 7) {
			    var newpos = Objeto.value.length - 1;
			    Objeto.value = Objeto.value.substring(0,newpos);
			    return;
		    }
	    }
    }

    if (window.event.keyCode == 39) {
	    if (tpCampo == "data") {
            if (Objeto.value.length == 2 || Objeto.value.length == 5) {
			    Objeto.value = Objeto.value + "/";
            }
        }
        return;
    }

	if (tpCampo == "orgaoEmissor") {
    	Objeto.value = PassaDominio(Objeto.value, "0123456789ABCDEFGHIJLMNOPQRSTUVXZabcdefghijlmnopqrstuvxz");
		Objeto.value = FmtLacre(Objeto.value);
		return;
	}

	Objeto.value = PassaDominio(Objeto.value, "0123456789");
	if (tpCampo == "data") {
		Objeto.value = FmtData(Objeto.value);
	}
	if (tpCampo == "DataMesAno") {
		Objeto.value = FmtDataMesAno(Objeto.value);
	}
	if (tpCampo == "valor") {	
		Objeto.value = FmtCurr(Objeto.value);
	}
	if (tpCampo == "hora") {	
		Objeto.value = FmtHora(Objeto.value);
	}
	if (tpCampo == "lacre") {	
		Objeto.value = FmtLacre(Objeto.value);
	}
}

function FmtLacre(Dado)
{
	var Result, i;

	Dado = TiraTracos(Dado);

	if (Dado.length > 2)
	{
		Result = "-" + Dado.substr(Dado.length-2, 2);
		Result = Dado.substr(0, Dado.length-2) + Result;
	}
	else
	{
		Result = Dado;
	}
	return Result;
}
  
function FmtCurr(Dado)
  {
  var Result, i;
  
  if (Dado.length > 2)
    {
    Result = "," + Dado.substr(Dado.length-2, 2);
    for (i=5; i<=Dado.length; i+=3)
      {
      Result = Dado.substr(Dado.length-i, 3) + Result;
      if (Dado.length > i) Result = "." + Result;
      }
    Result = Dado.substr(0, Dado.length-i+3) + Result;
    }
  else
    {
    Result = Dado;
    }
  return Result;
}

function TiraPontos (NumeroFormatado)
{
    var s
    s  = NumeroFormatado.split (".");
    return s.join("");
}

function TiraVirgula (NumeroFormatado)
{
    var s
    s  = NumeroFormatado.split (",");
    return s.join("");
}

function TiraTracos (NumeroFormatado)
{
    var s
    s  = NumeroFormatado.split ("-");
    return s.join("");
}

function printit()
{  
	window.print();
}

function FiltraTexto(Dado) {
	var r = ''
	var asc1 = new String()
	var asc2 = new String()
	asc1 = 'áàãäâÁÀÃÄÂéèêëÉÈÊËìíîïÌÍÎÏòóôõöÒÓÔÕÖùúûüÙÚÛÜýÝçÇñÑ'
	asc2 = 'AAAAAAAAAAEEEEEEEEIIIIIIIIOOOOOOOOOOUUUUUUUUYYCCNN'
	for(i=0;i<=Dado.length-1;i++)
	    {
		c = Dado.charAt(i)
		for(j=0;j<=asc1.length-1;j++)
		if(Dado.charAt(i) == asc1.charAt(j))
			c = asc2.charAt(j)
			if((c<'0' || c>'9') && (c<'A' || c>'Z') && (c<'a' || c>'z') && (c != ' ') && (c != ',') && (c != '.') && (c != '/'))
			{
			return "";
			}
			r = r + c
		}
	return r.toUpperCase();
}

/* --------------------------------------------------------
	Nome: gfunCalcCEI
	Descricao: Verifica se o nro do CEI eh valido
	Parametros: Numero do CEI sem Formatacao
-------------------------------------------------------- */ 

function gfunCalcCEI(strCei) 
{
	var numLen;
	var strChar;
	var numSomat;
	var strAux;
	var strCharAux;
	var strSomat;
	var numLenumSomat;
	var numAux;
	var numDigit;

	numLen = strCei.length;

	//Verifica se o tamanho do CEI eh de 12 posicoes
	if	(numLen != 12) 
	{
		return false;
	}

	// Calcula o digito verificador
	strAux = "74185216374"
	numSomat = 0;
	for (i = 0; i < 11; i++) 
	{
		strChar = strCei.charAt(i)
		strCharAux = strAux.charAt(i)
		numSomat += (eval(strChar)) * (eval(strCharAux));
	}
	strSomat = numSomat + "";
	numLenumSomat = strSomat.length;
	numAux = parseInt(strSomat.charAt(numLenumSomat - 1)) + parseInt(strSomat.charAt(numLenumSomat - 2));
	
	if	((numAux >= 11) && (numAux <= 18))
		numDigit = 20 - numAux;
	else
		if	(numAux == 10)
			numDigit = 1;
		else
			numDigit = 10 - numAux;
	
	if (numDigit == 10) 
	{
	   numDigit = 1
	}

	//	Condicao para atender ao SFG. Aceitar o digito informado (0 ou 1), mesmo que o
	//	resultado do calculo seja igual a 1
	
	if ((strCei.charAt(11) < "2")) 
	{
	 if ((numDigit == 0) || (numDigit == 1)) 
	 {
		numDigit = eval(strCei.charAt(11));
		return true;
	 }
	}
	
	// Fim-condicao
	if	(eval(strCei.charAt(11)) != numDigit) 
	{
		return false;
	}

	return true;
}

/*--------------------------------------------
'nome...: Recarrega combo com valor informado
----------------------------------------------*/
function recCombo(campo,valor)
{
	for (i=0;i < eval(campo + '.options.length') ;i++)
	{
		if (eval(campo + '[i].value') == valor)
		{
			eval(campo + '.options[i].selected = true');
			break;
		}
	}
	return;
}

/*-----------------------------------------------------------------*
'nome...: RTRIM
 *-----------------------------------------------------------------*/
function RTrim(StrDado) {
    TemEspaco = true;

    sCampo = StrDado
	if (sCampo != "") {
        while (TemEspaco) {

            if (sCampo.substr(sCampo.length - 1,1) == ' ') {
                sCampo = sCampo.substr(0,sCampo.length - 1);
            }
            else {
                TemEspaco = false;
            }
        }
    }
	return sCampo;
}

var iTimerOtico;
var oldServOtico;
function SaibaMaisOtico() {
    oldServOtico = document.FormMenu.serv.value;
    document.FormMenu.serv.value = "LeituraOptica";
    TelaAjuda();
    iTimerOtico = window.setTimeout("atualizaSaibaMais()", 2000);
    document.FormMenu.serv.value = oldServOtico;
}

function atualizaSaibaMais() {
    document.FormMenu.serv.value = oldServOtico;
    window.clearTimeout(iTimerOtico);
}

function fPulaCampoOtico(nomeform,campo1,campo2,tam,tpcampo) {
    if (!eval('document.' + nomeform + '.chkLeitorOptico.checked')) {
        campo1 = eval('document.' + nomeform + '.' + campo1);
        campo2 = eval('document.' + nomeform + '.' + campo2);
        fPulaCampo(campo1,campo2,tam,tpcampo);
    }
    return;
}

function verificaAgend(nomeForm,dataAgend) {
    if (dataAgend != "") {
        nomeForm.rdoAgendamento[1].checked = true;
    }
}

function alteraAgend(nomeForm, dataAgend) {
    if (nomeForm.rdoAgendamento[1].checked) {
        dataAgend.disabled = false;
        dataAgend.focus();
    } else {
        dataAgend.value = "";
        dataAgend.disabled = true;
    }
}

function verificaBrowserImpressao() {
    if (navigator.appName.substr(0,9) != "Microsoft") {
	    window.print();
	    history.back();
    }
}

function imprimeDireto(nomeForm, uri) {
	if (navigator.appName.substr(0,9) == "Microsoft") {
		printHidden(uri);
    } else {
	    nomeForm.action=uri;
		nomeForm.submit();
	}
}

function imprimeTeste(nomeForm, uri) {
    nomeForm.action=uri;
	nomeForm.submit();
}

function focoProximoElemento(nomeForm, nomeCampo) {    
    for (i = 0; i < nomeForm.length-1; i++) {        
        if (nomeForm.elements[i].name == nomeCampo) {                        
            if ((nomeForm.elements[i+1].type != "hidden") 
                    && (nomeForm.elements[i+1].name != nomeCampo)) {                
                nomeForm.elements[i+1].focus();
                break;
            } else {
                nomeCampo = nomeForm.elements[i+1].name;
            }
        }
    }
}

function limpaCamposTxt(nomeForm) {
    for (i = 0; i < nomeForm.length; i++) {
        if (nomeForm.elements[i].type == "text") {
            nomeForm.elements[i].value = "";
        }
    }
}

/* Retorna o DV do IPTU (tipo="1") / ISS (tipo="0") */
function calculaDAC11A(numero, tipo) {
    var intPeso = 2;
    var intCalcula = 0;

    for (var intPosicao = (numero.length-1); intPosicao >= 0; intPosicao --) {
        // soma o produto das multiplicações do dígito pelo peso
        intCalcula += (intPeso * parseInt(numero.charAt(intPosicao), 10));
        intPeso ++;
        if (intPeso == 11) {
            intPeso = 1;
        }
    }
    
    // divide a soma dos produtos por 11
    intCalcula = intCalcula % 11;
    
    // monta retorno a partir de intCalcula
    if (intCalcula == 0) {
        return "0";
    } else if (intCalcula == 1) {
        return tipo;
    } else {
        return 11 - intCalcula;
    }
}

function CalculaDigitoMod1129(valor) {
    mult = 2;
    somaTotal = 0;
    for (j=valor.length;j>0;j--) {
        soma = (mult * parseInt(valor.substring(j,j-1),10));
        if (parseInt(soma,10) > 9) {
            soma = soma.toString();
            soma = parseInt(soma.substring(0,1),10)
                    + parseInt(soma.substring(1,2),10);
        }
        somaTotal = somaTotal + soma;
        mult++;
        if (mult > 9) mult = 2;
    }
    resto = somaTotal % 11;

    if (resto == 0 || resto == 1) {
        dig = 1;
    } else {
        dig = 11 - resto;
    }
    return dig;
}

function obtemTipoCampo(texto) {
    var alfa = false;
    var numerico = false;

    for (i=0; i<texto.length; i++) {
        if (isNumber(texto.charAt(i))) {
            numerico = true;
        } else {
            alfa = true;
        }
        if (numerico && alfa) {
            break;
        }
    }

    if (numerico && alfa) {
        return "alfanumerico";
    } else if (numerico && !alfa) {
        return "numerico";
    } else {
        return "alfa";
    }
}

function AlertaUsoTeclado() {
    alert("Para maior segurança, favor utilizar o Teclado Virtual."); 
}

function CalculaDigitoMod11_v4(valor) {
    mult = 2;
    somaTotal = 0;
    for (j=valor.length;j>0;j--) {
        soma = (mult * parseInt(valor.substring(j,j-1),10));
        somaTotal = somaTotal + soma;
        mult++;
        if (mult > 9) mult = 2;
    }
    resto = somaTotal % 11;

    if (resto == 0 || resto == 1) {
        dig = 0;
    } else {
        dig = 11 - resto;
    }
   
    return dig;
}

/*troca uma imagem, utilizada em OVERS || PIAZZA 03/05/2011*/
function trocaImagem(Img,imagem){
	Img.src="images/"+imagem;
}

