﻿// JScript File
var theForm = document.forms['aspnetForm'];

if (!theForm) 
{
    theForm = document.aspnetForm;
}

function fncValidaDados(asNomeForm)
{
	var lsValor;
	var i;		
	var lsCtg;
	
						
    if (theForm.ctl00$cphMain$edtVlrIni != null)
    {
        lsValor = theForm.ctl00$cphMain$edtVlrIni.value;

	    if ( !IsEmpty(lsValor)) 
	    {
    			
	       if (!ValidaEntrada(1,"D",theForm.ctl00$cphMain$edtVlrIni))
	        {
		        return false;
	        }
    	    
	        lsValor = DesFormatDec(theForm.ctl00$cphMain$edtVlrIni.value) / 100;
    	
	        if ( lsValor <= 0 )
	        {
		        alert("Valor Inicial precisa ser maior do que zero.");
		        theForm.ctl00$cphMain$edtVlrIni.focus();
		        return false;
	        }
    	    
	    }
	}

    if (theForm.ctl00$cphMain$edtVlrFin != null)
    {
    	
	    lsValor = theForm.ctl00$cphMain$edtVlrFin.value;

	    if ( !IsEmpty(lsValor)) 
	    {
    			
	       if (!ValidaEntrada(1,"D",theForm.ctl00$cphMain$edtVlrFin))
	        {
		        return false;
	        }
    	    
	        lsValor = DesFormatDec(theForm.ctl00$cphMain$edtVlrFin.value) / 100;
    	
	        if ( lsValor <= 0 )
	        {
		        alert("Valor Final precisa ser maior do que zero.");
		        theForm.ctl00$cphMain$edtVlrFin.focus();
		        return false;
	        }
    	    
	    }
	}
	
    return true;


}

function IsEmpty(asValor)
{
		
		if ( asValor == null || asValor =="")
			return true;
		return false;
}


function IsNumeric(asValor)
{
		
	if (IsEmpty(asValor))
		return false;
			
	for (var i = 0; i <= asValor.length - 1; i++) 
	{
		
		if (isNaN(parseInt(asValor.substr(i,1),10)))
		{
			
			return false;
		}
		
	} 
		
		return true;
}

   
function DesFormatDec(lsValor)
   {
   	var lsDecText;
		var lsIntText;
		var liCont;
		var re = /\./g
		
		if (lsValor.indexOf(',') == -1) 
			lsValor += ",";
			     
		lsDecText = lsValor.substring(lsValor.indexOf(',')+1,lsValor.length);
		lsIntText = lsValor.substring(0,lsValor.indexOf(','));
		
		if (lsDecText.length > 2 )
			lsDecText = lsDecText.substring(0,2);
		   
		
		liCont = (2 - lsDecText.length );
		for (i=0; i < liCont;i++) 
		{
			lsDecText += "0";
	   }
      
      lsIntText = lsIntText.replace(re,"");
      
      return lsIntText + lsDecText
   
   }
   
  
 function FormatDec(asValor)
   {
      var lsDecText;
      var lsIntText;
      var re = /(-?\d+)(\d{3})/
     
      lsIntText = asValor.substring(0,(asValor.length - 2));
      lsDecText = asValor.substring((asValor.length - 2),asValor.length);
		
	   while (re.test(lsIntText)) 
		{
			lsIntText = lsIntText.replace(re, "$1.$2");
		}
	
   	return lsIntText + "," + lsDecText ;  
   }
 
 function FormatInt(asValor)
   {
      var lsDecText;
      var lsIntText;
      var re = /(-?\d+)(\d{3})/
     
      lsIntText = asValor.substring(0,(asValor.length - 2));
      lsDecText = asValor.substring((asValor.length - 2),asValor.length);
		
	   while (re.test(lsIntText)) 
		{
			lsIntText = lsIntText.replace(re, "$1.$2");
		}
	
   	return lsIntText;  
   }
   
function StrToDec(lsValor)
   {
      lsValor = DesFormatDec(lsValor)
      
      if (isNaN(lsValor))
      {
         alert("Entrada Inválida.\nFavor preencher o campo com um valor decimal. Exemplo: 1.500,50");
			return "ERROR"
		}
      
      
		lsValor = FormatDec(lsValor);
		
		return lsValor;
   }

function StrToInt(lsValor)
   {
      lsValor = DesFormatDec(lsValor)
      
      if (isNaN(lsValor))
      {
         alert("Entrada Inválida.\nFavor preencher o campo com um valor inteiro. Exemplo: 1.500");
			return "ERROR"
		}
      
      
		lsValor = FormatInt(lsValor);
		
		return lsValor;
   }
   

function ValidaEntrada(abPermitiVazio,asTipo,aoItForm)
{
	var lsValor =  aoItForm.value;

	if ((abPermitiVazio == 0) && (IsEmpty(lsValor)))
	{
		alert("Favor preencher o campo, pois o preenchimento é obrigatório. ");
		aoItForm.focus();
		return false;
	}

       
	if (asTipo=="D")
   {
		lsValor = StrToDec(lsValor);
		if (lsValor=="ERROR")
		{   
			aoItForm.focus();
			aoItForm.value = "";		
			return false;
		}
		aoItForm.value = lsValor;
	}	
       
	if (asTipo=="I")
	{
		lsValor = StrToInt(lsValor);
		if (lsValor=="ERROR")
		{   
			aoItForm.focus();
			aoItForm.value = "";		
			return false;
		}
		aoItForm.value = lsValor;
	}	
       
       
     return true;
}


function fncOpenWindow(asURL,asNome,asProp) 
{ 
	var Janela
	Janela = window.open(asURL,asNome,asProp);
	Janela.focus();
}





function CpfOK(fNum)
{
	var d1;
	var d2;
	var Digitado;
	var Calculado;
	var N = new Array(12);
	var liTamStr;
	var liCntNum;
	
	liTamStr = parseInt(fNum.length,10);
	liCntNum = 0;
			
    for (var i = 0; i <= liTamStr; i++) 
	{
		if (!(isNaN(parseInt(fNum.substr(i,1),10))))
		{
			N[liCntNum+1] = parseInt(fNum.substr(i,1),10);
			liCntNum = liCntNum + 1;
		}
	} 
	
	if (liCntNum != 11)
	{
	    return false;
	}
	
	d1 = ((N[9]*2)+(N[8]*3)+(N[7]*4)+(N[6]*5)+(N[5]*6)+(N[4]*7)+(N[3]*8)+(N[2]*9)+(N[1]*10));
	d1 = (11-(d1 % 11));
	if (d1 >= 10)
	  d1 = 0;
	  
	d2 = ((d1*2)+(N[9]*3)+(N[8]*4)+(N[7]*5)+(N[6]*6)+(N[5]*7)+(N[4]*8)+(N[3]*9)+(N[2]*10)+(N[1]*11));
	d2 = (11-(d2 % 11));
  
	if (d2 >= 10)
	  d2 = 0;
	  
	Calculado = ("" + d1) +  d2;
	Digitado = fNum.substr(9,1) + fNum.substr(10,1);
	if (Calculado == Digitado)
	{
	   return true;
	}
	else
	{
	  return false;
	}
}

function BuscaCep()
{
  	var		s;
	var indAutoatend = "0";
	
	
	s = 'busca_cep.htm';
	
	
	window.open(s, "_blank", "left=80,height=370,width=400,resizable=yes,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no");
    
}


function CNPJOK(fNum)
{
	var d1;
	var d2;
	var Digitado;
	var Calculado;
	var N = new Array(14);
	
	
	
	liTamStr = parseInt(fNum.length,10);
	liCntNum = 0;
			
    for (var i = 0; i <= liTamStr; i++) 
	{
		if (!(isNaN(parseInt(fNum.substr(i,1),10))))
		{
			N[liCntNum+1] = parseInt(fNum.substr(i,1),10);
			liCntNum = liCntNum + 1;
		}
	} 
	
	if (liCntNum != 14)
	{
	    return false;
	} 
	
	d1 = ((N[12]*2)+(N[11]*3)+(N[10]*4)+(N[9]*5)+(N[8]*6)+(N[7]*7)+(N[6]*8)+(N[5]*9)+(N[4]*2)+(N[3]*3)+(N[2]*4)+(N[1]*5));
	d1 = (11-(d1 % 11));
  
	if (d1 >= 10) d1 = 0;
	d2 = ((d1*2)+(N[12]*3)+(N[11]*4)+(N[10]*5)+(N[9]*6)+(N[8]*7)+(N[7]*8)+(N[6]*9)+(N[5]*2)+(N[4]*3)+(N[3]*4)+(N[2]*5)+(N[1]*6));
	d2 = (11-(d2 % 11));
	if (d2 >= 10) d2 = 0; 
  
	Calculado = ("" + d1) +  d2;
	Digitado = fNum.substr(12,1) + fNum.substr(13,1);
	
  
	if (Calculado == Digitado)
		{
		   return true;
		}
		else
		{
		  return false;
		}
	
}

