<?php
require("../sub_inc/commfunc.php");
$DEBUG = 0;
if($DEBUG) echo "<br>ÕýÔÚµ÷ÊÔÖÐ£¡£¡";
?>
<body topmargin="0" leftmargin="12">


<?php
$pubdb = 'OMC_PUB';
$sqltable = 'OBJ_225.param_99';
$net_Conf = 'OBJ_225.paramConf';
$listPageUrl = "msc.php";

if($DEBUG) 
{
	echo "prefix_group_no = $prefix_group_no<br>";
	echo "cc = $cc<br>";
	echo "ndc = $ndc<br>";
	echo "IPrefix = $IPrefix<br>";
	echo "NPrefix = $NPrefix<br>";
}

$getsql = "select initValue from $net_Conf where level_1 = '2' and level_2 = '8' and level_3 = '0'";  
$pre_org = @mysqli_query($pubConn,$getsql);
$pre_org = @mysqli_fetch_array($pre_org);
$pre_org = $pre_org[0];
echo mysqli_error($pubConn);
if($DEBUG) echo "pre_org = $pre_org<br>";
		
if($_SERVER['REQUEST_METHOD'] == "POST")
{
	$xapp_prefix_loc = array('00.0-15.7','16.0-16.7','17.0-17.7','18.0-33.7','34.0-34.7','35.0-35.7','36.0-36.7');
	$xapp_prefix = array(
		array(
			omc_keep_length(toHex($prefix_new),'0','32','0'),
			'00',
			omc_keep_length(dechex(strlen($cc.$ndc)),'0','2','1'),
			omc_keep_length(toHex($cc.$ndc),'0','32','0'),
			'00',
			omc_keep_length(dechex($prefix_group_no),'0','2','1'),
			'00'
		),
		array(
			omc_keep_length(toHex($ndc.$prefix_new),'0','32','0'),
			'00',
			omc_keep_length(dechex(strlen($cc)),'0','2','1'),
			omc_keep_length(toHex($cc),'0','32','0'),
			'00',
			omc_keep_length(dechex($prefix_group_no),'0','2','1'),
			'01'
		),
		array(
			omc_keep_length(toHex($NPrefix.$ndc.$prefix_new),'0','32','0'),
			omc_keep_length(dechex(strlen($NPrefix)),'0','2','1'),
			omc_keep_length(dechex(strlen($cc)),'0','2','1'),
			omc_keep_length(toHex($cc),'0','32','0'),
			'00',
			omc_keep_length(dechex($prefix_group_no),'0','2','1'),
			'02'
		),
		array(
			omc_keep_length(toHex($cc.$ndc.$prefix_new),'0','32','0'),
			'00',
			'00',
			omc_keep_length('0','0','32','0'),
			'00',
			omc_keep_length(dechex($prefix_group_no),'0','2','1'),
			'03'
		),
		array(
			omc_keep_length(toHex($IPrefix.$cc.$ndc.$prefix_new),'0','32','0'),
			omc_keep_length(dechex(strlen($IPrefix)),'0','2','1'),
			'00',
			omc_keep_length('0','0','32','0'),
			'00',
			omc_keep_length(dechex($prefix_group_no),'0','2','1'),
			'04'
		),
	);
		
	if(isset($addFlag))	//add a prefix group
	{	
		$getsql = "select instanceNo from $sqltable where level_1 = '2' and level_2 = '8' and level_3 = '0'
			and initValue = '$pre_org'";  
		$result = @mysqli_query($pubConn,$getsql);
		echo mysqli_error($pubConn);
		$i = 0;
		while($row = @mysqli_fetch_array($result))
		{
			$prefix_ins[$i] = $row[instanceNo];
			if($DEBUG)	echo "<br>prefix instanceNo[$i] = $prefix_ins[$i]";
				
			if(++$i >= sizeof($xapp_prefix))
				break;
		}
	}
	else	//edit a prefix group
	{
		$getsql = "select instanceNo from $sqltable where level_1 = '2' and level_2 = '8' and level_3 = '0'
			and initValue != '$pre_org'";  
		$result = @mysqli_query($pubConn,$getsql);
		echo mysqli_error($pubConn);
		$i = 0;
		while($row = @mysqli_fetch_array($result))
		{
			$prefix_ins[$i] = $row[instanceNo];				
			if(++$i >= sizeof($xapp_prefix))
				break;
		}
		if($i < sizeof($xapp_prefix))	
		{
			$getsql = "select instanceNo from $sqltable where level_1 = '2' and level_2 = '8' and level_3 = '0'
			and initValue = '$pre_org'";  
			$result = @mysqli_query($pubConn,$getsql);
			echo mysqli_error($pubConn);
			while($row = @mysqli_fetch_array($result))
			{
				$prefix_ins[$i] = $row[instanceNo];					
				if(++$i >= sizeof($xapp_prefix))
					break;
			}
		}
		if($DEBUG)
		{
			for($i=0; $i < sizeof($prefix_ins); $i++)
				echo "<br>prefix_ins[$i] = $prefix_ins[$i]";
		}
	}
	
	if(sizeof($prefix_ins) < sizeof($xapp_prefix) || $prefix_group_no < 0)	
	{
		echo "<script LANGUAGE=\"JavaScript\">";
		echo "alert('Parameter table is full!')";
		echo "\n </script>";
	} 		
	else
	{
		for($k=0; $k < sizeof($xapp_prefix); $k++)
		{	
			$initValue = omc_keep_length($initValue,'0',strlen($pre_org));
			for($j=0; $j < sizeof($xapp_prefix[$k]); $j++)
				$initValue =  setBitValue($initValue,$xapp_prefix_loc[$j],$xapp_prefix[$k][$j]);
				
			$setsql = "update $sqltable set initValue = '$initValue' 
				where level_1 = '2' and level_2 = '8' and instanceNo = '$prefix_ins[$k]'";
			if($DEBUG) echo "setsql = $setsql<br>";
			mysqli_query($pubConn,$setsql);
			echo mysqli_error($pubConn);
		}
	}
	
	echo "<script LANGUAGE=\"JavaScript\">";
	if(!$DEBUG){
		echo "\n window.location.href=\"$listPageUrl\";";
	} 
	echo "\n </script>";
}


echo "<form method=\"POST\" name=\"Set\" action=\"$PHP_SELF\">";
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"80%\">";
echo "Configuration > Network > Element > MSC ";
echo "<td width=\"20%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<br>";

$getsql = "select initValue from $sqltable where level_1 = '2' and level_2 = '8' and level_3 = '0'
	and initValue != '$pre_org'";  
$result = @mysqli_query($pubConn,$getsql);
echo mysqli_error($pubConn);
if($prefix_group_no > 0)	//edit a prefix group
{
	while($row = @mysqli_fetch_array($result))
	{
		$tmpGroupNo = hexdec(getbitValue($row[initValue],'35.0-35.7'));
		$tmpPrefixType = hexdec(getbitValue($row[initValue],'36.0-36.7'));
		if($tmpGroupNo == $prefix_group_no && $tmpPrefixType == 0)
		{
			$prefix = trim(toAscii(getBitValue($row[initValue],'00.0-15.7')));
			if($DEBUG)
			{
				echo "<br>tmpGroupNo = $tmpGroupNo";
				echo "<br>tmpPrefixType = $tmpPrefixType";
			}
			break;
		}
	}
}
else //found a unused prefix group no.
{
	$i = 0;
	while($row = @mysqli_fetch_array($result))
		$tmpGroupNo[$i++] = getbitValue($row[initValue],'35.0-35.7');
		
	for($j=1; $j < 256; $j++)
	{
		$tmpFlag = 0;
		for($k=0; $k < $i; $k++)
		{
			if($j == $tmpGroupNo[$k])
				$tmpFlag = 1;
		}
		if($tmpFlag == 0)
		{
			$prefix_group_no = $j;
			break;
		}
	}
	echo "<input type=\"hidden\" name=\"addFlag\" value=\"1\">";
}


echo "<table width=\"100%\">";
echo "<tr><td>";
echo "<b>Local prefix:&nbsp;&nbsp;</b>";
echo "<input type=\"submit\" value=$strSave class=\"button\" name=\"Set\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";	
echo "<input type=\"reset\" value=$strReset  class=\"button\" name=\"Reset\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";
echo "</td>";
echo "<td><p align=\"right\"><a href=\"./msc.php\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
echo "</td></tr>";
echo "</table>";
?>

<TABLE  width = 100% border="1"  cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF"> 

<?php

echo "<tr bgcolor = \"#E6E6E6\">";
echo "<td width=\"25%\">Parameters</td>";
echo "<td width=\"25%\">Value</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Local prefix</td>";
echo "<td><input type=\"text\" size = \"16\" maxlength=\"16\" value=\"$prefix\" class=\"text\" 
		name=\"prefix_new\" onchange = checkInput(this,10,0)></td>";
		
echo "<input type=\"hidden\" name=\"prefix_group_no\" value=\"$prefix_group_no\">";
echo "<input type=\"hidden\" name=\"cc\" value=\"$cc\">";
echo "<input type=\"hidden\" name=\"ndc\" value=\"$ndc\">";
echo "<input type=\"hidden\" name=\"IPrefix\" value=\"$IPrefix\">";
echo "<input type=\"hidden\" name=\"NPrefix\" value=\"$NPrefix\">";
echo "</table>";

?> 

<?php
echo "</form>";
?>
