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


<?php
$pubdb = 'OMC_PUB';
$net_paramTable		='OBJ_225.param_99';
$net_paramConf		='OBJ_225.paramConf';
$net_sysId		='225_99_99';

$vlr_paramTable		='OBJ_325.param_99';
$vlr_paramConf		='OBJ_325.paramConf';
$vlr_sysId		= '325_99_99';
$vlr_oid = '2.1.13';

$mc_paramTable		='OBJ_350.param_99';
$mc_paramConf		='OBJ_350.paramConf';
$mc_sysId		= '350_99_99';
$mc_oid = '2.1.13';

$msc_paramTable		='OBJ_322.param_99';
$msc_paramConf		='OBJ_322.paramConf';
$msc_sysId		= '322_99_99';    
$listPageUrl = "msc.php";                       

$DEFAULT_MSRN_RAC = 32;

$NetworkCodeArr=array(
//array('oid',	'inputName','Maxlength',	'showName'),
	array('2.3.0',	'cc'	,	'4',		'Country code(CC)'),
	array('2.4.0',	'ndc'	,	'6',		'National destination code(NDC)'), 
	array('2.1.0',	'mcc'	,	'4',		'Mobile country code(MCC)'),
	array('2.2.0',	'mnc'	,	'4',		'Mobile network code(MNC)'), 
	array('2.5.0',	'IPrefix',	'4',		'International prefix'),
	array('2.6.0',	'NPrefix',	'4',		'National prefix')	
);

for ($i = 0;$i < sizeof($NetworkCodeArr);$i++)
{
	$sqltable = 'OBJ_225.param_99';
	
	$levelWhere = getLevelWhere($NetworkCodeArr[$i][0]);
	$getsql = "SELECT initValue FROM $sqltable where $levelWhere";
	
	if($DEBUG) echo "<br>getsql = $getsql";
	$result = @mysqli_query($pubConn,$getsql);
	$Net_row = @mysqli_fetch_array($result);
	
	${$NetworkCodeArr[$i][1]} = str_replace('E','',$Net_row[0]);
	echo mysqli_error($pubConn);
};

if($DEBUG) 
{
	echo "<br>instanceNo = $instanceNo<br>";
	echo "cc = $cc<br>";
	echo "ndc = $ndc<br>";
	echo "IPrefix = $IPrefix<br>";
	echo "NPrefix = $NPrefix<br>";
}
	
//=========================================================//
//Get Database data                                        //
//=========================================================//
if($typeFlag == 'TLDN')
{
	$getsql = "select initValue from $vlr_paramConf where level_1 = '2' and level_2 = '1' and level_3 = '13'";  
	$msrn_org = @mysqli_query($pubConn,$getsql);
	$msrn_org = @mysqli_fetch_array($msrn_org);
	if($DEBUG) echo "getsql = $getsql<br>";
	$msrn_org = $msrn_org[0];
	echo mysqli_error($pubConn);
	if($DEBUG) echo "msrn_org = $msrn_org<br>";
	
	$getsql = "select initValue from $msc_paramConf where level_1 = '2' and level_2 = '1' and level_3 = '1'";  
	$subprefix_org = @mysqli_query($pubConn,$getsql);
	$subprefix_org = @mysqli_fetch_array($subprefix_org);
	$subprefix_org = $subprefix_org[0];
	echo mysqli_error($pubConn);
	if($DEBUG) echo "subprefix_org = $subprefix_org<br>";
			
	$getsql = "select initValue from $msc_paramConf where level_1 = '2' and level_2 = '1' and level_3 = '2'";  
	$subpre_attr_org = @mysqli_query($pubConn,$getsql);
	$subpre_attr_org = @mysqli_fetch_array($subpre_attr_org);
	$subpre_attr_org = $subpre_attr_org[0];
	echo mysqli_error($pubConn);
	if($DEBUG) echo "subpre_attr_org = $subpre_attr_org<br>";
	
	$getsql = "select initValue from $msc_paramTable where level_1 = '2' and level_2 = '1' and level_3 = '1' and instanceNo < '400'";  
	$sub_prefix_rst = @mysqli_query($pubConn,$getsql);
	echo mysqli_error($pubConn);
	if($DEBUG) echo "getsql = $getsql<br>";
	
	$sub_ra_org = hexdec(getBitValue($subprefix_org,'8.0-8.7'));
	if($DEBUG) echo "sub_ra_org=$sub_ra_org<br>";
	$subprefix_no = 0;
	$unused_no = 0;
	$i = 0;
	while($sub_prefix_row = @mysqli_fetch_array($sub_prefix_rst))
	{
		$sub_prefix_row = $sub_prefix_row[0];
		$sub_ra = hexdec(getBitValue($sub_prefix_row,'8.0-8.7'));
		if($DEBUG) echo "sub_ra=$sub_ra<br>";
		if($sub_ra == $DEFAULT_MSRN_RAC)
		{
			$curr_sub_ins = $i;
			break;
		}
		else if($sub_prefix_row == $subprefix_org)
		{
			$unused_sub_ins[$unused_no] = $i;
			$unused_no++;	
		}
		$i++;
	}
}
else
{
	$getsql = "select initValue from $mc_paramConf where level_1 = '2' and level_2 = '1' and level_3 = '4'";  
	$msisdn_org = @mysqli_query($pubConn,$getsql);
	$msisdn_org = @mysqli_fetch_array($msisdn_org);
	$msisdn_org = $msisdn_org[0];
	echo mysqli_error($pubConn);
	if($DEBUG) echo "msisdn_org = $msisdn_org<br>";
}

if($_SERVER['REQUEST_METHOD'] == "POST")
{
	//update vlr-TLDN
	if($typeFlag == 'TLDN')
	{
		//blank if start or end value equals cc+ndc
		$start == '' ? $start = '' : $start = $cc.$ndc.$start;
		$end == '' ? $end = '' : $end = $cc.$ndc.$end;
		
		$start = omc_keep_length($start,'F','16','0');
		$end = omc_keep_length($end,'F','16','0');
		$init = setBitValue($msrn_org,'0.0-7.7',$start);
		$init = setBitValue($init,'8.0-15.7',$end);
		$setsql = "update $vlr_paramTable set initValue = '$init' 
		where level_1 = '2' and level_2 = '1' and level_3 = '13' and instanceNo = '$DEFAULT_MSRN_RAC'";
		if($DEBUG) echo "setsql = $setsql<br>";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
		
		for($i=0; $i < strlen($start); $i++)
		{
			if(substr($start,0,$i) != substr($end,0,$i))
				break;	
			$msrn_prefix = substr($start,0,$i);
		}
		if($DEBUG) echo "msrn_prefix=$msrn_prefix<br>";
		
		$prefix_loc = array('0.0-7.7','8.0-8.7');
		$ra_no = omc_keep_length(dechex($DEFAULT_MSRN_RAC),'0','2');
		$prefix_arr = array(omc_keep_length($msrn_prefix,'E','16','0'),$ra_no);
		
		$pre_attr_loc = array('0.0-0.3','0.4-0.7','10.0-10.0','10.2-10.3','10.4-10.6','10.7-10.7','11.0-11.7','12.0-12.3',
			'12.4-12.7','13.0-16.7','17.0-17.7');
			
		//array('TON incoming call','TON outgoing call','CDR','Properties','Call type','Enable','Length of dialed number','deletion count',
		//	'insertion count','inserted digits','Cli-type','annoucement');
		$pre_attr_arr =	array('0100','0100','0','10','000','1',omc_keep_length(strlen($cc.$ndc.$msrn_start),'0','2'),'0',
			'0',omc_keep_length(0,'E','8','0'),'02');
			
		$prefix_init = $subprefix_org;
		for($j=0; $j < sizeof($prefix_loc); $j++)
			$prefix_init = setBitValue($prefix_init,$prefix_loc[$j],$prefix_arr[$j]);
		
		$prefix_attr_init = $subpre_attr_org;
		for($j=0; $j < sizeof($pre_attr_loc); $j++)
		{
			if($DEBUG) 
			{
				echo "pre_attr_loc = $pre_attr_loc[$j]<br>";
				echo "pre_attr_arr = {$pre_attr_arr[$i][$j]}<br>";
			}
			$prefix_attr_init = setBitValue($prefix_attr_init,$pre_attr_loc[$j],$pre_attr_arr[$j]);
		}
		
		if($DEBUG) 
		{
			echo "prefix_init = $prefix_init<br>";
			echo "prefix_attr_init = $prefix_attr_init<br>";
		}
		
		if(!isset($curr_sub_ins))
			$curr_sub_ins = $unused_sub_ins[0];
		
		$setsql = "update $msc_paramTable set initValue = '$prefix_init' 
		where level_1 = '2' and level_2 = '1' and level_3 = '1' and instanceNo = '$curr_sub_ins'";
		if($DEBUG) echo "setsql = $setsql<br>";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
		
		$setsql = "update $msc_paramTable set initValue = '$prefix_attr_init' 
		where level_1 = '2' and level_2 = '1' and level_3 = '2' and instanceNo = '$curr_sub_ins'";
		if($DEBUG) echo "setsql = $setsql<br>";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);		
	}
	else
	{
		if($DEBUG) 
		{
			echo "start = $start<br>";
			echo "end = $end<br>";
		}
		
		$init = $msisdn_org;
		//blank if start or end value equals cc+ndc
		if($start != '')
		{
			//$start = omc_keep_length($cc.$ndc.$start,'E','18','0');
			$start = omc_keep_length($start,'E','18','0');
			$init = setBitValue($init,'00.0-08.7',$start);
		}
		
		if($end != '')
		{
			//$end = omc_keep_length($cc.$ndc.$end,'E','18','0');
			$end = omc_keep_length($end,'E','18','0');
			$init = setBitValue($init,'09.0-17.7',$end);	
		}	
		$setsql = "update $mc_paramTable set initValue = '$init' 
		where level_1 = '2' and level_2 = '1' and level_3 = '4' and instanceNo = '$instanceNo'";
		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=\"$backurl\";";
	} 
	echo "\n </script>";
}

$i = 0;
$free_no = 0;

if($typeFlag == 'TLDN')
{
	$getsql = "select initValue from $vlr_paramTable where level_1 = '2' and level_2 = '1' and level_3 = '13' and instanceNo = '$DEFAULT_MSRN_RAC'";  
	$msrn_rst = @mysqli_query($pubConn,$getsql);
	echo mysqli_error($pubConn);
	if($DEBUG) echo "getsql = $getsql<br>";
	
	$msrn_row = @mysqli_fetch_array($msrn_rst);
	$msrn_row = $msrn_row[0];
	
	if($DEBUG) echo "<br>msrn_row = $msrn_row<br>";
	$start = getBitValue($msrn_row,'00.0-07.7');
	$end = getBitValue($msrn_row,'08.0-15.7');
	
	$prefix_length = strlen($cc.$ndc);
	$tmp_len = strlen($start) - $prefix_length;
	$start = substr($start,$prefix_length,$tmp_len);		
	$tmp_len = strlen($end) - $prefix_length;
	$end = substr($end,$prefix_length,$tmp_len);		
	$start = str_replace('F','',$start);
	$end = str_replace('F','',$end);
	if($DEBUG) 
	{
		echo "start = $start<br>";
		echo "end = $end<br>";
	}
}
else
{
	$getsql = "select initValue from $mc_paramTable where level_1 = '2' and level_2 = '1' and level_3 = '4' and instanceNo = '$instanceNo'";  
	$msisdn_rst = @mysqli_query($pubConn,$getsql);
	$msisdn_rst = @mysqli_fetch_array($msisdn_rst);
	$msisdn_rst = $msisdn_rst[0];
	echo mysqli_error($pubConn);
	if($DEBUG) echo "getsql = $getsql<br>";
	
	if($DEBUG) echo "<br>msisdn_rst = $msisdn_rst<br>";
	if($msisdn_rst == $msisdn_org)
		$msisdn_rst = '';
	$start = getBitValue($msisdn_rst,'00.0-08.7');
	$end = getBitValue($msisdn_rst,'09.0-17.7');
	if($DEBUG) 
	{
		echo "start = $start<br>";
		echo "end = $end<br>";
	}
	/*
	$prefix_length = strlen($cc.$ndc);
	$tmp_len = strlen($start) - $prefix_length;
	$start = substr($start,$prefix_length,$tmp_len);		
	$tmp_len = strlen($end) - $prefix_length;
	$end = substr($end,$prefix_length,$tmp_len);		
	*/
	$start = str_replace('E','',$start);
	$end = str_replace('E','',$end);
	if($DEBUG) 
	{
		echo "start = $start<br>";
		echo "end = $end<br>";
	}
}

echo "<form method=\"POST\" name=\"Set\" action=\"$PHP_SELF\">";
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"80%\">";
if($typeFlag == 'TLDN')
{
	echo "Configuration > Network > Element > MSC ";
	echo "<td width=\"20%\" align=\"right\" title=\"Help\">";
	//showHelp($helpId);
	echo "</td>";
	echo "</tr>";
	echo "</table>";
	echo "<br>";
	
	echo "<table width=\"100%\">";
	echo "<tr><td>";
	echo "<b>Routing area:&nbsp;&nbsp;&nbsp;&nbsp;</b>";
	if($instanceNo == 'new')
	{
		echo "<select name=\"ra_ins\">";
		for($i=0; $i < $free_no; $i++)
		{
			echo "<option value=\"$ra_unused[$i]\">$ra_unused[$i]";
		}
		echo "</select>&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	else
	{
		echo "$instanceNo&nbsp;&nbsp;&nbsp;&nbsp;";	
	}
}
else
{
	echo "$strConfiguration > <a href=\"../overview/overview.php\">Network</a> > <a href=\"$backurl\">SMSC</a> > <a href=\"#\">$typeFlag</a>";
	echo "<td width=\"20%\" align=\"right\" title=\"Help\">";
	//showHelp($helpId);
	echo "</td>";
	echo "</tr>";
	echo "</table>";
	echo "<br>";
}
	
echo "<table width=\"100%\">";
echo "<tr><td width=\"70%\">&nbsp";
//echo "<b>$typeFlag Segment:&nbsp;&nbsp;&nbsp;&nbsp;</b>";
echo "</td>";
echo "<td width=\"20%\">";
echo "<input type=\"submit\" value=$strSave class=\"button\" name=\"Set\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";	
echo "&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<input type=\"reset\" value=$strUndo  class=\"button\" name=\"Reset\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";
echo "</td>";
$backurl=$backurl."?scroll_y=".$sub_page_top;
echo "<td width=\"10%\"><p align=\"right\"><a href=\"$backurl\"><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
//Title
{
	echo "<tr bgcolor = \"#E6E6E6\">";
	echo "<td width=\"50%\"><b>Parameters</b></td>";
	echo "<td width=\"50%\"><b>Value</b></td>";
	echo "</tr>";
}
//TLDN or MSISDN
{
	$prefix = $cc.$ndc;
	if($typeFlag == 'TLDN')
		$len = 16 - strlen($prefix);
	else
		$len = 18 - strlen($prefix);
	echo "<tr>";
	echo "<td width=50%>Start $typeFlag</td>";
	//echo "<td>$prefix<input type=\"text\" size = \"16\" maxlength=\"$len\" value=\"$start\" class=\"text\" 
	//		name=\"start\" onchange = checkInput(this,10,0)></td>";
	echo "<td><input type=\"text\" size = \"16\" maxlength=\"$len\" value=\"$start\" class=\"text\" 
			name=\"start\" onchange = checkInput(this,10,0)></td>";
			
	echo "</tr><tr>";
	echo "<td width=50%>End $typeFlag</td>";
	//echo "<td>$prefix<input type=\"text\" size = \"16\" maxlength=\"$len\" value=\"$end\" class=\"text\" 
	//		name=\"end\" onchange = checkInput(this,10,0)></td>";
	echo "<td><input type=\"text\" size = \"16\" maxlength=\"$len\" value=\"$end\" class=\"text\" 
			name=\"end\" onchange = checkInput(this,10,0)></td>";
	echo "<tr>";
}
echo "<input type=\"hidden\" name=\"typeFlag\" value=\"$typeFlag\">";
echo "<input type=\"hidden\" name=\"instanceNo\" value=\"$instanceNo\">";
echo "<input type=\"hidden\" name=\"backurl\" value=\"$backurl\">";
echo "</table>";

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

?>
