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

// Query to get the details of the Question to be edited
$faqId = clean(base64_decode($_GET['quest']));
$qryRes = mysql_fetch_array(mysql_query("SELECT Id, Heading, Question FROM tbl_questions WHERE Id = '".$faqId."'"));

// Update the record
if ( isset($_POST['submit']) ){
	
	if ( !empty($_POST['question']) ){
		$updateQry = "UPDATE tbl_questions
				SET
					Heading = '".clean($_POST['title'])."',
					Question = '".clean($_POST['question'])."'
				WHERE Id = '".$faqId."'";
		mysql_query($updateQry);
	}
	$redirectUrl = 'view-questions.php';
	$_SESSION['succesMessage'] = 5;
	header("Location: $redirectUrl");
}


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

<script type="text/javascript" src="<?php echo TINYMCE;?>jscripts/tiny_mce/tiny_mce.js"></script>
<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 = $("#editQuestForm").submit(function() {
			// update underlying textarea before submit validation
			tinyMCE.triggerSave();
		}).validate({
			ignore: "",
			rules: {
				title: "required",
				question: "required"
			},
			messages: {
				title: "Enter question title.",
				question: "Enter question description."
			},
			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>
<section id="main" class="column">
    <article class="module width_full">
        <header>
            <h3>Edit Question</h3>
        </header>
        <form name="editQuestForm" id="editQuestForm" action="" method="post">
            <fieldset style="margin:15px">
                <table width="100%">
                	<tr>
                    	<td>
                        	<label>Title</label>
                			<input type="text" name="title" id="title" value="<?php echo $qryRes['Heading'];?>" />
                        </td>
                    </tr>
                    <tr>
                    	<td>
                        	<label>Question</label>
                            <span class="tinyMCE"><textarea id="question" name="question" rows="15" cols="80" style="width: 99%;float:left;"><?php echo $qryRes['Question'];?></textarea></span>
                			<label for="question" class="error" style="display:none;">Enter Question Description.</label>
                        </td>
                    </tr>
                	<tr>
                    	<td>
                            <input type="submit" name="submit" id="submit" value="Update" />
                            <input type="button" name="cancel" id="cancel" value="Cancel" onclick="window.location='<?php echo ADMIN_URL;?>view-questions.php'" />
                        </td>
                    </tr>
                </table>
            </fieldset>
        </form>
    </article>
    <div class="spacer"></div>
</section>

<?php
endHtml();
?>
