<?php
// $Id: mod_poll.php,v 1.12 2003/12/03 20:56:34 rcastley Exp $
/**
* Polls Module
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.12 $
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

show_poll_vote_form( $Itemid );

function show_poll_vote_form( $Itemid ) {
	global $database;
	
	$Itemid = mosGetParam( $_REQUEST, 'Itemid', 0 );
	
	$query1 = "SELECT p.id, p.title"
	."\nFROM #__poll_menu AS pm, #__polls AS p"
	."\nWHERE (pm.menuid='$Itemid' OR pm.menuid='0') AND p.id=pm.pollid"
	."\nAND p.published=1";
	
	$database->setQuery( $query1 );
	$polls = $database->loadObjectList();
	
	if($database->getErrorNum()) {
		echo "MB ".$database->stderr(true);
		return;
	}
	
	foreach ($polls as $poll) {
		if ($poll->id && $poll->title) {
			
			$query = "SELECT id, text FROM #__poll_data"
			. "\nWHERE pollid='$poll->id' AND text <> ''"
			. "\nORDER BY id";
			$database->setQuery($query);
			
			if(!($options = $database->loadObjectList())) {
				echo "MD ".$database->stderr(true);
				return;
			}
			poll_vote_form_html( $poll, $options, $Itemid );
		}
	}
}

function poll_vote_form_html( &$poll, &$options, $Itemid ) {
	
	$tabclass_arr=array("sectiontableentry2","sectiontableentry1");
	$tabcnt = 0;
?>
	<style>
		.pollstableborder {
		border: 1px solid;
		padding: 1px;
		}
	</style>
	<form name="form2" method="post" action="<?php echo sefRelToAbs("index.php?option=com_poll&Itemid=$Itemid"); ?>">
	<table width="95%" border="0" cellspacing="0" cellpadding="1" align="center">
		<tr>
			<td colspan="2" class="poll"><b><?php echo $poll->title; ?></b></td>
		</tr>
		<tr><td align="center">
			<table class='pollstableborder' cellspacing='0' cellpadding='0' border='0'>
			<tr><td>
<?php
	for ($i=0, $n=count( $options ); $i < $n; $i++) { ?>
		<tr>
			<td class='<?php echo $tabclass_arr[$tabcnt]; ?>' valign="top"><input type="radio" name="voteid" value="<?php echo $options[$i]->id;?>" /></td>
			<td class='<?php echo $tabclass_arr[$tabcnt]; ?>' valign="top"><?php echo $options[$i]->text; ?></td>
		</tr>
<?php
	if ($tabcnt == 1){
		$tabcnt = 0;
	} else {
		$tabcnt++;
	}
	} ?>
			</td></tr>
			</table>
		</td></tr>
		<tr>
			<td colspan="2" align="center">
				<input type="submit" name="task_button" class="button" value="<?php echo _BUTTON_VOTE; ?>" />&nbsp;&nbsp;
				<input type="button" name="option" class="button" value="<?php echo _BUTTON_RESULTS; ?>" onclick="document.location.href='<?php echo sefRelToAbs("index.php?option=com_poll&amp;task=results&amp;id=$poll->id"); ?>';" />
			</td>
		</tr>
	</table>

	<input type="hidden" name="id" value="<?php echo $poll->id;?>" />
	<input type="hidden" name="task" value="vote" />
	</form>
<?php
}
?>
