/* ----------------------------------------------------------------------------
	
	JavaScript Public Document
	Castanet Polling System
	/poll/support/poll_system.js
	
	Written November 2006 - Todd Cytra <tcytra@gmail.com>
	
	Usage:
	Called from /newNews/news_11.htm	(this file is cgi processed)
	
---------------------------------------------------------------------------- */


var myedition	= getCookie('edition');
var pollpage	= "poll.new.php";


/**
getCookie()
----------
Returns the edition id (integer) from user's cookies, else returns 0
----------------------------------------------------------------------------- */
function getCookie(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 0;
}



/**
openAWindow()
------------
Opens the poll.php document in a seperate window, salvaged from original system
----------------------------------------------------------------------------- */
function openAWindow(pageToLoad,winName,width,height,center) {
  xposition=0; yposition=0;
  if (parseInt(navigator.appVersion) >= 4 ) {
  	xposition = (screen.width - width) / 2;
  	yposition = (screen.height - height) / 2;
  }
  args = "width=" + width + "," + "height=" + height + "," + "location=0," + "menubar=0," + "resizable=0," + "scrollbars=1," + "status=0," + "titlebar=0," + "toolbar=0," + "hotkeys=0," + "screenx=" + xposition + "," + "screeny=" + yposition + "," + "left=" + xposition + "," + "top=" + yposition;
  window.open(pageToLoad,winName,args);
}



/**
generateCurrentPoll()
--------------------
Much of this code is salvaged from the old poll system
But the means by which it is implemented has been changed
----------------------------------------------------------------------------- */
function generateCurrentPoll() {
  document.write(' <table border="0" width="100%" cellspacing="0" cellpadding="0">');
  document.write(' <tr>');
  document.write(' <td valign="top">');

  // To start we'll find out whether an edition cookie exists..
  // + If not we alert the user they must choose an edition first
  if (!myedition) {
	document.write('<center>');
  	document.write('<span class="newsstory">');
  	document.write('<br />We\'re sorry.. You must select a default edition before you are eligible to vote.\n<br /><br />\n');
  	document.write('<form><input type="button" value="Let me choose!" onclick="loadWindow(\'/choose_edition.htm\');" /></form><br/>\n');
  	document.write('</span></center>');
  
  // Otherwise, the user has an edition and we can present the poll
  } else {
  	// So we need to cycle through the polls and determine if a specific poll exists for this user's edition
  	usepoll	= null;
  	for (i=0; i<objCurrentPoll.length; i++) {
  	  if (objCurrentPoll[i].edition == myedition) { usepoll = i; break; }
  	}
  	
  	// If not, then there must be a poll assigned to 'All Edition' or 'All Remaining Editions'
  	// + So we cycle through the array again to determine which index it is.. always edition 1
  	if (usepoll == null) {
  	  for (i=0; i<objCurrentPoll.length; i++) {
  	  	if (objCurrentPoll[i].edition == 1) { usepoll = i; break; }
  	  }
  	}
  	
  	// Here's the poll question
  	document.write('<p class="newsstory">' + objCurrentPoll[usepoll].question + '</p>');
  	
  	// And the responses
  	responses = new Array();
  	responses = objCurrentPoll[usepoll].responses.split("::");
  	document.write(' <table align="center" border="0">');
  	for (i=0; i<responses.length; i++) {
  	  document.write(' <tr><td width="20" valign="top"><a href="javas' + 'cript:openAWindow(\'/poll/' + pollpage + '?method=process&pollid=' + objCurrentPoll[usepoll].pollid + '&myvote=' + (i+1) + '\',\'poll\',180,765,1)"><img src="/news/images/radiobutton.gif" border=0></a></td><td valign="top"><font class="newsstory">' + responses[i] + ' </font></td></tr>');
  	}
  	document.write(' </table>');

  
  // Finally let's just close off all these tags, write up the user options, and call it a job well done
  //document.write(' <br />');
  document.write(' <center>');
  document.write(' <p>');
  document.write(' <a href="javascript:openAWindow(\'/poll/poll.php\',\'poll\',180,765,1)">');
  document.write(' <img src="/news/images/icon-pollresults.gif" width="20" height="17" border="0" align="absmiddle"></a> <a href="javas' + 'cript:openAWindow(\'/poll/' + pollpage + '?pollid=' + objCurrentPoll[usepoll].pollid + '\',\'poll\',180,765,1)" class="link">View Results</a>');
  document.write(' <a href="/poll/recent_polls.php">');
  document.write(' <img src="/news/images/icon-pollprevious.gif" width="20" height="17" border="0" align="absmiddle"></a> <a href="/poll/recent_polls.php" class="link" target=_top">Previous Polls</a>');
  document.write(' </p>');
  document.write(' </center>');
}

  document.write(' </td>');
  document.write(' </tr>');
  document.write(' </table>');
}

/**
generatePreviousPoll()
---------------------
Much of this code is salvaged from the old poll system
But the means by which it is implemented has been changed
----------------------------------------------------------------------------- */
function generatePreviousPoll() {
  usepoll = null;
  
  // if the user doesn't have an edition we'll try to assign an all editions first
  if (!myedition) {
  	for (i=0; i<objPreviousPoll.length; i++) {
  	  if (objPreviousPoll[i].edition == 1) { usepoll = i; break; }
  	}
  	// otherwise we'll just default to kelowna's poll (edition = 2)
  	if (usepoll == null) {
  	  for (i=0; i<objPreviousPoll.length; i++) {
  	  	if (objPreviousPoll[i].edition == 2) { usepoll = i; break; }
  	  }
  	}
  
  // if the user has an edition let's ascertain whether there's a specific poll assigned to it
  } else {
  	for (i=0; i<objPreviousPoll.length; i++) {
  	  if (objPreviousPoll[i].edition == myedition) { usepoll = i; break; }
  	}
  	// if not, there has to be a poll assigned to All Editions (edition = 1)
  	if (usepoll == null) {
  	  for (i=0; i<objPreviousPoll.length; i++) {
  	  	if (objPreviousPoll[i].edition == 1) { usepoll = i; break; }
  	  }
  	}
  }
  
  // So, right now usepoll has to contain an index reference for objPreviousPoll
  // We can go ahead and display the information
  
  // Here's the poll question
  document.write('<span class="newsstory">' + objPreviousPoll[usepoll].question + '?</span>');
  
  // And the responses
  document.write('<p><table width="90%">');
  
  totalvotes	= 0;
  responses	= new Array();
  responses	= objPreviousPoll[usepoll].responses.split("::");
  results	= new Array();
  results	= objPreviousPoll[usepoll].results.split("::");
  
  // Let's tally up and show the total votes first
  for (i=0; i<results.length; i++) { totalvotes = totalvotes + parseInt(results[i]); }
  
  // And write it out
  for (i=0; i<responses.length; i++) {
  	imgwidth = parseInt((results[i] / totalvotes) * 100);
  	document.write('<tr><td><font class="newsstory">' + responses[i] + '</font></td><td><image src="/poll/pollbar.gif" height="7" width="' + imgwidth + '"><font class="smalltext">&nbsp;' + results[i] + '</td></tr>');
  }
  
  // Finally, close it up... we're done folks.
  document.write('<tr><td colspan="2" align="center">Total votes: ' + totalvotes + '</td></tr>');
  document.write('</table></p>');
  
}



/**
Poll object constructors
----------------------------------------------------------------------------- */
var objCurrentPoll	= new Array();
var objPreviousPoll	= new Array();

function polldef(pollid,edition,question,responses,results) {
  this.pollid		= pollid;
  this.edition		= edition;
  this.question		= question;
  this.responses	= responses;
  this.results		= results;
}
