
var nVezes = 0;

function ValidaCpf(source, arguments){	
   var lsCPFAux;
   var liCont;
   var liCont2;
   var liSoma;
   var liDigito;
   var lsControle = arguments.Value;
   
   //Manutenção abaixo para adicionar os zeros a esquerda

   var VintResultado;
   var VintTamCampo = lsControle.length;
   VintResultado = 11 - VintTamCampo;

   if(VintResultado > 0 && lsControle != "")
   {
		for(VintControle = 0; VintControle < VintResultado; VintControle++)
		{
			lsControle = "0" + lsControle;
		}		
   }
   //fim da manutencao

   lsCPFAux = lsControle.substring(0, 9);
   liSoma = 0

 if (lsControle == "")
 {
	arguments.IsValid = true;
	return true;
 }
 var car = lsCPFAux.charAt(0);
 var flag = true;
	for (i=1; i< lsCPFAux.length; i++)
    {
		if (car != lsCPFAux.charAt(i))
		{
			flag = false;
			break;
		}
    }
    if (flag == true)
    {
		arguments.IsValid = false;
        return (false);
    }

 var re = /^(\s*)(\d+)(\s*)$/;
 if (re.test(lsControle) == false)
 {  
  arguments.IsValid = false;
  return false;
 }


   for (liCont2 = 1; liCont2 <= 2; liCont2++)
   {
       for (liCont = lsCPFAux.length; liCont >= 1; liCont--)
     {
           liSoma = liSoma + ((lsCPFAux.length + 2 - liCont) *
    (eval(lsCPFAux.substring(liCont-1, liCont))));
     }
       liDigito = 11 - (liSoma % 11);
       if (liDigito >= 10)
       {
           liDigito = 0;
       }

       lsCPFAux = lsCPFAux + liDigito;

       liSoma = 0;
   }

   if (lsControle == lsCPFAux)
   {       
       arguments.IsValid = true;
       return true;
   }
   else
   {		
		arguments.IsValid = false;
		return false;
   }	
	arguments.IsValid = false;
	return false;
}


function ValidaCnpj(source, arguments){	
    var lsCGC;
    var lsCGCAux;
    var liCont;
    var liDigito;
    var liSoma;
    var lsControle = arguments.Value;

   //Manutenção abaixo para adicionar os zeros a esquerda

   var VintResultado;
   var VintTamCampo = lsControle.length;
   VintResultado = 14 - VintTamCampo;

   if(VintResultado > 0 && lsControle != "")
   {
		for(VintControle = 0; VintControle < VintResultado; VintControle++)
		{
			lsControle = "0" + lsControle;
		}
		arguments.Value = lsControle;
   }
   //fim da manutencao



 if (lsControle == "")
 {  
  arguments.IsValid = true;
  return true;
 }

 var re = /^(\s*)(\d+)(\s*)$/;
 if (re.test(lsControle) == false)
 {
  arguments.IsValid = false;
  return false;
 }

   if (lsControle.length < 12)
   {	  
		arguments.IsValid = false;
		return false;
   }


   lsCGCAux = lsControle.substring(0,12);

   liSoma = 0;

   for (liCont = 2; liCont <= 9; liCont++)
   {
       liSoma = liSoma + (liCont * eval(lsCGCAux.substring(13 - liCont, 14-liCont)));
   }

   for (liCont = 2; liCont <= 5; liCont++)
   {
       liSoma = liSoma + (liCont * eval(lsCGCAux.substring(5 - liCont, 6 - liCont)));
   }

   liDigito = 11 - (liSoma % 11);

   if (liDigito >= 10)
   {
       liDigito = 0;
   }

   lsCGCAux = lsCGCAux + liDigito;


   liSoma = 0;

   for (liCont = 2; liCont <= 9; liCont++)
   {
       liSoma = liSoma + liCont * eval(lsCGCAux.substring(14 - liCont, 15 - liCont));
   }

   for (liCont = 2; liCont <= 6; liCont++)
   {
       liSoma = liSoma + (liCont) * eval(lsCGCAux.substring(6 - liCont, 7 - liCont));
   }

   liDigito = 11 - (liSoma % 11);

   if (liDigito >= 10)
   {
       liDigito = 0;
   }

   lsCGCAux = lsCGCAux + liDigito;

   liSoma = 0;

   if (lsControle == lsCGCAux)
   {	  
	  arguments.IsValid = true;
	  return true;
   }
   else
   {  
	  arguments.IsValid = false;
      return false;
   }
 }
 
 function ChecarBrowser()
{
		this.ver=navigator.appVersion;
		this.dom=document.getElementById?1:0;
		this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
		this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
		this.ie4=(document.all && !this.dom)?1:0;
		this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
		this.ns4=(document.layers && !this.dom)?1:0;
		this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5);
}

var VerifiqueTAB=true;
function Mostra(quem, tammax)
	{		
	var PobjBr = new ChecarBrowser();	
	if(PobjBr.ie5 == 1 || PobjBr.ie4 == 1 || PobjBr.ie6 == 1)
	{

		if ((quem.value.length == tammax) && (VerifiqueTAB) ) { 
			var i=0,j=0, indice=-1;
			for (i=0; i<document.forms.length; i++) { 
			for (j=0; j<document.forms[i].elements.length; j++) { 
				VobjElementos = document.forms[i].elements[j].name
				//alert(VobjElementos)
			    if (document.forms[i].elements[j].name == quem.name) { 
				    indice=i;
				    break;
			    } 
			} 
			if (indice != -1) break; 
		} 
		for (i=0; i<=document.forms[indice].elements.length; i++) { 
			if (document.forms[indice].elements[i].name == quem.name) { 
				while ( (i < document.forms[indice].elements.length) && 
					(document.forms[indice].elements[(i+1)] != null) &&
					(document.forms[indice].elements[(i+1)].type == "hidden")) { 
					i++;
				}
				if (document.forms[indice].elements[(i+1)] == null) { VerifiqueTAB=false; return; }
				document.forms[indice].elements[(i+1)].focus();
				VerifiqueTAB=false;
				break;
				} 
			} 
		} 
	}
} 

function PararTAB(quem) { VerifiqueTAB=false; } 
function ChecarTAB() { VerifiqueTAB=true; } 

var dia, mes, ano; // Variáveis globais para validações de datas

function TenteSubmeter(bp){
	/*****************************************************************************
	Colocar no Form {' onkeypress="TenteSubmeter('ID_Client_Botao_Principal');" '}
	*****************************************************************************/
	try {
		TesteSubmit(bp);
	}
	catch(e){} //não faz nada 		
}

// Browser detect code
/* Esta função deverá ser escrita em todas as páginas com a  */
var focustextarea = false;
function TesteSubmit(bp){
/*****************************************************************************
Quando for escrever uma textarea, colocar:
onblur=javascript:focustextarea=false; onfocus=javascript:focustextarea=true; 
, pois a textarea deve aceitar a quebra de linha
*******************************************************************************/
	var ie4=document.all;
	var ns4=document.layers;
	var ns6=document.getElementById&&!document.all;

	if (ie4){ //codigo para IE
		if (event.keyCode == 13 && (!focustextarea)){ 
		// ao pressionar ENTER fora de textarea, realizar a principal atividade do formulário	
			var x = document.getElementById (bp);
			x.click();
		}
	}else if (ns4 || ns6) //codigo para netscape
		if (event.which == 13 && (!focustextarea)){				
			//document.forms[0].submit();
			var x = document.getElementById (bp);
			x.click();
		}		
}

function ValidaNumeroDig(e)
{		
	// Reseta Variável usada na validação de qtd
	nVezes = 0;

	var re = /^(\s*)(\d+)(\s*)$/;	
	var PobjBr = new ChecarBrowser();	
	
	if(PobjBr.ie6 == 1 || PobjBr.ie5 == 1 || PobjBr.ie4 == 1)
	{
		var valor = String.fromCharCode(e.keyCode);					
	}
	else
	{
		if(e.which == 8) //aceita o backspace
		{
			return true;
		}
		var valor = String.fromCharCode(e.which);			
	}

	if (re.test(valor) == false && valor != "")
	{
		return false;
	}
	else
	{
		ChecarTAB();
		return true;
	}		
}


function arredonda(PstrValor)
{
	var VstrValor;
	var VarrValor;
	var VintDecimal;
	var VbVerificar;	
	var VstrArredonda;
	var PbSoma1 = false;
	VstrValor = PstrValor;
	VarrValor = VstrValor.split(",");
	
	if(PstrValor == ''){return ''}
	
	if(isNaN(Number(VarrValor[1])) == true)
	{
		VstrArredonda = "00";
	}
	else
	{
		VintDecimal = VarrValor[1];
		if(VintDecimal.length == 1)
		{
			VstrArredonda = VintDecimal + "0";
		}
		else
		{
			if(VintDecimal.length >= 3)
			{
				VstrArredonda = VintDecimal.substring(2,3);
				NArredonda = Number(VstrArredonda);
				if(NArredonda > 5)
				{
					VstrArredonda = VintDecimal.substring(0,2);
					var NArredonda;
					NArredonda = new Number(VstrArredonda);
					NArredonda = NArredonda + 1;
					if(NArredonda == 100)
					{
						PbSoma1 = true;
					}
					VstrArredonda = String(NArredonda);
					if(VstrArredonda.length == 1)
					{
						VstrArredonda = "0" + VstrArredonda;
					}

				}
				else
				{
					VstrArredonda = VintDecimal.substring(0,2);
				}
			}
			else
			{
				
				VstrArredonda = VintDecimal.substring(0,2);
			}
		}
	}
	var VstrFinal;
	if(PbSoma1 == true)
	{
		var PintSoma;
		PintSoma = new Number(VarrValor[0]);
		PintSoma = PintSoma + 1;
		VstrArredonda = "00";
		VstrFinal = String(PintSoma) + "," + String(VstrArredonda);
	}
	else
	{
		VstrFinal = String(VarrValor[0]) + "," + String(VstrArredonda);
	}
	
	return VstrFinal;
}


function TrocaVirgula(valor){
	var i,ch;
	ch = "";

	// Se está no formato moeda
	if (valor.substring(0,2) == "R$")
	{
		valor = valor.substring(3);

		for (i=0;i < valor.length;i++)
		{
			// retira os pontos do formato moeda
			if (valor.substring(i,i+1) != ".")
				ch = ch + valor.substring(i,i+1);  
		}
		
		valor = ch;
		ch = "";
	}
		
	if (valor.length != 0)
	{
		for (i=0;i < valor.length;i++)
		{
			if (valor.substring(i,i+1) == ",")
				ch = ch + ".";
			else
				ch = ch + valor.substring(i,i+1);   
		}
	}
	
	return ch;
		
}

function TrocaPonto(valor){
	var i,ch;
	ch = "";

	if (valor.length != 0)
	for (i=0;i < valor.length;i++){
		if (valor.substring(i,i+1) == ".")
		   ch = ch + ",";
		else
		   ch = ch + valor.substring(i,i+1);   
	}
	
	return ch;
}

function TravaText(PobjCampo)
{
	var focou = false;
	for(i=0;i <= document.forms[0].elements.length-1;i++)
	{
		//alert(document.forms[0].elements[i].name);
		if(document.forms[0].elements[i].name == PobjCampo.name)
		{
			while(i < document.forms[0].elements.length-1)
			{
				i++;
				if(document.forms[0].elements[i].type != "hidden")
				{
					focou = true;
					document.forms[0].elements[i].focus();
					break;
				}
			}
		}
	}
	if(focou == true)
	{
		return true;
	}
	else
	{
		i = 0;
		while(i < document.forms[0].elements.length-1)
		{
			if(document.forms[0].elements[i].type != "hidden")
			{
				document.forms[0].elements[i].focus();
				break;
			}
			i++;
		}
		return true;
	}
}

function HabilitaData(campo, c)
{
	eval("document.forms [0]."+ campo +"_dia.disabled = !c;");
	eval("document.forms [0]."+ campo +"_mes.disabled = !c;");
	eval("document.forms [0]."+ campo +"_ano.disabled = !c;");
	AtivaDesativaValidacao(campo, c);
}

function AtivaDesativaValidacao(sId, ativar)
{
	var el;
	
	if (ativar)
	{
		el = document.all[sId + "_val_re_ano"];
		Page_Validators = PushArray(Page_Validators,el)
		el.setAttribute("enabled", "True");

		el = document.all[sId + "_val_range_dia"];
		Page_Validators = PushArray(Page_Validators,el)
		el.setAttribute("enabled", "True");

		el = document.all[sId + "_val_range_mes"];
		Page_Validators = PushArray(Page_Validators,el)
		el.setAttribute("enabled", "True");

		el = document.all[sId + "_val_range_ano"];
		Page_Validators = PushArray(Page_Validators,el)
		el.setAttribute("enabled", "True");

		el = document.all[sId + "_val_data"];
		Page_Validators = PushArray(Page_Validators,el)
		el.setAttribute("enabled", "True");
	}
	else
	{
		var aux = new Array();
		el1 = document.all[sId + "_val_re_ano"];
		el1.setAttribute("enabled", "False");
		el1.style.display = "none";
		el2 = document.all[sId + "_val_range_dia"];
		el2.setAttribute("enabled", "False");
		el2.style.display = "none";
		el3 = document.all[sId + "_val_range_mes"];
		el3.setAttribute("enabled", "False");
		el3.style.display = "none";
		el4 = document.all[sId + "_val_range_ano"];
		el4.setAttribute("enabled", "False");
		el4.style.display = "none";
		el5 = document.all[sId + "_val_data"];
		el5.setAttribute("enabled", "False");
		el5.style.display = "none";
		
		for (i=0; i< Page_Validators.length; i++)
		{
			if (Page_Validators[i] != el1 && 
				Page_Validators[i] != el2 &&
				Page_Validators[i] != el3 &&
				Page_Validators[i] != el4 &&
				Page_Validators[i] != el5)
			{
				aux = PushArray(aux, Page_Validators[i]);
			}
		}
		Page_Validators = aux;
	}
}

function PushArray(array, newElem)
{
	arrayTemp = new Array(array.length + 1);
	for (i=0; i < array.length; i++)
	{
		arrayTemp[i] = array[i];
	}
	
	arrayTemp[arrayTemp.length - 1] = newElem;
	
	return arrayTemp;
}

function PopArray(array)
{
	arrayTemp = new Array(array.length - 1);
	for (i=0; i < array.length - 1; i++)
	{
		arrayTemp[i] = array[i];
	}
	
	return arrayTemp;
}

