var objIdArray = ["serviceId", "serviceName", "serviceType", "outputMode", "prioritty", "minBitrate", "maxBitrate", "conBitrate", "dtsInterval"];
var COL_NUM = 9;

var SERVICE_ID = 0;
var SERVICE_NAME = 1;
var SERVICE_TYPE = 2;
var OUTPUT_MODE = 3;
var MIN_RATE = 5;
var MAX_RATE = 6 ;
var CON_RATE = 7;

var HD = 0;
var SD = 1;

var HD_MIN_RATE = 12000000;
var HD_MAX_RATE = 20000000;
var HD_CBR = 14000000;
var SD_MIN_RATE = 2000000;
var SD_MAX_RATE = 5000000;
var SD_CBR = 8000000;

var STAT_MULTI = 0;
var CBR = 1;
var BYPASS = 2;

var rowNum;

window.onresize = function()
{
	GetMainContentHeight(BUTTON_TOP_MENU, NO_BUTTON_CHANGE);
}

$(document).ready(function()
{
	var args = GetArgs();
	slotNo = parseInt(args["slotNo"], 10);
	portNo = parseInt(args["portNo"], 10);
	GetMainContentHeight(BUTTON_TOP_MENU, NO_BUTTON_CHANGE);
	funcUpdateDisplay();
});

function funcUpdateDisplay() 
{	 
	DisableButton("applyBtn");
	DisableButton("refreshBtn");
	PageLoadStatus(true);
	
	displayParam();
	
	ResetColor();
	EnableButton("refreshBtn");
	PageLoadStatus(false);
}

function displayParam() 
{
	var data;
	var i;
	var response;
	var xmlHttp = GetHTTPObject();

	xmlHttp.open("GET", "/goform/formEMR30?type=" + PORT_SERVICE + "&cmd=" + GET + 
		"&language=" + language + "&slotNo=" + slotNo + "&portNo=" + portNo + "&ran=" + Math.random(), true);
	xmlHttp.onreadystatechange = function()
	{
		if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
		{
			response = AjaxStrDeal(xmlHttp.responseText);
			if(response == "")
			{
				rowNum = 0;
			}
			else
			{
				data = response.split(FIRST_DELIMITER);
				rowNum = data.length;
			}
			DrawTableRow("dataTable", "dataRow", "CaseRow", rowNum);
			if(rowNum != 0)
			{
				updateTableData(data);
			}			
			refreshFlag = true;
		}
	}
	xmlHttp.send(null);
}

function updateTableData(data)
{
	var rowData;
	
	for(var i = 0; i < rowNum; i++)
	{
		var index = i + 1;
		rowData = data[i].split(SECOND_DELIMITER);
		for(var j = SERVICE_ID; j < COL_NUM; j++)
		{
			if((j == SERVICE_ID) || (j == SERVICE_NAME))
			{
				$("#" +  objIdArray[j] + index).html(rowData[j]);
			}
			else
			{
				$("#" + objIdArray[j] + index).val(rowData[j]);
			}
		}
		changeBitrateStatus($("#" + objIdArray[OUTPUT_MODE] + index));
	}
}

function pageApply()
{
	var data;
	var response;
	var xmlHttp = GetHTTPObject();
	
	if(paramCheck() == false)
	{
		return;
	}
	data = "type=" + PORT_SERVICE + "&cmd=" + SET + "&language=" + language + "&slotNo=" + slotNo + "&portNo=" + portNo + "&setString=";
	data += generateData();
	
	xmlHttp.open("POST", "/goform/formEMR30", true)
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", data.length);

	xmlHttp.onreadystatechange = function()
	{
		if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
		{
			response = parseInt(AjaxStrDeal(xmlHttp.responseText), 10);
			if(response == RETURN_OK)
			{
				applyFlag = true;
			}
			else
			{
				alert(returnError[language][response]);
			}
			GeneratePrompt(applyFlag, TOP, APPLY, language);  //设置后页面弹出提示
			applyFlag = false;  //设置标识符恢复为false
			funcUpdateDisplay();
		}
	}
	xmlHttp.send(data);
}

function generateData()
{
	var data;
	
	for(var i = 0; i < rowNum; i++)
	{
		for(var j = 0; j < COL_NUM; j++)
		{
			if((i == 0) && (j == 0))
			{
				data = $("#" + objIdArray[j] + (i + 1)).html();
			}
			else if((i != 0) && (j == 0))
			{
				data += FIRST_DELIMITER + $("#" + objIdArray[j] + (i + 1)).html();
			}
			else if(j != SERVICE_NAME)
			{
				data += SECOND_DELIMITER + $("#" + objIdArray[j] + (i + 1)).val();
			}
		}
	}	
	return data;
}

function defaultBitrate(obj)
{
	var rowIndex = GetIndexFromId(obj, 7);	
	if(parseInt(obj.val(), 10) == HD)
	{
		$("#minBitrate" + rowIndex).val(HD_MIN_RATE);
		$("#maxBitrate" + rowIndex).val(HD_MAX_RATE);
		$("#conBitrate" + rowIndex).val(HD_CBR);
	}
	else
	{
		$("#minBitrate" + rowIndex).val(SD_MIN_RATE);
		$("#maxBitrate" + rowIndex).val(SD_MAX_RATE);
		$("#conBitrate" + rowIndex).val(SD_CBR);
	}
}

function changeBitrateStatus(obj)
{
	var rowIndex = GetIndexFromId(obj, 7);
	var outputMode = parseInt(obj.val(), 10);
	if(outputMode == STAT_MULTI)
	{
		$("#prioritty" + rowIndex).attr("disabled", false);
		$("#minBitrate" + rowIndex).attr("disabled", false);
		$("#maxBitrate" + rowIndex).attr("disabled", false);
		$("#conBitrate" + rowIndex).attr("disabled", true);		
		$("#dtsInterval" + rowIndex).attr("disabled", false);
	}
	else if(outputMode == CBR)
	{
		$("#prioritty" + rowIndex).attr("disabled", true);
		$("#minBitrate" + rowIndex).attr("disabled", true);
		$("#maxBitrate" + rowIndex).attr("disabled", true);
		$("#conBitrate" + rowIndex).attr("disabled", false);		
		$("#dtsInterval" + rowIndex).attr("disabled", false);
	}
	else if(outputMode == BYPASS)
	{
		$("#prioritty" + rowIndex).attr("disabled", true);
		$("#minBitrate" + rowIndex).attr("disabled", true);
		$("#maxBitrate" + rowIndex).attr("disabled", true);
		$("#conBitrate" + rowIndex).attr("disabled", true);		
		$("#dtsInterval" + rowIndex).attr("disabled", true);
	}
}

function paramCheck()
{
 	var returnFlag;
	for(var i = 0; i < rowNum; i++)
	{
		var index = i + 1;	
		if($("#" + objIdArray[SERVICE_TYPE] + index).val() == HD)
		{
			returnFlag = ValidateIP($("#" + objIdArray[MIN_RATE] + index) , "INT_DEC", 10000000, 18000000, "<Minimum Bitrate(bps)>", "<最小码率(bps)>", language);
			if(returnFlag == false)
			{
				return false;
			}
			returnFlag = ValidateIP($("#" + objIdArray[MAX_RATE] + index) , "INT_DEC", 12000000, 32000000, "<Maximum Bitrate(bps)>", "<最大码率(bps)>", language);
			if(returnFlag == false)
			{
				return false;
			}
			returnFlag = ValidateIP($("#" + objIdArray[CON_RATE] + index) , "INT_DEC", 10000000, 32000000, "<固定码率(bps)>", "<CBR(bps)>", language);
			if(returnFlag == false)
			{
				return false;
			}
		}
		else if($("#" + objIdArray[SERVICE_TYPE] + index).val() == SD)
		{
			returnFlag = ValidateIP($("#" + objIdArray[MIN_RATE] + index) , "INT_DEC", 2000000, 8000000, "<Minimum Bitrate(bps)>", "<最小码率(bps)>", language);
			if(returnFlag == false)
			{
				return false;
			}
			returnFlag = ValidateIP($("#" + objIdArray[MAX_RATE] + index) , "INT_DEC", 2000000, 8000000, "<Maximum Bitrate(bps)>", "<最大码率(bps)>", language);
			if(returnFlag == false)
			{
				return false;
			}
			returnFlag = ValidateIP($("#" + objIdArray[CON_RATE] + index) , "INT_DEC", 2000000, 8000000, "<固定码率(bps)>", "<CBR(bps)>", language);
			if(returnFlag == false)
			{
				return false;
			}
		}
		if(parseInt($("#" + objIdArray[MIN_RATE] + index).val(), 10) > parseInt($("#" + objIdArray[MAX_RATE] + index).val(), 10))
		{
			if(language == ENGLISH)
			{
				alert("Minimum bitrate cannot be larger than maximum bitrate!");
			}
			else
			{
				alert("最小码率不得大于最大码率！");
			}
			return false;
		}
	}		
	return true;
}
