// DOM ready event
jQuery(document).ready(function() {
	MemberServ.Forget.getInstance();
});

// Creating Namespace
var MemberServ = {};

/**
 * Module: Forget
 */
MemberServ.Forget = (function() {
	function constructor() {
		// Private members.
		function onSend() {
			var curLang = jQuery('#language').val();
			var validEmail = Common.Validate.validateFormat(
					Common.Validate.FORMAT.EMAIL, jQuery('#emailForm #email')
							.val());
			if (!validEmail) {
				jQuery('#emailForm #email').focus();
				Common.Utils.showMessage('MSG_ForgetEmailError', curLang);
				return false;
			}

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

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

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

		function afterSend(data, textStatus) {
			Common.Utils.log(Common.Utils.tostr(data, 2));

			if (Common.Utils.isObject(data)) {
				if (data.type == 'INFO') {
					alert(data.message);

					if (data.result) {
						window.location = '/member/login.jsp';
						return false;
					} else {
						jQuery('#emailForm').clearForm();
					}
				} else {
					// occur exception
					alert(data.message);
				}
			}
		}

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

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

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

})();