<?php
include('../config/config.php');
include(ADMIN_HTML.'html.inc.php');
include(ADMIN_INCLUDES.'functions.php');
checkLoginAdmin();

if ( isset($_POST['submit']) )
{	
	if ( !empty($_POST['question']) )
	{
	  for($i=0;$i<sizeof($_POST['question']);$i++)
	  {	
		$insQry = "INSERT INTO tbl_party_response
				SET
				    CandidateId='".clean($_POST['candidate'])."',
					QuestionId = '".clean($_POST['question'][$i])."',
					Answer = '".clean($_POST['answer'][$i])."'";
		mysql_query($insQry);		
	  }
	  
	  $checkBox=array();
	  $checkBox=$_POST['check'];
	  foreach($checkBox as $key=>$box)
	  {
		$insQry = "UPDATE tbl_party_response
					SET
						PriorityBit='1'
					WHERE tbl_party_response.CandidateId='".$_POST['candidate']."' AND tbl_party_response.QuestionId='$key'";
		
		mysql_query($insQry);  
	  }
  
	}
	$redirectUrl = 'index.php';
	$_SESSION['succesMessage'] = 4;
	header("Location: $redirectUrl");
}

startHtml($title = "Add Question Response");
tophead($title);
leftNav();
?>

<script type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "simple",
		// update validation status on change
		onchange_callback: function(editor) {
			tinyMCE.triggerSave();
			$("#" + editor.id).valid();
		}
	});
	$(function() {
		var validator = $("#addQuestForm").submit(function() {
			// update underlying textarea before submit validation
			tinyMCE.triggerSave();
		}).validate({
			ignore: "",
			rules: {
				candidate: "required",
			},
			messages: {
				candidate: "Select Candidate",
			},
			errorPlacement: function(label, element) {
				// position error label after generated textarea
				if (element.is("textarea")) {
					label.insertAfter(element.next());
				} else {
					label.insertAfter(element)
				}
			}
		});
		validator.focusInvalid = function() {
			// put focus on tinymce on submit validation
			if( this.settings.focusInvalid ) {
				try {
					var toFocus = $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []);
					if (toFocus.is("textarea")) {
						tinyMCE.get(toFocus.attr("id")).focus();
					} else {
						toFocus.filter(":visible").focus();
					}
				} catch(e) {
					// ignore IE throwing errors when focusing hidden elements
				}
			}
		}
	});
</script>
<style>
.radio{
	cursor:pointer;
}
</style>

<section id="main" class="column">
    <article class="module width_full">
        <header>
            <h3>Add Questions Response</h3>
        </header>
        <form name="addQuestForm" id="addQuestForm" action="" method="post">
            <fieldset style="margin:15px">
            	<table width="100%">
                	<tr>
                    	<td>
                        	<label>Select Candidate</label>
               				<select name="candidate" style="width:30%;">
							 <option value="">-Select-</option>
							 <?php
							 $flag=false;
							 $getRes = mysql_query("SELECT Id, Name FROM tbl_candidate ORDER BY Id DESC");
								 while( $row = mysql_fetch_array($getRes) )
								 {
									 $qry="SELECT COUNT(tbl_party_response.Id) AS ROW FROM tbl_party_response WHERE tbl_party_response.CandidateId='".$row["Id"]."'";
									 $getRemain=mysql_query($qry);
									 $remain = mysql_fetch_array($getRemain);
									 
									 if($remain["ROW"]<1)
									 {
										 $flag=true;
									 ?>
								  <option value="<?php echo $row["Id"];?>"><?php echo $row["Name"];?></option>
									 <?php
									 }
								 }
							  ?>
                              <?php
							  if($flag==false)
							  {
							  ?>
                               <option value="">No Candidate Left</option>
                              <?php
							  }
							  ?> 
							</select>
                        </td>
                    </tr>
					<tr>
					 <td>
					 <label>Questions</label>
					 </td>
					</tr>
					<tr><td><hr/></td></tr>
					<?php
					$getRes = mysql_query("SELECT Id, Heading,Question FROM tbl_questions ORDER BY Id DESC");
					 $i=0;
					 while( $row = mysql_fetch_array($getRes) )
					 {
					?>
					<tr>
					 <td>
					  <label class="trigger" style="color:#00F"><?php echo $row['Heading'];?></label>
                      <input type="hidden" name="question[<?php echo $i;?>]" value="<?php echo $row['Id']; ?>"/> 
					  <ul class="toggle" style="list-style:none;">
						<li><?php echo "<br/>".$row['Question'];?></li>
					  </ul>
					  </td>
					</tr>
					<tr>
					 <td>
					  <span style="padding-left:10px;">
					   Agree<input type="radio" name="answer[<?php echo $i;?>]" value="A" class="radio" required="required"/>&nbsp;&nbsp;&nbsp;
					   Neutral<input type="radio"  name="answer[<?php echo $i;?>]" value="N" class="radio" required="required"/>&nbsp;&nbsp;&nbsp;
					   Disagree<input type="radio"  name="answer[<?php echo $i;?>]" value="D" class="radio" required="required"/>&nbsp;&nbsp;&nbsp;
					   Priority<input type="checkbox" name="check[<?php echo $row['Id'];?>]" class="radio"/>
					  </span>
					 </td>
					</tr>
					<?php
					  $i++;
					 }
					?>
					<tr><td><hr/></td></tr>
					<tr>
                    	<td>
                            <input type="submit" name="submit" id="submit" value="Add Record"/>
                            <input type="button" name="cancel" id="cancel" value="Cancel" onclick="window.location='<?php echo ADMIN_URL;?>view-responses.php'" />
                        </td>
                    </tr>		
                </table>
		</fieldset>
      </form>		
    </article>
	  <div class="spacer"></div>  
</section>

<?php
endHtml();
?>