<?php
/**

	Send Notifications to Multiple users

**/
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog="SELECT FI_SendNotification as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");


if ($remChar == '') $remChar = 256;

$queryReceivers = "select if(ifnull(fullname,'') = '',user_name,fullname) as username, user_account from ".$_SESSION['dbLabel'].".users
					where upper(concat(defaultComp,'_',defaultYear)) = upper('".$_SESSION['dbName']."')
					order by username";
$resReceivers = mysql_query($queryReceivers, $connection);					

print <<< HERE
<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<title>Send Notification</title>
<style type="text/css">

.title1 {
	width: 80%;
    border: 1px solid lightgray;
	height: 25px;
    font-family: serif;
    font-size: 15px;
    font-weight: 600;
	box-shadow: lightgrey 2px 2px;
}

.msgbody {
	border: 2px solid lightgray;
    background-color: white;
	font-family: serif;
    font-size: 15px;
	width: 100%;
	height: 300px;
	resize: none;
	box-shadow: lightgrey 2px 2px;
}
.msgbody:hover, .msgbody:focus {
	border: 2px solid #016a84;
	box-shadow: #016a8499 2px 2px;
}
.title1:hover, .title1:focus {
	border: 2px solid #016a84;
	box-shadow: #016a8499 2px 2px;
}

#remChar {
	border: none;
    background-color: #f7f7f7;
	font-size: 14px;
    font-family: monospace;
}

#noteReceiver {
	width: 90%;
    height: 300px;
	font-size: 14px;
    font-family: sans-serif;
    box-shadow: lightgrey 2px 2px;
}
</style>
<script>
function countChar(){
	var maxRemChar = 256;
	var charCount = document.getElementById('note_body').value.length;
	if (charCount > maxRemChar){
		document.getElementById('note_body').value = document.getElementById('note_body').value.substring(0,maxRemChar);
		charCount = maxRemChar;
	} 
	document.getElementById('remChar').value = parseInt(maxRemChar) - parseInt(charCount);
}

function sendNotif(){
	var x = document.sendnotification;
	if (x.note_title.value == ''){
		alert("Title is Empty!");
		x.note_title.focus();
		return 0;
	} else if (x.note_body.value == ''){
		alert("Message is Empty!");
		x.note_body.focus();
		return 0;
	} else if (x.noteReceiver.value == ''){
		alert("No Receiver was selected!");
		x.noteReceiver.focus();
		return 0;
	} 
	else {
		x.action = "Form_SendNotifications_Action.php?mode=sendNotification";	
		x.submit();
	}	
}

function cancelNotif(){
	var x = document.sendnotification;
	x.note_title.value 		= '';
	x.note_body.value 		= '';
	x.noteReceiver.value 	= '';
	x.message.value 		= '';
	x.remChar.value 		= '';
	x.submit();	
}
</script>
</head>
<body>
<form id='sendnotification' name='sendnotification' method='POST' action=''>
<input type='hidden' name='message' id='message' value='$message'>
HERE;


echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>NOTIFICATION</span></td>
				<td valign='middle' style='height:26px;'>&nbsp;</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'><span class='message'>".htmlspecialchars($message, ENT_QUOTES)."</span></td>";
echo"		</tr></table>";
echo"</td></tr>";


echo"<tr><td id='content-body'>
	<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>
		<table border='0' style='table-layout:fixed' width='100%'><col width=5%><col width=95%>";	

// Notification Title
echo "<tr height='30px'><td><span class='style1'>Title</span></td><td><input type='text' name='note_title' id='note_title' value='$note_title' class='title1' maxlength='45'></td></tr>";
echo "<tr><td colspan='2'>&nbsp;</td></tr>";

echo"   </table></td></tr>";

echo "<tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>
		<table border='0' style='table-layout:fixed' width='100%'><col width=70%><col width=30%>";	
// Notification Body 
//echo "<tr><td colspan='2'><div id='note_body' onClick=\"this.contentEditable='true';\" class='msgbody' onkeyup=\"countChar();\"></div></td></tr>";
echo "<tr><td ><textarea name='note_body' id='note_body' class='msgbody' onkeyup=\"countChar();\"></textarea></td>";

// Select Receivers
echo "<td valign='top' align='center'><select name='noteReceiver[]' id='noteReceiver' multiple>
			<option value=''></option>";
	while ($row = mysql_fetch_array($resReceivers)){
		echo "<option value='$row[1]'>$row[0]</option>";
	}	
echo "	</select></td></tr>";

echo "<tr><td colspan='2'>&nbsp;</td></tr>";

// Show Remaining Characters
echo "<tr><td colspan='2'><span class='style1'>Remaining Characters: </span><input type='text' name='remChar' id ='remChar' value='$remChar' readonly maxlength='256'></td></tr>";

// Send / Cancel Notification
echo "<tr><td colspan='2' align='center'>
			<button type='button' name='btnNotif1' id='btnNotif1' onclick=\"sendNotif();return false;\" title='Send Notification'><span><span>Send</span></span></button>
			<button type='button' name='btnCancel' id='btnCancel' onclick=\"cancelNotif();return false;\" title='Cancel Notification'><span><span>Cancel</span></span></button>
		</td></tr>";
echo "<tr><td colspan='2'>&nbsp;</td></tr>";
echo"   </table></td></tr>"; 
echo"</table></td></tr>"; 



echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo" </table>  ";//end of last table menu



echo"  
</form>
</body>
</html>";

?>