' =======================================================================
' >>> Função FormataNumeroCartao
'			Striong: Retorna o numero do cartao com separadores;
' =======================================================================

Function FormataNumeroCartao(campo)
	i = 0
	FormataNumeroCartao = ""
	If Len(Campo) = 15 Then
		FormataNumeroCartao = left(campo,2) & "." & _
							  mid(campo,3,3) & "." & _
							  mid(campo,6,8) & "." & _
							  right(campo,2)		
	Else
		While i <> Len(Campo)
		  i = i + 1
		  FormataNumeroCartao = FormataNumeroCartao & Mid(Campo, i, 1)
		  if ((i mod 4) = 0) and (i<13) then
		    FormataNumeroCartao = FormataNumeroCartao & "."
		  End if	  
		Wend 
	End If
  End Function
  
' =======================================================================
' >>> Procedimento ComplementaEspacos
' =======================================================================

Sub ComplementaEspacos(indCampo, tamanho)
	While len(document.Formulario(indCampo).value) < tamanho
		document.Formulario(indCampo).value = document.Formulario(indCampo).value & " "
	WEnd
End Sub

' =======================================================================
' >>> Procedimento VerificaEnter
' =======================================================================

Sub VerificaEnter(evento,proximo)
	if evento.keyCode = 13 then document.Formulario(proximo).focus
End Sub

' =======================================================================
' >>> Procedimento SelecionaCampo
'			campo: sequencia do campo a ser selecionado no objeto Document.
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

Sub SelecionaCampo(campo) 	
	document.Formulario(campo).select	
End Sub

' =======================================================================
' >>> Função RetiraSeparadores
'			campo: sequencia do campo a ser selecionado no objeto Document.
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

Function RetiraSeparadores(campo) 	
	strCampo = Trim(campo)
	strCampo = Replace(strCampo, ".", "")
	strCampo = Replace(strCampo, ",", "")
	RetiraSeparadores = strCampo
End Function


' =======================================================================
' >>> Função VerificaNomeCartao
'			Campo: Campo a ser verificado.
'	Última atualização => 10/08/2000 (Cláudio Junes)
' =======================================================================

Function VerificaNomeCartao(campo) 	
   Dim i
	strCampo = Trim(campo)
	For i = 1 to len(strCampo)
		AscLetra = Asc(mid(strCampo,i,1))
		If not ((AscLetra = 32) or _
			    (AscLetra >= 65 and AscLetra <= 90) or _
		        (AscLetra >= 97 and AscLetra <= 122)) Then
					VerificaNomeCartao = false
					Exit Function
		End If		
	Next
	VerificaNomeCartao = true
End Function


' =======================================================================
' >>> Função IIF
'			BoolExp: expressão booleana de referência;
'			TruePart: retorno em caso verdadeiro;
'			FalsePart: retorno em caso verdadeiro.
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

Function IIf(BoolExp,TruePart,FalsePart)
	If BoolExp Then
		IIf = TruePart
	Else
		IIf = FalsePart
	End If
End Function


' =======================================================================
' >>> Função RetornaValorOperadorAnd
'			Objeto: Objeto passado como parametro
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

Function RetornaValorOperadorAnd(Objeto)
	Dim valor,i
	valor = 0
	for i = 0 to Objeto.length-1
		if Objeto(i).checked = true then
			valor = valor + Objeto(i).value
		end if
	next
	RetornaValorOperadorAnd = valor
End Function


' =======================================================================
' >>> Função VerificaData
'			dtaData: data a ser convertida.
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

Function VerificaData(dtaData)
	If dtaData <> "" Then
		strDia = left(dtaData,2)
		strMes = mid(dtaData,4,2)
		If strDia > 31 Then
			VerificaData = false
			Exit Function
		End If
		If strMes > 12 Then
			VerificaData = false
			Exit Function
		End If
		VerificaData = true
	end If
End Function


Function VerificaObjData(objData)
	VerificaObjData = false
	strData = objData.value

	strDia = left(dtaData,2)
	strMes = mid(dtaData,4,2)
	strAno = Right(dtaData,4)
	
	strDataInv = strMes & "/" & strDia & "/" & strAno

	If isDate(strData) or isDate(strDataInv) or strData = "" Then
		VerificaObjData = True
	End if

	If VerificaObjData = false then 
		msgbox "Data Inválida: " & strData
		'objData.value = ""
		objData.focus()	
	End If
End Function

' =======================================================================
' >>> Função VerificaCPF
'			sCPF: Número do CPF a ser validado.
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

Function VerificaCPF (sCpf)
   'type
   '  CpfTotal  = array [1..11] Of Integer;
   
   Dim iNro(11)        ' Array com os 9 primeiros dígitos do CPF }
   Dim iDigito         ' LongInt; { Variável para obter o dígito na sequencia }
   Dim iCount          ' Integer;
   Dim iFirstDigit     ' Integer;
   Dim iSecondDigit    ' Integer;
   Dim iResto          ' Integer;
   Dim iSoma           ' Integer;

   iSoma           = 0
   iFirstDigit     = 0
   iSecondDigit    = 0
   
   sCpf = Trim(sCpf)
   sCpf = Replace(sCpf, ".", "")
   sCpf = Replace(sCpf, "-", "")
   
   If Not Len(Trim(sCpf)) = 11 Then
		VerificaCPF = false
		Exit Function
   End If   

   For iCount = 1 to 11        ' { laço para separar os dígitos }
      iDigito  = CInt(Mid(sCpf, iCount, 1))
      iNro(iCount) = iDigito
   Next

   '{**** calcula o primeiro digito verificador **** }
   '{**** Calcula-se o valor dos digitos multiplicando-os por 10 em reverso}

   iSoma = ((10 * iNro(1)) + ( 9 * iNro(2)) + ( 8 * iNro(3)) + _
            ( 7 * iNro(4)) + ( 6 * iNro(5)) + ( 5 * iNro(6)) + _
            ( 4 * iNro(7)) + ( 3 * iNro(8)) + ( 2 * iNro(9))   )

   iResto = iSoma Mod 11      '{ obtem o resto }
   '{ obtem o primeiro digito }
   If ((iResto = 0) or (iResto = 1)) Then
      iFirstDigit = 0
   Else
      iFirstDigit = (11 - iResto)
   End If

   '{***** calcula o segundo digito verificador }
   iSoma = ((11 * iNro(1)) + (10 * iNro(2)) + ( 9 * iNro(3)) + _
            ( 8 * iNro(4)) + ( 7 * iNro(5)) + ( 6 * iNro(6)) + _
            ( 5 * iNro(7)) + ( 4 * iNro(8)) + ( 3 * iNro(9)) + _
            ( 2 * iFirstDigit))

   iResto = iSoma mod 11      '{ obtem o resto }
   '{ obtem o segundo digito }
   If ((iResto = 0) or (iResto = 1)) then
      iSecondDigit = 0
   Else
      iSecondDigit = (11 - iResto)
   End If

   '{ Obtém a verificação dos dígitos }
   Result = ((iFirstDigit = iNro(10)) And (iSecondDigit = iNro(11)))

   VerificaCPF = Result

End Function


' =======================================================================
' >>> Função VerificaCNPJ
'			sCNPJ: Número do CNPJ a ser validado.
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

Function VerificaCNPJ ( sCNPJ ) ': Boolean;
   'type
   '  CNPJTotal  = array [1..14] of Integer;
   '  CNPJMult   = array [1..12] of Integer;
   'Const
   '  CNPJMultD1: CNPJMult = (5,4,3,2,9,8,7,6,5,4,3,2);
   '  CNPJMultD2: CNPJMult = (6,5,4,3,2,9,8,7,6,5,4,3);

   Dim iNro(14)        ': CNPJTotal; { Array com os 14 primeiros dígitos do CNPJ }
   Dim iDigito         ': LongInt; { Variável para obter o dígito na sequencia }
   Dim iCount          ': Integer;
   Dim iFirstDigit     ': Integer;
   Dim iSecondDigit    ': Integer;
   Dim iResto          ': Integer;
   Dim iSoma           ': Integer;
   Dim iMultDigit1     ': CNPJMult;
   Dim iMultDigit2     ': CNPJMult;
   Dim CNPJMultD1
   Dim CNPJMultD2

'begin
   iSoma           = 0
   iFirstDigit     = 0
   iSecondDigit    = 0
   
   sCNPJ = Trim(sCNPJ)
   sCNPJ = Replace(sCNPJ, ".", "")
   sCNPJ = Replace(sCNPJ, "-", "")
   sCNPJ = Replace(sCNPJ, "/", "")   
   
   If Not Len(Trim(sCNPJ)) = 14 Then
		VerificaCNPJ = false
		Exit Function
   End If   

   CNPJMultD1 = Array(0,5,4,3,2,9,8,7,6,5,4,3,2)
   CNPJMultD2 = Array(0,6,5,4,3,2,9,8,7,6,5,4,3)

   For iCount = 1 to 14 'do        { laço para separar os dígitos }
          iDigito  = CInt(Mid(sCNPJ, iCount, 1))
          iNro(iCount) = iDigito
   Next

   '{**** calcula o primeiro digito verificador **** }
   '{**** Calcula-se o valor dos digitos multiplicando-os por 10 em reverso}
   For iCount = 1 to 12 ' do
       iSoma = iSoma + (iNro(iCount) * CNPJMultD1(iCount))
   Next

   iResto = (iSoma * 10 ) mod 11 ';      { obtem o resto }

   '{ obtem o primeiro digito }
   If (iResto = 10) Then
      iFirstDigit = 0
   Else
      iFirstDigit = iResto
   End If

   '{***** calcula o segundo digito verificador }
   iSoma = 0

   For iCount = 1 to 12 'do
       iSoma = iSoma + (iNro(iCount) *  CNPJMultD2(iCount))
   Next

   iSoma = iSoma + (2 * iFirstDigit)

   iResto = (iSoma * 10 ) mod 11      '{ obtem o resto }

   '{ obtem o segundo digito }
   If (iResto = 10) then
      iSecondDigit = 0
   Else
      iSecondDigit = iResto
   End If

   '{ Obtém a verificação dos dígitos }
   Result = ((iFirstDigit = iNro(13)) and (iSecondDigit = iNro(14)))

   VerificaCNPJ = Result

End Function


' =======================================================================
' >>> Procedimento cmdMontaValidacao_onClick()
'	Última atualização => 25/05/2000 (Cláudio Junes)
' =======================================================================

' HTML do Botão:
' ==============
' <!-- 
' <br>
' <input type="Button" name="cmdMontaValidacao" value="Monta Validação">

' HTML do Campo de Texto para o resultado:
' ========================================
' <br><br>
' <b>Resultado da Montagem da Validação:</b><br>
' <TEXTAREA rows=12 cols=100 name=TextAreaResultados></TEXTAREA>
' <br>
' -->

Sub cmdMontaValidacao_onClick()

    strEstrutura =	"'Código HTML do Botão: " + chr(13) + _
					"<input type=Button name=cmdEnvia value=Salvar>" + chr(13) + chr(13) + _
					"Sub cmdEnvia_onClick() " + chr(13) + _
					"	Dim theform, isValid, theMsg " + chr(13) + _
					"	isValid = true " + chr(13) + _
					"	Set theform=Document.Formulario " + chr(13)
					
	strAcao = "("
    
    For i = 0 To document.all.length-1
		if UCase(document.all(i).tagName) = "INPUT" then
			if UCase(document.all(i).type) = "TEXT" then				
				select case UCase(Left(document.all(i).Name,3))
					case "STR"
						strAcao = strAcao + _
						" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
					case "INT"
						strAcao = strAcao + _
						" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13) + _
						"	If not isNumeric(theform." + document.all(i).Name + ".value) Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' não numérico. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
					case "DEC"
						strAcao = strAcao + _
						" ConverteDecimal(Request.Form(" + """" + document.all(i).name + """" + ")), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13) + _
						"	If not isNumeric(theform." + document.all(i).Name + ".value) Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' não numérico. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
					case "PRC"
						strAcao = strAcao + _
						" ConverteDecimal(Request.Form(" + """" + document.all(i).name + """" + ")), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13) + _
						"	If not isNumeric(theform." + document.all(i).Name + ".value) Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' não numérico. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	ElseIf retiraSeparadores(theform." + document.all(i).Name + ".value)/100 < 0 or retiraSeparadores(theform." + document.all(i).Name + ".value)/100 > 100 Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' fora da faixa de 0 a 100%. """ + chr(13) + _
						"		isValid = false " + chr(13) + _						
						"	End If " + chr(13)
					case "DTA"
						strAcao = strAcao + _
						" ConverteData(Request.Form(" + """" + document.all(i).name + """" + ")), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13) + _
						"	If not VerificaData(theform." + document.all(i).Name + ".value) Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' inválido. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
					case "TEL"
						strAcao = strAcao + _
						" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
					case "CEP"
						strAcao = strAcao + _
						" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
					case "CPF"
						strAcao = strAcao + _
						" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13) + _
						"	If not VerificaCPF(theform." + document.all(i).Name + ".value) Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - CPF inválido. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
					case "CPJ"
						strAcao = strAcao + _
						" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)
						strEstrutura = strEstrutura + _
						"	If theform." + document.all(i).Name + ".value = " + """" + """" + " Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - Campo '" + right(document.all(i).Name,len(document.all(i).Name)-3) + "' em branco. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13) + _
						"	If not VerificaCNPJ(theform." + document.all(i).Name + ".value) Then " + chr(13) + _
						"		theMsg = theMsg + chr(13) + chr(10) + " + """ - CNPJ inválido. """ + chr(13) + _
						"		isValid = false " + chr(13) + _
						"	End If " + chr(13)	
				end select
			else
				strAcao = strAcao + _
				" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)	
			end if
		elseif UCase(document.all(i).tagName) = "SELECT" then
			strAcao = strAcao + _
			" Request.Form(" + """" + document.all(i).name + """" + "), _ " + chr(13)
		end if
    Next
	strEstrutura = strEstrutura + _
	"	theMsg = " + """" + "Os campos abaixo são inválidos ou não foram preenchidos: " + """" + " + theMsg " + chr(13) + _
	"	If isValid then " + chr(13) + _
	"		theform.Submit " + chr(13) + _
	"	Else " + chr(13) + _
	"		MsgBox (theMsg), 48, " + """" + document.title + """" + chr(13) + _
	"	End If " + chr(13) + _
	"End Sub "
	strAcao = strAcao + ")"
    
   document.all("TextAreaResultados").value = strEstrutura + chr(13) + strAcao

End Sub

Sub chgMenu(psItem)
	Dim iTotalMenu
	dim i
	
	iTotalMenu = 17
	
	
	Set doc = top.frames(1).document
	
	For i = 1 to iTotalMenu
	
		if i <> 5 then
		
			If ("C-" & i) = psItem Then
				Doc.All("C-" & i).Src = "images/menu3.gif"
			Else
				Doc.All("C-" & i).Src = "images/quadrado.gif"
			End If
			
		End If
	Next
	
End Sub

Function RetornaDiaReferencia(Dia)
	If Dia <= 0 then 
		RetornaDiaReferencia = (Dia mod 30) + 30
		Exit Function
	End If
	
	If Dia > 30 then 
		RetornaDiaReferencia = (Dia mod 30)
		if (Dia mod 30) = 0 then
			RetornaDiaReferencia = 30
		end if
		Exit Function
	End If
		
	RetornaDiaReferencia = Dia
	
  End Function