<?php
include ("header.php");

// Database connection variables 
$dbServer = $mysqlserver; 
$dbDatabase = $mysqldatabase; //contactus
$dbUser = $mysqlusername; 
$dbPass = $mysqlpassword;

echo "<h2 style=\"font:Arial; color:#ae0011;\">Mail Room</h2>";
$sConn = mysql_connect($dbServer, $dbUser, $dbPass) 
or die("Couldn't connect to database server"); 
$dConn = mysql_select_db($dbDatabase, $sConn) 
or die("Couldn't connect to database $dbDatabase");
if ($_GET['statustop'] != "")
$status = $_GET['statustop']; // this be the real magic
else if ($_GET['status'] != "")
$status = $_GET['status']; // this be the real magic
else $status = "";

if (isset($_GET['search']))
$searchperson = trim($_GET['search']); // this be the real magic
else if (isset($search)) $searchperson = trim($search);
else $searchperson = "";
//select from database all the referers
if ($status == "Active")
$dbQuery = "SELECT COUNT(*) AS numrows FROM mailinglist WHERE (Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%'  || Coupon LIKE '%" . $searchperson . "%') AND `status` = 'Active';";
elseif ($status == "Inactive")
$dbQuery = "SELECT COUNT(*) AS numrows FROM mailinglist WHERE (Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%'  || Coupon LIKE '%" . $searchperson . "%') AND `status` = 'Inactive';";
elseif ($status != "")
$dbQuery = "SELECT COUNT(*) AS numrows FROM mailinglist WHERE (Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%'  || Coupon LIKE '%" . $searchperson . "%' || userID LIKE '%" . $searchperson . "%') AND `deliveryStatus` = '" . $status . "';";
else 
$dbQuery = "SELECT COUNT(*) AS numrows FROM mailinglist WHERE Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%'  || Coupon LIKE '%" . $searchperson . "%' || userID LIKE '%" . $searchperson . "%';";
//echo $dbQuery;
$result = mysql_query($dbQuery) or die("Couldn't get the Contacts");
$row = mysql_fetch_array($result); 
$nume = $row['numrows'];
if(!isset($start)) { // This variable is set to zero for the first page
$start = 0;
}
$eu = ($start - 0);
$limit = 100; // No of records to be shown per page.
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
?>
 <script language="javascript">
    
			function sendRequest(formid) {
			//alert(formid);
			var form = $(formid);
			var userID = Form.Element.getValue(form['userID']);
			var Coupon = Form.Element.getValue(form['Coupon']);
			var Notes = Form.Element.getValue(form['Notes']);
			var status1 = Form.Element.getValue(form['status1']);
//			alert(formid);
				var formData = 'userID='+userID+'&Coupon='+Coupon+'&Notes='+Notes+'&status1='+status1;
			//alert(formData);
				new Ajax.Request("mailerUpdate.php", 
					{ 
					method: 'post', 
					postBody: formData,
					onComplete: function(response) {
							showResponse(response,userID);
							}
					});
				}
			function showResponse(req,id){
				$('show'+id).innerHTML= req.responseText;
			}
            
            function trim(str, chars) {
              return ltrim(rtrim(str, chars), chars);
            }
 
            function ltrim(str, chars) {
                chars = chars || "\\s";
                return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
            }
             
            function rtrim(str, chars) {
                chars = chars || "\\s";
                return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
            }
            
         
            
</script>

<style>
#text_close{
    background: #ae0011;
    font-weight:bold;
    text-align:center;
    color:white;
}
</style>
<form name="sorting" action= "mailroom.php" method="get">
<?php
$result1 = mysql_query ("SELECT DISTINCT `deliveryStatus` FROM `mailinglist`");
 echo "<select name=\"statustop\" id=\"statustop\">\r";
 ?>
 <option value="" <? if($status=="") echo "selected=\"selected\"";?>> - None - </option>
 <option value="Active" <? if($status=="Active") echo "selected=\"selected\"";?>> Active </option>
  <option value="Inactive" <? if($status=="Inactive") echo "selected=\"selected\"";?>> Inactive </option>
 <?php
 if ($srow = mysql_fetch_array($result1)) {
   do {
   if ($srow["deliveryStatus"] == "")
		  continue;
    $selected = "" ;
     if ($srow["deliveryStatus"] == $status)
		  $selected="selected";
   print "<option $selected value=\"" . $srow["deliveryStatus"]."\">" . $srow["deliveryStatus"]."\r";
	 } while($srow = mysql_fetch_array($result1));
 }
?>
</select>
&nbsp; | &nbsp; Search: <input type="text" value="" name="search" />
<input type="submit" value="sort & search"/> &nbsp; | &nbsp; <a class="numbering" href="mailerLoad.php">Renew List</a> <BR />&nbsp; | &nbsp; <a class="numbering" href="mailerLoadTy.php">Add Thank you Addresses</a> &nbsp; | &nbsp; <a class="numbering" href="mailerLoadCat.php">Add From Catalog Requests</a> &nbsp; | &nbsp; <a class="numbering" href="mailerExport.php" target="_blank">Export Active </a> &nbsp; | &nbsp; <a class="numbering" href="mailerAdd.php" target="_blank">Set Contacts to Inactive</a>
<!--&nbsp; | &nbsp; <a class="numbering" href="mailerLoad.php" target="_blank">Regenerate Mail Room </a>-->
</form>
<?
if ($status == "Active")
$dbQueryRef = "SELECT * FROM mailinglist WHERE (Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%'  || Coupon LIKE '%" . $searchperson . "%') AND `status` = 'Active' ORDER BY 'userID' ASC limit $eu, $limit;";
elseif ($status == "Inactive")
$dbQueryRef = "SELECT * FROM mailinglist WHERE (Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%'  || Coupon LIKE '%" . $searchperson . "%') AND `status` = 'Inactive' ORDER BY 'userID' ASC limit $eu, $limit;";
elseif ($status != "")
$dbQueryRef = "SELECT * FROM `mailinglist` WHERE (Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%' || Coupon LIKE '%" . $searchperson . "%' || userID LIKE '%" . $searchperson . "%') AND `deliveryStatus` = '" . $status . "' ORDER BY 'userID' ASC limit $eu, $limit;";
else
$dbQueryRef = "SELECT * FROM `mailinglist` WHERE Name LIKE '%" . $searchperson . "%' || Address1 LIKE '%" . $searchperson . "%'  || Coupon LIKE '%" . $searchperson . "%' || userID LIKE '%" . $searchperson . "%' ORDER BY 'userID' ASC limit $eu, $limit;";
//echo $dbQueryRef;
$result = mysql_query($dbQueryRef) or die("Couldn't get the Connectors 2");
//$rowConnector = mysql_fetch_array($result); 
echo "<table align = 'center' width='100%'><tr><td align='left' width='10%'>";
if($back >=0) {
print "<a href='mailroom.php?start=$back&status=$status&search=$searchperson' class='numbering'><< PREV</a>";
}
echo "</td><td align=center width='60%'>";
$i=0;
$l=1;
for($i=0; $i<$nume; $i=$i+$limit){
if($i <> $eu){
echo " <a href='mailroom.php?start=$i&status=$status&search=$searchperson' class='numbering'>$l</a> ";
}
else { echo "<font face='Verdana' size='4' color='#ae0011'>$l</font>";
} 
/// Current page is not displayed as link and given font color red
$l=$l+1;
}
echo "</td><td align='right' width='10%'>";
if($this1 < $nume) {
print "<a href='mailroom.php?start=$next&status=$status&search=$searchperson' class='numbering'>NEXT >></a>";
}
echo "</td>";
if ($limit > $nume)
$onpage = $nume;
else
$onpage = $limit;
echo "<td align='right' width='20%' class='numbering'>Displaying $onpage of $nume </td>";
echo "</tr></table>";
?>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#111111" width="100%">
<tr bgcolor="#293b74">
<td width="25%" height="21"> 
<p style="margin-left: 10"><b><font size="2" face="Verdana" color="#FFFFFF"> 
Name & Company</font></b></td>
<td width="25%" height="21"> 
<p style="margin-left: 10"><b><font face="Verdana" size="2" color="#FFFFFF"> 
Full Address</font></b></td>
<td width="25%" height="21"> 
<p style="margin-left: 10"><b><font face="Verdana" size="2" color="#FFFFFF"> 
Status</font></b></td>
<td width="25%" height="21"> 
<p style="margin-left: 10"><b><font face="Verdana" size="2" color="#FFFFFF"> 
Notes & Save</font></b></td>
</tr>
<? 
while($row = mysql_fetch_array($result)) 
{ 
?>
<a href="label<?= $row["userID"]; ?>"><tr <?php echo ($row['deliveryStatus'] == "") ? "bgcolor='#ffffff'" : "bgcolor='#cecece'"; ?> >
<form name="recordUpdate<?= $row["userID"]; ?>" id="recordUpdate<?= $row["userID"]; ?>" onsubmit="return false;">
<td width="25%" height="21"> 
<b>User ID: <?php echo $row["userID"]; ?> </b>
<br />
<b><?php echo $row["Name"]; ?> </b>
<br />
<?php echo $row["Title"]; ?> 
<br />
<b><?php echo $row["Company"] . "</b>";
echo '<br /><br />';
echo '<b>Main Phone:</b>' . $row["Telephone"];
?>
</td> 
<td width="25%" height="21"> 
<?php echo $row["Name"] . "<br><br>"; echo $row["Address1"] . "<br>";
    if($row["Address2"] != "") echo $row["Address2"] . "<br>"; 
	echo $row["City"] . ", " . $row["State"] . " " . $row["Zip"] . " " . $row["Country"]; 
?> 
<a style="float:right;" href="mailedit.php?userid=<?= $row["userID"]; ?>" target="_blank">edit &raquo; </a>
</td> 
<td width="25%" height="21">
<table><tr>
<td>
Delivery Status:
<?php
echo $row['deliveryStatus'];
?>
<br />
Delivery Code:
<?php
echo $row['deliveryCode'];
?>
<BR /><BR />
Set Coupon: <input type="text" name="Coupon" id="Coupon" width="5" value="<?= $row["Coupon"]; ?>" />
<BR /><BR />
Set Status:
<?php
$result1 = mysql_query ("SELECT * FROM `status-mailer`");
 echo "<select name=\"status1\" id=\"status1\">\r";
 if ($srow = mysql_fetch_array($result1)) {
   do {
    $selected = "" ;
     if ($srow["status"] == $row["status"])
		  $selected="selected";
   print "<option $selected value=\"" . $srow["status"] ."\">" . $srow["status"]."\r";
	 } while($srow = mysql_fetch_array($result1));
 }
?>

</td>
</tr>

</table>
</td> 
<td width="20%" height="21">
<b>Date Added:</b> <?php echo date('Y-m-d',$row["Date"]); ?> 
<?php
if ($row["LastDate"] != '')
{ 
?>
<b>Last Updated:</b> <?php echo date('Y-m-d',$row["LastDate"]); ?> 
<? } ?>

<br /> Notes:
<br />
<textarea name="Notes" id="Notes"><?= $row["Notes"]; ?></textarea>
<input type="hidden" name="userID" id="userID" value="<?= $row["userID"]; ?>" />
<input style="padding:10px; margin:10px;" type="submit" value="update" onClick="sendRequest('recordUpdate<?= $row["userID"]; ?>')" align="right">
</form>
<div id="show<?= $row["userID"]; ?>" style="color:#AE0011; font-weight:bold;"></div>
</td> 
</tr>
<?php 
}
echo "</table>";
?>
		
<?php
/**
Remove non-numeric characters from a string.

function stripNonNumeric($str='') {
return preg_replace('(\D+)', '', $str);
}**/

?>
