var cp_html= "";
var cp_id= "";
var cp_parametro = "";
var cp_texto_padrao_cadastro =  '<span class="texto"><br>Receba not&iacute;cias, '
								+'promo&ccedil;&otilde;es e informa&ccedil;&otilde;es</span>';
								
var cp_texto_padrao_indique =  '<span class="texto">Recomende este site</span>';
								
var cp_texto_conclussao_cadastro = " " +
								   "<div class='texto_img'></div>" +
								   "<span class='texto'><br>Receba not&iacute;cias, "+
								   "promo&ccedil;&otilde;es e informa&ccedil;&otilde;es</span> " +
								   " <br><div class='area_alerta'>" +
								   " <img class='texto_img_ok' src='imagens/ok_indicator.gif'/>" +  
								   " <span class='texto_concluido' onclick='concluir();'>" +
								   " O seu e-mail foi cadastrado com sucesso<br><br>" +								   
								   "<a href='javascript:concluir_cadastro();'>"+
								   "<span onclick='concluir_cadastro();'>" +								   
								   "Clique aqui para cadastrar outro e-mail</a></span>" +
								   " </span> </div>";	
								   
var cp_texto_conclussao_indique = " " +
								   "<div class='texto_img'></div>" +
								   "<span class='texto'><br>Receba not&iacute;cias, "+
								   "promo&ccedil;&otilde;es e informa&ccedil;&otilde;es</span> " +
								   " <br><div class='area_alerta'>" +
								   " <img class='texto_img_ok' src='imagens/ok_indicator.gif'/>" +  
								   " <span class='texto_concluido' onclick='concluir();'>" +
								   " A sua solicita&ccedil;&atilde;o foi enviada com sucesso<br><br>" +								   
								   "<a href='javascript:concluir_indique();'>"+
								   "<span onclick='concluir_indique();'>" +								   
								   "Clique aqui para enviar outro e-mail</a></span>" +
								   " </span> </div>";									   
function concluir(){
  var topo = document.getElementById("topo");  		
	topo.focus();  
	document.pagamentodigital.submit();
}


function concluir_cadastro(){
  limparErroCadastro("cadastro",  "div_msg");	
  document.getElementById("cadastro_nome").focus();	
}

function concluir_indique(){
  limparErroCadastro("indique",  "div_msg_indique");	
  document.getElementById("indique_nome").focus();	
}
								
//FUNÇÃO QUE LIMPA OS CAMPOS DA FORMA CADASTRO E DEFINE O TEXTO DE SAUDAÇÃO
function limparErroCadastro(id,  div_id){
  if(id=='cadastro'){	
    document.getElementById(div_id).innerHTML = cp_texto_padrao_cadastro;	
  }
  if(id=='indique')	
    document.getElementById(div_id).innerHTML = cp_texto_padrao_indique;	
}

//FUNÇÃO QUE MONTA A URL E CHAMA A FUNÇÃO AJAX 	
function ajax_forma(url, html_conclussao){ 
  req = null; 
  // Procura por um objeto nativo (Mozilla/Safari) 
  if (window.XMLHttpRequest) { 
    req = new XMLHttpRequest(); 
    req.onreadystatechange = processReqChangeForma; 
    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 = processReqChangeForma; 
        req.open("GET",url,true); 
        req.send(); 
	  } 
  } 
} 

function processReqChangeForma(){ 
  // apenas quando o estado for "completado" 
  if (req.readyState == 4) { 
  // apenas se o servidor retornar "OK" 
    if (req.status ==200) { 
      if(req.responseText==1){
		if(cp_id=='cadastro') {
          document.getElementById("cadastro_nome").value = "";
          document.getElementById("cadastro_email").value = "";	
          document.getElementById("div_msg").innerHTML = cp_texto_conclussao_cadastro;			  
          document.getElementById("cadastro_nome").focus();			  
  	    }
		
		if(cp_id=='indique_LojaVirtual') {
          document.getElementById("indique_nome").value = "";
          document.getElementById("indique_email").value = "";	
          document.getElementById("indique_amigo").value = "";
          document.getElementById("indique_email_amigo").value = "";			  
          document.getElementById("div_msg_indique").innerHTML = cp_texto_conclussao_indique;			  
          document.getElementById("indique_nome").focus();			  
  	    }	
		
		if(cp_id=='cadastro_Sites') {
          document.getElementById("cadastro_nome").value = "";
          document.getElementById("cadastro_email").value = "";	
          document.getElementById("div_msg").innerHTML = cp_texto_conclussao_cadastro;			  
          document.getElementById("cadastro_nome").focus();			  
  	    }		
		
		if(cp_id=='indique_Sites') {
          document.getElementById("indique_nome").value = "";
          document.getElementById("indique_email").value = "";	
          document.getElementById("indique_amigo").value = "";
          document.getElementById("indique_email_amigo").value = "";			  
          document.getElementById("div_msg_indique").innerHTML = cp_texto_conclussao_indique;			  
          document.getElementById("indique_nome").focus();			  
  	    }	
		
      }else{ 	
  	    alert(req.responseText);    
        document.getElementById("div_msg").className = "msg_erro";		  
  	    document.getElementById("div_msg").innerHTML = "Ocorreu 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, div_id, html_conclussao, par){ 
  cp_id = id;
  //MONTA O CAMINHO DO CADASTRO DE NEWSLETTER
  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=News Letter - "+ par;
    document.getElementById('btn_cadastrar').disabled = true;	
  }
  
  if (id=="indique_SFF"){
    cp_nome		      = document.getElementById("indique_nome");
    cp_amigo 		  = document.getElementById("indique_amigo"); 
    cp_email 		  = document.getElementById("indique_email");  
    cp_email_amigo    = document.getElementById("indique_email_amigo"); 	
    caminho  = "indique.php?nome=" + cp_nome.value + "&email="+cp_email.value + "&email_amigo="+cp_email_amigo.value + "&id=Indique - " + par +  "&amigo=" + cp_amigo.value;
  }  
  
  if (id=="indique_LojaVirtual"){
    cp_nome		      = document.getElementById("indique_nome");
    cp_amigo 		  = document.getElementById("indique_amigo"); 
    cp_email 		  = document.getElementById("indique_email");  
    cp_email_amigo    = document.getElementById("indique_email_amigo"); 	
    caminho  = "indique.php?nome=" + cp_nome.value + "&email="+cp_email.value + "&email_amigo="+cp_email_amigo.value + "&id=Indique - " + par + "&amigo=" + cp_amigo.value;
  }  
  
  if (id=="cadastro_Sites"){
    cp_nome  = document.getElementById("cadastro_nome");
    cp_email = document.getElementById("cadastro_email"); 	  
    caminho  = "cadastro.php?nome=" + cp_nome.value + "&email="+cp_email.value + "&id=News Letter - "+ par;
    document.getElementById('btn_cadastrar').disabled = true;	
  } 
  
  if (id=="indique_Sites"){
    cp_nome		      = document.getElementById("indique_nome");
    cp_amigo 		  = document.getElementById("indique_amigo"); 
    cp_email 		  = document.getElementById("indique_email");  
    cp_email_amigo    = document.getElementById("indique_email_amigo"); 	
    caminho  = "indique.php?nome=" + cp_nome.value + "&email="+cp_email.value + "&email_amigo="+cp_email_amigo.value + "&id=Indique - " + par + "&amigo=" + cp_amigo.value;
  }  
  

  document.getElementById(div_id).className = "texto_concluido";
  document.getElementById(div_id).innerHTML = "<br><br><br><img class='img_load' src='imagens/indicator.gif'/>"+
  											  "<span class='texto_load'>Por favor aguarde, "+
											  "processando...</span>"; 
  ajax_forma(caminho, html_conclussao);
} 	


function chamadaErro(msg,cp_nome, div){
  document.getElementById(cp_nome).focus();
  document.getElementById(div).innerHTML = msg;	          
}	
	
function isValidMail(email, div_id, campo_id){
  r=true;

  if (r){  
    if (email.value.length == 0){
	  chamadaErro(htmlAlerta('Informe o e-mail'), campo_id, div_id);
	  r=false;
    }
  } 

  if (r){  
    if ((email.value.length != 0) && ((email.value.indexOf("@") < 1) || (email.value.indexOf('.') < 7))){
	  chamadaErro(htmlAlerta('informe um e-mail v&aacute;lido'), campo_id, div_id);		
      r=false;
    }
  }

  return  r;
}

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

//VALIDAR CADASTRO NEWSLETTER
function validar_cadastro(id) {
  cp_nome		    = document.getElementById("cadastro_nome");
  cp_email 		    = document.getElementById("cadastro_email"); 
  r=true;	
  
  if(id=='cadastro'){
    cp_parametro      = 'Loja Virtual';	  
  }
  
  if(id=='cadastro_Sites'){
    cp_parametro      = 'Criação de Sites';	  
  }
  
    // 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 (!isValidMail(cp_email, "div_msg", "cadastro_email")){
      r=false;				  		  
    }}	

    if (r){
      //HTML DE CONCLUSSÃO PARA SER EXIBIDO NO TÉRMINO DA OPERAÇÃO
	  cp_html = ' ' +
       		    '<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>';  	
      enviar(id, "div_msg", cp_html, cp_parametro);
    }	 
  
}

//VALIDAR INDIQUE AO AMIGO
function validar_indique_loja(id) {
  cp_nome		    = document.getElementById("indique_nome");
  cp_amigo 		    = document.getElementById("indique_amigo"); 
  cp_email 		    = document.getElementById("indique_email");  
  cp_email_amigo    = document.getElementById("indique_email_amigo");       
  r=true;	  
  
  if(id=='indique_LojaVirtual'){
    cp_parametro      = 'Loja Virtual';	
  }
  
  if(id=='indique_Sites'){
    cp_parametro      = 'Criação de Sites';	
  }  
	  
    // INDIQUE: NOME 
    if ((cp_nome.value.length <= "2")) {		
	  chamadaErro(htmlAlerta('Informe o seu nome'), "indique_nome", "div_msg_indique");
      r=false;				  		  
    }
	
	
    // INDIQUE: EMAIL 	
    if (r){ 
    if (!isValidMail(cp_email, "div_msg_indique", "indique_email")){		
      r=false;				  		  
    }}	
	
	// INDIQUE: AMIGO NOME 
	if (r){ 	
    if ((cp_amigo.value.length <= "2")) {		
	  chamadaErro(htmlAlerta('Informe o nome do seu amigo'), "indique_amigo", "div_msg_indique");
      r=false;				  		  
    }}		

	
    // INDIQUE: AMIGO EMAIL 	
    if (r){ 
      if (!isValidMail(cp_email_amigo, "div_msg_indique", "indique_email_amigo")){	
	    r=false;				  		  
    }}		
	


    if (r){
      //HTML DE CONCLUSSÃO PARA SER EXIBIDO NO TÉRMINO DA OPERAÇÃO
	  cp_html = ' ' +
  
													  '<div class="texto_img">' +
													  ' </div>' +
													  ' <span class="texto_data">' +
													  '   Obrigado por indicar! ' +
													  ' </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 indica&ccedil;&atilde;o foi processada com sucesso<br><br><a href="javascript:novoIndique();">Clique aqui para indicar outro amigo</a>' +
													  ' </span> </div>';  	
													
      enviar(id, "div_msg_indique", cp_html, cp_parametro);		
    }	 
  
}