
	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;
			var params = {};
			window.location.search.replace(/([^?=&]+)(=([^&]*))?/g, function(a, b, c, d) { params[b] = d; });
			
			var nextUrl = '';
			if (params['dirID'] && params['ID'])
			{
				nextUrl = '&nextUrl=' + encodeURIComponent(window.location);
			}
			
			xmlhttp2.open("POST", document.location.pathname + "?ajaxWEBsysLogin=auth" + nextUrl, true);
			xmlhttp2.onreadystatechange=function()
			{
				if (xmlhttp2.readyState == 4) 
				{
					if (xmlhttp2.readyState)
					{
						if (xmlhttp2.responseText == 'failed')
						{
							login.setErrorState();
							setTimeout('login.setNormalState();', 3000);
						}
						else
						{
							location.href = xmlhttp2.responseText;
						}
					}
				}
			};
			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";
		}
	}
	
