// (c) 1999-2009 Bright Interactive Limited. All rights reserved.
// http://www.bright-interactive.com | info@bright-interactive.com

// Last modified: $Date: 2009-02-18 16:52:42 $
// Release $Name:  $
// Revision $Revision: 1.7 $


// addLoadEvent function as seen at http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// allows you to stack functions and apply them to the onload event and also means you 
// can abstract onload functions from the html
// I've added an argument 'arg' for onload functions with an argument
// Could probably be better implemented with 'arguments[]'

function addLoadEvent(func,arg) {
	
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func(arg);
    }
  }
}


 
/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/
// ---
function $(strId){
	return document.getElementById(strId);
}
// ---
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}
// ---
function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}
// ---
function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
// ---




// simple toggle visibility function for showing and hiding html elements
function toggle_visibility(id) {
   var e = $(id);
   if(e.style.display == 'block')
	  e.style.display = 'none';
   else
	  e.style.display = 'block';
}

//toggle visibility of hide and show links
function toggle_links() {
	var s = $('showLink');
	var h = $('hideLink');
	if(s.style.display == 'block') {
		s.style.display = 'none';
		h.style.display = 'block';
	}
	else {
		s.style.display = 'block';	
		h.style.display = 'none';
	}
}

function url_param(p) {
	// Retrieve an url param specified by p=value
	var re = new RegExp( p + "=([^&]+)" );
	var matches = document.location.href.match(re);
	if (matches) {
		return matches[1];
	}
	else {
		return '';
	}
}

function set_cookie(name, value, expires) {
	// Set a cookie that expires in the specified number of days
	var expiry_date = new Date();
	expiry_date.setTime(expiry_date.getTime()+1000*60*60*24*expires);
	document.cookie = name + "=" + value + "; path=/; expires=" + expiry_date.toGMTString();	
}


// ------------------------------------------------------------------
// Country specific redirects
// There will be a suite of wildlifeextra.com websites (e.g. UK, New Zealand)
// and this code ensures that the right one is reached

function country_redirect() {
	// Check to see if we need to redirect to a country specific site
	// Only do this if the user is arriving from an external site
	// i.e. their referrer doesn't include "wildlife-extra."
	// Add cookie check too, might be useful one day / or for 
	// testing
	
	var our_domain = new RegExp( '^http(s)?://[^/]*wildlife(-)?extra\..*' );
	var test_domain = new RegExp( '^http://wlx.bright.*' );

	if (!document.referrer.match(our_domain) && !document.referrer.match(test_domain)) {
		if (window.location.href.indexOf('#cr') == -1) {
			window.location = '/do/wildlife.py/country_redirect?current_url=' + escape(window.location);
		}
	}
}
// Always call this(!) Cookies and referrer check should
// ensure this works OK
country_redirect();


