function limparErroCadastro(id){
  document.getElementById("div_msg").innerHTML    = '<div id="div_msg" class="div_msg"><span class="texto">Previs&atilde;o de lan&ccedil;amento: <span class="style10"><em>10/01/2010</em></span></span></div>';	
}

//FUNÇÃO QUE MONTA A URL E CHAMA A FUNÇÃO AJAX 	
function ajax(url) 
{ 
//alert(nick); 
//alert(dest); 

req = null; 
// Procura por um objeto nativo (Mozilla/Safari) 
if (window.XMLHttpRequest) { 
req = new XMLHttpRequest(); 
req.onreadystatechange = processReqChange; 
req.open("GET",url,true); 
req.send(null); 
// Procura por uma versão ActiveX (IE) 
} else if (window.ActiveXObject) { 
req = new ActiveXObject("Microsoft.XMLHTTP"); 
if (req) { 

req.onreadystatechange = processReqChange; 
req.open("GET",url,true); 

req.send(); 
} 
} 
} 

function processReqChange() 
{ 

// apenas quando o estado for "completado" 
if (req.readyState == 4) { 

// apenas se o servidor retornar "OK" 

if (req.status ==200) { 

// procura pela div id="pagina" e insere o conteudo 
// retornado nela, como texto HTML 

  if(req.responseText==1){
    //document.getElementById("div_msg").className = "msg_enviado";		  
	document.getElementById("div_msg").innerHTML    = ' ' +
  
													  '<div class="texto_img">' +
													  ' </div>' +
													  ' <span class="texto_data">' +
													  '   Previs&atilde;o de lan&ccedil;amento: ' +
													  '   <em>10/01/2010</em>' +
													  ' </span>' +
													  ' <br><div class="area_alerta">' +
													  ' <img class="texto_img_ok" src="imagens/ok_indicator.gif"/>' +  
													  ' <span class="texto_concluido" style="margin-left:25px; margin-top:5px">' +
													  '   A sua solicitação foi processada com sucesso<br><br><a href="javascript:novoEmail();">Clique aqui para cadastrar outro e-mail</a>' +
													  ' </span> </div>';    
  
    document.getElementById("cadastro_nome").value = "";
    document.getElementById("cadastro_email").value = "";
  } else { 	
	alert(req.responseText);    
    document.getElementById("div_msg").className = "msg_erro";		  
	document.getElementById("div_msg").innerHTML = "O correu um erro ao enviar a sua mensagem, tente mais tarde";  
  }

  } else { 
    alert("Houve um problema ao obter os dados:n" + req.statusText); 
  } 

  document.getElementById('btn_cadastrar').disabled = false;
}} 

function enviar(id){ 

  if (id=="cadastro"){
    cp_nome  = document.getElementById("cadastro_nome");
    cp_email = document.getElementById("cadastro_email"); 	  
    caminho  = "cadastro.php?nome=" + cp_nome.value + "&email="+cp_email.value + "&id=NewsLetter - SFF";
  }
  
  document.getElementById('btn_cadastrar').disabled = true;
  document.getElementById("div_msg").className = "texto_concluido";
  document.getElementById("div_msg").innerHTML = "<br><br><br><img class='img_load' src='imagens/indicator.gif'/><span class='texto_load'>Por favor aguarde, sua solicita&ccedil;&atilde;o est&aacute; sendo processada...</span>"; 
  
  ajax(caminho);
} 	

function novoEmail(){
  cp_nome		    = document.getElementById("cadastro_nome");
  cp_email 		    = document.getElementById("cadastro_email"); 
  cp_nome.value	    = "";
  
  document.getElementById("div_msg").innerHTML    = ' ' +
													  '<div class="texto_img">' +
													  ' <span class="texto_data">' +
													  '   Previs&atilde;o de lan&ccedil;amento: ' +
													  '   <em>10/01/2010</em>' +
													  ' </span>' +
													  '</div>'  
  cp_email.value    = "";
  cp_nome.focus();
}

function chamadaErro(msg,cp_nome, div){
  document.getElementById(cp_nome).focus();
  //document.getElementById(div).className = "r";
  //document.getElementById("div_msg").className = "msg_erro";			
  document.getElementById(div).innerHTML = msg;	          
}
		
function chamadaSemErro(msg, div){
  //document.getElementById("div_msg").className = "msg";		  
  document.getElementById(div).innerHTML = ' ' +
  
  '<div class="texto_img">' +
  ' </div>' +
  ' <span class="texto_data">' +
  '   Previs&atilde;o de lan&ccedil;amento: ' +
  '   <em>10/01/2010</em>' +
  ' </span>' +
  ' <br><div class="area_alerta">' +
  ' <img class="texto_img_ok" src="imagens/ok_indicator.gif"/>' +  
  ' <span class="texto_concluido">' +
  '   O seu e-mail foi cadastrado com sucesso<br><br><a href="javascript:novoEmail();">Clique aqui para cadastrar outro e-mail</a>' +
  ' </span> </div>';    
  
  //document.getElementById(div).className = "required";		          
}	
	
	
function isValidMailCadastroEmail(email){
  cp_email = email.value;  
  r=true;

  if (r){  
    if (cp_email.length == 0){
	  chamadaErro(htmlAlerta('Informe o seu e-mail'), "cadastro_email", "div_email_cadastro");
	  r=false;
    }else{
      chamadaSemErro("", "div_msg");	 
    }
  } 

  if (r){  
    if ((cp_email.length != 0) && ((cp_email.indexOf("@") < 1) || (cp_email.indexOf('.') < 7))){
	  chamadaErro(htmlAlerta('informe um e-mail v&aacute;lido'), "cadastro_email", "div_msg");		
      r=false;
    }else{
      chamadaSemErro("", "div_msg");	 
    }
  }
		  
  if (r){
    document.getElementById("div_msg").className = "msg";				  
  }
  return  r;
}

function isValidMailCadastroEmail(email){
  cp_email = email.value;  
  r=true;

  if (r){  
    if (cp_email.length == 0){
	  chamadaErro(htmlAlerta('Informe o seu e-mail'), "cadastro_email", "div_msg");
	  r=false;
    }else{
      chamadaSemErro("", "div_msg");	 
    }
  } 

  if (r){  
    if ((cp_email.length != 0) && ((cp_email.indexOf("@") < 1) || (cp_email.indexOf('.') < 7))){
	  chamadaErro(htmlAlerta('informe um e-mail v&aacute;lido'), "cadastro_email", "div_msg");		
      r=false;
    }else{
      chamadaSemErro("","div_msg");	 
    }
  }
		  
  if (r){
    document.getElementById("div_msg").className = "msg";				  
  }
  return  r;
}

function htmlAlerta(texto){
  texto = '<img class="img_alerta" src="imagens/icon_error.gif"/><span class="alerta">'+ texto +'</span>';
  return texto;	  
}

function validar_cadastro(id) {
  cp_nome		    = document.getElementById("cadastro_nome");
  cp_email 		    = document.getElementById("cadastro_email"); 
  r=true;	  
  
  if(id=='cadastro'){
	  
    // CADASTRO: NOME 
    if ((cp_nome.value.length <= "2")) {		
	  chamadaErro(htmlAlerta('Informe o seu nome'), "cadastro_nome", "div_msg");
      r=false;				  		  
    }
	
    // CADASTRO: EMAIL 	
    if (r){ 
    if (!isValidMailCadastroEmail(cp_email)){
      r=false;				  		  
    }}	

    if (r){
      enviar(id);
    }	 
  }
}
