/**
 * JavaScript handler for the Win Stuff form in single page
 * @author Charles Peter
 * @email vmxbmx@yahoo.com
 */
;(function($) {
	
	$.WinStuffClass = function() {
		this.construct();
	};
	
	$.extend($.WinStuffClass.prototype, {
		construct: function() {
			if (!$('#win-stuffs-form')) {
				return false;
			}
			
			$('#win-stuff-submit').click(function() {
				
				var i = 0;
				var errmsg = '';
				var qmsg = '';
				var questions = [];
				
				$('.ws-question').each(function(i) {
					
					questions[i] = {};
					questions[i].question = '';
					questions[i].answer   = '';
					questions[i].label    = '';
					
					if ( $(this).parents('li').find('input[type="radio"]').length ) {
						if (  $(this).parents('li').find('input[type="radio"]:checked').length == 0 ) {
							qmsg += $.trim($(this).html()) + "\n";
						}
						else {
							questions[i].question = $.trim($(this).html());
							questions[i].answer   = $.trim( $(this).parents('li').find('input[type="radio"]:checked').val() );
							questions[i].label    = 'radio';
						}
					}
					
					if ( $(this).parents('li').find('input[type="checkbox"]').length ) {
						if (  $(this).parents('li').find('input[type="checkbox"]:checked').length == 0 ) {
							qmsg += $.trim($(this).html()) + "\n";
						}
						else {
							var q = $.trim($(this).html());
							questions[i].answer = [];
							
							$(this).parents('li').find('input[type="checkbox"]:checked').each(function(c) {
								questions[i].question = q;
								questions[i].answer[c]   = $.trim($(this).val());
								questions[i].label    = 'checkbox';
							});
						}
					}
					
					if ( $(this).parents('li').find('textarea').length ) {
						if (  $.trim($(this).parents('li').find('textarea').val()) == '' ) {
							qmsg += $.trim($(this).html()) + "\n";
						}
						else {
							questions[i].question = $.trim($(this).html());
							questions[i].answer   = $.trim( $(this).parents('li').find('textarea').val() );
							questions[i].label    = 'textarea';
						}
					}
					
					if ( $(this).parents('li').find('input[type="text"]').length ) {
						if (  $.trim($(this).parents('li').find('input[type="text"]').val()) == '' ) {
							qmsg += $.trim($(this).html()) + "\n";
						}
						else {
							questions[i].question = $.trim($(this).html());
							questions[i].answer   = $.trim( $(this).parents('li').find('input[type="text"]').val() );
							questions[i].label    = 'text';
						}
					}
				});
				
				var post_ID  = $.trim($('#post_ID').val());
				var fullname = $.trim($('#win_stuff_fullname').val());
				var email    = $.trim($('#win_stuff_email').val());
				var contact  = $.trim($('#win_stuff_contact').val());
				
				if (!post_ID)  errmsg += (++i) + '. post_ID is missing.\n';
				if (!fullname) errmsg += (++i) + '. Full Name is required.\n';
				if (!email)    errmsg += (++i) + '. Email is required.\n';
				if (!contact)  errmsg += (++i) + '. Contact is required.\n';
				
				if (qmsg) {
					errmsg += (++i) + '. You haven\'t answered these questions:\n\n' + qmsg;
				}
				
				if (errmsg) {
					alert(errmsg);
					return false;
				}
				
				var jform = JSON.stringify(questions);
				var params = {
						post_ID   : post_ID,
						fullname  : encodeURIComponent(fullname),
						email     : encodeURIComponent(email),
						contact   : encodeURIComponent(contact),
						jform     : encodeURIComponent(jform),
						action    : 'win_stuffs_form_process'
				};
				
				$('#win-stuff-submit').attr('disabled', true);
				$('#win-stuff-submit').val('Please wait...');
				
				$.post(location.href, params, function(resp) {
					if (resp) {
						if ( resp.err == 0 ) {
							document.getElementById('winstuff_messages').innerHTML = '<div class="thankyou">Thank you for entering our competition.\n Keep checking back because we\'re always giving away great prizes!</div>';
							//alert('Thank you for entering our competition.\n Keep checking back because we\'re always giving away great prizes!');
							//location.href = location.href;
						}
						else {
							//alert(resp.errmsg);
							document.getElementById('winstuff_messages').innerHTML = '<div class="ouups">'+resp.errmsg+'</div>';
						}
						
						$('#win-stuff-submit').attr('disabled', false);
						$('#win-stuff-submit').val('Submit');
					}
				}, 'json');
				
				return false;
			});
			
			return false;
		}
	});
	
	$(document).ready(function() {
		if (typeof $.WinStuff === 'undefined') {
			$.WinStuff = new $.WinStuffClass();
		}
	});
	
})(jQuery);
