window.onload = initPage;
window.onresize = resizeContent;

//initPage: initializes a page after it loads
function initPage() 
{
	resizeContent();
}

//resizeContent: resizes the Content div based on the width of the page
function resizeContent() {
	var oSecNav = getObj("secNav");
	var oContent = getObj("content");
	var oMainContent = getObj("mainContent");
	var oHeader = getObj("header");
	var oFooter = getObj("footer");
	var oRelated = getObj("related");
	var oCopyright = getObj("copyright");
	var iMinWidth = 600;
	
	//set the width of the content div
	var iCopyrightWidth;
	if (oCopyright) 
	{ 
		iCopyrightWidth = Math.max(0,getWidth(oCopyright));
	} 
	else 
	{
		iCopyrightWidth = 0;
	}
	var iPageWidth = iCopyrightWidth;

	var iSecNavWidth;
	var iSecNavHeight;
	var iSecNavTop;
	var iSecNavBottom;
	if (oSecNav) 
	{ 
		if (mBrowser.isIE) { setHeight(oSecNav,5); }
		iSecNavWidth = Math.max(0,getWidth(oSecNav));
		iSecNavHeight = Math.max(0,getHeight(oSecNav));
		iSecNavTop = getTop(oSecNav);
		iSecNavBottom = iSecNavTop + iSecNavHeight;
	} 
	else 
	{
		iSecNavWidth = 0;
		iSecNavHeight = 0;
		iSecNavTop = 0;
	}
	
	var iContentWidth = Math.max(iPageWidth - iSecNavWidth,iMinWidth);
	var iContentHeight;
	var iContentTop;
	var iContentBottom;
	var iContentBottomMargin = 22;
	if (oContent) 
	{
		iContentTop = getTop(oContent);

		//set the width of the content div
		setWidth(oContent,iContentWidth);

		var iRelatedWidth;
		if (oRelated) 
		{ 
			iRelatedWidth = Math.max(0,getWidth(oRelated));
		} 
		else 
		{
			iRelatedWidth = 0;
		}

		//set the width of the main content area
		var iMainContentWidth = iContentWidth - iRelatedWidth - 40;
		if (oMainContent)
		{
			setWidth(oMainContent,iMainContentWidth);
		}

		iContentHeight = Math.max(0,getHeight(oContent));
	}
	else
	{
		iContentHeight = 0;
		iContentTop = 0;
		iContentBottom = 0;
	}
			
	iContentBottom = getTop("footer");
	
	//set the height of the content area if secNav is taller
	if (iSecNavBottom > iContentBottom)
	{
		if (oContent) 
		{ 
			setHeight(oContent,iSecNavBottom - iContentTop - iContentBottomMargin);
		}
	}
	else if (iSecNavBottom < iContentBottom)
	//otherwise, set the height of the secNav div
	{
		//set the height of the secNav div
		if (oSecNav) 
		{
			//set the height of the content div
			var iSecNavHeight = iContentBottom - iSecNavTop;
			setHeight(oSecNav,iSecNavHeight);
		}	
	}

	//set the width of the header and footer
	if (oHeader && oFooter)
	{
		iPageWidth = iSecNavWidth + iContentWidth;
		setWidth(oHeader,iPageWidth+10);
		setWidth(oFooter,iPageWidth);
	}
}

//highlight: navigation rollovers --------------------------------------------------
function highlight(sImg,sSrc) {
	var oImg = getObj(sImg);
	oImg.src = sSrc;
}

//showFlash: wrapper for popUp function to show flash movies
function showFlash(sFlashContentURL, iWidth, iHeight) {
	popUp(sFlashContentURL, iWidth, iHeight, 'flash',"","",false);
}

//setCookie: creates a cookie ------------------------------------------------------
function setCookie(sName,sValue,sExpires) 
{
	if (sExpires == null) 
	{
		//if not expiration date is provided, set the expiration date
		//to a long time from now
		var dExpires = new Date("12/31/2010");
	} else
	{
		dExpires = new Date(sExpires);
	}
	var sCookieString = sName + "=" + escape(sValue) + "; expires=" + dExpires.toUTCString();
	document.cookie = sCookieString;
}

//getCookie: retrieves a cookie ------------------------------------------------------
function getCookie(sName) 
{
  	// cookies are separated by semicolons
  	var aCookie = document.cookie.split("; ");
  	var iCookieLength = aCookie.length;
  	for (var i=0; i < iCookieLength; i++)
  	{
    	// a name/value pair (a crumb) is separated by an equal sign
    	var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
		{
    	  return unescape(aCrumb[1]);
		}
  	}
  	//return null if a cookie with the requested name does not exist
  	return null;
}

//getFullYear: returns the full four-digit year
function getFullYear(d)
{
	var y = d.getYear(d);
	if (y < 1000)
	{
		y += 1900;
	}
	return y;
}

//isValidDate: validates whether a date is a real date
function isValidDate(d,sFieldName)
{
	if (isNaN(d))
	{
		alert("The " + sFieldName + " is not valid");
		return false;
	}
	return true;
}

//getLastDateOfMonth: returns the date representing the last date of the month
function getLastDateOfMonth(dDate)
{
	var iDay = dDate.getDate();
	var iMonth = dDate.getMonth();
	var iYear = dDate.getFullYear();
	if (mBrowser.isNS4)
	{
		var aDates = new Array(31,30,29,28);
		var iDatesLength = aDates.length;
		
		for (var i=0; i < iDatesLength; i++) 
		{
			var dNewDate =  new Date(iYear,iMonth,aDates[i]);
			if (dNewDate.getMonth() == iMonth)
			{
				return dNewDate;
			}
		}
	}
	else
	{
		var dNewDate =  new Date(iYear,iMonth + 1,0);
		return dNewDate;
	}
}

//isPositive: determines whether iNumber is a number and is greater than zero
function isPositive(iNumber)
{
 	//check whether iNumber is a number and is greater than zero
	if (isNaN(iNumber/iNumber) || iNumber < 0)
	{
		return false
	}
	else
	{
		return true;
	}
}

//compareDates: compares two dates and returns false if the FROM date isn't <= TO date
function compareDates(dFromDate,dToDate)
{
	//make sure the TO date is after the FROM date
	if (dToDate.getTime() - dFromDate.getTime() < 0)
	{
		alert("The TO date must be equal to or after the FROM date");
		return false;
	}
	else
	{
		return true;
	}
}

//setOptions: adds options to a select box
function setOptions(oSelect,oOptions)
{
	var iNumOptions = oSelect.length;
	
	if (!mBrowser.isNS4)
	{
		//initialize the list
		for (var i=0; i < iNumOptions; i++) 
		{
			oSelect.options.remove(0);
		}
		
		//add the new options
		var iNumOptions = oOptions.length;
		for (var i=0; i < iNumOptions; i++) 
		{
			var oOption = document.createElement("OPTION");
			oOption.text = oOptions[i].text;
			oOption.value = oOptions[i].value;
			oOption.defaultSelected = oOptions[i].defaultSelected;
			oSelect.options.add(oOption);
		}
	}
	else
	{
		//initialize the list
		for (var i=0; i < iNumOptions; i++) 
		{
			oSelect.options[0] = null;
		}
		
		//add the new options
		var iNumOptions = oOptions.length;
		for (var i=0; i < iNumOptions; i++) 
		{
			oSelect.options[i-1] = new Option(oOptions[i].text,oOptions[i].value,oOptions[i].defaultSelected,false);
		}
	}
}

//isEnterKey: determines whether the Enter key was pressed
function isEnterKey()
{
	if (!mBrowser.isNS4)
	{
		if (window.event.keyCode == 13)   
		{
			return true;
		}
		return false;
	}
	return null;
}