var auth_code_set = new Array(0,0,0,0);
var control_name = new Array("authorized_code0", "authorized_code1", "authorized_code2", "authorized_code3");
var language;

/*fuction to get the authority information*/
function getAuthInfo()
{
	var xmlhttp = false;
	xmlhttp = getHTTPObject();

	var getParams = "type=34&cmd=1&getAuthInfo=112233";

	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 authInfo = xmlhttp.responseText.slice(7, -8).split("&");
			var count = 0;
			for(var i = 0; i < 4; i++)
			{
				document.getElementById("authorized_code" + i).value = authInfo[count++];
				document.getElementById("authority_limit" + i).innerHTML = authInfo[count++];
			}
			
			document.body.style.cursor = "auto";
			window.status = "Done";
		}
	}
	xmlhttp.send(getParams);
	return false;
}

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

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

function ResetSetArrays()
{
	for(var i = 0; i < 4; i++)
	{
		auth_code_set[i] = 0;
	}
}

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

function pageRefresh()
{
	PageLoadHandler();
}

window.onload = function()
{
	var apply = document.getElementById("Apply");
	language = document.getElementById("language").value;
	applyDispay(parent.group, apply);
	PageLoadHandler();
}

/*只能输入由数字和26个英文字母组成的32个字符的字符串*/
function checkAuthCode(comp)
{
	EnableApply();
	var AuthCode = comp.value;
	var patrn = /^[A-Za-z0-9]{32}$/;  
	if(!patrn.exec(AuthCode)) 
	{
		if(language == 0)
		{
			alert("Authorized code should consist of 32 chars(only integer or letter).");
		}
		else
		{
			alert("授权码应由32位数字或字母组成。");
		}
		setTimeout(function () {comp.focus()}, 50);
		comp.select();
		DisableApply();
		return false; 
	}
	for(var i = 0; i < 4; i++)
	{
		if(comp.id == control_name[i])
		{
			auth_code_set[i] = 1;
		}
	}
	return true;
}

function setAuthCodeInfo()
{
	var getParams = "type=34&cmd=2";
	var authorized_code;

	for(var i = 0; i < 4; i++)
	{
		if(auth_code_set[i] == 1)
		{
			authorized_code = document.getElementById(control_name[i]).value;
			getParams = getParams + "&" + control_name[i] + "=" + authorized_code;
		}
	}
	
	var xmlhttp = getHTTPObject();
	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 url;
			if(language == 0)
			{
				alert("Authority configuration updated successfully.");
				url = "../asp/main.asp?nodeId=" + parent.curTree.getSelectedNodes()[0].id;
			}
			else
			{
				alert("授权参数已设置成功。");
				url = "../asp/main_cn.asp?nodeId=" + parent.curTree.getSelectedNodes()[0].id;
			}
			window.parent.location.replace(url);
			//PageLoadHandler();
		}
	}
	xmlhttp.send(getParams);
	return true;
}

function setAuthCode()
{
	DisableApply();
	document.body.style.cursor = "wait";
	window.status = "Updating authority information, please wait...";
	setAuthCodeInfo();		
}
