
// DOM ready event
jQuery(document).ready(function() {
	Service.Unsubscribe.getInstance();
});

// Creating Namespace
var Service = Service || {};

/**
 * Module: Unsubscribe
 */
Service.Unsubscribe = (function()  {
	function constructor(caller, options) {
		// Private members.
		function onUnsubscribe() {
			var curLang = jQuery('#language').val();
			if (jQuery.trim(curLang) == '') {
				curLang = "en";
			}

			var validEmail = Common.Validate.validateFormat(
				Common.Validate.FORMAT.EMAIL, jQuery('#email').val());
			if (!validEmail) {
				jQuery('#email').focus();
				Common.Utils.showMessage('MSG_UnsubscribeEmailError', curLang);
				return false;
			}

			var validCaptcha = Common.Validate.validateFormat(
				Common.Validate.FORMAT.CAPTCHA, jQuery('#captchaId').val());
			if (!validCaptcha) {
				jQuery('#captchaId').focus();
				Common.Utils.showMessage('MSG_UnsubscribeCaptchaIdError', curLang);
				return false;
			}

			var param = jQuery('#emailForm').serialize();
			Common.Utils.log("email form: \n" + Common.Utils.tostr(param));
			Common.AJAX.getInstance().getOnBlock("/member/unsubscribeEdm.call",
				param, afterUnsubscribe);

			// force browser ignore the another event at one time because this method binds the event 'onsubmit' and 'onclick'
			return false;
		}

		function afterUnsubscribe(data, textStatus) {
			if (data.type == 'INFO') {
				jQuery('#emailForm').clearForm();
				alert(data.message);
			} else {
				// occur exception
				alert(data.message);
			}
		}

		return { // Public members.
			initialize: function() {
				jQuery('#sbUnsubscribeLink').removeClass('sidebarNav').addClass('sidebarNavCur');
				jQuery('#emailForm').submit(onUnsubscribe);
				jQuery('#unsubscribe').click(onUnsubscribe);
				jQuery('#email').focus();

				Common.Utils.loadCaptcha();
			}
		};
	} // constructor end

	// Singleton control code
	var instance;
	return {
		getInstance: function()	{
			if(!instance) {
				instance = constructor();
				instance.initialize();
			}
			return instance;
		}
	}
})();


