<?php

// -----------------------------------------------------------------------
// This file is part of Igloo
// 
// Copyright (C) 2003 - 2006 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301, USA.
// -----------------------------------------------------------------------

?>


<script language="JavaScript" type="text/javascript">
	function setCommentParent(comment_id, comment_parent_id) {
		//make form parent_id hidden field value = comment_id
		document.getElementById('id_comment_parent').value=comment_id;
		
		//turn comment red
		formElementCommentBody = 'comment_body_id_'+comment_id;
		formElementCommentLink = 'comment_link_id_'+comment_id;
		document.getElementById(formElementCommentBody).style.color='blue';
		document.getElementById(formElementCommentLink).style.textDecoration='none';
		
		document.getElementById('commentIntro').innerHTML ='<?php echo $lang['txt_comment_position1'];?>';
		
		document.location.href='#commentform';
	}
</script>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="comment" id="id_frm_comment">

<?php
if (isset($poll) && isset($poll['vote_required']) && !isset($poll['current_answer']['answer_id'])) {
	echo $lang['txt_poll_required'] . "<br />";
}
else {
	if (isset($comments)) {
		if (isset($_SESSION['user_preferences']['comment_mode']) && $_SESSION['user_preferences']['comment_mode'] == $comment_view_ids['threaded']) {
			function getChildren($comments, $parent_id, $level_id, $date) {
				
				global $lang, $template_path, $poll;
				
				foreach($comments as $key => $i):
				?>
					
						<?php
						if (empty($i['comment_parent_id'])) {
							$i['comment_parent_id'] = 0;
						}
						
						if ($key !=0 && $parent_id == 0 && $i['comment_parent_id'] == $parent_id) {
							echo "<hr />";
						}
						
						if ($i['comment_parent_id'] == $parent_id) {
						
							if ($parent_id == 0) {
								
							unset($thread_view);
							?>
						<div class="comment">
						<a href="javascript:setCommentParent(<?php echo $i['comment_id'];?>, <?php echo $i['comment_parent_id'];?>)" style="float:right;" id="comment_link_id_<?php echo $i['comment_id'];?>"><?php echo $lang['href_comment_reply'];?></a>
						
						<span class="comment_author">
							<b><?php echo $lang['txt_comment'];?></b> -&#62; 
							<?php echo $date->formatDate($i['comment_create_datetime'], 1);?><br />
							<?php
							if (empty($poll['poll_anon'])) {
							?>
							<b><?php echo $lang['txt_from'];?></b> -&#62;
							<a href="sn_profile.php?user_id=<?php echo $i['user_id'];?>"><?php echo $i['profile_nic'];?></a>, 
							<?php echo $lang['txt_joined'] . ": " . $date->formatDate($i['profile_create_datetime']);?>, 
							<?php echo $lang['txt_hometown'] . ": " . $i['profile_hometown'];?><br />
							<?php }?>
							
							<?php
							if (isset($poll) && empty($poll['poll_anon'])) {
							?>
							<b><?php echo $lang['hdr_vote_history'];?></b> -&#62; 
							<?php
							if (isset($i['vote'])) {
								$separator = ", ";
								foreach($i['vote'] as $key => $r):
									if ($key == (count($i['vote'])-1)) {
										$separator=".";
									}
									echo $r['option_body'] . " (" . $r['answer_create_datetime'] . ")" . $separator;
								endforeach;
							}
							?>
							<br />
							<?php }?>
						</span>
						
						<span id="comment_body_id_<?php echo $i['comment_id'];?>">
						<?php echo $i['comment_body'];?><br />
						</span>
						</div>
						<?php }
						else {?>
						
						<?php
						$width_calc =20*$level_id;
						?>
						
						<div class="comment_child" id="comment_id_<?php echo $i['comment_id'];?>">
							<?php
							if (!isset($new_level)) {
							?>
							
							<img src="<?php echo $template_path;?>img/drop_arrow.png" style="margin-left:<?php echo $width_calc+5;?>px;" width="10" height="10" alt="indent" border="0" /><br />
							
							<?php
							$new_level = 1;
							}?>
							<div style="margin-left:<?php echo $width_calc+20;?>px;">
							
							<a href="javascript:setCommentParent(<?php echo $i['comment_id'];?>, <?php echo $i['comment_parent_id'];?>)" style="float:right;" id="comment_link_id_<?php echo $i['comment_id'];?>">reply</a>
							<i><b><?php echo $lang['txt_comment'];?></b> -&#62; 
									<?php echo $date->formatDate($i['comment_create_datetime'], 1);?><br />
									<?php
									if (empty($poll['poll_anon'])) {
									?>
									<b><?php echo $lang['txt_from'];?></b> -&#62;
									<a href="sn_profile.php?user_id=<?php echo $i['user_id'];?>"><?php echo $i['profile_nic'];?></a>, 
									<?php echo $lang['txt_joined'] . ": " . $date->formatDate($i['profile_create_datetime']);?>, 
									<?php echo $lang['txt_hometown'] . ": " . $i['profile_hometown'];?><br />
									<?php }?>
									
									<?php
							if (isset($poll) && empty($poll['poll_anon'])) {
							?>
							<b><?php echo $lang['hdr_vote_history'];?></b> -&#62; 
							<?php
							if (isset($i['vote'])) {
								$separator = ", ";
								foreach($i['vote'] as $key => $r):
									if ($key == (count($i['vote'])-1)) {
										$separator=".";
									}
									echo $r['option_body'] . " (" . $r['answer_create_datetime'] . ")" . $separator;
								endforeach;
							}
							?>
							<br />
							<?php }?>
							
							
								<span id="comment_body_id_<?php echo $i['comment_id'];?>">
								<?php echo $i['comment_body'];?><br />
								</span>
								</i>
							</div>
						</div>
					<?php }?>
					<?php
					getChildren($comments, $i['comment_id'], $level_id+1, $date);
					}
					unset($new_level);
			   endforeach;
			}
			getChildren($comments, 0,0, $this->date);
		}
		else {
		foreach($comments as $key => $i):
		
			if ($key !=0) {
				echo "<hr />";
			}
			
		?>	
		<div class="comment">
			<span class="comment_author">
				<b><?php echo $lang['txt_comment'];?></b> -&#62; 
				<?php echo $this->date->formatDate($i['comment_create_datetime'], 1);?><br />
				<?php
				if (empty($poll['poll_anon'])) {
				?>
				<b><?php echo $lang['txt_from'];?></b> -&#62;
				<a href="sn_profile.php?user_id=<?php echo $i['user_id'];?>"><?php echo $i['profile_nic'];?></a>, 
				<?php echo $lang['txt_joined'] . ": " . $this->date->formatDate($i['profile_create_datetime']);?>, 
				<?php echo $lang['txt_hometown'] . ": " . $i['profile_hometown'];?><br />
				<?php }?>
				
				<?php
				if (isset($poll) && empty($poll['poll_anon'])) {
				?>
					<b><?php echo $lang['hdr_vote_history'];?></b> -&#62; 
					<?php
					if (isset($i['vote'])) {
						$separator = ", ";
						foreach($i['vote'] as $key => $r):
							if ($key == (count($i['vote'])-1)) {
								$separator=".";
							}
							echo $r['option_body'] . " (" . $r['answer_create_datetime'] . ")" . $separator;
						endforeach;
					}
					?>
				<br />
				<?php }?>
			</span>
			
			<?php echo $i['comment_body'];?><br />
			
		</div>
		<?php
		endforeach;
		?>
		<?php }?>
	<?php }?>

	<div id="comment_form">

	<?php
	if (isset($library['library_id'])) {
	?>
		<p align="right">
			<?php
			$comm_url = $page_url;
			
			if (isset($_REQUEST['ico'])) {
				$comm_url = str_replace('&ico=1', '', $comm_url);
				$comm_lang = $lang['href_all_comments'];
			}
			else {
				$comm_url = $comm_url . "&amp;ico=1";
				$comm_lang = $lang['href_item_comments_only'];
			}
			?>
			
			<a href="<?php echo $comm_url;?>"><?php echo $comm_lang;?></a>&nbsp;<br />
		</p>
		<br />
	<?php }?>


	<h3>
	<?php
		if (isset($display) && $display == "preview_comment") {
		?>
		<?php echo $lang['hdr_comment_preview'];?>
		<?php }?>
	</h3>
	
	<?php
	if (!isset($me['user_id'])) { //not logged in
	?>
		<p>
			<b><?php echo $lang['txt_no_comment_allowed_login'];?></b><br />
		</p>
	<?php
	}
	elseif (!empty($unlocked)) {
	?>
	
	
	<?php
	if (isset($hidden_form)) {
		foreach($hidden_form as $key => $i):
	?>
	<input type="hidden" name="<?php echo $key?>" value="<?php echo $i;?>" />
	<?php 
		endforeach;
	}
	?>
	
	<?php
	if (isset($display) && $display == "preview_comment") {
	?>
	<p>
		<input type="hidden" name="comment_body" value="<?php echo $comment['comment_body'];?>" />
		<input type="hidden" name="comment_parent_id" value="" />
		<?php echo $comment['display_comment_body'];?><br />
	</p>
	<p align="right">
		<input type="submit" name="edit_again" class="input_submit" value="<?php echo $lang['sub_edit_again'];?>" />&nbsp;&nbsp;
		<input type="submit" name="insert" class="input_submit" value="<?php echo $lang['sub_save'];?>" /><br />
	</p>
	<?php
	}
	else {
	?>
	
	<span class="gui">
		<script language="JavaScript" type="text/javascript">
			var displayType = 'comment';
		</script>
		<script language="JavaScript" type="text/javascript" src="js/gui_wiki.js"></script>
	</span>
		
	<a name="commentform"></a>
	
	<h4><?php echo $lang['hdr_add_comment'];?></h4>
	
	<p>
		<span id="commentIntro"><?php echo $lang['txt_comment_position'];?></span>
	</p>
	
	<p>
		<input type="hidden" id="id_comment_parent" name="comment_parent_id" value="" />
		<textarea id="id_comment" name="comment_body" rows="4" cols="70"><?php if(isset($comment['comment_body'])) { echo $comment['comment_body'];}?></textarea><br />
		<script language="JavaScript" type="text/javascript">
			var txtarea = document.comment.comment_body;
		</script>
	</p>
	
	<p align="right">
		<?php if (isset($allow_spellcheck)) {?>
		<input type="button" class="input_submit" value="<?php echo $lang['sub_check_spelling'];?>" class="button_light" onClick="openSpellChecker();" />&nbsp;
		<?php }?>
		<input type="submit" name="preview" class="input_submit" value="<?php echo $lang['sub_preview'];?>" />&nbsp;
		
		<input type="submit" name="insert" class="input_submit" value="<?php echo $lang['sub_add'];?>" /><br />
	</p>
	<?php }?>
	
	<p class="comment_author">
		<?php
		if (isset($_SESSION['user_preferences']['comment_mode']) && $_SESSION['user_preferences']['comment_mode'] == $comment_view_ids['threaded']) {
		?>
		<?php echo $lang['txt_comment_threaded'];?><br />
		<?php
		}
		else {
		?>
		<a href="<?php echo $comment_url;?>&amp;comment_view_id=<?php echo $comment_view_ids['threaded'];?>"><?php echo $lang['href_comment_threaded'];?></a> - <?php echo $lang['txt_comment_threaded_hint'];?><br />
		<?php }?>
	
		<?php
		if (isset($_SESSION['user_preferences']['comment_mode']) && $_SESSION['user_preferences']['comment_mode'] == $comment_view_ids['normal']) {
		?>
		<?php echo $lang['txt_comment_normal'];?><br />
		<?php
		}
		else {
		?>
		<a href="<?php echo $comment_url;?>&amp;comment_view_id=<?php echo $comment_view_ids['normal'];?>"><?php echo $lang['href_comment_normal'];?></a> - <?php echo $lang['txt_comment_normal_hint'];?><br />
		<?php }?>
	
		<?php
		if (isset($_SESSION['user_preferences']['comment_mode']) && $_SESSION['user_preferences']['comment_mode'] == $comment_view_ids['hybrid']) {
		?>
		<?php echo $lang['txt_comment_hybrid'];?><br />
		<?php
		}
		else {
		?>
		<a href="<?php echo $comment_url;?>&amp;comment_view_id=<?php echo $comment_view_ids['hybrid'];?>"><?php echo $lang['href_comment_hybrid'];?></a> - <?php echo $lang['txt_comment_hybrid_hint'];?><br />
		<?php }?>
	</p>
	
	<?php
	}
	else {
	?>
		<p>
			<?php echo $lang['txt_no_comment_allowed'];?><br />
		</p>
	<?php }?>
	</div>
<?php }?>
</form>