
/////////////////////
// Newsletter scripts
//
function alphaChars(value) {
  var rvalue='';
  var c='';
  var n=0;
  var error;
  for (i=0; i<value.length; i++) {
    c = value.substring(i,i+1);
    if ('-_.'.indexOf(c)>=0) { rvalue+=c; }
    else {
      n = c.charCodeAt(0);
      // c in [a-z] [A-Z] [0-9]
      if (((n>=97)&&(n<=136))||(((n>=65)&&(n<=90))||((n>=48)&&(n<=57)))){
	rvalue+=c;
      }
    }
  }
  return rvalue;
}


function newsletterAction() {

  var email    = document.getElementById("emailAddress").value;
  var insertBt = document.getElementById("newsLetterSubscribe");

  // control email
  var emr = email.split('@');
  try{
    if (emr.length != 2) { stop; }
    var account = alphaChars(emr[0]);
    if (account != emr[0]) { stop; }
    var host = alphaChars(emr[1]);
    if (host != emr[1])  { stop; }
    if (host.indexOf('.')<0) { stop; }
  }
  catch(e) {
   alert('Adresse email non valide');
   return false;
  }

  // Server request
  var action;

  if (insertBt.checked) {
	  action='insert';
  } else {
	  action='remove';
  }

  var newsSend = 'newsletter.php?action='+action;
  var param    = '&emailAddress='+email ;
  var httpSendAddress = cad_getHTTPObject();


  if (httpSendAddress.readyState == 4 || httpSendAddress.readyState == 0) {
	  httpSendAddress.open("POST", newsSend, true);
	  httpSendAddress.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    httpSendAddress.send(param);

      httpSendAddress.onreadystatechange = function () {
        if (httpSendAddress.readyState == 4) {
            results  = httpSendAddress.responseText;
            alert(results);
            email = '';
            return false;
        }
      }
  }
 return false;
}


//initiates the XMLHttpRequest object
function cad_getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}