<?

include_once("../includes/site_config.php");
include_once("include/header.php");
include_once("config.inc.php");
require_once("functions.inc.php");

$simplepollurl = simplepollurl;


eval("dooutput(\"".gettemplate(templatedir,"admin_header")."\");");

connect();

$subaction   = $_REQUEST['subaction'];
$action      = $_REQUEST['action'];
$name        = $_REQUEST['name'];
$question    = $_REQUEST['question'];
$bgcolor     = $_REQUEST['bgcolor'];
$fontsize    = $_REQUEST['fontsize'];
$font        = $_REQUEST['font'];
$fontcolor   = $_REQUEST['fontcolor'];
$orientation = $_REQUEST['orientation'];
$tbgcolor    = $_REQUEST['tbgcolor'];
$tfontsize   = $_REQUEST['tfontsize'];
$tfontcolor  = $_REQUEST['tfontcolor'];
$tfont       = $_REQUEST['tfont'];
$pollid      = $_REQUEST['pollid'];
$choice      = $_REQUEST['choice'];
$barcolor    = $_REQUEST['barcolor'];
$active		 = $_REQUEST['active'];	
if(isset($active)){$active=='1';}else{$active=='0';}

switch($subaction){
    case "add":
    $sql_result = query("INSERT INTO poll VALUES (NULL,'$name','$question','$bgcolor','$fontsize','$font','$fontcolor','$orientation','$tbgcolor','$tfontsize','$tfontcolor','$tfont','0','$active')");
	break;
    case "modify":
    $sql_result = query("UPDATE poll SET name='$name', question='$question', bgcolor='$bgcolor', fontsize='$fontsize', font='$font', fontcolor='$fontcolor', orientation='$orientation', tbgcolor='$tbgcolor', tfontsize='$tfontsize', tfontcolor='$tfontcolor', tfont='$tfont', active='$active' WHERE id='$pollid'");
    break;
    case "addchoice":
    $sql_result = query("INSERT INTO choices VALUES (NULL,'$pollid','$choice','$barcolor','0')");
    break;
}

switch($active){
	case "1":
	$sql_res= query("UPDATE poll set active='0' where id!='$pollid'");
	break;
}

switch($action){
case "modify":

    $sql_result = query("SELECT * FROM poll WHERE id = '$pollid'");


    if (isset($sql_result)) {

       $numpolls = num_rows($sql_result);

       for ($i=0; $i<$numpolls; $i++) {

          $row=fetch($sql_result);

          $pollid = $row[id];

			 $question   = $row[question];
			 $name       = $row[name];
			 $bgcolor    = $row[bgcolor];
			 $tfont      = $row[tfont];
			 $tfontcolor = $row[tfontcolor];
             $tfontsize  = $row[tfontsize];
             $fontsize   = $row[fontsize];
			 $font       = $row[font];
			 $fontcolor  = $row[fontcolor];
			 $tbgcolor   = $row[tbgcolor];
			 $active     = $row[active];	
if($active=='1'){$checked="SELECTED";} 	

          eval("storeoutput(\"".gettemplate(templatedir,"admin_input")."\");");

	    }
	 }


    break;
    case "delete":
    $sql_result = query("DELETE FROM poll WHERE id=$pollid LIMIT 1");
    break;
    case "addchoice":
    $sql_result = query("SELECT * FROM poll WHERE id = '$pollid'");


    if (isset($sql_result)) {

       $numpolls = num_rows($sql_result);

       for ($i=0; $i<$numpolls; $i++) {

          $row=fetch($sql_result);


			 $question   = $row[question];

	    }
	 }

    eval("storeoutput(\"".gettemplate(templatedir,"admin_choice")."\");");

    break;
    case "add":
    eval("storeoutput(\"".gettemplate(templatedir,"admin_input")."\");");
    break;
    default:
    // view polls in admin page

    $sql_result = query("SELECT * FROM poll LIMIT 0,30");

    if (isset($sql_result)) {

       $numpolls = num_rows($sql_result);

       for ($i=0; $i<$numpolls; $i++) {

          $row=fetch($sql_result);

          $pollid = $row[id];

		  $pollquestion   = $row[question];
		  $pollname       = $row[name];

		  $pollbgcolor    = $row[bgcolor];
          $pollfontsize   = $row[fontsize];
		  $pollfont       = $row[font];
		  $pollfontcolor  = $row[fontcolor];

          $polltfontsize  = $row[tfontsize];
		  $polltfont       = $row[tfont];
		  $polltfontcolor  = $row[tfontcolor];
          $polltbgcolor   = $row[tbgcolor];


		  $sql_result2 = query("SELECT * FROM choices WHERE pollid = '$row[id]' ORDER BY id");

		  $pollanswers = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n";

          if (isset($sql_result2)) {

             $numanswers = num_rows($sql_result2);

             for ($x=0; $x<$numanswers; $x++) {

                $row2=fetch($sql_result2);

                if ($x == 0) {
   					 $selected = "checked";
   					 $dfltval  = $row2[id];
                } else {
                   $selected = "";
                }

					 $pollanswers .= "<tr>\n";

					 $pollanswers .= "<td width=\"5\" align=\"center\"><input type=\"radio\" name=\"aid\" value=\"".$row2[id]."\" onClick=\"document.poll".$pollid.".haid".$pollid.".value='".$row2[id]."';\" ".$selected."></td>\n";
					 $pollanswers .= "<td align=\"left\"><font face=\"".$pollfont."\" size=\"".$pollfontsize."\" color=\"".$pollfontcolor."\">".$row2[choice]."</font></td>";

					 $pollanswers .= "</tr>\n";

				 }
          } else {
            $pollanswers .= "<tr><td align=center>No options have been entered</td></tr>";
          }

			 $pollanswers .= "</table>\n";

			 if ($numanswers < 1) {
				 $pollanswers = "<font face=\"".$pollfont."\" size=\"".$pollfontsize."\" color=\"".$pollfontcolor."\">You need to add an option.</font>";
			 }

			 eval("storeoutput(\"".gettemplate(templatedir,"poll")."\");");

		 }

    }

    break;
}

// $admincontent = "hey wassup";

eval("dooutput(\"".gettemplate(templatedir,"admin_poll")."\");");

eval("dooutput(\"".gettemplate(templatedir,"admin_footer")."\");");


   echo $error[0];

close();
include_once("include/footer.php");
?>