<?php
function getLevelWhere($objIdStr)
{
	$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($j == 0){
			$levelWhere ="level_$levelNo='${"level_$levelNo"}'";
		}else{
			$levelWhere .=" AND level_$levelNo='${"level_$levelNo"}'";
		}
	}
	return($levelWhere);
}

function getSubSyss($num)
{
	$i=0;
	do
	{
		$arr[$i++] = $num % 2;		
	}while($num = floor($num/2));
	for($j=$i; $j<4; $j++)
		$arr[$j] = 0;
	
	return $arr;
}

//*******************************************************************************************
//函 数 名:	oidSysCommSend($sysId,$commType,$objIdStr,$selectedSysId=-1,$instanceNo='')
//功能说明:	系统参数维护发送命令,与~www/wxc2_omc/inc/lib.inc sysCommSend()的区别是:此函数可以
//		同时操作同一个系统不同的oid)
//变量说明: 略                 
//返 回 值:
//	成功: 命令编号字符串
//	失败: -1

//作者：崔体存    2006-01-10   
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function oidSysCommSend($sysId,$commType,$objIdStr,$selectedSysId=-1,$instanceNo='')
{
	
	//global $pubDb,$commTable;
	global $STR_CONF_COMMAND_ERR_0000;
	global $STR_CONF_PROMPT_001,$STR_CONF_COMMAND_ERR_0011,$STR_CONF_COMMAND_ERR_0012;
	global $STR_CONF_PROMPT_002,$STR_CONF_COMMAND_ERR_0021,$STR_CONF_COMMAND_ERR_0022;
	global $STR_CONF_PROMPT_003,$STR_CONF_COMMAND_ERR_0031,$STR_CONF_COMMAND_ERR_0032,$STR_CONF_COMMAND_ERR_0033;
	global $STR_CONF_PROMPT_004,$STR_CONF_COMMAND_ERR_0041,$STR_CONF_COMMAND_ERR_0042,$STR_CONF_COMMAND_ERR_0043;
	global $STR_CONF_COMMAND_ERR_9999;
	
	
	
	
	//=========insert the operation log into sysOperLog table====================
	
	
	//===get the operate type set or get==
	$logStr="";
	if($commType==0)
		$logStr.="Get ";
	else if($commType==3)	
		$logStr.="Set ";
	//===get the sysTypeNo=====
	$sysIdArr = explode('_',$sysId);
	$sysTypeNo = $sysIdArr[0];
	$sysNo	   = $sysIdArr[1];
	$subSysNo  = $sysIdArr[2];
	//=====get the LevelWhere
	$temp=getLevelWhere($objIdStr);
	
	
	$database="OBJ_".$sysTypeNo;
	$ssql="select name_2 from paramConf where $temp ";
	$resultt=@mysqli_query($pubConn,$ssql);
	$rowss=@mysqli_fetch_array($resultt);
	//==get the parameter name
	$p_name=$rowss[name_2];
	
	
	$logStr.=$p_name;
	
	insertLog($logStr);
	
	
	
	$DEBUG = 0;
	$pubDb = 'OMC_PUB';
	$commTable = 'parameterComm';
	$nGetComm = 0;
	$nSetComm = 3;
	
	$nSubSysParam		= 0;
	$nGlobalSubSysParam	= 1;
	$nGlobalSysParam	= 2;



	if(0) echo "<br>oidSysCommSend($sysId,$commType,$objIdStr,$selectedSysId)";
	
	if($sysNo == '99'){
		$paramType = $nGlobalSysParam;
	}else if($subSysNo == '99'){
		$paramType = $nGlobalSubSysParam;
	}else{
		$paramType = $nSubSysParam;
	}

	if($selectedSysId != -1){
		$selectedSysIdArr = explode('_',$selectedSysId);
		$selectedSysTypeNo = $selectedSysIdArr[0];
		$selectedSysNo	   = $selectedSysIdArr[1];
		$selectedSubSysNo  = $selectedSysIdArr[2];
	}
	if($DEBUG) echo"<br>selectedSysId=$selectedSysId";
	if($DEBUG) echo"<br>selectedSysNo=$selectedSysNo";
	if($DEBUG) echo"<br>selectedSubSysNo=$selectedSubSysNo";
	
	/*
	$objIdStrArr=explode(".",$objIdStr);
	
	$oidQueryWhere=" AND (";
	for($i=0;$i<sizeof($objIdStrArr);$i++){
		if($i==0)
			$tmpOidQueryWhere="objId = "."'";
		else
			$tmpOidQueryWhere=" OR objId = "."'";			
		for($j=0;$j<$i;$j++)
			$tmpOidQueryWhere=$tmpOidQueryWhere.$objIdStrArr[$j].'.';
		$tmpOidQueryWhere=$tmpOidQueryWhere.$objIdStrArr[$j]."'";		
		$oidQueryWhere=$oidQueryWhere.$tmpOidQueryWhere;
	}
	$oidQueryWhere=$oidQueryWhere.")";
	if($DEBUG){
		echo "<br>oidQueryWhere=$oidQueryWhere";
		exit();
	}
	*/

	//step 1: delete overtime and imcompetent command , check the exist command num
	$overTime = date("Y-m-d H:i:s", mktime(date("H"),date("i")-1,date("s"),date("m"),date("d"),date("Y")));
	$delSql = "DELETE FROM $commTable WHERE (commState ='0' OR commState ='3') AND sendTime < '$overTime'
				";
	if($DEBUG) echo"<br>delete command sql = $delSql";
	mysqli_query($pubConn,$delSql);
	echo mysqli_error($pubConn);

	$overTime = date("Y-m-d H:i:s", mktime(date("H"),date("i")-30,date("s"),date("m"),date("d"),date("Y")));
	$delSql = "DELETE FROM $commTable WHERE sendTime < '$overTime'
				";
	if($DEBUG) echo"<br>delete command sql = $delSql";
	mysqli_query($pubConn,$delSql);
	echo mysqli_error($pubConn);

	//check the exist command num
	$chkSql = "SELECT commId FROM $commTable";
	if($DEBUG) echo"<br>check exist commands sql = $chkSql";
	$chkResult = mysqli_query($pubConn,$chkSql);
	echo mysqli_error($pubConn);
	$existNum = @mysqli_num_rows($chkResult);
	
	/*
	//step 2: check the command(不能同时操作同一个系统的相同的oid)
	if($paramType == $nGlobalSysParam) {
		if($selectedSysId == -1){
			$chkSql = "SELECT commId FROM $commTable
						WHERE sysTypeNo='$sysTypeNo' $oidQueryWhere
							";
		}else{
			//选择指定子系统
			$chkSql = "SELECT commId FROM $commTable
						WHERE sysTypeNo='$selectedSysTypeNo' $oidQueryWhere
						";
		}
	}else if($paramType == $nGlobalSubSysParam) {
		if($selectedSysId == -1){
			$chkSql = "SELECT commId FROM $commTable
						WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' $oidQueryWhere
							";
		}else{
			//选择指定子系统
			$chkSql = "SELECT commId FROM $commTable
						WHERE sysTypeNo='$selectedSysTypeNo'  
						AND sysNo='$selectedSysNo' $oidQueryWhere
						";
		}
	}else{
		$chkSql = "SELECT commId FROM $commTable
					WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo' $oidQueryWhere
					";	
	}
	if($DEBUG)echo"<br>check command sql = $chkSql";
	//exit();
	
	$chkResult = mysqli_query($pubConn,$chkSql);
	echo mysqli_error($pubConn);
	//exit();
	$rowsNum = @mysqli_num_rows($chkResult);
	//echo "<br>rowsNum=$rowsNum";
	if($rowsNum > 0){
		echo "<script language=\"javascript\"> ";
		echo "alert('Command object is being operated by others,please try it later!'); ";
		echo "</script>";
		return(-1);
	}
	*/
	
	//step 3:insert commands into command table
	if(($paramType != $nSubSysParam)  && ($commType == $nSetComm))//写公共参数
	{ 
		if($selectedSysId == -1)
		{
			if($paramType == $nGlobalSysParam)
			{
				$selSql="SELECT sysNo,subSysNo FROM sysInfo 
					WHERE sysTypeNo = '$sysTypeNo' 
					ORDER BY sysNo,subSysNo,baseStat DESC
					";				
			}
			else
			{
				$selSql="SELECT sysNo,subSysNo FROM sysInfo 
					WHERE sysTypeNo = '$sysTypeNo' 
						AND sysNo='$sysNo' 
					ORDER BY sysNo,subSysNo,baseStat DESC
					";			
			}
		}
		else//选择指定子系统
		{
			$selSql="SELECT sysNo,subSysNo FROM sysInfo 
					WHERE sysTypeNo = '$selectedSysTypeNo' 
						AND sysNo	='$selectedSysNo' 
						AND subSysNo='$selectedSubSysNo'
					ORDER BY baseStat DESC
					";
		}
		if($DEBUG) 
		{
			echo"<br>selSql=$selSql";
			exit();
		}
		$selResult = mysqli_query($pubConn,$selSql);
		$selRowsNum  = mysqli_num_rows($selResult);

		if($selRowsNum+$existNum>256)
		{
			echo "<br><br><b>Error:$STR_CONF_COMMAND_ERR_0032<br>";
			echo "<b>Prompt1:$STR_CONF_PROMPT_003<br>";
			echo "<p align=\"left\"><a href=\"JavaScript:history.go(-1)\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a>";			
			exit();
		}

		//echo "paramType=$paramType";
		//exit();

		if($selRowsNum > 0)
		{
			while($selRows  = mysqli_fetch_array($selResult))
			{
				$sysNo	  = $selRows[sysNo];
				if(($paramType==$nGlobalSysParam)
				 &&($sysTypeNo == '222' || $sysTypeNo == '223') 
				 &&('8' == $sysNo ||'11'==$sysNo))//MTP3,SCCP
					continue;
				//add by oven 2006-8-22 19:23
				if(($paramType==$nGlobalSysParam)&&
				($sysTypeNo == '225'&&'9' != $sysNo))
					continue;
				//end add by oven
				$subSysNo = $selRows[subSysNo];
				$sendSql="INSERT INTO $commTable 
					(commType,sysTypeNo,sysNo,subSysNo,paramType,objId,InstanceNo,commState,sendTime)
					VALUES
					('$commType','$sysTypeNo','$sysNo','$subSysNo','$paramType','$objIdStr','$instanceNo','1',CURRENT_TIMESTAMP)
					";
				if($DEBUG) echo"<br>send command sql = $sendSql";
				$sendResult=mysqli_query($pubConn,$sendSql);
				echo mysqli_error($pubConn);
			}
		}
		else
		{
			echo "<br><br><b>Error:$STR_CONF_COMMAND_ERR_0011<br>";
			echo "<b>Prompt:$STR_CONF_PROMPT_001<br>";
			echo "<p align=\"left\"><a href=\"JavaScript:history.go(-1)\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a>";			
			exit();
		}
	}
	else//读系统/子系统参数或写子系统参数
	{
		if($paramType != $nSubSysParam)//读公共参数
		{
			if($selectedSysId == -1)
			{
				//get baseStat 0=uninstalled, 1=nomal, 2=warning, 3=error 
				//先按状态排序，再按系统编号,子系统编号排序，同种状态优先选择编号小的
				if($paramType == $nGlobalSysParam)
				{
					if($sysTypeNo[0]=='2')//Platform
					{
						$selSql="SELECT sysNo,subSysNo FROM sysInfo 
								WHERE sysTypeNo = '$sysTypeNo' AND sysNo='9' 
								ORDER BY baseStat DESC,sysNo DESC,subSysNo,updateTime DESC
							";
					}
					else
					{
						$selSql="SELECT sysNo,subSysNo FROM sysInfo 
							WHERE sysTypeNo = '$sysTypeNo' 
							ORDER BY baseStat DESC,updateTime DESC,sysNo,subSysNo
							";
					}
				}
				else
				{
					$selSql="SELECT sysNo,subSysNo FROM sysInfo 
							WHERE sysTypeNo = '$sysTypeNo' AND sysNo='$sysNo'
							ORDER BY baseStat DESC,updateTime DESC,sysNo,subSysNo
							";				
				}
			}
			else
			{
				//选择指定子系统
				$selSql="SELECT sysNo,subSysNo FROM sysInfo 
						WHERE sysTypeNo = '$sysTypeNo' AND sysNo='$selectedSysNo' AND subSysNo='$selectedSubSysNo'
						ORDER BY baseStat DESC,updateTime DESC
						";
			}

			$selResult=mysqli_query($pubConn,$selSql);
			if($DEBUG) 
			{
				echo"<br>get system info sql = $selSql";
			}
			echo mysqli_error($pubConn);
			if(@mysqli_num_rows($selResult) > 0)
			{
				$selRows = @mysqli_fetch_array($selResult);
				$sysNo = $selRows[sysNo];
				$subSysNo = $selRows[subSysNo];
			}
			else
			{
				echo "<br><br><b>Error:$STR_CONF_COMMAND_ERR_0011<br>";
				echo "<b>Prompt:$STR_CONF_PROMPT_001<br>";
				echo "<p align=\"left\"><a href=\"JavaScript:history.go(-1)\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a>";			
				exit();
			}
		}
		
		if(1+$existNum>256)
		{
			echo "<br><br><b>Error:$STR_CONF_COMMAND_ERR_0032<br>";
			echo "<b>Prompt2:$STR_CONF_PROMPT_003<br>";
			echo "<p align=\"left\"><a href=\"JavaScript:history.go(-1)\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a>";			
			exit();
		}

		$sendSql="INSERT INTO $commTable 
			(commType,sysTypeNo,sysNo,subSysNo,paramType,objId,InstanceNo,commState,sendTime)
			VALUES
			('$commType','$sysTypeNo','$sysNo','$subSysNo','$paramType','$objIdStr','$instanceNo','1',CURRENT_TIMESTAMP)
			";
		if($DEBUGs) echo"<br>send command sql = $sendSql";
		$sendResult=mysqli_query($pubConn,$sendSql);
		echo mysqli_error($pubConn);
	}
	
	//step 4:Return commIdStr
	if($DEBUG) echo"<br>if(($paramType != $nSubSysParam) && ($commType == $nSetComm))";
	if(($paramType != $nSubSysParam) && ($commType == $nSetComm))
	{
		if($instanceNo=='')
		{
			if($paramType == $nGlobalSysParam)
			{
				$selSql = "SELECT commId FROM $commTable 
					WHERE paramType='$paramType' AND sysTypeNo='$sysTypeNo' AND objId='$objIdStr'  
					ORDER BY sysTypeNo,sysNo,subSysNo";
			}
			else
			{
				$selSql = "SELECT commId FROM $commTable 
					WHERE paramType='$paramType' AND sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND objId='$objIdStr'
					ORDER BY sysTypeNo,sysNo,subSysNo";
			}
		}
		else
		{
			if($paramType == $nGlobalSysParam)
			{
				$selSql = "SELECT commId FROM $commTable 
					WHERE paramType='$paramType' AND sysTypeNo='$sysTypeNo' AND objId='$objIdStr' AND instanceNo='$instanceNo'  
					ORDER BY sysTypeNo,sysNo,subSysNo,instanceNo";
			}
			else
			{
				$selSql = "SELECT commId FROM $commTable 
					WHERE paramType='$paramType' AND sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND objId='$objIdStr' AND instanceNo='$instanceNo'
					ORDER BY sysTypeNo,sysNo,subSysNo,instanceNo";
			}
		}
	}
	else
	{
		if($instanceNo=='')
		{
			$selSql = "SELECT commId FROM $commTable 
				WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo' AND objId='$objIdStr'"
				;
		}
		else
		{
			$selSql = "SELECT commId FROM $commTable 
				WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo' AND objId='$objIdStr' AND instanceNo='$instanceNo'"
				;
		}
			
	}
	if($DEBUG) echo"<br>get command id sql = $selSql";
	$selResult=mysqli_query($pubConn,$selSql);
	echo mysqli_error($pubConn);
	$commIdStr='';
	while($selRows = mysqli_fetch_array($selResult))
	{
		$commIdStr .= $selRows[commId].'-';
	}
	if($DEBUG) echo "<br>commIdStr = $commIdStr";
	if($DEBUG) exit();
	return($commIdStr);
}//end of function oidSysSendComm()

function ChComm($sysId,$chkType)
{     
	global $omc_db;
	global $remark,$nProcFail,$nWaitProc,$nInProc,$nOutProc;
	
	$commTable = "parameterComm";
	
	$sysIdArr = explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];
	
	if($chkType==0)
		$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' ";
	else
		$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' AND sysNo = '$sysNo' ";
	if($DEBUG)echo "<br>selComm=$selComm";
	
	$selRes  = @mysqli_query($pubConn,$selComm);
	$selRow  = @mysqli_fetch_array($selRes);
	echo mysqli_error($pubConn);
	$outProcCount=0;
	do
	{
		$temCommState = $selRow[commState];
		if(isset($selRow[remark])){
			$remark = $selRow[remark];
			if($DEBUG)echo "<br>remark=$remark";
		}
		if($DEBUG)echo "<br>temCommState=$temCommState";
		if($temCommState == $nWaitProc){
			return($nWaitProc);
		}elseif($temCommState == $nInProc){
			return($nInProc);
		}elseif($temCommState == $nOutProc){
			$commState = $nOutProc;
			$outProcCount++;
		}else{
			$commState = $nProcFail;
		}
	}while($selRow  = @mysqli_fetch_array($selRes));
	if($outProcCount > 0){
		return($nOutProc);
	}else{
		return($nProcFail);
	}
}

//****************************************************************
//函 数 名:	function checkSaveFunction($saveHref)
//功能说明:	在打开一个编辑参数页面后，按"back"按钮，判断参数是否修改，如果修改，提醒操作者进行保存操作
//变量说明:	$saveHref=$backHref+"OBJ:"+"$sysTypeNo&$oidStr&$instanceNo&$newValue"+
//			            "OBJ:"+"$sysTypeNo&$oidStr&$instanceNo&$newValue"+
//				    "OBJ:"+"$sysTypeNo&$oidStr&$instanceNo&$newValue"+
//				    ..................................................
//				    "OBJ:"+"$sysTypeNo&$oidStr&$instanceNo&$newValue"
//返 回 值:
//	
//作者：崔体存    2005-12-19   
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function checkSaveFunction($saveHref)
{
	$tmpSaveHref=$saveHref;
	//echo "<br>saveHref=$saveHref";
	$varAttributeArr=explode("OBJ:",$tmpSaveHref);
	
	for($i=1;$i<sizeof($varAttributeArr);$i++){
		//echo "<br>var=$varAttributeArr[$i]";
		//continue;
		$unitVarAttributeArr=explode("&",($varAttributeArr[$i]));
		
		$tmpSysIdArr	= explode("_",$unitVarAttributeArr[0]);
		$tmpSysTypeNo	= $tmpSysIdArr[0];
		$tmpSysNo	= $tmpSysIdArr[1];
		$tmpSubSysNo	= $tmpSysIdArr[2];
		
		$tmpObjDb	= 'OBJ_'.$tmpSysTypeNo;
		//echo "<br>tmpObjDb=$tmpObjDb";
		$tmpParamTable = 'param_'.$tmpSysNo;
		$tmpObjWhereStr = getLevelWhere($unitVarAttributeArr[1]);
		$tmpGetsql = "select initValue from $tmpParamTable 
				where $tmpObjWhereStr AND instanceNo='$unitVarAttributeArr[2]'";
		//echo "<br>$tmpGetsql<br>";
				
		$tmpResult = mysqli_query($pubConn,$tmpGetsql);
		$tmpRow = mysqli_fetch_array($tmpResult);
		$tmpInitValue=$tmpRow[initValue];
		//echo "<br>tmpInitValue=$tmpInitValue";
		//echo "<br>inputValue=$unitVarAttributeArr[3]";
		
		if($tmpInitValue!=$unitVarAttributeArr[3]){
		?>
			<script LANGUAGE="JavaScript">
				 if(confirm('Do you want to save the parameters?')==true)
					window.location.href='<?php echo "../../element/sub_inc/saveParameters.php?"; ?>'+'saveHref='+escape('<?php echo $saveHref; ?>')+escape("OBJ:needSave="+'y');
				 else
					window.location.href='<?php echo "../../element/sub_inc/saveParameters.php?"; ?>'+'saveHref='+escape('<?php echo $saveHref; ?>')+escape("OBJ:needSave="+'n');
			</script>
			
		<?php
			break;
		}
	}
	?>
	if($i==sizeof($varAttributeArr))
		<script LANGUAGE="JavaScript">
		window.location.href='<?php echo "../../element/sub_inc/saveParameters.php?"; ?>'+'saveHref='+escape('<?php echo $saveHref; ?>')+escape("OBJ:needSave="+'n');
		</script>
	<?php
}
function isPrefixExsit($prefix,$callType,$trunkgp,&$prefixIns)
{
	$msc_objDb = 'OBJ_322';
	$msc_paramTable = 'param_99';
	
	$objstrset_1='2.1.1';
	$objWhereStr_1 =getLevelWhere($objstrset_1);
	$objstrset_2='2.1.2';
	$objWhereStr_2 =getLevelWhere($objstrset_2);
	$objstrset_3='2.1.3';
	$objWhereStr_3 =getLevelWhere($objstrset_3);
				
	$findPrefix = omc_keep_length($prefix,'E',16,0);
	$paramSql_1 = " SELECT initValue,readValue,instanceNo FROM $msc_paramTable  
			WHERE $objWhereStr_1 AND initValue = '$findPrefix' ";
	$Result_1 = mysqli_query($pubConn,$paramSql_1);
	$findflag=0;
	while($Row_1	= mysqli_fetch_array($Result_1))
	{
		$paramSql_2 = " SELECT initValue,readValue,instanceNo FROM $msc_paramTable  
					WHERE $objWhereStr_2 AND instanceNo = '$Row_1[instanceNo]'	";
		if($DEBUG) echo "paramSql2=$paramSql2<br>";
		$Result_2 = mysqli_query($pubConn,$paramSql_2);				
		$Row_2		= mysqli_fetch_array($Result_2);
		$prefixflag=bindec(getBitValue($Row_2[initValue],'10.7-10.7'));
		$call_type  =bindec(getBitValue($Row_2[initValue],'10.4-10.6'));
		if($prefixflag==1&&$call_type==$callType)
		{
						
			$paramSql_3 = " SELECT initValue,readValue,instanceNo FROM $msc_paramTable  
						WHERE $objWhereStr_3 AND instanceNo ='$Row_2[instanceNo]'";
									
			$Result_3 = mysqli_query($pubConn,$paramSql_3);				
			$Row_3	= mysqli_fetch_array($Result_3);
			$prefixTG = hexdec($Row_3[initValue]);		
			if($prefixTG==$trunkgp)
			{
				$prefixIns=$Row_2[instanceNo];
				$findflag=1;
				break;
			}
		}
	}
	
	return $findflag;	
}
function setPrefix($prefix,$callType,$trunkgp,$setInstanceNo)
{
	$msc_objDb = 'OBJ_322';
	$msc_paramTable = 'param_99';
	$setPrefix = omc_keep_length($prefix,'E',16,0);
	$objstrset='2.1.1';
	$objWhereStr =getLevelWhere($objstrset);				
	$updSql = "UPDATE $msc_paramTable SET initValue = '$setPrefix' WHERE $objWhereStr AND instanceNo = '$setInstanceNo'";
	if($DEBUG) echo "updSql=$updSql<br>";
	mysqli_query($pubConn,$updSql);
	
	$objstrset='2.1.2';
	$objWhereStr =getLevelWhere($objstrset);				
	$paramSql = " SELECT initValue FROM paramConf WHERE $objWhereStr";
	if($DEBUG) echo "paramSql=$paramSql<br>";
	$Result_def = mysqli_query($pubConn,$paramSql);					
	$Row_def	= mysqli_fetch_array($Result_def);
	$CT = omc_keep_length(decbin($callType),0,3);
	if($prefix=='BB')
		$tonIN=$tonOut = 2;//unkown
	else 
		$tonIN=$tonOut = 4;//idd
	$setTonIn = omc_keep_length(decbin($tonIN),0,4);
	$setTonOut = omc_keep_length(decbin($tonOut),0,4);
	$Row_def[initValue] = setBitValue($Row_def[initValue],'0.0-0.3',$setTonIn);//ton in = IDD:4
	$Row_def[initValue] = setBitValue($Row_def[initValue],'0.4-0.7',$setTonOut);//TON OUT= IDD:4
	$Row_def[initValue] = setBitValue($Row_def[initValue],'10.7-10.7','1');//flag on
	$Row_def[initValue] = setBitValue($Row_def[initValue],'18.0-18.0','1');//VoicePromptFlag=on
	$Row_def[initValue] = setBitValue($Row_def[initValue],'10.4-10.6',$CT);//call type
	$Row_def[initValue] = setBitValue($Row_def[initValue],'10.2-10.3','00');//trunk					
	$Row_def[initValue] = setBitValue($Row_def[initValue],'11.0-11.7','00');//length=0
	
	$setValue = strtoupper($Row_def[initValue]);
	$updSql = "UPDATE $msc_paramTable SET initValue = '$setValue'   
			WHERE $objWhereStr AND instanceNo = '$setInstanceNo'";
	if($DEBUG) echo "updSql=$updSql<br>";
	mysqli_query($pubConn,$updSql);	

	$objstrset='2.1.3';
	$objWhereStr =getLevelWhere($objstrset);				
	$paramSql = " SELECT initValue,readValue,instanceNo FROM $msc_paramTable  
					WHERE $objWhereStr AND instanceNo ='$setInstanceNo'";
							
	$Result = mysqli_query($pubConn,$paramSql);				
	$Row	= mysqli_fetch_array($Result);

	$HEXtrunkgp = omc_keep_length(dechex($trunkgp),0,2);		
	$Row[initValue] = strtoupper($HEXtrunkgp);
	$updSql = "UPDATE $msc_paramTable SET initValue = '$Row[initValue]'   
		WHERE $objWhereStr AND instanceNo = '$setInstanceNo'";
	mysqli_query($pubConn,$updSql);			
}
function delPrefix($inst)
{
	global $pubDb,$DEBUG;
	$tmpParamConfTable='OBJ_322.paramConf';
	$tmpObjIdStr='2.1';
	$levelWhere = getLevelWhere($tmpObjIdStr);
	if($DEBUG) echo "<BR>levelWhere=$levelWhere";
	
	$getSql="SELECT initValue,level_3 FROM $tmpParamConfTable 
				WHERE $levelWhere AND (level_3='1' OR level_3='2' OR level_3='3') ORDER BY level_3
			";
	if($DEBUG) echo "<BR>getSql=$getSql";
	$delPreResult	=mysqli_query($pubConn,$getSql);
	echo mysqli_error($pubConn);
	$i=0;
	while($rows=mysqli_fetch_array($delPreResult))
	{
		$tmpDefaultValueArr[$i][initValue]=$rows[initValue];
		$tmpDefaultValueArr[$i][level_3]=$rows[level_3];
		$i++;
	}
							
	for($i=0;$i<sizeof($tmpDefaultValueArr);$i++)
	{
		$updateSql="UPDATE OBJ_322.param_99 SET initValue='{$tmpDefaultValueArr[$i][initValue]}'
					WHERE $levelWhere AND level_3='{$tmpDefaultValueArr[$i][level_3]}' AND instanceNo='$inst'";
		if(0) echo "<BR>updateSql=$updateSql";
		mysqli_query($pubConn,$updateSql);
		echo mysqli_error($pubConn);
	}
}
function updatePrefix($oldPrefix,$newPrefix,$callType,$trunkgp)
{
	$msc_objDb = 'OBJ_322';
	$msc_paramTable = 'param_99';
	
	$oldPreIns = $newPreIns = -1;
	$oldExist = $newExist = 0;
	$oldExist = isPrefixExsit($oldPrefix,$callType,$trunkgp,$oldPreIns);
	$newExist = isPrefixExsit($newPrefix,$callType,$trunkgp,$newPreIns);
	if($oldExist==1)
	{
		if(strcmp($oldPrefix,$newPrefix)==0)
			return;
		if($newExist==1 || strlen($newPrefix)==0)
		{
			delPrefix($oldPreIns);
			return;
		}
		$findflag = 1;
		$setInstanceNo = $oldPreIns;
		//delPrefix($setInstanceNo);
	}
	else
	{
		if($newExist==1 || strlen($newPrefix)==0)
			return;
		$objstrset_2='2.1.2';
		$objWhereStr_2 =getLevelWhere($objstrset_2);
		$paramSql = " SELECT initValue,readValue,instanceNo FROM $msc_paramTable WHERE $objWhereStr_2 ";
		if($DEBUG) echo "paramSql=$paramSql<br>";
		$Result_2 = mysqli_query($pubConn,$paramSql);				
		while($Row_2 = mysqli_fetch_array($Result_2))
		{					
			$prefixflag=bindec(getBitValue($Row_2[initValue],'10.7-10.7'));
			if($prefixflag==0)
			{
				$setInstanceNo = $Row_2[instanceNo];
				$findflag=1;
				break;
			}
		}												
	}
	if($findflag==1)
		setPrefix($newPrefix,$callType,$trunkgp,$setInstanceNo);
	
	return $setInstanceNo;
	//exit;
}//end updatePrefix

function checkDupPrefix($instNo,$setPrefix,$setTonIn,$setTonOut)
{		
	$sysId = '322_99_99';
	
	$sysIdArr	= explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];
	
	$objDb		= 'OBJ_'.$sysTypeNo;
	$paramTable = 'param_'.$sysNo;
	
	{//get all valid prefix No
	$oidstr = '2.1.2';
	$objWhereStr= '';
	$objWhereStr =getLevelWhere($oidstr);		
	
	$defSql = " SELECT initValue,instanceNo FROM $paramTable WHERE $objWhereStr ORDER BY instanceNo";
	//echo $defSql;
	$defRes = @mysqli_query($pubConn,$defSql);
	echo mysqli_error($pubConn);
	$offset_enable = "10.7-10.7";
	$offset_Properties = "10.2-10.3";	
	$offset_calltype = "10.4-10.6";
	$offset_ton_in = "0.0-0.3";
	$offset_ton_out="0.4-0.7";
	$prefix_num=0;	
	while($row = @mysqli_fetch_array($defRes))
	{
		$prefix_enable=hexdec(getBitValue($row[initValue],$offset_enable));
		if($prefix_enable)//enable
		{
			$validPrefix[$prefix_num][inst_no]=$row[instanceNo];
			$validPrefix[$prefix_num][init_value]=$row[initValue];
			$validPrefix[$prefix_num][ton_in]=BinDec(getBitValue($row[initValue],$offset_ton_in));
			$validPrefix[$prefix_num][ton_out]=BinDec(getBitValue($row[initValue],$offset_ton_out));
			$prefix_num++;
		}
	}	
	}//end get valid prefix No
	
	{//get prefix value
	$oidstr = '2.1.1';
	$objWhereStr= '';
	$objWhereStr =getLevelWhere($oidstr);	
	
	$paramSql = " SELECT initValue,readValue,instanceNo FROM $paramTable  
				WHERE subSysNo = '$subSysNo' AND  $objWhereStr ORDER BY instanceNo";
	//				echo "$paramSql";
	$Result_1 = mysqli_query($pubConn,$paramSql);	
	$prefix_no=0;			
	while($Row_1 = mysqli_fetch_array($Result_1))
	{
		if($Row_1[instanceNo]==$validPrefix[$prefix_no][inst_no])
		{
			$tempprefix=strtoupper($Row_1[initValue]);
			$prefix = explode('E',$tempprefix);
			if($prefix[0] =="")
				$prefix[0]="--";
			$validPrefix[$prefix_no][prefix]=$prefix[0];
			//echo "<br>asas->".$validPrefix[$prefix_no][prefix];
			$prefix_no++;
		}
		if($prefix_no >= $prefix_num)
			break;		
	}
	}//end get prefix value
	//exit;

	for($i=0; $i< $prefix_num; $i++)
	{
		//echo "<br>".$validPrefix[$i][inst_no].":".$validPrefix[$i][prefix].":".$validPrefix[$i][ton_in].":".$validPrefix[$i][ton_out];
		if($instNo==$validPrefix[$i][inst_no])
			continue;
		if(strcmp($setPrefix,$validPrefix[$i][prefix])==0
			&&($setTonIn==$validPrefix[$i][ton_in])
			/*&&($setTonOut==$validPrefix[$i][ton_out])*/)
		{
			echo "<script language='javascript'>";  
			echo "window.alert(\"Prefix, TON_in are all the same to PrefixNo:{$validPrefix[$i][inst_no]},Please check!\");";
			echo "history.go(-1)";
			echo "</script>";											 		
			exit();
		}
	}
}//end checkDupPrefix
?>
