var ajax = geraAjax();
var msg_cadastro_ok = "<strong>Obrigado por assinar nosso Boletim!</strong>Uma mensagem de confirmação em breve será enviada para seu e-mail. Por favor, certifique-se de clicar no link para confimar sua inscrição.";
var msg_erro = "<strong>Ocorreu um erro durante o envio!</strong>Verifique os campos e tente novamente.";
var msg_cancelamento_ok = "<strong>Sua assinatura do Boletim foi cancelada!</strong>Uma mensagem de confirmação em breve será enviada para seu e-mail.";
var msg_atualiza_ok = "<strong>Seus dados foram atualizados!</strong>Você irá receber uma mensagem com suas mudanças.";
var msg_envialink_ok = "<strong>Sucesso!!</strong>Em breve você irá receber uma mensagem com o link para alterar seus dados.";
var msg_usernot = "<strong>Usuário não encontrado!!</strong>Seu e-mail consta em nossa lista de assinantes.";
var msg_fatal = "<strong>Erro fatal!!</strong>Você não pode alterar para esse endereço de e-mail pois ele já existe.";  
//ajax.onreadystatechange = retorno;

function retorno() {
  if (ajax.readyState == 4) { // Completo 
    if (ajax.status == 200) document.cadastro.botao.disabled=false;//alert("Dados recebidos"); 
    else alert("Problema: " + ajax.statusText);  
  }
}


function cadastroBoletim(f) {
  limpaMsgErro();
  if (!verificaForm(f)) return false;
  f.botao.disabled=true;
  email = f.Email.value;
  nome = f.Nome.value;//attribute9
  uf = f.Estado.value;//attribute4
  idade = f.Faixa_etaria.value;//attribute1
  inst = f.Grau_de_instrucao.value;//attribute2
  sexo = f.Sexo.value;//attribute8
  ajax.open("POST","http://www.acessa.com/phpList/?p=subscribe",false);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.send("email="+email+"&attribute9="+nome+"&attribute4="+uf+"&attribute8="+sexo+"&attribute2="+inst+"&attribute1="+idade+"&htmlemail=1&list[1]=signup&listname[1]=Boletim ACESSA.com&subscribe=Assinar");   
  if(ajax.responseText.indexOf("Thank you for subscribing to our newsletters") != -1) {     
    document.getElementById("msg_retorno").innerHTML = msg_cadastro_ok;
    f.reset();    
  } else {    
    document.getElementById("msg_retorno").innerHTML = msg_erro;
  }
  document.getElementById("msg_retorno").className = "msg";     
  f.botao.disabled=false;
  return false;      
}

function cancelamentoBoletim(f) {
  limpaMsgErro();
  if (!verificaForm(f)) return false;
  f.botao.disabled=true;
  email = f.Email.value;
  motivo = f.Motivo.value;
  ajax.open("POST","http://www.acessa.com/phpList/?p=unsubscribe",false);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.send("unsubscribeemail="+email+"&unsubscribereason="+motivo+"&unsubscribe=Unsubscribe");
  if(ajax.responseText.indexOf("You have been unsubscribed from our newsletters") != -1) {     
    document.getElementById("msg_retorno").innerHTML = msg_cancelamento_ok;
    f.reset();    
  } else if(ajax.responseText.indexOf("Error: User not found") != -1) {    
    document.getElementById("msg_retorno").innerHTML = msg_usernot;
  } else {    
    document.getElementById("msg_retorno").innerHTML = msg_erro;
  }
  //
  document.getElementById("msg_retorno").className = "msg";     
  f.botao.disabled=false;
  return false;
}


function atualizaBoletim(f) {
  limpaMsgErro();
  if (!verificaForm(f)) return false;
  f.botao.disabled=true;
  email = f.Email.value;
  nome = f.Nome.value;//attribute9
  uf = f.Estado.value;//attribute4
  idade = f.Faixa_etaria.value;//attribute1
  inst = f.Grau_de_instrucao.value;//attribute2
  sexo = f.Sexo.value;//attribute8
  uid = f.uid.value;
  ajax.open("POST","http://www.acessa.com/phpList/?p=preferences&uid="+uid,false);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.send("email="+email+"&attribute9="+nome+"&attribute4="+uf+"&attribute8="+sexo+"&attribute2="+inst+"&attribute1="+idade+"&htmlemail=1&list[1]=signup&listname[1]=Boletim ACESSA.com&update=Update");   
  if(ajax.responseText.indexOf("Thank you for your submission") != -1) {     
    document.getElementById("msg_retorno").innerHTML = msg_atualiza_ok;    
  } else if(ajax.responseText.indexOf("Fatal Error") != -1) {    
    document.getElementById("msg_retorno").innerHTML = msg_fatal;
    f.reset();    
  } else {    
    document.getElementById("msg_retorno").innerHTML = msg_erro;
  }
  document.getElementById("msg_retorno").className = "msg";     
  f.botao.disabled=false;
  return false;      
}

function enviaLinkatualizaBoletim(f) {
  limpaMsgErro();
  if (!verificaForm(f)) return false;
  f.botao.disabled=true;
  email = f.Email.value;
  ajax.open("POST","http://www.acessa.com/phpList/?p=preferences",false);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.send("email="+email+"&sendpersonallocation=Continue");   
  if(ajax.responseText.indexOf("Success") != -1) {     
    document.getElementById("msg_retorno").innerHTML = msg_envialink_ok;
    f.reset();    
  } else if(ajax.responseText.indexOf("User not found") != -1) {    
    document.getElementById("msg_retorno").innerHTML = msg_usernot;
  } else {    
    document.getElementById("msg_retorno").innerHTML = msg_erro;
  }
  document.getElementById("msg_retorno").className = "msg";     
  f.botao.disabled=false;
  return false;      
}

function confirmaEmailcancelaBoletim(f) {
  limpaMsgErro();
  if (!verificaForm(f)) return false;
  f.botao.disabled=true;
  email = f.Email.value;
  ajax.open("POST","http://www.acessa.com/phpList/?p=unsubscribe",false);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.send("unsubscribeemail="+email);   
  if(ajax.responseText.indexOf("you are not subscribed") != -1) {     
    document.getElementById("msg_retorno").innerHTML = msg_usernot;    
    f.reset();    
  } else {    
    location.href="/boletim/cancelar/?eml="+email;
  }
  document.getElementById("msg_retorno").className = "msg";     
  f.botao.disabled=false;
  return false;      
}

function limpaMsgErro() {
  document.getElementById("msg_retorno").innerHTML = "";
  document.getElementById("msg_retorno").className = "";
}