function check_cpf (numcpf) {
  x = 0;
  soma = 0; 
  dig1 = 0;
  dig2 = 0;
  texto = "";
  numcpf1="";
  len = numcpf.length;
  
  if (len == 11) {
    x = len -1;
    
    for (var i=0; i <= len - 3; i++) {
      y = numcpf.substring(i,i+1);
      soma = soma + ( y * x);
      x = x - 1;
      texto = texto + y;
    }

    dig1 = 11 - (soma % 11);
    if (dig1 == 10) dig1=0 ;
    if (dig1 == 11) dig1=0 ;
    numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
    x = 11; soma=0;

    for (var i=0; i <= len - 2; i++) {
      soma = soma + (numcpf1.substring(i,i+1) * x);
      x = x - 1;
    }

    dig2= 11 - (soma % 11);
    if (dig2 == 10) dig2=0;
    if (dig2 == 11) dig2=0;

    if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
      return true;
    }

    return false;
  } 
  else if (len == 14) {
    cgc1 = "";
    cgc2 = "";
    soma = 0;
    digito = 0;
    i = 0;
    j = 0;
    controle = "";
    mult =  "";
    len = numcpf.length;

    if (len == 14) {
      cgc1 = numcpf.substring(0, 12);
      cgc2 = numcpf.substring(12, 14);
      mult = "543298765432";

      for (var j=0; j<2; j++) {
	soma = 0;

	for (var i=0; i<12; i++) {
	  soma = soma + (cgc1.substring(i,i+1) * mult.substring(i,i+1));
	}

	if (j == 1) {
	  soma = soma + (2 * digito);
	}
	digito = (soma * 10) % 11;

	if (digito == 10) {
	  digito = 0;
	}

	controle = controle + digito.toString(10);
	mult = "654329876543";
      }
    }
    if (controle == cgc2) {
      return true;
    }
    return false;
  }
  return false;
}

function isEmail(string) {
  if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
    return true;
  } 
  else {
    return false;
  }
}  

function isNumber (string) {
  if (string.search(/^[0-9]+$/) != -1) {
    return true;
  } 
  else {
    return false;
  }
}

function hasNumber (string) {
  if (string.search(/[0-9]/) != -1) {
    return true;
  } 
  else {
    return false;
  }
}

function checkValidDate(dateStr) {
  // dateStr must be of format month day year with either slashes
  // or dashes separating the parts. Some minor changes would have
  // to be made to use day month year or another format.
  // This function returns True if the date is valid.
  var slash1 = dateStr.indexOf("/");
  if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
  // if no slashes or dashes, invalid date
  if (slash1 == -1) { return false; }
  var dateMonth = dateStr.substring(0, slash1);
  var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
  var slash2 = dateMonthAndYear.indexOf("/");
  if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
  // if not a second slash or dash, invalid date
  if (slash2 == -1) { return false; }
  var dateDay = dateMonthAndYear.substring(0, slash2);
  var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
  if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
  // if any non-digits in the month, invalid date
  for (var x=0; x < dateMonth.length; x++) {
    var digit = dateMonth.substring(x, x+1);
    if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text month to a number
  var numMonth = 0;
  for (var x=0; x < dateMonth.length; x++) {
    digit = dateMonth.substring(x, x+1);
    numMonth *= 10;
    numMonth += parseInt(digit);
  }
  if ((numMonth <= 0) || (numMonth > 12)) { return false; }
  // if any non-digits in the day, invalid date
  for (var x=0; x < dateDay.length; x++) {
    digit = dateDay.substring(x, x+1);
    if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text day to a number
  var numDay = 0;
  for (var x=0; x < dateDay.length; x++) {
    digit = dateDay.substring(x, x+1);
    numDay *= 10;
    numDay += parseInt(digit);
  }
  if ((numDay <= 0) || (numDay > 31)) { return false; }
  // February can't be greater than 29 (leap year calculation comes later)
  if ((numMonth == 2) && (numDay > 29)) { return false; }
  // check for months with only 30 days
  if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) {
    if (numDay > 30) { return false; }
  }
  // if any non-digits in the year, invalid date
  for (var x=0; x < dateYear.length; x++) {
    digit = dateYear.substring(x, x+1);
    if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text year to a number
  var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
      digit = dateYear.substring(x, x+1);
      numYear *= 10;
      numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
      var div4 = numYear % 4;
      var div100 = numYear % 100;
      var div400 = numYear % 400;
      // if not divisible by 4, then not a leap year so Feb 29 is invalid
      if (div4 != 0) { return false; }
      // at this point, year is divisible by 4. So if year is divisible by
      // 100 and not 400, then it's not a leap year so Feb 29 is invalid
      if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}


function validaCadastroDemonstracao() {
  for (i=0;i<3;i++) {
    box = document.frm_cadastro.elements[i];
    if (!box.value) {   
      box.focus();
      switch (i) {
      case 0: alert('Por favor, digite seu nome.'); break; 
      case 1: alert('Por favor, digite seu e-mail.'); break;
      case 2: alert('Por favor, digite seu CPF.'); break;
      case 3: alert('Por favor, digite o DDD de seu telefone.'); break;
      case 4: alert('Por favor, digite o número de seu telefone.'); break;     
      }
  
      box.focus();
      return ;
    }
  }
  
  box = document.frm_cadastro.Nome
  if (hasNumber(box.value)) {
    alert ("Por favor, digite um nome válido.");
    box.focus();
    return ;	 
  }
  if (box.value.lastIndexOf(" ") == -1) {
    alert ("Por favor, digite seu nome completo.");
    box.focus();
    return ;	 
  }
	 
  box = document.frm_cadastro.Email;
  if (!isEmail(box.value)) {
    alert ("Por favor, digite um E-mail válido.");
    box.focus();
    return ;
  }

  box = document.frm_cadastro.CPF;
  box.value = box.value.replace(/\./gi,"");
  box.value = box.value.replace(/\-/gi,"");
  box.value = box.value.replace(/ /gi,"");
  if (!check_cpf(box.value)) {
    alert ("Por favor, digite um CPF válido,");
    box.focus();
    return ;
  }
  
  box = document.frm_cadastro.DDD;  
  box.value = box.value.replace(/ /gi,"");
  if ((box.value.length > 5) || (box.value.length < 2) || (!isNumber(box.value))) {
    alert ("Por favor, digite um DDD válido.");
    box.focus();
    return;
  }

  box = document.frm_cadastro.Telefone;
  box.value = box.value.replace(/\-/gi,""); 
  box.value = box.value.replace(/ /gi,"");  
  if ((box.value.length < 6) || (box.value.length > 8) || (!isNumber(box.value))) {
    alert ("Por favor, digite um telefone válido.");
    box.focus();
    return ;
  }
      
  box = document.frm_cadastro.Internet;
  if ((box[0].status == false) && (box[1].status == false)) {
      alert ("Por favor, selecione o seu tipo de acesso a Internet.") ;
      box[0].focus;
      return;
  }    
  document.frm_cadastro.submit();
}
  
function validaCadastroAssinatura() {
  for (i=0;i<15;i++) {
    box = document.frm_assinatura.elements[i];
    if (!box.value) {   
      box.focus();
      switch (i) {
      case 0: alert('Por favor, digite seu nome.'); break; 
      case 1: alert('Por favor, digite seu E-mail.'); break;
      case 2: alert('Por favor, digite seu CPF.'); break;
      case 3: alert('Por favor, digite seu RG.'); break;
      case 5: alert('Por favor, digite seu endereço.'); break;
      case 6: alert('Por favor, digite seu bairro.'); break;
      case 7: alert('Por favor, digite sua cidade.'); break;
      case 8: alert('Por favor, digite seu CEP.'); break;
      case 11: alert('Por favor, digite o código DDD de seu telefone.'); break;
      case 12: alert('Por favor, digite seu telefone.'); break;
      case 13: case 14: case 15 : alert('Por favor preencha a sua data de nascimento.'); break;
      }  
      box.focus();
      return;
    }
  }

  box = document.frm_assinatura.NomeAssinatura;
  if (hasNumber(box.value)) {
    alert ("Por favor, digite um nome válido.");
    box.focus();
    return ;	 
  }  
  if (box.value.lastIndexOf(" ") == -1) {
    alert ("Por favor, digite seu nome completo.");
    box.focus();
    return ;	 
  }
	
  box = document.frm_assinatura.EmailAssinatura;
  if (!isEmail(box.value)) {
    alert ("Por favor, digite um E-mail válido.");
    box.focus();
    return ;
  }

  box = document.frm_assinatura.CPFAssinatura;
  box.value = box.value.replace(/\./gi,"");
  box.value = box.value.replace(/\-/gi,""); 
  box.value = box.value.replace(/ /gi,"");
  if (!check_cpf(box.value)) {
    alert ("Por favor, digite um CPF válido.");
    box.focus();
    return ;
  }

  box = document.frm_assinatura.RG;
  box.value = box.value.replace(/\./gi,"");
  box.value = box.value.replace(/\-/gi,""); 
  box.value = box.value.replace(/ /gi,"");
  if ((!isNumber(box.value)) ) {
    alert ("Por favor, digite um RG válido.");
    box.focus();
    return ;
  }

  box = document.frm_assinatura.CEP;
  box.value = box.value.replace(/\-/gi,""); 
  box.value = box.value.replace(/ /gi,"");
  if ((!isNumber(box.value)) || (box.value.length < 8)) {
    alert ("Por favor, digite um CEP válido.");
    box.focus();
    return ;
  }

  box = document.frm_assinatura.DDDAssinatura;  
  box.value = box.value.replace(/ /gi,"");
  if ((box.value.length > 5) || (box.value.length < 2) || (!isNumber(box.value))) {
    alert ("Por favor, digite um DDD válido.");
    box.focus();
    return ;
  }

  box = document.frm_assinatura.TelefoneAssinatura;
  box.value = box.value.replace(/\-/gi,""); 
  box.value = box.value.replace(/ /gi,"");  
  if ((box.value.length < 6) || (box.value.length > 8) || (!isNumber(box.value))) {
    alert ("Por favor, digite um telefone válido.");
    box.focus();
    return ;
  }

  var data = document.frm_assinatura.MesNascimento.value + "/" +
    document.frm_assinatura.DiaNascimento.value + "/" +
    document.frm_assinatura.AnoNascimento.value;
  if (!checkValidDate(data)) {
    alert ("Por favor, digite uma data de nascimento válida.");
    document.frm_assinatura.DiaNascimento.focus();
    return ;
  }
  
  box = document.frm_assinatura.Sexo;
  if ((box[0].status == false) && (box[1].status == false)) {
      alert ("Por favor, selecione o seu sexo.") ;
      box[0].focus;
      return ;
  }

 box = document.frm_assinatura.FormaQueConheceu;
  if ((box[0].status == false) && (box[1].status == false)&& (box[2].status == false)
       && (box[3].status == false)&& (box[4].status == false)&& (box[5].status == false)
       && (box[6].status == false) && (box[7].status == false)) {
      alert ("Por favor, responda como nos encontrou.") ;
      box[0].focus;
      return ;
  }
   if (box[0].status == true) {
     if (!document.frm_assinatura.FormaQueConheceu_Corretora.value) {
        alert ("Por favor, informe a corretora que nos indicou.") ;
        box[0].focus;
        return ;
     }
   }
   if (box[1].status == true) {
     if (!document.frm_assinatura.FormaQueConheceu_Indicacao.value) {
        alert ("Por favor, informe o e-mail de quem nos indicou.") ;
        box[1].focus;
        return ;
     }
   }
   if (box[2].status == true) {
     if (!document.frm_assinatura.FormaQueConheceu_Internet.value) {
        alert ("Por favor, informe o site.") ;
        box[2].focus;
        return ;
     }
   }
   if (box[3].status == true) {
     if (!document.frm_assinatura.FormaQueConheceu_Jornal.value) {
        alert ("Por favor, informe o jornal.") ;
        box[3].focus;
        return ;
     }
   }
   if (box[6].status == true) {
     if (!document.frm_assinatura.FormaQueConheceu_Outros.value) {
        alert ("Por favor, informe como nos conheceu.") ;
        box[6].focus;
        return ;
     }
   }
   if (box[7].status == true) {
     if (!document.frm_assinatura.FormaQueConheceu_Revista.value) {
        alert ("Por favor, informe a revista.") ;
        box[7].focus;
        return ;
     }
   }   

 box = document.frm_assinatura.Opera;
  if ((box[0].status == false) && (box[1].status == false)) {
      alert ("Por favor, responda corretamente se opera ou não no mercado de ações.") ;
      box[0].focus;
      return ;
  }
  
 box = document.frm_assinatura.Bancos;
  if (!box.value) {
      alert ("Por favor, informe com qual(is) banco(s) você trabalha.") ;
      box.focus;
      return ;
  }
     
  box = document.frm_assinatura.InternetAssinatura;
  if ((box[0].status == false) && (box[1].status == false)) {
      alert ("Por favor, selecione o seu tipo de acesso a Internet.") ;
      box[0].focus;
      return ;
  }  
  
//  box = document.frm_assinatura.PlanoPagamento;
//  if ((box[0].status == false) && (box[1].status == false) &&
//      (box[2].status == false) && (box[3].status == false)) {
//      alert ("Por favor, selecione o plano de pagamento que você deseja.") ;
//      box[0].focus;
//      return ;
      
  box = document.frm_assinatura.PlanoPagamento;
  if ((box[0].status == false) && (box[1].status == false) &&
      (box[2].status == false) && (box[3].status == false) &&
      (box[4].status == false) && (box[5].status == false) &&
      (box[6].status == false) && (box[7].status == false) &&
	  (box[8].status == false) && (box[9].status == false) &&
	  (box[10].status == false) && (box[11].status == false) &&
	  (box[12].status == false) && (box[13].status == false)) {
      alert ("Por favor, selecione o plano de pagamento que você deseja.") ;
      box[0].focus;
      return ;
      
  }  
  return ;
}   

function validaCadastroDownload1() {
  for (i=0;i<2;i++) {
    box = document.frm_download1.elements[i];
    if (!box.value) {   
      box.focus();
      switch (i) {
      case 0: alert('Por favor, digite seu login.'); break; 
      case 1: alert('Por favor, digite seu CPF.'); break;
      }
  
      box.focus();
      return ;
    }
  }
  
  box = document.frm_download1.Login
  if ((box.value.lastIndexOf("-") == -1) || (box.value.length != 11)) {
    alert ("Por favor, digite um login válido no formato XXXX-XXXXXX : 4 caracteres alfabéticos, seguido pelo caractere - (sinal de menos), seguido por 6 caracteres numéricos.");
    box.focus();
    return ;	 
  }
	 
  box = document.frm_download1.CPF;
  box.value = box.value.replace(/\./gi,"");
  box.value = box.value.replace(/\-/gi,"");
  box.value = box.value.replace(/ /gi,"");
  if (!check_cpf(box.value)) {
    alert ("Por favor, digite um CPF válido.");
    box.focus();
    return ;
  }
  document.frm_download1.submit();
}

function validaCadastroDownload2() {
  for (i=0;i<2;i++) {
    box = document.frm_download2.elements[i];
    if (!box.value) {   
      box.focus();
      switch (i) {
      case 0: alert('Por favor, digite sua senha.'); break; 
      case 1: alert('Por favor, repita sua senha.');
      }
  
      box.focus();
      return ;
    }
  }
  
  box = document.frm_download2.Senha
  if (box.value.length < 6) {
    alert ("Por favor, digite uma senha com pelo menos 6 caracteres alfanuméricos.");
    box.focus();
    return ;	 
  }
  if (box.value.lastIndexOf(" ") != -1) {
    alert ("A senha não pode conter caracteres em branco (espaço).");
    box.focus();
    return ;	 
  }  
	 
  box1 = document.frm_download2.ConfirmaSenha;
  if (box1.value != box.value) {
    alert ("Senha não pôde ser confirmada. Por favor, digite novamente");
    box.focus();
    return ;
  }
  document.frm_download2.submit();
}
