<?php

// -----------------------------------------------------------------------
// This file is part of AROUNDMe
//
// Copyright (C) 2003 - 2007 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.
// -----------------------------------------------------------------------

?>

<style type="text/css">
<!--
@import url(<?php echo $template_path;?>css/aroundme_form.css);
-->
</style>

<script type="text/javascript" src="speller/spellChecker.js"></script>
<script type="text/javascript">
	function openSpellChecker() {
		var speller = new spellChecker();
		speller.spellCheckAll();
	}
</script>


<form action="index.php?amn=core_msg_edit" method="post" name="frm" id="id_frm">
<input type="hidden" name="to_identity_id" value="<?php if (isset($identity['identity_id'])) { echo $identity['identity_id'];}?>" /><br />
				
<?php
if (isset($allow_compose)) {
?>
<div class="col_main">
	<div class="box">
		<div class="box_top">
			<?php echo $lang['hdr_message'];?><br />
		</div>
	
		<div class="box_mid">
			<div class="content">
				<p>
					<label for="id_identitynames"><?php echo $lang['txt_to'];?></label>
					<span id="id_identitynames"><?php if (isset($identity['identity_nic'])) echo $identity['identity_nic']; ?></span><br />
				</p>
				
				<p>
					<label for="id_message_subject"><?php echo $lang['txt_subject'];?></label>
					<input id="id_message_subject" name="message_subject" class="input1" type="text" size="48" value="<?php echo $message['message_subject'];?>" /><br />
				</p>
				
				<?php
				if (count($lang['messaging']['priority']) > 1) {
				?>
				<p>
					<label for="id_message_priority"><?php echo $lang['txt_priority'];?></label>
					<select name="message_priority">
					<?php
					foreach($lang['messaging']['priority'] as $key => $i):
					$selected="";
					if (isset($message['message_priority']) && $key == $message['message_priority']) {
						$selected = "selected=\"selected\"";
					}
					?>
					<option value="<?php echo $key;?>" <?php echo $selected;?> /><?php echo ucfirst($i);?><br />
					<?php
					endforeach;
					?>
					</select>
				</p>
				<?php }?>
					
				<?php
				if (count($lang['messaging']['response']) > 1) {
				?><p>
					<label for="id_message_response"><?php echo $lang['txt_response'];?></label>
					<select name="message_response">
					<?php
					foreach($lang['messaging']['response'] as $key => $i):
					$selected="";
					if (isset($message['message_response']) && $key == $message['message_response']) {
						$selected = "selected=\"selected\"";
					}
					?>
					<option value="<?php echo $key;?>" <?php echo $selected;?> /><?php echo ucfirst($i);?><br />
					<?php
					endforeach;
					?>
					</select>
				</p>
				<?php }?>
				
				<p>
					<label for="id_message_body"><?php echo $lang['txt_body'];?></label><br />
				</p>
				
				<p>
					<textarea id="id_message_body" name="message_body" cols="76" rows="10" /><?php echo $message['message_body'];?></textarea><br />
				</p>
				
					
				<?php
				$txtarea = "id_message_body";
				include($template_path . "inc/form_gui_html_editor.inc.php");
				?>
				
				<?php
				if (isset($message['display_message_body'])) {
				?>
				<p>
					<b><?php echo $message['message_subject']; ?></b><br />
				</p>
				<p>
					<?php echo $message['display_message_body']; ?>
				</p>
				<?php } ?>
				
				<hr />
				
				<p align="right">
					<input name="preview_message" type="submit" class="input_submit" value="<?php echo $lang['sub_preview'];?>" />&nbsp;&nbsp;
					<?php
					if (isset($allow_spellcheck)) {
					?>
					<input type="button" class="input_submit" value="<?php echo $lang['sub_check_spelling'];?>" onClick="openSpellChecker();" />&nbsp;&nbsp;
					<?php }?>
					<input name="send_message" type="submit" class="input_submit" value="<?php echo $lang['sub_send'];?>" /><br />
				</p>
			</div>
		</div>
	</div>
</div>

<div class="col_side">

	<div class="box">
		<div class="box_top">
			<?php echo $lang['hdr_recipient'];?><br />
		</div>
					
		<div class="box_mid">
			<div class="content">
				<?php
				if (isset($identity)) {
					$id_card = $identity;
					include("inc/identity.inc.php");
				}
				elseif (isset($identities)) {
				?>
				<input type="checkbox" onClick="checkFormBoxes(this);" />&nbsp;
				<?php echo $lang['txt_mark_all'];?>
				<br />
				<?php
				foreach ($identities as $idkey => $id):
				
				$checked = "";
				if (isset($tag) && !empty($id['tags'])) {
					foreach ($id['tags'] as $keytg => $t):
						if ($t['tag_body'] == $tag) {
							$checked = "checked=\"checked\"";
						}
					endforeach;
				}
				
				if (!empty($_POST['to_identity_ids'])) {
					foreach ($_POST['to_identity_ids'] as $key => $r):
						if ($r == $id['identity_id']) {
							$checked = "checked=\"checked\"";
							break;
						}
					endforeach;
				}
							
				$hover_title = $id['identity_nic'] . ", " . $id['location_name'] . ", " . $this->date->formatDate($id['identity_create_datetime'],1);
				
				?>
				<input title="<?php echo $hover_title;?>" type="checkbox" name="to_identity_ids[]" value="<?php echo $id['identity_id'];?>" onClick="javascript:setToNames();" <?php echo $checked;?> />
				<input type="hidden" name="to_identity_names[]" value="<?php echo $id['identity_nic'];?>" />
				<span title="<?php echo $hover_title;?>">
				<?php echo $id['identity_nic'];?></span><br />
				<?php
				endforeach;
				}
				?>
				
				<hr />
				
				<p>
					<b><?php echo $lang['txt_tags'];?></b><br />
					<?php
					if (isset($tags)) {
					$prnt = "";
					foreach($tags as $key => $i):
						$prnt .= "<a href='index.php?amn=core_msg_edit&amp;tag=" . $i['tag_body'] . "'>" . $i['tag_body'] . "</a>, ";
					endforeach;
					$prnt = substr($prnt, 0, -2);
					echo $prnt;
					}
					?>
				</p>
						
				<script type="text/javascript">
				function setToNames() {
					var identitynames = '';
					var id_names = document.getElementsByName("to_identity_names[]");
					var id_ids   = document.getElementsByName("to_identity_ids[]");
				
					for (i=0; i < id_ids.length; i++) {
						if (id_ids.item(i).checked) {
							identitynames += id_names.item(i).value + ', ';
						}
					}
					
					identitynames = identitynames.substr(0,identitynames.length-2);
					document.getElementById('id_identitynames').innerHTML = identitynames+'<br />';
				}
				
				if (!document.getElementById('id_identitynames').innerHTML) {
					setToNames();
				}
				
				</script>
			</div>
		</div>
	</div>
</div>

<?php
}
else {
?>

<div class="box">
	<div class="box_top">
		<?php echo $lang['hdr_no_pref'];?><br />
	</div>
					
	<div class="box_mid">
		<div class="content">
			<?php echo $lang['hdr_no_pref_intro'];?><br />
		</div>
	</div>
</div>

<?php }?>

</form>

<script language="javascript">
<!--

function checkFormBoxes(id) { //task is check or uncheck
	
	var form = document.frm.elements['to_identity_ids[]'];
	var state = true;
	
	if (id.checked == false) {
		state = false;
	}

	if (form.length) {
		for (var i=0; i < form.length; i++) {
			form[i].checked = state;
		}
	}
	else {
		form.checked = state;
	}
	
	setToNames();
}
//-->
</script>