//
// var.jcmsUser.js
//

var jcmsUser =
{
	toString: function()
	{
		return '[object jcmsUser]';
	},
	
	onLoginKeyUp: function(e)
	{
		if (e && e.keyCode == 27)
		{
			this.HideLoginDialog();
		}
		
	},
	
	onload: function()
	{
		var nodeLogin = jcmsNode('jcmsUserLoginDialog');
		// There shouldn't be one!
		if (jcmsIsObject(nodeLogin))
		{
			nodeLogin.parentNode.removeChild(nodeLogin);
		}
		
		// LOGIN-DIALOG
		nodeLogin = document.createElement('div');
		nodeLogin.id = 'jcmsUserLoginDialog';
		nodeLogin.style.position = 'absolute';
		nodeLogin.style.visibility = 'hidden';
		nodeLogin.title = this.GetString('loginDialog_Title');
		nodeLogin.onkeyup = jcmsAssignEvent(this, "onLoginKeyUp");
		
		// FORM
		var nodeForm = document.createElement('form');
		nodeForm.className = 'scForm';
		nodeLogin.appendChild(nodeForm);
		
		// CAPTION-BOX
		var nodeBox = document.createElement('div');
		nodeBox.className = 'scCaptionBox';
		nodeBox.innerHTML = this.GetString('loginDialog_Caption');
		nodeForm.appendChild(nodeBox);
		
		// ID-BOX
		nodeBox = document.createElement('div');
		nodeBox.className = 'scIdBox';
		nodeForm.appendChild(nodeBox);
		// Label
		var nodeLabel = document.createElement('label');
		nodeLabel.className = 'scLabel';
		nodeLabel.innerHTML = this.GetString('loginDialog_CaptionUserId');
		nodeBox.appendChild(nodeLabel);
		// TextBox
		var nodeControl = document.createElement('input');
		nodeControl.id = 'jcmsUserLoginDialog_UserId';
		nodeControl.type = 'text';
		nodeControl.className = 'scTextBox';
		nodeControl.maxLength = 16;
		nodeBox.appendChild(nodeControl);
		
		// PASSWORD-BOX
		nodeBox = document.createElement('div');
		nodeBox.className = 'scPasswordBox';
		nodeForm.appendChild(nodeBox);
		// Label
		nodeLabel = document.createElement('label');
		nodeLabel.className = 'scLabel';
		nodeLabel.innerHTML = this.GetString('loginDialog_CaptionPassword');
		nodeBox.appendChild(nodeLabel);
		// PasswordBox
		nodeControl = document.createElement('input');
		nodeControl.id = 'jcmsUserLoginDialog_Password';
		nodeControl.type = 'password';
		nodeControl.className = 'scTextBox';
		nodeControl.maxLength = 16;
		nodeBox.appendChild(nodeControl);
				
		// BUTTON-BOX
		var nodeBox = document.createElement('div');
		nodeBox.className = 'scButtonBox';
		nodeForm.appendChild(nodeBox);
		// Button Login
		nodeControl = document.createElement('input');
		nodeControl.type = 'submit';
		nodeControl.className = 'scButton';
		nodeControl.value = this.GetString('loginDialog_CaptionLogin');
		nodeControl.onclick = jcmsAssignEvent(this, "Login");
		nodeBox.appendChild(nodeControl);
		// Button Logout
		nodeControl = document.createElement('input');
		nodeControl.type = 'button';
		nodeControl.className = 'scButton';
		nodeControl.value = this.GetString('loginDialog_CaptionLogout');
		if (!this.IsLoggedIn())
		{
			nodeControl.disabled = 'disabled';
		}
		nodeControl.onclick = jcmsAssignEvent(this, "Logout");
		nodeBox.appendChild(nodeControl);
		// Button Cancel
		nodeControl = document.createElement('input');
		nodeControl.type = 'button';
		nodeControl.className = 'scButton';
		nodeControl.value = this.GetString('loginDialog_CaptionCancel');;
		nodeControl.onclick = jcmsAssignEvent(this, "HideLoginDialog");
		nodeBox.appendChild(nodeControl);
		
		// FINISH LOGIN-DIALOG
		document.body.insertBefore(nodeLogin, document.body.firstChild);
	},
	
	GetUserId: function()
	{
		var node = jcmsNode('jcmsUserLoginDialog_UserId');
		if (node)
		{
			return node.value;
		}
		return null;
	},
	
	GetPassword: function()
	{
		var node = jcmsNode('jcmsUserLoginDialog_Password');
		if (node)
		{
			return node.value;
		}
		return null;
	},
	
	GetLoginDialogNode: function()
	{
		return jcmsNode('jcmsUserLoginDialog');
	},
	
	ShowLoginDialog: function()
	{
		var node = this.GetLoginDialogNode();
		if (node)
		{
			try
			{
				node.style.zIndex = jcmsShade.GetZIndex() + 1;
				jcmsShade.Show();
				jcmsShade.SetOpacity(0.75);
			}
			catch(e) {}
			this.Position();
			node.style.visibility = 'visible';
			
			node = jcmsNode('jcmsUserLoginDialog_UserId');
			if (node)
			{
				node.focus();
				node.select();
			}
		}
		
	},

	HideLoginDialog: function()
	{
		var node = this.GetLoginDialogNode();
		if (node)
		{
			node.style.left = '0px';
			node.style.top = '0px';
			node.style.visibility = 'hidden';
			try
			{
				jcmsShade.Hide();
				node.style.zIndex = 0;
			}
			catch(e) {}
		}
	},
	
	IsLoggedIn: function()
	{
		if (jcmsHasText(document.cookie))
		{
			if(document.cookie.match(/jcmsUser_UserId/) && document.cookie.match(/jcmsUser_Password/))
			{
				return true;
			}
		}
		return false;
	},
	
	Logout: function()
	{
		document.cookie = 'jcmsUser_UserId=;;path=/'; 
		document.cookie = 'jcmsUser_Password=;;path=/'; 
		window.location.reload();
	},
	
	Login: function()
	{
		if (jcmsTrim(this.GetUserId()) == '')
		{
			node = jcmsNode('jcmsUserLoginDialog_UserId');
			if (node)
			{
				node.focus();
				node.select();
			}
			return false;
		}
		if (jcmsTrim(this.GetPassword()) == '')
		{
			node = jcmsNode('jcmsUserLoginDialog_Password');
			if (node)
			{
				node.focus();
				node.select();
			}
			return false;
		}
		document.cookie = 'jcmsUser_UserId=' + MD5(this.GetUserId()) + ';;path=/'; 
		document.cookie = 'jcmsUser_Password=' + MD5(this.GetPassword()) + ';;path=/';
		window.location.reload();
		return true;
	},
	
	Position: function()
	{
		var node = this.GetLoginDialogNode();
		if (node)
		{
			jcmsCenterOnScreen(node);
		}
	},
	
	Reposition: function()
	{
		var node = this.GetLoginDialogNode();
		if (node && node.style.visibility == 'visible')
		{
			this.Position();
		}
	},
	
	strings:
	{
		loginDialog_Caption: 'jcmsLogin',
		loginDialog_CaptionUserId: 'Anwender-ID:',
		loginDialog_CaptionPassword: 'Passwort',
		loginDialog_CaptionLogin: 'Einloggen',
		loginDialog_CaptionLogout: 'Ausloggen',
		loginDialog_CaptionCancel: 'Abbrechen'
	},
	
	GetString: function(pName)
	{
		if (!jcmsHasText(pName))
		{
			throw 'jcmsUser.GetString: invalid parameter: pName: ' + jcmsGetTypeSpecial(pName);
		}

		if (jcmsIsObject(this.strings) && jcmsIsString(this.strings[pName]))
		{
			return this.strings[pName];
		}
		
		return 'undefined';
	},
	
	SetStrings: function(pStrings)
	{
		if (jcmsIsObject(pStrings))
		{
			var a = ['Title', 'Caption', 'CaptionUserId', 'CaptionPassword', 'CaptionLogin', 'CaptionLogout', 'CaptionCancel'];
			
			for (i = 0; i < a.length; i++)
			{
				if (jcmsIsString(pStrings['loginDialog_' + a[i]]))
				{
					this.strings['loginDialog_' + a[i]] = pStrings['loginDialog_' + a[i]];
				}
			}
		}
	}
	
	

};

jcmsWindowEvents.Add('onload', function() { jcmsUser.onload(); });
jcmsWindowEvents.Add('onresize', function() { jcmsUser.Reposition(); });
