<?

/*
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
*/



	// ========================================================================================
	// send new message
	// ========================================================================================
	
	$query = "select * from `".$Settings['DBPrefix']."nl-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$nlSettings[$row['Name']] = $row['Value'];
	}
	
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_newsletter.gif" width="22" height="21"> '.$MsgText[5][2];
	$BrowserTitle = $MsgText[5][2];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[5][2];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[501] == 'y') {
		
			ReadFormArguments();
			Global $FormArguments;
	
			if (count($FormArguments) > 0) {
			
			if ($FormArguments['ShowPreview'] != '') {
			// display preview instead of posting the message
	
				include_once('./inc-nl-admin-newsletter_preview.php');		

			}
			else {
			// not in preview mode, so post the message	
				
				$query = "select * from `".$Settings['DBPrefix']."nl-Messages` where Title='".$FormArguments['Title']."' and DateCode>'".(GetTimestamp()-(60*60*4))."' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				if (mysql_num_rows($result) == 0) {
				
					
					if ($FormArguments['PageType'] == 2) {
						$FormArguments['Message'] = $FormArguments['PageContents2'];
					}
					else {
						$FormArguments['Message'] = $FormArguments['PageContents'];
					}
					
				
			
					$SendTime = GetTimestamp();
							
						
					$query = "insert into `".$Settings['DBPrefix']."nl-Messages` (Title,Message,MailFormat,DateCode,Sender,SentToCount) values ('".$FormArguments['Title']."','".$FormArguments['Message']."','".$FormArguments['MailFormat']."','".$SendTime."','".$UserNumber."','0')";
					$result = mysql_query($query) or die (mysql_error());
					$RecordNumberCreated = mysql_insert_id();
					
					
					$query2 = "select RecordNumber from `".$Settings['DBPrefix']."nl-Addresses` where DateAdded<='".$SendTime."'";
					$result2 = mysql_query($query2) or die (mysql_error());
					while ($row2 = mysql_fetch_array($result2)) {
						
						$query3 = "insert into `".$Settings['DBPrefix']."nl-SendLog` (MessageID, AddressID) values ('".$RecordNumberCreated."','".$row2['RecordNumber']."')";
						$result3 = mysql_query($query3) or die (mysql_error());
											
					}
					
					
					header('Location: '.$MainURL.'/admin/newsletter_sendfinal/'.$RecordNumberCreated.'/start.html');
										
					
				} // end if no duplicate found
				else {
						
					$PageContents = '<br>'.$MsgText[5][72];
						
				}
				
			} // end if not previewing
		
			}
			else {
				$PageContents = $MsgText[0][98];
			}
			
			
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}
		


?>
