var language;
var auth_code_set = 0;

function mouseOverShow(obj)
{
	if(obj.disabled == false)
	{
		obj.style.background = "url(../images/btn4_hover.png)";
	}
}
function mouseOutShow(obj)
{
	if(obj.disabled == false)
	{
		obj.style.background = "url(../images/btn4_normal.png)";
	}
}

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

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

function GetAuthInfo()
{
	var count = 0;
	var authInfo;
	var xmlhttp = false;
	var getParams = "type=40&cmd=1&getAuthInfo=112233";

	xmlhttp = getHTTPObject();
	xmlhttp.open("POST", "/goform/formIPQAM", true);
	if((bd.browser != "IE") && (language == 1))
	{
		xmlhttp.overrideMimeType("text/html;charset=gb2312");
	}
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", getParams.length);
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			if((bd.browser == "IE") && (language == 1))
			{
				authInfo = gb2utf8(xmlhttp.responseBody);
				var index = authInfo.indexOf("</html>");
				var bias = authInfo.length - index;
				authInfo = authInfo.slice(7, - bias).split("&");
			}
			else
			{
				authInfo = xmlhttp.responseText.slice(7, -8).split("&");
			}

			document.getElementById("sc_auth_code").value = authInfo[count++];
			document.getElementById("sc_auth_limit").innerHTML = authInfo[count++];

			document.body.style.cursor = "auto";
			window.status = "Done";
		}
	}
	xmlhttp.send(getParams);
	return false;
}

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

window.onload = function()
{
	language = document.getElementById("language").value;

	var args = getArgs();
	var group = args["group"];
	var apply = document.getElementById("authApply");
	applyDispay(group, apply);
	
	PageLoadHandler();
}

/*只能输入由数字和26个英文字母组成的32个字符的字符串*/
function checkAuthCode(comp)
{	
	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();
		return false; 
	}
	
	EnableApply();
	auth_code_set = 1;
	return true;
}

function SetAuthCode()
{
	DisableApply();

	var xmlhttp = getHTTPObject();
	var getParams = "type=40&cmd=2";
	if(auth_code_set == 1)
	{
		var sc_auth_code = document.getElementById("sc_auth_code").value;
		getParams = getParams + "&sc_auth_code=" + sc_auth_code;
	}
	
	document.body.style.cursor = "wait";
	window.status = "Updating card information, please wait...";

	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)
		{
			if(language == 0)
			{
				alert("SC authorization code updated successfully.");
			}
			else
			{
				alert("同密授权参数已设置成功。");
			}
			PageLoadHandler();
		}
	}
	xmlhttp.send(getParams);
	return true;
}

