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

if($btnSave || $btnSend){

	$sql="
		INSERT INTO `sms_template` (
		`smst_name` ,
		`smst_msg` ,
		`smst_user` ,
		`smst_created` 
		)
		VALUES (\"$name\", \"$message\", '".$_SESSION["egoUser"]."', CURRENT_TIMESTAMP 
		);
	";
	@mysql_query($sql) or die(mysql_error());
	$iid=mysql_insert_id();	
	
	if($btnSave){
		print("<script type='text/javascript'>alert('Template Added.')</script>");
		print("<script type='text/javascript'>location.href='sms_tempSearch.php'</script>");
	}else{
		print("<script type='text/javascript'>location.href='sms_newAlert.php?template=$iid'</script>");
	}
}
?>

<!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=UTF-8" />
<link rel="stylesheet" type="text/css" href="sddm.css" >
<script src="include/validation.js" type="text/javascript"></script>
 <script language="JavaScript">
 function show_length()	
{
   if (eval("document.form.chi.checked") == true)document.getElementById('totword').innerHTML = 60;
   else document.getElementById('totword').innerHTML = 130;
}

function check_length()
{
	if (eval("document.form.chi.checked") == true) var maxchars = 60;
    else var maxchars = 130;

	var message = document.getElementById('message');
	var len = message.value.length;
	
	if (len > maxchars) {
	 message.value = message.value.substr(0,maxchars);
	 len = maxchars;
	}
	
	document.getElementById('wordcount').innerHTML = len;
}
</script>	 
<title><? echo TITLE?></title>
</head>
<body>

<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 ValidateAddSMS(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 </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;
              <input name="ty" type="hidden" id="ty" value="<? echo $ty ?>" /></td>
          </tr>

        </table>
          <table width="100%" border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td width="19%" height="30" class="TextH1">SMS Name :<span class="require"> *</span></td>
              <td width="81%"><input name="name" type="text" id="name" value="<? echo $name ?>" size="50" maxlength="50" /></td>
            </tr>
            <tr>
              <td height="30" class="TextH1">&nbsp;</td>
              <td><input name="chi" type="checkbox" id="chi" value="Y" <?=($chi=="Y" ? "CHECKED" : " ")?> onclick="show_length();check_length()"/>
                  <span class="TextH1">Chinese/Others</span></td>
            </tr>
            <tr>
              <td valign="top"><span class="TextH1">Message :<span class="require"> *</span></span></td>
              <td><span class="text2">
                <textarea name="message" id="message" cols="50" rows="4" 
onkeyup="check_length();" onchange="check_length();" 
onmouseout="check_length();"><? echo $message ?></textarea>
              </span><span class="subItem"><span id="wordcount">0</span> / <span id="totword">130</span> characters</span></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 as Template" onclick="return confirm('Are you sure ?')"/>
                <input name="btnSend" type="submit" class="btn150" id="btnSend" value="Send Now / Set Scheduler" onclick="return confirm('Are you sure ?')"/>
              </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>");
?>