<?php
require("../sub_inc/commfunc.php");


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

/*
if($clock==1)
	echo "This is clock<hr>";
if($signal==1)
	echo "This is signal<hr>";	
*/

$helpId = "";
$DEBUG = 0;
$leftWidth = '20%';
$pubDb	 = 'OMC_PUB';
$sysInfoTab = 'sysInfo';
$nSetComm = 3;
//echo "<br>cardNo:$cardNo";
if(!isset($cardNo))
{
	$selSql = "select sysNo,subSysNo from $sysInfoTab where sysTypeNo='111' order by sysNo,subSysNo";
	$Result = mysqli_query($pubConn,$selSql);
	if($Rows	= mysqli_fetch_array($Result))
		$cardNo=$Rows[sysNo].'-'.$Rows[subSysNo];
}	

$temp=trim($sysNo_slot);
if(strlen($temp)==0)
	$sysNo_slot=0;
if(!isset($sysNo_slot))
	$sysNo_slot=0;

$tmpArr = explode('-',$cardNo);
$sysNo_slot = $tmpArr[0];
$subSysNo_slot = $tmpArr[1];

$k8e1Table 	 = 'param_' . $sysNo_slot;
$k8e1Db		 = "OBJ_111";
$pubSysId = '111_' . $sysNo_slot . '_99';
$subSysId = '111_' . $sysNo_slot . '_'.$subSysNo_slot;
?>

<script LANGUAGE="JavaScript">
function checkValue(flag,item,sysNo,subSysNo)
{
	var v = item.value;
	var ulr="check8kCardSignalSlot.php?flag="+flag+"&cmpValue="+v+"&sysNo="+sysNo+"&subSysNo="+subSysNo;
	//alert(ulr);
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_o = new XMLHttpRequest();
	}

	//alert(ulr);
	request_o.open("GET",ulr,"false");

	request_o.onreadystatechange = ifReset; 

	request_o.send(null);
}
function ifReset()
{	
	if(request_o.readyState == 4)
	{
		var strResult = request_o.responseText;
		//alert(strResult);
		if(strResult == 'reset')
		{
			//alert('Signaling slot changed,you should to reset the E1/T1 card.');
			hideIt('KB4Child','Reset8ke1','1');
		}
		else
			hideIt('KB4Child','Reset8ke1','0');
	}
}
function hideIt(term, ckbox, flag)
{
	whichEl = document.getElementById(term);
	ckboxObj = document.getElementById(ckbox);
	if(flag==1)
	{
		whichEl.style.display = "";
		ckboxObj.checked=true;
	}
	if(flag==0)
	{
		whichEl.style.display = "none";
		ckboxObj.checked=false;
	}
}
function select8ke1Card(item)
{
	var base_url,url_add,url; 
	
	base_url='<?php echo"$PHP_SELF?&backurl=$backurl&signal=1&cardNo=";?>';
	url_add=item.value;
	url=base_url+url_add;
	window.location.href=url; 
}
</script>

<?php
//echo "<br>cardNo:$cardNo";

{//get all sysNo.
	$selSql = "select sysNo,subSysNo from $sysInfoTab where sysTypeNo='111' order by sysNo,subSysNo";
	$Result = mysqli_query($pubConn,$selSql);
	$sysNo = -1;
	$i = 0;
	while($Rows	= mysqli_fetch_array($Result))
	{
		if($sysNo==$Rows[sysNo])
			continue;
		$sysNo = $Rows[sysNo];
		$sysNoArr[$i] = $sysNo;
		$sysId = '111_' . $sysNo . '_99';
		//$sysId_arr[$i] = array($sysId, array('2.5'), '8ke1');
		$sysId_arr[$i] = array('oid'=>'2.5','sysid'=>$sysId, 'selectsysid'=>'-1'); 	
		$i++;
	}
	
	//$sysId_arr[$i] = array($pubSysId,array('2.10.2'),'8ke1',$subSysId);
	//echo "pubSysId:$pubSysId---subSysId:$subSysId<hr>";
	$sysId_arr[$i] = array('oid'=>'2.10.2','sysid'=>$pubSysId, 'selectsysid'=>'-1'); 
	
 		
}//end 

?>

<body topmargin="0" leftmargin="12">

<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
	{//signaling slot
		$levelWhere = getLevelWhere('2.10.2');
		$setValue = omc_keep_length(dechex($sigSlot),0,2);
		$updSql = "update $k8e1Table set initValue='$setValue' where $levelWhere and instanceNo='$subSysNo_slot'";
		mysqli_query($pubConn,$updSql);
	}//end signaling slot
	
	{//dpll
		for($i=0; $i< sizeof($sysNoArr); $i++)
		{
			$sysNo = $sysNoArr[$i];
			$tmpTable 	 = 'param_' . $sysNo;
			$levelWhere = getLevelWhere('2.5');
			$selSql = "select instanceNo,initValue from $tmpTable where $levelWhere and (instanceNo='0' or instanceNo='1')";
			$Result = mysqli_query($pubConn,$selSql);
			while($Rows = mysqli_fetch_array($Result))
			{
				$name = "csu".$sysNo."_card".$Rows[instanceNo];
				$setValue = omc_keep_length(dechex($$name),'0','2');
				$updSql = "update $tmpTable set initValue='$setValue' where $levelWhere and instanceNo='$Rows[instanceNo]'";
				mysqli_query($pubConn,$updSql);
			}
		}
	}//END dpll
	
	
	if(isset($setSys) || isset($getSys))
	{
		if(isset($setSys))
		{
			
			echo "<br>Setting parameters<br>";
				$sysId_arr[$i] = array('oid'=>'2.10.2','sysid'=>$pubSysId, 'selectsysid'=>'-1'); 

		  //print_r($sysId_arr);
				
			SetParam($sysId_arr);	
			//echo "$resetCard<hr>";
			
			
			//exit;
			session_register("SetFlag");
			$showType="Set";
			
			
			if(isset($resetCard))
			{
				
				echo "<br>Reseting CSU$sysNo-Card $subSysNo_slot ...";
				//echo "<hr><hr>$subSysId";
				
				$SaveArr=array(
					array('oidstr'=>'3.1',	'setvalue'=>'5A'	,'sysid'=> $subSysId)
					);
				
				/*
				$SaveArr=array(
					array('oidstr'=>'3.1',	'setvalue'=>'5A'	,'sysid'=> '111_0_99'),
					array('oidstr'=>'3.1',	'setvalue'=>'5A'	,'sysid'=> '111_1_99'),
					);	
					*/
				//print_r($SaveArr);	
				SaveParam($SaveArr);
			}
		}
		else
		{ 
			
			$database="OMC_PUB";
			$sql="select detailStat from sysStat where sysTypeNo=111 and sysNo=0 ";
			$result=@mysqli_query($pubConn,$sql);
			$rows=@mysqli_fetch_array($result);
			
			if($rows)
				$plat0_have=1;
			else	
				$plat0_have=0;
			
			$database="OMC_PUB";
			$sql="select detailStat from sysStat where sysTypeNo=111 and sysNo=1 ";
			$result=@mysqli_query($pubConn,$sql);
			$rows=@mysqli_fetch_array($result);
			
			if($rows)
				$plat1_have=1;
			else	
				$plat1_have=0;
			if($plat1_have==1&&$plat0_have==1)
			{	
				$sysId_arr_get = array(				
							array('oid'=>'2.5','sysid'=>'111_0_99', 'selectsysid'=>'-1'), 						
							array('oid'=>'2.10.2','sysid'=>'111_0_99', 'selectsysid'=>'-1'),	
							array('oid'=>'2.5','sysid'=>'111_1_99', 'selectsysid'=>'-1'), 			
							array('oid'=>'2.10.2','sysid'=>'111_1_99', 'selectsysid'=>'-1')
							);
			}
			if($plat1_have==1&&$plat0_have==0)
			{
				$sysId_arr_get = array(				
							array('oid'=>'2.5','sysid'=>'111_1_99', 'selectsysid'=>'-1'), 			
							array('oid'=>'2.10.2','sysid'=>'111_1_99', 'selectsysid'=>'-1')
							);
			}		
			
			if($plat1_have==0&&$plat0_have==1)
			{
				$sysId_arr_get = array(				
							array('oid'=>'2.5','sysid'=>'111_0_99', 'selectsysid'=>'-1'), 			
							array('oid'=>'2.10.2','sysid'=>'111_0_99', 'selectsysid'=>'-1')
							);
			}		
			if($plat1_have==0&&$plat0_have==0)
			{
				$sysId_arr_get = array(				
							array('oid'=>'2.5','sysid'=>'111_0_99', 'selectsysid'=>'-1'), 			
							array('oid'=>'2.10.2','sysid'=>'111_0_99', 'selectsysid'=>'-1')
							);
			}		
						
			echo "<br>Getting parameters<br>";
			session_register("GetFlag");
			//echo "<hr>Get";
			//print_r($sysId_arr_get);	
			GetParam($sysId_arr_get);
			$showType="Get";
		}
	}
	echo "<script LANGUAGE=\"JavaScript\">";
	if(!$DEBUG)
		if($clock==1)
			echo "\n window.location.href=\"$PHP_SELF?backurl=$backurl&cardNo=$cardNo&clock=1\";";
		else	
			echo "\n window.location.href=\"$PHP_SELF?backurl=$backurl&cardNo=$cardNo&signal=1\";";
				
	echo "\n </script>";	
	exit();	
}
if($clock==1)
	echo "<form method=\"POST\" name=\"setForm\" action=\"$PHP_SELF?backurl=$backurl&cardNo=$cardNo&clock=1\">";
else
	echo "<form method=\"POST\" name=\"setForm\" action=\"$PHP_SELF?backurl=$backurl&cardNo=$cardNo&signal=1\">";	
{
	echo "<table border=\"0\" width=\"100%\">";
	echo "<tr>";
	echo "<td width=\"50%\">";
	echo "$strConfiguration > <a href=\"../overview/overview.php\">Network</a> > <a href=\"$backurl\">MSC</a> > <a href=\"#\">";
	if($clock==1)
		echo "Clock</a>";
	else
	  echo "Signaling Channel</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%\">";
	//WXC2-MSC 
	if($clock==1)
		echo "<b></b>";
	echo "</td>";
	echo "<td width=\"22%\">";
		echo "<input type=\"submit\" title=\"$strTitleGet\" name=\"getSys\" value=\"$strGet\" class=\"button\" style=\"$buttonStyle\">";
		echo "&nbsp;&nbsp;&nbsp;";
		echo "<input type=\"submit\" title=\"$strTitleSet\" name=\"setSys\" value=\"$strSet\" class=\"button\" style=\"$buttonStyle\">";
	echo "</td>";
	echo "<td width=\"8%\" 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>";
	echo "<br>";

}
?>

<?php
if($clock==1)
	echo "<table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
else
	echo "<table style=\"display:none\" border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";

{//DPLL
	$disabled0 = $disabled1 = 'disabled';
	for($i=0; $i< sizeof($sysNoArr); $i++)
	{
		$sysNo = $sysNoArr[$i];
		if($sysNo==0)
			$disabled0 = '';
		if($sysNo==1)
			$disabled1 = '';
		$tmpTable 	 = 'param_' . $sysNo;
		$levelWhere = getLevelWhere('2.5');
		$selSql = "select instanceNo,initValue from $tmpTable where $levelWhere and (instanceNo='0' or instanceNo='1')";
		$Result = mysqli_query($pubConn,$selSql);
		while($Rows 	= mysqli_fetch_array($Result))
		{
			$name = "csu".$sysNo."_card".$Rows[instanceNo];
			$$name = hexdec($Rows[initValue]);
		}
	}
	echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"30%\"><b>Priority</b></td>";
		echo "<td width=\"35%\"><b>CSU0</b></td>";
		echo "<td width=\"35%\"><b>CSU1</b></td>";
	echo "</tr>";
	
	echo "<tr>";
		echo "<td>Primary</td>";
		echo "<td>";
			echo "<select size=\"1\" value=\"$csu0_card0\" name=\"csu0_card0\" $disabled0>";
			for($i=0; $i< 9; $i++)
			{
				if($i==8)
					$var = "Internal";
				else
					$var = "Port-" . $i;
				echo "<option value=\"$i\" ";
				if($i == $csu0_card0)
					echo "selected";
				echo ">$var</option>";
			}
			echo "</select>";
		echo "</td>";
		
		echo "<td>";
			echo "<select size=\"1\" value=\"$csu1_card0\" name=\"csu1_card0\" $disabled1>";
			for($i=0; $i< 9; $i++)
			{
				if($i==8)
					$var = "Internal";
				else
				{
					$var = $i+128;
					$var = "Port-" . $var;
				}
				echo "<option value=\"$i\" ";
				if($i == $csu1_card0)
					echo "selected";
				echo ">$var</option>";
			}
			echo "</select>";
		echo "</td>";
	echo "</tr>";
	
	echo "<tr>";
		echo "<td>Secondary</td>";
		echo "<td>";
			echo "<select size=\"1\" value=\"$csu0_card1\" name=\"csu0_card1\" $disabled0>";
			for($i=0; $i< 9; $i++)
			{
				if($i==8)
					$var = "Internal";
				else
				{
					$var = $i+8;
					$var = "Port-" . $var;
				}
				echo "<option value=\"$i\" ";
				if($i == $csu0_card1)
					echo "selected";
				echo ">$var</option>";
			}
			echo "</select>";
		echo "</td>";
		
		echo "<td>";
			echo "<select size=\"1\" value=\"$csu1_card1\" name=\"csu1_card1\" $disabled1>";
			for($i=0; $i< 9; $i++)
			{
				if($i==8)
					$var = "Internal";
				else
				{
					$var = $i+136;
					$var = "Port-" . $var;
				}
				echo "<option value=\"$i\" ";
				if($i == $csu1_card1)
					echo "selected";
				echo ">$var</option>";
			}
			echo "</select>";
		echo "</td>";
	echo "</tr>";
}//END DPLL
echo "</table>";




echo "<input type=\"hidden\" name=\"backurl\" value=\"$backurl\">";	

{
	$selSql = "select initValue from $k8e1Table 
				where level_1='2' and level_2='10' and level_3='2' and instanceNo='$subSysNo_slot' ";
	
		$result = mysqli_query($pubConn,$selSql);
		$row = mysqli_fetch_array($result);
		
	
	$sigSlot = hexdec($row[initValue]);
if($signal==1)
{
	echo "<b></b>";
	echo "<table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
}
else
	echo "<table style=\"display:none\" border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
	
	echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"30%\"><b>Card</b></td>";
		echo "<td width=\"35%\"><b>Parameter</b></td>";
		echo "<td width=\"35%\"><b>Value</b></td>";
		//echo "<td width=\"10%\"></td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td>";
		{//select a 8ke1 card.
			$selSql = "select sysNo,subSysNo from $sysInfoTab where sysTypeNo='111' order by sysNo,subSysNo";
			$Result = mysqli_query($pubConn,$selSql);
			
			echo "<select name=\"cardNo\" onchange=\"select8ke1Card(this)\" >";
				while($Rows	= mysqli_fetch_array($Result))
				{
					$tmpCard=$Rows[sysNo].'-'.$Rows[subSysNo];
					$tmpSysName = "CSC-" . $Rows[sysNo]." Card ".$Rows[subSysNo];
					if($cardNo==$tmpCard)
					{
						$selectedStr='selected';
					}else{
						$selectedStr='';
					}
					echo "<option value=\"$tmpCard\" $selectedStr>$tmpSysName</option>";
				}
			echo "</select>";
		}//end select
		echo "</td>";
		echo "<td>Signaling Time Slot</td>";
		echo "<td>";
			echo "<select name=\"sigSlot\" size=\"1\" style=width:\"$nShowsize\" onchange=\"checkValue('sigSlot',this,$sysNo_slot,$subSysNo_slot)\">";
				for($i=1; $i< 25; $i++)
				{
					echo "<option value=\"$i\" ";
					if($i==$sigSlot) echo "selected";
					echo ">TS$i</option>";
				}
			echo "</select>";
		
		echo "&nbsp;&nbsp;&nbsp;<span id=\"KB4Child\" style=\"DISPLAY: none\">";
		echo "<input type=\"Checkbox\" id=\"Reset8ke1\" name=\"resetCard\" onClick=\"hideIt('KB4Child','Reset8ke1','0');\">Reset 8kE1/T1 Card.";
		echo "</span>";
		echo "</td>";
	echo "</tr>";
echo "</table>";
}
?>

</form>
</body>
