<?php
require("../sub_inc/commfunc.php");
$DEBUG = 0;
if($DEBUG) echo "<br>正在调试中！！";
?>
<script LANGUAGE="JavaScript">       
function delFirm(flag,No)
{
	var delValue,x,y;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;
    
	if(confirm('Do you want to delete the selected?') != true)
		window.location.href='<?php echo $PHP_SELF;?>';
	else
		window.location.href='<?php echo $PHP_SELF;?>?&delValue='+No+'&delflag='+flag;
}
</script>
<body topmargin="0" leftmargin="12">

<?php

$helpID = "1.1.1";

$nProcFail = 0;//failure
$nWaitProc = 1;//wait
$nInProc   = 2;
$nOutProc  = 3;

$nGetComm = 0;
$nSetComm = 3;
	
$maxNum = 120;

$mg_sqltable = 'OBJ_322.param_99';
$mg_oid = '2.7.1';
$sg_sqltable = 'OBJ_222.param_99';
$sg_oid = '2.9.0';
$locTable = 'sysLocConf';
$pubdb = 'OMC_PUB';

//get location name information
$getsql = "SELECT * FROM $locTable order by keyId";
if($DEBUG) echo "getsql = $getsql<br>";
$result = @mysqli_query($pubConn,$getsql);
$loc_num = 0;
while($row = @mysqli_fetch_array($result))
{
	$loc_arr[$loc_num][0] = $row[keyId];
	$loc_arr[$loc_num][1] = $row[Location_name];
	
	if($DEBUG)
	{
		echo "loc_arr[$loc_num][0] = {$loc_arr[$loc_num][0]}<br>";
		echo "loc_arr[$loc_num][1] = {$loc_arr[$loc_num][1]}<br>";
	}
	$loc_num++;
}

if($_SERVER['REQUEST_METHOD'] == "POST")
{
	if($DEBUG) echo "mg_num = $mg_num<br>";
	for($i=0;$i < $mg_num;$i++)
	{
		$ip = '';
		$ipname =256;
		for($j=0; $j < 4; $j++)
		{
			$tmp = "mg_".$i."_ip".$j;
			$tmp = ${$tmp};
			
			if($ipname==256)
				$ipname =$tmp;
			else
				$ipname =$ipname.'.'.$tmp;
			
			$tmp = dechex($tmp);
			$tmp = omc_keep_length($tmp,'0','2');
			if($DEBUG) echo "mg_$i_$j = $tmp<br>";
			$ip .= $tmp;
		}
		if($DEBUG) echo "mg_ip = $ip<br>";
		$initValue = '';
		$initValue = omc_keep_length($initValue,'0','106');
  		$initValue = setBitValue($initValue,'0.0-0.0','1');
	  	$initValue = setBitValue($initValue,'0.4-0.7',1);
		$initValue = setBitValue($initValue,'1.0-4.7',$ip);
		if($DEBUG) echo "initValue = $initValue<br>";

		$tmp = toHex($ipname);
		$tmp = omc_keep_length($tmp,'0','64','0');
		$initValue = setBitValue($initValue,'5.0-36.7',$tmp);

		$tmp = 'mg_unit_num_'.$i;
		$tmp = toHex($loc_arr[${$tmp}][1]);		
		$tmp = omc_keep_length($tmp,'0','20','0');
		$initValue = setBitValue($initValue,'37.0-46.7',$tmp);
		
		$tmp = 'mg_unit_num_'.$i;		
		$tmp = omc_keep_length(decHex($showNum_to_unitNum[${$tmp}]),"0","2");
		$initValue = setBitValue($initValue,'51.0-51.7',$tmp);
		
		$tmp = 'mg_dev_num_'.$i;		
		$tmp = omc_keep_length(decHex(${$tmp}),"0","2");
		$initValue = setBitValue($initValue,'52.0-52.7',$tmp);
		
		if($DEBUG) echo "initValue = $initValue<br>";
		$levelArr=explode('.',$mg_oid);
		for($j=0;$j<sizeof($levelArr);$j++)
		{
			$levelNo=$j+1;
			${"level_$levelNo"}=$levelArr[$j];
		}
		$initValue = strtoupper($initValue);
		$setsql = "update $mg_sqltable set initValue = '$initValue' 
				where level_1 = '$level_1' and level_2 = '$level_2' and level_3 = '$level_3' and instanceNo = '${mg_ins_.$i}'";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
		if($DEBUG) echo "$setsql<br>";
	}

	if($DEBUG) echo "sg_num = $sg_num<br>";
	for($i=0;$i < $sg_num;$i++)
	{
		$ip = '';
		for($j=0; $j<4; $j++)
		{
			$tmp = "sg_".$i."_ip".$j;
			$tmp = ${$tmp};
			$tmp = dechex($tmp);
			$tmp = omc_keep_length($tmp,'0','2');
			if($DEBUG) echo "sg_$i_ip$j = $tmp<br>";
			$ip .= $tmp;
		}
		if($DEBUG) echo "sg_ip = $ip<br>";
		$initValue = '';
		$initValue = omc_keep_length($initValue,'0','14');
		$sg_mode_val = omc_keep_length(${'sg_mode_'.$i},'0','2');
		$sg_ack_val = omc_keep_length(${'sg_ack_'.$i},'0','2');
  		$initValue = setBitValue($initValue,'00.0-00.7','01');
	  	$initValue = setBitValue($initValue,'01.0-01.7',$sg_mode_val);
		$initValue = setBitValue($initValue,'02.0-02.7',$sg_ack_val);
		$initValue = setBitValue($initValue,'03.0-06.7',$ip);
		if($DEBUG) echo "initValue = $initValue<br>";
		$levelArr=explode('.',$sg_oid);
		for($j=0;$j<sizeof($levelArr);$j++)
		{
			$levelNo=$j+1;
			${"level_$levelNo"}=$levelArr[$j];
		}
		$initValue = strtoupper($initValue);
		$setsql = "update $sg_sqltable set initValue = '$initValue' 
				where level_1 = '$level_1' and level_2 = '$level_2' and level_3 = '$level_3' and instanceNo = '${sg_ins_.$i}'";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
		if($DEBUG) echo "$setsql<br>";
	}
	
	if(isset($mg_new) || isset($sg_new))
	{
			if($DEBUG)
			{
				echo "mg_unused = $mg_ins_no<br>";	
				echo "sg_unused = $sg_ins_no<br>";
			}
			if(isset($mg_new) && $mg_num != 16)
			{
				$initValue = '';
				$initValue = omc_keep_length($initValue,'0','106');
		  	$initValue = setBitValue($initValue,'00.0-00.0','1');
		  	$initValue = setBitValue($initValue,'0.4-0.7',1);
				$tmp = toHex("0.0.0.0");
				$tmp = omc_keep_length($tmp,'0','64','0');
				$initValue = setBitValue($initValue,'5.0-36.7',$tmp);		  			  	
		  	$levelArr=explode('.',$mg_oid);
				for($j=0;$j<sizeof($levelArr);$j++)
				{
					$levelNo=$j+1;
					${"level_$levelNo"}=$levelArr[$j];
				}
				$initValue = strtoupper($initValue);
				$setsql = "update $mg_sqltable set initValue = '$initValue' 
						where level_1 = '$level_1' and level_2 = '$level_2' and level_3 = '$level_3' and instanceNo = '$mg_ins_no'";
				mysqli_query($pubConn,$setsql);
				echo mysqli_error($pubConn);
				if($DEBUG) echo "$setsql<br>";
			}
			else if(isset($sg_new) && $sg_num != 256)
			{
				$initValue = '';
				$initValue = omc_keep_length($initValue,'0','14');
		  	$initValue = setBitValue($initValue,'00.0-00.7','01');
		  	$levelArr=explode('.',$sg_oid);
				for($j=0;$j<sizeof($levelArr);$j++)
				{
					$levelNo=$j+1;
					${"level_$levelNo"}=$levelArr[$j];
				}
				$initValue = strtoupper($initValue);
				$setsql = "update $sg_sqltable set initValue = '$initValue' 
						where level_1 = '$level_1' and level_2 = '$level_2' and level_3 = '$level_3' and instanceNo = '$sg_ins_no'";
				mysqli_query($pubConn,$setsql);
				echo mysqli_error($pubConn);
				if($DEBUG) echo "$setsql<br>";
			}
	}
	else if(!$DEBUG)
	{
		echo "<script LANGUAGE=\"JavaScript\">";
		echo "\n window.location.href=\"./msc.php\";";
		echo "\n </script>";
	}	
}	

if(isset($delflag))
{
	if($delflag == 'mg')
	{
		if($DEBUG) echo "delValue = $delValue<br>";
		$initValue = '';
		$initValue = omc_keep_length($initValue,'0','106');
	 	$initValue = setBitValue($initValue,'00.0-00.0','0');
	 	$levelArr=explode('.',$mg_oid);
		for($j=0;$j<sizeof($levelArr);$j++)
		{
			$levelNo=$j+1;
			${"level_$levelNo"}=$levelArr[$j];
		}
		
		$setsql = "update $mg_sqltable set initValue = '$initValue' 
				where level_1 = '$level_1' and level_2 = '$level_2' and level_3 = '$level_3' and instanceNo = '$delValue'";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
		if($DEBUG) echo "$setsql<br>";
	}	
	else
	{
		if($DEBUG) echo "delValue = $delValue<br>";
		$initValue = '';
		$initValue = omc_keep_length($initValue,'0','14');
	 	$initValue = setBitValue($initValue,'00.0-00.7','00');
	 	$levelArr=explode('.',$sg_oid);
		for($j=0;$j<sizeof($levelArr);$j++)
		{
			$levelNo=$j+1;
			${"level_$levelNo"}=$levelArr[$j];
		}
		
		$setsql = "update $sg_sqltable set initValue = '$initValue' 
				where level_1 = '$level_1' and level_2 = '$level_2' and level_3 = '$level_3' and instanceNo = '$delValue'";
		mysqli_query($pubConn,$setsql);
		echo mysqli_error($pubConn);
		if($DEBUG) echo "$setsql<br>";
	}
}
//==========================================================
//取数据库数据
//========================================================== 
$levelArr=explode('.',$mg_oid);
for($j=0;$j<sizeof($levelArr);$j++)
{
	$levelNo=$j+1;
	${"level_$levelNo"}=$levelArr[$j];
	if($DEBUG) echo "<BR>levelArr[$j]={$levelArr[$j]}";
}
$getsql = "SELECT initValue FROM $mg_sqltable
where level_1='$level_1' and level_2 = '$level_2' and level_3 = '$level_3'";
if($DEBUG) echo "getsql = $getsql<br>";
$mg_result = @mysqli_query($pubConn,$getsql);
$i = 0;
$mg_num = 0;
while($mg_row = @mysqli_fetch_array($mg_result))
{
	$mg_row = $mg_row[0];
	$enable_flag = getBitValue($mg_row,'0.0-0.0');	
	$mg_type = getBitValue($mg_row,'0.4-0.7');
	//echo "mg_row $i = $mg_row<br>";
	if($enable_flag == 1 && $mg_type == 1)
	{
		$mg_name[$mg_num] = trim(toascii(getBitValue($mg_row,'5.0-36.7')));
		$mg_ip_tmp = getBitValue($mg_row,'1.0-4.7');	
		$mg_unit_num[$mg_num]=$tmpnum=hexdec(getBitValue($mg_row,'51.0-51.7'));	
		$mg_dev_num[$mg_num]=$tmpnum=hexdec(getBitValue($mg_row,'52.0-52.7'));	
		$unitnumflag[$tmpnum]=1;
		$mg_ip[$mg_num][0] = hexdec(getBitValue($mg_ip_tmp,'0.0-0.7'));
		$mg_ip[$mg_num][1] = hexdec(getBitValue($mg_ip_tmp,'1.0-1.7'));
		$mg_ip[$mg_num][2] = hexdec(getBitValue($mg_ip_tmp,'2.0-2.7'));
		$mg_ip[$mg_num][3] = hexdec(getBitValue($mg_ip_tmp,'3.0-3.7'));
		$mg_ins[$mg_num] = $i;
		if($DEBUG)
		{
			echo "mg_unit_num = $mg_unit_num[$mg_num]<br>";	
			$index = $mg_dev_num[$mg_num];
			echo "mg_dev_num = $unitNum_to_showNum[$index]<br>";	
			echo "mg_ins = $mg_ins[$mg_num]<br>";	
			echo "mg_name = $mg_name[$mg_num]<br>";	
			echo "mg_ip = $mg_ip_tmp<br>";	
		}
		$mg_num++;
	}
	else if($enable_flag == 0 && !isset($mg_unuesd))
		$mg_unuesd = $i;
	$i++;
}

$levelArr=explode('.',$sg_oid);
for($j=0;$j<sizeof($levelArr);$j++)
{
	$levelNo=$j+1;
	${"level_$levelNo"}=$levelArr[$j];
	if($DEBUG) echo "<BR>levelArr[$j]={$levelArr[$j]}";
}
$getsql = "SELECT initValue FROM $sg_sqltable
where level_1='$level_1' and level_2 = '$level_2' and level_3 = '$level_3'";
if($DEBUG) echo "getsql = $getsql<br>";
$sg_result = @mysqli_query($pubConn,$getsql);

$k = 0;
$sg_num = 0;
while($sg_row = @mysqli_fetch_array($sg_result))
{
	$sg_row = $sg_row[0];
	$enable_flag = getBitValue($sg_row,'0.0-0.7');	
	if($enable_flag == 1)
	{
		$sg_mode[$sg_num] = getBitValue($sg_row,'1.0-1.7');
		$sg_ack[$sg_num] = getBitValue($sg_row,'02.0-02.7');
		$sg_ip_tmp = getBitValue($sg_row,'03.0-06.7');	
		$sg_ip[$sg_num][0] = hexdec(getBitValue($sg_ip_tmp,'0.0-0.7'));
		$sg_ip[$sg_num][1] = hexdec(getBitValue($sg_ip_tmp,'1.0-1.7'));
		$sg_ip[$sg_num][2] = hexdec(getBitValue($sg_ip_tmp,'2.0-2.7'));
		$sg_ip[$sg_num][3] = hexdec(getBitValue($sg_ip_tmp,'3.0-3.7'));
		$sg_ins[$sg_num] = $k;
		if($DEBUG)
		{
			echo "sg_row = $sg_row<br>";
			echo "sg_ins = $sg_ins[$sg_num]<br>";	
			echo "sg_mode = $sg_mode[$sg_num]<br>";	
			echo "sg_ip = $sg_ip_tmp<br>";	
			echo "sg_ack = $sg_ack[$sg_num]<br>";	
		}
		$sg_num++;
	}
	else if(!isset($sg_unuesd))
		$sg_unuesd = $k;
	$k++;
}
//==========================================================
//输出并显示数据
//==========================================================
echo "<form method=\"POST\" name=\"Set\" action=\"$PHP_SELF\">";
echo "<input type=\"hidden\" name=\"mg_num\" value=\"$mg_num\">";
echo "<input type=\"hidden\" name=\"sg_num\" value=\"$sg_num\">";
echo "<input type=\"hidden\" name=\"mg_ins_no\" value=\"$mg_unuesd\">";
echo "<input type=\"hidden\" name=\"sg_ins_no\" value=\"$sg_unuesd\">";

echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "Configuration > Network > MSC > MG ";
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=80%>";
echo "<b>Media Gateway:&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 width=10% align=left><input type=\"submit\" name=\"mg_new\" value=\"Add\" 
	onmouseover=\"this.className='style8'\" onmouseout=\"this.className='style7'\" class=\"style7\"></td>";
echo "<td 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="1" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF"> 
<?php

echo "<tr>";
echo "<td bgcolor=\"#E6E6E6\"><table width=100%><tr>";
echo "<td width=10% ><b>No.</b></td>";
echo "<td width=30% ><b>Cluster</b></td>";
echo "<td width=30% ><b>IP</b></td>";
echo "<td width=20% ><b>Device Number</b></td>";
echo "<td align=right ><b>Delete</b></td>";
echo "</tr>";
echo "</table></td></tr>";
for($i=0;$i < $mg_num;$i++)
{
	echo "<tr>";
	echo "<td><table width=100%><tr>";
	echo "<td width=10%>$mg_ins[$i]</td>";
	echo "<td width=30%>";
	echo "<select name=\"mg_unit_num_$i\" size=\"1\" style=width:\"$nShowsize\">";
	for($ki=0; $ki < $loc_num; $ki++){
		if($loc_arr[$ki][0]==$unitNum_to_showNum[$mg_unit_num[$i]])
			echo "<option value={$loc_arr[$ki][0]} selected>{$loc_arr[$ki][1]}</option>";
		else //elseif(!$unitnumflag[$ki])
			echo "<option value={$loc_arr[$ki][0]}>{$loc_arr[$ki][1]}</option>";
	}
	echo "</select>";
	echo "</td>";	
	
	echo "<td width=30%>";
	for($j=0; $j < 4; $j++)
	{
		$name = 'mg_'.$i.'_ip'.$j;
		echo "<INPUT TYPE=\"text\" NAME=\"$name\" size=\"3\" maxlength=\"3\" 
			value=\"{$mg_ip[$i][$j]}\" class=\"text\" onchange = checkInput(this,10,255)>";
		if($j != 3)
			echo '.';
	}
	echo "</td>";
	
	echo "<td>";
	echo "<select name=\"mg_dev_num_$i\" size=\"1\" style=width:\"$nShowsize\">";
	if($mg_dev_num[$i] == 0)
	{
		echo "<option value=0 selected>0</option>";
		echo "<option value=1>1</option>";
	}
	else
	{
		echo "<option value=0>0</option>";
		echo "<option value=1 selected>1</option>";
	}
	echo "</select>";
	echo "</td>";	
	
	echo "<td align=right><input type=\"checkbox\" name=\"mg_del_$i\" value=\"\" onClick=\"javascript:delFirm('mg',$mg_ins[$i])\"></td>";
	echo "</table></td>";
	echo "</tr>";
	echo "<input type=\"hidden\" name=\"mg_ins_$i\" value=\"$mg_ins[$i]\">";
}
echo "</table>";

echo "<br>";
echo "<table width=100%><tr>";
echo "<td width=80% ><b>Signaling Gateway:&nbsp;&nbsp;</b></td>";
echo "<td align=left><input type=\"submit\" name=\"sg_new\" value=\"Add\" 
	onmouseover=\"this.className='style8'\" onmouseout=\"this.className='style7'\" class=\"style7\"></td>";
echo "</tr></table>";
?>
<table  width=100%   border="1"  cellpadding="1" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF"> 
<?php

echo "<tr>";
echo "<td bgcolor=\"#E6E6E6\"><table width=100%><tr>";
echo "<td width=10% ><b>No.</b></td>";
echo "<td width=30% ><b>Mode</b></td>";
echo "<td width=30% ><b>IP</b></td>";
echo "<td width=20% ><b>Signaling ACK</b></td>";
echo "<td align=right ><b>Delete</b></td>";
echo "</tr>";
echo "</table></td></tr>";
for($i=0;$i < $sg_num;$i++)
{
	echo "<tr>";
	echo "<td><table width=100%><tr>";
	echo "<td width=10%>$sg_ins[$i]</td>";
	echo "<td width=30%><select name = sg_mode_$i>";
	if ($sg_mode[$i] == 0)
	{
		echo "<option value = 0 selected>Client";
		echo "<option value = 1 >Server";
	}
	else
	{
		echo "<option value = 1 selected>Server";
		echo "<option value = 0 >Client";
	}
	echo "</td>";
	echo "<td width=30%>";
	for($j=0; $j < 4; $j++)
	{
		$name = 'sg_'.$i.'_ip'.$j;
		echo "<INPUT TYPE=\"text\" NAME=\"$name\" size=\"3\" maxlength=\"3\" 
			value=\"{$sg_ip[$i][$j]}\" class=\"text\" onchange = checkInput(this,10,255)>";
		if($j != 3)
			echo '.';
	}
	echo "</td>";
	echo "<td width=20%><select name = sg_ack_$i>";
	if ($sg_ack[$i] == 0)
	{
		echo "<option value = 0 selected>No";
		echo "<option value = 1 >Yes";
	}
	else
	{
		echo "<option value = 1 selected>Yes";
		echo "<option value = 0 >No";
	}
	echo "</td>";
	echo "<td align=right><input type=\"checkbox\" name=\"sg_del_$i\" value=\"\" onClick=\"javascript:delFirm('sg',$sg_ins[$i])\"></td>";
	echo "</table></td>";
	echo "</tr>";
	echo "<input type=\"hidden\" name=\"sg_ins_$i\" value=\"$sg_ins[$i]\">";
}
echo "</form>";
?>
</table>
