var erro_consiste = false;
var local_retorno = "";

function enviar_form()
{
 if (verifica_assunto() == false)
     return false;

 if (verifica_nome() == false)
     return false;
         
 if (verifica_sobrenome() == false)
     return false;
     
 if (verifica_email() == false)
     return false;

 if (verifica_cpf() == false)
     return false;
     
 if (verifica_contato() == false)
     return false;
     
 if (verifica_origem_destino() == false)
     return false;
     
 if (verifica_mesmo_destino() == false)
     return false;

 if (verifica_ocorrencia() == false)
     return false;
}

// =========================
// Funções para consistência
// =========================
  function ver_data(data)
  {
     var mdata = '';
     var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;

     mdata = data.value;
     
     if (mdata == '')
         return true;
     
     if (mdata.length == 10 && mdata.substring(6) < 2007)
        {
         if (document.form1.txIdioma.value == 'PTB')
             alert(mdata + " - ano deve ser maior que 2006");
         else
         if (document.form1.txIdioma.value == 'INT')
             alert(mdata + " - year should be later than 2006");
         else
             alert(mdata + " - ano debe ser mayor que 2006");

         data.focus();
         return false;
        }
        
     if (!reDate.test(mdata))
        {
         if (document.form1.txIdioma.value == 'PTB')
             alert(mdata + ' - data invalida');
         else
         if (document.form1.txIdioma.value == 'INT')
             alert(mdata + ' - invalid date');
         else
             alert(mdata + ' - fecha invalida');
        
         data.focus();
         return false;
        }
        
     return true;
  }
  
  function edita_data(data)
    {
    if (event.keyCode < 48 || event.keyCode > 57)
        return false;
    
     var mdata  = ''; 

     mdata = mdata + data.value; 
     if (mdata.length == 2 || mdata.length == 5)
         { 
          mdata = mdata + '/'; 
          data.value = mdata; 
         } 
    }        

function verifica_assunto()
{
 if (document.form1.ddAssunto.value == '')
    {
     erro_consiste = true;
     
     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe o Assunto');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('State subject');
     else
         alert('Informe el Asunto');

     document.form1.ddAssunto.focus();
     return false;
    }
}


function verifica_cpf()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return;
    }
     
 numero = document.form1.txCPF.value;

 if (numero.length < 11 && document.form1.txIdioma.value != 'PTB')
    {
     erro_consiste = false;
     return true;
    }

 if (numero != '' && !checkCPF(numero))
    {
     erro_consiste = true;

     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe CPF corretamente');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('State Individual Taxpayer Number (CPF) correctly');
     else
         alert('Informe NIF correctamente');

     document.form1.txCPF.focus();
     document.form1.txCPF.select();
     return false;
    }
 
 erro_consiste = false;
 return true;
}

function verifica_contato()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return;
    }
     
 if (document.form1.txContato.value == '')
    {
     erro_consiste = true;
     
     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe o telefone para contato');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('Enter a telephone number for contact');
     else
         alert('Informe el telefono para contacto');
     
     document.form1.txContato.focus();
     document.form1.txContato.select();
     return false;
    }
 else
    {
     if (!IsNumeric(document.form1.txContato.value))
        {
         erro_consiste = true;

     if (document.form1.txIdioma.value == 'PTB')
         alert('Telefone deve conter somente numeros');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('Telephone should consist of numbers only');
     else
         alert('Telefono debe contener solamente numeros');

         document.form1.txContato.focus();
         document.form1.txContato.select();
         return false;
        }
    }
    
 erro_consiste = false;
 return true;
}

function verifica_nome()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return;
    }
     
 if (document.form1.txNome.value == '')
    {
     erro_consiste = true;
     
     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe seu nome');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('State your name');
     else
         alert('Informe su nombre');

     document.form1.txNome.focus();
     document.form1.txNome.select();
     return false;
    }
    
 erro_consiste = false;
 return true;
}

function verifica_sobrenome()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return;
    }
     
 if (document.form1.txSobrenome.value == '')
    {
     erro_consiste = true;
     
     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe seu sobrenome');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('State your last name');
     else
         alert('Informe su apellido');
     
     document.form1.txSobrenome.focus();
     document.form1.txSobrenome.select();
     return false;
    }

 erro_consiste = false;
 return true;
}

function verifica_email()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return;
    }

 if (!emailCheck(document.form1.txEmail.value))
    {
     erro_consiste = true;
     
     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe e-mail corretamente');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('State e-mail correctly');
     else
         alert('Informe correo electronico correctamente');

     document.form1.txEmail.focus();
     document.form1.txEmail.select();
     return false;
    }
    
 erro_consiste = false;
 return true;
}

function verifica_origem_destino()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return;
    }
    
 if (document.form1.ddOrigem.value != '' && document.form1.ddDestino.value == '')
    {
     erro_consiste = true;
     
     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe o destino do seu voo');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('State your flight\'s destination');
     else
         alert('Informe el destino de su vuelo');
     
     return false;
    }
 else
 if (document.form1.ddOrigem.value == '' && document.form1.ddDestino.value != '')
    {
     erro_consiste = true;
     
     if (document.form1.txIdioma.value == 'PTB')
         alert('Informe a origem do seu voo');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('State your flight\'s origin');
     else
         alert('Informe el origen de su vuelo');

     return false;
    }
    
 erro_consiste = false;
 return true;
}

function verifica_mesmo_destino()
{
 if (document.form1.ddOrigem.value != '' && document.form1.ddDestino.value != '' && 
     document.form1.ddOrigem.value == document.form1.ddDestino.value)
    {
     erro_consiste = true;

     if (document.form1.txIdioma.value == 'PTB')
         alert('Origem do voo deve ser diferente do destino');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('Flight origin must be different from destination');
     else
         alert('Origen del vuelo debe ser diferente del destino');

     return false;
    }

 erro_consiste = false;
 return true;
}

function verifica_ocorrencia()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return;
    }

 if (document.form1.txDescricao.value == '')
    {
     erro_consiste = true;

     if (document.form1.txIdioma.value == 'PTB')
         alert('Descreva a ocorrencia');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('Describe occurrence');
     else
         alert('Describa el evento');

     document.form1.txDescricao.focus();
     return false;
    }
 else
     document.form1.txTarefa.value = document.form1.txDescricao.value;

 erro_consiste = false;
 return true;
}

function verifica_erro()
{
 if (erro_consiste == true)
    {
     erro_consiste = false;
     return false;
    }
 
 return true;
}
//==================================
//==================================
//==================================
function somente_numero()
{
 if (event.keyCode < 48 || event.keyCode > 57)
     return false;
 else
     return true;
}

function suprime_aspas()
{
 if (event.keyCode == 13)
     return true;
     
 if (event.keyCode == 39)
     return false;
 else
     return true;
}

function fOcorrencia(event){
    
    valor  = document.form1.txDescricao.value;
    limite = document.form1.TextBox2.value;
    tam =valor.length;    
    if (tam >limite)
    {
       bRetorno = false;        
    }
    else
    {
       bRetorno= true;
    }
    
    if (bRetorno==true)
    {
      if (event.keyCode == 13)
         return true;
     
      if (event.keyCode == 39)
          return false;
      else
          return true;    
    }
    
    return bRetorno;
    
}


function somente_letras()
{
 if (event.keyCode == 13)
     return true;

 if ((event.keyCode < 65 && event.keyCode != 32) ||
     (event.keyCode > 90 && event.keyCode < 97)  ||
     (event.keyCode > 122 && event.keyCode < 192))
      return  false;
}

function carrega_limites()
{
 document.form1.TextBox1.value = document.form1.hfLimite.value;
 document.form1.TextBox2.value = document.form1.hfLimite.value;
}

function ver_tamanho()
{
 valor  = document.form1.txDescricao.value;
 limite = document.form1.TextBox2.value;
 resto  = 0;

 document.form1.TextBox2.value = limite;

 if (valor.length > limite)
    {
     if (document.form1.txIdioma.value == 'PTB')
         alert('Limite para ocorrencia esgotado');
     else
     if (document.form1.txIdioma.value == 'INT')
         alert('Limit for occurrence passed');
     else
         alert('Limite para evento agotado');
    
     return false;
    }
 else
    {
     resto = limite - valor.length;
     document.form1.TextBox1.value = resto;
    }
}

function IsNumeric(strString)
{
 var strValidChars = "0123456789";
 var strChar;
 var blnResult = true;

 if (strString.length == 0)
     return false;

 for (i = 0; i < strString.length && blnResult == true; i++)
     {
      strChar = strString.charAt(i);

      if (strValidChars.indexOf(strChar) == -1)
         {
          blnResult = false;
         }
     }

 return blnResult;
}

// ====== Verifica CPF - inicio ====
function checkCPF(strNumber)
{
   var result = false;
   var digit;
   var calc;
 
   strNumber = getNumber(strNumber);
   if (strNumber.length < 11) {
      result = false;
   }
   
   if (strNumber == '00000000000' || strNumber == '11111111111' || strNumber == '22222222222' ||
       strNumber == '33333333333' || strNumber == '44444444444' || strNumber == '55555555555' ||
       strNumber == '66666666666' || strNumber == '77777777777' || strNumber == '88888888888' ||
       strNumber == '99999999999') {
       return false;
   }

   if (!parseInt(strNumber.substring(strNumber.length - 6 , strNumber.length)))
   {
      result = false;
   }

   digit  = strNumber.substring(strNumber.length-2, strNumber.length);
   number = strNumber.substring(0 , strNumber.length - 2);
   calc   = getCPFDigit(number);

   if ( digit != calc )
   {
      result = false;
   }
   else
   {
      result = true;
   }
   return result;
}

function getNumber(strNumber)
{
   var number;
   var strRetNumber="";
 
   for (var i=0 ; i < strNumber.length ; i++)
   {
      number = parseInt(strNumber.charAt(i));
      if ( number )
      {
         strRetNumber += strNumber.charAt(i)
      }
      else
      {
         if ( number == 0 )
         {
            strRetNumber += strNumber.charAt(i)
         }
      }
   }
   return strRetNumber;
}

function getCPFDigit(strNumber)
{
   var i;
   var digit1;
   var digit2;
   var total;
   var result;

   strNumber = '000000000' + getNumber(strNumber)
   strNumber = strNumber.substring(strNumber.length - 9, strNumber.length)

   total=0;
   for (i = 1 ; i < 10; i++)
      total = total + strNumber.substring(i - 1 , i) * (11 - i);

   digit1 = total * 10 % 11;
   if (digit1 == 10)
   {
      digit1 = 0;
   }

   aux = digit1.toString(10);
   strNumber = strNumber + aux.substring(aux.length - 1 , aux.length);

   total=0;

   for (i = 1 ; i < 11 ; i++)
      total = total + strNumber.substring(i - 1 , i) * (12 - i);

   digit2 = total * 10 % 11;
   if (digit2 == 10)
   {
      digit2 = 0;
   }

   result = "" + digit1 + "" + digit2;
   return result;
}
// ====== Verifica CPF - fim ====


// ====== Consiste email - inicio ====
function emailCheck (emailStr)
{
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
  var matchArray=emailStr.match(emailPat)

  if (matchArray==null)
     {
      return false
   }

  var user=matchArray[1]
  var domain=matchArray[2]

  if (user.match(userPat)==null)
     {
      return false
     }

  var IPArray=domain.match(ipDomainPat)

  if (IPArray!=null)
     {
      for (var i=1;i<=4;i++)
          {
           if (IPArray[i]>255)
              {
               return false
              }
          }
      return true
     }

  var domainArray=domain.match(domainPat)

  if (domainArray==null)
     {
      return false
     }

  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length

  if (domArr[domArr.length-1].length<2 || 
      domArr[domArr.length-1].length>3)
     {
      return false
     }

  if (len<2)
     {
      return false
     }

  return true;
}
// ====== Consiste email - fim ====

function abre_atendimento()
{
 var Wpopupsize =(320/2);
 var Hpopupsize =(450/2);
 var CenterPopUpX = (screen.width/2)-(Wpopupsize); 
 var CenterPopUpY = (screen.height/2)-(Hpopupsize); 
 var pos = "left="+CenterPopUpX+",top="+CenterPopUpY; 
 
 if (document.form1.txIdioma.value == 'PTB')
     window.open('http://gol.directtalk.com.br/clientes/custom/voegol/init_atendimento_online.htm', '_blank', 'width=320,height=450,toolbar=no,location=no,status=no,menubar=no,scrollbars=,resizable=no,' + pos)
 else
 if (document.form1.txIdioma.value == 'INT')
     window.open('http://gol.directtalk.com.br/clientes/custom/voegol/init_help_online.htm', '_blank', 'width=320,height=450,toolbar=no,location=no,status=no,menubar=no,scrollbars=,resizable=no,' + pos)
 else     
     window.open('http://gol.directtalk.com.br/clientes/custom/voegol/init_soporte_online.htm', '_blank', 'width=320,height=450,toolbar=no,location=no,status=no,menubar=no,scrollbars=,resizable=no,' + pos)
}

