<?php
require("../sub_inc/commfunc.php");
$helpId = "";
$DEBUG = 0;
$leftWidth = '20%';
$pubDb	 = 'OMC_PUB';
$sysInfoTab = 'sysInfo';

//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];
}	
//echo "<br>cardNo:$cardNo";
$tmpArr = explode('-',$cardNo);
$sysNo = $tmpArr[0];
$subSysNo = $tmpArr[1];

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

//echo "sysNo:$sysNo<hr>";
//=============================================================
if(!isset($plat_select))
{
	$plat_label=array(0=>"CSC-0",1=>"CSC-1");
	$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)
		$plat_select=1;
	else
		$plat_select=0;	
	
}
$plat_select=$plat_select+0;
//echo "plat select:$plat_select";

$sysNo=$plat_select;
$k8e1Table 	 = 'param_' . $sysNo;
$k8e1Db		 = "OBJ_111";

$sysId = '111_' . $sysNo . '_99';
$slctSysId = "111_$sysNo"."_$subSysNo";
//echo "<br>$sysId,$slctSysId";

/*
$sysId_arr = array(
			array($sysId, array('2.7'), '8ke1',$slctSysId)
		);
*/

$sysId_arr=array(
	array('oid'=>'2.7',			'sysid'=> $sysId,			'selectsysid'=>$slctSysId), 										
);
$toneMapArr = array(
				"0" => "Ringback tone",
				"1" => "Busy tone",
				"2" => "Unavailable tone",
				"3" => "Congestion tone",
				"4" => "Proceeding tone",
				"5" => "Warning tone",
				//"6" => "Call hold tone",
				);
$toneArray	= array(
				"Called number invalid ",
				"Called number not activated",
				"Subscriber busy",
				"Network busy",
				"Caller party deactivated",
				"Called party deactivated",
				"Feature unavailable",
				"Called party unavailable",
				"Phone ringing",
				"Warning before termination",
				"Warning after termination",
				"Reserve",
				"Reserve",
				"Reserve",
				"Reserve",
				"Call hold"
				);
				
$ToneSettingArr = array(
			array("oid" => "2.7.1", "remark" => "Ringback"),
			array("oid" => "2.7.2", "remark" => "Busy"),
			array("oid" => "2.7.3", "remark" => "Unavailable"),
			array("oid" => "2.7.4", "remark" => "Congestion"),
			array("oid" => "2.7.5", "remark" => "Proceeding"),
			array("oid" => "2.7.6", "remark" => "Warning tone"),
			array("oid" => "2.7.7", "remark" => "Call hold tone")
		);
$toneOffsetArr = array(
			array("offset" => "00.0-00.7",	"len" => '2' , "min" => 0,	"max" => 31),
			array("offset" => "01.0-01.7",	"len" => '2', "min" => 1,	"max" => 128),
			array("offset" => "02.0-17.7",	"len" => '32')
		);
?>

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

<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
	{//update announcement
		$oidStr = '2.7.9';
		$levelWhere = getLevelWhere($oidStr);
		$selSql = "select initValue from $k8e1Table where $levelWhere";
		$Result = mysqli_query($pubConn,$selSql);
		$Rows 	= mysqli_fetch_array($Result);
		$setValue = $Rows[initValue];
		for($i=0; $i< sizeof($toneArray); $i++)
		{
			$offset = "$i.0-$i.7";
			$hexValue = omc_keep_length(dechex(${"failureTone_$i"}),'0','2');
			$setValue = setBitValue($setValue,$offset,$hexValue);
		}
		$updSql = "update $k8e1Table set initValue='$setValue' where $levelWhere";
		mysqli_query($pubConn,$updSql);
	}//end update announcement
	
	{//update tone
		for($i=0; $i< sizeof($ToneSettingArr); $i++)
		{
			$levelWhere = getLevelWhere($ToneSettingArr[$i][oid]);
			$selSql = "select initValue from $k8e1Table where $levelWhere";
			$Result = mysqli_query($pubConn,$selSql);
			$Rows 	= mysqli_fetch_array($Result);
			$setValue = $Rows[initValue];
			for($j=0;$j< sizeof($toneOffsetArr); $j++)
			{
				$len = $toneOffsetArr[$j][len];
				$offset = $toneOffsetArr[$j][offset];
				
				$tmp_name = "type_".$i."_".$j;
				$tmpValue = $$tmp_name;
				
				if($j == 2)
					$tmpValue = omc_keep_length($tmpValue,'0',$len,0);
				else	
					$tmpValue = omc_keep_length(dechex($tmpValue),'0',$len);
				$setValue = setBitValue($setValue,$offset,$tmpValue);
			}
			$setValue = strtoupper($setValue);
			$updSql = "update $k8e1Table set initValue='$setValue' where $levelWhere";
			mysqli_query($pubConn,$updSql);
		}
	}//end 
	
	{//DTMF
		$levelWhere = getLevelWhere('2.7.10');
		$selSql = "select initValue from $k8e1Table where $levelWhere";
		$Result = mysqli_query($pubConn,$selSql);
		$Rows 	= mysqli_fetch_array($Result);
		$setValue = $Rows[initValue];
		$mark = omc_keep_length(dechex($mark),'0','2');
		$space = omc_keep_length(dechex($space),'0','2');
		$setValue = setBitValue($setValue,'0.0-0.7',$mark);
		$setValue = setBitValue($setValue,'1.0-1.7',$space);
		$updSql = "update $k8e1Table set initValue='$setValue' where $levelWhere";
		mysqli_query($pubConn,$updSql);
		
	}//END DTMF
	
	if(isset($setSys) || isset($getSys))
	{
		if(isset($setSys))
		{
			echo "<br>Setting parameters<br>";
			//print_r($sysId_arr);
			echo "plat_select";			
			if($plat_select==0)
				$sysId_arr_set=array(
						array('oid'=>'2.7',			'sysid'=> '111_0_99',			'selectsysid'=>'-1'),
				);
			else	
				$sysId_arr_set=array(
						array('oid'=>'2.7',			'sysid'=> '111_1_99',			'selectsysid'=>'-1'),
				);	
			//SetParam($sysId_arr);	
			SetParam($sysId_arr_set);	
			session_register("SetFlag");

		}
		else
		{ 
			echo "<br>Getting parameters<br>";
			session_register("GetFlag");
			//print_r($sysId_arr);
			if($plat_select==0)
				$sysId_arr_get=array(
						array('oid'=>'2.7',			'sysid'=> '111_0_99',			'selectsysid'=>'-1'),
				);
			else	
				$sysId_arr_get=array(
						array('oid'=>'2.7',			'sysid'=> '111_1_99',			'selectsysid'=>'-1'),
				);	
			//GetParam($sysId_arr);
			GetParam($sysId_arr_get);
			$showType="Get";
		}
	}
	echo "<script LANGUAGE=\"JavaScript\">";
	if(!$DEBUG)
		echo "\n window.location.href=\"./toneAnnounce.php?backurl=$backurl&cardNo=$cardNo&plat_select=$plat_select\";";
	echo "\n </script>";	
	exit();	
}

echo "<form method=\"POST\" name=\"setForm\" action=\"$PHP_SELF?backurl=$backurl&cardNo=$cardNo&plat_select=$plat_select\">";

{
	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=\"#\">Tone</a>";
	echo "</td>";
	echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
	$helpId='2.14';
	//showHelp($helpId);
	echo "</td>";
	echo "</tr>";
	echo "</table>";
	echo "<br>";
	
	
	
	echo "<table width=\"100%\">";
	echo "<tr><td width=\"60%\">";
	/*
	{//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
	*/
	//===select a plat no==========================
	//==================add the CSC plat selection===========================
	//===================wether the system have csc-0======
	$plat_label=array(0=>"CSC-0",1=>"CSC-1");
	$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)
		$plat1_plat2[0]=0;
	else
		$plat1_plat2[0]=1;
		
	$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_plat2[1]=0;
	else
		$plat1_plat2[1]=1;
		
	//print_r($plat1_plat2);
	echo "<select name=\"plat_select\" onChange=\"javascript:change_plat(this)\">";
		for($i=0;$i<2;$i++)
		{
			if($plat1_plat2[$i]==1)
			{
				if($sysNo==$i)
					echo "<option value=$i selected>{$plat_label[$i]}";
				else
				  echo "<option value=$i>{$plat_label[$i]}";	
				echo "</option>";
			}	
		}
	echo "</select>";
	//=========================================================================	
	//=============================================
	echo "</td>";
	echo "<td width=\"30%\">";
		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><p width=\"10%\" 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
echo "<table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
{//Announcement
	echo "<tr>";
	echo "<td width=\"$leftWidth\" bgcolor=\"#E6E6E6\"><b>Tone Usage</b></td>";
	echo "<td>";
	echo "<table width=\"100%\" cellpadding=\"1\" cellspacing=\"0\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td>No.</td>";
		echo "<td>Call failure</td>";
		echo "<td>Tone</td>";
		echo "</tr>";
		$oidStr = '2.7.9';
		$levelWhere = getLevelWhere($oidStr);
		$selSql = "select initValue from $k8e1Table where $levelWhere";
		if($DEBUG) echo "<br>Line ".__LINE__.": ".$selSql;
		$Result = mysqli_query($pubConn,$selSql); 
		$Rows 	= mysqli_fetch_array($Result);
		for($i=0; $i< sizeof($toneArray); $i++)
		{
			$offset = "$i.0-$i.7";
			$toneValue = hexdec(getBitValue($Rows[initValue],$offset));
			if($i>=11&&$i<=14)
				echo "<tr style=\"display:none\" onMouseOver=\"this.bgColor='#D2F0FF'\" onMouseOut=\"this.bgColor='#ffffff'\">";
			else
				echo "<tr onMouseOver=\"this.bgColor='#D2F0FF'\" onMouseOut=\"this.bgColor='#ffffff'\">";
				
				echo "<td>$i</td>";
				echo "<td>{$toneArray[$i]}</td>";
				echo "<td>";
					echo "<select size=\"1\" value=\"$toneValue\" name=\"failureTone_$i\">";			
					while (list ($key, $val) = each ($toneMapArr)) 
					{
						if($i < 9 && $key > 3)
							continue;
						echo "<option value=\"$key\" ";
						if($key == $toneValue)
							echo "selected";
						echo ">$val</option>";
					}
					if($i==15)
					{
						if($toneValue==6)
							echo "<option value=6 selected>Call hold tone</option>";
						else
							echo "<option value=6>Call hold tone</option>";	
					}
					reset($toneMapArr);
				echo "</td>";
			echo "</tr>";
		}
	echo "</table></td>";
	echo "</tr>";
}//end Announcement

{//Tone
	echo "<tr>";
	echo "<td width=\"$leftWidth\" bgcolor=\"#E6E6E6\"><b>Tone Setup</b></td>";
	echo "<td>";
	echo "<table width=\"100%\" cellpadding=\"1\" cellspacing=\"0\">";
		echo "<tr bgcolor=\"#E6E6E6\">";
		echo "<td width=\"20%\">Type</td>";
		echo "<td width=\"15%\">Channel</td>";
		echo "<td width=\"15%\">Cycle(<font style=\"font-family:monospace\">*</font>50ms)</td>";
		echo "<td width=\"50%\">Cadence</td>";
		echo "</tr>";
		for($i=0; $i< sizeof($ToneSettingArr); $i++)
		{
			echo "<tr>";
			$levelWhere = getLevelWhere($ToneSettingArr[$i][oid]);
			$selSql = "select initValue from $k8e1Table where $levelWhere";
			$Result = mysqli_query($pubConn,$selSql);
			$Rows 	= mysqli_fetch_array($Result);
			echo "<td>{$ToneSettingArr[$i][remark]}</td>";
			for($j=0;$j< sizeof($toneOffsetArr); $j++)
			{
				if($j==2)
				{
					$tmp_value = explode('000',getBitValue($Rows[initValue],$toneOffsetArr[$j][offset]));
					$tmp_value = $tmp_value[0];
					if(strlen($tmp_value)%2)
						$tmp_value .= '0';
				}
				else
					$tmp_value = hexdec(getBitValue($Rows[initValue],$toneOffsetArr[$j][offset]));
				$len = $toneOffsetArr[$j][len]+1;
				$size=$len + 2;
				$tmp_name = "type_".$i."_".$j;
				echo "<td>";
				echo "<input type=\"text\" name=\"$tmp_name\" value=\"$tmp_value\" size=\"$size\" maxlength=\"$len\">";
				echo "</td>";
			}
			echo "</tr>";
		}
	echo "</table></td>";
	echo "</tr>";
}//end Tone

{//DTMF
	$levelWhere = getLevelWhere('2.7.10');
	$selSql = "select initValue from $k8e1Table where $levelWhere";
	$Result = mysqli_query($pubConn,$selSql);
	$Rows 	= mysqli_fetch_array($Result);
	$mark = hexdec(getBitValue($Rows[initValue],'0.0-0.7'));
	$space = hexdec(getBitValue($Rows[initValue],'1.0-1.7'));
	echo "<tr>";
		echo "<td bgcolor=\"#E6E6E6\"><b>DTMF</b></td>";
		echo "<td>";
			echo "<table width=\"100%\" cellpadding=\"1\" cellspacing=\"0\">";
			echo "<tr>";
			echo "<td width=\"10%\" bgcolor=\"#E6E6E6\">Mark:</td>";
			echo "<td width=\"40%\" ><input type=\"text\" name=\"mark\" value=\"$mark\" size=\"4\" maxlength=\"3\"></td>";
			echo "<td width=\"10%\" bgcolor=\"#E6E6E6\">Space:</td>";
			echo "<td width=\"40%\" ><input type=\"text\" name=\"space\" value=\"$space\" size=\"4\" maxlength=\"3\"></td>";
			echo "</tr>";
			echo "</table>";
		echo "</td>";
	echo "</tr>";
}//END DTMF
echo "</table";
echo "<input type=\"hidden\" name=\"backurl\" value=\"$backurl\">";
echo "<input type=\"hidden\" name=\"cardNo\" value=\"$cardNo\">";	

?>
<script LANGUAGE="JavaScript">
function select8ke1Card(item)
{
	var base_url,url_add,url; 
	base_url='<?php echo"$PHP_SELF?&backurl=$backurl&cardNo=";?>';
	url_add=item.value;
	url=base_url+url_add;
	window.location.href=url; 
}
function change_plat(item)
{
	
	var base_url,url_add,url; 
	var plat_no=item.value;
	url_add=plat_no;
	
	base_url='<?php echo"$PHP_SELF?&backurl=$backurl&plat_select=";?>';
	url=base_url+url_add;
	window.location.href=url; 
}
</script>
</form>
</body>
