<?php
require("../../inc/header.inc");      
$DEBUG=0;
?>
<?php   
	if(stristr($HTTP_USER_AGENT,"sun") != false)
	{
		exit;
		//echo "Sun system<br>";
	}
	else
	{
		//exit;
		//echo "other system<br>";
	}
	//sleep(1);
	echo "<meta http-equiv=\"refresh\" content=\"10; url=$PHP_SELF\">";
	
	$dualTableLevelWhere="T1.level_1=T2.level_1 AND
						  T1.level_2=T2.level_2 AND
						  T1.level_3=T2.level_3 AND
						  T1.level_4=T2.level_4 AND
						  T1.level_5=T2.level_5 AND
						  T1.level_6=T2.level_6 AND
						  T1.level_7=T2.level_7 AND
						  T1.level_8=T2.level_8 AND
						  T1.level_9=T2.level_9 AND
						  T1.level_10=T2.level_10";
	
	
	/*******************************************************************************/
	/**************               TrunkGroup x 256              ********************/
	/*******************************************************************************/
	//Get the count of TrunkGroup
	$TrunkGroupCount=0;
	$database="OBJ_320";
	$objDbConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'],$database);	
	$selSql = "SELECT T1.* FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND T2.name_2='tgIndex'";
	if($DEBUG) echo "<br>selSql=$selSql";
	$selRes = @mysqli_query($objDbConn,$selSql);
	$TrunkGroupCount=mysqli_num_rows($selRes);
	echo mysqli_error($objDbConn);
	//echo "count:$TrunkGroupCount<br>";

	$TrunkGroupInstanceNoList="";
	while($selRow = @mysqli_fetch_array($selRes)){
		$tmpSelRow=$selRow;
		$TrunkGroupInstanceNoList=$TrunkGroupInstanceNoList.$selRow[instanceNo].",";
		if($DEBUG) echo "<br>TrunkGroupInstanceNoList=$TrunkGroupInstanceNoList";
	};
	
	
	for($i=0;$i<=10;$i++){
		$level='level_'.$i;
		$nextLevel='level_'.($i+1);
		if($tmpSelRow[$level]!='0'&&$tmpSelRow[$nextLevel]==0)
			break;
		if($i==1)
			$tmpLevelWhere='T1.'.$level.'='.$tmpSelRow[$level];
		else
			$tmpLevelWhere=$tmpLevelWhere.' AND T1.'.$level.'='.$tmpSelRow[$level];
			
	}
	
	if($DEBUG) echo "<br>tmpLevelWhere=$tmpLevelWhere";
	//Get the parameter of TrunkGroup
	$index=0;
	$database="OBJ_320";
	$selSql = "SELECT T1.*,T2.name_2 FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND $tmpLevelWhere ";
	if($DEBUG) echo "<br>selSql=$selSql";
	
	if($TrunkGroupCount!=0)
	{
		$selRes = @mysqli_query($objDbConn,$selSql);
		$selRow = @mysqli_fetch_array($selRes);
		do
		{
			$instanceNo_tmp=$selRow[instanceNo]."";
			//Title
			if(stristr($selRow[name_2],'tgTitle'))
			{
				if(strlen($selRow[initValue])==0)
					$TrunkGroup_Title[$instanceNo_tmp]="--";
				else
					$TrunkGroup_Title[$instanceNo_tmp]=$selRow[initValue];
					
			}
			
			//Status
			else if(stristr($selRow[name_2],'tgAvailabilityStatus'))
			{	
				$TrunkGroup_Status[$instanceNo_tmp]=$selRow[initValue];
				//echo "instanceNo $instanceNo_tmp status:{$TrunkGroup_Status[$instanceNo_tmp]}<br>";
			}
			//Protocol Type
			else if(stristr($selRow[name_2],'tgProtocolType'))
			{
				$TrunkGroup_ProtocolType[$instanceNo_tmp]=$selRow[initValue];
			}
			//Protocol Variant
			else if(stristr($selRow[name_2],'tgProtocolVariant'))
			{
				$TrunkGroup_ProtocolVariant[$instanceNo_tmp]=$selRow[initValue];
			}
			
		}while($selRow = @mysqli_fetch_array($selRes));
	}
	
	/********************           Trunk x 256           ****************************/
	//Get the count of Trunk
	$TrunkCount=0;
	$database="OBJ_320";
	$selSql = "SELECT T1.* FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND T2.name_2='trunkIndex'";
	//echo "$selSql<br>";
	$selRes = @mysqli_query($objDbConn,$selSql);
	$TrunkCount=mysqli_num_rows($selRes);
	echo mysqli_error($objDbConn);
	//echo "count:$TrunkCount<br>";
	
	$TrunkInstanceNoList="";
	while($selRow = @mysqli_fetch_array($selRes)){
		$tmpSelRow=$selRow;
		$TrunkInstanceNoList=$TrunkInstanceNoList.$selRow[instanceNo].",";
		if($DEBUG) echo "<br>TrunkInstanceNoList=$TrunkInstanceNoList";
	};
	for($i=0;$i<=10;$i++){
		$level='level_'.$i;
		$nextLevel='level_'.($i+1);
		if($tmpSelRow[$level]!='0'&&$tmpSelRow[$nextLevel]==0)
			break;
		if($i==1)
			$tmpLevelWhere='T1.'.$level.'='.$tmpSelRow[$level];
		else
			$tmpLevelWhere=$tmpLevelWhere.' AND T1.'.$level.'='.$tmpSelRow[$level];
			
	}
	
	//Get the parameter of Trunk
	$database="OBJ_320";
	$selSql = "SELECT T1.*,T2.name_2 FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND $tmpLevelWhere ";
	if($DEBUG) echo "selSql=$selSql<br>";
	if($TrunkCount!=0)
	{
		$selRes = @mysqli_query($objDbConn,$selSql);
		$selRow = @mysqli_fetch_array($selRes);
		do
		{
			$instanceNo_tmp=$selRow[instanceNo];
			$t=explode(".",$instanceNo_tmp);
			//index for tg
			if(stristr($selRow[name_2],'trunkTgIndex'))
			{
				$Trunk_TrunkGroupIndex[$instanceNo_tmp]=$t[0];
				if(stristr($TrunkInstanceNoList,$instanceNo_tmp)!=FALSE)
					$TrunkGroup_TrunkCount[$t[0]]++;
				//echo "TrunkInstanceNoList=$TrunkInstanceNoList,   find={$t[0]},   instanceNo_tmp=$instanceNo_tmp<br>";
				//echo "tK:$instanceNo_tmp,{$Trunk_TrunkGroupIndex[$instanceNo_tmp]}<br>";
			}
			//Title
			else if(stristr($selRow[name_2],'trunkTitle'))
			{
				
				if(strlen($selRow[initValue])==0)
					$Trunk_Title[$instanceNo_tmp]="--";
				else
					$Trunk_Title[$instanceNo_tmp]=$selRow[initValue];
			}
			//Status
			else if(stristr($selRow[name_2],'trunkAvailabilityStatus'))
			{
				$Trunk_Status[$instanceNo_tmp]=$selRow[initValue];
			}
			//MG ID
			else if(stristr($selRow[name_2],'trunkMgID'))
			{
				$Trunk_MGID[$instanceNo_tmp]=$selRow[initValue];
			}
			//MG Port
			else if(stristr($selRow[name_2],'trunkPortID'))
			{
				$Trunk_PortID[$instanceNo_tmp]=$selRow[initValue];
			}
			//Channel In Use
			else if(stristr($selRow[name_2],'trunkChannelInUse'))
			{
				$Trunk_ChannelInUse[$instanceNo_tmp]=$selRow[initValue];
			}
			
		}while($selRow = @mysqli_fetch_array($selRes));
	}
	
	
	/********************     Media Gate x 256    *****************************/
	//Get the count of MG
	$MGCount=0;
	$database="OBJ_320";
	$selSql = "SELECT T1.* FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND T2.name_2='mgIndex'";
	$selRes = @mysqli_query($objDbConn,$selSql);
	$MGCount=mysqli_num_rows($selRes);
	echo mysqli_error($objDbConn);
	//echo "MGCount:$MGCount<br>";
	
	$MGInstanceNoList="";
	while($selRow = @mysqli_fetch_array($selRes)){
		$tmpSelRow=$selRow;
		if($selRow[instanceNo]!=0)
			$MGInstanceNoList=$MGInstanceNoList.$selRow[instanceNo].",";
		if($DEBUG) echo "<br>MGInstanceNoList=$MGInstanceNoList";
	};
	
	//echo "MGInstanceNoList:$MGInstanceNoList<br>";
	
	for($i=0;$i<=10;$i++){
		$level='level_'.$i;
		$nextLevel='level_'.($i+1);
		if($tmpSelRow[$level]!='0'&&$tmpSelRow[$nextLevel]==0)
			break;
		if($i==1)
			$tmpLevelWhere='T1.'.$level.'='.$tmpSelRow[$level];
		else
			$tmpLevelWhere=$tmpLevelWhere.' AND T1.'.$level.'='.$tmpSelRow[$level];
			
	}
	
	
	//Get the parameter of MG
	$database="OBJ_320";
	$selSql = "SELECT T1.*,T2.name_2 FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND $tmpLevelWhere ";
	if($MGCount!=0)
	{
		$selRes = @mysqli_query($objDbConn,$selSql);
		$selRow = @mysqli_fetch_array($selRes);
		do
		{
			$instanceNo_tmp=$selRow[instanceNo];
			
			//ID
			if(stristr($selRow[name_2],'mgIndex'))
			{
				if(strlen($selRow[initValue])==0)
					$MG_ID[$instanceNo_tmp]="--";
				else	
					$MG_ID[$instanceNo_tmp]=$selRow[initValue];
			}
			//Title
			else if(stristr($selRow[name_2],'mgTitle'))
			{
				if(strlen($selRow[initValue])==0)
					$MG_Title[$instanceNo_tmp]="--";
				else
					$MG_Title[$instanceNo_tmp]=$selRow[initValue];
			}
			//Domain Name
			else if(stristr($selRow[name_2],'mgDomainName'))
			{
				if(strlen($selRow[initValue])==0)
					$MG_DomainName[$instanceNo_tmp]="--";
				else
					$MG_DomainName[$instanceNo_tmp]=$selRow[initValue];
			}
			//IP
			else if(stristr($selRow[name_2],'mgIp'))
			{
				if(strlen($selRow[initValue])==0)
					$MG_IP[$instanceNo_tmp]="--";
				else
					$MG_IP[$instanceNo_tmp]=$selRow[initValue];
			}
			
			
		}while($selRow = @mysqli_fetch_array($selRes));
	}
	
	/*******************    Port 256 x 32     ************************/
	//Get the count of Port
	$PortCount=0;
	$database="OBJ_320";
	$selSql = "SELECT T1.* FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND T2.name_2='ppIndex'";
	$selRes = @mysqli_query($objDbConn,$selSql);
	$PortCount=mysqli_num_rows($selRes);
	echo mysqli_error($objDbConn);
	//echo "PortCount:$PortCount<br>";
	
	$PortInstanceNoList="";
	while($selRow = @mysqli_fetch_array($selRes)){
		$tmpSelRow=$selRow;
		$PortInstanceNoList=$PortInstanceNoList.$selRow[instanceNo].",";
	};
	for($i=0;$i<=10;$i++){
		$level='level_'.$i;
		$nextLevel='level_'.($i+1);
		if($tmpSelRow[$level]!='0'&&$tmpSelRow[$nextLevel]==0)
			break;
		if($i==1)
			$tmpLevelWhere='T1.'.$level.'='.$tmpSelRow[$level];
		else
			$tmpLevelWhere=$tmpLevelWhere.' AND T1.'.$level.'='.$tmpSelRow[$level];
			
	}
	
	
	//echo "PortInstanceNoList:$PortInstanceNoList<br>";
	
	//Get the parameter of Port
	$database="OBJ_320";
	$selSql = "SELECT T1.*,T2.name_2 FROM param_99 AS T1,paramConf AS T2 WHERE $dualTableLevelWhere AND $tmpLevelWhere ";
	if($DEBUG) echo "selSql=$selSql<br>";
	if($PortCount!=0)
	{
		$selRes = @mysqli_query($objDbConn,$selSql);
		$selRow = @mysqli_fetch_array($selRes);
		do
		{
			$instanceNo_tmp=$selRow[instanceNo];
			$t=explode(".",$instanceNo_tmp);
			//index for mg
			if(stristr($selRow[name_2],'ppMgIndex'))
			{
				$Port_MGIndex[$instanceNo_tmp]=$t[0];
				$Port_Index[$instanceNo_tmp]=$t[1]+0;
			}
			//Used flag
			else if(stristr($selRow[name_2],'ppUsedFlag'))
			{
				$Port_UsedFlag[$instanceNo_tmp]=$selRow[initValue];
			}
			//Status
			else if(stristr($selRow[name_2],'ppStatus'))
			{
				$Port_Status[$instanceNo_tmp]=$selRow[initValue];
			}
			
		}while($selRow = @mysqli_fetch_array($selRes));
	}
	
	/***********        Test the data         ************************/
	
	//echo "TG InstanceNo:$TrunkGroupInstanceNoList<br>";
	//echo "TK InstanceNo:$TrunkInstanceNoList<br>";
	$insTg=explode(",",$TrunkGroupInstanceNoList);
	//TG
	for($i=0;$i<$TrunkGroupCount;$i++)
	{
		$instanceNo_tmp=$insTg[$i];

		if(0)
		{
			echo "TrunkGroup InstanceNo:$instanceNo_tmp<br>";
			echo "           Title:{$TrunkGroup_Title[$instanceNo_tmp]}<br>";
			echo "           Status:{$TrunkGroup_Status[$instanceNo_tmp]}<br>";
			echo "           ProtocolType:{$TrunkGroup_ProtocolType[$instanceNo_tmp]}<br>";
			echo "           ProtocolVariant:{$TrunkGroup_ProtocolVariant[$instanceNo_tmp]}<br>";
			echo "           Trunk Count:{$TrunkGroup_TrunkCount[$instanceNo_tmp]}<br>";
		}
	}
	
	/*
	//Trunk
	for($i=0;$i<$TrunkCount;$i++)
	{
		$instanceNo_tmp=$i;
		echo "Trunk InstanceNo:{$Trunk_InstanceNo[$i]} index:$i<br>";
		echo "           Title:{$Trunk_Title[$instanceNo_tmp]}<br>";
		echo "           Status:{$AvailStatusLabel[$Trunk_Status[$instanceNo_tmp]]}<br>";
		echo "           MGID:{$Trunk_MGID[$instanceNo_tmp]}<br>";
		echo "           PortID:{$Trunk_PortID[$instanceNo_tmp]}<br>";
		echo "           CH In use:{$Trunk_ChannelInUse[$instanceNo_tmp]}<br>";
		echo "           Be to Tg:{$Trunk_TrunkGroupIndex[$instanceNo_tmp]}<br><br>";
	}
	*/
	/*
	$insMg=explode(",",$MGInstanceNoList);
	for($i=0;$i<$MGCount;$i++)
	{
		$instance_tmp=$insMg[$i];
		echo "MG ID:{$MG_ID[$instance_tmp]}<br>";
		echo "   Title:{$MG_Title[$instance_tmp]}<br>";
		echo "   Domain:{$MG_DomainName[$instance_tmp]}<br>";
		echo "   IP::{$MG_IP[$instance_tmp]}<br>";
	}
	
	echo "--------------------------------------------<br>";
	$insP=explode(",",$PortInstanceNoList);
	for($i=0;$i<$PortCount;$i++)
	{
		$instance_tmp=$insP[$i];
		echo "Port Index:{$Port_Index[$instance_tmp]}<br>";
		echo "Port to MG:{$Port_MGIndex[$instance_tmp]}<br>";
		echo "Port InstanceNo:{$insP[$i]}<br>";
		echo "Port UsedFlag:{$Port_UsedFlag[$instance_tmp]}<br>";
		echo "Port Status:{$Port_Status[$instance_tmp]}<br>";
	}
	*/
	/************ Create the HTML for TrunkGroup Table ***************/
	//Table Title
	$TrunkGroupHTML="<table width=100% border=1 cellpadding=1 cellspacing=0  bordercolor=#666666 bordercolordark=#FFFFFF bgcolor=#FFFFFF>";
	$TrunkGroupHTML=$TrunkGroupHTML."<caption><center><font size=2 color=blue>TG Status</font></center></caption>";
	//Table Column Name
	$TrunkGroupHTML=$TrunkGroupHTML."<tr bgcolor=#E6E6E6>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=2 width=10%>Title</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=2 width=10%>Status</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=2 width=10%>Protocol<br>Type</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=2 width=10%>Protocol<br>Variant</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=1 colspan=5 width=60%>Trunk</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<tr bgcolor=#E6E6E6>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=1 width=12%>Title</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=1 width=12%>MG ID</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=1 width=12%>MG Port</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=1 width=12%>Status</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."<td align=center rowspan=1 width=12%><nobr>Channel In Use</td>";
	$TrunkGroupHTML=$TrunkGroupHTML."</tr>";
	$TrunkGroupHTML=$TrunkGroupHTML."</tr>";
	
	//Table Content
	$AvailStatusLabel=array(0=>"In Test",1=>"Failed",2=>"Power Off",3=>"Offline",4=>"Online",5=>"Dependency",6=>"Degraded",7=>"Not Installed","null"=>"--");
	$AvailStatusColor=array(0=>"#CECECE",1=>"#FF0000",2=>"#FF0000",3=>"#FF0000",4=>"#0000FF",5=>"#0000FF",6=>"#0000FF",7=>"#C6C6C6","null"=>"#CECECE");
	$ProtocolTypeLabel=getParamOptionArray("320","ProtocolType",PARAM_NAME);
	$ProtocolVariantLabel=getParamOptionArray("320","ProtocolVariant",PARAM_NAME);
	
	$insTg=explode(",",$TrunkGroupInstanceNoList);
	//echo "TrunkCount:$TrunkCount<br>";
	for($i=0;$i<$TrunkGroupCount;$i++)
	{
		$instanceNo_tmp=$insTg[$i];
		$trunkCount_tmp=$TrunkGroup_TrunkCount[$instanceNo_tmp];
		//echo "rowspan:$trunkCount_tmp<br>";
		$TrunkGroupHTML=$TrunkGroupHTML."<tr>";
		//title
		$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=".$trunkCount_tmp.">".$TrunkGroup_Title[$instanceNo_tmp]."</td>";
		//status
		if(strlen($TrunkGroup_Status[$instanceNo_tmp]) == 0)
		{
			$TrunkGroup_Status[$instanceNo_tmp]="null";
		}
		$tmpColor=$AvailStatusColor[$TrunkGroup_Status[$instanceNo_tmp]];
		$tmpValue=$AvailStatusLabel[$TrunkGroup_Status[$instanceNo_tmp]];
		$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=".$trunkCount_tmp."><font color=".$tmpColor.">".$tmpValue."</font></td>";
		//Type
		$tmpValue=$ProtocolTypeLabel[$TrunkGroup_ProtocolType[$instanceNo_tmp]];
		$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=".$trunkCount_tmp.">".$tmpValue."<br></td>";
		//Variant
		$tmpValue=$ProtocolVariantLabel[$TrunkGroup_ProtocolVariant[$instanceNo_tmp]];
		$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=".$trunkCount_tmp.">".$tmpValue."<br></td>";
		
		$trunkCount_tmp=$trunkCount_tmp+0;
		if($trunkCount_tmp==0)
		{
			$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>--</td>";
			$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>--</td>";
			$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>--</td>";
			$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>--</td>";
			$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>--</td></tr>";
		}
		
		$insTk=explode(",",$TrunkInstanceNoList);
		for($j=0;$j<$TrunkCount;$j++)
		{
			$inst=$insTk[$j];
			if($Trunk_TrunkGroupIndex[$inst]==$instanceNo_tmp)
			{
				//echo "$instanceNo_tmp<br>";
				//title
				$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>".$Trunk_Title[$inst]."</td>";
				//MG ID
				$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>".$Trunk_MGID[$inst]."</td>";
				//Port ID
				$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>".$Trunk_PortID[$inst]."</td>";
				//Status
				if(strlen($Trunk_Status[$inst]) == 0)
					$Trunk_Status[$inst]="null";
				$tmpColor=$AvailStatusColor[$Trunk_Status[$inst]];
				$tmpValue=$AvailStatusLabel[$Trunk_Status[$inst]];
				$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1><font color=".$tmpColor.">".$tmpValue."</font></td>";
				//Channel In Use
				if(strlen($Trunk_ChannelInUse[$inst]) == 0)
					$Trunk_ChannelInUse[$inst]=0;
				$TrunkGroupHTML=$TrunkGroupHTML."<td rowspan=1>".$Trunk_ChannelInUse[$inst]."</td></tr>";
			}
		}
		$TrunkGroupHTML=$TrunkGroupHTML."</tr>";
		
	}
	$TrunkGroupHTML=$TrunkGroupHTML."</table>";
	
	/*********** Create the MG Table  *************************/
	//Table Title
	$MGHTML="<table width=100% border=1 cellpadding=1 cellspacing=0  bordercolor=#666666 bordercolordark=#FFFFFF bgcolor=#FFFFFF>";
	$MGHTML=$MGHTML."<caption><center><font size=2 color=blue>MG Status</font></center></caption>";
	//Table Column Name
	$MGHTML=$MGHTML."<tr bgcolor=#E6E6E6>";
	$MGHTML=$MGHTML."<td align=center rowspan=1 width=5%>MG ID</td>";
	$MGHTML=$MGHTML."<td align=center rowspan=1 width=6%>Title</td>";
	$MGHTML=$MGHTML."<td align=center rowspan=1 width=9%><nobr>Domain Name</td>";
	$MGHTML=$MGHTML."<td align=center rowspan=1 width=7%>IP Address</td>";
	$MGHTML=$MGHTML."<td align=center rowspan=1 colspan=8 width=60%>Port Status(0-31)</td>";
	$MGHTML=$MGHTML."</tr>";
	
	//Table Content
	$PortStatusLabel=array(0=>"Offline",1=>"Online",2=>"--");
	$PortStatusColor=array(0=>"#FF0000",1=>"#0000FF",2=>"#CECECE");
	$insMg=explode(",",$MGInstanceNoList);
	$insPo=explode(",",$PortInstanceNoList);
	for($i=0;$i<$MGCount;$i++)
	{
		$insMg_tmp=$insMg[$i];
		$MGHTML=$MGHTML."<tr>";
		//MG ID
		$MGHTML=$MGHTML."<td rowspan=4>".$MG_ID[$insMg_tmp]."</td>";
		//Title
		$MGHTML=$MGHTML."<td rowspan=4>".$MG_Title[$insMg_tmp]."</td>";
		//Domain Name
		$MGHTML=$MGHTML."<td rowspan=4>".$MG_DomainName[$insMg_tmp]."</td>";
		//IP Address
		$MGHTML=$MGHTML."<td rowspan=4>".$MG_IP[$insMg_tmp]."</td>";
		
		//initial
		for($j=0;$j<32;$j++)
		{
			//no use
			$Port_Status_Show[$j]=2;
		}
		
		//get the state of ports for every MG
		for($j=0;$j<$PortCount;$j++)
		{
			$insPo_tmp=$insPo[$j];
			if($Port_MGIndex[$insPo_tmp]==$MG_ID[$insMg_tmp])
			{
				if($Port_UsedFlag[$insPo_tmp]==1)
					$Port_Status_Show[$Port_Index[$insPo_tmp]]=$Port_Status[$insPo_tmp];
				/*
				//in use
				if($Port_UsedFlag[$insPo_tmp]==1)
				{
					//ready
					if($Port_Status[$insPo_tmp]==1)
						$Port_Status_Show[$Port_Index[$insPo_tmp]]=1;
					//not ready
					else
						$Port_Status_Show[$Port_Index[$insPo_tmp]]=2;
				}
				//idle
				else
				{
					//do nothing
				}
				*/
			}
		}
		
		//show the status for ports
		for($j=0;$j<4;$j++)
		{			
			for($k=0;$k<8;$k++)
			{
				if(strlen($Port_Status_Show[$j*8+$k]) == 0)
					$Port_Status_Show[$j*8+$k]=2;
				$tmpColor=$PortStatusColor[$Port_Status_Show[$j*8+$k]];
				$tmpValue=$PortStatusLabel[$Port_Status_Show[$j*8+$k]];
				$MGHTML=$MGHTML."<td width=5%><font color=".$tmpColor.">".$tmpValue."&nbsp;</font></td>";
			}
			$MGHTML=$MGHTML."</tr>";
		}
	}
	$MGHTML=$MGHTML."</table>";
	
	/*********** Write the HTML to the div object ********************/
	echo "\n<script LANGUAGE=\"JavaScript\">";
	echo "\nparent.show_frame.document.getElementById('TrunkGroupTable').innerHTML=\"$TrunkGroupHTML\";";
	echo "\nparent.show_frame.document.getElementById('MediaGateTable').innerHTML=\"$MGHTML\";";
	echo "\n</script>"; 
	
?>
