// Add the jQuery for forms
$(document).ready(function()
{
	$('#custom_form').submit(function() 
	{
		var serialized = $(this).serialize();

		// Get the required captcha response
		if (!isEmpty(serialized))
		{
			var obj = {sessid: $('#sessid').val()};
			$.get('../site_resources/php/getRequiredCaptchaValue.php', obj, function(response)
			{
				if (!isEmpty(response))
				{
					// Check for invalid captcha user input
					if ($('#captcha-question').val() != response)
					{
						alert('Invalid captcha response');
						return false;
					}
				
					// Submit
					submitData(obj, serialized);
				}
				else
				{
					// Submit
					submitData(obj, serialized);
				}
			});
		}
		else
		{
			// Check to see if processing subscriptions
			if ($('#confirmation').is(':hidden'))
			{
				$('#subForm').hide();
				$('#confirmation').show();
			}
		}
		return false;
	});
	
	var getVars = function(serialized)
	{
		var data = new Array;
		var posts = String(serialized).split('&');
		for (i = 0; i < posts.length; i++)
		{
			data.push((posts[i]).split('='));
		}
		return data;
	}
	
	function isEmpty(variable)
	{
		if (typeof(variable) == 'undefined' || variable == '' || variable == null || !(variable) || variable == 'null')
		{
			return true;
		}
		return false;
	}
	
	function submitData(obj, serialized)
	{
		var $vars = serialized;
	
		// Submit the form data
		$.post('../site_resources/php/formProcessor.php', {vars: $vars}, function(response)
		{
			if (!isEmpty(response))
			{
				alert(response);
				return false;
			}
		});
	
		// Get the form response
		$.get('../site_resources/php/getFormResponse.php', obj, function(response)
		{
			// Add the response text
			$('#custom_form #response-text').html(response);
		
			// Slide the response text out of view in 3 seconds
			setTimeout("$('#custom_form #response-text').slideUp()", 3000);
		});
	}
});

