// Cookie
function defineCookie(nome,valor,dias)
{
	if (dias) {
		var date = new Date();
		date.setTime(date.getTime()+(dias*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = nome+"="+valor+expires+"; path=/";
}

// Administrações
function confirma_excluir(id,modulo)
{
	var jsExclui = confirm("Deseja excluir o registro?");
	if (jsExclui == true)
	{
		location.href="index.php?pagina=admin&adm="+modulo+"&excluir=sim&id="+id;
	}
}

function confirma_excluir1(id,modulo)
{
	var jsExclui = confirm("Deseja excluir o registro?");
	if (jsExclui == true)
	{
		location.href="index.php?pagina="+modulo+"&excluir=sim&id="+id;
	}
}

// Enquetes
function verifica_radio (obj)
{
	for (i=0; i < obj.length; i++)
		{
	if (obj[i].checked)
		return true;
	}
	return false;	
}

function envia_enquete(frm)
{
	if  (!verifica_radio(frm.op)) 
	{
		alert ('Você deve selecionar uma opção');
		return false; 
	}
	return true;
}

// Abrir PopUp
function popup(pasta, arquivo, largura, altura, mostrascroll, l, t)
{
	if (mostrascroll == '') mostrascroll='no';
	if (l==0 && l=='') l=0;
	if (t==0 && t=='') t=0;
	window.open(pasta+arquivo,"","resizable=no,toolbar=no,status=no,menubar=no,scrollbars="+mostrascroll+",width="+largura+",height="+altura+", left="+l+", top="+t);
}

function abrejanela(url)
{
	window.open(url);
}

function abrevideo(id)
{
	window.open("paginas/video.php?id="+id,"","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=no,width=362,height=317,left=0,top=0")
}

function abrefoto(idFoto)
{
	window.open("paginas/fotoview.php?id="+idFoto,"","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=yes,width=400,height=400,left=0,top=0")
}

function abregaleria(idGaleria)
{
	window.open("paginas/fotos_galeria.php?id="+idGaleria,"","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=auto,width=700,height=500,left=0,top=0")
}

// Validação de entradas - numericos, letras e alfanuméricos

/*
Adicionar ao txtbox
onkeypress="return validaTecla('an',this, event);"
*/
function isNum(caractere) 
{ 
	var strValidos = "0123456789";
	if (strValidos.indexOf(caractere) == -1 ) return false; 
	return true;
} 

function isAlfa(caractere) 
{ 
	var strValidos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if (strValidos.indexOf(caractere) == -1 ) return false; 
	return true;
} 

function isAlfaNum(caractere) 
{ 
	var strValidos = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if (strValidos.indexOf(caractere ) == -1 ) return false; 
	return true;
} 

function valida_tecla(tipo,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); 
  
	if (tecla == 13) return false; 
	
	if (tecla == BACKSPACE) return true;

	if (tipo == 'n') return (isNum(key));
	if (tipo == 'a') return (isAlfa(key));
	if (tipo == 'an') return (isAlfaNum(key));
}

// Validação de e-mail
function valida_email(campo)
{
}

// Form
function valida_txtbox(obj)
{
   	if (obj.value.length == 0)
	{
   		return false;
	}
   	else 
	{
   		return true;
   	}
}

// Limite de caracteres

/**
Colcar no Textbox os seguintes parâmetros:
onkeydown="limite_char(this)" onblur="limite_char(this)" onkeyup="limite_char(this)"
e para mostrar o contador, é só criar um objeto com o ID = ch_restante
*/
var strErro="Você ultrapassou o limite de caracteres.";
function limite_char(obj, limite)
{
	intLng=obj.value.length;
	if(intLng>limite)
	{
		obj.value=obj.value.substring(0,limite)
	}
	else
	{
		document.getElementById('ch_restante').innerHTML=(limite-obj.value.length);
	}
}

// Validação de e-mails
function valida_email(obj) {
	prim = obj.value.indexOf("@")
	if(prim < 2 || obj.value.indexOf("@",prim + 1) != -1 || obj.value.indexOf(".") < 1 || obj.value.indexOf(" ") != -1 || obj.value.indexOf(".@") > 0 || obj.value.indexOf("@.") > 0 || obj.value.indexOf(".com.br.") > 0 || obj.value.indexOf("/") > 0 || obj.value.indexOf("[") > 0 || obj.value.indexOf("]") > 0 || obj.value.indexOf("(") > 0 || obj.value.indexOf(")") > 0 || obj.value.indexOf("..") > 0) {
		alert("Formato de e-mail inválido.");
		return false;
	}
	return true;
}
//********************************************************
/*
Verificação de CIC / CGC
Devem ser passados como parametros:
campo = valor a ser pesquisado
empresa = se true, verifica CGC, senao CPF
*/
function SoNumero(v)
{
	var x, ret;
	ret = '';
	for ( x=0; x<=v.length; x++ )
		ret += v.substr( x, 1 ) >= '0' && v.substr( x, 1 ) <= '9' ? v.substr( x, 1 ) : '';
	return ( ret );
}

// Transforma o CGC para a rotina corretamente, os numeros devem ser filtrados antes
function TransCGC(vlr)
{
	var x, y, z;
	x = vlr.substr( 0, 8 ); y = vlr.substr( 8, 4 ); z = vlr.substr( 12, 2 );
	return ( x + '/' + y + '-' + z );
}

//ps: campo é a string com o cpf/cnpj
function validaCpfCgc(campo)
{
	if (campo == "")
	{
			return false ;
	}
	var intNumero, intMais, i, intResto, strCampo, strCaracter, strConf;
	var intDig1, intDig2, dblDivisao, rt, intSoma, intSoma1, intSoma2, intInteiro;
	var lngSoma, lngInteiro, strCGC; // somente para o caso de CGC

	vlr = SoNumero(campo);

	if ( vlr.length == 14 ) // Pessoa jurídica
	{
		vlr = TransCGC(vlr);
		intSoma = 0;
		intSoma1 = 0;
		intSoma2 = 0;
		intNumero = 0;
		intMais = 0;
		strCGC = vlr.substr( vlr.length-7, 4 );
		strCampo = vlr.substr( 4, 4 );
		strCampo += strCGC;

		for ( i=1; i<9; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma1 += intMais;
		}
		strCampo = vlr.substr( 0, 4 );
		for ( i=1; i<5; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma2 += intMais;
		}
		intSoma = intSoma1 + intSoma2;
		dblDivisao = intSoma / 11;
		intInteiro = parseInt( dblDivisao ) * 11;
		intResto = intSoma - intInteiro;
		intDig1 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;

		intSoma = 0;
		intSoma1 = 0;
		intSoma2 = 0;
		intNumero = 0;
		intMais = 0;
		strCGC = vlr.substr( vlr.length-7, 4 );
		strCampo = vlr.substr( 5, 3 );
		strCampo += ( strCGC + intDig1 );

		for ( i=1; i<9; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma1 += intMais;
		}
		strCampo = vlr.substr( 0, 5 );
		for ( i=1; i<6; i++ )
		{
			strCaracter = strCampo.substr( strCampo.length - i );
			intNumero = strCaracter.substr( 0, 1 );
			intMais = intNumero * ( i+1 );
			intSoma2 += intMais;
		}
		intSoma = intSoma1 + intSoma2;
		dblDivisao = intSoma / 11;
		intInteiro = parseInt( dblDivisao ) * 11;
		intResto = intSoma - intInteiro;
		intDig2 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;

		strConf = intDig1.toString() + intDig2.toString();
		rt = ( strConf == vlr.substr( vlr.length - 2 ) );

		if (! rt ) 
		{
				//alert("Verifique o CNPJ digitado!");
				//alert(campo);
				return false;
		}
		
	}
	else // Pessoa física
	{
		if ( vlr.length == 11 ) // Pessoa física
		{
			lngSoma = 0;
			intNumero = 0;
			intMais = 0;
			strCampo = vlr.substr( 0, 9 );

			for ( i=1; i<10; i++ )
			{
				strCaracter = strCampo.substr( strCampo.length - i );
				intNumero = parseInt( strCaracter.substr( 0, 1 ) );
				intMais = intNumero * ( i+1 );
				lngSoma += intMais;
			}
			dblDivisao = lngSoma / 11;
			lngInteiro = parseInt( dblDivisao ) * 11;
			intResto = lngSoma - lngInteiro;
			intDig1 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;
			strCampo += intDig1;
			lngSoma = 0;
			intNumero = 0;
			intMais = 0;
			for ( i=1; i<11; i++ )
			{
				strCaracter = strCampo.substr( strCampo.length - i );
				intNumero = parseInt( strCaracter.substr( 0, 1 ) );
				intMais = intNumero * ( i+1 );
				lngSoma += intMais;
			}
			dblDivisao = lngSoma / 11;
			lngInteiro = parseInt( dblDivisao ) * 11;
			intResto = lngSoma - lngInteiro;
			intDig2 = ( intResto == 0 || intResto == 1 ) ? 0 : 11 - intResto;
			strCampo += intDig2;
			strConf = intDig1.toString() + intDig2.toString();

			rt = ( strConf == vlr.substr( vlr.length - 2 ) );

			if (! rt ) 
			{
				//alert("Verifique o CPF digitado!");
				return false;
			}
	}
	else // quantidade de digitos errada cpf->11 e cgc->14 
	{
		if ( campo != "" )
		{
				//alert("Verifique o CPF/CGC digitado!");
				return false;
		}
	}
	}
	return true;		
}	
//********************************************************
//********************************************************

function validaCPF(form) {

	if (!validaCpfCgc(form.value))
		return false;	
	else
		return true;
}