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

<?
$settedColor="blue";
$notSettedColor="#008000";
$inputBkgrdColor = "#E7FEFD";
$saveBkgrdColor="#F3F77D";
$waitingBkgrdColor = "red";

function getParamValue(&$titleStr,&$IniEquRead,$sysId,$oidStr,$order,$instanceNo=0,$otherWhereStr='')
{
	global $language;
	//$DEBUG=1;
	$sysIdArr	= explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];	
	$objDb		= 'OBJ_'.$sysTypeNo;
	$paramTable = 'param_'.$sysNo;
	
	$sysConfTable = 'OMC_PUB.sysConf';	
	$pubDb			='OMC_PUB';
	$paramConfTable	= $objDb.".paramConf";
	
	$maxLevelNum	= 10;
	$nInteger = 2;
	$nOctStr = 4;
	
	if($language == 'eng'){
		$objNameField=name_2;
		$objSetField=setTemplate_2 ; //语种2
	}else{	
		$objNameField=name_1;
		$objSetField=setTemplate_1 ; //语种1(默认语种)
	}

	$objWhereStr = getLevelWhere($oidStr);
	
	$objSql = "SELECT $objNameField,$objSetField,valueType,length,minValue,maxValue FROM $paramConfTable WHERE $objWhereStr";
	$objResult	= mysqli_query($pubConn ,$objSql);
	$objRows	= @mysqli_fetch_array($objResult);
	$totalLen	= $objRows[length]*2;
	$objName	= $objRows[$objNameField];
	$setTemplate = $objRows[$objSetField];
	$valueType	= $objRows[valueType];
	$minValue	= $objRows[minValue];
	$maxValue	= $objRows[maxValue];
		
	//explode by '[remark]'
	$remarkArr = explode('[remark]',$setTemplate);
	$remark  = $remarkArr[1];	
	//explode by '[unit]'
	$unitArr = explode('[unit]',$remarkArr[0]);
	
	//explode by '[u]'				
	$tlvArr = explode('[u]',$unitArr[$order]);

	//Unit Name
	if($DEBUG) echo "$tlvArr[0] : $tlvArr[1]";
	//explode by '-'
	$offsetArr = explode('-',$tlvArr[1]);
	$bgnOffsetArr = explode('.',$offsetArr[0]);
	$bgnByte = $bgnOffsetArr[0];
	$bgnBit  = $bgnOffsetArr[1];
	$endOffsetArr = explode('.',$offsetArr[1]);
	$endByte = $endOffsetArr[0];
	$endBit  = $endOffsetArr[1];
	$byteLen = $endByte - $bgnByte + 1;
	
	$paramSql = " SELECT initValue,readValue FROM $paramTable WHERE $objWhereStr $otherWhereStr AND instanceNo = '$instanceNo' ";
  	$v_Result = mysqli_query($pubConn,$paramSql);				
	$v_Row = mysqli_fetch_array($v_Result);
	$initGetValue = $v_Row[0];
	$readGetValue = $v_Row[1];

	$initByteValue = substr($initGetValue,$bgnByte*2,$byteLen*2);
	$readByteValue = substr($readGetValue,$bgnByte*2,$byteLen*2);
	if($DEBUG) echo "<br>init:$initByteValue<br>read:$readByteValue";
	$readBitValue = '';
	if($bgnBit > 0 ||$endBit < 7)
	{
		$bitLen   = $byteLen*8;//1 byte = 8 bit
		$readBitValue = DecBin(HexDec($readByteValue));
		$readBitValue = omcStrFill($readBitValue,$bitLen,'high','0');
		$readBitValue = strrev($readBitValue);
		$catBitLen = $endByte*8+$endBit - ($bgnByte*8+$bgnBit) + 1; //1 byte = 8 bit
		if($DEBUG) echo "<br>readBitValue = substr($readBitValue,$bgnBit,$catBitLen)";
		$readBitValue = substr($readBitValue,$bgnBit,$catBitLen);
		$readBitValue = strrev($readBitValue);
		if($DEBUG) echo "<br>readBitValue = $readBitValue";
	}
	$initBitValue = '';
	if($bgnBit > 0 ||$endBit < 7)
	{
		$bitLen   = $byteLen*8;//1 byte = 8 bit
		$initBitValue = DecBin(HexDec($initByteValue));
		$initBitValue = omcStrFill($initBitValue,$bitLen,'high','0');
		$initBitValue = strrev($initBitValue);
		$catBitLen = $endByte*8+$endBit - ($bgnByte*8+$bgnBit) + 1; //1 byte = 8 bit
		if($DEBUG) echo "<br>initBitValue = substr($initBitValue,$bgnBit,$catBitLen)";
		$initBitValue = substr($initBitValue,$bgnBit,$catBitLen);
		$initBitValue = strrev($initBitValue);
		if($DEBUG) echo "<br>initBitValue = $initBitValue";
	}
	$IniEquRead = false;
	if(strlen($initBitValue)>0)
	{
		if($initBitValue==$readBitValue)
			$IniEquRead = true;
	}
	else
	{
		if($initByteValue == $readByteValue)
			$IniEquRead = true;
	}
	//explode by '[v]'
	$valueArr = explode('[v]',$tlvArr[2]);
	$typeAddTitleArr = explode('[t]',$valueArr[0]);
	$operType = chop($typeAddTitleArr[0]);
	$titleStr = chop($typeAddTitleArr[1]);
	$valueArr[1] = chop($valueArr[1]);

	if($DEBUG) echo "<br>operType = $operType";
	switch($operType)
	{
		case 'input':
		{
			//explode by '-'
			$defArr = explode('-',$valueArr[1]);
			if($DEBUG) echo "<br>defArr = $defArr[0],$defArr[1],$defArr[2]";
			switch($defArr[0])
			{
				case 'toDec':
					if(strlen($initBitValue) > 0){
						$initParsedValue = BinDec($initBitValue);
					}else{
						$initParsedValue = HexDec($initByteValue);
					} 
					break;
				case 'keepDec':
					$initParsedValue = $initByteValue;
					break;
				case 'toHex':
					echo "<br>Error:toHex";
					break;
				case 'keepHex':
					$initParsedValue = $initByteValue;
					break;
				case 'toAscii':
					$charValue = "";
					if($bgnBit > 0 ||$endBit < 7)
					{
						echo "<br>Error:toAscii";
						exit(1);
					}
					else
					{
						$tmpLen=strlen($initByteValue);
						for($charCount=0;$charCount<$tmpLen;$charCount++)
						{
							$unitByte = substr($initByteValue,$charCount,2);
							$unitChar = chr(HexDec($unitByte)); // hex -> ascii
							if($DEBUG) echo "<br>unitChar=$unitChar";
							$charValue .= $unitChar;
							$charCount++;
						}
						$charValue = trim($charValue);
						$initParsedValue = $charValue;
					}
					break;
				default: 
					echo "<br>Error:valueType=$defArr[0]";
					break;
			}
			if($DEBUG) echo "<br>initParsedValue = $initParsedValue";
			switch($defArr[1])
			{
				case 'high':
					switch($defArr[2])
					{
						case '0':
							if($defArr[0] == 'toHex' || $defArr[0] == 'keepHex'){
								//$initParsedValue = str_replace('0','',$initParsedValue);
							}else{
								$initParsedValue = $initParsedValue - 0;
							}
							break;
						case 'E':
							echo "Error:high fill E";
							break;
						default: break;
					}
					break;
				case 'low':
					if ("toAscii" != $defArr[0]) 
					{
						switch($defArr[2]){
							case '0':
								$initParsedValue = str_replace('00','',$initParsedValue);
								break;
							case 'E':
								$initParsedValue = str_replace('E','',$initParsedValue);
								break;
							case 'F':   //modify zane 20051004
								$initParsedValue = str_replace('F','',$initParsedValue);
								break;	
							default: break;
						}
					}
					break;
				default: break;
			}
			$returnValue = $initParsedValue;
			break;
		}
		case 'select':
		{
			if($valueType == $nOctStr){
				if($initBitValue != ''){
					$selectValue = BinDec($initBitValue);
				}else{
					$selectValue = HexDec($initByteValue);
				}
			}else{
				$selectValue=$initByteValue;
			}
			$isSelected = 0;
			for($j=1;$j<sizeof($valueArr);$j++){
				//explode by '[opt]'
				$defArr = explode('[opt]',$valueArr[$j]);
				if(sizeof($defArr) < 2){
					$selectShow = "Reserved Value";
					break;
				}elseif($defArr[0] ==  $selectValue){
					$selectShow =$defArr[1];
					$isSelected = 1;
				}else{
					//echo "<option value=\"$defArr[0]\">$defArr[1]</option>";
				}
			}
			
			if($isSelected ==0 && $j == sizeof($valueArr)){
				$selectShow = "Undefined Value";			
			}
			$returnValue = $selectShow;												
			break;
		}
		default: echo "<br>Error:operType =|$operType|";
			break;
	}//end of switch(..)
	if(strlen($returnValue)==0) $returnValue='--';
	return $returnValue;
}//end getParamValue

function setParamValue($unitValue,$sysId,$oidStr,$order,$instanceNo=0,$otherWhereStr='')
{
	global $language;
	$DEBUG=0;
	$sysIdArr	= explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];	
	$objDb		= 'OBJ_'.$sysTypeNo;
	$paramTable = 'param_'.$sysNo;
	
	$sysConfTable = 'OMC_PUB.sysConf';	
	$pubDb			='OMC_PUB';
	$paramConfTable	= $objDb.".paramConf";
	
	$maxLevelNum	= 10;
	$nInteger = 2;
	$nOctStr = 4;
	
	if($DEBUG) echo "===================<br>unitValue:$unitValue<br>";
	//----------------------
	//选择语言版本
	if($language == 'eng'){
		$objNameField=name_2;
		$objSetField=setTemplate_2 ; //语种2
	}else{	
		$objNameField=name_1;
		$objSetField=setTemplate_1 ; //语种1(默认语种)
	}
		

	$objWhereStr = getLevelWhere($oidStr);
	
	$objSql = "SELECT $objNameField,$objSetField,valueType,length,minValue,maxValue FROM $paramConfTable WHERE $objWhereStr";
	$objResult	= mysqli_query($pubConn ,$objSql);
	$objRows	= @mysqli_fetch_array($objResult);
	$totalLen	= $objRows[length]*2;
	$objName	= $objRows[$objNameField];
	$setTemplate = $objRows[$objSetField];
	$valueType	= $objRows[valueType];
	$minValue	= $objRows[minValue];
	$maxValue	= $objRows[maxValue];
 
 	
	$paramSql = " SELECT initValue FROM $paramTable WHERE $objWhereStr $otherWhereStr AND instanceNo = '$instanceNo' ";
  	$v_Result = mysqli_query($pubConn,$paramSql);				
	$v_Row = mysqli_fetch_array($v_Result);
	$getValue = $v_Row[0];
	if($DEBUG) echo "<br>getValue = $getValue";
	
		
	//explode by '[remark]'
	$remarkArr = explode('[remark]',$setTemplate);
	$remark  = $remarkArr[1];	
	//explode by '[unit]'
	$unitArr = explode('[unit]',$remarkArr[0]);
	
	//explode by '[u]'				
	$tlvArr = explode('[u]',$unitArr[$order]);

	//Unit Name
	if($DEBUG) echo "<br>$tlvArr[0] : $tlvArr[1]";
	//explode by '-'
	$offsetArr = explode('-',$tlvArr[1]);
	$bgnOffsetArr = explode('.',$offsetArr[0]);
	$bgnByte = $bgnOffsetArr[0];
	$bgnBit  = $bgnOffsetArr[1];
	$endOffsetArr = explode('.',$offsetArr[1]);
	$endByte = $endOffsetArr[0];
	$endBit  = $endOffsetArr[1];
	$byteLen = $endByte - $bgnByte + 1;
	
	//explode by '[v]'
	$valueArr = explode('[v]',$tlvArr[2]);
	//$operType = chop($valueArr[0]);
	$typeAddTitleArr = explode('[t]',$valueArr[0]);
	$operType = chop($typeAddTitleArr[0]);
	$titleStr = chop($typeAddTitleArr[1]);
	$valueArr[1] = chop($valueArr[1]);

	if($DEBUG) echo "<br>operType = $operType";
	if($operType == 'select')
	{ //select
		if($valueType == $nOctStr)
			$changeTypeStr = 'toDec-high-0';
		else
			$changeTypeStr = 'keepDec-high-0';
	}
	else
	{
		$changeTypeStr = $valueArr[1];
	}
	$changeTypeArr = explode('-',$changeTypeStr);
	$changeType = $changeTypeArr[0];
	$fillWay	= $changeTypeArr[1];
	$fillChar	= $changeTypeArr[2];

	$setUnitValue = '';
	if($DEBUG)echo "<br>changeType:$changeType";
	switch($changeType)
	{
		case 'toDec':
			if($bgnBit > 0 ||$endBit < 7)
			{
				$bitLen = $endBit - $bgnBit +1;
				$bitValue = DecBin($unitValue);
				if($DEBUG) echo "<br>bitValue = $bitValue ";
				$bitValue  = omcStrFill($bitValue,$bitLen,$fillWay,$fillChar);
				if($DEBUG) echo "<br>omcStrFill($bitValue,$bitLen,$fillWay,$fillChar)";
				$setUnitValue = $bitValue;
			}
			else
			{
				$setUnitValue = DecHex($unitValue);
				$strLen = $byteLen*2;
				$setUnitValue = omcStrFill($setUnitValue,$strLen,$fillWay,$fillChar);
				if($DEBUG) echo "<br>omcStrFill($setUnitValue,$strLen,$fillWay,$fillChar)";
			}
			if($DEBUG) echo "<br>setUnitValue = $setUnitValue";
			break;
		case 'keepHex':
			if($bgnBit > 0 ||$endBit < 7)
			{
				$bitLen = $endBit - $bgnBit +1;
				$bitValue = ($unitValue);
				if($DEBUG) echo "<br>bitValue = $bitValue ";
				$bitValue  = omcStrFill($bitValue,$bitLen,$fillWay,$fillChar);
				if($DEBUG) echo "<br>omcStrFill($bitValue,$bitLen,$fillWay,$fillChar)";
				$setUnitValue = $bitValue;
			}
			else
			{
				$setUnitValue = ($unitValue);
				$strLen = $byteLen*2;
				$setUnitValue = omcStrFill($setUnitValue,$strLen,$fillWay,$fillChar);
				if($DEBUG) echo "<br>omcStrFill($setUnitValue,$strLen,$fillWay,$fillChar)";
			}
			if($DEBUG) echo "<br>setUnitValue = $setUnitValue";
			break;
		case 'keepDec':
			//echo "<br>Error:keepDec";
			$setUnitValue = $unitValue - 0;
			break;
		case 'toHex':
			echo "<br>Error:toHex";
			break;
		case 'toAscii':
			if($bgnBit > 0 ||$endBit < 7){
				echo "<br>Error:toAscii";
				exit(1);
			}else{
				$tmpLen=strlen($unitValue);
				for($i=0;$i<$tmpLen;$i++){
					$unitChar = substr($unitValue,$i,1);
					$decValue = ord($unitChar); //ascii -> dec
					$setUnitValue .= DecHex($decValue);
					if(strlen($setUnitValue) < 2){
						$setUnitValue = '0'.$setUnitValue;
					}
				}
				$strLen = $byteLen*2;
				$setUnitValue = omcStrFill($setUnitValue,$strLen,$fillWay,$fillChar);
			}
			break;
		default: 
			echo "Error:Unknown valueType";
			break;
	}
	if($DEBUG) echo "<br>setUnitValue  = $setUnitValue ";
	//--><-//$lastValue = $setUnitValue.$lastValue;
	
	$setValue = setBitValue($getValue,$tlvArr[1],$setUnitValue);
		
	$setValue = strtoupper($setValue);
	$updSql = "UPDATE $paramTable SET initValue = '$setValue'   
			WHERE $objWhereStr $otherWhereStr AND instanceNo = '$instanceNo'";
	mysqli_query($pubConn,$updSql);	
	echo mysqli_error($pubConn);		
}//end setParamValue

function showParameter($sysId,$oidStr,$order,$inputValueType,$instanceNo=0,$doingOtherUlr='',$otherWhereStr='',$showStr='')
{
	global $language,$notSettedColor,$settedColor;
	$tmp1 = str_replace('.','_',$oidStr);
	$id = 'ID_' . $sysId . '_' . $tmp1 . '_' . $order . '_ist' . $instanceNo;
	$newId = "New".$id;
	$showValue = getParamValue($titleStr,$IniEquRead,$sysId,$oidStr,$order,$instanceNo,$otherWhereStr);
	if($IniEquRead)$color = $settedColor;
	else $color = $notSettedColor;
	$doingOtherUlr=urlencode($doingOtherUlr);
	if(strlen($showStr))
	{
	    $color = $settedColor;
	    $showValue = $showStr;
	}
	//$otherWhereStr=urlencode($otherWhereStr);
	echo "<span id=\"$id\" title=\"$titleStr\" style=\"cursor:pointer\" onclick=\"javascript:getPar('getVar','$language','$sysId','$oidStr','$order','$instanceNo','0','$doingOtherUlr','$otherWhereStr','$showStr','$inputValueType');\"><font color=\"$color\">$showValue</font></span>";
	echo "<span id=\"$newId\" style=\"display:none\" ></span>";
}

?>

<script LANGUAGE="JavaScript">
var globalID;
function getPar(flag,lan,sysId,oidStr,order,instanceNo,inputID,doingOtherUlr,otherWhereStr,showStr,inputValueType)
{
	
	var ulr="../sub_inc/getParameter.php?flag="+flag+"&lan="+lan+"&sysId="+sysId+"&oidStr="+oidStr+"&order="+order+"&instanceNo="+instanceNo;
	if(flag=='saveVar')
	{
		var setValue = document.getElementById(inputID).value;
		ulr = ulr + "&setValue=" + setValue;
	}
	ulr = ulr+"&doingOtherUlr="+doingOtherUlr+"&otherWhereStr="+otherWhereStr+"&showStr="+showStr+"&inputValueType="+inputValueType;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_o = new XMLHttpRequest();
	}
	
	request_o.open("GET",ulr,"false");
	request_o.onreadystatechange = updateID; 
	request_o.send(null);
}
function updateID()
{	
	if(request_o.readyState == 4)
	{
		var strResult = request_o.responseText;
		var arrResult = strResult.split("###");
		var flag = arrResult[0];
		var idName = arrResult[1];
		var innerStr = arrResult[2];
		//alert(strResult);
		if(flag=='getVar')
		{
			var getVarId = idName.substr(3);//ID...
			var InputId = "Input"+getVarId;
			var saveId  = "Save" + getVarId;
			document.getElementById(getVarId).style.display="none";
			document.getElementById(idName).style.display="";
		}
		if(flag=='saveVar')
		{
			globalID = idName;
			var saveVarId = "New"+idName;//NewID...
			document.getElementById(saveVarId).style.display="none";
			document.getElementById(idName).style.display="";
			document.getElementById(idName).style.cursor="pointer";
			var isEqual = arrResult[3];
			if(isEqual == '1') color="<?php echo $settedColor;?>";
			else	color = "<?php echo $notSettedColor;?>";
			document.getElementById(idName).style.color = color;
			//alert(innerStr);
			var doingOtherUlr = arrResult[4];
			if(doingOtherUlr.length>0)
			{
		        //alert(doingOtherUlr);
				doingOther(doingOtherUlr);
			}
		}
		document.getElementById(idName).innerHTML = innerStr;
		if(flag == 'getVar')
		{
			document.getElementById(InputId).style.background="<?php echo $inputBkgrdColor;?>";
			document.getElementById(saveId).style.background="<?php echo $saveBkgrdColor;?>";
			//alert(document.getElementById(InputId).value);
		}
	}
}

function doingOther(ulr)
{
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_o = new XMLHttpRequest();
	}
	request_o.open("GET",ulr,"false");
	request_o.onreadystatechange = otherReturn; 
	request_o.send(null);
}

//当额外操作没有完成时，参数背景颜色为waitingBkgrdColor
function otherReturn()
{	
	if(request_o.readyState == 3)//没有完成
	{
		//var strResult = request_o.responseText;
		document.getElementById(globalID).style.background="<?php echo $waitingBkgrdColor;?>";
	}
	if(request_o.readyState == 4)//完成
	{
		document.getElementById(globalID).style.background="";
	}
}
</script>
