//----------------------------------------------------------------------------------------------------------------------
/**
 * This file contains the {@link BlueMoonIt_Newsletter_Class} class
 *
 * @author      Blue Moon IT <info@bluemoonit.net>
 * @copyright   2008, Blue Moon IT
 * @package     BlueMoonIt--NewsletterModule
 * @subpackage  Javascript
 * @version     SVN: $Id: Newsletter.js 511 2009-02-26 15:20:49Z johan $
 */
//----------------------------------------------------------------------------------------------------------------------
/**
 * Class to represent general functionality for NewsLetter module AJAX calls
 *
 * @package     BlueMoonIt--NewsletterModule
 * @subpackage  Javascript
 * @author      Johan B.W. de Vries <johan@bluemoonit.net>
 */
BlueMoonIt_Newsletter_Class = function()
{
	//------------------------------------------------
	/**
	 * Sends a synchronous POST call to subscribe the user to the given newsletter
	 *
	 * Shows a popup to tell the user the result
	 *
	 * @param  integer  in_newsletterId  The identity of the newsletter
	 * @param  string   in_language      The language of the newsletter translation
	 *
	 * @pre  User is logged in
	 * @return  void
	 */
	this.subscribeUser = function(in_newsletterId, in_language)
	{
		var loader = dhtmlxAjax.postSync(
			BlueMoonIt_App.basePath + 'newsletter/subscriptions/user-ajax/',
			'newsletter_subscriptions[' + in_newsletterId + '][' + in_language + ']=1'
		);

		if( null == loader.xmlDoc.responseXML ) {
			alert(loader.xmlDoc.responseText);
			return;
		}

		var result = BlueMoonIt_App.parseResult(loader);

		alert(result.errorMessage);
	}
	//------------------------------------------------
	/**
	 * Sends a synchronous POST call to unsubscribe the user from the given newsletter
	 *
	 * Shows a popup to tell the user the result
	 *
	 * @param  integer  in_newsletterId  The identity of the newsletter
	 * @param  string   in_language      The language of the newsletter translation
	 *
	 * @pre  User is logged in
	 * @return  void
	 */
	this.unsubscribeUser = function(in_newsletterId, in_language)
	{
		var loader = dhtmlxAjax.postSync(
			BlueMoonIt_App.basePath + 'newsletter/subscriptions/user-ajax/',
			'newsletter_subscriptions[' + in_newsletterId + '][' + in_language + ']=0'
		);

		if( null == loader.xmlDoc.responseXML ) {
			alert(loader.xmlDoc.responseText);
			return;
		}

		var result = BlueMoonIt_App.parseResult(loader);

		alert(result.errorMessage);
	}
	//------------------------------------------------
	/**
	 * Sends a synchronous POST call to subscribe the visitor to the given newsletter
	 *
	 * Shows a popup to tell the visitor the result
	 *
	 * @param  integer  in_newsletterId  The identity of the newsletter
	 * @param  string   in_language      The language of the newsletter translation
	 * @param  string   in_email         The email address of the visitor
	 *
	 * @pre  User is not logged in
	 * @return  void
	 */
	this.subscribeVisitor = function(in_newsletterId, in_language, in_email)
	{
		var loader = dhtmlxAjax.postSync(
			BlueMoonIt_App.basePath + 'newsletter/subscriptions/visitor-ajax/',
			'email=' + encodeURIComponent(in_email)
			  + '&newsletter_subscriptions[' + in_newsletterId + '][' + in_language + ']=1'
		);

		if( null == loader.xmlDoc.responseXML ) {
			alert(loader.xmlDoc.responseText);
			return;
		}

		var result = BlueMoonIt_App.parseResult(loader);

		alert(result.errorMessage);
	}
	//------------------------------------------------
	/**
	 * Sends a synchronous POST call to unsubscribe the visitor from the given newsletter
	 *
	 * Shows a popup to tell the visitor the result
	 *
	 * @param  integer  in_newsletterId  The identity of the newsletter
	 * @param  string   in_language      The language of the newsletter translation
	 * @param  string   in_email         The email address of the visitor
	 *
	 * @pre  User is not logged in
	 * @return  void
	 */
	this.unsubscribeVisitor = function(in_newsletterId, in_language, in_email)
	{
		var loader = dhtmlxAjax.postSync(
			BlueMoonIt_App.basePath + 'newsletter/subscriptions/visitor-ajax/',
			'email=' + encodeURIComponent(in_email)
			  + '&newsletter_subscriptions[' + in_newsletterId + '][' + in_language + ']=0'
		);

		if( null == loader.xmlDoc.responseXML ) {
			alert(loader.xmlDoc.responseText);
			return;
		}

		var result = BlueMoonIt_App.parseResult(loader);

		alert(result.errorMessage);
	}
	//------------------------------------------------
}
//----------------------------------------------------------------------------------------------------------------------
/**
 * Create the instance of the BlueMoonIt_App class
 */
BlueMoonIt_Newsletter = new BlueMoonIt_Newsletter_Class;
//----------------------------------------------------------------------------------------------------------------------
