function $(id) {
	return document.getElementById(id);
}
function $C(type) {
	return document.createElement(type);
}
function setCookie(key, value, expireDay) {
	var date = new Date();
	date.setTime(date.getTime() + (expireDay ? expireDay : 365) * 24 * 3600 * 1000);
	document.cookie = key + "=" + escape(value) + "; expires=" + date.toGMTString();
}

function getCookie(key) {
	var result = document.cookie.match(new RegExp(key + "=([^;]*)"));
	return result != null ? unescape(result[1]) : null;
}

var LoaderScript = function () {
	this.elementContainer = null;
	this.init = function (stringContainerId) {
		if (stringContainerId) {
			this.elementContainer = document.getElementById(stringContainerId);
		}
		else {
			this.elementContainer = document.createElement("div");
			this.elementContainer.style.display = "none";
			document.body.appendChild(this.elementContainer);
		}
	};
	this.load = function (src, callback, valueCallback) {
		var elementLoader = document.createElement("script");
		elementLoader.type = "text/javascript";
		elementLoader.language = "javascript";
		elementLoader.charset = "gb2312";
		elementLoader.src = src;
		elementLoader.valueCallback = valueCallback;
		elementLoader[document.all ? "onreadystatechange" : "onload"] = this.unload;
		elementLoader.callback = callback;
		this.elementContainer.appendChild(elementLoader);
	};
	this.unload = function () {
		if (document.all) {
			if (this.readyState != "loaded" && this.readyState != "complete") {
				return;
			}
		}
		if (this.callback) {
			this.callback(this.valueCallback);
		}
		this.callback = null;
		this[document.all ? "onreadystatechange" : "onload"] = null;
		this.parentNode.removeChild(this);
	};
};


var LoaderAjax = function () {
	this.createXMLHttpObject = function () {
		try {
			return new XMLHttpRequest();
		}
		catch (e) {
		}
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {
		}
		try {
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e) {
		}
		return null;
	};
	this.load = function (url, callback, content) {
		var xmlhttp = this.createXMLHttpObject();
		if (xmlhttp != null) {
			xmlhttp.open("post", url, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-Type","text/xml");
			xmlhttp.setRequestHeader("Content-Type","gb2312");
			xmlhttp.onreadystatechange = function () {
				if (xmlhttp.readyState == 4) {
					if (xmlhttp.status == 200) {
						var text = xmlhttp.responseText;
						xmlhttp.abort();
						xmlhttp = null;
						callback(text);
					}
				}
			};
			content = content == undefined ? null : content;
			xmlhttp.send(content);
		}
	};
};




var Model = function (stringType, stringContainerId) {
	this.getRandomString = function () {
		var date = new Date();
		return date.getTime();
	};
	this.stringType = stringType.toLowerCase();
	this.responseText = null;
	this.booleanReturnForced = false;
	switch (this.stringType) {
		case "ajax":
			this.loader = new LoaderAjax();
			break;
		case "script":
			this.loader = new LoaderScript();
			this.loader.init(stringContainerId);
			break;
	}
	this.stringUrl = "";
	this.stringMark = "@RANDOM@";
	this.callback = function (responseText) {
		var model = arguments.callee.model;
		switch (model.stringType) {
			case "ajax":
				model.responseText = responseText;
				model.processData.model = model;
				model.processData(responseText);
				model.updateView();
				break;
			case "script":
				model.processData(responseText);
				model.updateView(responseText);
				break;
		}
	};
	this.callback.model = this;
	this.processData = function () {};
	this.updateView = function () {};
	this.stringContent = null;
	this.intThread = -1;
	this.intInterval = 5000;
	this.start = function () {
		this.stop();
		//~ this.load();
		this.intThread = setInterval(this.load, this.intInterval);
	};
	this.stop = function () {
		if (this.intThread != -1) {
			clearInterval(this.intThread);
		}
		this.intThread = -1;
	};
	this.load = function () {
		var model = arguments.callee.model;
		switch (model.stringType) {
			case "ajax":
				model.loader.load(model.stringUrl.replace(model.stringMark, model.getRandomString()), model.callback, model.stringContent);
				break;
			case "script":
				model.loader.load(model.stringUrl.replace(model.stringMark, model.getRandomString()), model.callback, model.stringUrl);
				break;
		}
	};
	this.load.model = this;
};
var Login = function () {
	var stringCardNo = $("inputCardNo").value != "" ? $("inputCardNo").value : getCookie("CardNo");
	if (stringCardNo) {
		$("inputCardNo").value = stringCardNo;
		$("inputPassword").focus();
	}
	else {
		$("inputCardNo").focus();
	}
	var loginModel = new Model("ajax");
	loginModel.object = this;
	this.loginModel = loginModel;
	loginModel.stringUrl = "";
	loginModel.stringOriginalUrl = "login_valid.php?rn=@RANDOM@&cardNo=@CARDNO@&password=@PASSWORD@&auth_number=@AUTHNUMBER@";
	loginModel.processData = function (responseText) {
		$("buttonSubmit").value = "µÇÂ¼";
		$("buttonSubmit").disabled = false;
		try {
			eval("this['tempData'] = " + responseText + ";");
		}
		catch (e) {
			alert("´«Êä´íÎó");
			return;
		}
		if (this["tempData"]) {
			var data = this["tempData"];
			delete this["tempData"];
			switch (data["retval"]) {
				case "0":
					window.location.href = data["errmsg"];
					break;
				case "1":
					updateAuthNumber();
					alert(data["errmsg"]);
					break;
				case "2":
					updateAuthNumber();
					alert(data["errmsg"]);
					break;
			}
		}
	};
	window.funcLogin = function (code) {
		var object = arguments.callee.object;
		if ($("inputCardNo").value == "") {
			$("inputCardNo").focus();
			alert("ÇëÌîÐ´¿¨ºÅ");
			return;
		}
		if ($("inputPassword").value == "") {
			$("inputPassword").focus();
			alert("ÇëÌîÐ´ÃÜÂë");
			return;
		}
		if ($("inputAuth_number").value == "") {
			$("inputAuth_number").focus();
			alert("ÇëÌîÐ´ÑéÖ¤Âë");
			return;
		}
		if ($("remChk").checked) {
			setCookie("CardNo", $("inputCardNo").value);
		}
		$("buttonSubmit").value = "µÇÂ¼ÖÐ";
		$("buttonSubmit").disabled = true;
		object.loginModel.stringUrl = object.loginModel.stringOriginalUrl.replace("@CARDNO@", $("inputCardNo").value).replace("@PASSWORD@", $("inputPassword").value).replace("@AUTHNUMBER@", $("inputAuth_number").value);
		object.loginModel.load();
		return;
	};
	window.funcLogin.object = this;
};
window.updateAuthNumber = function () {
	$("auth_img").src="auth_img.php?sid=" + window.sid + "&t=" + (new Date()).getTime();;
	return false;
};
function main() {
	var login = new Login();
}

window.onload = main;