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

<script LANGUAGE="JavaScript">  
function checkSelect(term,old)
{
	var select_val = term.value;
	if(select_val == 'all' & old != 'all')
	{
		window.location.href = '<?php echo"$PHP_SELF?ra_ins=all&allFlag=1&instanceNo=new&cc=$cc&ndc=$ndc&IPrefix=$IPrefix&NPrefix=$NPrefix"; ?>';
	}
	else if(select_val != 'all' & old == 'all')
	{
		window.location.href = '<?php echo"$PHP_SELF?ra_ins='+select_val+'&allFlag=0&instanceNo=new&cc=$cc&ndc=$ndc&IPrefix=$IPrefix&NPrefix=$NPrefix"; ?>';
	}		
}
</script>

<?php
$pubdb = 'OMC_PUB';
$netdb = 'OBJ_225';
$net_paramTable		='param_99';
$net_paramConf		='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';

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

if($DEBUG) 
{
	echo "ra_ins = $ra_ins<br>";
	echo "all_ins_no = $all_ins_no<br>";
	echo "allFlag = $allFlag<br>";
	echo "instanceNo = $instanceNo<br>";
	echo "cc = $cc<br>";
	echo "ndc = $ndc<br>";
	echo "IPrefix = $IPrefix<br>";
	echo "NPrefix = $NPrefix<br>";
}
	
//=========================================================//
//Get Database data                                        //
//=========================================================//
$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);
$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_paramConf where level_1 = '2' and level_2 = '6' and level_3 = '5'";  
$bill_org = @mysqli_query($pubConn,$getsql);
$bill_org = @mysqli_fetch_array($bill_org);
$bill_org = $bill_org[0];
echo mysqli_error($pubConn);
if($DEBUG) echo "bill_org = $bill_org<br>";

$getsql = "select initValue from $vlr_paramTable where level_1 = '2' and level_2 = '1' and level_3 = '13'";  
$msrn_rst = @mysqli_query($pubConn,$getsql);
echo mysqli_error($pubConn);
if($DEBUG) echo "getsql = $getsql<br>";
		
$getsql = "select initValue from $msc_paramTable where level_1 = '2' and level_2 = '6' and level_3 = '5'";  
$bill_mscid_rst = @mysqli_query($pubConn,$getsql);
echo mysqli_error($pubConn);
if($DEBUG) echo "getsql = $getsql<br>";

$getsql = "select initValue from $msc_paramTable where level_1 = '2' and level_2 = '1' and level_3 = '2' and instanceNo < '400'";  
$prefix_attr_rst = @mysqli_query($pubConn,$getsql);
echo mysqli_error($pubConn);
if($DEBUG) echo "getsql = $getsql<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))
{
	$prefix_attr_row = mysqli_fetch_array($prefix_attr_rst);
	$prefix_attr_row = $prefix_attr_row[0];
	$sub_prefix_row = $sub_prefix_row[0];
	$sub_ra = hexdec(getBitValue($sub_prefix_row,'8.0-8.7'));
	$flag = getBitValue($prefix_attr_row,'10.1');
	if($sub_ra != $sub_ra_org && $sub_prefix_row != $subprefix_org)
	{
		$sub_prefix_tmp[$subprefix_no] = $sub_prefix_row;
		$sub_prefix_ins[$subprefix_no] = $i;
		$prefix_attr_tmp[$subprefix_no] = $prefix_attr_row;
		$msrn_prefix_flag[$subprefix_no] = $flag;
		$subprefix_no++;
	}
	else
	{
		if($DEBUG)echo "unused_no = $i<br>";
		$curr_sub_ins[$unused_no] = $i;
		$unused_no++;
	}
	$i++;
}

if($_SERVER['REQUEST_METHOD'] == "POST")
{
	if($instanceNo == 'new')
	{
		if($ra_ins == 'all')
			$ra_ins = $all_ins_no;
			
		$ra_no = $ra_ins;
	}
	else
	{
		$ra_no = $instanceNo;
		$msrn_prefix_found_flag == 0;
		$sub_tmp = 0;
		$i=0;
		while($sub_tmp < $subprefix_no)
		{
			$sub_row_tmp = $sub_prefix_tmp[$sub_tmp];
			$sub_ra = hexdec(getBitValue($sub_row_tmp,'8.0-8.7'));
			if($DEBUG) echo "<br>sub_ra = $sub_ra<br>";
			if($ra_no == $sub_ra)
			{
				if($msrn_prefix_flag[$sub_tmp] == 0)
				{
					$curr_sub_ins[$i] = $sub_prefix_ins[$sub_tmp];
					if($DEBUG) echo "<br>curr_sub_ins = $curr_sub_ins[$i]<br>";
					$i++;
				}
				else if($msrn_prefix_flag[$sub_tmp] == 1 && $msrn_prefix_found_flag == 0)
				{
					$msrn_prefix_insNo = $sub_prefix_ins[$sub_tmp]; //TLDN prefix instanceNo
					if($DEBUG) echo "<br>msrn_prefix_insNo = $msrn_prefix_insNo<br>";
					$msrn_prefix_found_flag == 1;
				}
			}
			$sub_tmp++;
			
		}	
	}
	if($DEBUG) 
	{	
		echo "instanceNo = $instanceNo<br>";
		echo "ra_no = $ra_no<br>";
		echo "sub_prefix = $sub_prefix<br>";
		echo "msrn_start = $msrn_start<br>";
		echo "msrn_end = $msrn_end<br>";
		echo "bill_market = $bill_market<br>";
		echo "bill_switch = $bill_switch<br>";
	}
	//update vlr-TLDN
	if($allFlag != 1)
	{
		//blank if start or end value equals cc+ndc
		$msrn_start == '' ? $start = '' : $start = $cc.$ndc.$msrn_start;
		$msrn_end == '' ? $end = '' : $end = $cc.$ndc.$msrn_end;
		
		$start = omc_keep_length($start,'F','16','0');
		$end = omc_keep_length($end,'F','16','0');
		$msrn_init = setBitValue($msrn_org,'0.0-7.7',$start);
		$msrn_init = setBitValue($msrn_init,'8.0-15.7',$end);
		$setsql = "update $vlr_paramTable set initValue = '$msrn_init' 
		where level_1 = '2' and level_2 = '1' and level_3 = '13' and instanceNo = '$ra_no'";
		if($DEBUG) echo "setsql = $setsql<br>";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
		
		for($i=0; $i < strlen($msrn_start); $i++)
		{
			if(substr($msrn_start,0,$i) != substr($msrn_end,0,$i))
				break;	
			$msrn_prefix = substr($msrn_start,0,$i);
		}
		if($DEBUG) echo "msrn_prefix=$msrn_prefix<br>";
	}
	
	//update msc-billing MSCID
	if($allFlag != 1)
	{
		$bill_market = omc_keep_length(dechex($bill_market),'0','4','1');
		$bill_switch = omc_keep_length(dechex($bill_switch),'0','2','1');
		$bill_init = setBitValue($bill_org,'0.0-1.7',$bill_market);
		$bill_init = setBitValue($bill_init,'2.0-2.7',$bill_switch);
		$setsql = "update $msc_paramTable set initValue = '$bill_init' 
		where level_1 = '2' and level_2 = '6' and level_3 = '5' and instanceNo = '$ra_no'";
		if($DEBUG) echo "setsql = $setsql<br>";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
	}
	
	//update msc-prefix and prefix attribute
	if($sub_prefix != '' || $msrn_prefix != '')
	{			
		if(!isset($num_length))
			$num_length = 0;
		//$num_length = omc_keep_length($num_length,'0','2','1');
		$ra_no = omc_keep_length(dechex($ra_no),'0','2');
		$prefix_loc = array('0.0-7.7','8.0-8.7');
		$prefix_arr = array(
			array(omc_keep_length($sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($NPrefix.$ndc.$sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($IPrefix.$cc.$ndc.$sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($ndc.$sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($cc.$ndc.$sub_prefix,'E','16','0'),$ra_no),	
			array(omc_keep_length($ndc.$sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($cc.$ndc.$sub_prefix,'E','16','0'),$ra_no),	
			array(omc_keep_length($ndc.$sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($ndc.$sub_prefix,'E','16','0'),$ra_no),
			array(omc_keep_length($cc.$ndc.$sub_prefix,'E','16','0'),$ra_no),			
			array(omc_keep_length($cc.$ndc.$msrn_prefix,'E','16','0'),$ra_no)
		);
		
		$pre_attr_loc = array('0.0-0.3','0.4-0.7','10.0-10.0','10.1-10.1','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','18.0-18.0','18.4-18.7');
		$pre_attr_arr = array(
		//array('TON incoming call','TON outgoing call','CDR','TLDN/Prefix Flag','Properties','Call type','Enable','Length of dialed number','deletion count',
		//		'insertion count','inserted digits','Cli-type','annoucement','MAP TON outgoing');
			//prefix	--Unkown
			array('0010','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length),'0','2'),'0',
				decbin(strlen($cc.$ndc)),omc_keep_length($cc.$ndc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//prefix	--MSISDN
			array('0001','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length),'0','2'),'0',
				decbin(strlen($cc.$ndc)),omc_keep_length($cc.$ndc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//prefix	--IDD
			array('0011','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length),'0','2'),'0',
				decbin(strlen($cc.$ndc)),omc_keep_length($cc.$ndc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//prefix	--NDD
			array('0100','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length),'0','2'),'0',
				decbin(strlen($cc.$ndc)),omc_keep_length($cc.$ndc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//NP+NDC+Prefix	--Unkown
			array('0010','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($NPrefix.$ndc)),'0','2'),decbin(strlen($NPrefix)),
				decbin(strlen($cc)),omc_keep_length($cc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//IP+CC+NDC+Prefix	--Unkown
			array('0010','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($IPrefix.$cc.$ndc)),'0','2'),decbin(strlen($IPrefix)),
				'000',omc_keep_length(0,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//NDC+Prefix -- Unknown			
			array('0010','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($ndc)),'0','2'),'0',
				decbin(strlen($cc)),omc_keep_length($cc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//CC+NDC+Prefix	-- Unknown	
			array('0010','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($cc.$ndc)),'0','2'),'0',
				'000',omc_keep_length(0,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),		
			//NDC+Prefix -- MSISDN			
			array('0001','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($ndc)),'0','2'),'0',
				decbin(strlen($cc)),omc_keep_length($cc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//CC+NDC+Prefix	-- MSISDN	
			array('0001','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($cc.$ndc)),'0','2'),'0',
				'000',omc_keep_length(0,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),					
			//NDC+Prefix -- NDD			
			array('0011','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($ndc)),'0','2'),'0',
				decbin(strlen($cc)),omc_keep_length($cc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//NDC+Prefix -- IDD			
			array('0100','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($ndc)),'0','2'),'0',
				decbin(strlen($cc)),omc_keep_length($cc,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),
			//CC+NDC+Prefix	-- IDD		
			array('0100','0100',$cdr,'0','01','000','1',omc_keep_length(dechex($num_length+strlen($cc.$ndc)),'0','2'),'0',
				'000',omc_keep_length(0,'E','8','0'),omc_keep_length($cli_type,'0','2'),omc_keep_length($VoicePrompt,'0','1'),omc_keep_length(decbin($MAP_type),'0','4')),			
			//MSRN_Prefix
			array('0100','0100','0','1','10','000','1',omc_keep_length(strlen($cc.$ndc.$msrn_start),'0','2'),'0',
				'000',omc_keep_length(0,'E','8','0'),'02','0',omc_keep_length(decbin($MAP_type),'0','4'))
		);
		
		//needn't add TLDN to prefix table when adding ALL prefix 
		if($allFlag == 1)
		{
			$start_loc = 0;
			$arr_size = sizeof($prefix_arr) - 1;
		}
		else  
		{
			if($sub_prefix == '')
			{
				$start_loc = 6;
			}else
			{
				$start_loc = 0;
			}
			$arr_size = sizeof($prefix_arr);
		}
		$index = 0;
		for($i=$start_loc; $i < $arr_size; $i++)
		{
			$prefix_init = $subprefix_org;
			for($j=0; $j < sizeof($prefix_loc); $j++)
				$prefix_init = setBitValue($prefix_init,$prefix_loc[$j],$prefix_arr[$i][$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[$i][$j]);
			}
			
			if($DEBUG) 
			{
				echo "prefix_init = $prefix_init<br>";
				echo "prefix_attr_init = $prefix_attr_init<br>";
			}
			
			if($i == 6 && isset($msrn_prefix_insNo))
				$prefix_insNo = $msrn_prefix_insNo;
			else
				$prefix_insNo = $curr_sub_ins[$index];
				
			$setsql = "update $msc_paramTable set initValue = '$prefix_init' 
			where level_1 = '2' and level_2 = '1' and level_3 = '1' and instanceNo = '$prefix_insNo'";
			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 = '$prefix_insNo'";
			if($DEBUG) echo "setsql = $setsql<br>";
			mysqli_query($pubConn,$setsql);
			echo mysqli_error($pubConn);
			
			$index++;
		}

//		//insert two prefix to XAPP
//		$tmpSQL = "SELECT $net_paramTable.instanceNo FROM $net_paramTable,$net_paramConf WHERE $net_paramTable.initValue = $net_paramConf.initValue
//			AND $net_paramTable.level_1 = '2' AND $net_paramTable.level_2 = '8' AND $net_paramTable.level_1 = $net_paramConf.level_1
//			AND $net_paramTable.level_2 = $net_paramConf.level_2";
//		if($DEBUG) echo "tmpSQL = $tmpSQL<br>";
//		$xapp_rst = @mysql_db_query($netdb,$tmpSQL);
//		echo mysqli_error($pubConn);		
//		if(mysqli_num_rows($xapp_rst) < 2)
//		{
//			echo "<script LANGUAGE=\"JavaScript\">";
//			echo "alert('Parameter table is full!')";
//			echo "\n </script>";
//		} 		
//		else
//		{
//			$k = 0;
//			while($xapp_row = mysqli_fetch_array($xapp_rst))
//			{
//				$xapp_prefix_ins[$k++] = $xapp_row[0];
//				if($k >= 2)
//					break;
//			}
//	
//			$xapp_prefix_loc = array('00.0-15.7','16.0-16.7','17.0-17.7','18.0-33.7','34.0-34.7');
//			$xapp_prefix = array(
//				array(omc_keep_length(toHex($sub_prefix),'0','32','0'),'00',
//					omc_keep_length(dechex(strlen($cc.$ndc)),'0','2','1'),omc_keep_length(toHex($cc.$ndc),'0','32','0'),'00'),
//				array(omc_keep_length(toHex($ndc.$sub_prefix),'0','32','0'),'00',
//					omc_keep_length(dechex(strlen($cc)),'0','2','1'),omc_keep_length(toHex($cc),'0','32','0'),'00'),
//			);
//			
//			for($k=0; $k < sizeof($xapp_prefix); $k++)
//			{	
//				$initValue = omc_keep_length($initValue,'0','70');
//				for($j=0; $j < sizeof($xapp_prefix[$k]); $j++)
//					$initValue =  setBitValue($initValue,$xapp_prefix_loc[$j],$xapp_prefix[$k][$j]);
//					
//				echo "<br>initValue = $initValue";
//				
//				$setsql = "update $net_paramTable set initValue = '$initValue' 
//					where level_1 = '2' and level_2 = '8' and instanceNo = '$xapp_prefix_ins[$k]'";
//			//	if($DEBUG) 
//				echo "setsql = $setsql<br>";
//				mysql_db_query($netdb,$setsql);
//				echo mysqli_error($pubConn);
//			}
//			exit();
//		}
	}

	echo "<script LANGUAGE=\"JavaScript\">";
	if(!$DEBUG)
	{
		echo "\n window.location.href=\"$listPageUrl\";";
	} 
	echo "\n </script>";
}

if($DEBUG)
{
	echo "<br>delFlag = $delFlag<br>";
	echo "delValue = $delValue<br>";	
}
if($delFlag == 1 && $delValue != '')
{
	$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>";
	$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($sub_ra == $delValue)
		{
			$objWhereStr = getLevelWhere('2.1.1');	
			$setsql = "update $msc_paramTable set initValue = '$subprefix_org' where $objWhereStr and instanceNo='$i'";		
			mysqli_query($pubConn,$setsql);
			echo mysqli_error($pubConn);
			if($DEBUG) echo "<br>$setsql<br>";
			
			$objWhereStr = getLevelWhere('2.1.2');	
			$setsql = "update $msc_paramTable set initValue = '$subpre_attr_org' where $objWhereStr and instanceNo='$i'";		
			mysqli_query($pubConn,$setsql);
			echo mysqli_error($pubConn);
			if($DEBUG) echo "<br>$setsql<br>";
		}			
		$i++;
	}
	
	//back to msc
	echo "<script LANGUAGE=\"JavaScript\">";
	if(!$DEBUG)
	{
		echo "\n window.location.href=\"$listPageUrl\";";
	} 
	echo "\n </script>";
}

$i = 0;
$free_no = 0;
$all_free_no = 0;
while($i < 100)
{
	$msrn_row = @mysqli_fetch_array($msrn_rst);
	$msrn_row = $msrn_row[0];

		$bill_mscid_row = @mysqli_fetch_array($bill_mscid_rst);
		$bill_mscid_row = $bill_mscid_row[0];
		
		$sub_prefix_row = '';
		$sub_tmp = 0;
		while($sub_tmp < $subprefix_no)
		{
			$sub_row_tmp = $sub_prefix_tmp[$sub_tmp];
			$sub_ra = hexdec(getBitValue($sub_row_tmp,'8.0-8.7'));
			if($DEBUG) echo "<br>$i = $sub_ra<br>msrn_prefix_flag[$sub_tmp]=$msrn_prefix_flag[$sub_tmp]<br>";
			if($msrn_prefix_flag[$sub_tmp]==0 && $i == $sub_ra)
			{
				$sub_prefix_row = $sub_row_tmp;
				$prefix_attr_row = $prefix_attr_tmp[$sub_tmp];
				break;
			}
			$sub_tmp++;
		}						
		
		if($msrn_row == $msrn_org 
		 &&$bill_mscid_row == $bill_org 
		 &&$sub_prefix_row == '')
		{
			$ra_unused[$free_no] = $i;	
			//if($DEBUG) echo "ra_unused = $ra_unused[$free_no]<br>";
			$free_no++;
		}
		else if($i >= 50 && $sub_prefix_row == '')
		{
			$all_unused[$all_free_no] = $i;	
			//if($DEBUG) echo "all_unused = $all_unused[$all_free_no]<br>";
			$all_free_no++;
		}
		else if($i == $instanceNo && $instanceNo != 'new')
		{
			//Get TLDN
			if($DEBUG) echo "<br>msrn_row = $msrn_row<br>";
			$msrn_start = getBitValue($msrn_row,'00.0-07.7');
			$msrn_end = getBitValue($msrn_row,'08.0-15.7');
			$msrn_start = str_replace('F','',$msrn_start);
			$msrn_end = str_replace('F','',$msrn_end);
			$prefix_length = strlen($cc.$ndc);
			$tmp_len = strlen($msrn_start) - $prefix_length;
			$msrn_start = substr($msrn_start,$prefix_length,$tmp_len);		
			$tmp_len = strlen($msrn_end) - $prefix_length;
			$msrn_end = substr($msrn_end,$prefix_length,$tmp_len);		
			if($DEBUG) 
			{
				echo "msrn_start = $msrn_start<br>";
				echo "msrn_end = $msrn_end<br>";
			}
			
			//Get Subscriber prefix
			if($sub_prefix_row != '')
			{
				if($DEBUG) echo "<br>sub_prefix_row = $sub_prefix_row<br>";
				$sub_prefix  = getBitValue($sub_prefix_row,'0.0-7.7');
				$sub_prefix  = str_replace('E','',$sub_prefix );
				if($DEBUG) echo "sub_prefix = $sub_prefix <br>";
				
				//Get prefix attribute
				if($DEBUG) echo "<br>prefix_attr_row = $prefix_attr_row<br>";
				$num_length = hexdec(getBitValue($prefix_attr_row,'11.0-11.7'));
				$cdr_flag = getBitValue($prefix_attr_row,'10.0-10.0');
				$MAP_type = bindec(getBitValue($prefix_attr_row,'18.4-18.7'));								
				$VoicePrompt = getBitValue($prefix_attr_row,'18.0-18.0');
				$cli_type = getBitValue($prefix_attr_row,'17.0-17.7');
				
				//Get Billing MSCID		
				if($DEBUG) echo "<br>msrn_row = $msrn_row<br>";
				$bill_market_id  = hexdec(getBitValue($bill_mscid_row,'0.0-1.7'));
				$bill_switch_num  = hexdec(getBitValue($bill_mscid_row,'2.0-2.7'));
			}
		}
	$i++;
}
if($DEBUG) echo "free_no = $free_no<br>";
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>";

echo "<table width=\"100%\">";
echo "<tr><td>";
echo "<b>Routing area:&nbsp;&nbsp;&nbsp;&nbsp;</b>";
if($instanceNo == 'new')
{
	echo "<select name=\"ra_ins\" onchange=JavaScript:checkSelect(this,'$ra_ins')>";
	for($i=0; $i < $free_no; $i++)
	{
		if($ra_ins == $ra_unused[$i])
			echo "<option value=\"$ra_unused[$i]\" selected>$ra_unused[$i]";
		else	
			echo "<option value=\"$ra_unused[$i]\">$ra_unused[$i]";
	}
	if($ra_ins == "all" || $free_no == 0)
	{
		$allFlag = 1;
		$ra_ins = $all_unused[0];
		echo "<option value=\"all\" selected>all";
	}
	else echo "<option value=\"all\">all";
	
	echo "</select>&nbsp;&nbsp;&nbsp;&nbsp;";
	
	if($DEBUG) echo "ra_ins = $ra_ins<br>";
	if($DEBUG) echo "all_unused = $all_unused[0]<br>";
}
else if($allFlag == 1)
{
	echo "ALL&nbsp;&nbsp;&nbsp;&nbsp;";		
}
else
{
	echo "$instanceNo&nbsp;&nbsp;&nbsp;&nbsp;";	
}
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>";
if($allFlag == 1 && $instanceNo != 'new')
	echo "<td><a href='$PHP_SELF?delFlag=1&delValue=$instanceNo'><b>del</b></a></td>";
echo "<td width=15%><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
//Title
{
	echo "<tr bgcolor = \"#E6E6E6\">";
	echo "<td width=\"50%\"><b>Parameters</b></td>";
	echo "<td width=\"50%\"><b>Value</b></td>";
	echo "</tr>";
}
//TLDN
if($allFlag != 1)
{
	$prefix = $cc.$ndc;
	$len = 16 - strlen($prefix);
	echo "<tr>";
	echo "<td width=50%>Start TLDN</td>";
	echo "<td>$prefix<input type=\"text\" size = \"16\" maxlength=\"$len\" value=\"$msrn_start\" class=\"text\" 
			name=\"msrn_start\" onchange = checkInput(this,10,0)></td>";
	echo "</tr><tr>";
	echo "<td width=50%>End TLDN</td>";
	echo "<td>$prefix<input type=\"text\" size = \"16\" maxlength=\"$len\" value=\"$msrn_end\" class=\"text\" 
			name=\"msrn_end\" onchange = checkInput(this,10,0)></td>";
	echo "<tr>";
}
//prefix attribute
{
	echo "<tr>";
	echo "<td>Subscriber prefix</td>";
	echo "<td><input type=\"text\" size = \"16\" maxlength=\"16\" value=\"$sub_prefix\" class=\"text\" 
			name=\"sub_prefix\" onchange = checkInput(this,10,0)></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td>Number length</td>";
	echo "<td><input type=\"text\" size = \"16\" maxlength=\"3\" value=\"$num_length\" class=\"text\" 
			name=\"num_length\" onchange = checkInput(this,10,255)></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td>CDR</td>";
	echo "<td><select name=\"cdr\" style=width:\"110\">";
	if($cdr_flag == '1')
	{
		echo "<option value=1 selected >ON";
		echo "<option value=0>OFF";
	}
	else
	{
		echo "<option value=0 selected>OFF";
		echo "<option value=1>ON";
	}
	echo "</td></tr>";
	
	$MAP_type_arr = array('','Sub'	,'Unknown','NDD','IDD');
	echo "<tr>";
	echo "<td>MAP TON outgoing</td>";
	echo "<td><select name=\"MAP_type\" style=width:\"110\">";
	for($i=1;$i< sizeof($MAP_type_arr);$i++){
		if($i==$MAP_type)
			echo "<option value=\"$i\" selected >$MAP_type_arr[$i]";
		else 
			echo "<option value=\"$i\" >$MAP_type_arr[$i]";
	}
	echo "</td></tr>";	
	
	echo "<tr>";
	echo "<td>Voice Prompt</td>";
	echo "<td><select name=\"VoicePrompt\" style=width:\"110\">";
	if($VoicePrompt == '1')
	{
		echo "<option value=1 selected>ON";
		echo "<option value=0>OFF";
	}
	else
	{
		echo "<option value=0 selected>OFF";
		echo "<option value=1>ON";
	}
	echo "</td></tr>";

	echo "<tr>";
	echo "<td>CLI &nbsp; type</td>";
	echo "<td><select name=\"cli_type\" style=width:\"110\">";
	switch($cli_type)
	{
		case 0:
		{
			echo "<option value=0 selected>Sub";
			echo "<option value=1>NDD";
			echo "<option value=2>IDD";
			echo "<option value=3>Unknown(local)";
			echo "<option value=4>Unknown(NDD)";
			echo "<option value=5>Unknown(IDD)";
		}
		break;
		case 1:
		{			
			echo "<option value=0>Sub";
			echo "<option value=1 selected>NDD";
			echo "<option value=2>IDD";
			echo "<option value=3>Unknown(local)";
			echo "<option value=4>Unknown(NDD)";
			echo "<option value=5>Unknown(IDD)";
		}
		break;
		case 2:
		{
			echo "<option value=0>Sub";
			echo "<option value=1>NDD";
			echo "<option value=2 selected>IDD";
			echo "<option value=3>Unknown(local)";
			echo "<option value=4>Unknown(NDD)";
			echo "<option value=5>Unknown(IDD)";
		}
		break;
		case 3:
		{			
			echo "<option value=0>Sub";
			echo "<option value=1>NDD";
			echo "<option value=2>IDD";
			echo "<option value=3 selected>Unknown(local)";
			echo "<option value=4>Unknown(NDD)";
			echo "<option value=5>Unknown(IDD)";
		}
		break;
		case 4:
		{
			echo "<option value=0>Sub";
			echo "<option value=1>NDD";
			echo "<option value=2>IDD";
			echo "<option value=3>Unknown(local)";
			echo "<option value=4 selected>Unknown(NDD)";
			echo "<option value=5>Unknown(IDD)";
		}
		break;
		case 5:
		{			
			echo "<option value=0>Sub";
			echo "<option value=1>NDD";
			echo "<option value=2>IDD";
			echo "<option value=3>Unknown(local)";
			echo "<option value=4>Unknown(NDD)";
			echo "<option value=5 selected>Unknown(IDD)";
		}
		break;
	}
	echo "</td></tr>";
}
//Billing MSCID
if($allFlag != 1)
{
	echo "<tr>";
	echo "<td>Billing MSCID</td>";
	echo "<td>Billing&nbsp;market&nbsp;ID:<input type=\"text\" size = \"16\" maxlength=\"6\" value=\"$bill_market_id\" class=\"text\" 
			name=\"bill_market\" onchange = checkInput(this,10,65535)>&nbsp;&nbsp;&nbsp;&nbsp;
				Billing&nbsp;switch&nbsp;number:<input type=\"text\" size = \"16\" maxlength=\"3\" value=\"$bill_switch_num\" class=\"text\" 
			name=\"bill_switch\" onchange = checkInput(this,10,255)></td>";
	echo "</tr>";
}
echo "<input type=\"hidden\" name=\"instanceNo\" value=\"$instanceNo\">";
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\">";
if($ra_ins >= 50)
{
	echo "<input type=\"hidden\" name=\"allFlag\" value=\"$allFlag\">";
	echo "<input type=\"hidden\" name=\"all_ins_no\" value=\"$ra_ins\">";
}
echo "</table>";

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