<?php include('inc/header.php'); ?>
<h1>Settings - Site: FAQ</h1>
<div class="container">
<?php
	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['web'] . ']');
	if(isset($_POST['faqsbm'])) {
		if($_POST['question'] == '' || $_POST['answer'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			$id = odbc_result(odbc_exec($odbc_connect, 'SELECT [faqid] FROM [web_faq] ORDER BY faqid DESC'), 'faqid')+1;
			odbc_exec($odbc_connect, 'INSERT INTO [web_faq](faqid, question, answer) VALUES('.$id.', \''.mssql_escape_string($_POST['question']).'\', \''.mssql_escape_string($_POST['answer']).'\')');
			echo '<div class="success">The FAQ has successfully been added!</div>';
		}
	} elseif(isset($_POST['savefaqsbm'])) {
		if($_POST['edit_question'] == '' || $_POST['edit_answer'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			odbc_exec($odbc_connect, 'UPDATE [web_faq] SET question=\''.mssql_escape_string($_POST['edit_question']).'\', answer=\''.mssql_escape_string($_POST['edit_answer']).'\' WHERE faqid=\''.mssql_escape_string($_POST['edit_faqid']).'\'');
			echo '<div class="success">The FAQ has successfully been changed!</div>';
		}
	}
	
	$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_faq] ORDER BY faqid ASC');
	
	echo '<span id="text"></span>';
	echo '<div id="box"></div>';
	echo '<table><tr><td><a href="javascript:faq_addform();" title="Add" rel="tooltip"><img src="../images/add.png" /></a></td><td>Add FAQ</td></tr></table>';
	
	echo '<form method="post" id="faqaddform" style="display: none;">
		<table id="table">
			<tr>
				<td id="key">Question:</td>
				<td id="value"><input type="text" name="question" style="width:500px;" /></td>
			</tr>
			<tr>
				<td id="key" valign="top">Answer:</td>
				<td id="value"><textarea name="answer" cols="90" rows="5" style="width:504px; height:200px;"></textarea></td>
			</tr>
			<tr>
				<td id="key"></td>
				<td id="value"><input type="submit" name="faqsbm" value="Add FAQ" /></td>
			</tr>
		</table>
	</form><br/>';
	
	echo '<table id="table" style="width: 100%;">
		<tr>
			<td id="key">ID</td>
			<td id="key">Question</td>
			<td id="key">Answer</td>
			<td id="key" style="width: 50px;"></td>
		</tr>';
	while($result = odbc_fetch_array($select)) {
		$question = $result['question'];
		$answer = $result['answer'];
		if(strlen($question) > 60) { $question = substr($question, 0, 60).'...'; }
		if(strlen($answer) > 60) { $answer = substr($answer, 0, 60).'...'; }
		echo '<tr id="faqid_'.$result['faqid'].'" class="row">
			<td id="value">'.$result['faqid'].'</td>
			<td id="value">'.$question.'</td>
			<td id="value">'.$answer.'</td>
			<td id="value"><a href="javascript:faq_edit('.$result['faqid'].');" title="Edit" rel="tooltip"><img src="../images/edit.png" /></a> <a href="javascript:faq_delete('.$result['faqid'].');" title="Delete" rel="tooltip"><img src="../images/delete.png" /></a></td>
		</tr>';
	}
	echo '</table>';
?>
</div>
<?php include('inc/footer.php'); ?>