<?php
include_once($staticpath.'classes/InputButton.php');
$submitButton = new InputButton;
DisplayHeader(".:".ProgramTitle().":.");
//Display Error
//var_dump($_REQUEST);
if(!empty($error) && !empty($sucsess))
{
	if($error)
	{
		DisplayHeader('<IMG SRC=images/x.gif> '.$error);
		$list = '<INPUT TYPE=BUTTON id=backbutton VALUE = '._('Back').' onclick="javascript:history.go(-1);">';
		Prompt(_('Error'),'<FONT color="red">'.$error.'</FONT>',ErrorMessage($errors,'error').'<BR>'.$sucsess,'','Cel',null,null,'get',false);
	}
	if(!empty($sucsess))
	{
		DisplayHeader('<IMG SRC=images/check.gif> '.$sucsess);
		$list = '<INPUT TYPE=BUTTON id=backbutton VALUE = '._('Success').' onclick="location.href=\'modloader.php?servicemod=mod_messaging/MassSendMessageStudents.php\'">';
		Prompt(_('Success'),'<FONT color="blue">'.$sucsess.'</FONT>',ErrorMessage('','error').'<BR>','','Cel',null,null,'get',false);
		unset($_SESSION['message']);
	}
}

if(empty($error) && empty($sucsess))
{
        if(!isset($_REQUEST['folder']))
        {
            $header .= $button->createText(_('Inbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=inbox');
            $header .= $button->createText(_('Outbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=outbox');
            $header .= $button->createText(_('New Message'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=new&servicefunction=create_mail&fcksource=true');
        }
        elseif($_REQUEST['folder']=='inbox')
        {
            $header .= $button->createButton(_('Inbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=inbox');
            $header .= $button->createText(_('Outbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=outbox');
            $header .= $button->createText(_('New Message'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=new&servicefunction=create_mail&fcksource=true');
        }
        elseif($_REQUEST['folder']=='outbox')
        {
            $header .= $button->createText(_('Inbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=inbox');
            $header .= $button->createButton(_('Outbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=outbox');
            $header .= $button->createText(_('New Message'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=new&servicefunction=create_mail&fcksource=true');
        }
        elseif($_REQUEST['folder']=='new')
        {
            $header .= $button->createText(_('Inbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=inbox');
            $header .= $button->createText(_('Outbox'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=outbox');
            $header .= $button->createButton(_('New Message'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=new&servicefunction=create_mail&fcksource=true');
        }
        
        if(!isset($_REQUEST['CREATEPDF']) && $_REQUEST['search_servicefunction']!='search')
            echo TabsHeader($header);
        if($_REQUEST['folder']=='inbox')
        {
            //Delete incoming message
            if($_REQUEST['servicefunction']=='delete')
            {
                 DeletePrompt('сообщение');
                 if($_REQUEST['delete_ok'])
                 {
                    if(get_user('USER_ID') && isset($_REQUEST['id']) && isset($_REQUEST['from_user']))
                    db_execute("DELETE FROM user_in_messages WHERE INCOMING='Y' AND TO_USER = '".get_user('USER_ID')."' AND MESSAGE_ID = '".$_REQUEST['id']."' AND FROM_USER='$_REQUEST[from_user]';");
                 }
            }
            if($_REQUEST['servicefunction']=='read')
            {
                if(get_user('USER_ID') && isset($_REQUEST['id']) && isset($_REQUEST['from_user']))
                {
                    $message=db_request(db_execute("SELECT um.ID,um.TITLE,um.CONTENT,uim.FROM_USER,uim.TO_USER,um.SEND_DATE, coalesce(u.LAST_NAME,' ')".CCAT."' '".CCAT."coalesce(u.FIRST_NAME,' ')".CCAT."' '".CCAT."coalesce(u.MIDDLE_NAME,' ') as USERS  FROM user_messages um,users u,user_in_messages uim where um.ID='$_REQUEST[id]' and uim.from_user=u.user_id AND uim.from_user='$_REQUEST[from_user]';"),array(),array());
                    db_execute("UPDATE user_messages SET NEW_MESSAGE='N' where id='".$message[1]['ID']."';");
                    ShowMessage($message);
                }
            }
            elseif($_REQUEST['delete_ok'] || $_REQUEST['servicefunction']<>'delete')
            {                //List incoming messages
                $inbox_array = db_request(db_execute("SELECT um.ID,um.TITLE,um.CONTENT,um.SEND_DATE, um.NEW_MESSAGE,uim.FROM_USER as FROM_USER_ID ,uim.MESSAGE_ID,coalesce(u.LAST_NAME,' ')".CCAT."' '".CCAT."coalesce(u.MIDDLE_NAME,' ')".CCAT."' '".CCAT."coalesce(u.FIRST_NAME,' ') as FROM_USER FROM user_in_messages uim,users u,user_messages um WHERE INCOMING='Y' AND u.USER_ID = uim.FROM_USER AND uim.message_id=um.id AND  uim.TO_USER = '".get_user('USER_ID')."' ORDER BY SEND_DATE DESC;"),array(),array());
                //$inbox_array = db_request(db_execute("SELECT ID,TITLE,CONTENT,FROM_USER_ID,TO_USER_ID,SEND_DATE FROM user_messages WHERE NEW_MESSAGE='Y' AND TO_USER_ID = '".get_user('USER_ID')."'"));
                $columns = array(
                    'STATUS'=>_('Status'),
                    'FROM_USER'=>_('From User'),
                    'TITLE'=>_('Message Title'),
                    'SEND_DATE'=>_('Date'),
                    );
                //$new = '<img src="images/newmail.png" style="height:15px;" />';
                $new= _('New');
                $old= _('Old');
                $link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&folder=$_REQUEST[folder]&servicefunction=delete";
                $link['remove']['variables'] = array('id'=>'ID','from_user'=>'FROM_USER_ID');
                $link['FROM_USER']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&folder=$_REQUEST[folder]&servicefunction=read";
                $link['FROM_USER']['variables'] = array('id'=>'ID','from_user'=>'FROM_USER_ID');
                foreach ($inbox_array as $key => $value)
                    $inbox_array[$key]['STATUS'] =($value['NEW_MESSAGE'] == 'Y')?$new:$old;
                Gridoutput($inbox_array,$columns,_('Messages'),_('Amount of Messages'),$link,array(),array('search'=>true));
            }
        }
        if($_REQUEST['folder']=='outbox')
        {
           //Delete incoming message
            if($_REQUEST['servicefunction']=='delete')
            {
                 DeletePrompt('сообщение');
                 if($_REQUEST['delete_ok'])
                 {
                    if(get_user('USER_ID') && isset($_REQUEST['id']) && isset($_REQUEST['to_user']))
                    db_execute("DELETE FROM user_in_messages WHERE OUTCOMING = 'Y' AND FROM_USER = '".get_user('USER_ID')."' AND TO_USER = '$_REQUEST[to_user]' AND MESSAGE_ID = '".$_REQUEST['id']."';");
                 }
            }
            if($_REQUEST['servicefunction']=='read')
            {
                if(get_user('USER_ID') && isset($_REQUEST['id']) && isset($_REQUEST['to_user']))
                {
                    $message=db_request(db_execute("SELECT um.ID,um.TITLE,um.CONTENT,uim.FROM_USER,uim.TO_USER,um.SEND_DATE, coalesce(u.LAST_NAME,' ')".CCAT."' '".CCAT."coalesce(u.MIDDLE_NAME,' ')".CCAT."' '".CCAT."coalesce(u.FIRST_NAME,' ') as USERS FROM user_messages um,users u,user_in_messages uim where um.id='$_REQUEST[id]' and uim.to_user=u.user_id and uim.to_user='".$_REQUEST['to_user']."' and uim.from_user='".get_user('USER_ID')."';"),array(),array());
                    ShowMessage($message);
                }
            }
            elseif($_REQUEST['delete_ok'] || $_REQUEST['servicefunction']<>'delete')
            {
                //List incoming messages
//                var_dump(("SELECT um.ID,um.TITLE,um.CONTENT,um.SEND_DATE, um.NEW_MESSAGE,uim.FROM_USER ,uim.MESSAGE_ID,coalesce(u.LAST_NAME,' ')||' '||coalesce(u.MIDDLE_NAME,' ')||' '||coalesce(u.FIRST_NAME,' ') as TO_USER FROM user_in_messages uim,users u,user_messages um WHERE OUTCOMING='Y' AND u.USER_ID = uim.FROM_USER AND uim.TO_USER = '".get_user('USER_ID')."' ORDER BY SEND_DATE DESC;"));
                $outbox_array = db_request(db_execute("SELECT um.ID,um.TITLE,um.CONTENT,um.SEND_DATE, um.NEW_MESSAGE,uim.FROM_USER,uim.TO_USER as TO_USER_ID ,uim.MESSAGE_ID,coalesce(u.LAST_NAME,' ')".CCAT."' '".CCAT."coalesce(u.MIDDLE_NAME,' ')".CCAT."' '".CCAT."coalesce(u.FIRST_NAME,' ') as TO_USER FROM user_in_messages uim,users u,user_messages um WHERE OUTCOMING='Y' AND uim.message_id=um.id AND u.USER_ID = uim.TO_USER AND uim.FROM_USER = '".get_user('USER_ID')."' ORDER BY SEND_DATE DESC;"),array(),array());
                //$inbox_array = db_request(db_execute("SELECT um.ID,um.TITLE,um.FROM_USER_ID,um.TO_USER_ID,um.SEND_DATE, um.NEW_MESSAGE, coalesce(u.LAST_NAME,' ')||' '||coalesce(u.MIDDLE_NAME,' ')||' '||coalesce(u.FIRST_NAME,' ') as TO_USER  FROM user_messages um,users u WHERE u.USER_ID = um.FROM_USER_ID AND FROM_USER_ID = '".get_user('USER_ID')."' ORDER BY SEND_DATE DESC;"));
                //$inbox_array = db_request(db_execute("SELECT ID,TITLE,CONTENT,FROM_USER_ID,TO_USER_ID,SEND_DATE FROM user_messages WHERE NEW_MESSAGE='Y' AND TO_USER_ID = '".get_user('USER_ID')."'"));
                $columns = array(
                    'TO_USER'=>_('TO User'),
                    'TITLE'=>_('Message Title'),
                    'SEND_DATE'=>_('Date'),
                    );
                $new = '<img src="images/new.gif" style="height:15px;" />';
                $link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&folder=$_REQUEST[folder]&servicefunction=delete";
                $link['remove']['variables'] = array('id'=>'ID','to_user'=>'TO_USER_ID');
                $link['TO_USER']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&folder=$_REQUEST[folder]&servicefunction=read";
                $link['TO_USER']['variables'] = array('id'=>'ID','to_user'=>'TO_USER_ID');
                foreach ($outbox_array as $key => $value)
                {
                    if($value['NEW_MESSAGE'] == 'Y')
                    {
                        $outbox_array[$key]['FROM_USER'] = $new.$value['FROM_USER'];
                    }
                }
                Gridoutput($outbox_array,$columns,_('Messages'),_('Amount of Messages'),$link,array(),array('search'=>true));
            }
        }
        elseif($_REQUEST['folder']=='new')
        {
            /****************1 step**************************begin*/
            if($_REQUEST['servicefunction']=='create_mail')
            {
                    echo "<CENTER><FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&folder=$_REQUEST[folder]&servicefunction=check_users&users_search_servicefunction=list&include_inactive=Y&_search_all_schools=Y&search_servicefunction=list method=POST>";

                    echo _('Email Subject').'<INPUT TYPE="text" NAME="title" style="width:700px;"/><BR />';
                    include_once($staticpath."js/fckeditor/fckeditor.php");
                    $oFCKeditor = new FCKeditor('message');
                    $oFCKeditor->BasePath = 'js/fckeditor/';
                    $oFCKeditor->ToolbarSet = 'Letterhead';
                    $oFCKeditor->Width = '800px';
                    $oFCKeditor->Height = '300px';
                    $oFCKeditor->Value = $_REQUEST['mymessage'];
                    echo '<BR><CENTER><fieldset style="width:620px;"><legend>'._('Letter Text').'</legend>'.$oFCKeditor->Create().'</CENTER>';
                    echo '<BR><CENTER>'.$submitButton->createIButton(_('Send'),'sending').'</CENTER>';
					//echo "<input type=submit name='send_msg' value='send' />";
            }
            /****************1 step**************************END*/
            /****************2 step**************************begin*/
            if($_REQUEST['servicefunction']=='check_users')
            {
				
				//$_SESSION['mytitle']=$_POST['title'];
				//echo $_SESSION['mytitle'];
				if($_REQUEST['mymess_profile']!='user' && $_REQUEST['mymess_profile']!='student' && $_REQUEST['mymess_profile']!='group')
					{
					if(empty($_POST['title'])||empty($_POST['message']))
					{
						echo "<p align='center'><font color='red'>Незаполнены поля: тема или сообщение!</font></p>";
						//DisplayHeader('Незаполнены поля: тема или сообщение!');
						$but=$button->createText(_('Назад'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=new&servicefunction=create_mail&fcksource=true');
						echo TabsHeader($but);
						$_REQUEST['msg_change']=false;
					}
					else{
						$_REQUEST['msg_change']=true;
						$_SESSION['mytitle']=htmlspecialchars($_POST['title']);;
						$_SESSION['msg']=htmlspecialchars($_POST['message']);
					}
					}
				if((get_user('PROFILE')=='admin' || get_user('PROFILE')=='teacher')&& $_REQUEST['msg_change']!=false)
				{
					if(!$_REQUEST['mymess_profile'])
					{
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');
					}
					elseif($_REQUEST['mymess_profile']=='user')
					{
						//echo $_SESSION['mytitle'];
						//$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createButton(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');					
					}
					elseif(($_REQUEST['mymess_profile']=='student'))
					{
						$tabl .= $button->createButton(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');
					}
					elseif(($_REQUEST['mymess_profile']=='group'))
					{
						//$tabl .= $button->createButton(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						$tabl .= $button->createButton(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');
					}
					elseif($_REQUEST['mymess_profile']=='parent')
					{
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createButton(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');

					}
				}
				if(get_user('PROFILE')=='student')
				{
					if(!$_REQUEST['mymess_profile'])
					{
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						//$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');
					}
					elseif($_REQUEST['mymess_profile']=='user')
					{
						//$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createButton(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						//$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');					
					}
					elseif(($_REQUEST['mymess_profile']=='student'))
					{
						$tabl .= $button->createButton(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						//$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');
					}
					elseif(($_REQUEST['mymess_profile']=='group'))
					{
						//$tabl .= $button->createButton(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						//$tabl .= $button->createButton(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createText(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');
					}
					elseif($_REQUEST['mymess_profile']=='parent')
					{
						$tabl .= $button->createText(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=student');
						$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=user');
						//$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=group');
						//$tabl .= $button->createButton(_('Parents'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mymess_profile=parent');

					}
				}
				/*elseif(get_user('PROFILE')=='teacher')
				{
				$tabl .= $button->createButton(_('Students'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mess_profile=student');
				$tabl .= $button->createText(_('Users'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mess_profile=user');
				$tabl .= $button->createText(_('GroupOfDelivering'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction='.$_REQUEST['servicefunction'].'&folder='.$_REQUEST['folder'].'&mess_profile=user');
				}*/
				echo TabsHeader($tabl);
				if($_REQUEST['mymess_profile']=='user' || $_REQUEST['mymess_profile']=='student')
				{
					//echo $_SESSION['mytitle'];
					echo '<FORM action="modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder='.$_REQUEST['folder'].'&servicefunction=send&users_search_servicefunction=list&DrawAlign=CENTER" method=POST>';
					if(!$addition['columns_before'])
                            $addition['columns_before'] = array();
                    //$_SYSTEM['user_search'] = true;
                    //$_REQUEST['users_search_servicefunction'] = 'list';
					//$_REQUEST['search_servicefunction'] = 'list';
                    echo '<BR><CENTER>'.$submitButton->createIButton(_('Send'),'sending').'</CENTER>';
                    echo '<INPUT TYPE="HIDDEN" NAME="message" VALUE="'.$_REQUEST['message'].'">';
                    echo '<INPUT TYPE="HIDDEN" NAME="title" VALUE="'.$_REQUEST['title'].'">';
					$addition['link'] = array('FULL_NAME'=>false);
                    $addition['SELECT'] = ",CAST (NULL AS CHAR(1)) AS CHECKBOX";
                    $addition['functions'] = array('CHECKBOX'=>'_makeChooseCheckbox');
                    $addition['columns_before'] = array('CHECKBOX'=>'</A><INPUT type=checkbox value=Y name=controller onclick="checkAll(this.form,this.form.controller.checked,\'user\');CheckAll_hilite(this.form,this.form.controller.checked,\'user\')"><A>');
                    $addition['options']['onclick']="check_clicked(this);";
/*                    $addition['new'] = false;
                    $addition['hide_default_search'] = true;*/
                    //$addition['search_title'] = _('Create Letter');
                    Widgets('all');
					//var_dump($addition);
					//echo $addition;
					//$_REQUEST['search_servicefunction']='list';
                    Search($_REQUEST['mymess_profile'].'_id',$addition);
                    echo "<script>CheckAll_hilite(document.forms[1],true,'user')</script>";
                    echo "</FORM>";
				}
				// отображаем группы
				if($_REQUEST['mymess_profile']=='group' && empty($_REQUEST['DELIVERY_GROUP_ID']))
				{
					$Gridoutput_options = array('save'=>false,'search'=>false,'add'=>true,'xscroll'=>false);
					echo '<TABLE align=center><TR>';
					$title_array = db_request(db_execute("SELECT ID as DELIVERY_GROUP_ID,TITLE,SHORT_NAME,SORT_ORDER FROM DELIVERY_GROUP ORDER BY SORT_ORDER"));
					if(count($title_array))
					{
							if($_REQUEST['group_title_id'])
							{
									foreach($title_array as $key=>$value)
									{
											if($value['DELIVERY_GROUP_ID']==$_REQUEST['DELIVERY_GROUP_ID'])
													$title_array[$key]['row_color'] = Preferences('HIGHLIGHT');
									}
							}
					}
					echo '<TD valign=top>';
					$columns = array('TITLE'=>_('Delivery Group'),'SHORT_NAME'=>_('Short Name'),'SORT_ORDER'=>_('Sort Order'));
					$link =array();
					$link['TITLE']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=check_users&folder=new&mymess_profile=group";
					$link['TITLE']['variables'] = array('DELIVERY_GROUP_ID'=>'DELIVERY_GROUP_ID');
					//$link['add']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&DELIVERY_GROUP_ID=new";
					Gridoutput($title_array,$columns,_('Groups Of Delivery'),_('Amount of Groups Of Delivery'),$link,array(),$Gridoutput_options);
					echo '</TD>';
				}
				if($_REQUEST['DELIVERY_GROUP_ID'] && $_REQUEST['mymess_profile']=='group')
					{
					echo "<p align='center'><font color='red'>Групповая рассылка в стадии разработки</font></p>";
					//$butt=$button->createText(_('Назад'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&servicefunction=check_users&folder=new&mymess_profile=no');
					//echo TabsHeader($butt);
					}
            }
            /****************2 step**************************END*/
            /****************3 step**************************begin*/
            if($_REQUEST['servicefunction']=='send')
            {
				if(empty($_REQUEST['user']))
				{
					//echo "Массив пустой";
					//echo "-->".$_SESSION['mytitle']."<br/>".$_SESSION['msg'];
				}
				else
				{
					//echo $_REQUEST['title']."<br/>".$_REQUEST['message'];
					$id = db_request(db_execute('SELECT '.db_seq_nextval('user_messages_seq').' as SEQ_ID '.FROM_DUAL));
					$id = $id[1]['SEQ_ID'];
					//var_dump($_REQUEST['user']);
					db_execute ("INSERT INTO user_messages (ID,TITLE,CONTENT,SEND_DATE) values('$id','$_SESSION[mytitle]','$_SESSION[msg]','".date("Y-m-d H:i:s")."');");
				   foreach ($_REQUEST['user'] as $user_id=>$Send)
					{
						if(!(db_execute("INSERT INTO user_in_messages (MESSAGE_ID,INCOMING,FROM_USER,TO_USER) VALUES ('$id','Y','".get_user('USER_ID')."','$user_id');")
							 &&
							 db_execute("INSERT INTO user_in_messages (MESSAGE_ID,OUTCOMING,FROM_USER,TO_USER) VALUES ('$id','Y','".get_user('USER_ID')."','$user_id');"))
						  )
							$false=true;

					}
					if(!$false)
					{
							$sucsess='Сообщение успешно отправленно';
							DisplayHeader('<IMG SRC=images/check.gif> '.$sucsess);
							$list = '<INPUT TYPE=BUTTON id=backbutton VALUE = '._('Success').' onclick="location.href=\'modloader.php?servicemod=mod_messaging/MassSendMessageStudents.php\'">';
							Prompt(_('Success'),'<FONT color="blue">'.$sucsess.'</FONT>',ErrorMessage('','error').'<BR>','','Cel',null,null,'get',false);
					}
				}
            }
            /****************3 step**************************END*/

    }
}

function _makeChooseCheckbox($value,$title)
{	global $THIS_array;
	return "<INPUT onchange='hl_cb(this)' type=checkbox name=user[".$THIS_array['USER_ID']."] value=Y>";
}
function ShowMessage($content)
{
    global $submitButton,$button,$header;
    $alternate_row_color = " bgcolor=\"".Preferences('COLOR')."\"";
    $colorline = Preferences('HIGHLIGHT');
    $TDleft = "style='border-width:1px;border-color:".Preferences('HIGHLIGHT').";border-style:none none inset solid;' valign=top align=".ALIGN_LEFT."";
    $TDright = 'style="border-width:1px;border-color:'.Preferences('HIGHLIGHT').';border-style:none solid inset none;"';
    echo '<CENTER><TABLE cellspacing=0 cellpadding=2 border=1 width=600px>';
		echo '<TR>
                    <TD colspan=2  background=themes/'.Preferences('HIGHLIGHT_FRAME').'/bottom_shadow.png height=20 style="border-width:1px;border-color:gray;border-style:solid solid solid solid;background-repeat:repeat-x;" valign=top align='.CENTER.'>
                            <font color=black><B>'._('Outgoing Message').'</B></font>
                    </TD>
                    </TR>';
		echo '<TR BGCOLOR=#'.Preferences('HIGHLIGHT_BACKGROUND').'>
                        <TD '.$TDleft.' width=300>
                            <font color=gray>'._('FROM ').'</font>'.$content[1]['USER'].'
                        </TD>
                        <TD '.$TDright.'>
                            <font color=gray>'._('DATE ').'</font>'.$content[1]['SEND_DATE'].'
                        </TD>
                        </TR>';
		echo '<TR>
                        <TD '.$TDcenter.' >
                            <font color=gray>'._('Subject').'</font> '.$content[1]['TITLE'].'</TD>
                        </TD>
                        <TD '.$TDcenter.' >';
                        $header  = $button->createText(_('Delete Message'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder='.$_REQUEST['folder'].'&servicefunction=delete&id='.$content[1]['ID'].'&from_user='.$content[1]['FROM_USER']);
                        $header  .= $button->createText(_('Back To List'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder='.$_REQUEST['folder']);
                        //$header .= $button->createButton(_('Reply'),'modloader.php?servicemod='.$_REQUEST['servicemod'].'&folder=new&servicefunction=create_mail&fcksource=true');
                        if(!isset($_REQUEST['CREATEPDF']) && $_REQUEST['search_servicefunction']!='search')
                        echo TabsHeader($header,$save_button);
                //echo '<INPUT TYPE="button" VALUE="'._('Back').'" onClick="history.back()">';
                echo '</TD>
                        </TD>
                      </TR>';
		echo '<TR BGCOLOR=#'.Preferences('HIGHLIGHT_BACKGROUND').'>
                    <TD '.$TDleft.' colspan=2>
                        <font color=black>'.$content[1]['CONTENT'].'</font></TD></TR>';
		echo '</TABLE></CENTER>';
}
?>
