// JavaScript Document
// quick browser tests
var ns4 = (document.layers) ? true : false;
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

//alert("ns4 ["+ns4+"] ie4 ["+ie4+"] ie5 ["+ie5+"] ns6 ["+ns6+"]")

//set global IP Address
var ip = '<!--#echo var="REMOTE_ADDR"-->';

//global variables
var showhidefooter;
var linktarget;
var cookieName = "sitevars";

//
// function to hide and area
// IE only
function hide(id) {
	if (ie4 || ie5) document.all[id].style.visibility = 'hidden';
	if (ns4) document.layers[id].visibility = 'hidden';
	if (ns6) document.getElementById([id]).style.display = "none";
}

//
// function to show a div area
// IE only
function show(id) {
	if (ie4 || ie5) document.all[id].style.visibility = 'visible';
	if (ns4) document.layers[id].visibility = 'visible';
	if (ns6) document.getElementById([id]).style.display = "block";
}

//
// function to switch visability and button lable
// called from switch and init
//
function searchAndFooter(button) {
	if (showhidefooter == "true") {
		button.value="Hide";
		show("footer");
		show("search");	
	} else {
		button.value="Show";
		hide("footer");
		hide("search");
	}
}

//
// function to hide the footer bar
//
function switchSearchAndFooter(button) {
	//switch the global variable and run searchAndFooter()
	if (showhidefooter == "true") {
		showhidefooter = "false";
	} else {
		showhidefooter = "true";
	}
	searchAndFooter(button);
	//save cookie
	set_cookie(cookieName);
}

//
// function to switch global variable
// called from switch and init
function linkTarget(button) {
	if (linktarget == "_self") {
		button.value="This Window";
	} else {
		button.value="New Window";
	}
}

//
// function to switch link target
//
function switchLinkTarget(button) {
	//switch the global variable
	if (linktarget == "_self") {
		linktarget = "_blank";
	} else {
		linktarget = "_self";
	}
	linkTarget(button);
	//save cookie
	set_cookie(cookieName);
}

//
// function to set the target link on all href links
//
function setLinkTarget(href){
	href.target=linktarget;
}

//
// initialisation funtion
//
function init()
{
	//preload main images
	 preloadImages()
	
	//check to see if cookie can reset values
	 get_cookie(cookieName);
	 searchAndFooter(document.forms[0].showhidefooterbtn);
	 linkTarget(document.forms[0].targetwindow);
	 
	 //initialise nav dropdowns
	  setUpIENav();
}


function set_cookie(name){
	//creating expiration date
	var now = new Date();
	now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 30)

	//create string of data
	var data = showhidefooter +"!"+ linktarget;
	document.cookie = name+ "=" + escape(data) + ";expires=" + now;
}

//
// Gets the cookies and sets the fields that were set in the cookie
//
function get_cookie(name){
	if (document.cookie && document.cookie != ""){
		var cookie_data = unescape(document.cookie);
		//strip out the data part
	    var index = cookie_data.indexOf(name + "=");
		//check that we have cookie values set up
	    if (index == -1) {
			showhidefooter = "false";
			linktarget = "_self";
	    }
		var start = index + name.length + 1;
	    var end = cookie_data.indexOf(";",start);
	    if (end == -1) end = cookie_data.length;
    	var data = cookie_data.substring(start, end);
    	//now split up the data
    	var breaker = data.indexOf("!");
    	showhidefooter = data.substring (0, breaker);
    	linktarget = data.substring (breaker + 1, data.length);
	} else {
		//we do not have a cookie so default values
		showhidefooter = "false";
		linktarget = "_self";
	}
}

function setUpIENav() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI" || node.nodeName=="li") {
				node.onmouseover=function() {
					this.className+=" over";
  				}
  				node.onmouseout=function() {
  					this.className=this.className.replace(" over", "");
   				}
   			}
  		}
 	}
}

function preloadImages() {
	//preload main images
	//_img = new Image(414, 102);
	//_img.src = "IMAGE URL HERE";
}

window.onload=init;
