<? 
require_once "include/application.php"; 
require_once "include/function.php";
require_once "include/parameter.php";

if($btnSave || $btnSend){
	$err=true;
	
	if($err){
		$senddate=$year."-".$month."-".$day;
		$sql="
		INSERT INTO `sms_alert` (
		`smsa_name` ,
		`smsa_template` ,	
		`smsa_pselect` ,
		`smsa_title` ,
		`smsa_role` ,
		`smsa_race` ,
		`smsa_status` ,
		`smsa_gender` ,
		`smsa_ageformula` ,
		`smsa_age` ,
		`smsa_type` ,
		`smsa_industry` ,
		`smsa_source` ,
		`smsa_classification` ,
		`smsa_country` ,
		`smsa_cselect` ,
		`smsa_cband` ,
		`smsa_ctype` ,
		`smsa_cindustry` ,
		`smsa_csource` ,
		`smsa_cclassification` ,
		`smsa_ccountry` ,
		`smsa_dob` ,
		`smsa_spdob` ,
		`smsa_senddate` ,
		`smsa_created` ,
		`smsa_sender` ,
		`smsa_user` 
		)

	VALUES (
	\"$name\",'$template','$p_select','$title', '$role','$race','$status', '$gender','$formula', '$age','$type', '$industry', '$source', '$classification', \"$country\",
	'$c_select','$c_band','$c_type', '$c_industry', '$c_source', '$c_classification', \"$c_country\",'$dob','$spdob','$senddate',CURRENT_TIMESTAMP,'$sender','".$_SESSION["egoUser"]."')
	";
		@mysql_query($sql) or die(mysql_error());
		$iid=mysql_insert_id();		
		
		if($btnSave){		
			print("<script type='text/javascript'>alert('SMS Scheduler Added.')</script>");

		}else if($btnSend){
			$n=SMSalert_sent($iid,'Now',$_SESSION["egoUser"]);	
			print("<script type='text/javascript'>alert('$n SMS Sending...')</script>");			
		}
		print("<script type='text/javascript'>location.href='sms_alertSearch.php'</script>");
	}
}

if(!$p_select)$p_select="";
if(!$c_select)$c_select="";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="sddm.css" >
<script src="include/validation.js" type="text/javascript"></script>

<title><? echo TITLE?></title>
</head>
<body onload="enable_person_alert('<? echo $p_select ?>');enable_company_alert('<? echo $c_select ?>')">

<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#f1efd8">
  <tr>
    <td  colspan="2" align="center"><? require_once "include/header.php"; ?></td>
  </tr>
  <tr>
    <td colspan="2" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td width="185" valign="top" bgcolor="e6f3f9">
	
<? require_once "include/columnLeft.php"; ?>	</td>
    <td width="834" valign="top">
	<form name="form" method="post" action="<?=$PHP_SELF ?>" onsubmit="return ValidateAddSmsAlert(this);">
	  <table width="810" border="0" align="center" cellpadding="0" cellspacing="0">
      
      <tr>

        <td valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td width="50%" height="30" class="TitleH3">New SMS Scheduler </td>
            <td width="50%" class="TitleH3"><span class="require">SMS Credit Balance : <? echo SMSBalance() ?></span></td>
          </tr>
          <tr>
            <td height="1" colspan="2" bgcolor="#B8ED83"></td>
          </tr>
          <tr>
            <td colspan="2" ><? echo "<div class='Msg'>$action_msg</div>" ?>&nbsp;</td>
          </tr>

        </table>
          <table width="100%" border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td width="28%" height="15" class="TextH1">Choose Template : <span class="require">*</span></td>
              <td width="72%"><span class="TextH1">
                <select name="template" id="template">
                  <option value=""></option>
                  <?
				if(array_search("viewall",$permlist)==true)$sql="select * from sms_template where smst_id > '0' order by smst_name";
				else $sql="select * from sms_template where smst_user='".$_SESSION["egoUser"]."' order by smst_name";
	 
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["smst_id"]."' ";
					if($template==$myrow["smst_id"])echo "selected";
					echo ">".$myrow["smst_name"]." </option>";
				}
			
?>
                </select>
              </span></td>
            </tr>
            <tr>
              <td height="14" class="TextH1">SenderID :<span class="require"> *</span></td>
              <td><input name="sender" type="text" id="sender" value="<? echo $sender ?>" size="20" maxlength="20" /></td>
            </tr>
            <tr>
              <td colspan="2"><table width="100%"  border="0" cellpadding="1" cellspacing="1" bgcolor="#FFCC33">
                <tr>
                  <td class="TextH1"><span class="MenuTitle">Choose Person Group </span>
                      <select name="p_select" id="p_select" onchange="enable_person_alert(this.value)">
                        <option value="None">None</option>
                        <option value="All">All Members</option>
                        <option value="Individual">Individual</option>
                    </select></td>
                  <td><span class="MenuTitle">Choose Company Group</span><span class="TextH1">
                    <select name="c_select" id="c_select" onchange="enable_company_alert(this.value)">
                      <option value="None">None</option>
                      <option value="All">All Members</option>
                      <option value="Individual">Individual</option>
                    </select>
                  </span></td>
                </tr>
                <tr>
                  <td class="TextH1"><table class='person' style=display:'none' width="100%" border="0" cellspacing="1" cellpadding="1">
	                <tr >
                      <th width="28%" height="23" class="TextH1"><div align="left">Title</div></th>
                      <td width="72%" class="TextH1"><span class="main">
                        <select name="title" id="title" style="WIDTH: 180px" >
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Title' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($title==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                        </select>
                      </span></td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Role </div></th>
                      <td class="TextH1"><select style="WIDTH: 180px"
      name="role" id="role">
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Role' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($role==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>

                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Gender</div></th>
                      <td class="TextH1"><select name="gender" id="gender" style="WIDTH: 180px">
                          <option value=""></option>
                          <option value="Male">Male</option>
                          <option 
        value="Female">Female</option>
                        </select></td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Age</div></th>
                      <td class="TextH1"><select name="formula" id="formula">
                        <option value=""></option>
                        <option value="=">Equal</option>
                        <option value="<">Bigger than</option>
                        <option value=">">Smaller than</option>
                        <option value="<=">Bigger &amp; equal than</option>
                        <option value=">=">Smaller &amp; equal than</option>
                      </select>
                        <input name="age" type="text" id="age" value="<? echo $age ?>" size="2" maxlength="2" /></td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Race</div></th>
                      <td class="TextH1"><select style="WIDTH: 180px"
      name="race" id="race">
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Race' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($race==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>

                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Status</div></th>
                      <td class="TextH1"><select style="WIDTH: 180px"
      name="status" id="status" >
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Status' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($status==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Type</div></th>
                      <td class="TextH1"><select 
      name="type" id="type" style="WIDTH: 180px"> 

                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Type' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($type==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                  
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Industry</div></th>
                      <td class="TextH1"><select 
      name="industry" id="industry" style="WIDTH: 180px">
      
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Industry' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($industry==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Source</div></th>
                      <td class="TextH1"><select 
      name="source" id="source" style="WIDTH: 180px"> 

                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Source' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($source==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                       
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Classification</div></th>
                      <td class="TextH1"><select 
      name="classification" id="classification" style="WIDTH: 180px" >

                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Classification' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($classification==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                          
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Country</div></th>
                      <td class="TextH1"> <select name="country" id="country">
						<option value=""></option>
						                <? require "country.inc"; ?>
										</select></td>
                    </tr>
                    
                  </table></td>
                  <td valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="1" class='company' style=display:'none'>
	                <tr >
                      <th width="31%" height="23" class="TextH1"><div align="left">Employee band</div></th>
                      <td width="69%" class="TextH1"><select name="c_band" id="c_band" style="WIDTH: 180px" >

                        <option value=""></option>
                        <?
			 
			 	$sql="select * from newvalue where nv_type='Band' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($role==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                      </select></td>
                    </tr>

                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Type</div></th>
                      <td class="TextH1"><select 
      name="c_type"  style="WIDTH: 180px">
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Type' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($c_type==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Industry</div></th>
                      <td class="TextH1"><select 
      name="c_industry"  style="WIDTH: 180px">
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Industry' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($c_industry==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Source</div></th>
                      <td class="TextH1"><select 
      name="source" style="WIDTH: 180px">
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Source' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($c_source==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                        </select>                      </td>
                    </tr>
                    <tr class="editTbl">
                      <th height="23" class="TextH1"><div align="left">Classification</div></th>
                      <td class="TextH1"><select 
      name="c_classification" style="WIDTH: 180px" >
                          <option value=""></option>
                          <?
			 
			 	$sql="select * from newvalue where nv_type='Classification' order by nv_id";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."' ";
					if($c_classification==$myrow["nv_id"])echo "selected";
					echo ">".$myrow["nv_name"]." </option>";
				}
			
?>
                        </select>                      </td>
                    </tr>
                    <tr>
                      <th height="23" class="TextH1"><div align="left">Country</div></th>
                      <td class="TextH1"><select name="c_country">
                          <option value=""></option>
                          <? require "country.inc"; ?>
                      </select></td>
                    </tr>
                  </table></td>
                </tr>
                <tr>
                  <td ><span class="MenuTitle">Scheduler Setting</span> </td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="TextH1"><table width="100%" border="0" cellspacing="1" cellpadding="1">
                    <tr>
                      <td>On Birthday </td>
                      <td><select 
      name="dob" id="dob" style="WIDTH: 180px" >
                          <option value=""></option>
                          <option value="99">On the same day</option>
                          <option value="1">1 Day Before</option>
                          <option value="2">2 Days Before</option>
                          <option value="3">3 Days Before</option>
                          <option value="4">4 Days Before</option>
                          <option value="5">5 Days Before</option>
                          <option value="6">6 Days Before</option>
                          <option value="7">1 Week Before</option>
                        </select>
                          <? stayfocus("form","dob",$dob) ?></td>
                    </tr>
                    <tr>
                      <td>On Spouse Birthday </td>
                      <td><select 
      name="spdob" id="spdob" style="WIDTH: 180px" >
                          <option value=""></option>
                          <option value="99">On the same day</option>
                          <option value="1">1 Day Before</option>
                          <option value="2">2 Days Before</option>
                          <option value="3">3 Days Before</option>
                          <option value="4">4 Days Before</option>
                          <option value="5">5 Days Before</option>
                          <option value="6">6 Days Before</option>
                          <option value="7">1 Week Before</option>
                        </select>
                          <? stayfocus("form","spdob",$spdob) ?></td>
                    </tr>
                    
                    <tr>
                      <td width="28%">Specify Date </td>
                      <td width="72%"><?
echo "<select name='day'>";
echo '<option value=""></option>\n';
foreach ($days as $value) {
	echo '<option value="'.$value.'"';
	if($day==$value)echo ' selected';
	echo '>'.$value.'</option>\n';
} echo '</select> ';

echo "<select name='month'>";
echo '<option value=""></option>\n';
foreach ($months  as $key => $value) {
	echo '<option value="'.$key.'"';
	if($month==$key)echo ' selected';
	echo '>'.$value.'</option>\n';
}
echo '</select> ';

echo "<select name='year'>";
echo '<option value=""></option>\n';
foreach ($thisyears as $value) {
	echo '<option value="'.$value.'"';
	if($year==$value)echo ' selected';
	echo '>'.$value.'</option>\n';
} 
echo '</select> ';
?></td>
                    </tr>
                    
                  </table></td>
                  <td>&nbsp;</td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td height="30" colspan="2"><span class="require">* Denotes a require_onced field</span></td>
            </tr>
            <tr>
              <td colspan="2"><span class="text12">
                <input name="btnSave" type="submit" class="btn150" id="btnSave" value="Save Scheduler " onclick="return confirm('Are you sure ?')"/>
                <input name="btnSend" type="submit" class="btn150" id="btnSend" value="Send SMS Now" onclick="return confirm('Are you sure want to send now?')"/>
              </span></td>
            </tr>
          </table></td>

      </tr>
 
    </table>
	  <br />
	</form>	</td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#333333"><div align="center"><span class="Footer">
      <br />
      <? require_once "footer.php"; ?>
    </span></div></td>
  </tr>
</table>
</body>
</html>
<?
if(array_search("SMS",$menulist)==false)
		print("<script type='text/javascript'>location.href='index.php'</script>");
?>