var NUM_DEV_CARD = 4;
var language;

function CardConfigDisplay()
{
	var xmlhttp = getHTTPObject();
	var getParams = "type=27&cmd=1&getCardConfig=1";
	
	xmlhttp.open("POST", "/goform/formIPQAM", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", getParams.length);

	xmlhttp.onreadystatechange = function()
    {
		if(xmlhttp.readyState == 4)
		{
			var response = xmlhttp.responseText.slice(7, -8);
			var cardData = response.split("&");
			var i,id;
			
			/* Main Board - IP Address */
			for(i = 0; i < NUM_DEV_CARD; i++)
			{
				id = i + 1;
				document.getElementById("card_reset_" + id).value = cardData[i];
			}
			
			document.body.style.cursor="auto";
			window.status = "Done";
		}/* End of readyState */
	}/* End of onreadystatechange */
	xmlhttp.send(getParams);
	return true;
}

function DisableApply()
{
	disabledButton(document.getElementById("Apply"), true);
}

function EnableApply()
{
	disabledButton(document.getElementById("Apply"), false);
}

function PageLoadHandler()
{
	DisableApply();
	document.body.style.cursor = "wait";
	window.status = "Retrieving data, please wait...";
	CardConfigDisplay();
}

function pageRefresh()
{
	PageLoadHandler();
}

/****************************************************************
	Populates the Channel table on page load
****************************************************************/
window.onload = function()
{
	var args = getArgs();
	userName = args["userName"];
	language = document.getElementById("language").value;
	PageLoadHandler();
}


function CardResetConfig()
{
	var i,id;
	/* Generate params to POST from the from controls*/
	var params = "type=27&cmd=2&";
	
	for(var i = 0; i < NUM_DEV_CARD; i++)
	{
		id = i + 1;
		var card_reset = document.getElementById("card_reset_" + id).value;
		params = params + "card_reset_" + id + "=" + card_reset + "&";
	}


	var xmlhttp = getHTTPObject();
	xmlhttp.open("POST", "/goform/formIPQAM", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			if(language == 0)
			{
				alert("Card update configuration updated successfully.");
			}
			else
			{
				alert("卡复位参数已设置成功。");
			}
			PageLoadHandler();
		}
	}
	xmlhttp.send(params)
	return;
}

function SetCardReset()
{
	DisableApply();
	document.body.style.cursor = "wait";
	window.status = "Applying settings, please wait...";
	
	CardResetConfig();	
}

function CardReboot(cardNo)
{
	var xmlhttp = getHTTPObject();
	var params = "type=27&cmd=2&rebootCardNo=" + cardNo + "&userName=" + userName;
	
	var confirmResult;
	if(language == 0)
	{
		confirmResult = confirm("Card " + cardNo + "will reboot, All unsaved configuration and connection to the Card will be lost.\r\nContinue?");
	}
	else
	{
		confirmResult = confirm("卡" + cardNo + "将重启，所有未保存的设置或连接将丢失。\r\n确定继续？");
	}
	if(confirmResult == true)
	{
		xmlhttp.open("POST", "/goform/formIPQAM", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState == 4)
			{
				if(xmlhttp.responseText.match("CardRebootAllowed"))
				{
					if(language == 0)
					{
						alert("Card reboot succseefully, please wait.");
					}
					else
					{
						alert("卡成功重启，请耐心等待。");
					}
				}
				else
				{
					if(language == 0)
					{
						alert("Card reboot operation is not allowed. Some other critical operation is in progress.");
					}
					else
					{
						alert("卡重启失败，其它重要的操作正在进行中。");
					}
				}
			}
		}
		xmlhttp.send(params);
	}
	else 
	{
		return false;
	}
	
}