$(document).ready(function(){
	$('form#sweepstakes_form').bind('submit',function(e){votePoll(e);});
});

votePoll = function(e){
	e.preventDefault();
	var numQuestions=1
	var pollID=122009;
	
	if(vreadCookie('petside_15_voted_today')){
		GATEWAY_MESSENGER.alertbox('You have already voted, come back tomorrow to vote again');
		setTimeout(function(){window.location='/BouncePets/thanks_for_vote.php';},3000);
	}
	else if($('input:radio:checked',e.target).length !== numQuestions){ //did not answer all the poll questions
		//find all the questions they missed
		GATEWAY_MESSENGER.alertbox('Please select one pet to vote for');
	}
	else{
		var data={};
		$('input:radio:checked',e.target).each(function(){
			var q=$(this).attr('name');
			var value=$(this).attr('value');
			data[q]=value;
		})
		data['action']='votePollByID';
		data['pollID']=pollID;
		$.ajax(setAjax({
			data:data,
			dataType: "json",
			processData: true,
			success: function(d){
				// GATEWAY_MESSENGER.alertbox('Thanks for voting');
				$('form#sweepstakes_form').unbind('submit');
				$('button[name=sweepstakes]').remove();
				vcreateCookie('petside_15_voted_today',1);
				window.location='/BouncePets/thanks_for_vote.php';
				}
		}));
	}
}


function vcreateCookie(name,value) {
	//if (days) {
		var date = new Date();
		date.setHours(23,59)
		date.setTime(date.getTime());
		var expires = "; expires="+date.toGMTString();
	//}
	//else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function vreadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}