/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/


var serverSideFile = '/journal/includes/vote/cast_vote.php';

var useCookiesToRememberCastedVotes = true;	// Use cookie to remember casted votes
var txt_totalVotes = 'Total 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 showVoteResults(pollId,ajaxIndex,optionId)
{

	document.getElementById('poller_waitMessage').style.display='none';

	var xml = ajaxObjects[ajaxIndex].response;
	xml = xml.replace(/\n/gi,'');

	var reg = new RegExp("^.*?<pollerTitle>(.*?)<.*$","gi");
	var pollerTitle = xml.replace(reg,'$1');

	var resultDiv = document.getElementById('poller_results');

	var options = xml.split(/<option>/gi);

	pollVotes[pollId] = new Array();
	totalVotes[pollId] = 0;
	for(var no=1;no<options.length;no++){

		var elements = options[no].split(/</gi);
		var currentOptionId = false;
		for(var no2=0;no2<elements.length;no2++){
			if(elements[no2].substring(0,1)!='/'){
				var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
				var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');

				if(key.indexOf('optionText')>=0){
					var pOption = document.createElement('P');
					pOption.className='result_pollerOption';
					pOption.innerHTML = value;
					resultDiv.appendChild(pOption);

				}

				if(key.indexOf('optionId')>=0){
					currentOptionId = value/1;
				}

				if(key.indexOf('votes')>=0){
					pollVotes[pollId][currentOptionId] = value;
					totalVotes[pollId] = totalVotes[pollId]/1 + value/1;
				}
			}
		}
	}

    yesVotes = parseInt(pollVotes[pollId][1]);
	noVotes = parseInt(pollVotes[pollId][2]);


    outOfFive = (yesVotes / (yesVotes + noVotes)) * 5;
    outOfFive = Math.round(outOfFive);


    //resultDiv.innerHTML = pollVotes[pollId][1];
	//resultDiv.innerHTML += pollVotes[pollId][2];
	//resultDiv.innerHTML += pollVotes[pollId][1] + pollVotes[pollId][2]

    resultStars = '';

	for (i=0;i<outOfFive;i++) {
   		resultStars += '<img src="/journal/partner_pages/statravel/images/star.gif" alt="Star">';
	}

	for (j=i;j<5;j++) {
    	resultStars += '<img src="/journal/partner_pages/statravel/images/empty_star.gif" alt="Empty Star">';
	}

    if (optionId === undefined) {
		resultDiv.innerHTML = '<span id="poller_rate"></span>';
	}

	else {

		if (optionId == 1) {
        	yesNo = 'yes';
		}

		else {
        	yesNo = 'no';
		}

		//resultDiv.innerHTML = '<span id="poller_rate">You voted<img src="/journal/partner_pages/statravel/images/vote_' + optionId +  '.gif" alt=" ' + optionId + '"></span>';

		resultDiv.innerHTML = '<span id="poller_rate">You voted ' + yesNo +  '.</span>';
	}

	resultDiv.innerHTML += '<span id="poller_current">Journal rating: ' + resultStars + '</span><br class="clear">';

    //resultDiv.innerHTML += '</span>';

	/*var totalVoteP = document.createElement('p');
	totalVoteP.className = 'result_totalVotes';
	totalVoteP.innerHTML = txt_totalVotes + totalVotes[pollId];
	resultDiv.appendChild(totalVoteP);*/
}

function prepareForPollResults(pollId)
{
	document.getElementById('poller_waitMessage').style.display='block';
	document.getElementById('poller_question').style.display='none';
}

function castMyVote(pollId,formObj,optionId) {
	var elements = formObj.elements['vote[' + pollId + ']'];
	/*var optionId = false;
	for(var no=0;no<elements.length;no++){
		if(elements[no].checked)optionId = elements[no].value;
	} */
	Poller_Set_Cookie('dhtmlgoodies_poller_' + pollId,'1',6000000);
	if(optionId){

		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId + '&optionId=' + optionId;
		prepareForPollResults(pollId);
		ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex,optionId); };	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

	}
}

/*function castMyVote(pollId,formObj)
{
	var elements = formObj.elements['vote[' + pollId + ']'];
	var optionId = false;
	for(var no=0;no<elements.length;no++){
		if(elements[no].checked)optionId = elements[no].value;
	}
	Poller_Set_Cookie('dhtmlgoodies_poller_' + pollId,'1',6000000);
	if(optionId){

		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId + '&optionId=' + optionId;
		prepareForPollResults(pollId);
		ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); };	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

	}	
}*/

function displayResultsWithoutVoting(pollId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId;
	prepareForPollResults(pollId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

}

function displayCurrentRating(pollId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId;
	//prepareForPollResults(pollId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showCurrentResults(pollId,ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

}

function showCurrentResults(pollId,ajaxIndex)
{

	var xml = ajaxObjects[ajaxIndex].response;
	xml = xml.replace(/\n/gi,'');

	var reg = new RegExp("^.*?<pollerTitle>(.*?)<.*$","gi");
	var pollerTitle = xml.replace(reg,'$1');

	var currentDiv = document.getElementById('poller_current');

	var options = xml.split(/<option>/gi);

	pollVotes[pollId] = new Array();
	totalVotes[pollId] = 0;
	for(var no=1;no<options.length;no++){

		var elements = options[no].split(/</gi);
		var currentOptionId = false;
		for(var no2=0;no2<elements.length;no2++){
			if(elements[no2].substring(0,1)!='/'){
				var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
				var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');

				if(key.indexOf('optionText')>=0){
					var pOption = document.createElement('P');
					pOption.className='result_pollerOption';
					pOption.innerHTML = value;
					currentDiv.appendChild(pOption);

				}

				if(key.indexOf('optionId')>=0){
					currentOptionId = value/1;
				}

				if(key.indexOf('votes')>=0){
					pollVotes[pollId][currentOptionId] = value;
					totalVotes[pollId] = totalVotes[pollId]/1 + value/1;
				}
			}
		}
	}

    yesVotes = parseInt(pollVotes[pollId][1]);
	noVotes = parseInt(pollVotes[pollId][2]);

	if ((yesVotes + noVotes) == 0) {
    	currentDiv.innerHTML = 'This entry has not been rated';
	}

	else {
	    outOfFive = (yesVotes / (yesVotes + noVotes)) * 5;
	    outOfFive = Math.round(outOfFive);

		currentDiv.innerHTML = 'Current rating: ';

		for (i=0;i<outOfFive;i++) {
	    currentDiv.innerHTML += '<img src="/journal/partner_pages/statravel/images/star.gif" alt="Star">';
		}

		for (j=i;j<5;j++) {
	    currentDiv.innerHTML += '<img src="/journal/partner_pages/statravel/images/empty_star.gif" alt="Empty Star">';
		}
    }
    //show total votes
	/*	var totalVoteP = document.createElement('p');
	totalVoteP.className = 'result_totalVotes';
	totalVoteP.innerHTML = txt_totalVotes + totalVotes[pollId];
	currentDiv.appendChild(totalVoteP);*/
}
