var objIdArray = ["index", "audio1WorkMode", "audio1EncodingType", "audio1Bitrate", "audio2WorkMode", "audio2EncodingType", "audio2Bitrate"];
var COL_NUM = 7;
var rowNum;

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

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

function funcUpdateDisplay() 
{	 
	DisableButton("applyBtn");
	DisableButton("refreshBtn");
	DisableButton("modifyBtn");	
	PageLoadStatus(true);
	
	displayParam();
	
	ResetColor();
	EnableButton("refreshBtn");
	PageLoadStatus(false);
	$("input[type=checkbox]").prop("checked", false);
}

function checkAll()
{
	var flag = $("#checkAll").prop("checked");
	for(var i = 0; i < rowNum; i++)
	{
		$("#check" + (i + 1)).prop("checked", flag);
	}
	
	if(parent.groupAccess != USER)
	{
		EnableButton("modifyBtn");
	}
}

function changeRowChecked()
{
	if(parent.groupAccess != USER)
	{
		EnableButton("modifyBtn");
	}
		
}

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

	xmlHttp.open("GET", "/goform/formEMR30?type=" + CARD_AUDIO + "&cmd=" + GET + "&language=" + language 
		+ "&slotNo=" + slotNo + "&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;
	var index;
	
	for(var i = 0; i < rowNum; i++)
	{
		index = i + 1;
		rowData = data[i].split(SECOND_DELIMITER);
		for(var j = 0; j < COL_NUM; j++)
		{
			if(j == 0)
			{
				$("#" + objIdArray[j] + index).html(rowData[j]);
			}
			else
			{
				$("#" + objIdArray[j] + index).val(rowData[j]);
			}
		}
	}
}

function pageApply()
{
	var data;
	var response;
	var xmlHttp = GetHTTPObject();
	
	data = "type=" + CARD_AUDIO + "&cmd=" + SET + "&language=" + language + "&slotNo=" + slotNo + "&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;
	var index;
	for(var i = 0; i < rowNum; i++)
	{
		index = i + 1;
		for(var j = 0; j < COL_NUM; j++)
		{
			if((i == 0) && (j == 0))
			{
				data = $("#" + objIdArray[j] + index).html();
			}
			else if((i != 0) && (j == 0))
			{
				data += FIRST_DELIMITER + $("#" + objIdArray[j] + index).html();
			}
			else
			{
				data += SECOND_DELIMITER + $("#" + objIdArray[j] + index).val();
			}
		}		
	}
	return data;
}


function modifyBulk()
{
	$("#modifyAudio1WorkMode").val("2");
	$("#modifyAudio1WorkModeCheck").prop("checked", false);
	$("#modifyAudio1EncodingType").val("4");
	$("#modifyAudio1EncodingTypeCheck").prop("checked", false);
	$("#modifyAudio1Bitrate").val("7");
	$("#modifyAudio1BitrateCheck").prop("checked", false);
	$("#modifyAudio2WorkMode").val("2");
	$("#modifyAudio2WorkModeCheck").prop("checked", false);
	$("#modifyAudio2EncodingType").val("4");
	$("#modifyAudio2EncodingTypeCheck").prop("checked", false);
	$("#modifyAudio2Bitrate").val("7");
	$("#modifyAudio2BitrateCheck").prop("checked", false);
	$("#modifyDiv").modal();
}

function modifyApply()
{
	var modifyAudio1WorkMode = $("#modifyAudio1WorkMode").val();
	var modifyAudio1WorkModeCheck = $("#modifyAudio1WorkModeCheck").prop("checked");
	var modifyAudio1EncodingType = $("#modifyAudio1EncodingType").val();
	var modifyAudio1EncodingTypeCheck = $("#modifyAudio1EncodingTypeCheck").prop("checked");
	var modifyAudio1Bitrate = $("#modifyAudio1Bitrate").val();
	var modifyAudio1BitrateCheck = $("#modifyAudio1BitrateCheck").prop("checked");
	var modifyAudio2WorkMode = $("#modifyAudio2WorkMode").val();
	var modifyAudio2WorkModeCheck = $("#modifyAudio2WorkModeCheck").prop("checked");
	var modifyAudio2EncodingType = $("#modifyAudio2EncodingType").val();
	var modifyAudio2EncodingTypeCheck = $("#modifyAudio2EncodingTypeCheck").prop("checked");
	var modifyAudio2Bitrate = $("#modifyAudio2Bitrate").val();
	var modifyAudio2BitrateCheck = $("#modifyAudio2BitrateCheck").prop("checked");
	
	for(var i = 0; i < rowNum; i++)
	{
		if($("#check" + (i + 1)).prop("checked"))
		{
			if(modifyAudio1WorkModeCheck)
			{
				$("#audio1WorkMode" + (i + 1)).val(modifyAudio1WorkMode);
			}
			if(modifyAudio1EncodingTypeCheck)
			{
				$("#audio1EncodingType" + (i + 1)).val(modifyAudio1EncodingType);
			}
			if(modifyAudio1BitrateCheck)
			{
				$("#audio1Bitrate" + (i + 1)).val(modifyAudio1Bitrate);
			}
			if(modifyAudio2WorkModeCheck)
			{
				$("#audio2WorkMode" + (i + 1)).val(modifyAudio2WorkMode);
			}
			if(modifyAudio2EncodingTypeCheck)
			{
				$("#audio2EncodingType" + (i + 1)).val(modifyAudio2EncodingType);
			}
			if(modifyAudio2BitrateCheck)
			{
				$("#audio2Bitrate" + (i + 1)).val(modifyAudio2Bitrate);
			}
		}
	}
	$.modal.close();
	EnableButton("applyBtn");
}

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