//-------------------------------------------------------------

function datastr(wdata)
{
   var retdata;

   retdata = "";
   retdata = wdata.substring(6,10) + wdata.substring(3,5) + wdata.substring(0,2);

   return retdata;
}

//-------------------------------------------------------------

function replaceAll(str, de, para){
    var pos = str.indexOf(de);
    while (pos > -1){
		str = str.replace(de, para);
		pos = str.indexOf(de);
	}
    return (str);
}

//-------------------------------------------------------------

function Insert_Slash(argvalue)
{
  
	var I = 0;
	var numslash = 0;
	var keycode = 0;
 
	if (argvalue.size >= 10)
		{  
			qtslash = 2;
   		}
	else
   		{  
			qtslash = 1;
   		}
   
	keycode = window.event.keyCode; 
   
	if (keycode != 8 && keycode != 46)
		{  
			I = InStr(argvalue.value, "/");
   
      		if (I >= 0)
      			{  
					numslash = numslash + 1;
      			}
   
      		I = InStr(argvalue.value.substring(I + 1, 10), "/");
   
      		if (I >= 0)
      			{  
					numslash = numslash + 1;
      			}
   
   			if (numslash < qtslash)
      			{  
					if ((argvalue.value.length == 2 || argvalue.value.length == 5) && (numslash < 2))
         				{  
							argvalue.value = argvalue.value + "/";
      					}
   				}  
   		}   
}

//-- Automaticly jumps to next field --
 
function Move(TheObj, size, TheNext)  
{
	var keycode = 0;
	if (TheObj.value.length == size)
		{  
			TheNext.focus();  
		}
	else
		{  
			if (window.event) 
				{  keycode = window.event.keyCode;
   					if (keycode == 13)
      					{  
							TheNext.focus();
         				}
      			}
   		}
}
	
	
	
	
	
	function leftTrim(sString) 
	{
	   while (sString.substring(0,1) == ' ')
	   {
	      sString = sString.substring(1, sString.length);
	   }
	   return sString;
	}

	function rightTrim(sString) 
	{
	   while (sString.substring(sString.length-1, sString.length) == ' ')
	   {
	      sString = sString.substring(0,sString.length-1);
	   }
	   return sString;
	}


	function trim(sString) 
	{
	   while (sString.substring(0,1) == ' ')
	   {
	      sString = sString.substring(1, sString.length);
	   }
	   while (sString.substring(sString.length-1, sString.length) == ' ')
	   {
	      sString = sString.substring(0,sString.length-1);
	   }
	   return sString;
	}
     
     
     
     
       function habilitaNovo(){
			TR_LINK.style.display='none';
			TR_BOX.style.display='block';
       }
       
       function desabilitaNovo(){
           TR_LINK.style.display='block';
           TR_BOX.style.display='none';
       }  
       
       function hideBox(i,i_max){
           if (i_max==1){
			  TR_LINKa.style.display='block';
			  TR_BOXa.style.display='none';     
		   }else{
			  TR_LINKa[i-1].style.display='block';
			  TR_BOXa[i-1].style.display='none';     		   
		   }
					   
			document.form_alterar.reset();  
       }           
       
       function showBox(i,i_max){
           if (i_max==1)
           {
			  TR_LINKa.style.display='none';
			  TR_BOXa.style.display='block';     
		   }
		   else
		   {
		   
			  // desmarcar 
			  if(marcada != -1)
			  {
				hideBox(marcada,i_max)
			  }		
			  	  
			  // marcar
			  marcada = i;
			  TR_LINKa[i-1].style.display='none';
			  TR_BOXa[i-1].style.display='block';     		   
		   }	
       }      






//-- CRÍTICA DE DATAS --
function CritData(argvalue)
{
   var wdia;
   var wmes;
   var wano;
   var rret;
   var rstr;

   rret = true;

   

   if (argvalue.length == 10)
   {
      for (n = 0; n < argvalue.length; n++)
      {
         if ((argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9") && (argvalue.substring(n, n+1) != "/"))
         {
            rret = false;
         }
      }

      if (rret)
      {
         wdia = argvalue.substring(0, 2);
         wmes = argvalue.substring(3, 5);
         wano = argvalue.substring(6, 10);

         if (argvalue.substring(2, 3) != "/" || argvalue.substring(5, 6) != "/")
         {
            rret = false;
         }
         else
         {
            if (wmes < "01" || wmes > "12")
            {
               rret = false;
            }
            else
            {
               if ((wmes == "01") || (wmes == "03") || (wmes == "05") || (wmes == "07") || (wmes == "08") || (wmes == "10") || (wmes == "12"))
               {
                  if (wdia < "01" || wdia > "31")
                  {
                     rret = false;
                  }
               }
               else
               {
                  if ((wmes == "04") || (wmes == "06") || (wmes == "09") || (wmes == "11"))
                  {
                     if (wdia < "01" || wdia > "30")
                     {
                        rret = false;
                     }
                  }
                  else
                  {
                     if (wmes == "02")
                     {
                        if ((wano % 4) == 0)
                        {
                           if (wdia < "01" || wdia > "29")
                           {
                              rret = false;
                           }
                        }
                        else
                        {
                           if (wdia < "01" || wdia > "28")
                           {
                              rret = false;
                           }
                        }
                     }
                     else
                     {
                        rret = false;
                     }
                  }
               }
            }
         }
      }
   }
   else
   {
      rret = false;
   }

   return rret;
}

//-- CRÍTICA DE HORAS --

function CritHora(argvalue)
{
   var whora;
   var wmin;
   var rret;
   var rstr;
   
   rret = true;

   if (argvalue.length != 5)
   {  rret = false; 
   }

   if (argvalue.substring(2,3) != ":")
   {  rret = false; 
   }
	  
   if (rret)
   {  whora = argvalue.substring(0,2);
	  wmin  = argvalue.substring(3,5);
		 
  	 if (whora < "00" || whora > "23")
	 {  rret = false; 
	 }
	  
	  
	 if (wmin < "00" || wmin > "59")
	 {  rret = false; 
	 }
   }
   return rret;
}

//-- CRÍTICA DE ENDEREÇO DE E-MAIL --
 
function CritMail(cadeia)
{
   var wok = true;
   var pos = InStr(cadeia, "@");
   
   if (pos <= 0)
   {  wok = false;
   }
   else
   {  if ((pos + 1) == cadeia.length)
      {  wok = false;
      }
      else
      {  pos = InStr(cadeia, " ");

         if (pos >= 0)
         {  wok = false;
         }
         else
         {  pos = InStr(cadeia, "\\");

            if (pos >= 0)
            {  wok = false;
            }
            else
            {  pos = InStr(cadeia, "/");

               if (pos >= 0)
               {  wok = false;
               }
            } 
         }
      }
   }
   
   return wok;	  
} 
       
//-- VALIDA A TECLA PRESSIONADA DE ACORDO COM O TIPO DO CAMPO

function ValKey (restricao, argvalue) 
{
   var keycode = 0;

   // "C"  -> permite somente letras
   // "N"  -> permite somente números 
   // "D"  -> permite números com casa decimal
   // "DN" -> permite números (positivos e/ou negativos) com casa decimal 
   // "DT" -> permite números e barra para campo data
   // "DH" -> permite números e dois pontos para campo hora
   // "X"  -> permite qualquer caractere menos plica e acento
   // "NL" -> impede que qualquer coisa seja digitada
 
   if (window.event) 
   {  keycode = window.event.keyCode;  
   
	  if ((restricao == "X") && (keycode == 39 || keycode == 180 || keycode == 38 || keycode == 34))
      {  window.event.keyCode = 0;
	  }
	  else 
      {  if ((keycode > 47) && (keycode < 58))   
	     {  if (restricao == "C")
	        {  window.event.keyCode = 0;
  		    }
	     }
	     else
	     {  if ((restricao == "DT") && (keycode != 47))
	        {  window.event.keyCode = 0;
		    }
		    else
		    {  
			   if ((restricao == "DH") && (keycode != 58))
			   {  window.event.keyCode = 0;			   
			   }
			   else
			   {
					if ((restricao == "D") && (keycode != 44))
  					{  window.event.keyCode = 0;
					} 
					else
					{  if ((restricao == "DN") && (keycode != 44) && (keycode != 45))
  						{  window.event.keyCode = 0;
						}
					}
				
					if (keycode == 44)
					{  if (InStr(argvalue, ",") >= 0 && restricao != "X")
  						{  window.event.keyCode = 0;
						}
					}  
					else
					{  if (keycode == 45 && restricao == "DN")
						{  if (InStr(argvalue, "-") >= 0)
  							{  window.event.keyCode = 0;
							} 
						}  
					}   
 
			   }
			   if ((restricao == "N"))
	           {  window.event.keyCode = 0;
  		       }
   
			}   
		 }
		 
	     //--- TECLA PLIC E ACENTO SEM VOGAL ---
   
		 if ((window.event.keyCode == 39) || (window.event.keyCode == 180) || (keycode == 34)) 
	     {  window.event.keyCode = 0;
	     }
	  }	 
   }
}

function FormatStr(argvalue, mask)  
{   
   var n;
   var strret = "";
   var nrep = 0;

   if ((argvalue.length==0)||(argvalue.substring(0,1)==" "))
   {  strret = argvalue;
   }
   else
   {  nrep   = mask.length - argvalue.length;
      strret = mask.substring(0, nrep) + argvalue;
   }
   
   return strret;
}   

function Arredondamento(valor, numdec)
{
   var mascara = "999.999.999.999";
   var valmasc = "";
   var I       = 1;

   if (valor == "")
   {  valor = "0";
   }
   
   valor = parseFloat(ReplStr(valor, ",", "."));
   valor = ReplStr("" + valor, ".", ",");
	
   if (InStr(valor, ",") < 0)
   {  valor = valor + ","
      I = 1;
   }   
   else
   {  valor = valor.substring(0, (InStr(valor, ",") + numdec + 1));
      I = (valor.length - InStr(valor, ","));
   }

   for (n = I; n <= numdec; n++)
   {  valor = valor + "0"; 
   }

   valor = ReplStr(valor, ",", "");
   
   if (numdec > 0)
   {  mascara = mascara + ","; 
      
      for (n = 1; n <= numdec; n++)
      {  mascara = mascara + "9"; 
	  }
   }
   
   valmasc = FormatMasc(valor, mascara, "S");	  
   
   return valmasc;
}

function Formatador(campo,tammax,teclapres) 
{
	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	if (tecla == 8 ){	tam = tam - 1 ; }
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
	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 <= 17) ){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 ) ;}
	}
}

function ReplStr(argvalue, strant, strnew)
{
   var straux = "";

   for (n = 0; n < argvalue.length; n++)
   {  if (argvalue.substring(n, n+1) == strant)
      {  straux = straux + strnew;
      }
	  else
	  {  straux = straux + argvalue.substring(n, n+1);
	  }
   }
 
   return straux;
}

function StringToDouble(valStr)
{
   valStr = ReplStr(valStr, ".", "");
   valStr = ReplStr(valStr, ",", ".");
   
   return valStr;
}

function FormatMasc(strin, mascara, formatar)
{
   var strout = "";
   var indin = strin.length - 1;

   if (formatar == "N")
   {  strout = ReplStr(strin, "-", "");
      strout = ReplStr(strout, ".", "");
      strout = ReplStr(strout, "/", "");
   }
   else
   {  if ((mascara == "") || (strin == ""))
      {  strout = strin;
      }
      else
      {  for (n = mascara.length - 1; n >= 0; n--)
         {  if ((mascara.substring(n, n+1) == ".") || (mascara.substring(n, n+1) == "-") || (mascara.substring(n, n+1) == "/") || (mascara.substring(n, n+1) == ","))
            {  strout = mascara.substring(n, n+1) + strout;
            }
	        else
	        {  strout = strin.substring(indin, indin+1) + strout;
			   indin = indin - 1;
			   
			   if (indin < 0)
			   {  n = -1;
			   } 
	        }
	     } 	 
		 
		 //if (strout.length < mascara.length)
		 //{  strout = mascara.substring(0, mascara.length - strout.length) + strout;
		 //}
      }
   }	  
 
   return strout;
}

function TrataCase(flcase) 
{
   if ((window.event.keyCode >= 65 && window.event.keyCode <= 90) || (window.event.keyCode >= 192 && window.event.keyCode <= 220))
   {  if (flcase == "L")
      {  window.event.keyCode = window.event.keyCode + 32;
	  }
   }
   else
   {  if ((window.event.keyCode >= 97 && window.event.keyCode <= 122) || (window.event.keyCode >= 224 && window.event.keyCode <= 252))
      {  if (flcase == "U")
         {  window.event.keyCode = window.event.keyCode - 32;
	     } 
      }
   }	  
}

function InStr(cadeia,busca)
{
   var posicao = -1;
   var n;
   var tamanho;

   tamanho = cadeia.length ;

   for (n=0; n < tamanho; n++)
   {  if (cadeia.substring(n, busca.length + n) == busca)
      {  posicao = n;
         n = tamanho + 1
	  }
   }
  
   return posicao;
}
	
function ShowObj(id,obj)
{
   var i = 0;
      
   if (obj[id].style.display == "block")
   {
      obj[id].style.display = "none";
   }
   else
   {
      obj[id].style.display = "block";
   }    

   for (i==0;i < obj.length;i++)
   {
      if (i != id)
      {
         obj[i].style.display = "none";
      }
   }
}
      
function PoePontos(argvalue)
{
   var I         = 0;
   var numpontos = 0;
   var keycode   = 0;

   keycode = window.event.keyCode; 
   
   if (keycode != 8 && keycode != 46)
   {  I = InStr(argvalue.value, ":");
   
      if (I >= 0)
      {  numpontos = numpontos + 1;
      }
   
      I = InStr(argvalue.value.substring(I + 1, 5), ":");
   
      if (I >= 0)
      {  numpontos = numpontos + 1;
      }
      
      if ((argvalue.value.length == 2) && (numpontos < 1))
      {  argvalue.value = argvalue.value + ":";
	  }
   }	  
}

function ChamaExcel()
{
   var strpar;
   strpar =  window.location.href + "&EXCEL=S"
 
   //alert("window.open('" + strpar + "','crianca','resizable=yes,width=790,height=550,left=0,top=0,toolbars=yes,menubar=yes')");
   eval( "window.open('" + strpar + "','crianca','resizable=yes,width=790,height=550,left=0,top=0,toolbars=yes,menubar=yes')",false); 
}

function recarrega()
{
   if ( parent.TMSG == "[object]")
   {
  	  parent.TMSG.style.display="block";
	  parent.TRESULTADO.style.display = "none"; 
   }
   
   window.location.reload();
}

function mensagem(waviso)
{
   if (trim(" " + waviso) != "")
   {
      alert(waviso);
   }
}

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_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_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];}
}      
      
var marcada = -1;      


