// JavaScript Document

function deleta(pag,p1,v1,p2,v2)
{
	if (confirm("Deseja realmente apagar o registro?"))
	{
		window.location.href = pag + "?" + p1 + "=" + v1 +"&" + p2 + "=" + v2
	}
}
function deletaDestino(pag,p1,v1,p2,v2)
{
	if (confirm("Excluir este destino implica na exclusão de todas as suas referências. Deseja realmente apagar este destino?"))
	{
		window.location.href = pag + "?" + p1 + "=" + v1 +"&" + p2 + "=" + v2
	}
}	
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 NTecla(e)
{

	event.keyCode = 0;

}	


function validaCNPJ(w_value_str)
{
	txt_length = w_value_str
	if (txt_length.length == 2)
	{
		document.getElementById("txtCNPJ").value = w_value_str + "."
	}
	if (txt_length.length == 6)
	{
		document.getElementById("txtCNPJ").value = w_value_str + "."
	}	
	if (txt_length.length == 10)
	{
		document.getElementById("txtCNPJ").value = w_value_str + "/"
	}	
	if (txt_length.length == 15)
	{
		document.getElementById("txtCNPJ").value = w_value_str + "-"
	}			
	if (txt_length.length == 18)
	{		
		if (VerifyCNPJ(w_value_str) == 0)
		{
			alert("CNPJ inválido!!!")	
			document.getElementById("txtCNPJ").focus()
			return;
		}
	}
}







function VerifyCNPJ(CNPJ) 
{ 
 	CNPJ = LIMP(CNPJ); 
 	//alert(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 
     		{ 
     			return(0); 
     		} 
    		} 
   		else 
    		{ 
    			return(0); 
    		} 
   	} 
  } 
} 
function RealTestaCNPJ(CNPJ,g) 
{ 
 	var VerCNPJ=0; 
 	var ind=2; 
 	var tam; 
 	for(f=g;f>0;f--) 
  	{ 
  		VerCNPJ+=parseInt(CNPJ.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(CNPJ.charAt(g))) 
  	{ 
  		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 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); 
  	} 
} 