function validacpf(){
	var i;
	s = document.getElementsByName('CPF')[0].value;
	
	if(s != ''){
		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){
		 	alert("CPF Inv\u00e1lido")
	 		return false;
		}
	 
		d1 = 11 - (d1 % 11);
		
		if (d1 > 9) d1 = 0;
		
		if (dv.charAt(0) != d1){
			alert("CPF Inv\u00e1lido")
			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){
			alert("CPF Inv\u00e1lido")
	 		return false;
		}
	 
		return true;
	}
	return true;
	
} 

	function somente_numero(campo){
		var digits="0123456789,"
		var campo_temp
		for (var i=0;i<campo.value.length;i++){
			campo_temp=campo.value.substring(i,i+1)
			if (digits.indexOf(campo_temp)==-1){
				campo.value = campo.value.substring(0,i);
				break;
			}
		}
	}

	function somente_numero_e_barra(campo){
		var digits="0123456789/"
		var campo_temp
		for (var i=0;i<campo.value.length;i++){
			campo_temp=campo.value.substring(i,i+1)
			if (digits.indexOf(campo_temp)==-1){
				campo.value = campo.value.substring(0,i);
				break;
			}
		}
	}
	
	function somente_numero_sem_ponto(campo){
		var digits="0123456789"
		var campo_temp
		for (var i=0;i<campo.value.length;i++){
			campo_temp=campo.value.substring(i,i+1)
			if (digits.indexOf(campo_temp)==-1){
				campo.value = campo.value.substring(0,i);
				break;
			}
		}
	}		
	
	function validacnpj(  ) {
		c = document.getElementsByName('CNPJ')[0].value;

		if(c != ''){
		    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj = c.replace(/\D+/g, '');
		    digitos_iguais = 1;
		    if (cnpj.length != 14)
		            {
		                 alert('CNPJ Inv\u00e1lido');
		                 //c.focus();
		                 return false;
		                 }
		 
		     for (i = 0; i < cnpj.length - 1; i++)
		           if (cnpj.charAt(i) != cnpj.charAt(i + 1))
		                 {
		                 digitos_iguais = 0;
		                 break;
		                 }
		     if (!digitos_iguais)
		           {
		           tamanho = cnpj.length - 2
		           numeros = cnpj.substring(0,tamanho);
		           digitos = cnpj.substring(tamanho);
		           soma = 0;
		           pos = tamanho - 7;
		           for (i = tamanho; i >= 1; i--)
		                 {
		                 soma += numeros.charAt(tamanho - i) * pos--;
		                 if (pos < 2)
		                       pos = 9;
		                 }
		           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		           if (resultado != digitos.charAt(0))
		{
		                 alert('CNPJ Inv\u00e1lido');
		                 c.focus();
		                 return false;
		                 }
		 
		           tamanho = tamanho + 1;
		           numeros = cnpj.substring(0,tamanho);
		           soma = 0;
		           pos = tamanho - 7;
		           for (i = tamanho; i >= 1; i--)
		                 {
		                 soma += numeros.charAt(tamanho - i) * pos--;
		                 if (pos < 2)
		                       pos = 9;
		                 }
		           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		           if (resultado != digitos.charAt(1)){
		                 alert('CNPJ Inv\u00e1lido');
		                 c.focus();
		                 return false;
		                 }
		           else {
		     // alert('CNPJ  OK !');
		            return true;
		            }
		           }
		     else{
		           alert('CNPJ Inv\u00e1lido');
		           c.focus();
		           return false;
		           }
		}
		
     }  

	function ToFmt(x){
		this.x=x;
		this.fmt00 = fmt00;
		this.fmtF = fmtF;
		this.fmtE=fmtE;
		this.fmtI=fmtI;
		this.spacer="";
		this.setSpacer=setSpacer;
	}

	function fmt00(){
		 // fmt00: Tags leading zero onto numbers 0 - 9.
		 // Particularly useful for displaying results from Date methods.
		 //
		 if (parseInt(this.x) < 0) var neg = true;
		 
		 if (Math.abs(parseInt(this.x)) < 10){
		 	this.x = "0"+ Math.abs(this.x);
		 }
		 
		 if (neg) this.x = "-"+this.x;
		 return this.x;
	}

	function fmtF(w,d){
	
		 // fmtF: formats in a style similar to Fortran's Fw.d, where w is the
		 // width of the field and d is the number of figures after the decimal
		 // point. 
		 // The result is aligned to the right of the field.  The default
		 // padding character is a space " ". This can be modified using the 
		 // setSpacer(string) method of ToFmt. 
		 // If the result will not fit in the field , the field will be returned
		 // containing w asterisks.
		 var width=w;
		 var dpls=d;
		 var lt1=false;
		 var len=this.x.toString().length;
		 var junk;
		 var res="";
		// First check for valid format request
		 if ( width < (dpls+2)){
		  window.alert("Illegal format specified : w = " + d +
		               " w = " + d +
		                "\nUsage: [ToFmt].fmtF(w,d)" +
		                "\nWidth (w) of field must be greater or equal to the number " +
		                "\nof digits to the right of the decimal point (d) + 2");
		  junk = filljunk(width);
		  return junk;
	 	}
		// Work with absolute value
		var absx=Math.abs(this.x);
		// Nasty fix to deal with numbers < 1 and problems with leading zeros!
		if ((absx < 1) && (absx > 0)){
			lt1 = true;
		  	absx+=10;
		}
		// Get postion of decimal point
		var pt_pos = absx.toString().indexOf(".");
		if ( pt_pos == -1){
			res+= absx;
		  	res+= ".";
		  for (var i = 0; i < dpls; i++){
		  	res += 0;
		  }  
		}
		else{
			res = Math.round(absx * Math.pow(10,dpls));
		  	res=res.toString();
		  	if (res.length == 
		    	Math.round(Math.floor(absx * Math.pow(10,dpls))).toString().length){ 
		   	res = res.substring(0,pt_pos) + "." + 
		         res.substring(pt_pos,res.length);
		}
		else{
			pt_pos++;
		   	res = res.substring(0,pt_pos) + "." + 
		          res.substring(pt_pos,res.length);
		} 
		// Remove leading 1 from  numbers < 1 (Nasty fix!)
		if (lt1) {
			res=res.substring(1,res.length);
		}
	}
	// Final formatting statements
	// Reinsert - sign for negative numbers
	if (this.x < 0)res = "-"+res;
	// Check whether the result fits in the width of the field specified
	if (res.length > width){
		res=filljunk(width);
	}
	// If necessary, pad from the left with the spacer string
	else if (res.length < width){
		var res_bl="";
		for (var i = 0; i < (width - res.length); i++){
			res_bl += this.spacer ;
	 	} 
	 	res = res_bl + res;
	}
	return res;
}

	function fmtE(w,d){
	
		// fmtE: formats in a style similar to Fortran's Ew.d, where w is the
		// width of the field and d is the number of figures after the decimal
		// point. 
		// The result is aligned to the right of the field.  The default
		// padding character is a space " ". This can be modified using the 
		// setSpacer(string) method of ToFmt. 
		// If the result will not fit in the field , the field will be returned
		// containing w asterisks.
		//
		var width=w;
		var dpls=d;
		var e="E+";
		var len=this.x.toString().length;
		var pow10;
		var xp10;
		var junk;
		var res="";
// First check for valid format request
 if ( width < (dpls+5)){
  window.alert("Illegal format specified : w = " + d +
               " w = " + d +
                "\nUsage: [ToFmt].fmtE(w,d)" +
                "\nWidth (w) of field must be greater or equal to the number " +
                "\nof digits to the right of the decimal point (d) + 6");
  junk = filljunk(w);
  return junk;
 }
// Work with absolute value
 var absx=Math.abs(this.x);
// Get postion of decimal point
 var pt_pos = absx.toString().indexOf(".");
// For x=0
 if (absx == 0){
  res +="0.";
  for (var i=0; i< dpls; i++){
   res += "0";
  }
  res  += "E+00";
 }
// For abs(x) >= 1 
 else if (absx >= 1.0){
  pow10=1;
  xp10 = absx;
  while (xp10 >= 1.){
   pow10++;
   xp10 /= 10;
  }
  res = Math.round(xp10 * Math.pow(10,dpls));
  res=res.toString();
  if (res.length == 
      Math.round(Math.floor(xp10 * Math.pow(10,dpls))).toString().length){ 
    pow10--;
  }
  res = "0." + res.substring(0,dpls) + e + (new ToFmt(pow10)).fmt00();
 }
// For abs(x) < 1
 else if (absx < 1.0){
  pow10=1;
  xp10 = absx;
  while (xp10 < 1.){
   pow10--;
   xp10 *= 10;
  }
  res = Math.round(xp10/10 * Math.pow(10,dpls));
  res=res.toString();
  if (res.length != 
      Math.round(Math.floor(xp10/10 * Math.pow(10,dpls))).toString().length){ 
    pow10++;
  }
  if (pow10 < 0) e = "E-";
  res = "0." + res.substring(0,dpls) + e + (new ToFmt(Math.abs(pow10))).fmt00();
 }
 
 if (this.x < 0)res = "-"+res;
 if (res.length > width){
  res=filljunk(width);
 }
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
 
}

function fmtI(w){

 // fmtI: formats in a style similar to Fortran's Iw, where w is the
 // width of the field.
 // Floating point values are truncated (rounded down) for integer
 // representation.
 // The result is aligned to the right of the field.  The default
 // padding character is a space " ". This can be modified using the 
 // setSpacer(string) method of ToFmt. 
 // If the result will not fit in the field , the field will be returned
 // containing w asterisks.
 var width=w;
 var lt0=false;
 var len=this.x.toString().length;
 var junk;
 var res="";
// Work with absolute value
 var absx = Math.abs(this.x);

// Test for < 0
 if (parseInt(this.x) < 0){
  lt0 = true;
 }
 res = Math.round(Math.floor((absx))).toString();
 if (lt0){
  res = "-"+res;
 }
 if (res.length > width){
  res=filljunk(width);
 }
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
}

function filljunk(lenf){
 // Fills field of length lenf with asterisks
 var str="";
 for (var i=0; i < lenf; i++){
  str +="*";
 }
 return str;
}

function setSpacer(spc){
 var spc;
 this.spacer=spc;
 return this.spacer;
}	

// Limita o tamanho de um campo do tipo textarea para no m?ximo maxTam caracteres
function limitaTamanhoTextArea(campo, maxTam) {
	var value = campo.value;
	
	if (document.all == undefined) {
		value = value.replace(/\n/g, '\r\n');
	}
	
	if (value.length > maxTam) {
		if (value.charAt(maxTam-1) == '\r') 
			maxTam --;
		
		campo.value = value.substring(0, maxTam);
	}
}


//colo o campo como uppercase e remove os caracteres especiais
function formataString(campo){
//		campo.value = campo.value.toUpperCase().replace(/([^A-Z][\S])/g,"");
}

//Mascara de data dd/mm/aaaa
function mascaraData(val){
   	
 		var pass = val.value;
		var expr = /[0123456789]/;	
		for(i=0; i<pass.length; i++){
			// charAt -> retorna o caractere posicionado no índice especificado
			var lchar = val.value.charAt(i);
			var nchar = val.value.charAt(i+1);
			if(i==0){
			   // search -> retorna um valor inteiro, indicando a posição do inicio da primeira
			   // ocorrência de expReg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
			   // instStr.search(expReg);
			   if ((lchar.search(expr) != 0) || (lchar>3)){
				  val.value = "";
			   }
			}else if(i==1){			   
				   if(lchar.search(expr) != 0){
					  // substring(indice1,indice2)
					  // indice1, indice2 -> será usado para delimitar a string
					  var tst1 = val.value.substring(0,(i));
					  val.value = tst1;				
 					  continue;			
				   }			   
				   if ((nchar != '/') && (nchar != '')){
					 	var tst1 = val.value.substring(0, (i)+1);				
						if(nchar.search(expr) != 0) 
							var tst2 = val.value.substring(i+2, pass.length);
						else
							var tst2 = val.value.substring(i+1, pass.length);
		
						val.value = tst1 + '/' + tst2;
				   }
			 }else if(i==4){
					if(lchar.search(expr) != 0){
						var tst1 = val.value.substring(0, (i));
						val.value = tst1;
						continue;			
					}
					if	((nchar != '/') && (nchar != '')){
						var tst1 = val.value.substring(0, (i)+1);
						if(nchar.search(expr) != 0) 
							var tst2 = val.value.substring(i+2, pass.length);
						else
							var tst2 = val.value.substring(i+1, pass.length);
		
						val.value = tst1 + '/' + tst2;
					}
   			  }
			  if(i>=6){
				  if(lchar.search(expr) != 0) {
						var tst1 = val.value.substring(0, (i));
						val.value = tst1;			
				  }
			  }
		 }
     		 if(pass.length>10)
			val.value = val.value.substring(0, 10);
		 return true;
		}

