/************************************************************************************************************
Ajax poller
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	

var serverSideFile = 'dcp_citations_cast_vote.php';
var serverSideFile2 = 'citation_latine_cast_vote.php';
var useCookiesToRememberCastedVotes = true;	// Use cookie to remember casted votes

var ajaxObjects = new Array();
var pollVotes = new Array();
var pollVoteCounted = new Array();
var totalVotes = new Array();

/*
These cookie functions are downloaded from 
http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
*/	
function Poller_Get_Cookie(name) { 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 
// This function has been slightly modified
function Poller_Set_Cookie(name,value,expires,path,domain,secure) { 
	expires = expires * 60*60*24*1000;
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
}

function setPercentageVotes(pollId)
{
	for(var prop in pollVotes[pollId]){
		pollVotes[pollId][prop] =  Math.round( (pollVotes[pollId][prop] / totalVotes[pollId]) * 100);				
	}	
	
	var currentSum = 0;
	for(var prop in pollVotes[pollId]){
		currentSum = currentSum + pollVotes[pollId][prop]/1;			
	}
	pollVotes[pollId][prop] = pollVotes[pollId][prop] + (100-currentSum);
}

function prepareForPollResults(id_exp_latine)
{
	document.getElementById('LienVote' + id_exp_latine).style.display='none';	
}

function castMyVote(cit_id)
{	
	//var elements = formObj.elements['vote[' + cit_id + ']'];
	//var optionId = false;
	//for(var no=0;no<elements.length;no++){
	//	if(elements[no].checked)optionId = elements[no].value;
	//}
	Poller_Set_Cookie('vote_abclettres_' + cit_id,'1',6000000);
	//if(optionId){
	
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?cit_id=' + cit_id;
		prepareForPollResults(cit_id);
		//ajaxObjects[ajaxIndex].onCompletion = function(){};	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
	//}	
}

function castMyVoteCitationLatine(id_exp_latine)
{	
	//var elements = formObj.elements['vote[' + id_exp_latine + ']'];
	//var optionId = false;
	//for(var no=0;no<elements.length;no++){
	//	if(elements[no].checked)optionId = elements[no].value;
	//}
	Poller_Set_Cookie('vote_abclettres_' + id_exp_latine,'1',6000000);
	//if(optionId){
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = serverSideFile2 + '?id_exp_latine=' + id_exp_latine;
		prepareForPollResults(id_exp_latine);
		//ajaxObjects[ajaxIndex].onCompletion = function(){};	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
	//}	
}