<?php
if($needGetParam){
	//send get command
	echo "<br>Loading parameters!<br>";
	$commStr='';
	for($i=0;$i<sizeof($getParamArr);$i++){
		$tmpCommStr=sysCommSend($getParamArr[$i][sysId],$nGetComm,$getParamArr[$i][oid]);
		if($DEBUG)echo "<br>tmpCommStr=$tmpCommStr";
		$commStr.=$tmpCommStr;
		if($DEBUG)echo "<br>commStr=$commStr";
	}
	
	//check the command status
	if($commStr==""){
		echo "<br><br><b>Send the command error, Please check the device state!<br>";
	}
		
	$cmmIdStrArr=explode("-",$commStr);
	
	for($i=0;$i<sizeof($cmmIdStrArr)-1;$i++){
		if($i==0)
			$tmpWhere="(commId='$cmmIdStrArr[$i]'";
		else
			$tmpWhere=$tmpWhere." OR commId='$cmmIdStrArr[$i]'";
	}
	$tmpWhere=$tmpWhere.")";	
	
				
	$i = 0;
	while(1){
		if($i > $maxNum){
			$tmpRemark='Timeout';			
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";
			mysqli_query($pubConn,$delComm);						
			
			echo "Error: $tmpRemark.</b>";				
			break;
		}
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();	
		
		$selComm = "SELECT commState,remark,objId,sysTypeNo FROM $sysCommTable WHERE $tmpWhere AND commState='$nProcFail'";
		if($DEBUG) echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		
		if(mysqli_num_rows($selRes)>0){
			$selRow  = mysqli_fetch_array($selRes);
			$tmpRemark=$selRow[remark];
			$tmpOid=$selRow[objId];
			$tmpSysTypeNo=$selRow[sysTypeNo];
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
			mysqli_query($pubConn,$delComm);
			
			for($j=0;$j<sizeof($getParamArr);$j++){
				if($getParamArr[$j][sysTypeNo]==$tmpSysTypeNo)
					echo "Error: $tmpRemark.({$getParamArr[$j][remark]})</b>";
			}
		}
		
		$selComm = "SELECT commState,remark FROM $sysCommTable WHERE $tmpWhere AND (commState='$nWaitProc' OR commState='$nInProc')";
	
		if($DEBUG)echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		if($DEBUG) $tmpNum=mysqli_num_rows($selRes);
		if($DEBUG) echo "<br>tmpNum=$tmpNum";
		
		if(mysqli_num_rows($selRes)>0){
			sleep(1);
			$i++;
			continue;
		}
		$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
		mysqli_query($pubConn,$delComm);
		echo "<br><font size=\"2\" color=\"#0000FF\">Process finished</font><br>";
		break;		
	}
	
	for($i=0;$i<sizeof($getParamArr);$i++){
		$levelWhere = getLevelWhere($getParamArr[$i][oid]);
		$moveSql = "UPDATE ".$getParamArr[$i][paramTable]." SET initValue = readValue WHERE ".$levelWhere;
		if($DEBUG) echo "<BR>moveSql=$moveSql";
		mysqli_query($pubConn,$moveSql);
		echo mysqli_error($pubConn);
	}
}

//XAPP Parameters
$selSql = "SELECT initValue FROM OBJ_225.param_99 
			WHERE level_1='2' AND level_2<'7' AND level_3='0' ORDER BY level_2 ASC";

if($DEBUG)
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);

$selRows=mysqli_fetch_array($selResult);
$tmpArr=explode("E",$selRows[initValue]);
$mcc=$tmpArr[0];

$selRows=mysqli_fetch_array($selResult);
$tmpArr=explode("E",$selRows[initValue]);
$mnc=$tmpArr[0];

$selRows=mysqli_fetch_array($selResult);
$tmpArr=explode("E",$selRows[initValue]);
$cc=$tmpArr[0];

$selRows=mysqli_fetch_array($selResult);
$tmpArr=explode("E",$selRows[initValue]);
$ndc=$tmpArr[0];

$selRows=mysqli_fetch_array($selResult);
$tmpArr=explode("E",$selRows[initValue]);
$internationalPrefix=$tmpArr[0];

$selRows=mysqli_fetch_array($selResult);
$tmpArr=explode("E",$selRows[initValue]);
$nationalPrefix=$tmpArr[0];

//MSRN
$selSql = "SELECT initValue FROM OBJ_325.param_99 
			WHERE level_1='2' AND level_2='1' AND level_3='13'";

if($DEBUG)
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);
$selRows=mysqli_fetch_array($selResult);
$msrnStart=getBitValue($selRows[initValue],"0.0-7.7");
$tmpArr=explode("F",$msrnStart);
$msrnStart=$tmpArr[0];

$msrnEnd=getBitValue($selRows[initValue],"8.0-15.7");
$tmpArr=explode("F",$msrnEnd);
$msrnEnd=$tmpArr[0];

//SN
$selSql = "SELECT initValue FROM OBJ_223.param_99 
			WHERE level_1='2' AND level_2='1' AND level_3='4'";

if($DEBUG)
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);
$selRows=mysqli_fetch_array($selResult);
$snStart=getBitValue($selRows[initValue],"1.0-10.7");
$tmpArr=explode("F",$snStart);
$snStart=$tmpArr[0];

$selSql = "SELECT initValue FROM OBJ_223.param_99 
			WHERE level_1='2' AND level_2='2' AND instanceNo='2'";

if($DEBUG)
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);
$selRows=mysqli_fetch_array($selResult);
$snEnd=getBitValue($selRows[initValue],"12.0-21.7");
$tmpArr=explode("F",$snEnd);
$snEnd=$tmpArr[0];
if(strlen($snEnd)<=0)
	$snEnd=$msrnEnd;

$selSql = "SELECT initValue FROM OBJ_223.param_99 
			WHERE level_1='2' AND level_2='2' AND instanceNo='1'";

if($DEBUG)
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);
$selRows=mysqli_fetch_array($selResult);

//check the nib configiration
$gtFlag=getBitValue($selRows[initValue],"0.0-0.7");
$gtNumber=getBitValue($selRows[initValue],"1.0-1.7");

if($gtFlag=0||$gtNumber!=1){
	$snStart='';
	$snEnd='';
}else{
	$tmpLength=strlen($cc)+strlen($ndc);
	$snStart=substr($snStart,$tmpLength);
	$snEnd=substr($snEnd,$tmpLength);
}

//MSIN
$selSql = "SELECT initValue FROM OBJ_223.param_99 
			WHERE level_1='2' AND level_2='2' AND instanceNo='0'";

if($DEBUG)
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);
$selRows=mysqli_fetch_array($selResult);
$msinStart=getBitValue($selRows[initValue],"2.0-11.7");
$tmpArr=explode("F",$msinStart);
$msinStart=$tmpArr[0];

$msinEnd=getBitValue($selRows[initValue],"12.0-21.7");
$tmpArr=explode("F",$msinEnd);
$msinEnd=$tmpArr[0];

//check the nib configiration
$gtFlag=getBitValue($selRows[initValue],"0.0-0.7");
$gtNumber=getBitValue($selRows[initValue],"1.0-1.7");

if($gtFlag==0||$gtNumber!=6){
	$msinStart='';
	$msinEnd='';
}else{
	$tmpLength=strlen($mcc)+strlen($mnc);
	$msinStart=substr($msinStart,$tmpLength);
	$msinEnd=substr($msinEnd,$tmpLength);

}


//PSTN Parameters
$selSql = "SELECT initValue FROM OBJ_322.param_99 
			WHERE level_1='2' AND level_2='1' AND level_3='4' AND instanceNo='0'";

if($DEBUG)
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);
$selRows=mysqli_fetch_array($selResult);
$tgEnableFlag=getBitValue($selRows[initValue],"4.0-4.0");
$tgCategory=getBitValue($selRows[initValue],"4.3-4.5");

if($tgEnableFlag==1&&$tgCategory==0)
{
	$dpc=getBitValue($selRows[initValue],"0.0-3.7");
	$dpc=$dpc-0;
	$niType=getBitValue($selRows[initValue],"5.0-5.7");
	$niLength=getBitValue($selRows[initValue],"12.0-12.7");
	$ni=$niLength*2+$niType/2;
	
	$levelWhere = getLevelWhere($NetOPCArr[$ni][0]);
	$tmpSql="SELECT initValue FROM OBJ_222.param_99 WHERE $levelWhere";
	if($DEBUG)
		echo "<br>tmpSql=$tmpSql";
	$tmpResult=mysqli_query($pubConn,$tmpSql);
	$tmpRows=mysqli_fetch_array($tmpResult);
	$opc= $tmpRows[initValue];
	$opc=$opc-0;
}
?>

<br>
<!--upload MSS parameters-->
<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" > 
<?php
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td width=100% align=center>";
echo "<input type=\"button\" value=\"Upload MSS Test Configuration\" name=\"uploadDefault\" onClick=\"confCheck(1)\">";
echo "</td>";
echo "</tr>";
echo "</table>"
?>
<br>

<!--upload BSS parameters-->
<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" > 
<?php
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td width=100% colspan=2 align=center>";
echo "<font size=3><b>Upload BSS Parameters</b></font>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">BSS IP Address</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"bssIpAddr\" maxlength=\"32\" size=\"32\" class=\"text\">&nbsp;&nbsp;Example:172.18.128.106</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">BSS Configuration File Directory</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"bssConfFileDirectory\" maxlength=\"32\" size=\"32\" class=\"text\">&nbsp;&nbsp;Example:&nbsp;/hd0/iw07_00.010/compile/cfg</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=2>";
echo "<input type=\"submit\" name=\"uploadBss\" value=\"Submit\">";
echo "</td>";
echo "</tr>";
echo "</table>"
?>
<br>

<!--configure the mss parameters to BSS-->

<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" > 
<?php
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td width=100% colspan=2 align=center>";
echo "<font size=3><b>Connect UNIB To BSS</b></font>";
echo "</td>";
echo "</tr>";

//mcc
echo "<tr>";
echo "<td width=\"40%\">MCC</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"mcc\" maxlength=\"15\" size=\"15\" value= \"$mcc\" class=\"text\"></td>";
echo "</tr>";

//mnc
echo "<tr>";
echo "<td width=\"40%\">MNC</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"mnc\" maxlength=\"15\" size=\"15\" value= \"$mnc\" class=\"text\"></td>";
echo "</tr>";


//cc
echo "<tr>";
echo "<td width=\"40%\">CC</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"cc\" maxlength=\"15\" size=\"15\" value= \"$cc\" class=\"text\"></td>";
echo "</tr>";

//ndc
echo "<tr>";
echo "<td width=\"40%\">NDC</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"ndc\" maxlength=\"15\" size=\"15\" value= \"$ndc\" class=\"text\"></td>";
echo "</tr>";

//IMSI Range
echo "<tr>";
echo "<td width=\"40%\">MSIN</td>";
echo "<nobr><td width=\"60%\"><input type=\"text\" name=\"msinStart\" maxlength=\"15\" size=\"15\" value= \"$msinStart\" class=\"text\">&nbsp;-&nbsp;";
echo "<input type=\"text\" name=\"msinEnd\" maxlength=\"15\" size=\"15\" value= \"$msinEnd\" class=\"text\"></td></nobr>";


//sn
echo "<tr>";
echo "<td width=\"40%\">SN</td>";
echo "<nobr><td width=\"60%\"><input type=\"text\" name=\"snStart\" maxlength=\"15\" size=\"15\" value= \"$snStart\" class=\"text\">&nbsp;-&nbsp;";
echo "<input type=\"text\" name=\"snEnd\" maxlength=\"15\" size=\"15\" value= \"$snEnd\" class=\"text\"></td></nobr>";
echo "</tr>";

//MSRN Range
echo "<tr>";
echo "<td width=\"40%\">MSRN Range</td>";
echo "<nobr><td width=\"60%\"><input type=\"text\" name=\"msrnStart\" maxlength=\"15\" size=\"15\" value= \"$msrnStart\" class=\"text\">&nbsp;-&nbsp;";
echo "<input type=\"text\" name=\"msrnEnd\" maxlength=\"15\" size=\"15\" value= \"$msrnEnd\" class=\"text\"></td></nobr>";
echo "</tr>";

//international prefix
echo "<tr>";
echo "<td width=\"40%\">International Prefix</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"internationalPrefix\" maxlength=\"15\" size=\"15\" value= \"$internationalPrefix\" class=\"text\"></td>";
echo "</tr>";

//national prefix
echo "<tr>";
echo "<td width=\"40%\">National Prefix</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"nationalPrefix\" maxlength=\"15\" size=\"15\" value= \"$nationalPrefix\" class=\"text\"></td>";
echo "</tr>";


echo "<tr>";
echo "<td colspan=2>";
echo "<input type=\"submit\" name=\"configureMss\" value=\"Submit\" >";
echo "</td>";
echo "</tr>";

echo "</table>";
?>
<br>

<!--configure the mss parameters to PSTN-->
<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" > 
<?php
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td width=100% colspan=2 align=center>";
echo "<font size=3><b>Connect UNIB To PSTN</b></font>";
echo "</td>";
echo "</tr>";

//Network Indicator
echo "<tr>";
echo "<td width=\"40%\">Network Indicator</td>";
echo "<td>";
echo "<select name=\"ni\">"; 
for ($i=0;$i<sizeof($NetOPCArr);$i++){
	echo "<option value=\"$i\" ";
	if ($i == $ni){
		echo "selected";
	}
	echo ">{$NetOPCArr[$i][1]}</option>";
}
echo "</select>"; 
echo "</td>";
echo "</tr>";

//opc
echo "<tr>";
echo "<td width=\"40%\">OPC</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"opc\" maxlength=\"15\" size=\"15\" value= \"$opc\" class=\"text\">(Hex)</td>";
echo "</tr>";

//dpc
echo "<tr>";
echo "<td width=\"40%\">DPC</td>";
echo "<td width=\"60%\"><input type=\"text\" name=\"dpc\" maxlength=\"15\" size=\"15\" value= \"$dpc\" class=\"text\">(Hex)</td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan=2>";
echo "<input type=\"submit\" name=\"configurePstn\" value=\"Submit\" >";
echo "</td>";
echo "</tr>";

echo "</table>";
?>