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

// Creating Namespace
var Service = {};

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

			var lastName = jQuery('#prForm #lastName').val();
			if (jQuery.trim(lastName) == '') {
				jQuery('#prForm #lastName').focus();
				alert('Please enter your last name.');
				return false;
			}

			var phone = jQuery('#prForm #phone').val();
			if (jQuery.trim(phone) == '') {
				jQuery('#prForm #phone').focus();
				alert('Please enter your contact number.');
				return false;
			}

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

			var country = jQuery('#prForm #country').val();
			if (jQuery.trim(country) == '') {
				jQuery('#prForm #country').focus();
				alert('Please select the country you live in.');
				return false;
			}

			var validMedia = jQuery('#prForm #mediaTypes :radio:checked').length;
			if (validMedia <= 0) {
				jQuery('#prForm #publisher').focus();
				alert('Please select your career field.');
				return false;
			}

			var inquiry = jQuery('#prForm #inquiry').val();
			if (jQuery.trim(inquiry) == '') {
				jQuery('#prForm #inquiry').focus();
				alert('Please provide your inquiry.');
				return false;
			}

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

			var param = jQuery('#prForm').serialize();
			Common.Utils.log("prForm form: \n" + Common.Utils.tostr(param));
			Common.AJAX.getInstance().postOnBlock("/contact/contactPRCall.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));

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

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

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

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

