<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php"><font color="blue">log in here.</font></a>';
	exit;
	}
 if($_SESSION['roladmin'] ==1){$roladmin=1;}
//require_once ('mysql_connect.php');
$action          = $_GET["a"];
include ('includes/admin-config.php');
	//echo 'Action = ' . $action; (for testing)
//require_once ('config.inc');
if ($action == "test") {
	//include test script
	$phone_num=$_POST['phone_number'];
	include('sendSMS.php');
	
}



if ($action == "update") {

		$query = "UPDATE storePrefs SET text_notice_phone='" . $_POST['text_notice_phone'] . "'";

		//query is created, now execute:
		if (!mysql_query($query)){
					echo $query;
  					die('Error: ' . mysql_error());
 			 } else {
			//echo $query .'<br>';	//for testing
				echo "<div align=center><font color=red>Text notice preferences updated successfully.</font></div>";
				
			}		


	}	//end of update query

$query ="select text_notice_subscribed from admin_options";	
$rt=mysql_query($query);
while($nt=mysql_fetch_array($rt)){
	$subscribed=$nt['text_notice_subscribed'];
}	
	if($subscribed !='1'){
		echo "You must be subscribed to this service in order to receive text notifications. Please contact your web service provider.";
	} else {
		$query = "SELECT * from storePrefs";
		$rt=mysql_query($query);          // query executed 
				//echo mysql_error();                    // if any error is there that will be printed to the screen
		while($nt=mysql_fetch_array($rt)){
			$text_phone = $nt['text_notice_phone'];
		}
		echo '<div align="center"><table border=0 cellpadding=0 cellspacing=8>';
		//record found - 
		echo '<tr><td><form action="' . $_SERVER['PHP_SELF'] . '?a=update&key=' . $nt['recordKey'] . '" method="post">';
		echo '<tr><td>Text order notification phone#: <input type="text" name="text_notice_phone" size="20" value="'.$text_phone.'">';
		echo "<br><br>This phone number will receive text message alerts each time an online order is placed.<br>
		<i>If you just set this up, please send a test to verify you will receive them.</i>";
		echo '<tr><td valign=top colspan=2><div align=right><input id="save-button" type="submit" name="save" value="Save Changes"></form>';
		echo ' <form action="' . $_SERVER['PHP_SELF'] . '?a=test&key=' . $nt['recordKey'] . '" method="post">';
		echo '<input type="hidden" name="phone_number" value="'.$text_phone.'"><input id="save-button" type="submit" name="save" value="Send Test"></form>';
		
		echo '</table><br><br>';
		echo '<br></div></table></div>';
	}


	

?>