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);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
		(key==9) || (key==13) || (key==27) )
		return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	
	// decimal point jump
	else if (dec && (keychar == ".")) {
		myfield.form.elements[dec].focus();
		return false;
	}
	else
		return false;
}

function FormataData(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = event.srcElement.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
		{
			event.srcElement.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		}
		if ( tam >= 5 && tam <= 10 )
		{
			event.srcElement.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); }
		}
}

function somenteNumeros(e){
//Alterada para funcionar no firefox.

	var validos = "0123456789";
	var codTecla;
	var tecla;
	
	if (window.event)
		codTecla = window.event.keyCode;
	else if (e)
		codTecla = e.which;
	else
		return true;
		
	tecla = String.fromCharCode(codTecla);
	
	//Teclas de Controle
	if ((codTecla==null) || (codTecla==0) || (codTecla==8) ||
		(codTecla==9) || (codTecla==13) || (codTecla==27) ){
		return true;
	}
	
	//Numeros
	if (validos.indexOf(tecla) > -1){
		return true;
	}
	
	return false;
}


function substitui(strIsso,strAquilo)
{
  var strOriginal = this.toString();
  var resultado = ""
  var intInicio, intFim;

  while(strOriginal.indexOf(strIsso) >= 0)
  {
    intInicio   = 0;
    intFim      = strOriginal.indexOf(strIsso);
    resultado  += strOriginal.substring(intInicio,intFim) + strAquilo;
    intInicio   = parseInt(intFim) + parseInt(strIsso.length);
    strOriginal = strOriginal.substring(intInicio,strOriginal.length);
  }
  resultado += strOriginal;
  return resultado;
}

String.prototype.substitui = substitui;

function validaIdentificador(obj){
	var vr = obj.value;
	vr = vr.substitui(".","");
	vr = vr.substitui("/","");
	vr = vr.substitui("-","");
	tam = vr.length;
	
	if (tam == 11){
	    if (validarCPF(vr) == false){
	    	alert("CPF inválido");
			obj.focus();
			return(false);
	    }
	}
	if (tam == 14){
		if (validarCGC(vr) == false){
	    	alert("CNPJ inválido");
			obj.focus();
			return(false);
	    }
	}
}


function formataIdentificador(obj, e){
	var vr = obj.value;
	vr = vr.substitui(".","");
	vr = vr.substitui("/","");
	vr = vr.substitui("-","");
	tam = vr.length;
	
	
		if (tam == 14){
			formataCnpj(14, obj, e);
		}
		else{
			if (tam > 0){
				alert("Informe corretamente o CNPJ");
				obj.focus();
				return(false);
			}
	}
}

function formataCpf(tammax, obj, e) {

	if (somenteNumeros(e)) {
	 	//var tecla = window.event.keyCode;
	 
		var tecla;
		
		if (window.event)
			tecla = window.event.keyCode;
		else if (e)
			tecla = e.which;
		else
			return true;
		  
		 var vr = obj.value;
		 vr = vr.substitui('.','');
		 vr = vr.substitui('-','');
		 tam = vr.length;
		
		 if (tam < tammax && tecla != 8 && tecla != 0){ tam = vr.length + 1 ; }
		
		 if (tecla == 8 ){ tam = tam - 1 ; }
		  
		 if ( tecla == 0 || tecla == 8 || tecla == 13 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
		   if ( tam <= 3 ){
		    obj.value = vr ; }
		   if ( (tam > 3) && (tam <= 7) ){
		    obj.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) ; }
		   if ( (tam >= 7) && (tam <= 10) ){
		    obj.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 6, 3 ) ; }
		   if ( (tam >= 10) && (tam <= 11) ){
		    obj.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 6, 3 ) + '-' + vr.substr( 9, 2 ) ; }
		 }
		 return true;
	} else {
		return false;
	}
		

}

function formataCnpj(tammax, obj, e) {

	if (somenteNumeros(e)) {
	
		//var tecla = window.event.keyCode;
		var tecla;
	
		if (window.event)
			tecla = window.event.keyCode;
		else if (e)
			tecla = e.which;
		else
			return true;
	  
		var vr = obj.value;
		vr = vr.substitui(".","");
		vr = vr.substitui("/","");
		vr = vr.substitui("-","");
		tam = vr.length;
	
		if (tam < tammax && tecla != 8 && tecla != 0){ tam = vr.length + 1 ; }
		if (tecla == 8 ){ tam = tam - 1 ; }
		if ( tecla == 0 || tecla == 8 || tecla == 13 ||(tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
			if ( tam <= 2 ){ 
				obj.value = vr ; }
			if ( (tam > 2) && (tam <= 5) ){
				obj.value = vr.substr( 0, 2) + '.' + vr.substr( 2, 3 ) ; }
			if ( (tam >= 6) && (tam <= 8) ){
				obj.value = vr.substr( 0, 2) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) ; }
			if ( (tam >= 9) && (tam <= 12) ){
				obj.value = vr.substr( 0, 2) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) ; }
			if ( (tam >= 13) && (tam <= 14) ){
				obj.value = vr.substr( 0, 2) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) + '-' + vr.substr( 12, 2 ) ; }
		}  
		return true;
	} else {
		return false;
	}
}


function validarCGC(strCGC)
{
  var d1,d4,d2,Conta,fator,sobra,digito1,digito2;
  var check;

  strCGC = strCGC.substitui(".","");
  strCGC = strCGC.substitui("/","");
  strCGC = strCGC.substitui("-","");

  if (strCGC.length != 14 || strCGC * 1 == 0)
  {
    return false;
  }

  d1 = 0;  //valor padrao
  d4 = 0;  //valor padrao
  d2 = 1;  //valor padrao
  for(conta= 0; conta <=strCGC.length -3; conta++)
  {
      if (d2 < 5)
      {
        fator = 6 - d2
      }
      else
      {
        fator = 14 - d2;
      }

      d1 = d1 + strCGC.substr(conta,1)* fator;
      if (d2 < 6)
      {
        fator = 7 - d2;
      }
      else
      {
        fator = 15 - d2;
      }
      d4 = d4 + strCGC.substr(conta,1) * fator;
      d2 = d2 + 1;
  }
  sobra = (d1 % 11);
  if(sobra < 2)
  {
    digito1 = 0;
  }
  else
  {
    digito1 = 11 - sobra;
  }
  d4 = d4 + (2 * digito1);
  sobra = (d4 % 11);

  if (sobra < 2)
  {
    digito2 = 0;
  }
  else
  {
    digito2 = 11 - sobra;
  }
  check  = new String(digito1) + new String(digito2);
  return (check == strCGC.substr(strCGC.length-2,2));
}

function validarCPF(strCPF)
{
  var strInvalidos = '11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000';
  var contador, num1, num2, posicao,fator;
  var cpf,digitos;

  strCPF = strCPF.substitui('.','');
  strCPF = strCPF.substitui('-','');

  if (strCPF.length != 11 || strInvalidos.indexOf(strCPF) >= 0)
  {
    return false;
  }
  else
  {
    digitos = strCPF.substr(9,2);
    cpf     = strCPF.substr(0,9);
    fator   = 0;
    for(contador = 1; contador <= cpf.length; contador++)
    {
      fator = fator + contador * cpf.charAt(contador-1);
    }
    num1  = fator % 11;
    if(num1 == 10)
    {
      num1 = 0;
    }

    cpf     = strCPF.substr(0,9) + parseInt(num1);
    fator   = 0;
    posicao = 0;

    for(contador = cpf.length; contador >= 1; contador--)
    {
      fator   = fator + (contador + 1) * cpf.charAt(posicao);
      posicao = posicao + 1;
    }

    num2 = (fator * 10) % 11;
    if (num2 == 10)
    {
      num2 = 0;
    }

    return (new String(num1) + new String(num2) == digitos);
  }
}


function FormataCampo(Campo,teclapres,mascara){ 
    
    strtext = Campo.value 
    tamtext = strtext.length 
    tammask = mascara.length 
    arrmask = new Array(tammask)     
  
    for (var i = 0 ; i < tammask; i++){ 
        arrmask[i] = mascara.slice(i,i+1) 
    } 
   
    if (((((arrmask[tamtext] == "#") || (arrmask[tamtext] == "9"))) || (((arrmask[tamtext+1] != "#") || (arrmask[tamtext+1] != "9"))))){ 
        if ((teclapres.keyCode >= 37 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)){ 
            Organiza_Casa(Campo,arrmask[tamtext],teclapres.keyCode,strtext)         
        } 
        else{ 
            Detona_Event(Campo,strtext) 
        } 
    } 
    else{
        if ((arrmask[tamtext] == "A"))    { 
            charupper = event.valueOf() 
            Detona_Event(Campo,strtext) 
            masktext = strtext + charupper 
            Campo.value = masktext 
        } 
    } 
} 
function Organiza_Casa(Campo,arrpos,teclapres_key,strtext){ 
    if (((arrpos == "/") || (arrpos == ".") || (arrpos == ",") || (arrpos == ":") || (arrpos == " ") || (arrpos == "-")) && !(teclapres_key == 8)){ 
        separador = arrpos 
        masktext = strtext + separador 
        Campo.value = masktext 
    } 
} 
function Detona_Event(Campo,strtext){ 
    event.returnValue = false 
    if (strtext != "") { 
        Campo.value = strtext 
    } 
} 

function Tecla(e)
{
	if (document.all) 
		var tecla = event.keyCode;
	else if(document.layers) 
		var tecla = e.which;
		if (tecla > 47 && tecla < 58)
			return true;
		else
			{
				if (tecla != 8) 
					event.keyCode = 0;
					
				else
					return true;
			}

}

function isEmpty (x)
{
	y = new String (x.value)
	return (y.length == 0)
}
