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

<?php

//*****
//$url_temp=$_SERVER["REQUEST_URI"];
//echo "This page URL:$url_temp<br>";
if($_SERVER['REQUEST_METHOD'] == "POST")
{
	if(isset($save_action) || isset($undo_action))
	{
		if(isset($save_action))
		{
			//============save the cell by the instanceNo=====================
			//========Emergency Call Flag
			if(!isset($emergency_call_flag_select))
				$emergency_call_flag_select=0;
			if($emergency_call_flag_select==0)
				$emergency_call_flag_bit="00";
			if($emergency_call_flag_select==1)
				$emergency_call_flag_bit="01";
			if($emergency_call_flag_select==2)
				$emergency_call_flag_bit="02";
			if($emergency_call_flag_select==3)
				$emergency_call_flag_bit="03";
			//========LAC
			$cell_lac_bit=dechex($cell_lac_select);
			$cell_lac_bit=omc_keep_length($cell_lac_bit,"0",4);
			//=========cell ID
			if(!isset($cell_id_text))
				$cell_id_text="0";
			$cell_id_bit=dechex($cell_id_text);
			$cell_id_bit=omc_keep_length($cell_id_bit,"0",4);
			//=========prefix index number
			//=====add a new prefix for emergency call
			if($cell_prefix_index_number_select==-1)
			{
				$database="OBJ_322";
				$sql="select initValue from paramConf where level_1=2 and level_2=1 and level_3=2 ";
				$result=@mysqli_query($pubConn,$sql);
				$rows=@mysqli_fetch_array($result);
				$prefix_default_value=$rows[initValue];
	
				$database="OBJ_322";
				$sql="select initValue,instanceNo from param_99 where level_1=2 and level_2=1 and level_3=2 ";
				$result=@mysqli_query($pubConn,$sql);
				$rows=@mysqli_fetch_array($result);
				$emergency_prefix_count=0;
				do
				{
					if($rows[initValue]==$prefix_default_value)
					{
						$cell_prefix_index_number_select=$rows[instanceNo];
						break;
					}
					
				}while($rows=@mysqli_fetch_array($result));
			}
			$cell_prefix_index_number_bit=dechex($cell_prefix_index_number_select);
			$cell_prefix_index_number_bit=omc_keep_length($cell_prefix_index_number_bit,"0",2);
			//=========local emergency 
			if(!isset($cell_local_emergency_number_text))
				$cell_local_emergency_number_text="0000000000000000";
			$cell_local_emergency_number_bit=trim($cell_local_emergency_number_text);
			$cell_local_emergency_number_bit=omc_keep_length($cell_local_emergency_number_bit,"E",16,0);
			//===regional zone flag
			if(!isset($cell_regional_zone_flag_select))
				$cell_regional_zone_flag_select=0;
			if($cell_regional_zone_flag_select==0)
				$cell_regional_zone_flag_bit="00";
			else if($cell_regional_zone_flag_select==1)
				$cell_regional_zone_flag_bit="01";
			//=======zone code
			if(!isset($cell_zone_code_text))
				$cell_zone_code_text="0";
			$cell_zone_code_bit=dechex($cell_zone_code_text);
			$cell_zone_code_bit=omc_keep_length($cell_zone_code_bit,"0",4);	
			
			$save_value=$emergency_call_flag_bit.$cell_lac_bit.$cell_id_bit.$cell_prefix_index_number_bit.$cell_local_emergency_number_bit.$cell_regional_zone_flag_bit.$cell_zone_code_bit;
			
			
			$database="OBJ_322";
			$setsql = "update OBJ_322.param_99 set initValue='$save_value'	where level_1=2 and level_2=2 and level_3=7 and instanceNo=$instanceNo ";
			if($DEBUG) 
				echo "setsql=$setsql<br>";	
			
			mysqli_query($pubConn,$setsql);
			echo mysqli_error($pubConn);
			

			$backurl="./wxc2_sp.php?instanceNo=$no_tmp&trunkgp=$trunkgp&linkset=$linkset&infoNo=$infoNo&operation=$oper&backurl=$url_temp";
			$cmd="window.location.href='$backurl';\n";
			echo "\n<script language=\"JavaScript\">\n";
			echo $cmd;
			echo "</script>\n";
		}
		if(isset($undo_action))
		{
			$backurl=urlencode($backurl);
			$url="./cell_edit.php?instanceNo=$instanceNo&trunkgp=$trunkgp&linkset=$linkset&infoNo=$infoNo&operation=$operation&backurl=$backurl";
			$cmd="window.location.href='$url';\n";
			echo "\n<script language=\"JavaScript\">\n";
			echo $cmd;
			echo "</script>\n";
		}
	}
}		










echo "<form method=\"POST\" name=\"saveForm\" action=\"$PHP_SELF\">";
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "Configuration > <a href=\"../overview/overview.php?scroll_y=1000\">Network</a> > <a href=$backurl>BSC</a> > <a href=#>Emergency Call</a>";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
//showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";



echo "<table border=0 width=100%>";
echo "<tr>";
echo "<td width=80% align=right>";
	echo "<input type=submit name=\"save_action\" value=\" Save \">";
echo "</td>";
echo "<td width=10% align=right>";
	echo "<input type=submit name=\"undo_action\" value=\" Undo \">";
echo "</td>";
echo "<td width=10%>";
  echo "<p align=\"right\"><a href=\"./wxc2_sp.php?infoNo=$infoNo&trunkgp=$trunkgp&linkset=$linkset\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
echo "</td>";
echo "</tr>";
echo "<table>";


//==============get the cell parameter by the instanceNo==================
			$database="OBJ_322";
			$sql="select initValue,instanceNo from param_99 where level_1=2 and level_2=2 and level_3=7 and instanceNo=$instanceNo ";
			$result=@mysqli_query($pubConn,$sql);
			$rows=@mysqli_fetch_array($result);
			
			$cell_instance_no=$rows[instanceNo];
			$cell_emergency_call_flag=hexdec(getBitValue($rows[initValue],'0.0-0.7'))+0;
			$cell_lac=hexdec(getBitValue($rows[initValue],'1.0-2.7'))+0;
			$cell_id=hexdec(getBitValue($rows[initValue],'3.0-4.7'))+0;
			$cell_prefix_index_number=hexdec(getBitValue($rows[initValue],'5.0-5.7'))+0;
			$cell_local_emergency_number=getBitValue($rows[initValue],'6.0-13.7');
			$cell_regional_zone_flag=hexdec(getBitValue($rows[initValue],'14.0-14.7'))+0;
			$cell_zone_code=hexdec(getBitValue($rows[initValue],'15.0-16.7'))+0;
//==============show the parameter======================================
echo "<table border=\"1\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";	
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td width=\"50%\"><b>Parameters</b></td>";
echo "<td><b>Value</b></td>";
echo "</tr>";

//===instanceNo=======
if($operation=="new")
	echo "<tr style=\"display:none\">";
else if($operation=="edit")
	echo "<tr>";
echo "<td>No.</td>";
echo "<td>";
echo "{$cell_instance_no}";
echo "</td>";
echo "</tr>";

//===emergency call flag
$emergency_call_flag_label=array(0=>"Disable",1=>"By Cell",2=>"By LAC",3=>"By MSC");
if($operation=="new")
	echo "<tr style=\"display:none\">";
else if($operation=="edit")
	echo "<tr>";

echo "<td>Emergency Call Flag</td>";
echo "<td>";
echo "<select name=\"emergency_call_flag_select\" style=\"width:30%\">";
	for($i=0;$i<4;$i++)
	{
		if($i==$cell_emergency_call_flag)
			echo "<option value=$i selected>{$emergency_call_flag_label[$i]}</option>";
		else
			echo "<option value=$i>{$emergency_call_flag_label[$i]}</option>";	
	}
echo "</select>";
echo "</td>";
echo "</tr>";

//===LAC
//===get the exist LAC

//=====get the default value of lac=================
			$database="OBJ_322";
			$sql="select initValue from paramConf where level_1=2 and level_2=2 and level_3=6 ";
			$result=@mysqli_query($pubConn,$sql);
			$rows=@mysqli_fetch_array($result);
			$lac_default_value=$rows[initValue];

			$database="OBJ_322";
			$sql="select initValue,instanceNo from param_99 where level_1=2 and level_2=2 and level_3=6 ";
			$result=@mysqli_query($pubConn,$sql);
			$rows=@mysqli_fetch_array($result);
			$lac_count=0;
			do
			{
				if($rows[initValue]==$lac_default_value)
					continue;
				else
				{
					$lac_instance_no[$lac_count]=$rows[instanceNo];
					$lac_flag[$lac_count]=hexdec(getBitValue($rows[initValue],'0.0-0.7'))+0;
					$lac_tg[$lac_count]=hexdec(getBitValue($rows[initValue],'1.0-1.7'))+0;
					$lac_lac[$lac_count]=hexdec(getBitValue($rows[initValue],'2.0-3.7'))+0;
					$lac_paging_type[$lac_count]=hexdec(getBitValue($rows[initValue],'4.0-4.7'))+0;
					$lac_count++;
				}	
			}while($rows=@mysqli_fetch_array($result));

echo "<tr>";
echo "<td>LAC</td>";
echo "<td>";
	echo "<select name=\"cell_lac_select\" style=\"width:30%\">";
		for($i=0;$i<$lac_count;$i++)
		{
			if($cell_lac==$lac_lac[$i])
				echo "<option value={$lac_lac[$i]} selected>{$lac_lac[$i]}</option>";
			else
				echo "<option value={$lac_lac[$i]}>{$lac_lac[$i]}</option>";
		}
	echo "</select>";
echo "</td>";
echo "</tr>";

//===Cell ID
if($operation=="new")
	echo "<tr style=\"display:none\">";
else if($operation=="edit")
	echo "<tr>";
echo "<td>Cell ID</td>";
echo "<td>";
echo "<input type=text name=\"cell_id_text\" maxlength=5 style=\"width:30%\" value=$cell_id>";
echo "</td>";
echo "</tr>";

//===prefix index number
//=====get the default value of prefix=================
			$database="OBJ_322";
			$sql="select initValue from paramConf where level_1=2 and level_2=1 and level_3=2 ";
			$result=@mysqli_query($pubConn,$sql);
			$rows=@mysqli_fetch_array($result);
			$prefix_default_value=$rows[initValue];

			$database="OBJ_322";
			$sql="select initValue,instanceNo from param_99 where level_1=2 and level_2=1 and level_3=2 ";
			$result=@mysqli_query($pubConn,$sql);
			$rows=@mysqli_fetch_array($result);
			$emergency_prefix_count=0;
			do
			{
				if($rows[initValue]==$prefix_default_value)
					continue;
				else
				{
					$prefix_call_type=hexdec(getBitValue($rows[initValue],'10.4-10.6'))+0;
					if($prefix_call_type==1)
					{
						$prefix_instance_no[$emergency_prefix_count]=$rows[instanceNo];
						$temp_no=$rows[instanceNo];
						{
							$databasee="OBJ_322";
							$sqll="select initValue,instanceNo from param_99 where level_1=2 and level_2=1 and level_3=1 and instanceNo=$temp_no ";
							$resultt=@mysqli_query($pubConn,$sqll);
							$rowss=@mysqli_fetch_array($resultt);
							
							$cell_prefix_prefix[$emergency_prefix_count]=str_replace("E","",$rowss[initValue]);
							if($cell_prefix_prefix[$emergency_prefix_count]=="")
								$cell_prefix_prefix[$emergency_prefix_count]="--";
						}
						$emergency_prefix_count++;
					}
					
				}	
			}while($rows=@mysqli_fetch_array($result));

echo "<tr>";
echo "<td>Prefix Index Number</td>";
echo "<td>";
echo "<select name=\"cell_prefix_index_number_select\" style=\"width:30%\">";
	for($i=0;$i<$emergency_prefix_count;$i++)
	{
		if($cell_prefix_index_number==$prefix_instance_no[$i])
			echo "<option value={$prefix_instance_no[$i]} selected>{$cell_prefix_prefix[$i]}</option>";
		else
			echo "<option value={$prefix_instance_no[$i]}>{$cell_prefix_prefix[$i]}</option>";
	}
	//======add New selection
	if($operation=="new")
		echo "<option value=\"-1\">New</option>";
echo "</select>";
echo "</td>";
echo "</tr>";

//===local emergency number
if($operation=="new")
	echo "<tr style=\"display:none\">";
else if($operation=="edit")
	echo "<tr>";
echo "<td>Local Emergency Number</td>";
echo "<td>";
$tmp=str_replace("E","",$cell_local_emergency_number);
echo "<input type=text name=\"cell_local_emergency_number_text\" maxlength=16 style=\"width:30%\" value=$tmp>";
echo "</td>";
echo "</tr>";

//===regional zone flag
$cell_regional_zone_flag_label=array(0=>"Disable",1=>"Enable");
if($operation=="new")
	echo "<tr style=\"display:none\">";
else if($operation=="edit")
	echo "<tr>";
echo "<td>Regional Zone Flag</td>";
echo "<td>";
echo "<select name=\"cell_regional_zone_flag_select\" style=\"width:30%\">";
	for($i=0;$i<2;$i++)
	{
		if($i==$cell_regional_zone_flag)
			echo "<option value=$i selected>{$cell_regional_zone_flag_label[$i]}</option>";
		else
			echo "<option value=$i>{$cell_regional_zone_flag_label[$i]}</option>";
	}
echo "</select>";
echo "</td>";
echo "</tr>";

//===zone code
if($operation=="new")
	echo "<tr style=\"display:none\">";
else if($operation=="edit")
	echo "<tr>";
echo "<td>Zone Code</td>";
echo "<td>";
echo "<input type=text name=\"cell_zone_code_text\" maxlength=5 style=\"width:30%\" value=$cell_zone_code>";
echo "</td>";
echo "</tr>";


echo "<input type=hidden name=\"backurl\" value=$backurl>";
echo "<input type=hidden name=\"instanceNo\" value=$instanceNo>";
echo "<input type=hidden name=\"infoNo\" value=$infoNo>";
echo "<input type=hidden name=\"trunkgp\" value=$trunkgp>";
echo "<input type=hidden name=\"linkset\" value=$linkset>";
echo "<input type=hidden name=\"operation\" value=$operation>";
echo "</form>";			
?>
</body>
