/*
FUNÇÃO PARA POSICIONAR O CURSOR NO PRIMEIRO CAMPO DO FORMULÁRIO
*/
function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "password") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
         }
      }
   }
}

/*-----------------------------------------------------------------*
'nome...: Função para verificar se dados são numérico
 *-----------------------------------------------------------------*/
	function isNumeric(sText)
{
		var ValidChars = "0123456789.";
		var IsNumber=true;
		var Char;


		for (i = 0; i < sText.length && IsNumber == true; i++) 
					{ 
					Char = sText.charAt(i); 
					if (ValidChars.indexOf(Char) == -1) 
								{
								IsNumber = false;
								}
					}
  return IsNumber;
}

/*-----------------------------------------------------------------*
'nome...: Função para validação de data
 *-----------------------------------------------------------------*/		
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 (!isNumeric(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
}

/*-----------------------------------------------------------------*
'nome...: Função para formatação de datas
 *-----------------------------------------------------------------*/
function DataPress(Data)
{ 

if (event.keyCode != 08)
{

 if ((Data.length == 2) || (Data.length == 5))
    {Data = Data + "/";}
   
 if (event.keyCode > 47 && event.keyCode < 58)
    {return Data;}
 else	
    {event.keyCode = 0;
	return Data;}		
}
}

/*-----------------------------------------------------------------*
'nome...: Função para formatação de datas
 *-----------------------------------------------------------------*/
function TimePress(hora)
{ 

if (event.keyCode != 08)
{
 if (hora.length == 2)
    {hora = hora + ":";}
   
 if (event.keyCode > 47 && event.keyCode < 58)
    {return hora;}
 else	
    {event.keyCode = 0;
	return hora;}		
}
}

/*-----------------------------------------------------------------*
'nome...: Função para formatação de datas ao perder o foco do campo
 *-----------------------------------------------------------------*/
function Formata_Hora(hora)
{ 
 var tam = hora.length;

	if(tam < 1)
	{
			hora = "00:00";
	}
	else
	{
					if(tam == 1)
					{
								hora = "0" + hora.substr(0, 1) + ":00";
					}
					else
					{
							if(tam == 2)
							{
								hora =  hora.substr(0, 2) + ":00";
							}
							else
							{
							if(tam == 3)
							{
									hora =  hora.substr(0, 2) + ":00";
							}
							else
							{
									if (tam==4)
									{
										hora =  hora + "0";
									}
									else
									{
										hora =  hora.substr(0, 2) + ":" + hora.substr(3, 2)
									}
							}
					}
			}
	}
	return hora;
}

/*-----------------------------------------------------------------*
'nome...: Função para validação de hora
 *-----------------------------------------------------------------*/
function ValidaHora(hora,minuto,meio)
{ 
 bret  = true;
 vhora = parseInt(hora);
 vmin  = parseInt(minuto);
	
	if (meio == ":")
	{
			if (vhora > 23) //hora inválida
			{
				bret = false;	
			}
			else
			{
					if (vmin > 59) //minutos inválidos
					{
						bret = false;	
					}
			}
	}
	else
	{
		bret = false;	//hora inválida
	}
	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;
  }
  
function isBissexto(iAno)
{
  var bRet
  bRet = false
  if (iAno % 4 == 0 && (iAno % 100 !=0 || iAno % 400 ==0 ))
    bRet = true
  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
}

/*--------------------------------------------
'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;
}

/*-----------------------------------------------------------------*
'nome...: Função para só permitir a digitação de números
 *-----------------------------------------------------------------*/
function ColocaZero(texto)
{
  x = 0;
  
  if (texto == "")
     {texto = "0,00";} 

  if (texto == "0")
     {texto = "0,00";} 	 
  
  if (texto == ",")
     {texto = "0,00";} 	 		  
   
  if (texto == "00")
     {texto = "0,00";} 

  if (texto == "000")
     {texto = "0,00";}	

  if (texto == "0,0")
     {texto = "0,00";}
	 
  for (i=1; i<=texto.length; i++)
	  {			
	  if (texto.substr(i, 1) == ",")
		 {	
		 x = 1;
		 }
	  }
  if (x == 0)
     {  
     return texto + ',00' 	  	 		  
	 }
  else
     {	 
     return texto;      
     }	 
}

/*-----------------------------------------------------------------*
'nome...: Função para só permitir a digitação de números
 *-----------------------------------------------------------------*/
function Zeracep(texto)
{
if (texto == "")
   texto = "00000000";     
return texto;   
   
}


/*-----------------------------------------------------------------*
'nome...: Função para atualização de horários
 *-----------------------------------------------------------------*/
function Horario(turno)
{ 
 if (turno == "M")
 {
   document.form1.txtHoraInicio.value = "08";
   document.form1.txtMinutoInicio.value = "00";
   document.form1.txtHoraFim.value = "12";
   document.form1.txtMinutoFim.value = "00";
 }
 
  if (turno == "T")
 {
   document.form1.txtHoraInicio.value = "12";
   document.form1.txtMinutoInicio.value = "00";
   document.form1.txtHoraFim.value = "18";
   document.form1.txtMinutoFim.value = "00";
 }
 
   if (turno == "N")
 {
   document.form1.txtHoraInicio.value = "18";
   document.form1.txtMinutoInicio.value = "00";
   document.form1.txtHoraFim.value = "24";
   document.form1.txtMinutoFim.value = "00";
 }
 
}

function SomenteNumeros(input)
	{
	if ((event.keyCode<48)||(event.keyCode>57))
		event.returnValue = false;
	}

/**********************************
FUNÇÃO PARA FORMATAÇÃO DE VALORES 
***********************************/
function FormataValor2(campo,tammax,teclapres) {

	var tecla = teclapres.keyCode;
	var 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 + ',00' ; }
	 	tam = tam - 1;
	 	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 currencyFormat(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '0,00';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 1) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}

function numbers_only(){
	if (event.keyCode < 48 || event.keyCode > 57 ) 
        event.returnValue = false;
}
function Num_onkeypress() {
			numbers_only()
		}

function money_only(){
	if (event.keyCode < 48 || event.keyCode > 57 ) 
	    if (event.keyCode != 44)
           event.returnValue = false;
}
function Money_onkeypress() {
			money_only()
		}

function DataHoje()
{
   var d, s = "";                            //Declare variables.
   d = new Date();                           //Create Date object.
   s += d.getDate() + "/";                   //Get day
   s += (d.getMonth() + 1) + "/";            //Get month
   s += d.getYear();                         //Get year.
   return(s);                                //Return date.
}


function isNUMB(c) 
 { 
 if((cx=c.indexOf(","))!=-1) 
  { 
  c = c.substring(0,cx)+"."+c.substring(cx+1); 
  } 
 if((parseFloat(c) / c != 1)) 
  { 
  if(parseFloat(c) * c == 0) 
   { 
   return(1); 
   } 
  else 
   { 
   return(0); 
   } 
  } 
 else 
  { 
  return(1); 
  } 
 } 

function LIMP(c) 
 { 
 while((cx=c.indexOf("-"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("/"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(","))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("."))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("("))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(")"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(" "))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 return(c); 
 } 

function VerifyCNPJ(CNPJ) 
 { 
 CNPJ = LIMP(CNPJ); 
 if(isNUMB(CNPJ) != 1) 
  { 
  return(0); 
  } 
 else 
  { 
  if(CNPJ == 0) 
   { 
   return(0); 
   } 
  else 
   { 
   g=CNPJ.length-2; 
   if(RealTestaCNPJ(CNPJ,g) == 1) 
    { 
    g=CNPJ.length-1; 
    if(RealTestaCNPJ(CNPJ,g) == 1) 
     { 
     return(1); 
     } 
    else 
     { 
					alert("CNPJ inválido!")
     return(0); 
     } 
    } 
   else 
    { 
				alert("CNPJ inválido!")
    return(0); 
    } 
   } 
  } 
 } 
function RealTestaCNPJ(TxtCodCNPJ,g) 
 { 
 var VerCNPJ=0; 
 var ind=2; 
 var tam; 
 for(f=g;f>0;f--) 
  { 
  VerCNPJ+=parseInt(TxtCodCNPJ.charAt(f-1))*ind; 
  if(ind>8) 
   { 
   ind=2; 
   } 
  else 
   { 
   ind++; 
   } 
  } 
  VerCNPJ%=11; 
  if(VerCNPJ==0 || VerCNPJ==1) 
   { 
   VerCNPJ=0; 
   } 
  else 
   { 
   VerCNPJ=11-VerCNPJ; 
   } 
 if(VerCNPJ!=parseInt(TxtCodCNPJ.charAt(g))) 
  { 
  return(0); 
  } 
 else 
  { 
  return(1); 
  } 
 } 
  

  function FormataCGC(Formulario, Campo, TeclaPres) 
  { 
    var tecla = TeclaPres.keyCode; 
    var strCampo; 
    var vr; 
    var tam; 
    var TamanhoMaximo = 14; 
  
    eval("strCampo = document." + Formulario + "." + Campo); 
  
    vr = strCampo.value; 
    vr = vr.replace("/", ""); 
    vr = vr.replace("/", ""); 
    vr = vr.replace("/", ""); 
    vr = vr.replace(",", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    vr = vr.replace(".", ""); 
    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 < TamanhoMaximo && 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) 
      { 
        strCampo.value = vr; 
      } 
       if ((tam > 2) && (tam <= 6)) 
       { 
         strCampo.value = vr.substr(0, tam - 2) + '-' + vr.substr(tam - 2, tam); 
       } 
       if ((tam >= 7) && (tam <= 9)) 
       { 
         strCampo.value = vr.substr(0, tam - 6) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
      } 
       if ((tam >= 10) && (tam <= 12)) 
       { 
         strCampo.value = vr.substr(0, tam - 9) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
      } 
       if ((tam >= 13) && (tam <= 14)) 
       { 
         strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
      } 
       if ((tam >= 15) && (tam <= 17)) 
       { 
         strCampo.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 TESTA() 
 { 
 if(VerifyCNPJ(document.forms[0].TxtCodCNPJ.value) == 1) 
  {  
  } 
 else 
  { 
  alert("CNPJ não é válido!"); 
  document.forms[0].TxtCodCNPJ.value ='';
  document.forms[0].TxtCodCNPJ.focus(); 
  } 
 
 return; 
 } 
	
	/*-----------------------------------------------------------------*
'nome...: Função para só permitir a digitação de números
*-----------------------------------------------------------------*/
function Tecla(e)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
		if (tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else
			{
				if (tecla != 8) // backspace
					event.keyCode = 0;
					//return false;
				else
					return true;
			}
}

function msg(tipo)
			{
					if (tipo == 1)
					{
							alert("Não foi possível localizar dados!")
					}
					else
					{
   				alert("Registro já Existe!")
					}
						
		  	return(true)
}
			
