/**
 * JavaScript handler for page Ford Club
 * @author Chemsi
 * @email clarbi@arnonline.com
 */
;(function($) {
	
	$.PosseClass = function() {
		this.construct();
	};
	
	$.extend($.PosseClass.prototype, {
		construct: function() {
			this.validate();
		},
               
        email: function(value) {
			return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
		},
		
		validate: function() {
			tClass = this;
			$('#registration_form_submission').unbind().click(function() {
																	   
				var fname               = $.trim($('#fname').val());
                var lname               = $.trim($('#lname').val());
                var nationality         = $.trim($('#nationality').val());
                var gender              = $.trim($('input[name="gender"]:checked').val());
                var job_title 			= $.trim($('#job_title').val()); //profession
                var area_of_residence   = $.trim($('#area_of_residence').val());
                var email 				= $.trim($('#email').val());
                var mobile				= $.trim($('#mobile').val());
				var age_group 			= $.trim($('#age_group').val());
				var privacy 			= $.trim($('input[name="privacy"]:checked').val());
				var recaptcha_challenge_field = $.trim($('#recaptcha_challenge_field').val());
				var recaptcha_response_field  = $.trim($('#recaptcha_response_field').val());
				
				var i = 0;
				var errmsg = '';
				
				if (!fname)         errmsg += (++i) + '. First Name is required.'                    + "\n";
				if (!lname)         errmsg += (++i) + '. Last Name is required.'                     + "\n";
				if (nationality==0) errmsg += (++i) + '. Nationality is required.'                   + "\n";
				if (!gender)        errmsg += (++i) + '. Gender is required.'                        + "\n";
				if (!mobile)        errmsg += (++i) + '. Mobile is required.'                        + "\n";
				if (!email)         errmsg += (++i) + '. Email is required.'                         + "\n";
				if (!privacy)       errmsg += (++i) + '. Please confirm reading the privacy policy.' + "\n";
                   
				if (!recaptcha_challenge_field) errmsg += (++i) + '. reCaptcha: Challenge words is missing. Refresh page?' + "\n";
				if (!recaptcha_response_field)  errmsg += (++i) + '. reCaptcha: Type the two words.'                       + "\n";
				if (!tClass.email(email)) errmsg += (++i) + '. Email is invalid. Please verify your email address'   + "\n";
				
				if (errmsg) {
					alert(errmsg);
					return false;
				}
				
				var params = {
						fname                     : encodeURIComponent(fname),
						lname                     : encodeURIComponent(lname),
						nationality               : encodeURIComponent(nationality),
						gender                    : encodeURIComponent(gender),
						job_title                 : encodeURIComponent(job_title),
						area_of_residence         : encodeURIComponent(area_of_residence),
						email                     : encodeURIComponent(email),
						mobile                    : encodeURIComponent(mobile),
						age_group                 : encodeURIComponent(age_group),
						recaptcha_challenge_field : encodeURIComponent(recaptcha_challenge_field),
						recaptcha_response_field  : encodeURIComponent(recaptcha_response_field),
						action                    : 'cityzen_signup_process'
				};
				
				$.post(location.href, params, function(resp) {
					if (resp) {
						if (resp.err == 0) {
							alert("Congratulations, you are now a Cityzen!\nThank you");
							location.href = location.href;
						}
						else {
							alert(resp.errmsg);
							if (resp.recaptcha == 1) {
								location.href = location.href;
							}
						}
					}
				}, 'json');
				
				return false;
			});
		}
	});
	
	$(document).ready(function() {
		if (typeof $.Posse === 'undefined') {
			$.Posse = new $.PosseClass();
		}
	});
	
})(jQuery);
