// Javascript Document
//adiciona mascara de cnpj
function MascaraCNPJ(cnpj){
        if(mascaraInteiro(cnpj)==false){
                event.returnValue = false;
        }       
        return formataCampo(cnpj, '00.000.000/0000-00', event);
}

//adiciona mascara de cep
function MascaraCep(cep){
                if(mascaraInteiro(cep)==false){
                event.returnValue = false;
        }       
        return formataCampo(cep, '00000-000', event);
}

//adiciona mascara de data
function MascaraData(data){
        if(mascaraInteiro(data)==false){
                event.returnValue = false;
        }       
        return formataCampo(data, '00/00/0000', event);
}

//adiciona mascara ao telefone
function MascaraTelefone(tel){  
        if(mascaraInteiro(tel)==false){
                event.returnValue = false;
        }       
        return formataCampo(tel, '(00)0000-0000', event);
}

//adiciona mascara ao CPF
function MascaraCPF(cpf){
        if(mascaraInteiro(cpf)==false){
                event.returnValue = false;
        }       
        return formataCampo(cpf, '000.000.000-00', event);
}

//valida telefone
function ValidaTelefone(tel){
        exp = /\(\d{2}\)\d{4}\-\d{4}/
        if(!exp.test(tel.value))
                alert('Numero de Telefone Invalido!');
}

//valida CEP
function ValidaCep(cep){
        exp = /\d{5}\-\d{3}/
        if(!exp.test(cep.value))
                alert('Numero de Cep Invalido!');               
}

function ValidarCPF(textbox){
	var cpf = textbox.value;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  window.alert("CPF inválido. Tente novamente.");
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   return true;
 }
 
 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }

//valida data
function ValidaData(textbox){
//script configuration - Please set the wanted time range
  var year_range_begin = '1800';
  var year_range_end = '2010';

  //  contains the given date-string
  var Date;

  //  contains the length of the given date-string
  var date_length;

  //  contains the number of days of the month
  var month_length;

  //  These contain the day,month and year of the given date string after
  //  format correction
  var Day,Month,Year;

  //  number of points in date-string
  var point_count = 0;

  //  positions of points in date-string
  var point_positions = new Array;

  //  the new formated date is filled in here
  var correct_date_temp = new Array;

  //  start position of the year in the date-string
  var year_start_pos = 0;

  Date = textbox.value;
  date_length = Date.length;

  if(Date != ""){
    for(var str_pos = 0;str_pos < date_length; str_pos++){
      if(Date.charAt(str_pos)<"0" || Date.charAt(str_pos)>"9"){
        if(Date.charAt(str_pos,1)=='/'){
          point_count++;
          if(point_count <= 2){
            point_positions[point_positions.length] = str_pos;
          }
        }
        else{
          alertbox(textbox);
          return false;
        }
      }
    }
 
    if(point_count!=2){
      alertbox(textbox);
      return false;
    }
 
    //case 1 day-length = 1
    if(point_positions[0] == 1){
      correct_date_temp[correct_date_temp.length] = "0";
      correct_date_temp[correct_date_temp.length] = Date.substr(0,1);
      correct_date_temp[correct_date_temp.length] = "/";
      //month-length = 1
      if(point_positions[1] == 3){
        correct_date_temp[correct_date_temp.length] = "0";
        correct_date_temp[correct_date_temp.length] = Date.substr(2,1);
        correct_date_temp[correct_date_temp.length] = "/";
        year_start_pos = 4;
      }
      //month-length = 2
      else if(point_positions[1] == 4){
        correct_date_temp[correct_date_temp.length] = Date.substr(2,2);
        correct_date_temp[correct_date_temp.length] = "/";
        year_start_pos = 5;
      }
      //point at wrong position
      else{
        alertbox(textbox);
        return false;
      }
    }
    //case 2 day-length = 2
    else if(point_positions[0] == 2){
      correct_date_temp[correct_date_temp.length] = Date.substr(0,2);
      correct_date_temp[correct_date_temp.length] = "/";
      //month-length = 1
      if(point_positions[1] == 4){
        correct_date_temp[correct_date_temp.length] = "0";
        correct_date_temp[correct_date_temp.length] = Date.substr(3,1);
        correct_date_temp[correct_date_temp.length] = "/";
        year_start_pos = 5;
      }
      //month-length = 2
      else if(point_positions[1] == 5){
        correct_date_temp[correct_date_temp.length] = Date.substr(3,2);
        correct_date_temp[correct_date_temp.length] = "/";
        year_start_pos = 6;
      }
      //point at wrong position
      else{
        alertbox(textbox);
        return false;
      }
    }
    //year-length = 1
    if(date_length - year_start_pos == 1){
      correct_date_temp[correct_date_temp.length] = "200";
      correct_date_temp[correct_date_temp.length] = Date.substr(year_start_pos,1);
    }
    //year-length = 2
    else if(date_length - year_start_pos == 2){
      if(Date.substr(year_start_pos,2)<=30){
        correct_date_temp[correct_date_temp.length] = "20";
        correct_date_temp[correct_date_temp.length] = Date.substr(year_start_pos,2);
      }
      else{
        correct_date_temp[correct_date_temp.length] = "19";
        correct_date_temp[correct_date_temp.length] = Date.substr(year_start_pos,2);
      }
    }
    //year-length must be 4
    else if(date_length - year_start_pos == 4){
      correct_date_temp[correct_date_temp.length] = Date.substr(year_start_pos,4);
    }
    else{
      alertbox(textbox);
      return false;
    }
    Date = correct_date_temp.join("");
    textbox.value = Date;

    if (Date.length==10 && Date.substring(2,3)=="/" && Date.substring(5,6)=="/"){
      Day = parseInt(Date.substr(0,2),10);
      Month = parseInt(Date.substr(3,2),10);
      Year = parseInt(Date.substr(6,4),10);
    }
    else{
      alertbox(textbox);
      return false;
    }
    if (Month==4 || Month==6 || Month==9 || Month==11){
      month_length=30;
    }
    else if (Month==1 || Month==3 || Month==5 || Month==7 || Month==8 || Month==10 || Month==12){
      month_length=31;
    }
    else if(Month==2 && Year%4==0 && Year%100!=0 || Year%400==0){
      month_length=29;
    }
    else if(Month==2 && Year%4!=0 || Year%100==0 && Year%400!=0){
      month_length=28;
    }
    if (Day>=1 && Day<=month_length && Month>=1 && Month<=12 && Year>=year_range_begin && Year<=year_range_end){
      return true;
    }
    else{
      alertbox(textbox);
      return false;
    }
  }
}

//*************************************************************************
//  name of function: alertbox(var textbox-object)
//        parameters: name of a textbox
//      last changes: 04.04.2002
//
//  Description:
//  This function is used to give an error message in case of an incorrect
//  date.
//  After printing the messagebox the textbox is cleared and focused.
//*************************************************************************
function alertbox(textbox){
  //  Printing the error message
  alert('O valor ' + textbox.value + ' não é uma data válida.');
  //  Clearing the textbox
  textbox.value='';
  //  Sets the focus to the document
  this.focus();
  // Sets the focus to the textbox
  textbox.focus();
}
/*
function seumdois(textbox){
  if (textbox.value <= 1){
  textbox.value = 2 } }
  /*
function ValidaData(data){
        exp = /\d{2}\/\d{2}\/\d{4}/
        if(!exp.test(data.value))
                alert('Data Invalida!'); 
				
}
*/
//valida o CPF digitado

//valida numero inteiro com mascara
function mascaraInteiro(){
        if (event.keyCode < 48 || event.keyCode > 57){
                event.returnValue = false;
                return false;
        }
        return true;
}

//valida o CNPJ digitado
function ValidarCNPJ(ObjCnpj){
        var cnpj = ObjCnpj.value;
        var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
        var dig1= new Number;
        var dig2= new Number;
        
        exp = /\.|\-|\//g
        cnpj = cnpj.toString().replace( exp, "" ); 
        var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
                
        for(i = 0; i<valida.length; i++){
                dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);  
                dig2 += cnpj.charAt(i)*valida[i];       
        }
        dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
        dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
        
        if(((dig1*10)+dig2) != digito)  
                alert('CNPJ Invalido!');
                
}

//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) { 
        var boleanoMascara; 
        
        var Digitato = evento.keyCode;
        exp = /\-|\.|\/|\(|\)| /g
        campoSoNumeros = campo.value.toString().replace( exp, "" ); 
   
        var posicaoCampo = 0;    
        var NovoValorCampo="";
        var TamanhoMascara = campoSoNumeros.length;; 
        
        if (Digitato != 8) { // backspace 
                for(i=0; i<= TamanhoMascara; i++) { 
                        boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                                                || (Mascara.charAt(i) == "/")) 
                        boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(") 
                                                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " ")) 
                        if (boleanoMascara) { 
                                NovoValorCampo += Mascara.charAt(i); 
                                  TamanhoMascara++;
                        }else { 
                                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo); 
                                posicaoCampo++; 
                          }              
                  }      
                campo.value = NovoValorCampo;
                  return true; 
        }else { 
                return true; 
        }
}