$(document).ready(function()
		{
			$('#asked').blur(function()
					{
						var poll_q = jQuery.trim($('#asked').attr('value'));
						
						var poll_q = poll_q.replace(/[\W]+/g, ' ');

						
						if (poll_q != '' && poll_q.length > 4)
						{
							var poll_split = poll_q.split(' ');
							var clean_query_string = poll_split[0];
							for (i = 1; i < poll_split.length; i++)
							{
								clean_query_string = clean_query_string + '_-_' + poll_split[i];
							}
							$.getJSON('/index.php/poll/find_related/words/' + clean_query_string, function(data)
									{
										if (data.results)
										{
											$('#suggestion').empty();
											$('#suggestion').append('<h3>Related Polls</h3>');
											$('#suggestion').append('<p>Maybe someone already created your poll?</p>');
											$('#suggestion').append('<ul id="suggestionlist">');
											$.each(data.results, function(i, result)
													{
														$('#suggestionlist').append('<li><a href="http://pollshare.myisay.com/index.php/show/poll/id/' + result[0].id + '" target="_blank">' + result[0].question + '</a></li>');
														$('#suggestionlist').append('<li>Created by ' + result[0].author + ' on ' + result[0].date + '</li>');
														$('#suggestionlist').append('<li>&nbsp;</li>');
	
													}); 
											$('#suggestion').append('</ul>');
										}
										else
										{
											$('#suggestion').empty();
											$('#suggestion').append('<h3>Related Polls</h3>');
											$('#suggestion').append('<p>We couldn\'t find any similar polls.  Continue filling out the form!</p>');
										}
									});
							$('#suggestion').show();
						}
					});
	
	
		});

function checkForm(form)
{

	// Check the length of the question
	var question = jQuery.trim(form.asked.value);
	if (question == '')
	{
		alert('You need to include a poll question.');
		return false;
	}
	else if (question.length < 5)
	{
		alert('Your question is too short.');
		return false;
	}
	else if (question.length > 150)
	{
		alert('Your question is too short');
		return false;
	}


	var answer_one = jQuery.trim(form.question_1.value);
	var answer_two = jQuery.trim(form.question_2.value);
	
	if (answer_one == '' || answer_two == '')
	{
		alert('You need to include at least the first two answers');
		return false;
	}
	
	var email = jQuery.trim(form.email.value);
	var email2 = jQuery.trim(form.email2.value);
	if (email == '')
	{
		alert('You need to include a valid email address');
		return false;
	}
	else
	{
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(email))
		{
			alert('You need to include a valid email address');
			return false;
		}
		else if (email != email2)
		{
			alert('Email addresses must match.');
			return false;
		}
	}
	

	return true;
	

}