// $Id: login.js 1973 2011-04-26 18:49:40Z joris $
// Niet gebouwd volgens de interface lib; Wordt gebruikt door iedere bezoekende vuurvos, moet zo lichtgewicht mogelijk;

(function()
{
	var loginWindow = null;
	var scriptElement = null;
	function L(id)
	{
		return rmdata.auth.login_lang[id] ? rmdata.auth.login_lang[id] : 'MISSING LANGUAGE IDENTIFIER '+id;
	}
	
	X.addDOMLoadEvent(function()
	{
		var loginbutton;
		scriptElement = document.getElementById(rmdata.auth.login_scriptid);
		if (!scriptElement)
			return;
		
		loginbutton = document.createElement('div');
		X.attr(loginbutton, 'id', 'riBackendLogin');
		X.attr(loginbutton, 'style', 'width: 32px; height: 32px; position: fixed; bottom: 0; right: 0; background-repeat: no-repeat; z-index: 500000');
		
		loginbutton.addEventListener('mouseover', function(e) 
		{
			if (!loginWindow)
			{
				this.style.backgroundImage = 'url(' + MIRACLE_URL + '/src/core/img/login_button.png)';
				loginbutton.style.cursor = 'pointer';
			} 
		}, false);
		
		loginbutton.addEventListener('mouseout', function(e) 
		{
			this.style.backgroundImage = null; 
			loginbutton.style.cursor = null;
		}, false);
		
		loginbutton.addEventListener('click', function(e) 
		{
			showLoginWindow();
		}, false);
		
		document.body.appendChild(loginbutton);
		
		if (rmdata.auth.autoOpenLoginWindow)
		{
			showLoginWindow();
		}
	
		
		try
		{
			window.addEventListener('keydown', function (e) {
				if (e.keyCode == 76 && e.altKey) // ALT - L;
				{
					e.preventDefault();
					showLoginWindow();
					
				}
			}, false);
		} catch (err){}
	});
	
	function showLoginWindow()
	{
		var elm, usernameInput, passwordInput, loginContainer, waitingAnimation, errorMessage;
		if (loginWindow) return;
		loginWindow = document.createElement('div');
		X.attr(loginWindow, 'style', 
			'position: fixed; left: 50%; top: 50%; width: 355px; z-index: 500000;'+ 
			'height: 315px; margin-left: -135px; margin-top: -195px; background-repeat: no-repeat'+
			'font-family: Tahoma,Arial,Helvetica,sans-serif; font-size: 11px; color: black;'
			);
			
		elm = document.createElement('div');
		X.attr(elm, 'style', 'position: absolute; top: 130px; left: 20px; width: 190px; font-size: 13px; font-weight: bold; color: red;');
		errorMessage = document.createTextNode("");
		elm.appendChild(errorMessage);
		loginWindow.appendChild(elm);
									
		loginWindow.style.backgroundImage = 'url(' + MIRACLE_URL + '/src/core/img/login_background.png)';
		loginContainer = document.createElement('div');
		loginWindow.appendChild(loginContainer);
		

		elm = document.createElement('label');
		X.attr(elm, 'for', 'loginWindow_username');
		elm.appendChild(document.createTextNode(L('login_login')));
		X.attr(elm, 'style', 'position: absolute; text-align: right; top: 180px; left: 0; width: 100px; font-weight: bold;');
		loginContainer.appendChild(elm);
		
		
		elm = usernameInput = document.createElement('input');
		X.attr(elm, 'type', 'text');
		X.attr(elm, 'id', 'loginWindow_username');
		X.attr(elm, 'style', 'position: absolute; top: 180px; left: 105px; width: 100px; font-size: 11px; background-color: #EEEEEE;');
		loginContainer.appendChild(elm);
		
		
		elm = document.createElement('label');
		X.attr(elm, 'for', 'loginWindow_password');
		elm.appendChild(document.createTextNode(L('login_password')));
		X.attr(elm, 'style', 'position: absolute; text-align: right; top: 210px; left: 0; width: 100px; font-weight: bold;');
		loginContainer.appendChild(elm);
		
		
		elm = passwordInput = document.createElement('input');
		X.attr(elm, 'type', 'password');
		X.attr(elm, 'id', 'loginWindow_password');
		X.attr(elm, 'style', 'position: absolute; top: 210px; left: 105px; width: 100px; font-size: 11px; background-color: #EEEEEE;');
		loginContainer.appendChild(elm);
		
		
		elm = document.createElement('div');
		X.attr(elm, 'style', 'position: absolute; top: 245px; left: 90px; width: 30px; height: 30px; cursor: pointer;');
		elm.style.backgroundImage = 'url(' + MIRACLE_URL + '/src/core/img/login_ok.png)';
		loginContainer.appendChild(elm);
		elm.addEventListener('click', function(e)
		{
			loginContainer.style.display = 'none';
			waitingAnimation.style.display = 'block';
			attemptLogin(usernameInput.value, passwordInput.value);
		}, false);
		
		loginWindow.addEventListener('keydown', function(e)
		{
			if (e.keyCode === 13) //enter
			{
				loginContainer.style.display = 'none';
				waitingAnimation.style.display = 'block';
				attemptLogin(usernameInput.value, passwordInput.value);
				e.preventDefault();
			}
			else if (e.keyCode === 27) //esc
			{
				document.body.removeChild(loginWindow);
				loginWindow = null;
				e.preventDefault();
			}
		}, false);
		
		elm = document.createElement('div');
		X.attr(elm, 'style', 'position: absolute; top: 245px; left: 140px; width: 30px; height: 30px; cursor: pointer;');
		elm.style.backgroundImage = 'url(' + MIRACLE_URL + '/src/core/img/login_cancel.png)';
		loginContainer.appendChild(elm);
		elm.addEventListener('click', function(e) 
		{
			document.body.removeChild(loginWindow);
			loginWindow = null;
		}, false);
		
		elm = waitingAnimation = document.createElement('div');
		X.attr(elm, 'style', 'position: absolute; top: 180px; left: 60px; width: 85px; height: 80px; background-repeat: no-repeat; display: none');
		elm.style.backgroundImage = 'url(' + MIRACLE_URL + '/src/core/img/wait.gif)';
		loginWindow.appendChild(elm);
		
		document.body.appendChild(loginWindow);
		usernameInput.focus();
		
		function attemptLogin(username, password)
		{
			var bridge
			
			if (scriptElement.getAttribute('src') != X.urldecode(rmdata.auth.login_scriptsrc))
			{
				alert("You may be using a web proxy which modifies the data of this page. This interferes with the correct working of this software. (Normal HTTP/SOCKS proxies are supported)");
				loginContainer.style.display = 'block';
				waitingAnimation.style.display = 'none';
				return;
			} 
			
			
			bridge = new PHPBridge();
			
			bridge.addData('username', username);
			bridge.addData('password', password);
			bridge.setDataByRequest(MIRACLE_URL_FULL + 'src/core/backend/login.be.php', bridge.getJSONData(), login_result);
		}
		
		function login_result(bridge)
		{
			var data, result, loc, parts;
			
			data = bridge.getData();
			result = data ? data.result : 'error';
			if (result == 'ok')
			{
				window.location = window.location;
			}
			else
			{
				loginContainer.style.display = 'block';
				waitingAnimation.style.display = 'none';
				
				if (result == 'badcredentials')
				{
					errorMessage.nodeValue = L('login_badcredentials');
				}
				else if (result == 'badlicense')
				{
					errorMessage.nodeValue = L('login_licensedisabled');
				}
				else if (result == 'error')
				{
					errorMessage.nodeValue = 'Error connecting to server';
				}
			}
		}
	}

})();
