/******************************************* 
 this object handles the all request 
 related to the newsletter

 Created: December, 9th 2008                 
 Modified: December, 17th 2008               
 Author: C.A. Nouta                          

 Copyright (c) 2007 Sanware. 
 All rights reserved.
 *******************************************/

var xmlHttp;

/**
 * urlencode
 * this function is used to prepare a string for URL use
 * @param inp - the input string
 * @return the url encoded input
 **/    
function urlencode( inp )
{
  var encodedInputString=escape(inp);
  encodedInputString=encodedInputString.replace("+", "%2B");
  encodedInputString=encodedInputString.replace("/", "%2F"); 
  return encodedInputString;
}

/**
 * subscribeToNewsletter
 * this function is used to send the information from the form to the server
 * @param classname - the classname to check if a checkbox is selected
 **/ 
function subscribeToNewsletter( base_edition, url, checked_src )
{
  /* create a XmlHttp object */  
  xmlHttp = getXmlHttpObject();
  if (xmlHttp == null)
  {
    alert ("Foutmelding: uw browser biedt geen ondersteuning voor XMLHTTP Requests!");
    return false;
  }
  
  /* load the information from the form */
  var editions = base_edition;
  var chkbxs_elmns = new Array('chk_jacht', 'chk_jachthonden', 'chk_paardrijden');
  var editions_ids = new Array('1102','1103','1101');
  for ( i=0; i < chkbxs_elmns.length; i++ )
  {
    var elm_ref = MM_findObj(chkbxs_elmns[i]);
    if ( elm_ref && elm_ref.src == checked_src )
      editions = editions + "," + editions_ids[i];
  }
   
  /* load the emailaddress */
  var emailaddress;
  email_elm = MM_findObj('NewsletterEmailaddress');
  emailaddress = email_elm.value;
    
  /* send the request to the server */
  xmlHttp.onreadystatechange = subscribeToNewsLetterResult;
  xmlHttp.open("POST", url, true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  xmlHttp.send( "emailaddress=" + urlencode(emailaddress) + "&editions=" + urlencode(editions) );   
  
  /* do not send the form */
  return false;
}

/**
 * subscribeToNewsLetterResult
 * this function is used to anticipate on the result of the subscribe request 
 **/ 
function subscribeToNewsLetterResult()
{
  /* check if the request is completed */
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    
	/* load the response code */
    var response_msg = xmlHttp.responseText;
                
    if ( response_msg.substr(0,2) == "OK" ) {
      
	  /* change the container */
	  var container = MM_findObj('NewsletterForm');
      container.style.display = 'none';	
      var container = MM_findObj('NewsletterSucces');
      container.style.display = 'block';
    
	} else {
      
	  /* show an error */
      alert( NewsletterSubscriptionErrors[parseInt(response_msg.substr(4))] );
    }  
  }
}

/**
 * unsubscribeFromNewsletter
 * this function is used to unsubscribe a visitor from the newsletter
 **/ 
function unsubscribeFromNewsletter(url)
{
  /* create a XmlHttp object */  
  xmlHttp = getXmlHttpObject();
  if (xmlHttp == null)
  {
    alert ("Foutmelding: uw browser biedt geen ondersteuning voor XMLHTTP Requests!");
    return false;
  }
   
  /* load the emailaddress */
  var emailaddress;
  email_elm = MM_findObj('Unsubscribe_NewsletterEmailaddress');
  emailaddress = email_elm.value;
    
  /* send the request to the server */
  xmlHttp.onreadystatechange = unsubscribeFromNewsLetterResult;
  xmlHttp.open("POST", url, true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  xmlHttp.send( "emailaddress=" + urlencode(emailaddress) );   
  return false;
}

/**
 * unsubscribeFromNewsLetterResult
 * this function is used to anticipate on the result of the unsubscribe request 
 **/ 
function unsubscribeFromNewsLetterResult()
{  
  /* check if the request is completed */
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
	/* load the response code */
    var response_msg = xmlHttp.responseText;
                
    if ( response_msg.substr(0,2) == "OK" ) {
	  /* change the container */
	  var container = MM_findObj('UnSubscriptionForm');
      container.style.display = 'none';	
      var container = MM_findObj('UnSubscriptionSucces');
      container.style.display = 'block';
    }
    else {
	  /* show an error */
      alert( NewsletterUnSubscriptionErrors[parseInt(response_msg.substr(4))] );
    }  
  }
}

/**
 * changeStateOfNewsEditions
 * this functions swaps the state of the news editions checkboxes
 * @param elm - the current checkbox elm
 **/
function changeStateOfNewsEditions( elm )
{
  if ( !elm.checked )
  {
    var chkbxs_elmns = new Array('CRecvNewsJacht', 'CRecvNewsJachthonden', 'CRecvNewsPaardrijden');
    for ( i=0; i < chkbxs_elmns.length; i++ )
    {
      var elm_ref = MM_findObj(chkbxs_elmns[i]);
      elm_ref.checked = false;
    } 
  }
} 

/**
 * checkGeneralNewsletter
 * the general newsletter must be checked if one of the specific editions is checked
 **/
function checkGeneralNewsletter()
{ 
  /* check every edtion */
  var chkbxs_elmns = new Array('CRecvNewsJacht', 'CRecvNewsJachthonden', 'CRecvNewsPaardrijden');
  for ( i=0; i < chkbxs_elmns.length; i++ )
  {
    var elm_ref = MM_findObj(chkbxs_elmns[i]);
    if ( elm_ref.checked )
    {
      /* add the general newsletter */
      var elm_general = MM_findObj("CRecvNews");
      elm_general.checked = true;
      
      /* close the loop */
      break;
    }
  }
}
  
