if ( window.name == kMainFrameName && window.parent !== window )
{
	window.parent.location = window.location.href;
}
if ( ! worksiteBrowserCheck.ok ) 
{
	window.location = "bc.aspx";
}

function writeDialog()
{
	if (showSmallDialog) 
	{
		writeSmallDialog()
	}
	else
	{
		writeNormalDialog();
	}
}

function writeNormalDialog()
{
	document.write(
	'<table class="loginbox" align="center" cellspacing="0" cellpadding="0">' +
		'<tr>' +
			'<td align="center">' +
				'<div>' +
					'<img src ="login-images/' + logoImage + '" width="517" height="120" border="0" alt="' + htmlString(logoAltText) + '">' +
				'</div>' +
				getLoginFormHTML() +
			'</td>' +
		'</tr>' +
	'</table>' +
	getVersionHTML() +
	getValidateBrowserHTML() +
	getMessageWindowHTML());
}

function writeSmallDialog()
{
	document.write(
	'<table class="smalldialogloginbox" border="0" align="center" cellspacing="0" cellpadding="0">' +
		'<tr>' +
			'<td align="center">' + htmlString(getLocStrByLID(37, 'Your session has expired. You must log in again.')) + 
				getLoginFormHTML() +
			'</td>' +
		'</tr>' +
	'</table>' +
	getMessageWindowHTML());
}

function getLoginFormHTML()
{
	var html =
	'<div class="loginform">' +
		'<table align="center" border="0" cellspacing="0" cellpadding="0" width="260">' +
			'<iframe name="trusted-login-frame" width="0" height="0" src="login-images/pixel.gif" style="border-style:none;"></iframe>' +
			//fix for 36774:  IE 55 crashes when session expires and redirected to login --> per IB, IE55 having
			//problems submitting to iframe
			'<form action="scripts/login.aspx" method="post" id="fm" name="fm"' + (g_browser.isIE55 ? '' : ' target="trusted-login-frame"') + '>' +
				'<input type="hidden" name="timeZone" value="">' +
				'<input type="hidden" name="ReturnUrl" id="ReturnUrl" value="' + htmlString(returnUrl) + '">';
				if ( trustedLoginEnabled ) 
				{
					html += getTrustedLoginHTML();
				}
				html += getUsernamePasswordHTML();
				html += getLoginButtonHTML();	
				html +=
				'</form>' +
		'</table>' +
	'</div>';
	return html;
}

function getTrustedLoginHTML()
{
	var html =
	'<tr>' +
		'<td align="left" class="worksite-login-text">' + getString('334', '', 'Trusted Login', 'trustedLoginCheck') + '</td>' +
		'<td align="left"><input type="checkbox" name="trustedLoginCheck" id="trustedLoginCheck" onClick="disable()" onkeyup="onKey(event)"></td>' +
	'</tr>' +
	'<tr>' +
		'<td colspan="2"><img src="login-images/pixel.gif" alt="" width="1" height="10" border="0"></td>' +
	'</tr>';
	return html;
}

function getUsernamePasswordHTML()
{
	var html = 
	'<tr valign="middle">' +
		'<td align="left" class="worksite-login-text" id="userID-label">' + getString(497, '', 'User Name', 'userID') + ':</td>' +
		'<td align="left">' +
			'<input id="userID" name="userID" maxlength="64" type="text" value="' + htmlString(defaultUsername) + '" onfocus="this.select()" onkeyup="onKey(event)">' +
		'</td>' +
	'</tr>' +
	'<tr>' +
		'<td colspan="2"><img src="login-images/pixel.gif" alt="" width="1" height="10" border="0"></td>' +
	'</tr>' +
	'<tr>' + 
		'<td align="left" class="worksite-login-text" id="password-label">' + getString(320, '', 'Password', 'password') + ':</td>' +
		'<td align="left">' +
			'<input id="password" name="password" maxlength="32" type="password" onfocus="this.select()" onkeyup="onKey(event)">' +
		'</td>' +
	'</tr>' +
	'<tr>' +
		'<td colspan="2"><img src="login-images/pixel.gif" alt="" width="1" height="10" border="0"></td>' +
	'</tr>';
	return html;
}

function getLoginButtonHTML()
{
	var html =	
	'<tr>' +
		'<td><img src="login-images/pixel.gif" alt="" width="1" height="10" border="0"></td>' +
		'<td colspan="2">' +
			'<input type="button" onclick="doSubmit()" value="' + htmlString(getLocStrByLID(317, 'Login')) + '" class="loginbutton"/>' +
		'</td>' +
	'</tr>';
	return html;
}

function getValidateBrowserHTML()
{
	var html =
	'<table border="0" width="517px" align="center" cellspacing="0" cellpadding="0">' +
		'<tr>' +
			'<td><img src="login-images/pixel.gif" alt="" width="1" height="10" border="0"></td>' +
		'</tr>' +
		'<tr>' +
			'<td align="right">' + getAnchorStr(828, 'Validate Browser', 'bc.aspx', '', '', '', 'vertical-align:top;cursor:pointer;', '', '', '') +	'</td>' +
		'</tr>' +
	'</table>';
	return html;
}

function getVersionHTML()
{
	var html =
	'<table class="versioninfo" align="center" cellspacing="0" cellpadding="0">' +		
		'<tr>' +
			'<td align="center"><div class="versioninfo"><div style="padding-top:15px;">' + htmlString(getVersionString()) + '</div></div></td>' +
		'</tr>' +		
	'</table>';
	return html;
}

function getMessageWindowHTML()
{
	return '<div align="center" class="worksite-login-msg" id="msg-window"></div>';
}

function testLoginFrame(reset)
{
	var loginFrame = window.frames["trusted-login-frame"]; 
	if ( ! reset && loginFrame && loginFrame.document.readyState == 'complete' )
	{
		var html = loginFrame.document.body.innerHTML;
		var msg = html.indexOf("Error:") == 0 ? html.substr("Error:".length, html.length) : ""
		showWait(loginFrame.document.getElementById("login") != null, htmlString(msg))
	}
	else
	{
		window.setTimeout('testLoginFrame()', 100, 'JavaScript')
	}
}

function showWait(show, msg)
{
	document.body.style.cursor = show ? 'wait' :'auto'
	
	var obj = document.getElementById("msg-window")
	if ( obj )
	{
		obj.innerHTML = show ? htmlString(getLocStrByLID(448, 'Logging in (please wait)...')) : '<span class="worksite-error-msg">' + (msg ? msg : '&nbsp;') + '</span>';
	}
}		

function doSubmit()
{	
	var fm = document.forms["fm"];	
	var isTrustedLogin = fm.trustedLoginCheck && fm.trustedLoginCheck.checked;
	
	if((isTrustedLogin || (fm.userID.value && fm.password.value)))
	{	    
	    var trustedLoginCookie = TrustedLoginCookie();		
	    if ( fm.trustedLoginCheck && fm.trustedLoginCheck.checked ) 
	    {		
		    trustedLoginCookie.setState(trustedLoginCookie.getState() | TrustedLoginStates().Login);
	    }			
	    else
	    {
		    trustedLoginCookie.setState(TrustedLoginStates().None);
	    }

	    testLoginFrame(true)			
	    showWait(true);
	    fm.submit();						    
	}
}	

function autoTrustedLogin() 
{	
	var trustedLoginStates = TrustedLoginStates();
	var trustedLoginCookie = TrustedLoginCookie();
	var state = trustedLoginCookie.getState();
	trustedLoginCookie.setState(state & ~trustedLoginStates.Logout);
	
	var fm = document.forms["fm"];
	if ( (state & (trustedLoginStates.Auto | trustedLoginStates.Login))
		|| trustedLoginAuto	) 
	{
		var checkBox = fm.trustedLoginCheck
		if ( checkBox ) 
		{
			checkBox.checked = true
		}
		disable()
		
		if ( ((state & trustedLoginStates.Auto) || trustedLoginAuto) && ! (state & trustedLoginStates.Logout) )
		{
			doSubmit()
		}
	}
}

function disable()
{
	var fm = document.forms["fm"]
	var disabled = fm.trustedLoginCheck ? fm.trustedLoginCheck.checked : false;
	var className = "worksite-login-text" + (disabled ? "-disabled" : "")
	
	fm.userID.disabled = disabled
	fm.password.disabled = disabled

	document.getElementById("userID-label").className = className
	document.getElementById("password-label").className = className
}

function onKey(e)
{	
	if ( e && e.keyCode == keyCodes.ENTER )
	{
		doSubmit();
	}
}

function setFocus()
{
	var fm = document.forms["fm"];
	if ( fm )
	{
		if ( fm.userID 
			&& ! fm.userID.disabled
			&& ! defaultUsername ) 
		{
			fm.userID.focus();
		} 
		else if ( fm.password 
			&& ! fm.password.disabled )
		{
			fm.password.focus();
		}
	}
}
