<?php
require("../../inc/header.inc");
?>
<body onload="javascript:adjust();" onresize="javascript:adjust();" leftmargin="15" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<script language="JavaScript">
	
	function saveNewAd()
	{
		var mcc=document.myform.mcc_text.value;
		var mnc=document.myform.mnc_text.value;
		var no=document.myform.no_text.value;
		var adStart=document.myform.adStart_text.value;
		var adEnd=document.myform.adEnd_text.value;
		var adContent=encodeURIComponent(document.myform.adContent_text.value);
		var encode=document.myform.encode_text.value;
		
		window.location.href="./operation.php?save_new_ad=1&edit_mcc="+mcc+"&edit_mnc="+mnc+"&edit_no="+no+"&edit_adStart="+adStart+"&edit_adEnd="+adEnd+"&edit_adContent="+adContent+"&edit_encode="+encode;
	}
	
	function saveOldAd()
	{
		var mcc=document.myform.mcc_text.value;
		var mnc=document.myform.mnc_text.value;
		var no=document.myform.no_text.value;
		var adStart=document.myform.adStart_text.value;
		var adEnd=document.myform.adEnd_text.value;
		var adContent=encodeURIComponent(document.myform.adContent_text.value);
		var encode=document.myform.encode_text.value;
		
		window.location.href="./operation.php?save_old_ad=1&edit_mcc="+mcc+"&edit_mnc="+mnc+"&edit_no="+no+"&edit_adStart="+adStart+"&edit_adEnd="+adEnd+"&edit_adContent="+adContent+"&edit_encode="+encode;
	}
	
	function deleteAd(no)
	{
		var adno=document.myform.no_text.value;
		window.location.href="./operation.php?delete_ad=1&ad_no="+adno;
	}
	
	function saveNewServ()
	{
		var mcc=document.myform.mcc_text.value;
		var mnc=document.myform.mnc_text.value;
		var flag=document.myform.flag_text.value;
		var smsEncode=document.myform.smsEncode_text.value;
		var wlContent=encodeURIComponent(document.myform.wlContent_text.value);
		
		window.location.href="./operation.php?save_new_serv=1&edit_mcc="+mcc+"&edit_mnc="+mnc+"&edit_flag="+flag+"&edit_smsEncode="+smsEncode+"&edit_wlContent="+wlContent;
	
	}
	function saveOldServ()
	{
		var mcc=document.myform.mcc_text.value;
		var mnc=document.myform.mnc_text.value;
		var flag=document.myform.flag_text.value;
		var smsEncode=document.myform.smsEncode_text.value;
		var wlContent=encodeURIComponent(document.myform.wlContent_text.value);
		
		
		window.location.href="./operation.php?save_old_serv=1&edit_mcc="+mcc+"&edit_mnc="+mnc+"&edit_flag="+flag+"&edit_smsEncode="+smsEncode+"&edit_wlContent="+wlContent;
	
	}
	
	function closeWindow()
	{
		window.close();
		window.opener.location.href="./wsms.php";
		window.opener.location.href="./wsms.php";
		//window.close();
	}
</script>	

<?php
function utf8_unicode($c) 
{
  switch(strlen($c)) {
    case 1:
      return ord($c);
    case 2:
      $n = (ord($c[0]) & 0x3f) << 6;
      $n += ord($c[1]) & 0x3f;
      return $n;
    case 3:
      $n = (ord($c[0]) & 0x1f) << 12;
      $n += (ord($c[1]) & 0x3f) << 6;
      $n += ord($c[2]) & 0x3f;
      return $n;
    case 4:
      $n = (ord($c[0]) & 0x0f) << 18;
      $n += (ord($c[1]) & 0x3f) << 12;
      $n += (ord($c[2]) & 0x3f) << 6;
      $n += ord($c[3]) & 0x3f;
      return $n;
  }
}

function gb2unicode($string)
{
	$result="";
	preg_match_all("/[\x80-\xff]?./",$string,$ar);
	foreach($ar[0] as $v)
	{
		$temp=dechex(utf8_unicode(iconv("GB2312","UTF-8",$v)));
		if(strlen($temp)==1)
			$temp="0".$temp;
	  	$result.=$temp;
	}
	echo "string=$string,result=$result<br>";
	return $result;
}
function unicode2gb($string)
{
	$result="";
	for($i=0;$i<strlen($string);$i++)
	{
		$temp.=$string[$i];
		if($i!=0 && ($i+1)%4==0)
		{
			$result.="&#".hexdec($temp);
			$temp="";
		}	
		
			
	}
	return $result;
}

?>

<?php
adjust_head();
adjust_title_head();
echo "<TABLE id=\"table_up\" border=\"0\" width=\"100%\">";
echo "<TR>";
echo "<TD width=\"50%\" align=\"left\">";
echo "$strMaintenance > Control Panel > Welcome SMS<br>";
echo "</TD>";
echo "<TD width=\"50%\" align=\"right\" title=\"Help\">";
$helpId="5.2.3";
	showHelp($helpId);
echo "</TD>";
echo "</TR>";
echo "</TABLE>";

adjust_title_tail();
adjust_content_head();
echo "<br>";

	$DEBUG=0;
	
	if($DEBUG)
		echo "mcc=$mcc,mnc=$mnc,operation=$operation<br>";	
	echo "<form name=\"myform\" action=\"./hpmnInfo.php\">";	
	
	if($delete_ad==1)
	{
		$pubDb="OMC_PUB";
		$delSql="DELETE FROM wsmsMsgConf where no=$ad_no ";
		$delRes = @mysqli_query($pubConn,$delSql);
		echo mysqli_error($pubConn);
		
		echo "\n<script language=\"JavaScript\">\n";
		echo "window.location.href=\"./wsms.php\";\n";
		echo "</script>\n";
	}
	if($save_new_ad==1)
	{
		$pubDb="OMC_PUB";
		if($edit_encode==2)
		{
			//echo "edit_adContent=$edit_adContent<br>";
			$edit_adContent=gb2unicode($edit_adContent);
			//echo "edit_adContent=$edit_adContent<br>";
		}
		$saveSql="INSERT INTO wsmsMsgConf values('$edit_no','$edit_mcc','$edit_mnc','$edit_adContent','$edit_adStart','$edit_adEnd')";
		mysqli_query($pubConn,$saveSql);
		echo mysqli_error($pubConn);
		
		echo "\n<script language=\"JavaScript\">\n";
		echo "window.location.href=\"./wsms.php\";\n";
		echo "</script>\n";
	}
	if($save_old_ad==1)
	{
		$pubDb="OMC_PUB";
		if($edit_encode==2)
			$edit_adContent=gb2unicode($edit_adContent);
		$saveSql="UPDATE wsmsMsgConf SET content='$edit_adContent',validStart='$edit_adStart',validEnd='$edit_adEnd' where no='$edit_no' ";
		mysqli_query($pubConn,$saveSql);
		echo mysqli_error($pubConn);
		
		echo "\n<script language=\"JavaScript\">\n";
		echo "window.location.href=\"./wsms.php\";\n";
		echo "</script>\n";
	}
	
	if($save_new_serv==1)
	{
		$pubDb="OMC_PUB";
		if($edit_smsEncode==2)
			$edit_wlContent=gb2unicode($edit_wlContent);
		//echo "edit_wlContent=$edit_wlContent<br>";	
		$saveSql="INSERT INTO wsmsServConf values('$edit_mcc','$edit_mnc','$edit_flag','$edit_smsEncode','$edit_wlContent')";
		
		mysqli_query($pubConn,$saveSql);
		echo mysqli_error($pubConn);
		echo "\n<script language=\"JavaScript\">\n";
		echo "window.location.href=\"./wsms.php\";\n";
		echo "</script>\n";
	}
	if($save_old_serv==1)
	{
		
		$pubDb="OMC_PUB";
		if($edit_smsEncode==2)
			$edit_wlContent=gb2unicode($edit_wlContent);
		$saveSql="UPDATE wsmsServConf SET flag='$edit_flag',smsEncode='$edit_smsEncode',welcomeMsg='$edit_wlContent' where mcc='$edit_mcc' and mnc='$edit_mnc' ";
		mysqli_query($pubConn,$saveSql);
		echo mysqli_error($pubConn);
		echo "\n<script language=\"JavaScript\">\n";
		echo "window.location.href=\"./wsms.php\";\n";
		echo "</script>\n";
	}

	//Add a new hpmn server
	if($operation=="new")
	{
		echo "<p><table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"50%\">Name</td><td width=\"50%\">Value</td>";
		echo "</tr>";
		
		//mcc
		echo "<tr>";
		echo "<td>MCC</td>";
		echo "<td><input type=text class=\"text\" size=5 maxlength=3 name=\"mcc_text\" value=\"\"></text></td>";
		echo "</tr>";
		
		//mnc
		echo "<tr>";
		echo "<td>MNC</td>";
		echo "<td><input type=text class=\"text\" size=5 maxlength=2 name=\"mnc_text\" value=\"\"></text></td>";
		echo "</tr>";
		
		//flag
		$flagLabel=array(0=>"Disable",1=>"Enable");
		$flag=1;
		echo "<tr>";
		echo "<td>Flag</td>";
		echo "<td><select name=\"flag_text\">";
		for($i=0;$i< sizeof($flagLabel);$i++)
		{
			if($i==$flag)
				echo "<option value=\"$i\" selected>{$flagLabel[$i]}</option>";
			else
				echo "<option value=\"$i\">{$flagLabel[$i]}</option>";
		}
		echo "</select></td>";
		echo "</tr>";
		
		//smsEncode
		$smsEncodeLabel=array("7 bit","8 bit","16 bit");
		$smsEncode=0;
		echo "<tr>";
		echo "<td>Encode</td>";
		echo "<td><select name=\"smsEncode_text\">";
		for($i=0;$i< sizeof($smsEncodeLabel);$i++)
		{
			if($i==$smsEncode)
				echo "<option value=\"$i\" selected>{$smsEncodeLabel[$i]}</option>";
			else
				echo "<option value=\"$i\">{$smsEncodeLabel[$i]}</option>";
		}
		echo "</select></td>";
		echo "</tr>";
		
		//welcome message
		echo "<tr>";
		echo "<td>Welcome Message</td>";
		echo "<td><textarea name=\"wlContent_text\" rows=5 cols=25>Edit Welcome message...</textarea></td>";
		echo "</tr>";
		
		echo "</table>";
		
		//save button
		echo "<input type=button onClick=\"javascript:saveNewServ();javascript:closeWindow();\" value=\"Save\" class=\"button\">";
	
	}
	//Edit a hpmn server
	else if($operation=="edit")
	{
		//Get the hpmn server info from database
		$database="OMC_PUB";
		$selSql = "SELECT * FROM wsmsServConf where mcc='$mcc' and mnc='$mnc' ";
		$selRes = @mysqli_query($pubConn,$selSql);
		$HpmnCount=mysqli_num_rows($selRes);
		echo mysqli_error($pubConn);
		//echo "count:$HpmnCount<br>";
	
		
		$selRow = @mysqli_fetch_array($selRes);
		$HpmnMcc=$selRow[mcc];
		$HpmnMnc=$selRow[mnc];
		$HpmnFlag=$selRow[flag];
		$HpmnSmsEncode=$selRow[smsEncode];
		$HpmnWlContent=$selRow[welcomeMsg];


		echo "<p><table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"50%\">Name</td><td width=\"50%\">Value</td>";
		echo "</tr>";
		
		//mcc
		echo "<tr>";
		echo "<td>MCC</td><td>$HpmnMcc</td>";
		echo "<input type=\"hidden\" name=\"mcc_text\" value=$HpmnMcc>";
		echo "</tr>";
		
		//mnc
		echo "<tr>";
		echo "<td>MNC</td><td>$HpmnMnc</td>";
		echo "<input type=\"hidden\" name=\"mnc_text\" value=$HpmnMnc>";
		echo "</tr>";
		
		//flag
		$flagLabel=array("Disable","Enable");
		echo "<tr>";
		echo "<td>Flag</td>";
		echo "<td><select name=\"flag_text\">";
		for($i=0;$i< sizeof($flagLabel);$i++)
		{
			if($HpmnFlag==$i)
				echo "<option value=\"$i\" selected>{$flagLabel[$i]}</option>";
			else
				echo "<option value=\"$i\">{$flagLabel[$i]}</option>";
		}
		echo "</select></td>";
		echo "</tr>";
		
		//smsEncode
		$smsEncodeLabel=array("7 bit","8 bit","16 bit");
		echo "<tr>";
		echo "<td>Encode</td>";
		echo "<td><select name=\"smsEncode_text\">";
		for($i=0;$i< sizeof($smsEncodeLabel);$i++)
		{
			if($HpmnSmsEncode==$i)
				echo "<option value=\"$i\" selected>{$smsEncodeLabel[$i]}</option>";
			else
				echo "<option value=\"$i\">{$smsEncodeLabel[$i]}</option>";
		}
		echo "</select></td>";
		echo "</tr>";
		
		//wlContent 
		echo "<tr>";
		//echo "<td>Welcome Message</td><td><input type=text class=\"text\" size=256 maxlength=256 name=\"wlContent_text\" value=\"$HpmnWlContent\"></text></td>";
		echo "<td>Welcome Message</td>";
		if($HpmnSmsEncode==2)
			$HpmnWlContent=unicode2gb($HpmnWlContent);
		echo "<td><textarea class=\"text\" name=\"wlContent_text\" rows=5 cols=25>$HpmnWlContent</textarea>";
		echo "</tr>";
		
		echo "</table>";
		
		//save button
		echo "<input type=button onClick=\"javascript:saveOldServ();javascript:closeWindow();\" value=\"Save\" class=\"button\">";
	
	}
	else if($operation=="ad_edit")
	{
		//Get the hpmn server info from database
		$database="OMC_PUB";
		$selSql = "SELECT * FROM wsmsMsgConf where mcc='$mcc' and mnc='$mnc' and no='$no'";
		$selRes = @mysqli_query($pubConn,$selSql);
		$HpmnCount=mysqli_num_rows($selRes);
		echo mysqli_error($pubConn);
		//echo "count:$HpmnCount<br>";
	
		
		$selRow = @mysqli_fetch_array($selRes);
		$adNo=$selRow[no];
		$adContent=$selRow[content];
		$adStart=$selRow[validStart];
		$adEnd=$selRow[validEnd];
		


		echo "<p><table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"50%\">Name</td><td width=\"50%\">Value</td>";
		echo "</tr>";
		
		//mcc
		echo "<tr>";
		echo "<td>MCC</td><td>$mcc</td>";
		echo "<input type=\"hidden\" name=\"mcc_text\" value=$mcc>";
		echo "</tr>";
		
		//mnc
		echo "<tr>";
		echo "<td>MNC</td><td>$mnc</td>";
		echo "<input type=\"hidden\" name=\"mnc_text\" value=$mnc>";
		echo "</tr>";
		
		//no
		echo "<tr>";
		echo "<td>No.</td><td>$no</td>";
		echo "<input type=\"hidden\" name=\"no_text\" value=$no>";
		echo "</tr>";
		
		//validStart
		echo "<tr>";
		echo "<td>Start Date</td>";
		echo "<td><input type=\"text\" name=\"adStart_text\" value=$adStart class=\"text\"></td>";
		echo "</tr>";
		
		//validEnd
		echo "<tr>";
		echo "<td>End Date</td>";
		echo "<td><input type=\"text\" name=\"adEnd_text\" value=$adEnd class=\"text\"></td>";
		echo "</tr>";
		
		//content
		echo "<tr>";
		echo "<td>AD Message</td>";
		echo "<input type=\"hidden\" name=\"encode_text\" value=$encode>";
		if($encode==2)
			$adContent=unicode2gb($adContent);
		echo "<td><textarea name=\"adContent_text\" cols=25 rows=5>$adContent</textarea></td>";
		
		echo "</table>";
		
		//save button
		echo "<input type=button onClick=\"javascript:saveOldAd();javascript:closeWindow();\" value=\"Save\" class=\"button\">";
	
		//delete button
		echo "<input type=button onClick=\"javascript:deleteAd('$no');javascript:closeWindow();\" value=\"Delete\" class=\"button\">";
		
		
	}
	else if($operation=="ad_new")
	{
		//get the new ad no
		for($new_no=1;$new_no<1600;$new_no++)
		{
			$database="OMC_PUB";
			$selSql = "SELECT * FROM wsmsMsgConf where no='$new_no'";
			$selRes = @mysqli_query($pubConn,$selSql);
			$tmp_count=mysqli_num_rows($selRes);
			echo mysqli_error($pubConn);
			if($tmp_count!=1)
			{
				break;
			}
		}
		//echo "new_no=$new_no<br>";
		echo "<p><table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"50%\">Name</td><td width=\"50%\">Value</td>";
		echo "</tr>";
		
		//mcc
		echo "<tr>";
		echo "<td>MCC</td><td>$mcc</td>";
		echo "<input type=\"hidden\" name=\"mcc_text\" value=$mcc>";
		echo "</tr>";
		
		//mnc
		echo "<tr>";
		echo "<td>MNC</td><td>$mnc</td>";
		echo "<input type=\"hidden\" name=\"mnc_text\" value=$mnc>";
		echo "</tr>";
		
		//no
		echo "<tr>";
		echo "<td>No.</td><td>$new_no</td>";
		echo "<input type=\"hidden\" name=\"no_text\" value=$new_no>";
		echo "</tr>";
		
		//validStart
		echo "<tr>";
		echo "<td>Start Date</td>";
		echo "<td><input type=\"text\" name=\"adStart_text\" class=\"text\"></td>";
		echo "</tr>";
		
		//validEnd
		echo "<tr>";
		echo "<td>End Date</td>";
		echo "<td><input type=\"text\" name=\"adEnd_text\" class=\"text\"></td>";
		echo "</tr>";
		
		//content
		echo "<tr>";
		echo "<td>AD Message</td>";
		echo "<input type=\"hidden\" name=\"encode_text\" value=$encode>";
		if($encode==2)
			$show="±à¼­¹ã¸æ¶ÌÐÅ";
		else
			$show="Edit AD Message...";	
		echo "<td><textarea name=\"adContent_text\" cols=25 rows=5>$show</textarea></td>";
		
		echo "</table>";
		//save button
		echo "<input type=button onClick=\"javascript:saveNewAd();javascript:closeWindow();\" value=\"Save\" class=\"button\">";
	
	}
	
	//remark
	echo "<p><b>Remark:</b><p>";
	/*
	echo "Interval: The interval to make NRTRDE files for the HPMN<br><br>";
	echo "Report Directory: The directory to send the report files to<br><br>";
	echo "NRTRDE Directory: The directory to send the NRTRDE files to<br><br>";
	*/
	echo "</form>";
adjust_content_tail();
?>
