﻿function getNumberCPF(e)
{
    var myCPF;
    var posTextBoxNome;
    var posTextBoxSobrenome;
    var keynum;
    
    if (window.event)
    {
        keynum = e.keyCode;
    }
    else
    {
        if (e.which)
        {
            keynum = e.which;
        }
    }
        
    myCPF = $('div#facebox2 div#divCpf input').eq(0).val();    
    myCPF+= "" + (keynum-48);
    
    if(myCPF.length == 14 && keynum != 8)
    {
        validaCPF(myCPF);
    }
    else
    {
       return true;
    }
}

function validaCPF(myCPF)
{
   var cpf = myCPF;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   
   if(!filtro.test(cpf))
   {
     alert('Por favor, informe um cpf válido.'); 
	 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")
	  {
    	  alert('Por favor, informe um cpf válido.'); 
    	  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)))
   {
	 alert('Por favor, informe um cpf válido.'); 
	 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)))
   {
     alert('Por favor, informe um cpf válido.'); 
	 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;
 }

function showPopup()
{
    $('div.popup').css("display", "");
    $('div#facebox2_overlay').show();
}

function closePopUp()
{
    $('div#selectMainBodyRight table#boxFareRules input').val($('div#facebox2 input').val());
    $('div#buttonContinueHide input').click();

    $('div.popup').css("display", "none");
    $('div.facebox2_hide').hide();
}

function maskCPF(cpf, e)
{
    var keynum;

    if(!numbersOnly(e))
    {
        return false;
    }

    var s = new String(cpf.value);
    s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

    tam = s.length + 1;

    if (tam > 3 && tam < 7)
    {
        cpf.value = s.substr(0,3) + '.' + s.substr(3, tam);
    }
    if (tam >= 7 && tam < 10)
    {
        cpf.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
    }
    if (tam >= 10 && tam < 12)
    {
        cpf.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);}
    }

function numbersOnly(e)
{
    var evt = (e) ? e : window.event;
    var key = (evt.keyCode) ? evt.keyCode : evt.which;

    if(key != null)
    {
        key = parseInt(key, 10);

        if(key == 8 || key == 127 )
        {
            return true;
        }
        if(key < 48 || key > 57)
        {
            return false;
        }
        else
        {
            if(evt.shiftKey)
            {
                return false;
            }
        }
    }
    return true;
}

function cpfNotEmpty()
{
    if ($('div#facebox2 table#boxFareRules input').val() == '')
    {
       alert("O campo CPF deve ser preenchido.");
       return false;
    }
    else 
    {
       return true;
    }
}