<?php
/***************************************************************
File Name: sgConf.php
File Relation: sgList.php
File Include:  wxc2_omc/inc/header.inc
Function: Configure the sigling gateway parameter
Global Variable: 
Global function: 
Author: JianHui Zheng
Date: 2008-06-16

----- Modify -----
Description: 
***************************************************************/
	require("../../inc/header.inc");
?>

<?php
	//Global
	$sysId="222_99_99";
	$listPageUrl = "sgList.php?setObjIdStr=$objIdStr&setInstanceNo=$instanceNo";
	if(0) echo "instanceNo=$instanceNo,objIdStr=$objIdStr<br>";
	
	$sgTableConfArr=array(
		array('SG Flag','00.0-00.7'),
		array('Application Mode','01.0-01.7'),
		array('Ack Option','02.0-02.0'),
		array('Signaling Type','02.1-02.7'),
		array('Remote IP','03.0-06.7')
	);

	$statusLabel=array(0=>"Disable",1=>"Enable");
	$serverLabel=array(0=>"Client",1=>"Server");
	$ackLabel=array(0=>"Allow",1=>"Forbid");
	$typeLabel=array(0=>"M2UA",1=>"IUA",2=>"M3UA");
	
	//Get the default value
	$OBJ_222="OBJ_222";
	$defSql = " SELECT initValue FROM paramConf WHERE keyId > 0 AND level_1 = 2 AND level_2 = 9 AND paramType=2";
	$defRes = @mysqli_query($pubConn,$defSql);
	$defRow = @mysqli_fetch_array($defRes);
	$defaultValue = $defRow[initValue];
	//echo "<br>defaultValue=$defaultValue<br>";
	
	if($_SERVER['REQUEST_METHOD'] == "POST")
	{
		//echo "instanceNo=$instanceNo,status=$status,server=$server,ack=$ack,typr=$type,ip=$ip<br>";
		$status=omc_keep_length($status,"0","2","1");
		$server=omc_keep_length($server,"0","2","1");
		$ip=omc_keep_length($ip,"0","8","1");
		$ip=ipDec2Hex($ip);
		//echo "instanceNo=$instanceNo,status=$status,server=$server,ack=$ack,typr=$type,ip=$ip<br>";
		$totalValue=$status.$server.$ack.$type.$ip;
		$totalValue="00000000000000";
		$totalValue=setBitValue($totalValue,$sgTableConfArr[0][1],$status);
		$totalValue=setBitValue($totalValue,$sgTableConfArr[1][1],$server);
		$totalValue=setBitValue($totalValue,$sgTableConfArr[2][1],$ack);
		//echo "type=$type,{$sgTableConfArr[3][1]},totalValue=$totalValue<br>";
		$totalValue=setBitValue($totalValue,$sgTableConfArr[3][1],decbin($type));
		$totalValue=setBitValue($totalValue,$sgTableConfArr[4][1],$ip);
		//echo "type=$type,{$sgTableConfArr[3][1]},totalValue=$totalValue<br>";
		//exit;
		$levelArr=explode('.',$objIdStr);
		for($j=0;$j<sizeof($levelArr);$j++)
		{
				$levelNo=$j+1;
				${"level_$levelNo"}=$levelArr[$j];
				if($DEBUG) echo "<BR>levelArr[$j]={$levelArr[$j]}";
		}
	
		$updSql="UPDATE OBJ_222.param_99 SET initValue = '$totalValue' WHERE level_1='$level_1' AND level_2='$level_2'  AND instanceNo='$instanceNo' ";
		if($DEBUG) echo "<BR>updSql=$updSql";
		mysqli_query($pubConn,$updSql);
		echo mysqli_error($pubConn);
		
		echo "<script LANGUAGE=\"JavaScript\">";
		echo "\n window.opener.location.href=\"$listPageUrl\";";
		if(!$DEBUG){
			echo "\n window.close();";
		} 
		echo "\n </script>";
		exit();
		
	}
	
	
	//Get the parameter
	//$value,$instanceNoList,instanceNoCount
	$levelArr=explode('.',$objIdStr);
	for($j=0;$j<sizeof($levelArr);$j++)
	{
		$levelNo=$j+1;
		${"level_$levelNo"}=$levelArr[$j];
		if($DEBUG) echo "<BR>levelArr[$j]={$levelArr[$j]}";
	}
	if($instanceNo == 'new')
	{
		$value=$defaultValue;	
	}
	else
	{
		$getSql="SELECT initValue FROM OBJ_222.param_99 WHERE level_1='$level_1' AND level_2='$level_2' AND instanceNo='$instanceNo' ";
		if($DEBUG) echo "<BR>getSql=$getSql";
		$result	=mysqli_query($pubConn,$getSql);
		echo mysqli_error($pubConn);
		$rows	=mysqli_fetch_array($result);
		echo mysqli_error($pubConn);
		$value=$rows[0];
	}
	
	if($instanceNo == 'new')
	{	
		$getInstanceSql="SELECT instanceNo FROM OBJ_222.param_99 WHERE level_1='$level_1' AND level_2='$level_2' AND initValue ='$defaultValue'"; 
		if($DEBUG) echo "<br>$getInstanceSql";
		$result=mysqli_query($pubConn,$getInstanceSql);
		echo mysqli_error($pubConn);
		$instanceNoCount=0;
		while($row=mysqli_fetch_array($result))
		{
			$instanceNoList[$instanceNoCount++]=$row[instanceNo];
			//echo "<option value=$row[instanceNo]>$row[instanceNo]</option>";
		}
	}
	
	$sgStatus=getBitValue($value,$sgTableConfArr[0][1])+0;
	$sgServer=getBitValue($value,$sgTableConfArr[1][1])+0;
	$sgACK=getBitValue($value,$sgTableConfArr[2][1])+0;
	$sgType=bindec(getBitValue($value,$sgTableConfArr[3][1]))+0;
	$sgIP=getBitValue($value,$sgTableConfArr[4][1]);
	$sgIP=ipHex2Dec($sgIP);
	if($DEBUG) echo "instanceNo=$instanceNo,status=$sgStatus,server=$sgServer,ack=$sgACK,type=$sgType,ip=$sgIP<br>";
	
	
	
	//show the page
	echo "<br><body topmargin=\"0\" leftmargin=\"12\">";
	echo "<form method=\"POST\" name=\"selectform\" action=\"$PHP_SELF\">";
	echo "<table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\"><td>Name</td><td>Value</td></tr>";

	//instanceNo
	echo "<tr>";
	if($instanceNo == 'new')
	{
		echo "<td>No.</td>";
		echo "<td><select name=\"instanceNo\">";
		for($i=0;$i<$instanceNoCount;$i++)
		{
			echo "<option value={$instanceNoList[$i]}>{$instanceNoList[$i]}</option>";
		}
		echo "</select></td>";
	}
	else
	{
		echo "<td>No.</td>";
		echo "<td>$instanceNo</td>";
		echo "<input type=\"hidden\" name=\"instanceNo\" value=$instanceNo>";
	}
	echo "</tr>";
	
	//SG Flag
	echo "<tr>";
	echo "<td>SG Flag</td>";
	echo "<td>";
	echo "<select name=\"status\">";
	for($i=0;$i<sizeof($statusLabel);$i++)
	{
		if($sgStatus == $i)
			echo "<option value=$i selected>{$statusLabel[$i]}</option>";
		else
			echo "<option value=$i>{$statusLabel[$i]}</option>";	
	}
	echo "</select>";
	echo "</td>";
	echo "</tr>";
	
	//server
	echo "<tr>";
	echo "<td>Application Mode</td>";
	echo "<td>";
	echo "<select name=\"server\">";
	for($i=0;$i<sizeof($serverLabel);$i++)
	{
		if($sgServer == $i)
			echo "<option value=$i selected>{$serverLabel[$i]}</option>";
		else
			echo "<option value=$i>{$serverLabel[$i]}</option>";
	}
	
	//ACK
	echo "<tr>";
	echo "<td>Ack Option</td>";
	echo "<td>";
	echo "<select name=\"ack\">";
	for($i=0;$i<sizeof($ackLabel);$i++)
	{
		if($sgACK == $i)
			echo "<option value=$i selected>{$ackLabel[$i]}</option>";
		else
			echo "<option value=$i>{$ackLabel[$i]}</option>";
	}
	
	//Signaling Type
	echo "<tr>";
	echo "<td>Signaling Type</td>";
	echo "<td>";
	echo "<select name=\"type\">";
	for($i=0;$i<sizeof($typeLabel);$i++)
	{
		if($sgType == $i)
			echo "<option value=$i selected>{$typeLabel[$i]}</option>";
		else
			echo "<option value=$i>{$typeLabel[$i]}</option>";
	}
	echo "</tr>";
	
	//ip
	echo "<tr>";
	echo "<td>Remote IP</td>";
	echo "<td>";
	echo "<input type=\"text\" name=\"ip\" value=$sgIP maxLength=16 length=12 class=\"text\">";
	echo "</td>";
	echo "</tr>";
	echo "</table>";
	
	echo "<br>";
	echo "\n <input type=\"hidden\" name=\"sysId\" value=\"$sysId\">";
	echo "\n <input type=\"hidden\" name=\"objIdStr\" value=\"$objIdStr\">";
	if($instanceNo != 'new')
	{
		echo "\n <input type=\"hidden\" name=\"instanceNo\" value=\"$instanceNo\">";
	}
	echo "\n <input type=\"submit\" name=\"set\" value=\"Set\">&nbsp;&nbsp;";
	echo "\n <input type=\"reset\" name=\"reset\" value=\"Reset\">";
	
	echo "</form>";
	echo "</body>";
	
	//function define
	function ipHex2Dec($ipHex)
	{
		$ipDec="";
		for($i=0;$i<4;$i++)
		{
			$temp=substr($ipHex,$i*2,2);
			$temp=hexdec($temp);
			$ipDec.=$temp.".";
		}
		$ipDec=substr($ipDec,0,strlen($ipDec)-1);
		return $ipDec;
	}
	

	function ipDec2Hex($ipDec)
	{
		$ipHex="";
		$unit=explode(".",$ipDec);
		for($i=0;$i<4;$i++)
		{
			$temp=dechex($unit[$i]);
			$temp=omc_keep_length($temp,"0","2","1");
			$ipHex.=$temp;
		}
		return strtoupper($ipHex);
	}

?>