
	
	function detectEnter(e)
	{
		var keycode;
		if (window.event)
			keycode = window.event.keyCode;
		else if (e)
			keycode = e.which;
		else
			return true;

		if (keycode == 13)
		{
			return true;
		} else
		{
			return false;
		}
	}

	function doLogin(e)
	{
		if (e && !detectEnter(e))
		{
			return false;
		} 
		
		if (document.getElementById('wsPassword').value == "" || document.getElementById('wsUserName').value == "")
		{
			return false;
		}
		
		login.start();
	}

	var login = {
		start: function()
		{
			this.setLoadingState();
			if(window.XMLHttpRequest)
				xmlhttp = new XMLHttpRequest();
			else if(window.ActiveXObject)
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			else
				return false;
			
			xmlhttp.open("GET", document.location.pathname + "?ajaxWEBsysLogin=getHash", true);
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState == 4)
					login.logging(xmlhttp.responseText);
			}
			xmlhttp.send(null);
		},
		setLoadingState: function()
		{
			with(document.getElementById("loginLoader")) {
				style.display = 'block';
			}
			with(document.getElementById("loginButton")) {
				style.display = 'none';
			}
			with(document.getElementById("loginError")) {
				style.display = 'none';
			}
		},
		setNormalState: function ()
		{
			    with(document.getElementById("loginLoader")) {
				style.display = 'none';
			}
			with(document.getElementById("loginButton")) {
				style.display = 'block';
			}
			with(document.getElementById("loginError")) {
				style.display = 'none';
			}
		},
		setErrorState: function ()
		{
			with(document.getElementById("loginLoader")) {
				style.display = 'none';
			}
			with(document.getElementById("loginError")) {
				style.display = 'block';
			}
			with(document.getElementById("loginButton")) {
				style.display = 'none';
			}
		},
		logging: function(hash)
		{
			if(window.XMLHttpRequest)
				xmlhttp2 = new XMLHttpRequest();
			else if(window.ActiveXObject)
				xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
			else
				return false;
			xmlhttp2.open("POST", document.location.pathname + "?ajaxWEBsysLogin=auth", true);
			xmlhttp2.onreadystatechange=function()
			{
				if (xmlhttp2.readyState == 4)
					if (xmlhttp2.readyState)
					{
						if (xmlhttp2.responseText)
						{
							if (xmlhttp2.responseText == 'failed')
							{
								login.setErrorState();
								setTimeout('login.setNormalState();', 3000);
							}
							else if (xmlhttp2.responseText == 'duplicated')
							{
								location.reload();
							}
						}
						else
						{
							location.reload();
						}
					}
			}
			xmlhttp2.send(hex_md5(hash + hex_md5(document.getElementById('wsPassword').value))+document.getElementById('wsUserName').value);
		},
		reload: function()
		{
			if(window.XMLHttpRequest)
				xmlhttp = new XMLHttpRequest();
			else if(window.ActiveXObject)
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			else
				return false;
			xmlhttp.open("GET", document.location.pathname + "?ajaxWEBsysLogin=reload", true);
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState == 4)
					if (xmlhttp.readyState)
						login.reloadFinish();
			}
			xmlhttp.send(null);
		},
		reloadFinish: function()
		{
			// document.getElementsByTagName("body").item(0).removeChild(document.getElementById("overlay"));
			// document.getElementsByTagName("body").item(0).removeChild(document.getElementById("overlayBox"));
		},
		logout: function()
		{
			location.href = document.location.pathname + "?logout=1";
		}
	}
	
