var ENGLISH = 0;
var CHINESE = 1;

var SEARCH_ALL = 4;
var PORT_GOTO = 1;
var PORT_SEARCH = 2;

var SHARING = 1;

function enter_down(form, event)
{ 
	if(event.keyCode== "13")
	{
	  stopDefault(event);
	  submitport(PORT_GOTO);
	}
}
  
function stopDefault(e)
{
	//如果提供了事件对象，则这是一个非IE浏览器 
	if(e && e.preventDefault)
	{
	　　//阻止默认浏览器动作(W3C)
	　　e.preventDefault();
	} 
	else
	{
	　　//IE中阻止函数器默认动作的方式 
	　　window.event.returnValue = false; 
	}
	return false;
}

$(document).ready(function()
{
	funcUpdateDisplay();
	$("#checkall").click(function()
	{
		for(var i=0; i<prognum; i++)
		{
			var progno = ProgNo[i];
			for(var j=0; j<PidNum[i]; j++)
			{
				$("input[name='PidCheck"+progno+"_"+j+"']").attr("checked",$(this).attr("checked"));
			}				 			
		}
		if($("#checkall").attr("checked"))
		{
			for(var i = 0; i < prognum; i++)
			{
				var progno = ProgNo[i];
				enableDetail(progno, false);			 			
			}
			if(value_type == "1")
			{
				var validnum = value_prg - scrambled_prg_other
				if((prognum > validnum)||(prognum > 1))
				{
					if(language == ENGLISH)
					{
						alert("Beyond the program authorization number!");
					}
					else
					{
						alert("超出节目授权个数！");
					}					
					return;
				}
			}
		}
		else
		{
			for(var i = 0; i < prognum; i++)
			{
				var progno = ProgNo[i];
				enableDetail(progno, true);			 			
			}
		}
	});
});

function funcUpdateDisplay()
{
	$("#curslot").val(cardid);
	$("#curport").val(portid);
	for(var i = 0; i < prognum; i++)
	{
		var progno = ProgNo[i];
		var tempcnt=0;
		for(var j = 0; j < PidNum[i]; j++)
		{
			if(eval("PidCheck_"+progno+"["+j+"]") == "1")
			{
				tempcnt++;	
			}			
		}
		if(tempcnt != 0)
		{
			enableDetail(progno, false);
		}
		else
		{
			enableDetail(progno, true);
		}
	}
}

function changelist(progid,pidid)
{
	if(value_type == "1")//当前授权类型为节目授权
	{
		var validnum = value_prg - scrambled_prg_other//当前端口允许加扰的节目数
		var progcount = 0;//该变量为遍历得到的当前加扰的节目数
		for(var i = 0; i < prognum; i++)
		{
			var progno = ProgNo[i];
			var tempcount=0;
			for(var j = 0; j < PidNum[i]; j++)
			{
				if($("#PidCheck"+progno+"_"+j).attr("checked"))
				{
					tempcount++;
				}		
			}
			if(tempcount != 0)
			{
				progcount++;
			}
			if((progcount > validnum)||(progcount > 1))
			{
				if(language == ENGLISH)
				{
					alert("Beyond the program authorization number!");
				}
				else
				{
					alert("超出节目授权个数！");
				}				
				$("#PidCheck" + ProgNo[progid] + "_" + pidid).attr("checked","");
				return;
			}
		}
	}
	var progno = ProgNo[progid];
	if(getPidCheckNum(progno, progid) == true)
	{
		enableDetail(progno, false);
	}
	else
	{
		enableDetail(progno, true);
	}
}

function enableDetail(index, type)
{
	for(var k = 1; k < 7; k++)
	{
		if(eval("casid" + k) != " ")
		{
			$("#ecmpid" + index + "_" +( k - 1)).attr("disabled", type);
			$("#accessdata" + index + "_" + (k - 1)).attr("disabled", type);
			$("#ecmdata" + index + "_" + (k - 1)).attr("disabled", type);
		}
	}
}

function submitport(type)
{
	var returnFlag;
	$("#curtype").val(type);
	if((type == PORT_GOTO) || (type == PORT_SEARCH))
	{
		if($("#curslot").val() == 7)
		{
			returnFlag = ValidateParam($("#curport"), "INT_DEC", 1, 512, "<Select Port>", "<选择端口>", language);
			if(returnFlag == false)
			{
				return;
			}
		}
		else
		{
			returnFlag = ValidateParam($("#curport"),"INT_DEC", 1, 8, "<Select Port>", "<选择端口>", language);
			if(returnFlag == false)
			{
				return;
			}
		}
	} 
	$("#myform").submit();
	setTimeout("self.location.reload(true)",1000);
}

function getPidCheckNum(programNo, index)
{
	var pidCheckNum = 0;
	for(var i = 0; i < PidNum[index]; i++)
	{
		if($("#PidCheck" + programNo + "_" + i).attr("checked"))
		{
			pidCheckNum++;
		}		
	}
	if(pidCheckNum > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function submitcheck()
{
	var returnFlag;
	/*为了防止在刚加载页面的时候就出现了超出授权范围的现象，在提交的时候应再做判断*/
	if(value_type == "1")//当前授权类型为节目授权
	{
		var validnum = value_prg - scrambled_prg_other//当前端口允许加扰的节目数
		var progcount = 0;//该变量为遍历得到的当前加扰的节目数
		for(var i=0; i<prognum; i++)
		{
			var progno = ProgNo[i];
			if(getPidCheckNum(progno, i) == true)
			{
				progcount++;
			}
			if((progcount > validnum)||(progcount > 1))
			{
				if(language == ENGLISH)
				{
					alert("Beyond the program authorization number!");
				}
				else
				{
					alert("超出节目授权个数！");
				}	
				return;
			}
		}
	}
	for(var i = 0; i < prognum; i++)
	{
		var progno = ProgNo[i];
		returnFlag = ValidateParam($("#scgid_" + progno), "INT_DEC_HEX", 0, 65535, "<SCG ID>", "<SCG ID>", language);
		if(returnFlag == false)
		{
			return;
		}		
		for(var j = i + 1; j < prognum; j++)
		{
			if(parseInt($("#scgid_" + progno).val()) == parseInt($("#scgid_" + ProgNo[j]).val()))
			{
				if(language == ENGLISH)
				{
					alert("<SCG ID> conflict!");
				}
				else
				{
					alert("<SCG ID>冲突！");
				}
				return;
			}
		}
		if(getPidCheckNum(progno, i) == true)
		{
			for(var k = 1; k < 7; k++)
			{
				if(eval("casid" + k) != " ")
				{
					returnFlag = ValidateParam($("#ecmpid" + progno + "_" + (k - 1)), "INT_DEC_HEX", 1, 8191, "<ECM PID>", "<ECM PID>", language);
					if(returnFlag == false)
					{
						return;
					}
					for(var j = 0; j < pidlist.length; j++)
					{			
						if(parseInt($("#ecmpid" + progno+ "_" + (k - 1)).val()) == pidlist[j])
						{
							if(language == ENGLISH)
							{
								alert("ECM_PID cann't be same with video or audio PID!");
							}
							else
							{
								alert("ECM_PID不能和音视频PID信息相同!");
							}							
							return;
						}	
					}
					for(var j = k + 1; j < 7; j++)
					{
						if(eval("casid" + j) != " ")
						{
							if(parseInt($("#ecmpid" + progno + "_" + (k - 1)).val()) == parseInt($("#ecmpid" + progno + "_" + (j - 1)).val()))
							{
								if(!((ac_mode == SHARING) && ($("#accessdata" + progno + "_" + (k - 1)).val() == $("#accessdata" + progno + "_" + (j - 1)).val())))
								{
									if(language == ENGLISH)
									{
										alert("<ECM_PID> conflict!");
									}
									else
									{
										alert("<ECM_PID>冲突！");
									}
									return;
								}													
							}
						}
					}
					for(var j = i + 1; j < prognum; j++)
					{
						if(getPidCheckNum(ProgNo[j], j) == true)
						{
							for(var z = 1; z < 7; z++)
							{
								if(eval("casid" + z) != " ")
								{
									if(parseInt($("#ecmpid" + progno + "_" + (k - 1)).val()) == parseInt($("#ecmpid" + ProgNo[j] + "_" + (z - 1)).val()))
									{
										if(!((ac_mode == SHARING) && ($("#accessdata" + progno + "_" + (k - 1)).val() == $("#accessdata" + ProgNo[j] + "_" + (z - 1)).val())))
										{
											if(language == ENGLISH)
											{
												alert("<ECM_PID> conflict!");
											}
											else
											{
												alert("<ECM_PID>冲突！");
											}
											return;
										}										
									}
								}
							}
						}
					}
				}  
			}
		}
		for(var k = 1; k < 7; k++)
		{
			if(eval("casid"+k) != " ")
			{
				returnFlag = ValidateParam($("#accessdata" + progno + "_" + (k - 1)), "INT_HEX_NULL", 0, 0, "<Access Criteria Data>", "<Access Criteria Data>", language);
				if(returnFlag == false)
				{
					return;
				}
				returnFlag = ValidateParam($("#accessdata" + progno + "_" + (k - 1)), "LENGTH", 0, 128, "<Access Criteria Data>", "<Access Criteria Data>", language);
				if(returnFlag == false)
				{
					return;
				}
				if($("#accessdata" + progno + "_" + (k - 1)).val().length % 2 != 0)
				{
					if(language == ENGLISH)
					{
						alert("The length of <Access Criteria Data> must be a even number!");
						return false; 
					}
					else
					{
						alert("<Access Criteria Data>只能输入偶数个字符!");
						return false; 
					}
				}
				returnFlag = ValidateParam($("#ecmdata"+progno+"_"+(k-1)), "INT_HEX_NULL", 0, 0, "<ECM Private Description>", "<ECM私用描述>", language);
				if(returnFlag == false)
				{
					return;
				}
				returnFlag = ValidateParam($("#ecmdata"+progno+"_"+(k-1)), "LENGTH", 0, 128, "<ECM Private Description>", "<ECM私用描述>", language);
				if(returnFlag == false)
				{
					return;
				}
				if($("#ecmdata"+progno+"_"+(k-1)).val().length % 2 != 0)
				{
					if(language == ENGLISH)
					{
						alert("The length of <ECM Private Description> must be a even number!");
						return false; 
					}
					else
					{
						alert("<ECM私用描述>只能输入偶数个字符!");
						return false; 
					}
				}
			}   
		}
	}
	$("#paramform").submit();
	setTimeout("self.location.reload(true)",1000);
}
