<?php
require("../../inc/header.inc");
?>

<script language="JavaScript">
function closeWindow(sysId,table_id,table_level,instanceNo)
{
	window.opener.location.href="./param_edit.php?sysId="+sysId+"&table_id="+table_id+"&table_level="+table_level+"&instanceNo="+instanceNo;
	window.close();
}
</script>

<?php
if($DEBUG)echo "sysId=$sysId<br>param_oid=$param_oid<br>";

if($_SERVER['REQUEST_METHOD'] == "POST")
{
	echo "coding_scheme_select=$coding_scheme_select<br>";
	echo "network_name_text=$network_name_text<br>";
	echo "ci_select=$ci_select<br>";
	echo "time_update_option_select=$time_update_option_select<br>";
	echo "time_zone_select=$time_zone_select<br>";
	
	$network_name_7bit_vec=string8bitTo7bit($network_name_text);
	$network_name_7bit_hexlen=sizeof($network_name_7bit_vec);
	$network_name_7bit_hexlen=$global_len;
	$network_name_7bit_hexstr="";
	for($i=0;$i<$network_name_7bit_hexlen;$i++)
	{
		$tmp=dechex($network_name_7bit_vec[$i]);
		$tmp=omc_keep_length($tmp,0,2);
		$network_name_7bit_hexstr.=$tmp;
	}
	$network_name_7bit_hexstr=omc_keep_length($network_name_7bit_hexstr,0,62,0);
	$tmp=dechex($network_name_7bit_hexlen);
	$tmp=omc_keep_length($tmp,0,2);
	$last_hex_value=$tmp;
	
	$spare_bit_num=strlen($network_name_text)%8;
	$byte3bin="1";
	if($coding_scheme_select == 0)
		$byte3bin.="000";
	else
		$byte3bin.="001";
	if($ci_select == 1)
		$byte3bin.="1";
	else
		$byte3bin.="0";
	
	$spare_bit_num_bin=decbin($spare_bit_num);
	$spare_bit_num_bin=omc_keep_length($spare_bit_num_bin,0,3);
	$byte3bin.=$spare_bit_num_bin;
		
	$byte3hex=omc_keep_length(dechex(bindec($byte3bin)),0,2);
	$last_hex_value.=$byte3hex;
	$last_hex_value.=$network_name_7bit_hexstr;
	
	$time_update_option_hex=omc_keep_length(dechex($time_update_option_select),0,2);
	$time_zone_hex=omc_keep_length(dechex($time_zone_select),0,2);
	
	$last_hex_value.=$time_update_option_hex;
	$last_hex_value.=$time_zone_hex;
	echo "last_hex_value=$last_hex_value<br>";
	$levels=getLevelWhere($param_oid);
	$updSql = "UPDATE OBJ_320.param_99 SET initValue='$last_hex_value' WHERE $levels and instanceNo='$instanceNo' ";
	mysqli_query($pubConn,$updSql);
	echo mysqli_error($pubConn);
	echo "";

	echo "\n<script language=\"JavaScript\">\n";
	echo "closeWindow('$sysId','$table_id','$table_level','$instanceNo');\n";
	echo "</script>\n";

}

$pubDb="OMC_PUB";

$mscTable="OBJ_320.param_99";
$levels=getLevelWhere($param_oid);
$sql="SELECT * FROM $mscTable WHERE $levels AND instanceNo=$instanceNo";
$res=mysqli_query($pubConn,$sql);
$rows=@mysqli_fetch_array($res);
$initValue=$rows[initValue];
//$initValue="0484d4f29c0e0000000000000000000000000000000000000000000000000000000101";
$readValue=$rows[readValue];


//decode
$byte3=substr($initValue,2,2);
$byte3bin=decbin(hexdec($byte3));
$coding_scheme_value=bindec(substr($byte3bin,1,3));
$ci_value=bindec(substr($byte3bin,4,1));
$spare_bit_num=bindec(substr($byte3bin,5,3));
$network_name_len=hexdec(substr($initValue,0,2));
$network_name_value=substr($initValue,4,$network_name_len*2);
$network_name_vec=string7bitTo8bit($network_name_value);
$network_name_l=$global_len;
if($spare_bit_num == 0 && $network_name_len != 0)
	$network_name_l++;
//$network_name_l-=$network_name_l/8;

$network_name_value="";

for($i=0;$i<$network_name_l;$i++)
{
	$network_name_value.=chr($network_name_vec[$i]);
}
//echo "network_name_value=$network_name_value,$network_name_l";
//exit();
$time_update_option_value=hexdec(substr($initValue,66,2));
$time_zone_value=hexdec(substr($initValue,68,2));

/*
echo "coding_scheme_value=$coding_scheme_value<br>";
echo "ci_value=$ci_value<br>";
echo "network_name_len=$network_name_len,network_name_value=$network_name_value<br>";
echo "time_update_option_value=$time_update_option_value<br>";
echo "time_zone_value=$time_zone_value<br>";
*/

echo "<form name=myform method=post action=\"./mscNITZ.php\">";
echo "<table border=0>";

//Coding Scheme
echo "<tr>";
echo "<td>";
echo "Coding Scheme";
echo "</td>";
echo "<td>";
echo "<select size=1 name=\"coding_scheme_select\">";
$coding_scheme_label=array(0=>"7-bit");
for($i=0;$i<sizeof($coding_scheme_label);$i++)
{
	if($coding_scheme_value == $i)
		echo "<option value=$i selected>{$coding_scheme_label[$i]}</option>";
	else
		echo "<option value=$i>{$coding_scheme_label[$i]}</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";

//CI
echo "<tr>";
echo "<td>";
echo "Add CI";
echo "</td>";
echo "<td>";
echo "<select size=1 name=\"ci_select\">";
$ci_label=array(0=>"Off",1=>"On");
for($i=0;$i<sizeof($ci_label);$i++)
{
	if($ci_value == $i)
		echo "<option value=$i selected>{$ci_label[$i]}</option>";
	else
		echo "<option value=$i>{$ci_label[$i]}</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";

//Network name
echo "<tr>";
echo "<td>Network Name</td>";
echo "<td>";
echo "<input type=text maxlength=32 class=text name=\"network_name_text\" value=\"$network_name_value\">";
echo "</td>";
echo "</tr>";

//Time update option
echo "<tr>";
echo "<td>";
echo "Time Update Option";
echo "</td>";
echo "<td>";
echo "<select size=1 name=\"time_update_option_select\">";
$tup_label=array(0=>"None",1=>"Time zone",2=>"Time zone and time");
for($i=0;$i<sizeof($tup_label);$i++)
{
	if($time_update_option_value == $i)
		echo "<option value=$i selected>{$tup_label[$i]}</option>";
	else
		echo "<option value=$i>{$tup_label[$i]}</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";

//Time zone
echo "<tr>";
echo "<td>";
echo "Time Zone";
echo "</td>";
echo "<td>";
echo "<select size=1 name=\"time_zone_select\">";
$time_zone_value_label=array(
	176,
	172,
	168,
	164,
	160,
	156,
	152,
	148,
	146,
	144,
	142,
	140,
	136,
	132,
	0,
	4,
	8,
	12,
	16,
	18,
	20,
	22,
	23,
	24,
	26,
	28,
	32,
	36,
	38,
	40,
	44,
	48,
	52
);

$time_zone_string_label=array(
	"GMT-12:00",
	"GMT-11:00",
	"GMT-10:00",
	"GMT-09:00",
	"GMT-08:00",
	"GMT-07:00",
	"GMT-06:00",
	"GMT-05:00",
	"GMT-04:30",
	"GMT-04:00",
	"GMT-03:30",
	"GMT-03:00",
	"GMT-02:00",
	"GMT-01:00",
	"GMT",
	"GMT+01:00",
	"GMT+02:00",
	"GMT+03:00",
	"GMT+04:00",
	"GMT+04:30",
	"GMT+05:00",
	"GMT+05:30",
	"GMT+05:45",
	"GMT+06:00",
	"GMT+06:30",
	"GMT+07:00",
	"GMT+08:00",
	"GMT+09:00",
	"GMT+09:30",
	"GMT+10:00",
	"GMT+11:00",
	"GMT+12:00",
	"GMT+13:00"
);
for($i=0;$i<sizeof($time_zone_value_label);$i++)
{
	if($time_zone_value == $time_zone_value_label[$i])
		echo "<option value={$time_zone_value_label[$i]} selected>{$time_zone_string_label[$i]}</option>";
	else
		echo "<option value={$time_zone_value_label[$i]}>{$time_zone_string_label[$i]}</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";

echo "</table>";

echo "<br><b>Remark:</b><br><br>";
echo "Network Identity and Time Zone<br><br>";
echo "- Coding Scheme: Indicate that 7-bit or UCS2 coding scheme is used.<br>";
echo "- Add CI: Indicate whether the MS should add the letters for the Country's Initials to the text string.<br>";
echo "- Network Name: A text string is to pass to the MS via NITZ.<br>";
echo "- Time Update Option: Determine whether time and time zone info is transferred to MS via NITZ.<br>";
echo "- Time Zone: Specify the local time zone.<br>";

echo "<br>";
echo "<input value=\"Set\" type=submit class=button >";
echo "&nbsp;&nbsp;<input type=reset value=Reset class=button>";

echo "<input type=hidden name=\"sysId\" value=$sysId>";
echo "<input type=hidden name=\"table_id\" value=$table_id>";
echo "<input type=hidden name=\"table_level\" value=$table_level>";
echo "<input type=hidden name=\"instanceNo\" value=$instanceNo>";
echo "<input type=hidden name=\"param_oid\" value=$param_oid>";
echo "</form>";

$global_len=0;
function string8bitTo7bit($strin)
{
	global $global_len;
	$len=strlen($strin);
	if($len <= 0)
		return "";
		
	$global_len=$len-floor($len/8);;
	
	for($i=0;$i<$len;$i++)
	{
		$inHex[$i]=ord($strin[$i]);
		switch($inHex[$i])
		{
		//'@'
		case 64:
			$inHex[$i]=0x00;
			break;
		//'$'
		case 36:
			$inHex[$i]=0x02;
			break;
		//'_'
		case 95:
			$inHex[$i]=0x11;
			break;
		//'['
		case 91:
			$inHex[$i]=0x28;
			break;
		//']'
		case 93:
			$inHex[$i]=0x29;
			break;
		//'/'
		case 47:
			$inHex[$i]=0x2f;
			break;
		//'''
		case 96:
			$inHex[$i]=0x27;
			break;
		//'{'
		case 123:
			$inHex[$i]=0x28;
			break;
		//'}'
		case 125:
			$inHex[$i]=0x29;
			break;
		case 0x82:
			$inHex[$i]=0x05;
			break;
		case 0x85:
			$inHex[$i]=0x7F;
			break;
		case 0x8A:
			$inHex[$i]=0x04;
			break;
		case 0x8D:
			$inHex[$i]=0x07;
			break;
		case 0x95:
			$inHex[$i]=0x08;
			break;
		case 0x97:
			$inHex[$i]=0x06;
			break;
		case 0x9D:
			$inHex[$i]=0x03;
			break;
		default:
			break;
		}
	}
	
	$n=0;
	$index=0;
	for($i=0;$i<=$len;$i++)
	{
		if($i<$len-1)
		{
			$mid1=$inHex[$i]+0;
			$mid2=$inHex[$i+1]+0;
			$cur=($mid1 >> $n) |(($mid2 << (7-$n)) & 0xff);
		}
		else
		{
			$mid1=$inHex[$i];
			$cur=($mid1 >> $n) & 0xff;
		}
		$outHex[$index++]=$cur;
		echo "$cur ";
		$n=($n+1)%7;
		if($n==0 ) 
			$i++;
	}
	
	
	//for($i=0;$i<$index;$i++)
	//	$strout[$i]=chr($outHex[$i]);
	
	return $outHex;
}

function string7bitTo8bit($strin)
{
	global $global_len;
	
	$len=strlen($strin);
	
	$ti=0;
	for($i=0;$i<$len;$i+=2)
	{
		$tmp=substr($strin,$i,2);
		$inHex[$ti]=hexdec($tmp);
		$ti++;
	}

	$global_len=$ti+floor($ti/8);
	
	
	$n=0;
	$j=0;
	for($i=0;$i <= $ti;)
  {
    if($i>0)
    {
      $mid1=$inHex[$i];
      $mid2=$inHex[$i-1];
      $cur=(($mid1 << $n) & 0x7f) |($mid2 >> (8-$n));
    }
    else
    {
       $mid1=$inHex[$i] & 0x7f;
       $cur=$mid1;
    }
    $outHex[$j++] = $cur;
    $n =($n+1)%8;
    if($n!=0) 
    	$i++;
  }
  
  return $outHex;
}

?>