<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="GB2312">
    <title></title>
    <link href="css/style.css" title="style" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.6.3.min.js"></script>

</head>
<body style="background: #F3F3F3">
<div>
<div class="list2" style="margin: 5px;">
    <a class="you_p" href="#" style="color: #fff">485设备</a>
  </div>
  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>基本设置</a>&nbsp;&nbsp;<b><span id="anote"></span></b>
  </div>
  <div class='setbase' style='border: #B0B7BF 1px solid'>
    <table  width='100%'>
    <tr><th width="15%"><span>监测项描述:</span></th><th style='text-align: left'><input id="tname" value="" style="width:250px"></input></th><th width='25%' style='text-align: left'>方便记忆</th></tr>
    <tr style="display:none"><th><span>设备描述:</span></th><th style='text-align: left'><input id="tnote" value="描述内容" style="width:250px"></input></th><th style='text-align: left'>帮助了解位置等</th></tr>
    <tr><th><span>端  口:</span> </th><th style='text-align: left'><select id="tport" style="width:150px" onchange="onport(this)">
      <option value="1">RS485 A
      <option value="2">RS485 B
      </select></th><th style='text-align: left'>端口选择</th style='text-align: left'></tr>
    <tr><th><span>波特率:</span> </th><th style='text-align: left'><select id="tbaud" style="width:150px">
      <option value="115200">115200
      <option value="57600">57600
      <option value="56000">56000
      <option value="38400">38400
      <option value="19200">19200
      <option value="14400">14400
      <option value="9600">9600
      <option value="4800">4800
      <option value="2400">2400
      <option value="1200">1200
      <option value="600">600
      </select></th><th style='text-align: left'>默认：9600&nbsp;</th></tr>
    <tr><th><span>数据位:</span> </th><th style='text-align: left'><select id="tbits" style="width:150px">
      <option value="8">8位
      <option value="7">7位
      <option value="6">6位
      <option value="5">5位
      </select></th><th style='text-align: left'>默认：8&nbsp;</th></tr>
    <tr><th><span>停止位:</span> </th><th style='text-align: left'><select id="tstop" style="width:150px">
      <option value="1">1
      <option value="1.5">1.5
      <option value="2">2
      </select></th><th style='text-align: left'>默认：1&nbsp;</th></tr>
    <tr><th><span>校验位:</span> </th><th style='text-align: left'><select id="tchk" style="width:150px">
      <option value="n">无
      <option value="o">奇
      <option value="e">偶
      </select></th><th style='text-align: left'>默认：无&nbsp;</th></tr>
    <tr><th><span>编  址:</span> </th><th style='text-align: left'><input id="taddr" value="12" style="width:150px"></input></th><th style='text-align: left'>编址范围1-254</th></tr>
   <tr><th><span>其  他:</span> </th><th style='text-align: left'><input id="text1" value="0" style="width:100px"></input>&nbsp;(扩展设置详见&nbsp;<a href="omdev-help.html" target="content">配置参考</a>)&nbsp;&nbsp;&nbsp;<input id="tdatam" type="checkbox" name="tdatam" value="1">采集数据</input></th><th style='text-align: left'></th></tr>
    </table>
  </div>
  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>参数设置</a>
    <span>&nbsp;&nbsp;&nbsp;<input id="pdefault" type="checkbox" name="pdefault" value="1">保存为默认设置</input></span>
  </div>
  <div id='setparam' style='border: #B0B7BF 1px solid'>
    <table  width='100%'>
      <tr><th><span>温  度:</span></th><th style='text-align: left'>
        <input value="20" style="width:100px"></input>&nbsp(最低允许温度)&nbsp;&nbsp;&nbsp;
        <input value="33" style="width:100px"></input>(最高允许温度)</th><th style='text-align: left'>&nbsp;参考范围:20~33</th></tr>
      <tr><th><span>湿  度:</span></th><th style='text-align: left'>
        <input value="20" style="width:100px"></input>&nbsp(最低允许湿度)&nbsp;&nbsp;&nbsp;
        <input value="33" style="width:100px"></input>(最高允许湿度)</th><th style='text-align: left'>&nbsp;参考范围:46~80</th></tr>
    </table>
  </div>
  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>告警设置</a>
    <span>&nbsp;&nbsp;&nbsp;模板选择：
    <select id="lsAlm" style="width:150px" onchange="onAlmSel()">
    <option value="0">无(不告警)
    <option value="1">声光告警
    <option value="2">短信告警
    <option value="3">电话告警
    <option value="4">邮件告警
    <option value="5">声光+短信+电话告警
    <option value="6">自定义告警
    </select></span>
  </div>
  <div id='setalm' style='border: #B0B7BF 1px solid'>
    <table  width='100%'>
      <tr><th width="15%"><span>预警保持时间(s):</span></th><th style='text-align: left'><input id="atimeon" value="5" style="width:100px"></input></th><th width='25%' style='text-align: left'>&nbsp;数值范围:0~9999</th></tr>
      <tr><th><span>重复告警间隔(s):</span></th><th style='text-align: left'><input id="atimeredo" value="900" style="width:100px"></input></th><th style='text-align: left'>&nbsp;数值范围:0~9999</th></tr>
      <tr><th><span>告警发送方式:</span></th><th style='text-align: left'>
        <input type="checkbox" name="almmask" value="1">声光</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="2">语音</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="4">短信</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="8">彩信</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="16">电话</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="32">邮件</input>&nbsp;&nbsp;
		<input type="checkbox" name="almmask" value="256">联动1</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="512">联动2</input>&nbsp;&nbsp;</th></tr>
      <tr><th><span>告警时段选择:</span></th><th style='text-align: left'>
        <select id="lsAlmTime" style="width:150px">
        <option value="0">全时段
        <option value="1">工作时段
        <option value="2">周六周日
        <option value="3">自定义时段
        </select></th><th>&nbsp;</th></tr>
      <tr><th><span>告警抓拍设置:</span></th><th style='text-align: left'>
        <select id="lsAlmCam" style="width:250px">
        <option value="0">无
        </select></th><th>&nbsp;</th></tr>
      <tr><th><span>告警短信内容:</span></th><th style='text-align: left'>
        <textarea id="almsms" style='width: 400px'>短信内容</textarea></th><th>&nbsp;</th></tr>
      <tr><th><span>告警电话内容:</span></th><th style='text-align: left'>
        <textarea id="almtel" style='width: 400px'>电话内容</textarea></th><th>&nbsp;</th></tr>
      <tr><th><span>告警解除方式:</span></th><th style='text-align: left'>
        <input type="checkbox" name="offmask" value="1">声光</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="2">语音</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="4">短信</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="8">彩信</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="16">电话</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="32">邮件</input>&nbsp;&nbsp;
		<input type="checkbox" name="offmask" value="256">联动1</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="512">联动2</input>&nbsp;&nbsp;</th></tr>
      <tr><th><span>告警解除内容:</span></th><th style='text-align: left'>
        <textarea id="almoff" style='width: 400px'>告警解除内容</textarea></th><th>&nbsp;</th></tr>
      <tr><th><span>信息接收人:</span></th><th id="lsRecv" style='text-align: left'>
        <input type="checkbox" name="arecv">管理员</input>&nbsp;&nbsp
        <input type="checkbox" name="arecv">用户1</input>&nbsp;&nbsp;
    </table>
  </div>

</div>
<div>
  <table width='100%'><tr>
    <th><a id='abtn' href='javascript:saveclick()' class='back' style='color: #fff;background: #ffa225'>保存</a></th>
    <th><a href='javascript:history.back()' class='back' style='color: #fff'>返回</a></th>
  </tr></table>
</div>

<script>

var itype=<% write(itype);%>;
var isid=<% write(sid);%>;
var aidType=0;
var aidSIDx=0;

<% db_Open("/NandFlash/dbCfg.db3"); %>
var jdef=<% db_QryAdv("select s.*,v.vmode as Dvmode,v.vref as Dvref,v.vref2 as Dvref2,v.ANote as VNote from tbSMDef s left join tbSMDefVal v on (v.Type=s.Type and v.Fld=s.Fld and v.SID="+sid+") where s.Type="+itype+" and s.vmode>=0 order by FldDept,Fld",0); %>;
var idefv=<% write(db_QryInt("select count(*) from tbSMDefVal where Type="+itype+" and SID="+sid,1)); %>;
var jport=<% db_QryAdv("select * from tbPort where Type=4 order by Type,Name",0); %>;
var jsid=<% db_QryAdv("select s.*,k.Name as KName,t.Name as TName,t.ExtDi,t.ExtSer from tbSM s left join tbSMKind k on k.Kind=s.Kind left join tbSMType t on t.Type=s.Type and t.Tag1=s.Tag1 where SID="+sid,0); %>;
var jcam=<% db_QryAdv("select s.SID,s.Name,t.Name as TName from tbSM s left join tbSMType t on t.Type=s.Type and t.Tag1=s.Tag1 where s.Type=3072 or s.Type=3073",0); %>;
var jalm=<% db_QryAdv("select * from tbAlm where AID<16",0); %>;
var jalmType=<% db_QryAdv("select * from tbAlm where AID="+itype,0); %>;
var jalmSid=<% db_QryAdv("select * from tbAlm where AID="+(sid+4096),0); %>;
var jrecv=<% db_QryAdv("select Name,Recv from tbUser where State=0 and Recv>0",0); %>;
var jalmTime=<% db_QryAdv("select TID, Note from tbAlmTime",0); %>;
<% db_Close(); %>

listAlmTime();
function listAlmTime(){
  if(jalmTime.reccnt==0) return;
  var s="";
  $.each(jalmTime.recs, function(i,n){
    s+="<option value='"+n.TID+"'>"+n.Note;
  });
  $("#lsAlmTime").html(s);
}

listP();
function listP(){
  if(jport.reccnt==0) return;
  var s="";
  $.each(jport.recs, function(i,n){
    s+="<option value='"+n.Val+"'>"+SACtoCOM(n.Name,n.Val);
  });
  $("#tport").html(s);
}

listSID();
function listSID(){
  if(jsid.reccnt==0) return;
  //$("#anote").html(jsid.recs[0].Name+" SID:"+isid+" 编址:"+jsid.recs[0].SerAddr+" ("+jsid.recs[0].KName+" "+jsid.recs[0].TName+")");
  $("#anote").html(jsid.recs[0].KName+" ("+jsid.recs[0].TName+")");
  $("#tname").val(jsid.recs[0].Name);
  $("#tnote").val(jsid.recs[0].Note);
  $("#tport").val(jsid.recs[0].SerType);
  var sdef=jsid.recs[0].SerDef;
  var iv=sdef.indexOf(":");
  if(iv>0){
    sdef=sdef.substr(iv+1);
    var defs;
    defs=sdef.split(",");
    iv=defs.length;
    if(iv>0) $("#tbaud").val(defs[0]);
    if(iv>1) $("#tchk").val(defs[1]);
    if(iv>2) $("#tbits").val(defs[2]);
    if(iv>3) $("#tstop").val(defs[3]);
  }
  onport(null);
  $("#taddr").val(jsid.recs[0].SerAddr);
  $("#text1").val(jsid.recs[0].Ext1);
  $("#tdatam").attr("value",jsid.recs[0].DataMode);
  if(jsid.recs[0].DataMode)
    $("#tdatam").prop("checked",true);
  else
    $("#tdatam").prop("checked",false);
}

listCam();
function listCam(){
  if(jcam.reccnt==0) return;
  var s="<option value='0'>无";  
  $.each(jcam.recs, function(i,n){
    s+="<option value='"+n.SID;
    s+="'>"+n.Name+" ("+n.TName+")</input>&nbsp;&nbsp;";
  });
  $("#lsAlmCam").html(s);
}

listRecv();
function listRecv(){
  if(jrecv.reccnt==0) return;
  var s="";
  $.each(jrecv.recs, function(i,n){
    s+="<input type='checkbox' name='arecv' value='"+n.Recv+"'>"+n.Name+"</input>&nbsp;&nbsp;";
  });
  $("#lsRecv").html(s);
}

listAlm();
function listAlm(){
  if(jalm.reccnt==0) return;
  var s="";
  $.each(jalm.recs, function(i,n){
    s+="<option value='"+n.AID+"'>"+n.Note;
    if(n.Note.indexOf("按类型")>=0) aidType=n.AID;
    if(n.Note.indexOf("自定义")>=0) aidSIDx=n.AID;
  });
  $("#lsAlm").html(s);
  if(jsid.reccnt>0){
    if (jsid.recs[0].AID<16)
      $("#lsAlm").val(jsid.recs[0].AID);
    else{
      if(jsid.recs[0].AID==jsid.recs[0].Type)
        $("#lsAlm").val(aidType);
      else
        $("#lsAlm").val(aidSIDx);
    }
    onAlmSel();
  }
}

$(function(){
    listParam();
});

function isNumber(obj) {
    return typeof obj === 'number' && !isNaN(obj)
}
function listParam() {
  if(jdef.reccnt==0) {
    $("#setparam").html("&nbsp;");
    //$("#setdetail").hide();
    return;
  }
  if(itype==512 || (itype>2304 && itype<2560)){
    idefv=1;
    $("#pdefault").attr("disabled","disabled");
  }
  
	if(itype==1027)
	{
		jsid.recs[0].Ext1 = 0;
	}
  if(idefv==0)
    $("#pdefault").prop("checked",true);
  else
    $("#pdefault").prop("checked",false);
  var stbl="<table width='100%'>";
  var ichks = 0;
  //$.each(jdef.recs,function(i,n){
  for(var i=0; i<jdef.reccnt; i++){
    var n=jdef.recs[i];
	if(itype==1027)
	{
		if (n.Fld == 1)
			jsid.recs[0].Ext1 = jsid.recs[0].Ext1 | (n.vref&0xff);
		else if (n.Fld == 2)
			jsid.recs[0].Ext1 = jsid.recs[0].Ext1 | ((n.vref&0xff) << 8);
		else if (n.Fld == 3)
			jsid.recs[0].Ext1 = jsid.recs[0].Ext1 | ((n.vref&0xff) << 16);
		else if (n.Fld == 4)
			jsid.recs[0].Ext1 = jsid.recs[0].Ext1 | ((n.vref&0xff) << 24);
	}
    if (isNumber(n.Dvmode))
      n.vmode=n.Dvmode;
    if (isNumber(n.Dvref))
      n.vref=n.Dvref;
    if (isNumber(n.Dvref2))
      n.vref2=n.Dvref2;
    var ivmode=n.vmode;
    if(n.Fld==0){
      if(ichks==1) stbl+="</th></tr>";
	  if(n.ASubSys == 2)
	  {
			
				  stbl+="<tr ><th width='15%'  style='padding: 10px;background:#E5E9EC'>&nbsp</th><th style='padding: 10px;background:#E5E9EC'>"+n.Note+"&nbsp&nbsp<span style='display:none'><input id='eachset' type='checkbox' name='eachset' onclick='clickeachSet()' value='1'  checked='checked'>单独设置</th></span><th width='25%' style='padding: 10px;background:#E5E9EC'>&nbsp;</th></tr>";
			
				stbl+="<tr class='allset' style='display:none'><th width ='15%'>蓄电池电压值</th><th style='text-align: left'>";
				stbl+="<select style='width:100px' id='pmode"+0+"'>";
				
				n = jdef.recs[i+1];
				ivmode=n.vmode;
				if(ivmode<10){
				  stbl+="<option value='0'>不检测<option value='1'";
				  if(ivmode==1) stbl+=" selected='true'"
				  stbl+=">等于<option value='2'";
				  if(ivmode==2) stbl+=" selected='true'"
				  stbl+=">不等于<option value='3'";
				  if(ivmode==3) stbl+=" selected='true'"
				  stbl+=">大于<option value='4'";
				  if(ivmode==4) stbl+=" selected='true'"
				  stbl+=">小于";
				}
				else{
				  stbl+="<option value='10'>不检测<option value='11'";
				  if(ivmode==11) stbl+=" selected='true'"
				  stbl+=">范围内<option value='12'";
				  if(ivmode==12) stbl+=" selected='true'"
				  stbl+=">范围外";
				}
				stbl+="</select>&nbsp;&nbsp;&nbsp;";
				if(ivmode<10){
				  stbl+="<input id='pref"+0+"' value='"+(n.vref/n.vfactor)+"' style='width:100px'/>&nbsp;";//+n.vtip;
				  
				}
				else {
				  stbl+="<input id='pref"+0+"' value='"+(n.vref/n.vfactor)+"' style='width:100px'/>&nbsp;~&nbsp;&nbsp;&nbsp;";
				  stbl+="<input id='prefb"+0+"' value='"+(n.vref2/n.vfactor)+"' style='width:100px'/>&nbsp;&nbsp;&nbsp;&nbsp;";//+n.vtip;
				}
	  }
	  else
	  {
			<!--stbl+="<tr><th width='15%'  style='padding: 10px;background:#E5E9EC'>&nbsp</th><th style='padding: 10px;background:#E5E9EC'>"+n.Note+"</th><th width='25%' style='padding: 10px;background:#E5E9EC'>&nbsp;</th></tr>";-->
			stbl+="<tr ><th width='15%'  style='padding: 10px;background:#E5E9EC'>&nbsp</th><th style='padding: 10px;background:#E5E9EC'>"+n.Note+"&nbsp&nbsp<span style='display:none'><input id='eachset' type='checkbox' name='eachset' onclick='clickeachSet()' value='1'  checked='checked'>单独设置</th></span><th width='25%' style='padding: 10px;background:#E5E9EC'>&nbsp;</th></tr>";
	  }
      ichks = n.vkind;
      if(ichks==1)
        stbl+="<tr><th>"+n.Note+"告警选择</th><th width ='15%'>";
    }
    else if(n.Note!='' && ivmode>=0){
      if(ichks==1) {
        stbl+="<input id='pmode"+n.Fld+"' type='checkbox'";
        if(ivmode) stbl+=" checked='true'";
        stbl+="/>"+n.Note+"&nbsp;&nbsp;&nbsp";
      }
      else{
		if(n.FldDept == 2 && n.Type ==1024)
		{
			stbl+="<tr class='eachsetc' ><th width ='15%'>"+n.Note+"</th><th style='text-align: left'>";
		}
		else
		{
			 stbl+="<tr ><th width ='15%'>"+n.Note+"</th><th style='text-align: left'>";
		}

        stbl+="<select style='width:100px' id='pmode"+n.Fld+"'>"
        if(ivmode<10){
          stbl+="<option value='0'>不检测<option value='1'";
          if(ivmode==1) stbl+=" selected='true'"
          stbl+=">等于<option value='2'";
          if(ivmode==2) stbl+=" selected='true'"
          stbl+=">不等于<option value='3'";
          if(ivmode==3) stbl+=" selected='true'"
          stbl+=">大于<option value='4'";
          if(ivmode==4) stbl+=" selected='true'"
          stbl+=">小于";
        }
        else{
          stbl+="<option value='10'>不检测<option value='11'";
          if(ivmode==11) stbl+=" selected='true'"
          stbl+=">范围内<option value='12'";
          if(ivmode==12) stbl+=" selected='true'"
          stbl+=">范围外";
        }
        stbl+="</select>&nbsp;&nbsp;&nbsp;";
        if(ivmode<10){
          stbl+="<input id='pref"+n.Fld+"' value='"+(n.vref/n.vfactor)+"' style='width:100px'/>&nbsp;";//+n.vtip;
		  
        }
        else {
          stbl+="<input id='pref"+n.Fld+"' value='"+(n.vref/n.vfactor)+"' style='width:100px'/>&nbsp;~&nbsp;&nbsp;&nbsp;";
          stbl+="<input id='prefb"+n.Fld+"' value='"+(n.vref2/n.vfactor)+"' style='width:100px'/>&nbsp;&nbsp;&nbsp;&nbsp;";//+n.vtip;
        }
        if(itype==512){
          stbl+="</th><th style='text-align: left'>描述:&nbsp;<input id='pnote"+n.Fld+"' value='"+n.VNote+"'/></th></tr>";
        }
        else
          stbl+="</th><th  width='25%' style='text-align: left'>"+n.vtip+"</th></tr>";
      }
    }
  }
  //})
  if (ichks==1) stbl+="</th><th></th></tr>";
  stbl += "</table>";
  $("#setparam").html(stbl);
  $("#text1").val(jsid.recs[0].Ext1);
}

//单独设置选中
function clickeachSet()
{
	var obj=document.getElementById("eachset");
	if (obj.checked) 
	{	
		$('.eachsetc').attr('style', 'display:');
		$('.allset').attr('style', 'display:none');
	}
	else 
	{
		$('.allset').attr('style', 'display:');
		$('.eachsetc').attr('style', 'display:none');
	}
		
}

function saveParam(){
  var idefault=0;
  if($("#pdefault").attr("checked")=="checked")
    idefault=1;
  else
  {
		$.ajax({
		  type: "POST",
		  url: "omdev/smdef.asp",
		  data: "op=chkx&type="+itype+"&sid="+isid,
		  async: false,
		  dataType:"json",
		  success: function(msg){
			if(msg.op!="ok")
			  alert("准备自定义参数失败!");
		  }
		});
  }
  
  var ichks=0;
  var pmode;
  var ext1=0;
  $.each(jdef.recs,function(i,n){
    if(n.Fld==0)
	{
      ichks = n.vkind;
    }
    else if(n.Note!='' && n.vmode>=0)
	{
		  if($("#eachset").attr("checked")!="checked" && n.Fld > 6 && n.Type == 1024)
		{
			pmode=$("#pmode"+0);
		}
		else
		{
			pmode=$("#pmode"+n.Fld);
		}  
		  var imode=pmode.val();
		  var iref=0;
		  var iref2=0;
		  var vnote=n.VNote;
		  if(ichks==1)
		  {
			if (pmode.prop("checked")) imode=1; else imode=0;
			iref=n.vref;
			iref2=n.vref2;
		  }
		  else
		  {
				 if($("#eachset").attr("checked")!="checked" && n.Fld > 6 && n.Type == 1024)
				{
					iref=parseInt(parseFloat($("#pref"+0).attr("value"))*n.vfactor);
				}
				else
				{
					iref=parseInt(parseFloat($("#pref"+n.Fld).attr("value"))*n.vfactor);
				}  
				
		  }
		  if (imode>=10)
		  {
				 if($("#eachset").attr("checked")!="checked" && n.Fld > 6 && n.Type == 1024)
				{
					iref2=parseInt(parseFloat($("#prefb"+0).attr("value"))*n.vfactor);
				}
				else
				{
					iref2=parseInt(parseFloat($("#prefb"+n.Fld).attr("value"))*n.vfactor);
				}  
				 
		  }
		   
		  if(itype==512)
			vnote=$("#pnote"+n.Fld).attr("value");
		  if(imode!=n.vmode || iref!=n.vref || iref2!=n.vref2 || vnote!=n.VNote)
		  {
				var sop="updd";
				var spost="";
				spost+="&sid="+isid;
				if(idefault)
				{
				  spost+="&setval=vmode="+imode;
				  if(ichks==0)
				  {
					spost+=",vref="+iref;
					spost+=",vref2="+iref2;
				  }
				}
				else
				{
				  sop="updx";
				  spost+="&setval="+itype+","+isid+","+n.Fld+","+imode;
				  if(ichks==0)
					spost+=","+iref+","+iref2;
				  else
					spost+=",'',''";
				  spost+=",0,'"+vnote+"'";
				}
				
				if(itype==1027)
				{
					if (n.Fld == 1)
						ext1 = ext1 | (iref&0xff);
					else if (n.Fld == 2)
						ext1 = ext1 | ((iref&0xff) << 8);
					else if (n.Fld == 3)
						ext1 = ext1 | ((iref&0xff) << 16);
					else if (n.Fld == 4)
						ext1 = ext1 | ((iref&0xff) << 24);
				}
				$.ajax({
				  type: "POST",
				  url: "omdev/smdef.asp",
				  data: "op="+sop+"&type="+itype+"&fld="+n.Fld+spost,
				  async: false,
				  dataType:"json",
				  success: function(msg){
					if(msg.op!="ok")
					  alert("保存 "+n.Note+" 失败!");
				  }
				});
		  }
    }
  });
  
  return ext1;
}

function onport(obj){
  var iv=$("#tport").val();
  var attr="disabled";
  if(iv&0xff0000){
    $("#tbaud").removeAttr(attr);
    $("#tbits").removeAttr(attr);
    $("#tstop").removeAttr(attr);
    $("#tchk").removeAttr(attr);
  }
  else{
    $("#tbaud").attr(attr,attr);
    $("#tbits").attr(attr,attr);
    $("#tstop").attr(attr,attr);
    $("#tchk").attr(attr,attr);
  }
}

//文本 "ttySAC1:2400,n,8,1" to COM 显示
function SACtoCOM(str , val)
{
	if(str.length<=0)
	{
		return "";
	}
	/*if((val & 0xf00000) ==0x100000)
	{
		var resultstr="COM";
		var s = str;
		resultstr =resultstr + (parseInt(s.substring(6,7))+1)+ s.substring(7);
	}
	else*/
	{
		resultstr = str;
	}
	return resultstr;
}

//文本 COM to "ttySAC1:2400,n,8,1" 取值
function COMtoSAC(str , val)
{
	if(str.length<=0)
	{
		return "";
	}
	/*if( (val & 0xf00000) ==0x100000 )
	{
		var resultstr="";
		var s = str;
		resultstr = "ttySAC" + (parseInt(s.substring(3,4))-1) + ":";
	}
	else*/
	{
		var resultstr=str;
	}
	
	
	return resultstr;
}
function onAlmSel(){
  var idx=$('#lsAlm').prop('selectedIndex');
  var alm=jalm.recs[idx];
  if(alm.AID==aidType){
    if(jalmType.reccnt>0) showAlm(jalmType.recs[0]);
  }
  else if(alm.AID==aidSIDx){
    if(jalmSid.reccnt>0) showAlm(jalmSid.recs[0]);
  }
  else
    showAlm(alm);
}

function showAlm(alm){
  $("#atimeon").val(alm.TimeOn);
  $("#atimeredo").val(alm.TimeRedo);
  showAlmMask(alm.AlmMask);
  $("#lsAlmTime").val(alm.AlmTid);
  $("#lsAlmCam").val(alm.AlmCam);
  $("#almsms").val(alm.AlmSms);
  $("#almtel").val(alm.AlmTel);
  showOffMask(alm.AlmOffMask);
  $("#almoff").val(alm.AlmOff);
  showRecv(alm.AlmRecv);
}
function showAlmMask(imask){
  $("input[name='almmask']").each(function(){
    var iv=parseInt(this.value);
    if(iv & imask)
      $(this).attr("checked",true);
    else
      $(this).attr("checked",false);
  });
}
function showOffMask(imask){
  $("input[name='offmask']").each(function(){
    var iv=parseInt(this.value);
    if(iv & imask)
      $(this).attr("checked",true);
    else
      $(this).attr("checked",false);
  });
}
function showRecv(irecv){
  $("input[name='arecv']").each(function(){
    var iv=parseInt(this.value);
    if(irecv & (1<<(iv-1)))
      $(this).attr("checked",true);
    else
      $(this).attr("checked",false);
  });
}

function saveAlm(){
  var spost="&setval=";
  spost+="TimeOn="+$("#atimeon").attr("value");
  spost+=",TimeRedo="+$("#atimeredo").attr("value");
  spost+=",AlmMask="+getAlmMask();
  spost+=",AlmTid="+$("#lsAlmTime").val();
  spost+=",AlmCam="+$("#lsAlmCam").val();
  spost+=",AlmSms='"+$("#almsms").attr("value");
  spost+="',AlmTel='"+$("#almtel").attr("value");
  spost+="',AlmOffMask="+getOffMask();
  spost+=",AlmOff='"+$("#almoff").attr("value");
  spost+="',AlmRecv="+getRecv();
  var idx=$('#lsAlm').prop('selectedIndex');
  var alm=jalm.recs[idx];
  var iaid=alm.AID;
  if(alm.AID==aidSIDx){
    iaid=isid+0x1000;
    spost+=",Note='"+$("#tname").attr("value")+" 告警'";
  }
  else if(alm.AID==aidType){
    iaid=itype;
    idx=$('#lsType').prop('selectedIndex');
    spost+=",Note='"+jtype.recs[idx].KName+" 告警'";
  }
  
  $.ajax({
    type: "POST",
    url: "omdev/almx.asp",
    data: "op=upd&aid="+iaid+"&sid="+isid+spost,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
        history.back();
    }
  });
}
function getAlmMask(){
  var iv=0;
  $("input[name='almmask']:checked").each(function(){
    iv+=parseInt(this.value);
  });
  return iv;
}
function getOffMask(){
  var iv=0;
  $("input[name='offmask']:checked").each(function(){
    iv+=parseInt(this.value);
  });
  return iv;
}
function getRecv(){
  var iv=0;
  $("input[name='arecv']:checked").each(function(){
    iv|=(1 << (parseInt(this.value)-1));
  });
  return iv;
}

function saveclick(){
  var sname="";
  var saddr="";
  var flag =0;
  sname=$("#tname").attr("value");
  saddr=$("#taddr").attr("value");
  var sdef=COMtoSAC($("#tport").find("option:selected").text(),$("#tport").find("option:selected").val());
  if (sname=="" || saddr==""){
    alert("错误: 输入不完整,请重新输入!");
    return;
  }
  
   $.ajax({
		type: "POST",
		url: "omdev/smbyport.asp",
		data: "port="+4,
		contentType:"application/x-www-form-urlencoded; charset=gb2312",
		dataType:"json",
		timeout : 5000,
		async: false,
		success: function(msg){
		  $.each(msg.recs,function(i,n){
			if(isid != n.SID && n.SerAddr == saddr && sdef == n.SerDef.substring(0,8))
					flag ++;
			});
		}
		
	});
	
	if (flag >= 1)
	{
		alert("错误: 相同端口地址已经存在");
		return;
	}
	 
  
  var ext1 = saveParam();

  var sdef=COMtoSAC($("#tport").find("option:selected").text(),$("#tport").find("option:selected").val());
  var iv=$("#tport").val();
  if (iv&0x100000){
    sdef+=$("#tbaud").val();
    sdef+=","+$("#tchk").val();
    sdef+=","+$("#tbits").val();
    sdef+=","+$("#tstop").val();
  }
  var spost="";
  spost+="&sid="+isid;
  spost+="&name=";
  if(jsid.recs[0].ExtDi>0 || jsid.recs[0].ExtSer>0 && sname!=jsid.recs[0].Name)
    spost+=sname;
  spost+="&setval=Name='"+sname;
  spost+="',Note='"+$("#tnote").attr("value");
  spost+="',SerDef='"+sdef;
  spost+="',SerKind="+(iv & 0xffff00)+",SerType="+iv;
  spost+=",SerAddr="+saddr;
  var sv=$("#text1").attr("value");
  if(sv=="") sv="0";
  if ((itype==1027) && (ext1 >= 0))
	sv = ext1;
  spost+=",Ext1="+sv;
  iv=$("#tdatam").attr("value");
  sv=$("#tdatam").attr("checked");
  if(iv==1 && sv!="checked")
    spost+=",DataMode=0";
  if(iv==0 && sv=="checked")
    spost+=",DataMode=1";

  $.ajax({
    type: "POST",
    url: "omdev/smx.asp",
    data: "op=upd"+spost,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
        saveAlm(); //history.back();
      else
        alert("保存 "+sname+" 编号SID="+isid+" 数据失败!");
    }
  });
}
</script>
</body>
</html>