// *****************************************
//  BJ: Cross-browser operability JavaScript - must be called prior to JScriptRoutines in order for browser detect to work
// *****************************************

// browser version / OS flags
var bFirefox, bIE, bNetscape, bIE9, bIE8, bIE7, bIE6, bIE5, bIE55, bNetscape6, bNetscape7, bOldBrowser, bWinXP;
var bBrowserDetectInit = false;

// set bIE, bNetscape, etc
function BrowserDetect() {
	if(!bBrowserDetectInit) {
		bFirefox = bIE = bNetscape = bIE9 = bIE8 = bIE7 = bIE6 = bIE5 = bIE55 = bNetscape6 = bNetscape7 = bOldBrowser = bWinXP = false;
		// DEBUG: 	alert(navigator.userAgent);
		
		bIE = UA( "MSIE" );
		if( bIE ) {																													// Browser is Internet Explorer
			bIE9 = bIE8 = bIE7 = bIE6 = bIE5 = bIE55 = ( UA( "MSIE 9" ) );		// bIE9, bIE8, bIE7, bIE6, bIE5 and bIE55 true if IE 9
			if( !bIE9 ) {
				bIE8 = bIE7 = bIE6 = bIE5 = bIE55 = ( UA( "MSIE 8" ) );					// bIE8, bIE7, bIE6, bIE5 and bIE55 true if IE 8
				if( !bIE8 ) {
					bIE7 = bIE6 = bIE5 = bIE55 = ( UA( "MSIE 7" ) );							// bIE7, bIE6, bIE5 and bIE55 true if IE 7
					if( !bIE7 ) {
						bIE6 = bIE5 = bIE55 = ( UA( "MSIE 6" ) );										// bIE6, bIE5 and bIE55 true if IE 6
						if( !bIE6 ) {
							bIE5 = bIE55 = UA( "MSIE 5.5" );													// otherwise, bIE5 and bIE55 true if IE 5.5
							if( !bIE55 ) {
								bIE5 = UA( "MSIE 5" );																	// otherwise, bIE5 true if IE 5
								if( !bIE5 )
									bOldBrowser = true;																		// otherwise bOldBrowser true
							}
						}
					}
				}
			}
		}
		else {
			bFirefox = UA( "Firefox" );
			if( !bFirefox ) {
				bNetscape = UA( "Netscape" );
				if( bNetscape ) {																					// Browser is Netscape
					bNetscape7 = bNetscape6 = UA( "Netscape/7" );						// bNetscape6 and bNetscape7 true if Netscape 7
					if( !bNetscape7 ) {
						bNetscape6 = UA( "Netscape6" );												// otherwise, bNetscape6 true if Netscape 6
						if( !bNetscape6 )
							bOldBrowser = true;																	// otherwise bOldBrowser true
					}
				}
				else
					bOldBrowser = true;																			// bOldBrowser true if unknown browser
			}
		}
		
		// OS version detection
		bWinXP = UA("Windows NT 5.1");																// bWinXP = Windows XP flag

		bBrowserDetectInit = true;																	// indicate that BrowserDetect complete
	}
}

// returns true if userAgent string includes sText, otherwise false
function UA( sText ) {
	return ( navigator.userAgent.indexOf( sText ) > 0 ) ? true : false;
}


// routines for setting up standard push()/pop() methods on all Array objects
function ExtendIE5Array() {
	Array.prototype.push = IE5ArrayPush;
	Array.prototype.pop = IE5ArrayPop;
}
function IE5ArrayPush(sText) { 	this[this.length] = sText; 	}
function IE5ArrayPop(sText) { 	var vLast = this[this.length-1]; this.length--; return vLast; }


// POINTERS ***********************************************************************************

// sets up global pointer to element with specified ID to cope with Netscape
//   If IE, returns pointer to element, accessed via document.all
function Ptr(sID) {
	if(bIE)
		return eval(sID);
	else if(bNetscape) {
		var oEl = document.getElementById(sID);
		// if global pointer to oEl doesn't already exist, set one up
		if( (typeof oEl != "undefined") && (oEl != undefined) )
			eval( sID + " = oEl" );
		return oEl;
	}
	else window.status = "ERROR: Ptr(): Unsupported browser type";
}


// ATTRIBS ***********************************************************************************

function GetNSAttrib( oEl, sAttribName ) {
	return oEl.attributes.getNamedItem(sAttribName).value;
}


// custom expando attribute object for use in Netscape 6+
function GetAttrib( sObjID, sAttribName ) {
	this.toString = ReturnAttrib;									// override default property to return specified attribute from specified element
	this.attribName = sAttribName;
	this.id = sObjID
	this.oAttrib = eval( sObjID + ".attributes.getNamedItem('" + sAttribName + "')" );
	
	function ReturnAttrib() {
		if( typeof this.oAttrib != "undefined" )
			return this.oAttrib.value;
		else
			return "";
	}
}


// BJ: adds IE-like expando property to specified object (oi oi!)
// 	e.g.:	 	AddAttrib( "oFrm.oCountry", "oldvalue" );
// 					(above sets up oFrm.oCountry.value so can access/set 'value' attribute of oCountry, as can in IE)
//
function AddAttrib( sObjID, sAttribName ) {
	// DEBUG: 	alert("Adding '" + sAttribName + "' attribute to '" + sObjID + "' element");
	if( typeof eval(sObjID) != "undefined" )
		eval( sObjID + "." + sAttribName + " = new GetAttrib( sObjID, sAttribName )" );
}


// *****************************************
// CHG specific x-browser routines
// *****************************************

function SetupNSPointers() {
	if(bNetscape && (typeof tblLayout == "undefined")) {
		Ptr("tblLayout");
		Ptr("tblMain");
		Ptr("tdMain");
		Ptr("tdContent");
		Ptr("flashCode");
		Ptr("divImagesBanner");
		Ptr("contentFrame");
		Ptr("flashBanner");
		Ptr("ulMenu");
		Ptr("divTodaysDate");
	}
}

function FitFrameToContentNS() {
	Msg("FitFrameToContentNS() called");
	
	try {
		tblMain.style.height = "0px";																												// minimise content table
		var iContentHeight = contentFrame.contentDocument.documentElement.offsetHeight;			// get content height
		tblMain.style.height = iContentHeight + 5;																					// resize content table to fit content
	}
	catch(oErr) {
		WarnMsg("FitFrameToContentNS() could't resize content frame (access denied due to cross-domain content?)");
		tblMain.style.height = "100%";
	}
}

// make top level items with sub menus navigate to first sub item when clicked
function InitTopLevelLinksNS() {
	var oLinks = ulMenu.getElementsByTagName("A");
	for(i=0; i<oLinks.length; i++)
		if(oLinks[i].parentNode.parentNode == ulMenu) {
			// for each top level link with 'empty' href, duplicate href of first sub link instead
			if(oLinks[i].href == "javascript:void(false)")
				oLinks[i].href = oLinks[i].parentNode.getElementsByTagName('A')[1].href;
		}
}
