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

// Creating Namespace
var Service = {};

/**
 * Module: ContactSupport
 */
Service.ContactSupport = (function()  {
	function constructor(caller, options) {
		// Private members.
		function onSend() {
			var validName = jQuery('#supportForm #name').val();
			if (jQuery.trim(validName) == '') {
				jQuery('#supportForm #name').focus();
				alert('Please enter your first name.');
				return false;
			}

			var validEmail = Common.Validate.validateFormat(
				Common.Validate.FORMAT.EMAIL, jQuery('#supportForm #email').val());
			if (!validEmail) {
				jQuery('#supportForm #email').focus();
				alert('Please enter a valid E-mail address.');
				return false;
			}

			var account = jQuery('#supportForm #account').val();
			if (jQuery.trim(account) != '') {
				var validAccount = Common.Validate.validateFormat(
					Common.Validate.FORMAT.ACCOUNT, account);
				if (!validAccount) {
					jQuery('#supportForm #account').focus();
					alert('Account ID format error, please enter a valid Account ID.');
					return false;
				}
			}

			var manufacturer = jQuery('#supportForm #manufacturer');
			if (jQuery(manufacturer).val() == 'e.g. Acer') {
				jQuery(manufacturer).val('');
			}

			var validManufacturer = jQuery('#supportForm #manufacturer').val();
			if (jQuery.trim(validManufacturer) == '') {
				jQuery('#supportForm #manufacturer').focus();
				alert('Please enter your computer manufacturer.');
				return false;
			}

			var model = jQuery('#supportForm #model');
			if (jQuery(model).val() == 'e.g. Aspire 8920') {
				jQuery(model).val('');
			}

			var validModel = jQuery('#supportForm #model').val();
			if (jQuery.trim(validModel) == '') {
				jQuery('#supportForm #model').focus();
				alert('Please enter your machine model.');
				return false;
			}

			var validOs = jQuery('#supportForm #os').val();
			if (jQuery.trim(validOs) == '') {
				jQuery('#supportForm #os').focus();
				alert('Please select your computer operation system.');
				return false;
			}

			var validVersion = jQuery('#supportForm #esobiVersion').val();
			if (jQuery.trim(validVersion) == '') {
				jQuery('#supportForm #esobiVersion').focus();
				alert('Please select your eSobi version.');
				return false;
			}

			var versionNumber = jQuery('#supportForm #versionNumber');
			if (jQuery(versionNumber).val() != '') {
				var validNumber = Common.Validate.validateFormat(
					Common.Validate.FORMAT.VERSION, jQuery('#supportForm #versionNumber').val());
				if (!validNumber) {
					jQuery('#supportForm #versionNumber').focus();
					alert('Please enter a valid eSobi Version number,\ne.g. 2.5.3.0203.');
					return false;
				}
			}

			var validSubject = jQuery('#supportForm #subject').val();
			if (jQuery.trim(validSubject) == '') {
				jQuery('#supportForm #subject').focus();
				alert('Please select the inquiry type that best describes your question.');
				return false;
			}

			var subject = jQuery('#supportForm #subject').val();
			if (subject == 'Others') {
				var validOtherSubject = jQuery('#supportForm #otherSubject').val();
				if (jQuery.trim(validOtherSubject) == '') {
					jQuery('#supportForm #otherSubject').focus();
					alert('Please describe your inquiry type with one short sentence.');
					return false;
				}
			}

			var validQuestion = jQuery('#supportForm #question').val();
			if (jQuery.trim(validQuestion) == '') {
				jQuery('#supportForm #question').focus();
				alert('Please describe your question(s) in detail.');
				return false;
			}

			var validCaptcha = Common.Validate.validateFormat(
				Common.Validate.FORMAT.CAPTCHA, jQuery('#supportForm #captchaId').val());
			if (!validCaptcha) {
				jQuery('#supportForm #captchaId').focus();
				alert('Please enter the correct Secure ID you see in the image.');
				return false;
			}

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

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

		function switchSubjectType() {
			var selectedVal = jQuery(this).val();
			if (selectedVal != 'Others') {
				jQuery('#supportForm #extraSubject').empty();
			} else {
				var otherSubject = document.createElement("input");
				otherSubject.type = 'text';
				otherSubject.id = 'otherSubject';
				otherSubject.name = 'otherSubject';
				otherSubject.value = '';
				otherSubject.className = 'textField longField';
				jQuery('#extraSubject').append('type: ');
				jQuery('#extraSubject').append(otherSubject);
				jQuery(otherSubject).focus();
			}
		}

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

			if (Common.Utils.isObject(data)) {
				if (data.type == 'INFO') {
					var ignoredNames = new Array();
					ignoredNames.push('os');
					ignoredNames.push('esobiVersion');
					ignoredNames.push('subject');
					jQuery('#supportForm').clearForm(ignoredNames);
					alert(data.message);
				} else {
					// occur exception
					alert(data.message);
				}
			}
		}

		return { // Public members.
			initialize: function() {
				jQuery('#sbSupportLink').removeClass('sidebarNav').addClass('sidebarNavCur');
				jQuery('#supportForm #subject').change(switchSubjectType);
				jQuery('#supportForm #subject').trigger('change');

				var manufacturer = jQuery('#supportForm #manufacturer');
				jQuery(manufacturer).val('e.g. Acer');
				jQuery(manufacturer).focus(function() {
					if (this.value == 'e.g. Acer') {
						this.value = '';
					}
				});

				var model = jQuery('#supportForm #model');
				jQuery(model).val('e.g. Aspire 8920');
				jQuery(model).focus(function() {
					if (this.value == 'e.g. Aspire 8920') {
						this.value = '';
					}
				});

				jQuery('#supportForm').submit(onSend);
				jQuery('#supportForm #send').click(onSend);
				jQuery('#supportForm #name').focus();

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

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