<?php 
/*********************************************************      
*********************************************************/    
require("../../inc/header.inc");
$DEBUG	= 0;
$TEST	= ($TEST > 0)?$TEST:0;
$objIdStr = '2.2.6';
//value Type
$sysIdArr	= explode('_',$sysId);
$sysTypeNo	= $sysIdArr[0];
$sysNo		= $sysIdArr[1];
$subSysNo	= $sysIdArr[2];

$objDb		= 'OBJ_'.$sysTypeNo;
$paramTable = 'param_'.$sysNo;

$tmpArr = explode('-',$instanceNo);
$lac_inst = $tmpArr[0];
$lac_value = $tmpArr[1];

function omcGetObjDb($sysTypeNo){
	$dbName = 'OBJ_'.$sysTypeNo;
	return($dbName);
}

function omcGetObjTbl($sysTypeNo,$tableType){
	$dbName = 'OBJ_'.$sysTypeNo;
	$tableName = $dbName.'.paramConf';
	return($tableName);
}

//-----------------------

//=======================Get value From submit===================

if($set == 'Save')
{
	{//check if lac==0 or same lac.
		if($LAC==0)
		{
			echo "<script language='javascript'>";  
			echo "window.alert(\"LAC can't be zero!\");";	
			echo "history.go(-1)";
			echo "</script>";
			exit;
		}
		$oidstr = '2.2.6';
		$objWhereStr =getLevelWhere($oidstr);
		
		$objDb		= 'OBJ_'.$sysTypeNo;
		$paramTable = 'param_'.$sysNo;
		
		$paramSql = " SELECT initValue,readValue,instanceNo FROM $paramTable  
					WHERE $objWhereStr ORDER BY instanceNo
					";
		$Result = mysqli_query($pubConn,$paramSql);
		$n=0;
		while($Row = mysqli_fetch_array($Result))
		{
			$tempGp = hexdec(getBitValue($Row[initValue],'01.0-01.7'));
			$tempFlag = hexdec(getBitValue($Row[initValue],'0.0-0.7'));
			
			if( $tempFlag == 1 && $trunkgp == $tempGp)
			{
				$LAC_value[$n] = hexdec(getBitValue($Row[initValue],'02.0-03.7'));
				$LAC_inst[$n] = $Row[instanceNo];
				//$LAC_pageType[$n] = hexdec(getBitValue($Row[initValue],'4.0-4.7'));
				$n++;
			}
		}
		for($i=0; $i<$n; $i++)
		{
			if($LAC == $LAC_value[$i] && $lac_inst!=$LAC_inst[$i])
			{
				echo "<script language='javascript'>";  
				echo "window.alert(\"LAC can't be same to LAC:{$LAC_inst[$i]}!\");";	
				echo "history.go(-1)";
				echo "</script>";
				exit;
				break;
			}
		}
	}//end check lac
	if($lac_inst == 'new')
	{
		$objDb 			= omcGetObjDb($sysTypeNo);
		$objWhereStr =getLevelWhere($objIdStr);		
		$paramTable = 'param_99';
				
		$getInstanceSql="SELECT initValue,instanceNo FROM $paramTable
							WHERE  $objWhereStr AND initValue LIKE '00%'
							";
		$result=mysqli_query($pubConn,$getInstanceSql);	
		$row=mysqli_fetch_array($result);

		$row[initValue] = setBitValue($row[initValue],'0.0-0.7','01');
		if($DEBUG) echo "flag--->$row[initValue]<br>";										
		$tmp = dechex($LAC);
		$tmp = omc_keep_length($tmp,0,4);				
		$row[initValue] = setBitValue($row[initValue],'02.0-03.7',$tmp);
		$tempGp = dechex($trunkgp);
		$tempGp = omc_keep_length($tempGp,0,2);
		$row[initValue] = setBitValue($row[initValue],'01.0-01.7',$tempGp);	
		$tmpPageType = dechex($page_Type);
		$tmpPageType = omc_keep_length($tmpPageType,0,2);
		$row[initValue] = setBitValue($row[initValue],'4.0-4.7',$tmpPageType);				
		$row[initValue] = strtoupper($row[initValue]);
		$updSql = "UPDATE $paramTable SET initValue = '$row[initValue]'   
		WHERE $objWhereStr AND instanceNo = '$row[instanceNo]'
		";
		mysqli_query($pubConn,$updSql);
	}
	else
	{
		$objDb 			= omcGetObjDb($sysTypeNo);		
		$objWhereStr =getLevelWhere($objIdStr);
		$paramTable = 'param_99';		

		$paramSql = " SELECT initValue,readValue,instanceNo FROM $paramTable  
							WHERE $objWhereStr AND instanceNo='$lac_inst'
							";
		$result = mysqli_query($pubConn,$paramSql);
		$row = mysqli_fetch_array($result);
		$tmp = dechex($LAC);
		$tmp = omc_keep_length($tmp,0,4);
		$row[initValue] = setBitValue($row[initValue],'02.0-03.7',$tmp);
		
		$tmpPageType = dechex($page_Type);
		$tmpPageType = omc_keep_length($tmpPageType,0,2);
		$row[initValue] = setBitValue($row[initValue],'4.0-4.7',$tmpPageType);
		
		$row[initValue] = strtoupper($row[initValue]);
		$updSql = "UPDATE $paramTable SET initValue = '$row[initValue]'   
		WHERE $objWhereStr AND instanceNo = '$lac_inst'
		";
		mysqli_query($pubConn,$updSql);
		if($lac_value != $LAC )
		{
			if($lac_value != 0)//update cell parameter(原来lac不为0，且lac改变时，则对应的cell中的lac值作相应改变)
			{
				$oidEmergency = '2.2.7';
				$EM_objWhereStr =getLevelWhere($oidEmergency);
				$Em_paramSql = " SELECT initValue,readValue,instanceNo FROM $paramTable  
							WHERE $EM_objWhereStr ";
				if($DEBUG) echo "$Em_paramSql<br>";
				$Em_Result = mysqli_query($pubConn,$Em_paramSql);
				while($Em_Row = mysqli_fetch_array($Em_Result))
				{
					$lacValue=hexdec(getBitValue($Em_Row[initValue],'01.0-02.7'));
					if($lacValue==$lac_value)
					{
						$tmp = dechex($LAC);
						$tmp = omc_keep_length($tmp,0,4);				
						$Em_Row[initValue] = setBitValue($Em_Row[initValue],'01.0-02.7',$tmp);
						$setValue = strtoupper($Em_Row[initValue]);
						if($LAC==0)//如果设置lac为0，那么改变对应的cell为默认值
						{
							$defSql = " SELECT initValue FROM paramConf WHERE $EM_objWhereStr";
							$defRes = @mysqli_query($pubConn,$defSql);
							$defRow = @mysqli_fetch_array($defRes);
							$setValue = $defRow[initValue];
						}
						$updSql = "UPDATE $paramTable SET initValue = '$setValue'   
						WHERE $EM_objWhereStr AND instanceNo = '$Em_Row[instanceNo]'
						";
						mysqli_query($pubConn,$updSql);
						if($DEBUG) echo "$updSql<br>";
					}
				}					
			}			
			if($DEBUG) echo "$num -$LAC<br>";
		}
			
	}
	if($DEBUG)
	{
		echo "<br>debug";
	}
	else
	{//return page
		echo "<script language='javascript'>";  
		echo " location='$backurl';";  
		echo "</script>";
		exit();
	}

}//end of if($set == 1)

//=======================Show GUI ========================
?> 
<body>
<script LANGUAGE="JavaScript"> 
function checkInt(term){
    
    var checkOK = "0123456789";
    var checkStr = term.value;
    var allValid = true;
	var termName = term.name;
	var minValue = eval("document.confForm."+termName+"_min.value");
	var maxValue = eval("document.confForm."+termName+"_max.value");
	var oldValue = eval("document.confForm."+termName+"_old.value");
	//alert(minValue+'<'+term.value+'<'+maxValue);
    for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++){
			if (ch == checkOK.charAt(j))
				break;
		}
		if (j == checkOK.length){
			allValid = false;
			break;
		}
	}

	if (!allValid){  
		var errAlert="parameter Must be a dec number!";
		alert(errAlert);
		term.value=oldValue;
		term.focus();
		return (false);
	}

	if((minValue - term.value) > 0){
		var errAlert="Too little!";
		alert(errAlert);
		term.value=oldValue;
		term.focus();
		return (false);
	}
	if((term.value - maxValue) > 0){
		var errAlert="Too Big!";
		alert(errAlert);
		term.value=oldValue;
		term.focus();
		return (false);
	}

}
function checkInput(term,flag,maxValue){
	term.value = term.value.toUpperCase();
    
    if (flag == 16)
    {
    	var checkOK = "0123456789ABCDEF";
    	var showmsg ="Parameter must be a hexadecimal value";
    }
    else 
    {
    	var checkOK = "0123456789";
    	var showmsg ="Parameter must be a decimal value";
    }

    var checkStr = term.value;
    var allValid = true;
    for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++){
			if (ch == checkOK.charAt(j))
				break;
		}
		if (j == checkOK.length){
			allValid = false;
			break;
		}
	}
	
	if (!allValid){  
		alert(showmsg+"!");
		term.focus();
		term.value = 0;
		return (false);
	}
	if (maxValue != 0)
	{
		if (checkStr > maxValue)
		{
			showmsg = "Parameter must less then "+ maxValue;
			alert(showmsg+"!");
			term.value = 0;
			term.focus();
			return (false);
		}
	}
}
</script>

<form method="POST" name="confForm" action="<?php echo"$PHP_SELF";?>" >
<?

echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
	echo "Configuration > <a href=\"../overview/overview.php\">Network</a> > <a href=\"$backurl\">BSC</a> > <a href=\"#\">LAC</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%\">";
echo "&nbsp;&nbsp;";
echo "</td>";
echo "<td width=\"20%\">";
echo "<input type=\"submit\" name=\"set\" value=\"Save\" class=\"button\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";
echo "&nbsp;&nbsp;&nbsp;";
echo "<input type=\"reset\" name=\"Reset\" value=\"Undo\" class=\"button\" style=\"left: 1; top: 2; width: 60; height: 25; position: relative\">";
echo "</td>";
echo "<td width=\"10%\">";
echo "<p 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 "<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>";

echo "<tr>";
echo "<td width=\"50%\">LAC</td>";
if($lac_inst =='new'){
	echo "<td><input type=\"text\" style=width:\"$nShowsize\" maxLength =\"5\" name=\"LAC\" value=\"0\" class=\"text\" onchange = checkInput(this,10,65535)></td>";
}else{
	echo "<td><input type=\"text\" style=width:\"$nShowsize\" maxLength =\"5\" name=\"LAC\" value=\"$lac_value\" class=\"text\" onchange = checkInput(this,10,65535)></td>";	
}
echo "</tr>";

echo "<tr>";
echo "<td width=\"50%\">Page Type</td>";
$pageTypeArr=array(
			5 => 'by LAC',
			6 => 'in All Cell'
			);
echo "<td width=\"50%\">";
echo "<select name=\"page_Type\" size=\"1\" style=width:\"$nShowsize\">";
	foreach($pageTypeArr as $key => $value)
	{
		echo "<option value=\"$key\" ";
		if($key==$pageType) echo "selected";
		echo ">$value</option>";
	}
echo "</select>";
echo "</td>";
echo "</tr>";

echo "</table>";

echo "\n<input type=\"hidden\" name=\"linkset\" value=\"$linkset\">";
echo "\n<input type=\"hidden\" name=\"backurl\" value=\"$backurl\">";
echo "\n<input type=\"hidden\" name=\"trunkgp\" value=\"$trunkgp\">";
echo "\n<input type=\"hidden\" name=\"language\" value=\"$language\">";
echo "\n<input type=\"hidden\" name=\"setname\" value=\"$setname\">";
echo "\n<input type=\"hidden\" name=\"sysId\" value=\"$sysId\">";
echo "\n<input type=\"hidden\" name=\"lac_inst\" value=\"$lac_inst\">";
echo "\n<input type=\"hidden\" name=\"lac_value\" value=\"$lac_value\">";
echo "\n<input type=\"hidden\" name=\"pageType\" value=\"$pageType\">";
echo "\n<input type=\"hidden\" name=\"instanceNo\" value=\"$instanceNo\">";
echo "</Form>";
?>
</body>
</html>
