// JavaScript Document
// Filename:	ims.js
// Author:		Carolyn Templeton
// Created:		March 24, 2006
// Purpose:		This JavaScript document contains the JavaScript code for the IMS site. 
// According to SLAC best practices, some standard page components (such as the header and footer)
// contain JavaScript, and are embedded in the page. 

/* begin Page Initialization master function ********************************************************************/
// Author:		Carolyn Templeton
// Created:		May 15, 2006
// Purpose:		This code allows each page to call one function, that then calls multiple individual highlighting functions
function initPage(CurrentPage) {
	var CurrentSection = CurrentPage.substring(0,2);
	highlightCurrentPageInNavBar (CurrentPage,CurrentSection);
	var CurrentResources = CurrentPage.substring(0,1) + 'R';
	highlightCurrentPageInResourceBar (CurrentResources);
	openAllLIInGroup(CurrentResources);
}
/* end Page Initialization master function ********************************************************************/

/* begin Current Page Indicator code ********************************************************************/
// Author:		Carolyn Templeton
// Created:		March 24, 2006
// Purpose:		This code allows 2 fields to be set in a page header that are then used to indicate the page and section in the nav bar display
function highlightCurrentPageInNavBar (CurrentPage,CurrentSection) {
/*	highlightCurrentPageInNavBar applies the  formatting required to indicate
	in the left navigation which page and section you are currently viewing
	it takes two parameters:
		CurrentPage - identifies which page you are currently viewing
		CurrentSection - indicates which section that page belongs to	*/
	var CurrentPageImg = CurrentPage + '_img';				// all bullets associated with a page in the nav bar should be named <<CurrentPage>>_img
	var img = document.getElementById(CurrentPageImg);		// for readability, the image is given a handle
		if (CurrentPage == CurrentSection) {				// if you are viewing the section's main page then
		var CurrentSectionHeader = CurrentSection + '_header';		// all section headers should be class <<CurrentSection>>_header
		var obj = document.getElementById(CurrentSectionHeader);	// for readability, the page is given a handle
		img.src="images/HomeSvcNavRedArrow.gif";					// change the bullet from unselected to selected
	} else {												// else you are viewing a subpage
		var obj = document.getElementById(CurrentPage);		// for readability, the page is given a handle
		img.style.visibility = 'visible';					// set the bullet to visible
	}
	obj.className = obj.className + '_Selected';	// change the class of the selected page to apply formatting
													// all bullets associated with a page in the nav bar should be named <<CurrentPage>>_img
	var section = document.getElementById(CurrentSection);	// for readability, the section is given a handle
	section.className = section.className + '_Selected';	// hange the class of the selected page to apply formatting
}
/* end Current Page Indicator code ********************************************************************/

/* begin Current Page Indicator code (resource area) **************************************************/
// Author:		Carolyn Templeton
// Created:		March 24, 2006
// Purpose:		This code allows 2 fields to be set in a page header that are then used to indicate the page and section in the nav bar display
function highlightCurrentPageInResourceBar (CurrentPage) {
/*	highlightCurrentPageInResourceBar applies the  formatting required to indicate
	in the resource area which page or section you are currently viewing it takes one parameter:
		CurrentPage - identifies which page you are currently viewing 
*/
	var obj = document.getElementById(CurrentPage);	// for readability, the page is given a handle
	obj.style.color = '#990000';	// change the attributes of the selected page to apply formatting
}
/* end Current Page Indicator code (resource area) *********************************************************/

/* begin Flyout Menu code **********************************************************************************/
/*	sfHover */
/*	author:			Jimmy Pham
	modified by:	Cari Templeton, SLAC InfoMedia, 30 Mar 2006 
	purpose:		This function provides the :hover functionality into IE, which doesn't have it yet.
*/
sfHover = function() {
	// For the following List IDs... 
	var flyoutmenus = new Array ("DS_flyout", 
								 "VM_flyout",
								 "WS_flyout",
								 "CS_flyout",
								 "PS_flyout");
	// curse through the IDs, gathering the LIs
	for (var j=0; j < flyoutmenus.length; j++) {
		var sfEls = document.getElementById(flyoutmenus[j]).getElementsByTagName("LI");
		for (var i=0; i < sfEls.length; i++) {	// foreach LI...
			sfEls[i].onmouseover=function() {	// attach the hover functionality to the onmouseover event
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {	// attach the hover functionality to the onmouseout event
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

/* autoloader for the sfHover function */
if (window.attachEvent) window.attachEvent("onload", sfHover);

/* end Flyout Menu code ************************************************************************************/


/* begin checkItem code **********************************************************************************/
/*	checkItem */
/*	author:	Cari Templeton, SLAC InfoMedia, 15 May 2006 
	purpose:This function auto-checks checkboxes identified in the query string.
*/
function checkItem () {
	var qs = new ParsedQueryString();
	var item = document.getElementById(qs.param('checkedItem'));
	if (item) {
		item.defaultChecked=true;
		item.checked=true;
	}
}
/* end checkItem code ************************************************************************************/

/* begin query string code **********************************************************************************/
/*	author:	Cari Templeton, SLAC InfoMedia, 08 June 2006 
	purpose:This function parses the query string
*/

/* NEW QUERY STRING CODE *************************/
String.prototype.decodeURL = function() { return unescape(this.replace(/\+/g, " ")); }

function ParsedQueryString()
{
    this.parameter = {};
    var parts = window.location.search.substr(1).split(/[&;]/);
    var keys = new Array;
    for (var i in parts) {
        var pair = parts[i].split(/=/);
        var name = pair[0].decodeURL();
        var value = pair[1] != undefined ? pair[1].decodeURL() : undefined;
        if (this.parameter[name] == undefined) {
            this.parameter[name] = [value];
			keys[keys.length]=name;
        } else
            this.parameter[name].push(value);
    }
    
    this.param = function(name) {
        return this.parameter[name] != undefined ? this.parameter[name][0] : undefined;
    }
    
    this.params = function(name) {
        if (arguments.length)
            return this.parameter[name] != undefined ? this.parameter[name] : null;
        else {
            var pnames = [];
            for (var p in this.parameter)
                pnames.push(p);
            return pnames;
        }
    }
    this.getKeys = function () {
    	return keys;
    }
}

/* USAGE
 * var qs = new ParsedQueryString();
 *
 * qs.param( name )
 * Returns the string value associated with name (name is case sensitive). If
 * name was not defined in the query string, returns undefined. If multiple
 * values were associated with name, returns the first value from the order it
 * occurs in the query string.
 *
 * e.g., QUERY-STRING: "hello=world&hello=hi&foo=bar"
 * qs.param('hello') returns "world"
 * qs.param('foo') returns "bar"
 * qs.param('not there') returns undefined
 */
 
/*
 * qs.params( [ name ] )
 * Returns an array for all values associated with name (name is case
 * sensitive). If name was not defined in the query string, returns null. If
 * name is omitted, returns an array of every name in the query string.
 *
 * e.g., QUERY-STRING: "hello=world&hello=hi&foo=bar"
 * qs.params('hello') returns ["world", "hi"]
 * qs.params('foo') returns ["bar"]
 * qs.params('not there') returns null
 * qs.params() returns ["hello", "foo"]
 */

/************************ OLD QUERY STRING CODE 
/*	qsParm 
/* from http://javascript.about.com/library/blqs.htm 
var qsParm = new Array();
function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
		      var key = parms[i].substring(0,pos);
		      var val = parms[i].substring(pos+1);
		      qsParm[key] = val;
		}
	}
}
*/

/* end query string code ************************************************************************************/

/* begin Service Header code **********************************************************************************/
/*	author:	Cari Templeton, SLAC InfoMedia, 16 May 2006 
	purpose:This pair of functions adds highlighting formatting to the non-flyout headers, and returns it to normal
*/
/*	highlightServiceHeader */
function highlightServiceHeader(obj) {
	obj.className=obj.className + '_hover';
}

/*	revertServiceHeader*/
function revertServiceHeader(obj) {
	var lastIndex = obj.className.lastIndexOf('_');
	obj.className=obj.className.substring(0,lastIndex);
}
/* end Service Header code ************************************************************************************/

/* begin pass along code **********************************************************************************/
/*	author:	Cari Templeton, SLAC InfoMedia, 17 May 2006 
	purpose:This function passes info along via pasting query string onto the link of an intermediate page
*/
function passalongQueryString(id) {
	var query = window.location.search.substring(1);
	var form = document.getElementsByTagName('form');
	var links = form[1].getElementsByTagName('a');
	for (var i=0;i<links.length;i++) {
		links[i].href = links[i].href +  '?' + query;
	}
}

/* end pass along code ************************************************************************************/


/* begin more info code **********************************************************************************/
/*	author:	Cari Templeton, SLAC InfoMedia, 17 May 2006 
	purpose:This pair of functions shows or hides a div based on id
*/
function show(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display='block'; return '';
	} else { return 'error in show';}
}
function hide(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display='none'; return '';
	} else { return 'error in hide';}
}
/* end more info code ************************************************************************************/

