<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// save edit message changes
	// ========================================================================================
	
		
	
	ReadFormArguments();
	Global $FormArguments;
			
	if (count($FormArguments) > 0) {
	
	
		if ($FormArguments['ShowPreview'] != '') {
		// display preview instead of posting the message
	
			include_once('./inc-mb-message_edit_preview.php');		

		}
		else {
		// not in preview mode, so post the message
	
			
		ReadPermissions();
		Global $Permissions;
		
		
		$FormArguments['MessageID'] = intval($FormArguments['MessageID']);
		
		
		$query = "select * from `".$Settings['DBPrefix']."mb-Messages` where RecordNumber=".$FormArguments['MessageID']." limit 1";
		$result = mysql_query($query) or die (mysql_error());
		$row = mysql_fetch_array($result);
		
		$MessageID = $row['RecordNumber'];
		$ThreadID = $row['ThreadID'];
		
		
		$query2 = "select Status from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber=".$ThreadID." limit 1";
		$result2 = mysql_query($query2) or die (mysql_error());
		$row2 = mysql_fetch_array($result2);
		
		$ThreadStatus = $row2['Status'];
		
				
		// if this user is the owner of the message, or has moderator permissions
		if ($row['Poster'] == $ThisUserNumber || $Permissions[202] == 'y') {
		
						
			$PostTime = GetTimestamp();
			
			$query55 = "update `".$Settings['DBPrefix']."mb-Messages` set Subject='".$FormArguments['Subject']."', Message='".$FormArguments['Message']."', LastEditTime='".$PostTime."', LastEditUser='".$ThisUserNumber."' where RecordNumber='".$MessageID."' limit 1";
			$result55 = mysql_query($query55) or die (mysql_error());
			
			
			// if the subject has changed
			if ($FormArguments['Subject'] != $FormArguments['OldSubject']) {
			
				// pull the first message from this thread
				$query = "select * from `".$Settings['DBPrefix']."mb-Messages` where ThreadID=".$ThreadID." order by DateCode asc limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
				
				// if this message being modified is the first message in the thread
				if ($row['RecordNumber'] == $MessageID) {
				
					$query55 = "update `".$Settings['DBPrefix']."mb-Threads` set Title='".$FormArguments['Subject']."' where RecordNumber='".$ThreadID."' limit 1";
					$result55 = mysql_query($query55) or die (mysql_error());
				
				}
			
			
			}
			
			
			$query55 = "select count(RecordNumber) as MessagesInThread from `".$Settings['DBPrefix']."mb-Messages` where ThreadID='".$ThreadID."' and RecordNumber<=".$MessageID." limit 1";
			$result55 = mysql_query($query55) or die (mysql_error());
			$row55 = mysql_fetch_array($result55);
			
			$MessagesInThread = $row55['MessagesInThread'];
			
			
			$MessagePage = intval($MessagesInThread / $mbSettings['MessagesPerPage']);
			if (($MessagesInThread / $mbSettings['MessagesPerPage']) != intval($MessagesInThread / $mbSettings['MessagesPerPage'])) {
				$MessagePage++;
			}
			if ($MessagePage == 0) {
				$MessagePage = 1;
			}
			
			$ThreadMessageNumber = ($MessagesInThread-(($MessagePage-1)*$mbSettings['MessagesPerPage']));
			
								
			
			//header('Location: ' . $MainURL . '/forum/'.$FormArguments['BoardID'].'/'.$FormArguments['ThreadID'].'.html');
			header('Location: ' . $MainURL . '/forum/thread/' . $FormArguments['ThreadID'] . '/' . $MessagePage . '.html#' . $ThreadMessageNumber);
			
			
			
		} // end if allowed to post
		else {
		
			$PageContents = $MsgText[2][79];
		
		}
		
		
		} // end post message (not preview)
		
	}
	else {
		$PageContents = $MsgText[0][98];
	}	
			
	
	
?>
