
//VALIDA CPF
function ValidaCPF(s)
{   
	var i;
		
	for(var j=1;j < s.length;j++)
		s = s.replace('-','');
	for(var j=1;j < s.length;j++)
		s = s.replace('/','');
	for(var j=1;j < s.length;j++)
		s = s.replace('.','');		
	if (s.length != 11)
		return false;
		
	if (s ==	'00000000000')
		return false;
	if (s ==	'11111111111')
		return false;
	if (s ==	'22222222222')
		return false;	
	if (s ==	'33333333333')
		return false;
	if (s ==	'44444444444')
		return false;
	if (s ==	'55555555555')
		return false;
	if (s ==	'66666666666')
		return false;
	if (s ==	'77777777777')
		return false;
	if (s ==	'88888888888')
		return false;
	if (s ==	'99999999999')
		return false;		
						
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;   
	
	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(10-i);
	}
	
	if (d1 == 0)
	{
		return false;
	}
	
	d1 = 11 - (d1 % 11);
	
	if (d1 > 9) d1 = 0;
	
	if (dv.charAt(0) != d1)
	{
		return false;
	}
	d1 *= 2;
	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
	return true;
}
	//VALIDA E-MAIL
	function ValidaMail(mail)
	{		
		var	re = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		
		if (mail.match(re))
			return true;	
		return false;	
	}
	//TIRA ESPACOS DO COMECO E FIM DAS STRING
	function Trim(Str)
	{
		while(Str.charAt(0) == ' ')
			Str = Str.substr(1);
		while(Str.charAt(Str.length-1) == ' ')
			Str = Str.substr(0,Str.length-1);
		return Str;	
	}
	
	//VALIDA CAMPOS MONETARIOS
	function ValidaValor(e,valor,nome)
	{
		if ( navigator.appName == "Netscape" ) {
			var tecla = e.charCode;
		} else {
			var tecla = e.keyCode;
		}
		
		if ( tecla != 13 && tecla != 0 ) {
			if (valor.indexOf(',') > 0 && tecla == 44) {
				return false;
			}
			if (tecla > 47 && tecla < 58 ) {
				return true;
			}
			if (tecla == 44 || tecla == 13 ) {
				return true;	
			}
			return false;
		} else {
			return true;
		}
	}
	
	//VALIDA CAMPOS NUMERICOS
	function ValidaInteiro(e,valor,nome)
	{	
		if ( navigator.appName == "Netscape" ) {
			var tecla = e.charCode;
		} else {
			var tecla = e.keyCode;
		}
		
		if(tecla > 47 && tecla < 58 ){
			return true;
		}
		if(tecla == 13){
			return true;	
		}
		if(tecla == 0){
			return true;	
		}
		
		return false;
			
	}
	//FORMATA CPF
	function FormataCPF(e,valor,nome)
	{
		var tecla,Str;
		if (valor.length == 3){	Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "." ;';eval(Str);}
		if (valor.length == 7){	Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "." ;';eval(Str);}
		if (valor.length == 11){Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "-" ;';eval(Str);}
	
		if(document.all)
			tecla = event.keyCode;
		else
			return true;
		if(tecla > 47 && tecla < 58 )
			return true;
		if(tecla == 13 )
			return true;
		
		return false;
	}
	//FORMATA TELEFONE
	function FormataTelefone(e,valor,nome)
	{
		var tecla,Str;
		if (valor.length == 4){	Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "-" ;';eval(Str);}
			
		if(document.all)tecla = event.keyCode;
		else return true;
		if(tecla > 47 && tecla < 58 )	return true;
		if(tecla == 13 )return true;	
		return false;
	}
	//VALIDA CNPJ
	function ValidaCnpj(cnpj)
	{
		for(var j=1;j < cnpj.length;j++)
			cnpj = cnpj.replace('-','');
		for(var j=1;j < cnpj.length;j++)
			cnpj = cnpj.replace('/','');
		for(var j=1;j < cnpj.length;j++)
			cnpj = cnpj.replace('.','');		
			
		var erro =  true;
		var  aux_cnpj = "";
		var  cnpj1=0,cnpj2=0;
		for(j=0;j<cnpj.length;j++)
		if(cnpj.substr(j,1)>="0"  && cnpj.substr(j,1)<="9")
			aux_cnpj +=  cnpj.substr(j,1);
		if(aux_cnpj.length!=14)
			erro  = false;
		else {
			cnpj1  = aux_cnpj.substr(0,12);
			cnpj2  =  aux_cnpj.substr(aux_cnpj.length-2,2);
			fator = "543298765432";
			controle  =  "";
			for(j=0;j<2;j++) {
				soma = 0;
				for(i=0;i<12;i++)
					soma  +=  cnpj1.substr(i,1) * fator.substr(i,1);
				if(j==1) soma +=  digito * 2;
				digito  = (soma * 10) % 11;
				if(digito==10) digito  = 0;
				controle +=  digito;
				fator = "654329876543";
			}
			if(controle!=cnpj2)
				erro  =  false;
		}
		return erro;
	}
	
	//FORMATA CNPJ
	function FormataCNPJ(e,valor,nome)
	{
		var tecla,Str;
		if (valor.length == 2){	Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "." ;';eval(Str);}
		if (valor.length == 6){	Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "." ;';eval(Str);}
		if (valor.length == 10){Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "-" ;';eval(Str);}
		if (valor.length == 15){Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "/" ;';eval(Str);}
			
		if(document.all)tecla = event.keyCode;
		else return true;
		if(tecla > 47 && tecla < 58 )	return true;
		if(tecla == 13 )return true;	
		return false;
	}
	//FORMATA CEP
	function FormataCEP(e,valor,nome)
	{
		var tecla,Str;
		if (valor.length == 5){	Str='document.frmForm.'+nome+'.value = document.frmForm.'+nome+'.value + "-" ;';eval(Str);}
			
		if(document.all)tecla = event.keyCode;
		else return true;
		if(tecla > 47 && tecla < 58 )	return true;
		if(tecla == 13 )return true;	
		return false;
	}
	
	
	function AbreCPF()
	{
		window.open('cpf.asp','','resizable=no,top=yes,menubar=no,status=no,location=no,toolbar=no,scrollbars=no,width=500,height=150');
	}
	
	function AbreCEP()
	{
		window.open('busca_cep.htm','','resizable=no,top=yes,menubar=no,status=no,location=no,toolbar=no,scrollbars=no,width=358,height=362');
	}
	
	function ValidaBlog(blog)
	{		
		var blogValido;
		var re;
		blogValido = false;		
		
		re = /^https?:\/\/([\w-]+\.)+([\w-]+)+(\/[\w- ./?%&#=]*)?$/;
		
		if (blog.match(re))
			blogValido = true;		
		
		return blogValido;
	}
	
	function corrigeBlog(blog)
	{
		blog = Trim(blog);
		var re;		
		
		re = /^https?:\/\/.*/;
				
		if (!blog.match(re))
			return "http://" + blog;
		else
			return blog;	
	}