function FocusUserNameTextBox()
{
    var userNameCell = document.getElementById("UserNameCell");
    var userNameTextBox = GetElement(userNameCell, "UserNameTextBox", "INPUT");

    var passwordCell = document.getElementById("PasswordCell");
    var passwordTextBox = GetElement(passwordCell, "PasswordTextBox", "INPUT");

    try
    {
        if (userNameTextBox.value == "")
        {
            userNameTextBox.focus();
        }
        else
        {
            passwordTextBox.focus();
        }
    }
    catch(e){}
}

function SimulateEnterToClick(triggerEvent, objectName)
{
	if (triggerEvent.keyCode == 13) //was the key an enter?
	{
		document.getElementById(objectName).click();
	}
	return true;
}

/// <summary>
/// Finds the element that has specified part of a string name of a certain type
/// e.g. "Arrow"
/// </summary>
function GetElement(element, partialName, type)
{
	if (element.tagName == type) if (element.id.indexOf(partialName) != -1) return element;
	if (element.childNodes.length < 1) return null;
	var elementArray = element.childNodes;
	var nodeCount = element.childNodes.length;
	
	for (var i = 0; i < nodeCount; i++)
	{
		childElement = GetElement(elementArray[i], partialName, type);
		if (childElement != null) return childElement;
	}
	return null;
}

///<Sumary>
///This function Displays that the user has given invalid details
///</Sumary>
function InvalidUserDetails()
{
    dispayForm();
	var errors = new ErrorCollection();
	errors.AddError(Messages.INVALID_USER_DETAILS);
	errors.DisplayAll(null, null, false, false);
}

function UnknownLoginError()
{
    dispayForm();
	var errors = new ErrorCollection();
	errors.AddError("Login failed, contact support.");
	errors.DisplayAll(null, null, false, false);
}

function LogonExpired()
{
    dispayForm();
	var errors = new ErrorCollection();
	errors.AddError(Messages.TRIAL_USER_LOGON_EXPIRED);
	errors.DisplayAll(null, null, false, false);
}

function AccountDisabled()
{
    dispayForm();
	var errors = new ErrorCollection();
	errors.AddError(Messages.USER_ACCOUNT_DISABLED);
	errors.DisplayAll(null, null, false, false);
}

function DisplayCustomError(error)
{
    dispayForm();
	var errors = new ErrorCollection();
	errors.AddInformation(error);
	errors.DisplayAll(null, null, false, false);
}

function DisplayLoggedInOtherWorkstationMessage()
{
    var errors = new ErrorCollection();
    errors.AddError(Messages.LOGGED_IN_OTHER_WORKSTATION);
    errors.DisplayAll(null, null, false, true);
}

function RedirectUserLogin()
{
    var errors = new ErrorCollection();
	errors.AddError("You are currently logged in at another work station, do you want to log in here?");
	errors.DisplayAllWithClose(LogOtherUsersOff, null, LogUserOffCurrentWorkStation, null, true, true);
}

function LogOtherUsersOff()
{
    var idDiv = document.getElementById('HiddenControlsDiv');    
	var button = GetElement(idDiv, "LogOtherUsersOutButton", "INPUT");
	button.click();
}

function LogUserOffCurrentWorkStation()
{
	var idDiv = document.getElementById('HiddenControlsDiv');    
	var button = GetElement(idDiv, "LogOutButton", "INPUT");
	button.click();
}

function AlertNumberOfLogonsLeft(numberLoggonsLeft, redirectAfterDisplay, directToAgentPortal)
{
	var page = null;
	var errors = new ErrorCollection();
	errors.AddInformation(Messages.TRIAL_USER_LOGONS_LEFT.format(numberLoggonsLeft));
	if (redirectAfterDisplay)
	{
	    errors.DisplayAll(RedirectUserLogin, null, false, true);
	}
	else if (directToAgentPortal)
	{
	    errors.DisplayAll(RedirectAgentPortal, null, false, true);
	}
	else
	{
	    errors.DisplayAll(null, null, false, true);
	}
}

function DisplayNumberOfLogonsLeft(numberLoggonsLeft, eventToRun, args)
{
	var page = null;
	var errors = new ErrorCollection();
	errors.AddInformation(Messages.TRIAL_USER_LOGONS_LEFT.format(numberLoggonsLeft));
	errors.DisplayAll(eventToRun, args, true, false);
}

function RedirectAgentPortal()
{
    var idDiv = document.getElementById('HiddenControlsDiv');    
	var button = GetElement(idDiv, "LogOtherUsersOutButton", "INPUT");
	button.click();
}