﻿var context;

function initializeSNLibrary () {

	context = new SNContext ();
	context.browser ();

// supplement the CSS menu with javascript if IE6

	if (context.internetExplorer && context.version < 7) {
		cssjsmenu('navigationBar');
		if (document.getElementById) {
			var hoverCSS = document.getElementById('hoverCSS'); 
			hoverCSS.disabled = true;
		}
	}


//	Hide and disable the usaSiteFlag if this is a USA site page and either there is no
//	cookie (i.e. US browser) or the cookie is set to en-us (i.e. visiting US site).  
//	The usaSiteFlag link is used on pages, that are actually or apparently part of a
//	regional site, to get to the USA site.

//	var	regionPattern = /\/..-..\//;
//	var cookieRegion = readCookie ("region");

//	if (!regionPattern.test (document.URL)) {			// a USA site page
//		if (cookieRegion.length == 0 || cookieRegion.toLowerCase () == "en-us") {
//			disable ("usaSiteFlag");
//		}
//	}
}

// ------------------------------------------------------------------------------------

function SNContext () {
	this.internetExplorer = false;
	this.opera = false;
	this.mozilla = false;
	this.version;
	this.browser = determineBrowser;
	
	function determineBrowser () {
	
		var endPosition = 0;
		var startPosition = 0;
		
		if (navigator.appName == "Netscape") {
			this.mozilla = true;
			this.opera = false;
			this.internetExplorer = false;
			endPosition = navigator.appVersion.indexOf ('(');
			this.version = parseFloat (navigator.appVersion.substring (startPosition, endPosition));
		}
		else {
			if (navigator.appName == "Opera") {
				this.mozilla = false;
				this.opera = true;
				this.internetExplorer = false;
				endPosition = navigator.appVersion.indexOf ('(');
				this.version = parseFloat (navigator.appVersion.substring (startPosition, endPosition));
			}
			else {
				 if (navigator.appName.indexOf ("Explorer") != -1) {
					this.mozilla = false;
					this.opera = false;
					this.internetExplorer = true;
					startPosition = navigator.appVersion.indexOf ("MSIE");
					startPosition = startPosition + 4;
					endPosition = navigator.appVersion.indexOf (';', startPosition);
					this.version = parseFloat (navigator.appVersion.substring (startPosition, endPosition));
				}
			}
		}
	}
}

// ------------------------------------------------------------------------------------

//	functions to write, read and remove cookies

//	format for creating cookies:
//		document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'
//		document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

function createCookie (name, value, expirationDays) {

	if (expirationDays) {
		var date = new Date();
		date.setTime (date.getTime ()+ (expirationDays * 24 * 60 *60 * 1000));	//	milliseconds
		var expires = "; expires=" + date.toGMTString ();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

//	return the specified cookie's value or "" if it is not found

function readCookie (name) {

	var nameSubstring = name + "=";
	var cookies = document.cookie.split (';');		//	split cookie string into array of cookies
	for (var i = 0; i < cookies.length; i++) {
		var cookie = cookies[i];
		while (cookie.charAt (0) == ' ')
			cookie = cookie.substring (1, cookie.length);
		if (cookie.indexOf (nameSubstring) == 0)
			return cookie.substring (nameSubstring.length, cookie.length);
	}
	return "";
}

//	Set the cookies value to nothing and its expiration in the past.

function eraseCookie(name) {

	createCookie (name, "", -1);
}

//	Determine whether the named cookie exists

function cookieExists (name) {

	if (readCookie (name).length == 0)
		return false;
	else
		return true;
}


// ------------------------------------------------------------------------------------

function show (identity) {

	if (document.getElementById) {
		var element = document.getElementById(identity); 
		element.style.visibility = 'visible';
		element.disabled = false;
    }
}

// ------------------------------------------------------------------------------------

function hide (identity) {

	if (document.getElementById) {
		var element = document.getElementById(identity); 
		element.style.visibility = 'hidden';
    }
}

// ------------------------------------------------------------------------------------

function display (identity) {

	if (document.getElementById) {
		var element = document.getElementById(identity); 
		element.style.display = 'block';
		element.style.visibility = 'visible';
		element.disabled = false;
    }
}

// ------------------------------------------------------------------------------------

function noDisplay (identity) {

	if (document.getElementById) {
		var element = document.getElementById(identity); 
		element.style.visibility = 'hidden';
		element.style.display = 'none';
    }
}

// ------------------------------------------------------------------------------------

function disable (identity) {

	if (document.getElementById) {
		var element = document.getElementById(identity); 
		element.style.visibility = 'hidden';
		element.disabled = true;
    }
}

// ------------------------------------------------------------------------------------

// 	Changes the selected item in a panel list to the currentNode by applying
// 	the selectionClass style to it. Also show the element with identity derived from
//	currentNode's.

//	The currentNode's identity has a leading # character that must be removed. This scheme
//	allows the list item and the controlled element (the one that must be displayed) to 
//	essentially have the same ID. The list item's ID is the controlled elements with a leading
//	#. This allows the currently displayed element to be hidden when the new one is displayed,
//	because its identity can be derived from the list item's that currently has the selectionClass
//	attribute.

function changePanelSelection (currentNode, selectionClass) {

	var	parentList;
	var	node;
	var classAttribute;
	var classValue;
	var i;
	var id;
	
	if (currentNode.nodeName != 'LI') {
		alert ("The current node must be an LI element");
		return;
	}
	

//	Find the parent UL element

	parentList = currentNode.parentNode;

//	Find the LI element that now has the selectionClass attached and remove it.
//	Also derive its controlled element's ID from the list item's ID and hide the
//	controlled element.

	for (i = 0; i < parentList.childNodes.length; i++) {
		node = parentList.childNodes[i];
		if (node.nodeName == 'LI') {
			classAttribute = node.attributes.getNamedItem ("class");
			if (classAttribute && classAttribute.nodeValue.search (selectionClass) != -1) {
				classValue = classAttribute.nodeValue;
				classValue = classValue.replace (selectionClass, "");
				classAttribute.nodeValue = classValue;
				id = node.id.substr (1); 		// extract from 2nd char to end
				noDisplay (id);
			}
		}
	}


//	Attach the selectionClass to the currentNode and show the element with the supplied identity.

	classAttribute = currentNode.attributes.getNamedItem ("class");
	if (classAttribute) {
		classValue = classAttribute.nodeValue;
		if (classValue.length > 0)
			classValue = classValue.concat (" ", selectionClass);
		else 
			classValue = classValue.concat (selectionClass);
		classAttribute.nodeValue = classValue;
	}
	else {
		classAttribute = document.createAttribute ("class")
		classAttribute.nodeValue = selectionClass;
		currentNode.attributes.setNamedItem (classAttribute);
	}
	id = currentNode.id.substr (1); 		// extract from 2nd char to end
	display (id);
}

// ------------------------------------------------------------------------------------

function imageWindow (element, width, height) {

	var newWindow;
	var attributes = "";
	
	if (element.nodeName != "IMG")			// only operate on images
		return;
		
	if (context.mozilla) {
		height 	= element.naturalHeight + 10;
		width 	= element.naturalWidth + 10;
	}
	else {
		height 	= height + 10;
		width	= width + 10;
	}
	attributes = "width=" + width + ", height=" + height +
		", dependent=yes, directories=no, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no";
		
	newWindow = window.open (element.src, "Image", attributes, true);
	return newWindow;
}

// ------------------------------------------------------------------------------------

function alternateImageWindow (src, width, height) {

	var newWindow;
	var attributes = "";
	
//	alert ("In ImageWindow with: " + src);
	
	height 	= height + 10;
	width	= width + 10;
	
	attributes = "width=" + width + ", height=" + height +
		", dependent=yes, directories=no, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no";
		
	newWindow = window.open (src, "Image", attributes, true);
	return newWindow;
}
// ------------------------------------------------------------------------------------

//	returns a US string format of a date x days in the future

function inDaysTime (days) {

	var month = new Array ("January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December");

	var	now = new Date ();
	var milliSecondsPerDay = 86400000;
	var futureDate;
	var stringDate;

	futureDate = new Date (now.getTime () + (days * milliSecondsPerDay));
	stringDate = month[futureDate.getMonth ()] + " " + futureDate.getDate () +
					", " + futureDate.getFullYear ();
	return stringDate;
}

// ------------------------------------------------------------------------------------

//	Determines the browser's language. IE does not implement the language property and 
//	Mozilla does not implement userLanguage.

function browserLanguage () {

	if (navigator.language) {
		return navigator.language.toLowerCase ();
	}
	else {
		return navigator.userLanguage.toLowerCase ();
	}
}
// ------------------------------------------------------------------------------------

//	Switches to the USA region by clearing the region cookie if the home region is en-us
//	and by setting a region cookie to en-US for everyone else.

function goToUSASite () {

	if (browserLanguage () == "en-us") {
		eraseCookie ("region");
	}
	else {
		createCookie ("region", "en-US", 1); 
	}
} 
// ------------------------------------------------------------------------------------
function dump (element) {
	
	var	debugWindow;
	var windowAttributes = "width=600, height=600, left=20, top=20, scrollbars=yes, resizable=yes";
	var	property;
	var script;
	
	debugWindow = window.open ("text\html", "Debug", windowAttributes, true);
	debugWindow.document.writeln ("<html><head><title>", element.nodeName, " Browser</title>");
	debugWindow.document.writeln ("</head>");
	debugWindow.document.writeln ('<body>');
	debugWindow.document.writeln ("<h3>", element.nodeName, "</h3>");
	debugWindow.document.writeln ("<hr />");
	debugWindow.document.writeln ("<ul>");
	
	for (property in element) {
		
		debugWindow.document.write ("<li><b>", property, ": ", "</b>");
		if (typeof (element[property]) == 'object' && element[property] != null) {

			script = "window.opener.dump (";
			script = script.concat (property, ");"); 
			debugWindow.document.write ('<a href="#" onclick="', script, '">');
			debugWindow.document.write (element[property]);
			debugWindow.document.write ("</a>");
		}
		else {
			debugWindow.document.write (element[property]);
		}
		debugWindow.document.writeln ("</li>");
	}
	
	debugWindow.document.writeln ("</ul>");
	debugWindow.document.writeln ("<hr />");
	debugWindow.document.writeln ("</body></html>");
	debugWindow.document.close ();
	return false;
}

// ------------------------------------------------------------------------------------

//	The for (property in element) loop sometimes fails and then no ouput is produced.

function dumpSorted (element) {
	
	var	debugWindow;
	var windowAttributes = "width=600, height=600, left=20, top=20, scrollbars=yes, resizable=yes";
	var	property;
	var properties = new Array ();
	var script;
	var item;
	var i = 0;
	
	debugWindow = window.open ("text\html", "Debug", windowAttributes, true);
	debugWindow.document.writeln ("<html><head><title>", element.nodeName, " Browser</title>");
	debugWindow.document.writeln ("</head>");
	debugWindow.document.writeln ('<body>');
	debugWindow.document.writeln ("<h3>", element.nodeName, "</h3>");
	debugWindow.document.writeln ("<hr />");
	debugWindow.document.writeln ("<ul>");
	
	for (property in element) {
		item = "<li><b>";
		item = item.concat (property, ": ", "</b>");
		if (typeof (element[property]) == 'object' && element[property] != null) {
			script = "window.opener.dump (";
			script = script.concat (property, ");"); 
			item = item.concat ('<a href="#" onclick="', script, '">');
			item = item.concat (element[property]);
			item = item.concat ("</a>");
		}
		else {
			item = item.concat (element[property]);
		}
		item = item.concat ("</li>");
		properties[i++] = item;
	}
	properties.sort ();
	for (i = 0; i < properties.length; i++) {
		debugWindow.document.writeln (properties[i]);
	}
	
	debugWindow.document.writeln ("</ul>");
	debugWindow.document.writeln ("<hr />");
	debugWindow.document.writeln ("</body></html>");
	debugWindow.document.close ();
	return false;	
}