
function mask_cpf(cpf,e){
	var key;
	var tempo;

	if (window.event){
   	key = window.event.keyCode;
		if ((cpf.length == 3)&&(key!=8))
		{
			cpf += '.';
		}
		if ((cpf.length == 7)&&(key!=8))
		{
			cpf += '.';
		}
		if ((cpf.length == 11)&&(key!=8))
		{
			cpf += '-';
		}
	}else{
	   if (e){
	      key = e.which;
			if(cpf.length==11)
		      tempo = cpf.substring(0,3)+'.'+cpf.substring(3,6)+'.'+cpf.substring(6,9)+'-'+cpf.substring(9,12);
			else
				tempo = '';
			cpf = tempo;
	   }
	}
	return(cpf);
}

function format_cep(valor){
		var numero  = '0123456789';
		var result  = '';
		var result1 = '';
		var zero = 'N';
		var i;
		var j;
		
		for (i=0; i < valor.length; i++){
				var temp1=valor.charAt(i);
				for (j=0; j < numero.length; j++){
						var temp2=numero.charAt(j);
					  if (temp1==temp2){
							 result = result + valor.charAt(i);
						}	
				}
		}
		while (result.charAt(0)==' '){
			result = result.substring(1, result.length);
		}
		var tamanho = result.length;
		if (tamanho>3){
				for (k=0; k < tamanho; k++){
						if (k==(tamanho-3)){
								result1 = result1 + '-';	 
								}
						if (((k==(tamanho-6))&&(tamanho!==6))||((k==(tamanho-9))&&(tamanho!==9))){
								result1 = result1 + '';	 
								}
						result1 = result1 + result.charAt(k);
						}
				}
		else{
				if (tamanho==2){
						result1 = ' ' + result;
						}
				else{
						result1 = ' ' + result;
						}
				}
		
		if (result1==''){
			result1=' ';
		}
		return(result1);
		}
		
function moeda(w_valor)
	{
	var numero  = '0123456789';
	var result  = '';
	var result1 = '';
	var zero = 'N';
	var i;
	var j;
	for (i=0; i < w_valor.length; i++)
		{
		var temp1=w_valor.charAt(i);
		for (j=0; j < numero.length; j++)
			{
			var temp2=numero.charAt(j);
			 if (temp1==temp2)
			 	{
				 result = result + w_valor.charAt(i);
				}	
			}
		}
	while (result.charAt(0)=='0')
		{
		result = result.substring(1, result.length);
		}
	var tamanho = result.length;
	if (tamanho>2)
		{
		for (k=0; k < tamanho; k++)
			{
			if (k==(tamanho-2))
				{
				result1 = result1 + ',';	 
				}
			if (((k==(tamanho-5))&&(tamanho!==5))||((k==(tamanho-8))&&(tamanho!==8)))
				{
				result1 = result1 + '.';	 
				}
			result1 = result1 + result.charAt(k);
			}
		}
	else
		{
		if (tamanho==2)
			{
			result1 = '0,' + result;
			}
		else
			{
			result1 = '0,0' + result;
			}
		}
	if (result1=='0,0')
		{
		result1='0,00';
		}
	return(result1);
	}
	
function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;

	if (window.event)
   	key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);

	if ((key==null) || (key==0) || (key==9) || (key==13) || (key==27) )
	   return true;
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}

function stringFilter (input) {
	s = input;
	filteredValues = ".-/";
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1) returnString += c;
	}
	return(returnString);
}

function mask_cpf(cpf,e){
	var key;
	var tempo;

	if (window.event){
   	key = window.event.keyCode;
		if ((cpf.length == 3)&&(key!=8))
		{
			cpf += '.';
		}
		if ((cpf.length == 7)&&(key!=8))
		{
			cpf += '.';
		}
		if ((cpf.length == 11)&&(key!=8))
		{
			cpf += '-';
		}
	}else{
	   if (e){
	      key = e.which;
			if(cpf.length==11)
		      tempo = cpf.substring(0,3)+'.'+cpf.substring(3,6)+'.'+cpf.substring(6,9)+'-'+cpf.substring(9,12);
			else
				tempo = '';
			cpf = tempo;
	   }
	}
	return(cpf);
}

function valida_cpf(pcpf)
{
pcpf = stringFilter(pcpf);
if (pcpf.length != 11) {sim=false} else {sim=true}

if (sim )  // valida o primeiro digito
{
	for (i=0;((i<=(pcpf.length-1))&& sim); i++)
   {
   	val = pcpf.charAt(i)
		if((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4")&&(val!="5")&&(val!="6")&&(val!="7")&&(val!="8"))
		{sim=false}
   }
   if (sim)
   {
		soma = 0
		for (i=0;i<=8;i++)
      {
			val = eval(pcpf.charAt(i))
			soma = soma + (val*(i+1))
		}
		resto = soma % 11
		if (resto>9){
			dig = resto -10
		}else{
			dig = resto
		}
      if (dig != eval(pcpf.charAt(9))) { sim=false }
      else   // valida o segundo digito
      {
			soma = 0
			for (i=0;i<=7;i++)
			{
				val = eval(pcpf.charAt(i+1))
				soma = soma + (val*(i+1))
			}
			soma = soma + (dig * 9)
			resto = soma % 11
			if (resto>9) dig = resto -10
			else  dig = resto
			if (dig != eval(pcpf.charAt(10))) { sim = false }
			else sim = true
		}
	}
}

if (sim == false)
	{
	alert("Número de CPF inválido");
	document.form.cpf.value = "000.000.000-00";
	return false;
	}
}
