function toggle_display(elementID) {
        var e = document.getElementById(elementID);
        if(e.style.display == "block") {
                e.style.display='none';
        }
        else {
                e.style.display='block';
        }
}

function show(element) {
	element.style.display = 'block';
}

function hide(element) {
	element.style.display = 'none';
}

function getURL(token) {
	if (!token || token == '') {
		return location.href;
	} else {
		var url = location.href;
		if (url.indexOf(token) == -1) {
			return url;
		} else {
			return url.substring(0, url.indexOf(token));
		}
	}
	
}

popupWins = new Array();

function popUp(url, w, h, name) {
	if (!name || name == '') {
		name = 'remote';
	}
	
	// Center popup in the middle of the screen
	var leftPos = (screen.width) ? (screen.width-w)/2 : 0;
	var topPos = (screen.height) ? (screen.height-h)/2 : 0;
	
	var windowprops = "width=" + w + ",height=" + h + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=" + topPos + ",left=" + leftPos + "";
	
	/*************************************************
	the popupWins array stores an object reference for
	each separate window that is called, based upon
	the name attribute that is supplied as an argument
	*************************************************/
	if ( typeof( popupWins[name] ) != "object" ) {
		popupWins[name] = window.open(url, name, windowprops);
	} else {
		if ( !popupWins[name].closed ) {
			popupWins[name].location.href = url;
			// Firefox uses the width and height to change the innerWidth and innerHeight
			// in the resizeTo() function.  These same values were used to create the window
			// with outerWidth and outerHeight, so we need to adjust the values.
			w += 6;
			h += 54;
			// end adjustment
			popupWins[name].resizeTo(w, h);
			popupWins[name].moveTo(leftPos, topPos);
		} else {
			popupWins[name] = window.open(url, name, windowprops);
		}
	}
	popupWins[name].focus();
}

function popUp2(url, w, h, name) {
	if (!name || name == '') {
		name = 'remote';
	}
	
	// Center popup in the middle of the screen
	var leftPos = (screen.width) ? (screen.width-w)/2 : 0;
	var topPos = (screen.height) ? (screen.height-h)/2 : 0;
	
	var windowprops = "width=" + w + ",height=" + h + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=" + topPos + ",left=" + leftPos + "";
	
	/*************************************************
	the popupWins array stores an object reference for
	each separate window that is called, based upon
	the name attribute that is supplied as an argument
	*************************************************/
	if ( typeof( popupWins[name] ) != "object" ) {
		popupWins[name] = window.open(url, name, windowprops);
	} else {
		if ( !popupWins[name].closed ) {
			popupWins[name].location.href = url;
			// Firefox uses the width and height to change the innerWidth and innerHeight
			// in the resizeTo() function.  These same values were used to create the window
			// with outerWidth and outerHeight, so we need to adjust the values.
			w += 6;
			h += 54;
			// end adjustment
			popupWins[name].resizeTo(w, h);
			popupWins[name].moveTo(leftPos, topPos);
		} else {
			popupWins[name] = window.open(url, name, windowprops);
		}
	}
	popupWins[name].focus();
}

function popUpWBars(url, w, h, name) {
	if (!name || name == '') {
		name = 'remote';
	}
	
	// Center popup in the middle of the screen
	var leftPos = (screen.width) ? (screen.width-w)/2 : 0;
	var topPos = (screen.height) ? (screen.height-h)/2 : 0;
	
	var windowprops = "width=" + w + ",height=" + h + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=" + topPos + ",left=" + leftPos + "";
	
	/*************************************************
	the popupWins array stores an object reference for
	each separate window that is called, based upon
	the name attribute that is supplied as an argument
	*************************************************/
	if ( typeof( popupWins[name] ) != "object" ) {
		popupWins[name] = window.open(url, name, windowprops);
	} else {
		if ( !popupWins[name].closed ) {
			popupWins[name].location.href = url;
			// Firefox uses the width and height to change the innerWidth and innerHeight
			// in the resizeTo() function.  These same values were used to create the window
			// with outerWidth and outerHeight, so we need to adjust the values.
			w += 6;
			h += 54;
			// end adjustment
			popupWins[name].resizeTo(w, h);
			popupWins[name].moveTo(leftPos, topPos);
		} else {
			popupWins[name] = window.open(url, name, windowprops);
		}
	}
	popupWins[name].focus();
}

/*
	Cookie functions
*/
// Set the cookie
function setCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString(); 
	} else {
		expires = "";
	}
	//CHANGED BY PEGGY 2008-10-12 added this line which effectively deletes the
	//cookie by setting its expiration to Jan 1, 1970, the year 0 for computers.
	document.cookie = name+"="+value+"; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT;";
	//CHANGED BY PEGGY 2008-09-15 from document.cookie = name+"="+value+expires+";";
	document.cookie = name+"="+value+"; path=/"+expires+";";
}

// Read the cookie
function readCookie(name) {
	var needle = name + "=";
	var cookieArray = document.cookie.split(';');
	for(var i=0;i < cookieArray.length;i++) {
		var pair = cookieArray[i];
		while (pair.charAt(0)==' ') {
			pair = pair.substring(1, pair.length);
		}
		if (pair.indexOf(needle) == 0) {
			return pair.substring(needle.length, pair.length);
		}
	}
	return null;
}


/*
	Text Resizing functions
*/
function displayTextSizer(wrapper) {
	// Using behaviour.js defined function
	var wrapping_container = document.getElementsBySelector(wrapper);
	
	if (wrapping_container && wrapping_container[0]) {
		// Build elements
		var slugs = new Array("small", "medium", "large");
		var controlContainer = document.createElement("p");
		var labelText = document.createTextNode("TEXT SIZE: ");
		var separator = " | ";
		
		controlContainer.appendChild(labelText);
		
		// Loop over the text size "slugs", and build a link for each one
		for (var i = 0; i < slugs.length; i++) {
			var text = document.createTextNode("A");
			var anchor = document.createElement("a");
			var item = document.createElement("span");
			
			anchor.appendChild(text);
			anchor.setAttribute("href", "javascript:;");
			anchor.setAttribute("title", "Make the story text " + slugs[i] + ".");
			item.appendChild(anchor);
			item.setAttribute("class", "" + ((slugs[i] == 'medium') ? 'med' : slugs[i]));
			
			if (i > 0) {
				controlContainer.appendChild(document.createTextNode(separator));
			}
			controlContainer.appendChild(item);
			
			if (document.all) { // IE only
			controlContainer.innerHTML = 'TEXT SIZE: <span class="small"><a title="Make the story text small." href="javascript:;">A</a></span> | <span class="med"><a title="Make the story text medium." href="javascript:;">A</a></span> | <span class="large"><a title="Make the story text large." href="javascript:;">A</a></span>';
			}
		}
		controlContainer.setAttribute("id", "textSize");
		//CHANGED BY PEGGY 2008-09-15wrapping_container[0].insertBefore(controlContainer, wrapping_container[0].lastChild);
		wrapping_container[0].insertBefore(controlContainer, wrapping_container[0].firstChild);
	}
}

function resizeText(size, wrapper) {
	var sizer = 'textSize';
	var item = 'span';
	var current = 'selected';
	// Using behaviour.js defined function
	var wrapping_container = document.getElementsBySelector(wrapper);
	var sizing_container = document.getElementById(sizer);
	if (wrapping_container && wrapping_container[0] && sizing_container) {
		//wrapping_container[0].className = ''; // reset first
		wrapping_container[0].removeAttribute('class'); // reset first
		addClassName(wrapping_container[0], size, true);
		
		if (document.all) { // IE only
			wrapping_container[0].removeAttribute('style'); // reset first
			if (size == 'small') {
				wrapping_container[0].style.fontSize = '11px';
				wrapping_container[0].style.lineHeight = '16px';
			} else if (size == 'med') {
				wrapping_container[0].style.fontSize = '14px';
				wrapping_container[0].style.lineHeight = '20px';
			} else if (size == 'large') {
				wrapping_container[0].style.fontSize = '16px';
				wrapping_container[0].style.lineHeight = '22px';
			}
		}
		
		var listItems = sizing_container.getElementsByTagName(item);
		for (var i = 0; i < listItems.length; i++) {
			if (hasClassName(listItems[i], size)) {
				addClassName(listItems[i], current, true);
				setCookie(sizer, size, 365);
			} else {
				removeClassName(listItems[i], current);
			}
		}
	}
}

// http://snippets.dzone.com/tag/add

// ----------------------------------------------------------------------------
// HasClassName
//
// Description : returns boolean indicating whether the object has the class name
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function hasClassName(objElement, strClass) {
	var class_name = objElement.getAttribute('class');
	if (!class_name) {
		class_name = objElement.className;
	}
	// if there is a class
	if ( class_name ) {
		// the classes are just a space separated list, so first get the list
		var arrList = class_name.split(' ');
		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();
		// find all instances and remove them
		for ( var i = 0; i < arrList.length; i++ ) {
			// if class found
			if ( arrList[i].toUpperCase() == strClassUpper ) {
				// we found it
				return true;
			}
		}
	}
	// if we got here then the class name is not there
	return false;
}
// 
// HasClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// AddClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function addClassName(objElement, strClass, blnMayAlreadyExist) {
	var class_name = objElement.getAttribute('class');
	if (!class_name) {
		class_name = objElement.className;
	}
	// if there is a class
	if ( class_name )	{
		// the classes are just a space separated list, so first get the list
		var arrList = class_name.split(' ');
		// if the new class name may already exist in list
		if ( blnMayAlreadyExist ) {
			// get uppercase class for comparison purposes
			var strClassUpper = strClass.toUpperCase();
			// find all instances and remove them
			for ( var i = 0; i < arrList.length; i++ ) {
				// if class found
				if ( arrList[i].toUpperCase() == strClassUpper ) {
					// remove array item
					arrList.splice(i, 1);
					// decrement loop counter as we have adjusted the array's contents
					i--;
				}
			}
		}
		// add the new class to end of list
		arrList[arrList.length] = strClass;
		// add the new class to beginning of list
		//arrList.splice(0, 0, strClass);
		// assign modified class name attribute
		objElement.setAttribute('class', arrList.join(' '));
	}
	// if there was no class
	else {
		// assign modified class name attribute
		objElement.setAttribute('class', strClass);
	}
}
// 
// AddClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// RemoveClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to remove
//
function removeClassName(objElement, strClass) {
	var class_name = objElement.getAttribute('class');
	if (!class_name) {
		class_name = objElement.className;
	}
	// if there is a class
	if ( class_name ) {
		// the classes are just a space separated list, so first get the list
		var arrList = class_name.split(' ');
		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();
		// find all instances and remove them
		for ( var i = 0; i < arrList.length; i++ ) {
			// if class found
			if ( arrList[i].toUpperCase() == strClassUpper ) {
				// remove array item
				arrList.splice(i, 1);
				// decrement loop counter as we have adjusted the array's contents
				i--;
			}
		}
		// assign modified class name attribute
		objElement.setAttribute('class', arrList.join(' '));
	}
	// if there was no class
	// there is nothing to remove
}
// 
// RemoveClassName
// ----------------------------------------------------------------------------