/*
name: workspace.security.logonPage
extends: workspace.security.logon
depends: workspace.UI
*/
function LogonPage()
{

	var txtUser
	var txtPassword
	var elemGo
	
	this.ondocumentcomplete = function()
	{
		txtUser = document.getElementById("txtUser")
		txtPassword = document.getElementById("txtPassword")		
		elemGo = document.getElementById("elemGo")
		txtUser.attachEvent("onkeydown", txt_onkeydown)	
		txtPassword.attachEvent("onkeydown", txt_onkeydown)	
		elemGo.attachEvent("onclick", elemGo_onclick)		
				
		tryNTLogon()	
	}

  function txt_onkeydown()
  {
		if (event.keyCode == workspace.UI.keyCode.enter)
		{
			if (txtUser.value.length > 0 && txtPassword.value.length > 0)
				tryFormLogon(workspace.security.getLogon('<logon/>'))
		}
  }
    
    
	function elemGo_onclick()
	{
		tryFormLogon(workspace.security.getLogon('<logon/>'))
	}

	function tryNTLogon()
	{
		var oXML = workspace.XML.getDOM()
		
		oXML.async = false
	
		try
		{
			oXML.load("/_security/NTlogonTest.asp")
			if (oXML.selectSingleNode("/logon/@userID"))
			{
				onsuccess()
			}
			else
			{
				document.body.style.visibility = "visible"
				try
				{
    				txtUser.focus();
    			}
    			catch(e){}
			}
		}
		catch(e)
		{
			document.body.style.visibility = "visible"
			try
			{
				txtUser.focus();
			}
			catch(e){}
		}		
	}

	function tryFormLogon(oLogon)
	{
		oLogon = attemptLogon(oLogon)
		
		if (oLogon)
		{
			if (oLogon.getPasswordExpired())
			{
				
				if (!workspace.isIE)
				{
					alert('Your password has now expired.  Contact a systems administrator')
					oLogon.putPassword(null);
					return false;
				}
				
				alert('Your password has now expired.  You will need to enter a new password to continue.')
				
				oLogon.putPassword(txtPassword.value);
				
				if (window.showModalDialog('/_security/changepassword.asp', oLogon.element, 'dialogWidth:300px;dialogHeight:140px;status:no;resizable:no;toolbar:no;help:no'))
				{
					txtPassword.value = oLogon.getNewPassword(txtPassword.value);
					oLogon.putUserID(null);
					oLogon.putPassword(null);
						
					oLogon = attemptLogon(oLogon)
				}
				else
				{
					oLogon.putPassword(null);
					oLogon = null;
				}						
			}
		}
				
		if (oLogon && oLogon.getUserID()) 
		{
			onsuccess()
		}
		else
		{
			alert('Incorrect Username or Password')
			txtUser.focus()
		}
	}

	function attemptLogon(oLogon)
	{
		
		var oXML
		
		oLogon.putNonce(workspace.security.getNonce());
		
		if (oLogon.getNonce())
		{
			oLogon.putUserName(txtUser.value);
			oLogon.createHash(txtPassword.value);
		
			try
			{

				oXML = oLogon.post("/_security/FormLogonTest.asp", true)
				return workspace.security.getLogon(oXML)
			}
			catch(e)
			{
			
				oXML = e.selectSingleNode("/error/extraInfo/logon")
			
				if (oXML)
				{
					return workspace.security.getLogon(oXML)
				}
				else
				{
					workspace.XML.showFriendlyError(oXML)
				}
			}
		}
		
		return null;
	}

	function onsuccess()
	{

		var hTimeout;
		
		registerWithCacheServer(doReload);
		
		hTimeout = window.setTimeout(doReload, 30000)
		
		function doReload()
		{
			
			if (hTimeout) window.clearTimeout(hTimeout)
			hTimeout = null;

			window.location.reload(false);
		}

	}

	function registerWithCacheServer(fnComplete)
	{
		if (!workspace.isIE)
		{
			fnComplete()
			return
		}
		
		try
		{
			var oXML = workspace.XML.get("/_security/GetLOI.asp", null, true)
			var gen = workspace.XML.builder.getGeneric(oXML)
			var bCacheRegistrationComplete = false
			
			if (gen.getAttribute("loi", null))
			{
				sURL = gen.getSimpleChild("publishServer")
				
				if (sURL.slice(-1) != '/') sURL += '/'
				
				sURL += '_security/RegisterLogon.asp'
				
				sURL += '?loi=' + encodeURIComponent(gen.getAttribute("loi"))
				sURL += '&siteID=' + gen.getID()
				
				var fraRegWithCache = document.createElement("iframe")
				fraRegWithCache.style.display = 'none'
				fraRegWithCache.src = sURL
				document.body.appendChild(fraRegWithCache)

				fraRegWithCache.onreadystatechange = onStateChange
				
				if (fraRegWithCache.readyState == 'complete') onStateChange()
				
				function onStateChange()
				{
					if (!bCacheRegistrationComplete)
					{
						if (this.readyState == 'complete')
						{
							bCacheRegistrationComplete = true;
							this.onreadystatechange = null
							fnComplete()
						}
					}
				}
			}
			else
			{
				fnComplete()
			}
		}
		catch(e) {fnComplete()}
	}

}
thisPage.specialise(LogonPage)