﻿var CSILogin = new __Login;

function __Login()
{
	this.ErrorId = "";
	this.UsernameId = "";
	this.PasswordId = "";
	this.LoginId = "";
	
	var LoginControl = null;
	var ErrorControl = null;
	var UsernameControl = null;
	var PasswordControl = null;
	
	this.Init = function()
	{
		if (LoginControl == null)
			LoginControl = document.getElementById(this.LoginId);
		if (ErrorControl == null)
			ErrorControl = document.getElementById(this.ErrorId);
		if (PasswordControl == null)
			PasswordControl = document.getElementById(this.PasswordId);
		if (UsernameControl == null)
			UsernameControl = document.getElementById(this.UsernameId);
	}
		
	this.CloseLogin = function()
	{
		if (LoginControl != null)
		{
			LoginControl.style.visibility = "hidden";
			LoginControl.style.height = "1px";
		}
	}
	
	this.ShowLogin = function(pCell)
	{
		this.Init();
	
		if (pCell.innerHTML.toLowerCase().indexOf("logout") > 0)
		{
			this.Logout();
		}
		else
		{
			if (LoginControl != null)
			{
				LoginControl.style.visibility = (LoginControl.style.visibility == "visible") ? "hidden" : "visible";
				
				if (LoginControl.style.visibility == "visible")
				{
					var iWidth = LoginControl.style.width.replace("px", "");
					var iLeft = (IE) ? document.body.clientWidth : innerWidth;
					
					LoginControl.style.height = "110px";
					LoginControl.style.left = (iLeft - iWidth) / 2 + "px";
					LoginControl.style.top = "140px";
							
					var objInput = LoginControl.getElementsByTagName("INPUT");
					if (objInput != null)
					{
						for (i = 0; i < objInput.length; i++)
						{
							if (objInput[i].name.indexOf("UserName") > 0)
							{
								objInput[i].focus();
							}
						}
					}
				}
				else
				{
					LoginControl.style.height = "1px";
				}
			}
		}
	}
	
	this.Logout = function()
	{
		__DoCallBack("?type=logout");
		window.location.reload();
	}
	
	this.Login = function()
	{
		if (UsernameControl != null && PasswordControl != null)
		{
			var UserName = UsernameControl.value;
			var PassWord = PasswordControl.value;
			var Params = "?type=login&field=" + UserName + "&value=" + PassWord;
			var Result = __DoCallBack(Params, false);
			
			if (Result != "Success")
			{
				if (ErrorControl != null)
				{
					ErrorControl.innerText = Result;
				}
			}
			else
			{
				window.location.reload();
			}
		}
		else
		{
			window.status = "Failure loading the controls";
		}
	}
	
	this.KeyPress = function(pText)
	{
		var Key = (window.event) ? event.keyCode : e.keyCode;
		if (Key == 13)
		{
			if (pText.id.toLowerCase().indexOf("username") > 0)
			{
				PasswordControl.select();
				PasswordControl.focus();
			}
			else
			{
				this.Login();
			}
			return false;
		}
		else
			return true;
	}
}
