<?php
require("../sub_inc/commfunc.php");
$DEBUG = 0;

if($DEBUG) echo "<br>正在调试中！！";
//==========================================================
//变量定义
//==========================================================

//*****
//$url_temp=$_SERVER["REQUEST_URI"];
//echo "This page URL:$url_temp<br>";


$pubDb			= 'OMC_PUB';
$numberPerPage	= 100;		//每页纪录数
$paramTable		='OBJ_223.param_99';

$nEnable = '01';

$sysId		='223_99_99';
$sysIdArr	= explode('_',$sysId);
$sysTypeNo	= $sysIdArr[0];
$sysNo		= $sysIdArr[1];
$subSysNo	= $sysIdArr[2];

//echo "sub_page_top:$sub_page_top<br>";

$objDb		= 'OBJ_'.$sysTypeNo;
if(1)
{//get ni & dpc
	{//get dpcRow
		$spcTree_array = array(
		array('INT',		'14-bit',	'spc_int_14',	 	'2.3.1'),
		array('INT-S',  	'14-bit',	'spc_int_sp_14',	'2.3.2'),
		array('NAT',		'14-bit',	'spc_nat_14',	 	'2.3.3'),
		array('NAT-S',  	'14-bit',	'spc_nat_sp_14',	'2.3.4'),
		array('International',			'24-bit',	'spc_int_24',	 	'2.3.5'),
		array('International spare',	'24-bit',	'spc_int_sp_24',	'2.3.6'),
		array('National',				'24-bit',	'spc_nat_24',	 	'2.3.7'),
		array('National spare',			'24-bit',	'spc_nat_sp_24',	'2.3.8')
		);
		$sqltable_spc = 'OBJ_222.param_99';
		$querysql = "select initValue from $sqltable_spc where level_1 = '2' and level_2 = '2'  and level_3 = '0'";
		$result = mysqli_query($pubConn,$querysql);
		$indicator = mysqli_fetch_array($result);
		echo mysqli_error($pubConn);
		if ($DEBUG )echo "<br>indicator = {$indicator[0]}<br>";
		
		for($i = 0; $i < 4; $i++)
		{
			$tmpflag = getBitValue($indicator[0],"0.$i");
			if ($tmpflag == 0)
				$spc_index[$i] = $i;
			else
				$spc_index[$i] = $i+4;
		}
		
		for($i = 0; $i < 4; $i ++)
		{
			$levelWhere = getLevelWhere($spcTree_array[$spc_index[$i]][3]);
			$getsql = "select initValue from $sqltable_spc where $levelWhere ";
			$result = mysqli_query($pubConn,$getsql);
			$dpcRow[$i] = mysqli_fetch_array($result);
			$dpcRow[$i] = hexdec($dpcRow[$i][0]);
			if ($DEBUG )
			{
				echo "<br>getsql = $getsql<br>";
				echo "<br>spc_$i=$dpcRow[$i]";
			}
		}
	}//end get dpcRow
	$getsql = "select initValue from $paramTable
	where level_1 = '2' and level_2 = '1' and level_3 = '6' ";
	$result = @mysqli_query($pubConn,$getsql);
	$sccp_row = @mysqli_fetch_array($result);
	echo mysqli_error($pubConn);
	$ni= hexdec(getBitValue($sccp_row[0],"0.0-0.7"));
	$gtt = getBitValue($sccp_row[0],"1.0-10.7");
	$gtt = str_replace('F','',$gtt);
	$DPC=$dpcRow[$ni];
}//end get ni & dpc

//echo "<br>ni=$ni";
//echo "<br>DPC=$DPC";
//==========================================================
//输入变量
//==========================================================
if($DEBUG) echo "<BR>sysId		= $sysId";
if($DEBUG) echo "<BR>instanceNo	= $instanceNo";
if($DEBUG) echo "<BR>objIdStr	= $objIdStr";


$GTTtableConfArr=array(
	array('Start number',			'03.0-12.7'),
	array('End number',				'13.0-22.7'),
	array('On/off',					'00.0-00.7'),
	array('Numbering plan',			'01.0-01.7'),
	array('Network indicator',		'23.0-23.7'),
	array('DPC',					'24.0-26.7'),
	array('SSN',					'27.0-27.7'),
	array('Route indicator',		'28.0-28.7'),
	array('Address indicator',		'29.0-29.7'),
	array('Replaced start digit',	'30.0-30.7'),
	array('Replaced end digit',		'31.0-31.7'),
	array('Outgoing number plan',	'32.0-32.7'),
	array('Replacing digits',		'33.0-42.7'),
	array('TT',						'02.0-02.7')
);

$NetIdConfArr=array(
	array('0','International'),	
	array('1','International spare'),
	array('2','National'),
	array('3','National spare')
);

$numPlanConfArr=array(
//	array('00','0'),
//	array('01','MSISDN'),
	array('06','IMSI(E.212)'),
	array('07','GT(E.214)')
);
$ttArr = array(14,10,142);

$addressIdConfArr=array(
	array('00','NULL'),
	array('01','DPC'),
	array('02','SSN'),
	array('03','DPC+SSN'),
	array('04','GT'),
	array('05','GT+DPC'),
	array('06','GT+SSN'),
	array('07','GT+DPC+SSN')
);

function showNumber($number)
{
	$newNumber =str_replace('F','',$number);
	return($newNumber);
}
//==========================================================
//数据处理
//==========================================================
$niTypeArr = getNiType();
//保存改动
if($_SERVER['REQUEST_METHOD'] == "POST")
{
	$nStartDigit = omc_keep_length($nStartDigit,"F","20","0");
	$nEndDigit = omc_keep_length($nEndDigit,"F","20","0");
	$nDPC = dechex($DPC);
	$nDPC = omc_keep_length($nDPC,"0","6","1");

	$nSSN = dechex(6);//roger
	$nSSN = omc_keep_length($nSSN,"0","2","1");//roger
	if($niTypeArr[$ni] == 0)//ITU 
	{
		
		//========IMSI====
		if($flag=='imsi')
		{
			$nTT = '00';
			//$nNumPlan = '06';
			$nNumPlan = $setNP;
		}
		//========MSISDN====
		else
		{
			$nTT = omc_keep_length(dechex($setTT),0,2);
			$nNumPlan = '01';
		}
	}
	else//ANSI
	{
		$nNumPlan = '00';
		$nTT = omc_keep_length(dechex($setTT),0,2);
	}
	//echo "setTT:$setTT<hr>";
	$NI = omc_keep_length($ni,'0','2');
	$nRep_digit = omc_keep_length('',"F","20","0");
	$nRep_start = omc_keep_length(0,"0","2","1");
	$nRep_end = omc_keep_length(0,"0","2","1");
		
	$totalValue=omc_keep_length($totalValue,'0','86');
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[0][1],$nStartDigit);
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[1][1],$nEndDigit);
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[2][1],'01');//on/off
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[3][1],$nNumPlan);
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[4][1],$NI);//NI
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[5][1],$nDPC);
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[6][1],$nSSN);

	$totalValue=setBitValue($totalValue,$GTTtableConfArr[7][1],0);//Route indicator 
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[8][1],'07');//Address indicator 
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[9][1],$nRep_start);
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[10][1],$nRep_end);
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[11][1],0);//Outgoing num_plan
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[12][1],$nRep_digit);
	$totalValue=setBitValue($totalValue,$GTTtableConfArr[13][1],$nTT);
		
	$objIdStr='2.2';
	$objWhereStr =getLevelWhere($objIdStr);
	$totalValue = strtoupper($totalValue);
	$updSql = "UPDATE $paramTable SET initValue = '$totalValue'   
					WHERE $objWhereStr AND instanceNo='$instanceNo'
					";
	mysqli_query($pubConn,$updSql);
	echo mysqli_error($pubConn);
	
	{//set gtt attribution from 2.4
		$selSql = "select initValue from $paramTable where level_1='2' and level_2='4' and instanceNo='$instanceNo'";
		$Result = mysqli_query($pubConn,$selSql);
		$Row = mysqli_fetch_array($Result);
		$setValue = $Row[initValue];
		$eleName = 'HLR';
		$tmpEleName = omc_keep_length(toHex($eleName),"0",20,"0");
		
		$setValue = setBitValue($setValue,'3.0-12.7',$tmpEleName);
		
		$updSql = "update $paramTable set initValue = '$setValue' where level_1='2' and level_2='4' and instanceNo='$instanceNo'";
		mysqli_query($pubConn,$updSql);
		echo mysqli_error($pubConn);
	}//end set gtt attribution form 2.4
	if($DEBUG)
	{
		echo "DEBUG<br>";
	}
	else
	{	
		echo "<script LANGUAGE=\"JavaScript\">";
		echo " location='$backurl';";  
		echo "</script>";
		exit();
	}
}

//==========================================================
//输出并显示数据
//==========================================================
if($DEBUG) echo "<BR>";
?>
<br>
<body topmargin="0" leftmargin="12">
<form method="POST" name="selectform" action="<?php echo "$PHP_SELF?"; ?>">

<?php 

{
	echo "<table border=\"0\" width=\"100%\">";
	echo "<tr>";
	echo "<td width=\"50%\">";
	$CurName = strtoupper($flag);
	echo "$strConfiguration > <a href=\"../overview/overview.php\">Network</a> > <a href=\"$backurl\">HLR</a> > <a href=\"#\">$CurName Range</a>";
	echo "</td>";
	echo "<td width=\"50%\" 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>$CurName Range:&nbsp;&nbsp;</b>";
	echo "</td>";
	echo "<td width=\"20%\">";
	echo "<input type=\"submit\" name=\"set\" value=\"Save\" class=\"button\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<input type=\"reset\" name=\"Reset\" value=$strUndo class=\"button\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";
	echo "</td>";
	echo "<td width=\"10%\" align=\"right\">";
	$backurl=$backurl."&scroll_y=".$sub_page_top;
	echo "<a href=\"$backurl\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
	
	echo "<input type=\"hidden\" name=\"linkset\" value=\"$linkset\">";
	echo "<input type=\"hidden\" name=\"backurl\" value=\"$backurl\">";
	echo "<input type=\"hidden\" name=\"trunkgp\" value=\"$trunkgp\">";
	echo "<input type=\"hidden\" name=\"webname\" value=\"$webname\">";
	echo "</td></tr>";
	echo "</table>";
}
echo "<table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";	

$objIdStr='2.2';
$objWhereStr =getLevelWhere($objIdStr);
$getSql="SELECT initValue
			FROM $paramTable 
			WHERE $objWhereStr AND instanceNo='$instanceNo'
			";
if($DEBUG) echo "<BR>getSql=$getSql";
$result	=mysqli_query($pubConn,$getSql);
echo mysqli_error($pubConn);
$rows	=mysqli_fetch_array($result);
echo mysqli_error($pubConn);
$value=$rows[0];

$SsnValue = 6;

$startDigit	=getBitValue($value,$GTTtableConfArr[0][1]);
$startDigit	=showNumber($startDigit);
$endDigit	=getBitValue($value,$GTTtableConfArr[1][1]);
$endDigit	=showNumber($endDigit);

$enableFlag	=getBitValue($value,$GTTtableConfArr[2][1]);
$numPlan	=getBitValue($value,$GTTtableConfArr[3][1]);
$TT			=hexdec(getBitValue($value,$GTTtableConfArr[13][1]));

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

echo "<tr>";
echo "<td>Start $CurName number</td>";
echo "<td><input type=\"text\" size = \"30\" maxlength=\"20\" value=\"$startDigit\" class=\"text\" name=\"nStartDigit\"  onchange = checkInput(this,10,0)></td>";
echo "</tr>";
echo "<tr>";
echo "<td>End $CurName number</td>";
echo "<td><input type=\"text\" size = \"30\" maxlength=\"20\" value=\"$endDigit\" class=\"text\" name=\"nEndDigit\"  onchange = checkInput(this,10,0)></td>";
echo "</tr>";


if($niTypeArr[$ni] == 0)//ITU
{
	if($flag=='imsi')
	{
		echo "<tr>";
		echo "<td>Number plan</td>";
		echo "<td>";
		echo "<select name=\"setNP\" style=width:\"195\">";
		for($j=0;$j<sizeof($numPlanConfArr);$j++){
			if($numPlan == $numPlanConfArr[$j][0]){
				echo "<option value=\"{$numPlanConfArr[$j][0]}\" selected>{$numPlanConfArr[$j][1]}";
			}else{
				echo "<option value=\"{$numPlanConfArr[$j][0]}\" >{$numPlanConfArr[$j][1]}";
			}	
		}
		echo "</select>";
		echo "</td>";
		echo "</tr>";
		
		
	}
	
	else
	{
		echo "<tr>";
		echo "<td>Translation Type</td>";
		echo "<td>";
		echo "<select name=\"setTT\" style=width:\"195\">";
		$ttArr_ITU=array(0,128);
		$ttArr_ITU_label=array(0=>"NOT USED(0)",128=>"MNP ENABLED(128)");
		for($j=0;$j<sizeof($ttArr_ITU);$j++){
			if($TT == $ttArr_ITU[$j]){
				echo "<option value=\"{$ttArr_ITU[$j]}\" selected>{$ttArr_ITU_label[$ttArr_ITU[$j]]}";
			}else{
				echo "<option value=\"{$ttArr_ITU[$j]}\" >{$ttArr_ITU_label[$ttArr_ITU[$j]]}";
			}	
		}
		echo "</select>";
		echo "</td>";
	}
	
	
}
else//ANSI
{
	echo "<tr>";
	echo "<td>Translation Type</td>";
	echo "<td>";
	$ttArr_ANSI_label=array(10=>"NET NODE(10)",14=>"MSISDN(14)",142=>"MNP ENABLED(142)");
	if($flag=='imsi') 
	{
		$ttValue=9;
		echo $ttValue;
		echo "<input type=\"hidden\" name=\"setTT\" value=\"$ttValue\">";
	}
	else	
	{
		$ttValue=14;
		echo "<select name=\"setTT\" style=width:\"195\">";
		for($j=0;$j<sizeof($ttArr);$j++){
			if($TT == $ttArr[$j]){
				echo "<option value=\"{$ttArr[$j]}\" selected>{$ttArr_ANSI_label[$ttArr[$j]]}";
			}else{
				echo "<option value=\"{$ttArr[$j]}\" >{$ttArr_ANSI_label[$ttArr[$j]]}";
			}	
		}
		echo "</select>";
	}
	echo "</td>";
	echo "</tr>";
}
echo "</table>";
echo "<br>";

echo "\n <input type=\"hidden\" name=\"appType\" value=\"$appType\">";
echo "\n <input type=\"hidden\" name=\"flag\" value=\"$flag\">";
echo "\n <input type=\"hidden\" name=\"ni\" value=\"$ni\">";
echo "\n <input type=\"hidden\" name=\"DPC\" value=\"$DPC\">";
echo "\n <input type=\"hidden\" name=\"instanceNo\" value=\"$instanceNo\">";

?>

<script LANGUAGE="JavaScript"> 

function checkInput(term,flag,maxValue){
	term.value = term.value.toUpperCase();
    
    if (flag == 16)
    {
    	var checkOK = "0123456789ABCDEF";
    	var showmsg ="Parameter must be a hexadecimal value";
    }
    else 
    {
    	var checkOK = "0123456789";
    	var showmsg ="Parameter must be a decimal value";
    }

    var checkStr = term.value;
    var allValid = true;
    for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++){
			if (ch == checkOK.charAt(j))
				break;
		}
		if (j == checkOK.length){
			allValid = false;
			break;
		}
	}
	
	if (!allValid){  
		alert(showmsg+"!");
		term.focus();
		term.value = 0;
		return (false);
	}
	if (maxValue != 0)
	{
		if (checkStr > maxValue)
		{
			showmsg = "Parameter must less then "+ maxValue;
			alert(showmsg+"!");
			term.value = 0;
			term.focus();
			return (false);
		}
	}
}
</script>
