// ## Show/hide links, this make the "show me more" type links work and slides the extra info into view ##


function MakePopUps( eles ) {
	// Add pop ups to any links with a type set to "popup"
	eles.forEach( function( link ) {
		if ( link.getAttribute('type') == 'popup' ) {
			link.addEvent( 'click', function() { return popup( this.href ) } );
			link.title += ' (Popup)';
		}
	});
}

window.addEvent('domready', function() {
	MakePopUps( $( document.body ).getElements( 'a' ) );
});

function slide( link, ele_id ) {
	if ( $(ele_id ) ) {
		if ( link ) {
			if ( String( link.innerHTML ).toLowerCase().indexOf( 'hide' ) > -1 ) {
				sliders.get( ele_id ).slideOut();
				link.innerHTML = link.innerHTML.replace( 'hide', 'show' ).replace( 'Hide', 'Show' );
			} else {
				sliders.get( ele_id ).slideIn();
				link.innerHTML = link.innerHTML.replace( 'show', 'hide' ).replace( 'Show', 'Hide' );
			}
		}
		else
		{
			sliders.get( ele_id ).slideIn();
		}
	}

	return false;
}

var sliders = new Hash();

// Prep any elements with a class of "slider" to use the Slide effect
var slider_rules = {
	'.slider' : function( element ) {
		var elem = $( element.id ).setStyle( 'display', 'block' );
		var slider = new Fx.Slide( element.id, { mode : 'vertical' } ).slideOut();
		sliders.set( element.id, slider );
	}
};

if ( $defined( Behaviour ) ) {
	Behaviour.register( slider_rules );
}


// ## Other bits and bobs ##

function popup( url, name, options ) {
	name = name ? name : "unknown";
	options = options ? options : {};
	var width = options['width'] ? options['width'] :800;
	var height = options['height'] ? options['height'] : 500;
	newwindow = window.open( url, name, 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=' + width + ',height=' + height );
	if ( window.focus ) { newwindow.focus() }
	return false;
}

// Form Helpers

function selected_value( ele )
{
	return ele.options[ ele.selectedIndex ].value;
}

function isVoid(p_strVal) {

	return (typeof(p_strVal) == 'undefined' || p_strVal == null || p_strVal == "" || String(p_strVal) == "" || String(p_strVal) == "undefined" || String( p_strVal) == "null" );
}

// Debug function - Render out the values of the form into an alert IF the user has requested so on the query string (?debug=xxx)
function check_for_debug_info( frm )
{
	if ( location.search.length == 0 || location.search.indexOf( "debug=" ) == -1 )
	{
		return;
	}

	frm = frm || ( document.forms.length > 0 ? document.forms[0] : null );

	// If user has passed in the debug flag on the query string then write some goodies out
	if ( frm )
	{
		eleArray = new Array();

		for( var i=0; i < frm.elements.length; i++ )
		{
			var ele = frm.elements[i];
			eleArray.push( ele.name + ": \t" + ele.value );
		}

		alert( "Debug information for form: " + frm.id + ":\n\n" + eleArray.join( "\n\t" ) );
	}
}

// TODO: Phase these out
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];}}
}

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_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_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function display(spanblock,condition,thisvalue){
	if (condition == "" || condition == "&nbsp;" || condition == thisvalue)
	{ MM_findObj(spanblock).className="hidden" }
}


function printformaton(){
	document.body.className = "brandPRINT";
}

function printformatoff(){
	document.body.className = "";
}


function setStyle(obj, style, value) {
	if (typeof obj == "string") { obj=MM_findObj(obj); }
	if (obj != null) {
		 obj.style[style] = value;
	}
}

function updateheader() {
	// Depreciated
}


// Adds the ref=someidentifier name value pair to the end of all hrefs for links, ignores javascript links
// ref_tag : the identifier to credit the link with
function addReferrerTag( ref_value, ref_tag )
{
	if ( !document.getElementsByTagName )
	{
		return;
	}

	ref_tag = ref_tag ? ref_tag : "ref";

	// Get all the links on the page and add the ?ref=[ref_tag] to the end of them
	links = document.getElementsByTagName( "a" );

	var link_total = links.length;

	for ( var i=0; i < link_total; i++ )
	{
		var link = links[i];
		if ( link.href.indexOf( ref_tag + "=" ) == -1 && link.href.indexOf( "javascript" ) == -1 && link.href.indexOf( "#" ) < 1 )
		{
			if ( link.href.indexOf( "?" ) > -1 )
			{
				link.href += "&" + ref_tag + "=" + ref_value;
			}
			else
			{
				link.href += "?" + ref_tag + "=" + ref_value;
			}
		}
	}
}


//////////////////////////////////////////////////////////////////////////////
// Generic cookie functions
//////////////////////////////////////////////////////////////////////////////
function setCookie(name, value, expires, path, domain ) {

	var thisCookie = new String(escape(name) + '=' + escape(value)),
		days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

	if (expires) {
		expires = new Date(expires);
		thisCookie += ';EXPIRES=' +
						days[expires.getUTCDay()] + ', ' +
						(expires.getUTCDate() < 10 ? '0' : '') + expires.getUTCDate() + '-' +
						months[expires.getUTCMonth()] + '-' +
						String(expires.getUTCFullYear()).substring(1) + ' ' +
						(expires.getUTCHours() < 10 ? '0' : '') + expires.getUTCHours() + ':' +
						(expires.getUTCMinutes() < 10 ? '0' : '') + expires.getUTCMinutes() + ':' +
						(expires.getUTCSeconds() < 10 ? '0' : '') + expires.getUTCSeconds() +
						' GMT';
	}

	if (path) {
		thisCookie += ';PATH=' + path;
	}

	if (domain) {
		thisCookie += ';DOMAIN=' + domain;
	}

	document.cookie = thisCookie;
}


function getCookie(name) {

	var c = new String(document.cookie).split(/; /),
		p;

	for (var i = 0; i < c.length; i++) {
		p = c[i].split('=');
		if (p[0] == escape(name)) return unescape(String(p[1]).replace(/\+/g, ' '));
	}
	return '';
}


function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


function AssertJSLoadedOk() {
	return true;
}