function pollLoader(container){
	var loader = '<div style="width:198px; padding-top:110px;padding-bottom:110px;background-color:#FFF;">';
	loader +='<img src="/images/ajax-loader.gif" style=" width:195px;">';
	loader +='</div>';
	$j('#'+container).html(loader);
}
function getPollResults(poll_id){
	pollLoader('poll_system');
	$j.ajax({
		url: getSiteRoot()+'/poll/fetch.php?action=post_result&id='+poll_id,
			cache: false,
			dataType: "html",
			success: function(data){
			$j('#poll_system').html(data);
		}
	});	
}

function getPoll(poll_id){
	pollLoader('poll_system');
	$j.ajax({
		url: getSiteRoot()+'/poll/fetch.php?action=showpoll&id='+poll_id,
			cache: false,
			dataType: "html",
			success: function(data){
			$j('#poll_system').html(data);
		}
	});	
}

function casteVote(poll_id){
	//Check if the vote has been casted
	var option  = document.getElementById("option").value;
	var $jfields = $j('#poll_system').find('input[name="note"]:checked');
	if (!$jfields.length){
		alert('Please select an option to vote for!');
		return false; 
	}
	//End of checking if the vote has bee casted
	pollLoader('poll_system');
	$j.ajax({
		url: getSiteRoot()+'/poll/fetch.php?action=post_result&id='+poll_id+'&option='+option,
			cache: false,
			dataType: "html",
			success: function(data){
			$j('#poll_system').html(data);
		}
	});	
}
function begen(vote){
	$j('#option').val(vote);
}

function getSiteRoot(){//Get the site toot url
	return document.getElementById("siteroot").innerHTML;
}

function validatePollCommentsForm(){
	if(document.getElementById("name").value == ""){ 
		document.getElementById("error").innerHTML="Please enter your name!";
		document.frm.name.focus();
		return false;
	}
	if(!document.getElementById("email").value.match(/^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+$/)){
		document.getElementById("error").innerHTML="Please enter your email!";
		document.frm.email.focus();
		return false;
	}
	if(document.getElementById("comment").value == ""){
		document.getElementById("error").innerHTML="Please enter comment!";
		document.frm.comment.focus();
		return false;
	}
	return true;
}

function savePollComments(){
	if(validatePollCommentsForm() == false){
		return false;
	}
	var name = document.getElementById("name").value;
	var email = document.getElementById("email").value;
	var comment = document.getElementById("comment").value;
	var pID = document.getElementById("pollID").value;
	$j("#error").html('<img src="/images/ajax-loader.gif">');
	$j.post(getSiteRoot()+"/poll/save-poll-comments.php", {name:name,email:email,comment:comment,pID:pID},function(data){
		if(data = "SAVED"){
			$j("#error").html('<span style="color:#000;">Thank You For commenting a poll<span>');
			document.getElementById("name").value = "";
			document.getElementById("email").value = "";
			document.getElementById("comment").value = "";
		}else{
			$j("#error").html('System error! Comments were not saved, please try again.');
		}
	});	
}

function pollRate(comID,comments,rate_type){//This function rates the poll comments
	$j("#"+comments).html('<img src="/images/ajax-loader.gif">');
	$j.post(getSiteRoot()+"/poll/poll-rate.php", {comID:comID,rate_type:rate_type},function(data){
		$j("#"+comments).html(data);
	});	
}

function getComments(pID,poll_comments_max_results,page,poll_comments_order){
	if(poll_comments_order=='Sort By'){
		return false;
	}
	$j("#comments").html('<div style="padding-left:120px; padding-top:60px;"><img src="/images/ajax-loader.gif"></div>');
	$j.post(getSiteRoot()+"/poll/get-comments.php", {pID:pID,poll_comments_max_results:poll_comments_max_results,page:page,poll_comments_order:poll_comments_order},function(data){
		$j("#comments").html(data);
	});	
}

