<?php 
	require_once('common.php');
	checkUser();
	include("header.php");
	include("constants.php");
?>
		<!-- content -->
		<div id="content">
			<!-- table -->
			<div class="box">
				<!-- box / title -->
				<div class="title">
					<h5>Jimmy Clean's - Send News Letter</h5>
				
				</div>
				<!-- end box / title -->
				<div class="box">
				
				
				
				
					<?php
  if (isset($_POST['submit'])) {
    $from = 'Jimmy@jimmycleans.com';
    $subject = $_POST['subject'];
    $template_choice = "email_templates/" . $_POST['template_choice'];
    $output_form = false;



    if (empty($subject)) {
      echo '
	  <div class="messages">
						<div id="message-error" class="message message-error">
							<div class="image">
								<img src="images/icons/error.png" alt="Error" height="32" />
							</div>
							<div class="text">
								<h6>Error Message</h6>
								<span>Please enter a subject line for the news letter</span>
							</div>
							<div class="dismiss">
								<a href="#message-error"></a>
							</div>
						</div>
					</div>
	  ';
	  
      $output_form = true;
    }

  }
  else {
    $output_form = true;
  }

  if ((!empty($subject))) {
    $dbc = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME)
      or die('Error connecting to MySQL server.');

    $query = "SELECT * FROM ". DB_TABLE;
    $result = mysqli_query($dbc, $query)
      or die('Error querying database.');

    while ($row = mysqli_fetch_array($result)){
      $to = $row['email'];
      $first_name = $row['first_name'];
      $last_name = $row['last_name'];
      
   


    


	$fd = fopen($template_choice,"r");
	$inject_tracker = fread($fd, filesize($template_choice));
	$tracker_code="<img src='http://jimmycleans.com/jc-newsletter/track.php?eid=$to'>"; 
	$msg1 = str_replace("??FIRSTNAME??", $first_name, $inject_tracker);
	$msg2 = str_replace("??LASTNAME??", $last_name, $msg1);
	$msg = str_replace("??TRACKERIMAGE??", $tracker_code, $msg2);

	$headers = "MIME-Version: 1.0\n" ;
      	$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; 
	$headers .= "From: $from\r\n";
	  
      mail($to, $subject, $msg, $headers);
      echo '
	  <div class="messages">
			<div id="message-success" class="message message-success">
							<div class="image">
								<img src="images/icons/success.png" alt="Success" height="32" />
							</div>
							<div class="text">
								<h6>Success</h6>
								<span>Email sent to: ' . $to . '<br /></span>
											
</div>
							<div class="dismiss">
								<a href="#message-success"></a>
							</div>
			</div>
			</div>
	  
	  ';
	  
    } 

    mysqli_close($dbc);
  }

  if ($output_form) {
?>
					<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
						<div class="form">
							<div class="fields">
								<div class="buttons">
									<div class="highlight">
														<?php include("js/upload.js.php"); ?>
										<div id="upload" ><span>Upload News Letter<span></div><span id="status" ></span>
												
												<ul id="files" ></ul>
														
									</div>
								</div>
								<div class="field">
									<div class="label">
										<label for="select">Select email template:</label>
									</div>
									<div class="select">
									 <script type="text/javascript">
												  $(document).ready(function() {
										   $("#select_letter").change(function() {
											 $("#display_newsletter").empty();
											 if ( $("#select_letter").val()!="" ){
												$("#display_newsletter").html('<iframe src ="http://jimmycleans.com/jc-newsletter/email_templates/' + $("#select_letter").val() + '" width="100%" height="500px">');
											 }
											 else{
												$("#display_newsletter").append("Displays Selected News Letter Template");
											 }
										   });
										 });
										</script>
										<select id="select_letter" name="template_choice">
											<option value=""> - Select Template - </option>

											<?php 
											$dir = "email_templates"; 
											$dir = opendir ("email_templates");
											while (false !== ($file = readdir($dir))) {
												if (strpos($file, '.html',1)||strpos($file, '.php',1) ) {
												print "<option value=\"{$file}\">{$file}</option>"; 
											}
											} 
											?>
										</select>
										
									</div>
								</div>
								
								<div class="field">
									<div id="display_newsletter">Displays Selected News Letter Template</div>
								</div>
		
								
								<div class="field">
									<div class="label">
										<label for="subject">Subject of email:</label><br />
									</div>
									<div class="input">
										<input id="subject" class="medium" name="subject" type="text" value="<?php echo $subject; ?>" size="30" /><br />
									</div>
								</div>
						
								<div class="buttons">
									<div class="highlight">
										<input type="submit" id="submit-button" class="add_email" name="submit" value="Submit">
									</div>
								</div>	
					
			
							</div>
						</div>
				</form>
				<?php
  }
?>
			</div>
			<!-- end box -->
			
		
			
			
			<!-- end box / right -->
		</div>
		<!-- end content -->
		<?php
include("footer.php");
?>