var UPDATING = 1;
var UPDATE_FINISHED = 0;
var DVBS = 0;
var DVBS2 = 1;
var SymbolRateMin = 50;
var SymbolRateMax = 45000;
var FrequencyMin = 950000;
var FrequencyMax = 2150000;
var LevelMin = 73;
var LevelMax = 113;
var display = true;
var modStandardChangeFlag = false;  //增加调制标准改变标志值，当调制标准值发生变化时，向主控提交所有的调制参数

var url = window.location.href;
var intPos = url.indexOf("="); 
var slotno = url.substr(intPos + 1);

var outpParamName = new Array("cardAlarmSwitch", "symbolRate", "outpFreq", "outpLevel", "rfOutpSwitch");
var OUTP_PARAM_NUM = outpParamName.length;
var outpParamSetFlag = new Array(0,0,0,0,0);

var constelParamName = new Array("modStandard", "baseBandProcess", "fecRateConstellationS", "fecRateConstellationS2", "constellation", "fecFrameType", "referClock", "guidePilotSwitch", "spectrumInver", "rollOffFactor", "inputSource");
var CONSTEL_PARAM_NUM = constelParamName.length;
var constelSetFlag = new Array(0,0,0,0,0,0,0,0,0,0,0);

function outpParamSet(obj)
{
	for(var i = 0; i < OUTP_PARAM_NUM; i++)
	{
		if(obj.id == outpParamName[i])
		{
			outpParamSetFlag[i] = 1;
			break;
		}
	}
}

function constelParamSet(obj)
{
	for(var i = 0; i < CONSTEL_PARAM_NUM; i++)
	{
		if(obj.id == constelParamName[i])
		{
			constelSetFlag[i] = 1;
			break;
		}
	}
}

function ValidateAccuracy(value, num)
{
	var pieces = value.split(".");
	if(pieces.length > 1 && pieces[1].length > num)
	{
		return false;
	}
	return true;
}

function ValidateNum(item, type, str)
{
	var i;
	var strVal = new String(item.val());
	if(type == "SYMBOL_RATE")
	{
		var len = strVal.length;
		var error = 0;
		if(len == 0)
		{
			 alert("请输入合法的" + str);
			 item.focus();
			 return false; 
		}
		for(index = 0; index < len; index++)
		{
			if(isNaN(parseInt(strVal.charAt(index))))
			{
				error = 1;
			}
		}
		if((strVal < SymbolRateMin) || (strVal > SymbolRateMax))
		{
			error = 1;
		}
		if(error == 1)
	    {
			alert(str + "请输入在" + SymbolRateMin + "~" + SymbolRateMax + "之间的合法数值!");
			item.focus();
			return false;
	    }
	}
	else if(type == "FREQUENCY")
	{
		if((strVal == "") || (isNaN(strVal)))
	    {
			 alert("请输入合法的" + str);
			 item.focus();
			 return false; 
	    }
		if(ValidateAccuracy(strVal, 3) == false)
		{
			alert(str + "步进为1Hz!");
			item.focus();
			return false;
		}
		if((strVal < FrequencyMin) || (strVal > FrequencyMax))
		{
			alert(str + "请输入在" + FrequencyMin + "~" + FrequencyMax + "之间的合法数值!");
			item.focus();
			return false;
		}
	}
	else if(type == "OUTP_LEVEL")
	{
		if((strVal == "") || (isNaN(strVal)))
	    {
			 alert("请输入合法的" + str);
			 item.focus();
			 return false; 
	    }
		if((strVal < LevelMin) || (strVal > LevelMax))
		{
			alert(str + "请输入在" + LevelMin + "~" + LevelMax + "之间的合法数值!");
			item.focus();
			return false;
		}
		intVal = parseInt(parseFloat(strVal) * 10);
		if((ValidateAccuracy(strVal, 1) == false) || ((intVal % 5) != 0))
		{
			alert(str + "步进为0.5dBuV!");
			item.focus();
			return false;
		}
	}
}

function fecFrameTypeChange()
{
	var fecRateS2 = $("#fecRateConstellationS2").val();
	//前向纠错帧类型为短帧
	if($("#fecFrameType").val() == 1)   
	{
		//前向纠错和调制值为QPSK-9/10、8PSK-9/10、16APSK-9/10、32APSK-9/10
		if((fecRateS2 == 11) || (fecRateS2 == 17) || (fecRateS2 == 23) || (fecRateS2 == 28))  
		{
			alert("该值在短帧情况下无意义，请重新选择前向纠错和调制值！");
			$("#fecRateConstellationS2").val(1);
			constelSetFlag[3] = 1;
		}
	}
}
 
function fecRateChange()
{
	fecFrameTypeChange();
	if(($("#fecRateConstellationS2").val() >= 18) && ($("#fecRateConstellationS2").val() <= 28))
	{
		$("#constellation").attr("disabled", false);
	}
	else
	{
		$("#constellation").val(1);
		$("#constellation").attr("disabled", true);
	}
}

function modStandardDetect()
{
	if($("#modStandard").val() == DVBS)
	{
		$("#fecRateConstellationS").show();
		$("#fecRateConstellationS2").hide();
		$("#fecFrameType").attr("disabled", true);
		$("#guidePilotSwitch").attr("disabled", true);
		$("#constellation").val(1);
		$("#constellation").attr("disabled", true);
	}
	else if($("#modStandard").val() == DVBS2)
	{
		$("#fecRateConstellationS").hide();
		$("#fecRateConstellationS2").show();
		$("#fecFrameType").attr("disabled", false);
		$("#guidePilotSwitch").attr("disabled", false);
		fecRateChange();
	}
}

function modStandardChange()
{
	modStandardChangeFlag = true;
	modStandardDetect();
}

function paramDisplay() 
{
	$("#cardAlarmSwitch").val(eval(VID_CARD_ALARM_SWITCH));
	
	$("#inputSource").val(eval(VID_INPUT_SOURCE));
	$("#modStandard").val(eval(VID_MODULATION_STANDARD));
	$("#baseBandProcess").val(eval(VID_BASE_BAND_PROCESS));
	$("#fecRateConstellationS").val(eval(VID_FEC_RATE_CONSTELLATION_S));
	$("#fecRateConstellationS2").val(eval(VID_FEC_RATE_CONSTELLATION_S2));
	$("#constellation").val(eval(VID_CONSTELLATION));
	$("#fecFrameType").val(eval(VID_FEC_FRAME_TYPE));
	$("#referClock").val(eval(VID_REFER_CLOCK));
	$("#guidePilotSwitch").val(eval(VID_GUIDE_PILOT_SWITCH));
	$("#spectrumInver").val(eval(VID_SPECTRUM_INVER));
	$("#rollOffFactor").val(eval(VID_ROLL_OFF_FACTOR));
	
	$("#symbolRate").val(eval("VID_SYMBOL_RATE"));
	$("#outpFreq").val(eval("VID_OUTPUT_FREQ"));
	$("#outpLevel").val(eval("VID_OUTPUT_LEVEL"));
	$("#rfOutpSwitch").val(eval(VID_RF_OUTPUT_SWITCH));

	$("#curslot").val(slotno);	
	modStandardDetect();
	detectUpdateState();
}

function jump(id)
{
	if(id == 1)
	{
		window.location.href = "dvb_s_s2_param_config.asp?slotno=" + slotno;
	}
	else
	{
		window.location.href = "dvb_s_s2_status_info.asp?slotno=" + slotno;
	}
}

//form 提交
function submitCheck()
{
	var returnnum ;
	var cnt = 0;
	var i;
	returnnum = ValidateNum($("#symbolRate"), "SYMBOL_RATE", "<符号率>");
	if(returnnum == false)
	{
		cnt++;
	}
	
	returnnum = ValidateNum($("#outpFreq"), "FREQUENCY", "<输出频率>");
	if(returnnum == false)
	{
		cnt++;
	}

	returnnum = ValidateNum($("#outpLevel"), "OUTP_LEVEL", "<输出电平>");
	if(returnnum == false)
	{
		cnt++;
	}
	
	if(cnt == 0)
	{
		for(i = 0; i < OUTP_PARAM_NUM; i++)
		{
			if(outpParamSetFlag[i] == 0)
			{
				$("#" + outpParamName[i]).attr("disabled", true);
			}
		}
		
		if(modStandardChangeFlag == true)
		{
			for(i = 0; i < CONSTEL_PARAM_NUM; i++)
			{
				constelSetFlag[i] = 1;
			}
		}
		for(i = 0; i < CONSTEL_PARAM_NUM; i++)
		{
			if(constelSetFlag[i] == 0)
			{
				$("#" + constelParamName[i]).attr("disabled", true);
			}
		}
		$("#paramForm").submit();	
		setTimeout("self.location.reload(true)",1000);
	}
}

function disableAllParam()
{
	var i;
	for(i = 0; i < OUTP_PARAM_NUM; i++)
	{
		$("#" + outpParamName[i]).attr("disabled", true);
	}
	
	for(i = 0; i < CONSTEL_PARAM_NUM; i++)
	{
		$("#" + constelParamName[i]).attr("disabled", true);
	}
}

function LockUI()
{
	var option = {
		close:false,
		opacity:60,
		minHeight:100,
		minWidth:500,
		maxHeight:120,
		maxWidth:600
	};
	
	$("#UpdateLock").modal(option);
}

function UnlockUI()
{
	$.modal.close(); 
}

var xmlHttp; 
function createXmlHttp()
{
    xmlHttp = false;
    if(window.ActiveXObject) 
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest) 
    {
    	xmlHttp = new XMLHttpRequest();
    }
}

function detectUpdateState()
{
    createXmlHttp();
    if(!xmlHttp)
    {
        alert("XMLHttpRequest is not Create!");
    }
    xmlHttp.open("GET", "dvb_s_s2_update_state.asp?slotno=" + slotno, true);
    xmlHttp.onreadystatechange = function()
	{
       if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
	   {
       		var strVal = xmlHttp.responseText;
			var index = strVal.indexOf("VID_CARD_UPDATING");
			var updateStatus = strVal[index + 21];

			if(eval(updateStatus) == UPDATE_FINISHED)
			{
				UnlockUI();
			}
			else if(eval(updateStatus) == UPDATING)
			{
				LockUI();
			}
        }
    }
    xmlHttp.setRequestHeader("If-Modified-Since", "0");
    xmlHttp.send(null);
    setTimeout("detectUpdateState()", 30000);
}

function setValue(num)
{
	if(num == 1)
	{
		if(confirm("确定要升级bootloader文件吗？") == true)
		{
			$("#updateBootloader").val(1);
			$("#updateBootloader").attr("disabled", false);
			$("#updateVxWorks").attr("disabled", true);
			disableAllParam();
			$("#paramForm").submit();	
			LockUI();
		}
		else
		{
			self.location.reload(true);
		}
	}
	else
	{
		if(confirm("确定要升级VxWorks文件吗？") == true)
		{
			$("#updateVxWorks").val(1);
			$("#updateBootloader").attr("disabled", true);
			$("#updateVxWorks").attr("disabled", false);
			disableAllParam();
			$("#paramForm").submit();	
			LockUI();
		}
		else
		{
			self.location.reload(true);
		}
	}
}

function updateConfigDisplay()
{
	if(display == true)
	{
		$("#updateConfig").show();
		$("#advancedDisplay").val("隐藏高级选项");
		display = false;
	}
	else
	{
		$("#updateConfig").hide();
		$("#advancedDisplay").val("显示高级选项");
		display = true;
	}
}