function novaJanela(pag,win,toolbar,location,directories,status,menubar,scrollbars,resizable,copyhistory,left,top,width,height)
{
	//novaJanela(pagina,nomeajanela,barradeferramentas,location,directories,status,menubar,scrollbars,resizable,copyhistory,left,top,width,height)
	//novaJanela(pagina,nomeajanela,'no','no','no','no','no','no','no','yes',200,200,300,300)
	var par = 'tollbar='+toolbar+',location='+location+',directores='+directories+',status='+status+',menubar='+menubar+',scrollbars='+scrollbars+',resizable='+resizable+',copyhistory='+copyhistory+',left='+left+',top='+top+',width='+width+',height='+height;
	window.open(pag, win, par);
}


function DigitarSoNumero()
{
	if(((event.keyCode>=0x30)&&(event.keyCode<=0x39))||(event.keyCode==8)||(event.keyCode==13)){
		return true;
	}
event.keyCode=0;
}

function selCidade(val)
{
	var x = val.selectedIndex;
	var url = val.options[x].value;
	if (url != "")
	{
		window.location.href=url;
	}
}



function validaCPF(CPF)
{
if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" || CPF == "22222222222" || CPF == "33333333333" || CPF == "44444444444" || CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" || CPF == "88888888888" || CPF == "99999999999")
return false;
soma = 0;
for (i=0; i < 9; i ++)
soma += parseInt(CPF.charAt(i)) * (10 - i);
resto = 11 - (soma % 11);
if (resto == 10 || resto == 11)
resto = 0;
if (resto != parseInt(CPF.charAt(9)))
return false;
soma = 0;
for (i = 0; i < 10; i ++)
soma += parseInt(CPF.charAt(i)) * (11 - i);
resto = 11 - (soma % 11);
if (resto == 10 || resto == 11)
resto = 0;
if (resto != parseInt(CPF.charAt(10)))
return false;
//alert('Correto');
return true;
}


function validaCNPJ(CNPJ)
{
//CNPJ = document.validacao.CNPJID.value;
erro = new String;
if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
}
//substituir os caracteres que nao sao numeros
if(document.layers && parseInt(navigator.appVersion) == 4){
x = CNPJ.substring(0,2);
x += CNPJ.substring(3,6);
x += CNPJ.substring(7,10);
x += CNPJ.substring(11,15);
x += CNPJ.substring(16,18);
CNPJ = x; 
}
else
{
CNPJ = CNPJ.replace(".","");
CNPJ = CNPJ.replace(".","");
CNPJ = CNPJ.replace("-","");
CNPJ = CNPJ.replace("/","");
}
var nonNumbers = /\D/;
if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n"; 
var a = [];
var b = new Number;
var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
for (i=0; i<12; i++){
a[i] = CNPJ.charAt(i);
b += a[i] * c[i+1];
}
if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
b = 0;
for (y=0; y<13; y++) {
b += (a[y] * c[y]); 
}
if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
erro +="Digito verificador com problema!";
}
if (erro.length > 0){
alert(erro);
return false;
} else {
//alert("CNPJ valido!");
}
return true;
}


function isNum( caractere ) 
{ 
         var strValidos = "0123456789" 
         if ( strValidos.indexOf( caractere ) == -1 ) 
                 return false; 
         return true; 
} 
function validaTecla(campo, event) 
{ 
         var BACKSPACE=  8; 
         var key; 
         var tecla; 


         CheckTAB=true; 
         if(navigator.appName.indexOf("Netscape")!= -1) 
                 tecla= event.which; 
         else 
                 tecla= event.keyCode; 


         key = String.fromCharCode( tecla); 
         //alert( \'key: \' + tecla + \'  -> campo: \' + campo.value); 


         if ( tecla == 13 ) 
                 return false; 
         if ( tecla == BACKSPACE ) 
                 return true; 
         return ( isNum(key)); 
} 
function FormataCNPJ( el ) 
{ 
         vr = el.value; 
         tam = vr.length; 


      if ( vr.indexOf(".") == -1 ) 
      { 
      if ( tam <= 2 ) 
              el.value = vr; 
      if ( (tam > 2) && (tam <= 6) ) 
              el.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam ); 
      if ( (tam >= 7) && (tam <= 10) ) 
              el.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/'; 
      if ( (tam >= 11) && (tam <= 18) ) 
             el.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) + '-' + vr.substr( 12, 2 ) ; 
      } 
      return true; 
} 



function soNumero(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 mascTEL(campo,event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	var total = campo.value.length;
	//38400-000
	//backspace=8
	if(keyCode!=8)
	{
		if( total==4 )
		{
			var m1 = campo.value.substr(0,4);
			var mfinal = m1+"-";
			campo.value = mfinal;
		}
	}
}


function CNPJ_CPF(campo)
{
	if( isNaN(campo.value) )
	{
		return true;
	}
	else
	{
		//CNPJ
		if(campo.value.length==14)
		{
			m_CNPJ(campo,14);
		}
		//CPF
		if(campo.value.length==11)
		{
			//043.125.452-85
			m_CPF(campo,11);
		}

	}
}


function escondeCNPJCPF()
{
	document.getElementById("divVALCNPJCPF").style.display='none';	
}

function validCNPJCPF(campo)
{
	var ajaxVALCNPJCPF = ajaxInit();
	if(ajaxVALCNPJCPF)
	{
		
		ajaxVALCNPJCPF.open("GET", "cnpjcpf.php?campo="+campo.value, true);
		ajaxVALCNPJCPF.onreadystatechange = function() 
		{
			if(ajaxVALCNPJCPF.readyState == 1)
			{
				document.getElementById("divVALCNPJCPF").style.display="block";
				document.getElementById("divVALCNPJCPF").innerHTML = "<img src='images/aguarde.gif' width='16' height='16'>&nbsp;Validando...";
			}
			if(ajaxVALCNPJCPF.readyState == 4)
			{
				if(ajaxVALCNPJCPF.status == 200)
				{
					if(document.getElementById(campo.name).value!="")
					{
						var ind = ajaxVALCNPJCPF.responseText;
						var expl = ind.split('|');
						//alert(ind);
						if(expl[0]=="0")
						{
							if(expl[1]=="cnpj")
							{
								document.getElementById("divVALCNPJCPF").innerHTML = "<br>CNPJ INV&Aacute;LIDO";
								setTimeout("escondeCNPJCPF()",2000);
								document.getElementById(campo.name).value="";
								document.getElementById(campo.name).style.background="#F8C560";
								document.getElementById(campo.name).focus();
							}
							else if(expl[1]=="cpf")
							{
								document.getElementById("divVALCNPJCPF").innerHTML = "<br>CPF INV&Aacute;LIDO";
								setTimeout("escondeCNPJCPF()",2000);
								document.getElementById(campo.name).value="";
								document.getElementById(campo.name).style.background="#F8C560";
								document.getElementById(campo.name).focus();
							}
							else
							{
								document.getElementById("divVALCNPJCPF").innerHTML = "CPF OU CNPJ INV&Aacute;LIDO";
								setTimeout("escondeCNPJCPF()",2000);
								document.getElementById(campo.name).value="";
								document.getElementById(campo.name).style.background="#F8C560";
							}

						}
						else
						{
							CNPJ_CPF(campo);
							document.getElementById("divVALCNPJCPF").innerHTML = "";
							document.getElementById("divVALCNPJCPF").style.display="none";
						}
					}
					else
					{
							document.getElementById("divVALCNPJCPF").innerHTML = "";
							document.getElementById("divVALCNPJCPF").style.display="none";
					}
				}
				else
				{
					alert(ajaxVALCNPJCPF.statusText);
				}
			}
		}
	ajaxVALCNPJCPF.send(null);
	}
}

//Busca CEP
function getCEP(campo, flag)
{
	if(campo!="")
	{
		var ajaxCEP = ajaxInit();
		if(ajaxCEP)
		{
			var str = "cep.php?acao=buscaCEP&campo="+campo+"&flag="+flag;
			ajaxCEP.open("GET", str, true);
			ajaxCEP.onreadystatechange = function() 
			{
				if(ajaxCEP.readyState == 1)
				{
					document.getElementById("msgCarCEP").style.display='block';
					document.getElementById("msgCarCEP").innerHTML = "<img src='images/tempo.gif' width='32' height='32'><br>Buscando CEP.";
				}
				if(ajaxCEP.readyState == 4)
				{
					if(ajaxCEP.status == 200)
					{
						document.getElementById("msgCarCEP").style.display='none';
						var strCEP = ajaxCEP.responseText;
						var str = strCEP.split(";");

						if(str[0]==1)
						{
							document.getElementById("cli_cidade").value = str[1];
							document.getElementById("cli_uf").value = str[2];
							document.getElementById("cli_endereco").value = "";
							document.getElementById("cli_bairro").value = "";
						}
						else if(str[0]==2)
						{
							document.getElementById("cli_endereco").value = str[1];
							document.getElementById("cli_uf").value = str[2];
							document.getElementById("cli_cidade").value = str[3];
							document.getElementById("cli_bairro").value = str[4];
						}
						else if(str[0]==3)
						{
							var strEnd = str[1].split(",");
							
							var clinum = strEnd[1].split(" ");
							var complem = strEnd[1].replace(clinum[1],"");
							
							document.getElementById("cli_endereco").value = strEnd[0];
							document.getElementById("cli_cidade").value = str[2];
							document.getElementById("cli_bairro").value = str[3];
							document.getElementById("cli_uf").value = str[4];
							if(clinum[1]!="")
							{
								document.getElementById("cli_numero").value = clinum[1];
							}
							document.getElementById("cli_complemento").value = complem;
						}
						else
						{
							alert('CEP nao encontrado.');
						}

					}
					else
					{
						alert(ajaxCEP.statusText);
					}
				}
			}
		ajaxCEP.send(null);
		}
	}//fim if
}



function mascCEP(campo,event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	var total = campo.value.length;
	//38400-000
	//backspace=8
	if(keyCode!=8)
	{
		if( total==5 )
		{
			var m1 = campo.value.substr(0,5);
			var mfinal = m1+"-";
			campo.value = mfinal;
		}
	}
}



var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e)
{
var keyCode = (isNN) ? e.which : e.keyCode;
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode))
{
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input)
{
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}
