// index:
//************************************************************
// 1.  some global variables
// 2.  macromedia rollover functions
// 3.  date string
// 4.  open window function
// 5.  get this page name
// 6.  get query string data
// 7.  navigation functions
// 8.  trim
// 9.  cross browser XMLHttpRequest
// 10. changing a named html element with innerHTML
// 11. manipulations with visibility and display of elements
// 12. locates elements of a certain class
// 13. isEmpty
//************************************************************
// 1. some global variables

var isNav, isIE, isNavSix;
var coll = "";
var styleObj = "";
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") {
		isNav = true;
		if (parseInt(navigator.appVersion) >= 5) {
		isNavSix = true;
		}
	} else {
		isIE = true;
		coll = "all.";
		styleObj = ".style";
	}
}

//************************************************************
// 2. macromedia rollover functions

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//onMouseOut="MM_swapImgRestore();"
//onMouseOver="MM_swapImage('efficacyRcc','','images/nav/efficacyRccOver.gif',1);"


//************************************************************
// 3. date string

var monthNames = new Array ("January",
							"February",
							"March",
							"April",
							"May",
							"June",
							"July",
							"August",
							"September",
							"October",
							"November",
							"December");
var now = new Date();
var amPm = (now.getHours() < 12) ? " AM" : " PM";
var hour = (now.getHours() > 12) ? (now.getHours() - 12) : (hour == 0) ? "12" : now.getHours();
var min = (now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes());
var theYear = (now.getYear() < 1900) ? (now.getYear() + 1900) : now.getYear()
var theDateString = monthNames[now.getMonth()] + " " + now.getDate() + ", " + theYear + " " + hour + ":" + min + amPm;
if (isIE) {
	if (navigator.platform.match("Mac")) {
		theDateString = monthNames[now.getMonth()] + " " + now.getDate() + ", " + theYear;
	}		
}

//************************************************************
// 4. open window function

function openNewWindow(page,name,width,height,top,left,propSet) {

	var windowProps = new Array (8);
	windowProps[0] = "resizable=yes";
	windowProps[1] = "scrollbars=yes";
	windowProps[2] = "titlebar=yes";
	windowProps[3] = "toolbar=yes";
	windowProps[4] = "menubar=yes";
	windowProps[5] = "location=yes";
	windowProps[6] = "status=yes";
	windowProps[7] = "directories=yes";
	
	var myProps = "";
	var mySize = "";
	
	if (propSet == 'one') {
		 myProps = ',' + windowProps[0] + ',' + windowProps[1];
	} else if (propSet == "full") {
		myProps = ',' + windowProps.join(",");
	} else {
		myProps = "";
	}	
	
	if ((width > 50)||(height > 50)) {
		var mySize = 'width=' + width + ',' + 'height=' + height + ',' + 'top=' + top + ',' + 'left=' + left;
	}
	
	var myString = mySize + myProps;
	window.open(page,name,myString);
	
}

function popGlossary(theUrl) {
	openNewWindow(theUrl,"gloss",484,289,0,0,"none");
}

//************************************************************
// 5. get this page name

var parts = new Array;
var thisSite = new Array;
var test;

//check the current page
test = location.href.substring(location.href.lastIndexOf("/"));
parts = test.split('?');
thisSite.thisPage = parts[0].replace("/","");

var pageName = thisSite.thisPage;

//************************************************************
// 6. get query string data
// qs is query string to parse

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get;
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

// test for common query strings in url
// flash
var qs = new Querystring();
var qsFlash = qs.get("flash");
var isFlash = true;

if (qsFlash == "false" || qsFlash == "FALSE") {
	isFlash = false;
} else {
	isFlash = true;
}

// printable version
var qs = new Querystring();
var qsPrintable = qs.get("print");
var qs = new Querystring();
var qsPrintable2 = qs.get("PrinterFriendly");
var isPrinter = false;

if (qsPrintable == "true" || qsPrintable == "TRUE" || qsPrintable2 == "true" || qsPrintable2 == "TRUE") {
	isPrinter = true;
} else {
	isPrinter = false;
}

//************************************************************
// 7. navigation functions

function theGo(thePlace) {
	window.location = thePlace;
}

function goBack() {
	window.history.back(-1);
}

function returnTo() {
	if (document.referrer != "") {
		window.location.href = document.referrer;
	}
}

//***************************************************************
// 8. trim

function trim(strToTrim) { 
	// leading spaces 
	while (strToTrim.substring(0,1) == ' ') {
		strToTrim = strToTrim.substring(1, strToTrim.length);
	}
	
	// trailing spaces 
	while (strToTrim.substring(strToTrim.length-1,strToTrim.length) == ' ') {
		strToTrim = strToTrim.substring(0, strToTrim.length-1);
	}
	return strToTrim;
}

//************************************************************
// 9. cross browser XMLHttpRequest

function getHTTPObject() {
	var objType = false;
	if (window.XMLHttpRequest) {
		objType = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
      objType = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        objType = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        objType = false;
      }
    }
  }
  return objType;
}

// read an xml file from the server
function grabFile(file) {
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
		parseResponse(request);
	};
	request.open("GET", file, true);
    request.send(null);
  }
}

// parse the response
function parseResponse(request) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var data = request.responseXML;
			//var data = request.responseText
			return data;
		}
	}
}

function parseData(which) {
	myData = data.getElementsByTagName(which)[0].firstChild.nodeValue;
	return myData;
}


//************************************************************
// 10. changing a named html element with innerHTML

// empty an element
function clearOut(anElement) {
	var myElement = document.getElementById(anElement);
	myElement.innerHTML = "";
}

// add to an element
function addTo(anElement, addThis) {
	var myElement = document.getElementById(anElement);
	var startHere = myElement.innerHTML
	myElement.innerHTML = startHere + addThis;
}

// replace an element
function changeTo(anElement, newStuff) {
	var myElement = document.getElementById(anElement);
	myElement.innerHTML = newStuff;
}

//************************************************************
// 11. manipulations with visibility and display of elements
// convert object name string or object reference
// into a valid object reference, and perfom

function getObject2(obj) {
	theObj = document.getElementById(obj).style;
	return theObj;
}

// Setting the visibility of an object to visible
function show(obj) {
	var theObj = getObject2(obj);
	theObj.visibility = "visible";
}

// Setting the visibility of an object to hidden
function hide(obj) {
	var theObj = getObject2(obj);
	theObj.visibility = "hidden";
}

// Setting the display of an object to block
function display(obj) {
	var theObj = getObject2(obj);
	theObj.display = "block";
}

// Setting the display of an object to none
function unDisplay(obj) {
	var theObj = getObject2(obj);
	theObj.display = "none";
}

// toggle display of an object
function toggleDisplay(obj) {
	var theObj = getObject2(obj);
	if (theObj.display == "none") {
		theObj.display = "block";
	} else {
		theObj.display = "none";
	}
}

//************************************************************
// 12. locates elements of a certain class
// strClass:
// string containing the class(es) that you are looking for
// strTag (optional, defaults to '*'):
// An optional tag name to narrow the search to specific tags e.g. 'a' for links.
// objContElm (optional, defaults to document):
// An optional object container to search inside. Again this narrows the scope of the search

function getElementsByClassName(strClass, strTag, objContElm) {
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = objContElm.getElementsByTagName(strTag);
	if (!objColl.length && strTag == "*" && objContElm.all) objColl = objContElm.all;
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1 ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (var i = 0, j = objColl.length; i < j; i++) {
		var arrObjClass = objColl[i].className.split(' ');
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:
		for (var k = 0, l = arrObjClass.length; k < l; k++) {
			for (var m = 0, n = arrClass.length; m < n; m++) {
				if (arrClass[m] == arrObjClass[k]) c++;
				if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}

// to cover Mac IE 5.0's lack of the push method
// but this may interfere with some ajax scripts
//Array.prototype.push = function(value) {
//	this[this.length] = value;
//}

/*
//usage examples:
//all elements of class "one"
var myObjColl = getElementsByClassName('one');
for (var i = 0, j = myObjColl.length; i < j; i++) {
   // Do your thing here.
}

//all links ('a') of class "one" in container id "container"
var cont = document.getElementById('container');

var myObjColl = getElementsByClassName('one', 'a', cont);
for (var i = 0, j = myObjColl.length; i < j; i++) {
   // Do your thing here.
}

//all spans of classes "one" OR "two" in container id "container"
var cont = document.getElementById('container');

var myObjColl = getElementsByClassName('one|two', 'span', cont);
for (var i = 0, j = myObjColl.length; i < j; i++) {
   // Do your thing here.
}

//all spans of classes "one" AND "two" in container id "container"
var cont = document.getElementById('container');

var myObjColl = getElementsByClassName('one two', 'span', cont);
for (var i = 0, j = myObjColl.length; i < j; i++) {
   // Do your thing here.
}


// now loop through the results and do something
var c = getElementsByClassName('blah');

for(var i=0;j=c.length; i<j; i++){
  c[i].style.display = 'none';
}
*/

//************************************************************
// 13. isEmpty
// Determine whether a variable is considered to be empty based on its type
// types = number, string, boolean, object, function, undefined
// 
//	Returns FALSE if var has a non-empty and non-zero value:
//	"" (an empty string)
//	null (an empty string)
//	0 (an empty number)
//	false (an empty boolean)
//	0 (an empty value length of an object)
//	null (an empty value of an object)
//	undefined (an empty object type)

function isEmpty(variable) {
	if(typeof variable == "number"){
		if (variable == 0){
			return true;
		}
	}
	else if (typeof variable == "string"){
		if(variable == null || variable == "" || variable.length <= 0){
			return true;
		}
	}
	else if (typeof variable == "boolean"){
		if (variable == false){
			return true;
		}
	}
	else if (typeof variable == "object"){
		if (variable.value.length == 0 || variable.value == null){
			return true;
		}
		else if(variable.type == "select-one")
		{
			if(variable.value.toLowerCase().indexOf('--') > 0)
			{
				return true;
			}
			if(variable.value.toLowerCase().indexOf("please select") > 0)
			{
				return true;
			}
			if(variable.value.toLowerCase().indexOf("select one") > 0)
			{
				return true;
			}
		}	
	}
	else if (typeof variable == "function"){
		return false;
	}
	else if (typeof variable == "undefined"){
		return true;
	}
	else if (typeof variable == "null"){
		return true;
	}
	return false;
}

//************************************************************

function makePrinter() {
	unDisplay("headerArea");
	unDisplay("fb1");
}
