<?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">
			<div class="content">
				<h1><?php echo $lang['hdr_message'];?></h1>
			</div>
		</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" style="width: 360px;" 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 }?>

				<hr />

				<table cellspacing="1" cellpadding="0" border="0" width="100%">
					<tr>
						<td>
							<label for="id_message_body"><?php echo $lang['txt_body'];?></label><br />
						</td>
						<td>
							<?php
							$gui_expert_editor = "id_message_body";
							include($template_path_core . "inc/form_gui_html_editor.inc.php");
							?>
						</td>
					</tr>
				</table>
					
				<table cellspacing="1" cellpadding="0" border="0" width="100%">
					<tr>
						<td width="1">
							<textarea id="id_message_body" name="message_body" cols="72" rows="8" style="width:480px;" onKeyUp="AMEditorSync(this.value, 'wysiwyg_editor');"><?php if (isset($message['message_body'])) { echo $message['message_body'];}?></textarea>
							<br />
						</td>
						<td>
							<br />
						</td>
					</tr>
					<tr>
						<td width="1">
							<iframe id="wysiwyg_editor" name="wysiwyg_editor" style="height:100px; width:480px;" frameborder="0" width="580"  scrolling="yes"  height="100">
				
							</iframe>
						</td>
						<td valign="top" width="20">
							<img src="<?php echo $template_path_core;?>img/arrow_up.png" width="9" height="12" alt="" border="0" onClick="AmEditorResize();" /><br />
							<img src="<?php echo $template_path_core;?>img/arrow_down.png" width="9" height="12" alt="" border="0" onClick="AmEditorResize('minus');" /><br />
							<img src="<?php echo $template_path_core;?>img/pixel.png" width="20" height="1" alt="" border="0" /><br />
						</td>
					</tr>
				</table>
				
				<script language="javascript" type="text/javascript">
					var wysiwygHeight = 100;
					var thisEditor = new AMEditor('id_message_body', 'wysiwyg_editor');
				</script>

				<p align="right">
					<?php
					$checked = "";

					if (isset($check_copy_to_out)) {
						$checked = " checked=\"checked\"";
					}
					?>
					
					<?php echo $lang['txt_copy_to_out'];?>&nbsp;
					<input type="checkbox" name="copy_to_out" value="1"<?php echo $checked;?> />
				
				</p>
				
				
				<?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">
			<div class="content">
				<h1><?php echo $lang['hdr_recipient'];?></h1>
			</div>
		</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 dots($id['identity_nic'],20);?></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 id="editor_help"><?php echo $lang['txt_help_default'];?></div>
</div>

<?php
}
else {
?>

<div class="box">
	<div class="box_top">
		<div class="content">
			<h1><?php echo $lang['hdr_no_pref'];?></h1>
		</div>
	</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>