//
// *** Header ***
//
function displayHeader( doc, pageDescriptor )
{
	doc.write( buildHeader(pageDescriptor) )
}

function buildHeader( pageDescriptor )
{
	// Our return string
	var sHTML = ''

	// Grab a reference to the header object
	var objHeader = pageDescriptor.objPageHeader
	if ( objHeader != null )
	{
		// Ensure that Header is enabled
		// If a header logo is specified, then display the logo.
		// This allows one to turn off the header-links, but still display the logo.
		if ( (!scCommon.bNoHeader) || (!isEmptyString(objHeader.objLogo.sImg)) )
		{
			// Open the header DIV section
			sHTML += '<div id="HEADER">\n'

			// Add the interdocument link to skip header links
			sHTML += getLinksSkipperHTML( 'skip_header_links', makeMediaPath( 'transparent_pixel.gif' ), scAccessibility.sSkipHeaderLinksAltText );

			// Write the links in order, separating them with a "-"
			if ( !scCommon.bNoHeader )
			{
				sHTML += '<div id="HEADER-LINKS">\n'
				var arLinks = objHeader.arLinks
				var nNumLinks = arLinks.getNumLinks()
				for (var i = 0; i < nNumLinks; i++ )
				{
					var oLink = arLinks.getLink(i);
					if ( (oLink.sID == 'WORKINGON') && !objCore.DisplayWorkingOnHeader( pageDescriptor.ePageID ) )
						continue;

					// Add the HTML for the link
					sHTML += oLink.getHTML();

					// If this isn't the last link, add the dash.
					if (i < nNumLinks-1)
						sHTML += '\n-\n'
				}
				sHTML += '\n</div>\n'
			}

			// Add the target for skipping header links
			sHTML += getLinksSkipperTargetHTML( 'skip_header_links' );

			// If there is a logo, write it out as well.
			if ( !isEmptyString(objHeader.objLogo.sImg) )
			{
				sHTML += '<div id="HEADER-LOGO">\n'
				sHTML += objHeader.objLogo.getHTML()
				sHTML += '\n</div>\n'
			}

			// Close the DIV section
			sHTML += '</div>\n'
		}
	}

	return sHTML
}


//
// *** Footer ***
//
function displayFooter( doc, pageDescriptor )
	{
	// The footer includes the footer images and the copyright.
	//	scCommon.bNoFooter can turn off the footer images.
	//  scCommon.bNoCopyright can turn off the copyright.
	var fDoFooter = false;
	if ( !scCommon.bNoFooter && (pageDescriptor.objPageFooter != null) )
		fDoFooter = true;
	if ( !scCommon.bNoCopyright )
		fDoFooter = true;

	if ( fDoFooter )
		{
		// Create the footer DIV section
		doc.write( '<div id="FOOTER">\n' )

		displayFooterImages( doc, pageDescriptor )
		displayCopyright( doc, pageDescriptor )

		// Close the footer
		doc.write( '</div>\n' )
		}
	}

function displayFooterImages( doc, pageDescriptor )
{
	doc.write( buildFooter(pageDescriptor) )
}

function buildFooter( pageDescriptor )
{
	// Our return string
	var sHTML = ''

	// Ensure that Footer is enabled
	if ( !scCommon.bNoFooter )
	{
		var objFooter = pageDescriptor.objPageFooter
		if ( objFooter != null )
		{
			// Add the line separator
			var img = scCommonImages.sFooterTopLine
			if ( isEmptyString( img ) )
			{
				var nLineHeight = scCommon.nFooterTopLineHeight
				if ( nLineHeight == null )
					nLineHeight = 1

				if ( nLineHeight > 0 )
					sHTML += '<hr id="FOOTER-TOPLINE" noshade size="' + nLineHeight + '">\n'
			}
			else
			{
				sHTML += '<div id="FOOTER-TOPLINE">' + objCore.getHTImgHTML( makeMediaPath(img) )+ '</div>\n'
			}

			// Add the interdocument link to skip footer links
			sHTML += getLinksSkipperHTML( 'skip_footer_links', makeMediaPath( 'transparent_pixel.gif' ), scAccessibility.sSkipFooterLinksAltText );

			var arLinks = objFooter.arLinks
			var sLoc = scCommon.sFooterLocation.charAt(0).toUpperCase()
			if (sLoc == '')
				sHTML += buildDefaultFooter( arLinks )
			else
			{
				// Just lay out the images, one after the other, then set the location
				sHTML += '<div id="FOOTER-IMAGES">\n';
				sHTML += getFooterLinks( 0, arLinks.getNumLinks(), arLinks )
				sHTML += '</div>\n'
			}

			// Add the links skipper target
			sHTML += getLinksSkipperTargetHTML( 'skip_footer_links' );
		}
	}

	return sHTML
}

function buildDefaultFooter( arLinks )
{
	var sHTML = ''

	// For now, assume that none of the footers has a position already specified.
	// To determine link locations, we do the following:
	// 1 - divide the number of links by 3 ==> n
	// 2 - if the remainder is 2, then (n+1) on the left and right, and n in the middle.
	//                         1, then (n+1) in the middle, and n on the left and right.
	//                         0, then n each for left, middle, and right.
	// Add the beginning of the table and the 1 row
	sHTML += '<table width="100%" id="FOOTER-IMAGES">\n<tr>\n'

	// Do the division by 3 (get quotient and remainder)
	var nQuotient  = Math.floor( arLinks.getNumLinks() / 3 )
	var nRemainder = arLinks.getNumLinks() % 3

	// Set the number to be dropped into each section
	var nSides = nQuotient
	var nMiddle = nQuotient
	if ( nRemainder == 2 )
		nSides++
	else if ( nRemainder == 1 )
		nMiddle++

	// Set the index of the first image for each section
	var nStartLeft   = 0
	var nStartMiddle = nSides
	var nStartRight  = nSides + nMiddle

	// Left
	sHTML += '<td align="left">'
	sHTML += getFooterLinks( nStartLeft, nSides, arLinks )
	sHTML += '</td>\n'

	// CENTER
	sHTML += '<td align="center">'
	sHTML += getFooterLinks( nStartMiddle, nMiddle, arLinks )
	sHTML += '</td>\n'

	// RIGHT
	sHTML += '<td align="right">'
	sHTML += getFooterLinks( nStartRight, nSides, arLinks )
	sHTML += '</td>\n'

	// Close the row and the table
	sHTML += '</tr>\n</table>\n'

	return sHTML
}

function getFooterLinks( nStart, nNum, arLinks )
{
	var sHTML = ''
	var nEnd = nStart + nNum
	for (var i = nStart; i < nEnd; i++ )
	{
		sHTML += arLinks.getLink(i).getHTML()
	}

	return sHTML
}


//
// *** Title ***
//
function displayTitle( doc, pageDescriptor )
{
	doc.write( buildTitle(pageDescriptor) )
}

function buildTitleStyle( img, position )
{
	var sHTML = ' style="BACKGROUND-IMAGE: URL(' + img + ');'
		              + 'BACKGROUND-REPEAT: NO-REPEAT;'
		              + 'BACKGROUND-POSITION: ' + position + ';'
		              + '"'

	return sHTML
}

function buildTitle( pageDescriptor )
{
	var BGPOS_LEFT = "LEFT CENTER"
	var BGPOS_RIGHT = "RIGHT CENTER"
	var BGPOS_CENTER = "CENTER CENTER"

	var sHTML = ''
	var sLeft = ''
	var sRight = ''

	var sBottomImageLine = ''

	var img = scCommonImages.sLine
	if ( img == '' )
		sBottomImageLine = ''
	else
		sBottomImageLine = '<div id="TITLE-BOTTOMLINE">' + objCore.getHTImgHTML( makeMediaPath(img) ) + '</div>'

	var sTitle   = HtmlEncode(pageDescriptor.sPageTitle);
	var sStudent = HtmlEncode(pageDescriptor.sUserDisplayName);
	if ( !isEmptyString( pageDescriptor.sEffectiveUserDisplayName ) )
		{
		var sWorkingOnID = 'TITLE-WORKINGONREADONLY';
		if ( pageDescriptor.fEditMode )
			var sWorkingOnID = 'TITLE-WORKINGON';

		sStudent += '&nbsp;<span id="' + sWorkingOnID + '">' + HtmlEncode(pageDescriptor.sWorkingOn) + '</span>';

		sStudent += '<span id="TITLE-WORKEDON">&nbsp;' + HtmlEncode(pageDescriptor.sEffectiveUserDisplayName) + '</span>';
		}

	// Check if we have background images.
	var sTitleImg = isEmptyString( scCommonImages.sTitleBG ) ? null : makeMediaPath( scCommonImages.sTitleBG )
	var sStudentImg = isEmptyString( scCommonImages.sTitlePersonBG ) ? null : makeMediaPath( scCommonImages.sTitlePersonBG )

	var sLoc = scCommon.sTitleLocation.charAt(0).toUpperCase()
	if (sLoc == 'C')
	{
		var sTitleStyle = ''
		var sStudentStyle = ''

		if ( sTitleImg != null )
			sTitleStyle = buildTitleStyle( sTitleImg, BGPOS_CENTER )
		if ( sStudentImg != null )
			sStudentStyle = buildTitleStyle( sStudentImg, BGPOS_CENTER )


		// Don't add an empty row if the student has not logged in yet.
		if ( !isEmptyString(sStudent) )
			sLeft  = '<tr><td id="TITLE-PERSON" align="CENTER"' + sStudentStyle + '>' + sStudent + '</td></tr>\n'
		sRight = '<tr><td id="TITLE-NAME" align="CENTER"' + sTitleStyle + '>'   + sTitle + '</td></tr>\n'

		// Put it together
		sHTML += '<div id="TITLE">\n'
				+ '<table ' + objCore.g_arPseudoStyles['titlestyle'] + '>\n'
				+ sLeft
				+ sRight
				+ '</table>\n'
				+ sBottomImageLine
				+ '</div>\n'
	}
	else
	{
		var sTitleStyle = ''
		var sStudentStyle = ''

		if ( sTitleImg != null )
			sTitleStyle = buildTitleStyle( sTitleImg, (sLoc == 'R') ? BGPOS_RIGHT : BGPOS_LEFT )
		if ( sStudentImg != null )
			sStudentStyle = buildTitleStyle( sStudentImg, (sLoc == 'R') ? BGPOS_LEFT : BGPOS_RIGHT )

		// If the first name and last name are empty, then set the student name
		// to a string of non-breaking spaces.  This forces the display to "reserve"
		// space for the first/last name.  Why is this important?  Only when
		// someone signs on and then uses the browser Back button.  Without reserving
		// the space, the name gets drawn partially off of the side of the page.
		if ( isEmptyString(sStudent) )
			sStudent = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'

		// If we have no title, then set the title to a string of non-breaking spaces.
		// This is to ensure that the title background (if any) actually gets displayed
		if ( isEmptyString(sTitle) )
			sTitle = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'

		if (sLoc == 'R')
		{
			sLeft  = '<td ' + objCore.g_arPseudoStyles['titleheight'] + ' NOWRAP id="TITLE-PERSON" align="LEFT"' + sStudentStyle + '>' + sStudent + '</td>\n'
			sRight = '<td ' + objCore.g_arPseudoStyles['titleheight'] + ' id="TITLE-NAME" align="RIGHT"' + sTitleStyle + '>'  + sTitle + '</td>\n'
		}
		else // (sLoc == 'L')
		{
			sLeft  = '<td ' + objCore.g_arPseudoStyles['titleheight'] + ' id="TITLE-NAME" align="LEFT"' + sTitleStyle + '>'    + sTitle + '</td>\n'
			sRight = '<td ' + objCore.g_arPseudoStyles['titleheight'] + ' NOWRAP id="TITLE-PERSON" align="RIGHT"' + sStudentStyle + '>' + sStudent + '</td>\n'
		}

		// Put it together
		sHTML += '<div id="TITLE">\n'
				+ '<table " ' + objCore.g_arPseudoStyles['titlestyle'] + '>\n'
				+ '<tr>\n'
				+ sLeft
				+ sRight
				+ '</tr>\n'
				+ '</table>\n'
				+ sBottomImageLine
				+ '</div>\n'
	}

	return sHTML
}

function setPersonNameInTitle( doc )
	{
	var sStudent = getSessionPersonName();
	if ( !isEmptyString ( sStudent ) )
		{
		var elem = doc.getElementById('TITLE-PERSON');
		if ( elem )
			elem.innerHTML = sStudent;

		return true;
		}

	return false;
	}

//
// *** Trail Links ***
//

// Displays the "page trail" and the header links.
// The page trail links are on the left-justified and the header
// links are right-justified.
function displayTrail( doc, pageDescriptor )
{
	doc.write( buildTrail(pageDescriptor) )
}

function buildTrail( pageDescriptor )
{
	// Our return string
	var sHTML = ''

	// Ensure that PageTrail is enabled
	if ( !scCommon.bNoPageTrail )
	{
		// Get a reference to the PageTrail object
		var objTrail = pageDescriptor.objPageTrail
		if ( (objTrail != null) && (objTrail.arLinks.getNumLinks() > 0) )
		{
			// Add the interdocument link to skip trail links
			if ( objTrail.arLinks.getNumLinks() > 1 )
				sHTML += getLinksSkipperHTML( 'skip_trail_links', makeMediaPath( 'transparent_pixel.gif' ), scAccessibility.sSkipTrailLinksAltText );

			// Create the table for formatting
			sHTML += '<div id="TRAIL">\n' 
			sHTML += '<table width="100%" border="0" cellpadding="4" cellspacing="0">\n'
			sHTML += '<tr>\n'

			// Add the PageTrail links
			// The "<nobr>" tags are set up to keep a link from breaking down to the next
			// line.  The "|" separator stays with the link that it PRECEDES.
			sHTML += '<td id="TRAIL-LINKS" align="LEFT" valign="BOTTOM">\n'
			sHTML += '<nobr>' + WSSHTLink.getLinksHTML( objTrail.arLinks, '</nobr> <nobr>| ' ) + '</nobr>'
			sHTML += '</td>\n'

			// Get the Header links (if the regular header is not displayed)
			if ( scCommon.bNoHeader )
			{
				var htm = '';

				var arLinks = pageDescriptor.objPageHeader.arLinks
				var nNumLinks = arLinks.getNumLinks()
				for (var i = 0; i < nNumLinks; i++ )
				{
					// Add the HEADER OPTION links
					var oLink = arLinks.getLink(i);
					if ( (oLink.sID == 'WORKINGON') && !objCore.DisplayWorkingOnHeader( pageDescriptor.ePageID ) )
						continue;

					// Add the HTML for the link
					if ( !isEmptyString( htm ) )
						htm += '</nobr> <nobr>| '
					htm += oLink.getHTML();
				}

				if ( !isEmptyString( htm ) )
					sHTML += '<td id="TRAIL-HEADER-LINKS" align="RIGHT" valign="BOTTOM">\n'
					       + '<nobr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + htm + '</nobr> ';
			}

			// Close the table for formatting
			sHTML += '</tr>\n'
			sHTML += '</table>\n'
			sHTML += '</div>\n' 

			// Add the target for skipping trail links
			// Don't add this if only one trail link
			if ( objTrail.arLinks.getNumLinks() > 0 )
				sHTML += getLinksSkipperTargetHTML( 'skip_trail_links' );
		}
	}

	return sHTML
}

//
// *** Page Description ***
//
function buildDescriptionAndLinksRow( sDesc, fDescIsHTML, arLinks )
{
	var sHTML = ''

	if ( !isEmptyString( sDesc ) || ((arLinks!=null) && (arLinks.getNumLinks() > 0)) )
		{
		// Create a table and a single row for the page description.
		// Add the page description to the left cell.
		// If there are "CANDO" links, create a right cell and add them.
		sHTML = '<table id="DESCRIPTION" border="0" cellpadding="0" cellspacing="0" width="100%">\n'
			+ '<tr>\n'
			+ '<td id="PAGE-DESC" align="LEFT" valign="TOP">'
			+ (fDescIsHTML ? sDesc : HtmlEncode(sDesc))
			+ '</td>\n'

		if ( (arLinks!=null) && (arLinks.getNumLinks() > 0) )
		{
			sHTML += '<td class="CANDO-LINKS" align="RIGHT" valign="TOP">'
			sHTML += '<nobr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + WSSHTLink.getLinksHTML( arLinks ) + '</nobr>';
			sHTML += '</td>\n'
		}

		sHTML += '</tr>\n'
			+ '</table>\n'
	}

	return sHTML
}

function buildPageDescription( pageDescriptor )
{
	return buildDescriptionAndLinksRow( pageDescriptor.sPageDesc,
	                                    pageDescriptor.fPageDescIsHTML,
	                                    pageDescriptor.getLinks( objCore.WSSHTLinkCategories.CANDO ) )
}

function displayPageDescription( doc, pageDescriptor )
{
	doc.write( buildPageDescription( pageDescriptor ) )
}

//
// *** Copyright ***
//
function buildCopyright( pageDescriptor )
{
	var sCopyright = ''

	if ( !scCommon.bNoCopyright )
	{
		sCopyright = '<div id="COPYRIGHT">\n' +
//					 HtmlEncode( pageDescriptor.sProductName ) + '<BR>\n' +
//					 HtmlEncode( pageDescriptor.sModuleName ) + ' ' +
//					 HtmlEncode( pageDescriptor.sModuleVersion ) + ' - ' +
					 HtmlEncode( pageDescriptor.sCopyright ) + '<BR>\n' +
					 (scCommon.bNoPageGenMsg ? '' : 'page generated ' + pageDescriptor.sGeneratedGMT + '<BR>\n') +
					 '</div>\n'
	}

	return sCopyright
}

function displayCopyright( doc, pageDescriptor )
{
	doc.write( buildCopyright(pageDescriptor) )
}

// Simple "message box"-type message used in a few places to avoid including displayDataEntry.js just for a message.
// Some day should add an URL and link title parameter for an "OK" button.
function buildSimpleMessage( sText )
	{
	var htm = '';
	htm += '<center><div class="msgbox"><div class="msgboxtext">'
	    + HtmlEncode(sText)
	    + '</div></div></center>\n';

	return htm;
	}

function displaySimpleMessage( doc, sText )
	{
	doc.write( buildSimpleMessage(sText) );
	}