$(document).ready(function(){
	
	$(".pollForm").each(function() {
		pollForm = this;
		var pollId = $(pollForm).attr('id');
		var formId = ".pollForm#" + pollId
		var pollFormMessageId = "td#pollFormMessage_" + pollId;
		
		var validator_formId = $(formId).validate({ 	
	      	messages: {
				pollOption: {
				required: "Please select an option"
				}
	        },
		    errorPlacement: function(error) {
				$(pollFormMessageId).html(error);
			},
			submitHandler: function(form) {
				
				showVotes(formId, pollId);
			}
		});
		
		$(formId).ajaxForm(function() {
		});
		
		
	});
	
	function showVotes(pollForm, pollId){
		command = $(pollForm + " input[name='command']").attr('value');
		language = $(pollForm + " input[name='language']").attr('value');
		lang_result = $(pollForm + " input[name='lang_result']").attr('value');
		lang_question = $(pollForm + " input[name='lang_question']").attr('value');
		window.setTimeout(function () {
	 		 $("#pollContent_" + pollId).load("/modules/poll/includes/ajaxExecuter.php",{language: language, pollId: pollId, pollFloat: 'right', lang_result: lang_result, lang_question: lang_question, command: 'showVotes'}, function () {
				$("#pollContent_" + pollId + " .results").fadeIn('slow');
				createCookie('voteCasted_' + pollId, pollId);
			});
		},100);
		
		return true;
	}
	
	
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	

 });
