function fnTrapKD(btnID, event){
btn = findObj(btnID);
 if (document.all){
  if (event.keyCode == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.click();
  }
 }
 else if (document.getElementById){
  if (event.which == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.focus();
   btn.click();
  }
 }
 else if(document.layers){
  if(event.which == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.focus();
   btn.click();
  }
 }
}

function findObj(n, d) { 
	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=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	return x;
}



function MouseOver(image) {
  if (document.images && image.src.indexOf(".gif")!=-1) {
 	 if(document.images && image.src.indexOf("-over.gif")==-1) {
  		image.src = image.src.substring(0,(image.src.indexOf(".gif"))) + "-over.gif";
  	}
  }
  if (document.images && image.src.indexOf(".jpg")!=-1) {
 	 if(document.images && image.src.indexOf("-over.jpg")==-1) {
  		image.src = image.src.substring(0,(image.src.indexOf(".jpg"))) + "-over.jpg";
  	}
  }
}

function MouseOut(image) {
  if(document.images && image.src.indexOf("-over.gif")!=-1) {
    image.src = image.src.substring(0,(image.src.indexOf("-over.gif"))) + ".gif";
  }
  if(document.images && image.src.indexOf("-over.jpg")!=-1) {
    image.src = image.src.substring(0,(image.src.indexOf("-over.jpg"))) + ".jpg";
  }
}

function setImageSrc(clientID, location) {
	if (document.getElementById(clientID)) {
		document.images[clientID].src = location;
	}
	return true;
}

function AttachEvent(elementObj, eventName, eventHandlerFunctionName){
	
		if (elementObj.addEventListener) {
			// Non-IE browsers
			elementObj.addEventListener(eventName, eventHandlerFunctionName, false);
		} else if (elementObj.attachEvent) {
			// IE 6+
			elementObj.attachEvent('on' + eventName, eventHandlerFunctionName);
		} else {
			// Older browsers
			var currentEventHandler = elementObj['on' + eventName];
			if (currentEventHandler == null) {
				elementObj['on' + eventName] = eventHandlerFunctionName;
			} else {
				elementObj['on' + eventName] = function(e) { currentEventHandler(e); eventHandlerFunctionName(e); }
			}
		}
}

// resize window contents when browser is over a certain size
AttachEvent(window, "resize", setSiteWidth); // also referenced in Footer
AttachEvent(window, "load", setSiteWidth);

function setSiteWidth() {
	var minWidth = 996; // 800 screen width
	var maxWidth = 1572; // 1600 screen width
	if (document.getElementById) {
	
		//alert(getBrowserWidth());
	
		var newWidth = ((getBrowserWidth() < (minWidth + 1)) ? minWidth + "px" : ((getBrowserWidth() > (maxWidth - 1)) ? maxWidth + "px" : "100%"));
		
			
		if (document.getElementById('frame'))
		{
			document.getElementById('frame').style.width = newWidth;
		}
		if (document.getElementById('header'))
		{
			document.getElementById('header').style.width = newWidth;
		}
		if (document.getElementById('footer'))
		{
			document.getElementById('footer').style.width = newWidth;
		}
	}
}


function getBrowserWidth() {
	if (document.getElementById){
		if (window.innerWidth)
		return window.innerWidth;
		if (document.documentElement&&document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
		if (document.body&&document.body.clientWidth)
		return document.body.clientWidth;
	}
}

function zoomIn(imgMap, btnMapZoomLevel1, btnMapZoomLevel2, btnMapZoomLevel3)
{
	var iPos1 = imgMap.src.indexOf('scale=') + 6;
	var iPos2 = imgMap.src.indexOf('&', iPos1);
	var strScale = imgMap.src.substring(iPos1, iPos2);
	var strScaleNew = "";
	btnMapZoomLevel1.src = btnMapZoomLevel1.src.replace('on.gif', 'off.gif');
	btnMapZoomLevel2.src = btnMapZoomLevel2.src.replace('on.gif', 'off.gif');
	btnMapZoomLevel3.src = btnMapZoomLevel3.src.replace('on.gif', 'off.gif');
	switch (strScale)
	{
		case ("100000"):
			strScaleNew = "10000";
			btnMapZoomLevel2.src = btnMapZoomLevel2.src.replace('off.gif', 'on.gif');
			break;
		default:
			strScaleNew = "5000";
			btnMapZoomLevel3.src = btnMapZoomLevel2.src.replace('off.gif', 'on.gif');
			break;
	}
	imgMap.src = imgMap.src.replace("scale=" + strScale + "&", "scale=" + strScaleNew + "&");
	return false;
}

function zoomOut(imgMap, btnMapZoomLevel1, btnMapZoomLevel2, btnMapZoomLevel3)
{
	var iPos1 = imgMap.src.indexOf('scale=') + 6;
	var iPos2 = imgMap.src.indexOf('&', iPos1);
	var strScale = imgMap.src.substring(iPos1, iPos2);
	var strScaleNew = "";
	btnMapZoomLevel1.src = btnMapZoomLevel1.src.replace('on.gif', 'off.gif');
	btnMapZoomLevel2.src = btnMapZoomLevel2.src.replace('on.gif', 'off.gif');
	btnMapZoomLevel3.src = btnMapZoomLevel3.src.replace('on.gif', 'off.gif');
	switch (strScale)
	{
		case ("5000"):
			strScaleNew = "10000";
			btnMapZoomLevel2.src = btnMapZoomLevel2.src.replace('off.gif', 'on.gif');
			break;
		default:
			strScaleNew = "100000";
			btnMapZoomLevel1.src = btnMapZoomLevel1.src.replace('off.gif', 'on.gif');
			break;
	}
	imgMap.src = imgMap.src.replace("scale=" + strScale + "&", "scale=" + strScaleNew + "&");
	return false;
}

function swSearch() { // Search box
	if (document.getElementById('queryBox').value == ""){
		alert("Please enter some search criteria");
		document.getElementById('queryBox').focus();
   		return false;
	} else {
		return true
	}
}

// Field initialise
function initTextBox(id, defaulttext) {
	id.value = defaulttext;
	id.onfocus = function(){if (id.value == defaulttext) id.value=''; id.style.color = '#000000';}
	id.style.color = '#999999';
	document.forms[document.forms.length - 1].onsubmit = function(){if (id.value == defaulttext) id.value='';}
}
function init3BoxRecipeSearch(id1, defaulttext1, id2, defaulttext2, id3, defaulttext3) {
	id1.value = defaulttext1;
	id1.onfocus = function(){if (id1.value == defaulttext1) {id1.value='';id2.value='';id3.value='';}; id1.style.color = '#000000';}
	id1.style.color = '#999999';
	document.forms[document.forms.length - 1].onsubmit = function(){if (id1.value == defaulttext1) id1.value='';}
	id2.value = defaulttext2;
	id2.onfocus = function(){if (id2.value == defaulttext2) {id1.value='';id2.value='';id3.value='';}; id2.style.color = '#000000';}
	id2.style.color = '#999999';
	document.forms[document.forms.length - 1].onsubmit = function(){if (id2.value == defaulttext2) id2.value='';}
	id3.value = defaulttext3;
	id3.onfocus = function(){if (id3.value == defaulttext3) {id1.value='';id2.value='';id3.value='';}; id3.style.color = '#000000';}
	id3.style.color = '#999999';
	document.forms[document.forms.length - 1].onsubmit = function(){if (id3.value == defaulttext3) id3.value='';}
}

function openPopup(url,name,width,height) {
	var popupLeft, popupTop;
	popupLeft = (screen.width - width)/2;
	popupTop = (screen.height - height)/2;
	var win = window.open(url, name, 'screenX='+popupLeft+',screenY='+popupTop+',left='+popupLeft+',top='+popupTop+',scrollbars=yes,status=yes,toolbar=no,menubar=no,height='+height+',width='+width, true);
	// make sure that each instance of a portal parent window has a unique name
	if (win) {
		win.name=name + Math.random();
		win.focus();
	}
	return false;
}



function clearSelects(which){
	for(a=0;a<document.forms.length;a++){
		for(i=0;i<document.forms[a].length;i++){
			if (document.forms[a].elements) {
				var tempobj = document.forms[a].elements[i];
				if (tempobj != null) {
					if(tempobj.type != null) {
						if (tempobj.type.substring(0,6) == "select"){
							if(which == "hide")
								tempobj.style.visibility = "hidden";
							else
								tempobj.style.visibility = "visible"; 
						}
					}
				}
			}
		}
	}
}

// cookie functions

function setCookie(name, value, expires)
{
    document.cookie= name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "");
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


// drop down menu navigation code
var ulOn = null;
var ulLastOn = null;
var hideNav = false;
document.onclick = handleClick;
function handleClick() {
	if (document.getElementById) {
		hideDropDownNav();
	}
	// no return value specified so it'll pass through other event's return values
}

function hideDropDownNav() {
	if (hideNav) {
		if (document.getElementById("nav-primary")) {
			var uls = document.getElementById("nav-primary").getElementsByTagName("UL");
			for (x = 1; x < uls.length; x++) { // skipping the first one since it is the master <ul> tag
				uls[x].style.display = "none";
				clearSelects("show");
			}
		}
	}
	hideNav = true;
}

function showDropDownNav(e) {
	//create a variable called 'targ' referring to the clicked element
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3)
	targ = targ.parentNode;
	
	targ = targ.parentNode.parentNode.parentNode // find an <li> tag above it
	
	ul = targ.getElementsByTagName('UL').item(0); // find <ul> tag beneath (there should only be 1 so we take the 1st regardless)
	hideDropDownNav();
	ul.style.display = "block";
	clearSelects("hide");
	ulLastOn = ulOn;
	ulOn = ul;
	hideNav = false;
	return false
}

function initPrimaryNav() {
	if (document.getElementById) {
		if (document.getElementById("nav-primary")) {
			var as = document.getElementById("nav-primary").getElementsByTagName("A");
			for (x = 0; x < as.length; x++) {
				var uls = as[x].parentNode.getElementsByTagName("UL");
				if (uls.length > 0) {	// only show drop down if there is a drop down to show!
					as[x].onclick = showDropDownNav;
				}
				for (y = 0; y < uls.length; y++) {
					if (uls[y].getAttribute("CLASS") == "sub") {
						var aas = uls[y].getElementsByTagName("A");
							for (z = 0; z < aas.length; z++) {
								aas[z].onclick = function() {return true};	// allow links in drop down to work
							}
					}
				}
			}
		}
	}
}

// Tip Slider
var currentPage = 0;
var pageCount = 1;
var scrolling = false;
var initialised = false;
var moveSpeed = 1590;

// for overview control
var moveBy = 72;

// for main tip images
var moveBy2 = 424;

var slideBar;
var slideThoseTips;

function initScroll() {
    $('bar').setProperty('left', 0);
    $('thosetips').setProperty('left', 0);
    slideBar = new Fx.Style($('bar'), 'left', {duration: moveSpeed, onComplete: finishedScroll});
    slideThoseTips = new Fx.Style($('thosetips'), 'left', {duration: moveSpeed, onComplete: finishedScroll});
}

function scrollLeft() {
    if (!initialised) {
        initScroll();
        initialised = true;
    }
	if (!scrolling) {
		if (currentPage > 0) {
			scrolling = true;
			slideBar.start($('bar').getProperty('left'), parseInt($('bar').getProperty('left')) - moveBy);
			$('bar').setProperty('left', parseInt($('bar').getProperty('left')) - moveBy);
			
			slideThoseTips.start($('thosetips').getProperty('left'), parseInt($('thosetips').getProperty('left')) + moveBy2);
			$('thosetips').setProperty('left', parseInt($('thosetips').getProperty('left')) + moveBy2);
			currentPage--;
		}
	}
	return true;
}
function scrollRight() {
    if (!initialised) {
        initScroll();
        initialised = true;
    }    
	if (!scrolling) {
		if (currentPage < pageCount) {
			scrolling = true;
			slideBar.start($('bar').getProperty('left'), parseInt($('bar').getProperty('left')) + moveBy);
			$('bar').setProperty('left', parseInt($('bar').getProperty('left')) + moveBy);
			
			slideThoseTips.start($('thosetips').getProperty('left'), parseInt($('thosetips').getProperty('left')) - moveBy2);
			$('thosetips').setProperty('left', parseInt($('thosetips').getProperty('left')) - moveBy2);
			currentPage++;
		}
	}
	return true;
}
function finishedScroll()
{
	scrolling = false;
	return true;
}

function showDiv(divId)
{
	elem = document.getElementById(divId)
	if (elem != null)
	{
		elem.style.display = "block";
	}
	return false;
}
