// JavaScript Code for Nykris-designed Websites
// (c) 2003 Nykris Digital Design Ltd.
// Engineer: Graham Bartram

// Global Settings

var gUseQuickTime = true; // If this is true the code will replace Flash elements with .movs if Flash isn't available and QuickTime is.



// Browser ID Utilities

var gAgent = window.navigator.userAgent;
var gAgentVers = parseInt(gAgent.charAt(gAgent.indexOf("/")+1),10);

function is_explorer()
{
	return gAgent.indexOf("MSIE") > 0;
}
	
function is_macexplorer()
{
	if (gAgent.indexOf("Mac") <= 0)
		return false;
	else
		return gAgent.indexOf("MSIE") > 0;
}
	
function is_macnetscape()
{
	if (gAgent.indexOf("Mac") <= 0)
		return false;
	else
		return !(gAgent.indexOf("MSIE") > 0);
}
	
function is_macie4()
{
	if ((gAgent.indexOf("Mac") > 0) && (gAgent.indexOf("MSIE") > 0) && (gAgentVers < 5))
		return true;
	else
		return false;
}

function is_pcnetscape()
{
	if (gAgent.indexOf("Mac") > 0)
		return false;
	else
		return !(gAgent.indexOf("MSIE") > 0);
}
	
function is_aol()
{
	return gAgent.indexOf("AOL") > 0;
}


function choose_stylesheet(name)
{
	if (is_macexplorer())
		name = name + "_macie";
	else if (is_macnetscape())
		name = name + "_macnet";
	else if (is_explorer())
		name = name + "_ie";
	else
		name = name + "_net";
		
	document.write('<link rel="stylesheet" href="/' + name + '.css" type="text/css">');
}


// Flash and Image Utilities

// Check whether Flash is installed on the user's computer return one of four possible values
// which tell other routines what to do when asked to insert a Flash movie:
// "img" - write out <IMG> tag instead
// "swf" - write out <EMBED> tag and use a Flash movie
// "mov" - write out <EMBED> tag and use a QuickTime movie (QT4 onwards plays Flash)
// "none" - do nothing, <NOSCRIPT> will handle the image in Netscape 2

var gImageHandling = "";

function check_flash()
{	
	gImageHandling = "none";
		
	if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
	{
		// Navigator and Explorer ver 5 onwards
		if (navigator.plugins && navigator.plugins["Shockwave Flash"])
		{
			var desc = navigator.plugins["Shockwave Flash"].description;
			var vers = parseInt(desc.charAt(16));
			if (vers >= 3) gImageHandling = "swf";
		}
	}
			
	if (gUseQuickTime && gImageHandling == "none" && navigator.mimeTypes && navigator.mimeTypes["video/quicktime"] && navigator.mimeTypes["video/quicktime"].enabledPlugin)
	{
		// Navigator and Explorer ver 5 onwards
		var instPlugins = navigator.plugins
		for (var i = 0; i < instPlugins.length; i++)
		{
   			if (instPlugins[i].name.indexOf("QuickTime")>=0)
   			{
				var desc = navigator.plugins[i].name;
				var vers = parseInt(desc.charAt(18));
				if (vers >= 2) gImageHandling = "mov";
				i = instPlugins.length;
	   		}
		}
	}
			
	if (gImageHandling == "none" && navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
	   && (navigator.userAgent.indexOf("Windows")>=0 || navigator.userAgent.indexOf("Win32")>=0))
	{
	   	// Explorer on a 32-bit PC
		document.write('<scr' + 'ipt language=VBSc' + 'ript\> \n');
		document.write('dim Obj\n');
		document.write('gImageHandling = "gif"\n');
		document.write('on error resume next \n');
		document.write('Set Obj = CreateObject("ShockwaveFlash.ShockwaveFlash.4")\n');
		document.write('if IsObject(Obj) = True then gImageHandling = "swf" \n');
		document.write('Set Obj = Null \n');
		document.write('</scr' + 'ipt\> \n');
	}
		
	if (gImageHandling == "none" && !(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0))
	{
		// Anything else, other than Navigator ver 2 which will use <NOSCRIPT>
		gImageHandling = "gif";
	}
	
	//	document.write('<p>gImageHandling = ' + gImageHandling + '</p>');
}




// Insert a Flash movie or fallback GIF into the current page depending upon the setting
// of the gImageHandling variable.

function insert_flash(name, width, height, bgCol)
{
	if (gImageHandling == "")
	{
		// The variable has not been set so do a check.
		check_flash();
	}
		
	if (gImageHandling.indexOf("none") != -1)
	{
	}
	else if (gImageHandling.indexOf("swf") != -1)
	{
		document.write('<embed src="' + name + '.swf"');
		document.write(' swLiveConnect=false width=' + width + ' height=' + height);
		document.write(' loop=false quality=high scale=noborder bgcolor=' + bgCol);
		document.write(' type="application/x-shockwave-flash"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
	}
	else if (imageHandling.indexOf("mov") != -1)
	{
		document.write('<embed src="' + name + '.mov"');
		document.write(' autoplay="true" controller="false" width="' + width + '" height="' + height + '"');
		document.write(' loop=false bgcolor=' + bgCol);
		document.write(' type="video/quicktime"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
	}
	else
	{
		document.write('<img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\>');
	} 
	
}



// Insert a Flash movie or fallback GIF into the current page depending upon the setting
// of the gImageHandling variable. If the browser is Mac Netscape then the fallback GIF is
// used instead to avoid the "Flash off the screen" bug.

function insert_flash_lowdown(name, width, height, bgCol)
{
	var imageHandling = gImageHandling;
	
	if (gImageHandling == "")
	{
		// The variable has not been set so do a check.
		check_flash();
		imageHandling = gImageHandling;
	}
	
	if (is_macnetscape()) imageHandling = "gif";

	if (imageHandling.indexOf("none") != -1)
	{
	}
	else if (imageHandling.indexOf("swf") != -1)
	{
		document.write('<embed src="' + name + '.swf"');
		document.write(' swLiveConnect=false width=' + width + ' height=' + height);
		document.write(' loop=false quality=high scale=noborder bgcolor=' + bgCol);
		document.write(' type="application/x-shockwave-flash"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
	}
	else if (imageHandling.indexOf("mov") != -1)
	{
		document.write('<embed src="' + name + '.mov"');
		document.write(' autoplay="true" controller="false" width="' + width + '" height="' + height + '"');
		document.write(' loop=false bgcolor=' + bgCol);
		document.write(' type="video/quicktime"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
	}
	else
	{
		document.write('<img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\>');
	} 
	
}



// Insert a random Flash movie or random fallback GIF into the current page depending upon
// the setting of the gImageHandling variable.

function insert_random_flash(imgPath,width,height,bgCol,numImages)
{
	if (gImageHandling == "")
	{
		// The variable has not been set so do a check.
		check_flash();
	}
		
	imgNum = (Math.floor((new Date()).getTime()/10)%numImages) + 1;
	imgNumStr = (new Number(imgNum)).toString();
	if (imgNum < 10) imgNumStr = '0' + imgNumStr;

	if (gImageHandling.indexOf("none") != -1)
	{
	}
	else if (gImageHandling.indexOf("swf") != -1)
	{
		document.write('<embed src="' + imgPath + imgNumStr + '.swf"');
		document.write(' swLiveConnect=false width=' + width + ' height=' + height);
		document.write(' loop=false quality=high scale=noborder bgcolor=' + bgCol);
		document.write(' type="application/x-shockwave-flash"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + imgPath + imgNumStr + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
	}
	else if (gImageHandling.indexOf("mov") != -1)
	{
		document.write('<embed src="' + imgPath + imgNumStr + '.mov"');
		document.write(' autoplay="true" controller="false" width="' + width + '" height="' + height + '"');
		document.write(' loop=false bgcolor=' + bgCol);
		document.write(' type="video/quicktime"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + imgPath + imgNumStr + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
	}
	else
	{
		document.write('<img src="' + imgPath + imgNumStr + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\>');
	} 
	
}




// Insert a random GIF image into the current page depending upon the setting of the
// gImageHandling variable.

function insert_random_image(imgType,imgPath,imgWidth,imgHeight,imgHspace,imgVspace,numImages)
{
	var imgNum,imgNumStr="";
	
	if (gImageHandling == "")
	{
		// The variable has not been set so do a check.
		check_flash();
	}
		
	if (gImageHandling.indexOf("none") == -1)
	{
		imgNum = (Math.floor((new Date()).getTime()/10)%numImages) + 1;
		imgNumStr = (new Number(imgNum)).toString();
		if (imgNum < 10) imgNumStr = '0' + imgNumStr;
		document.write('<img src="' + imgPath + imgNumStr + '.' + imgType + '" width=' + imgWidth + ' height=' + imgHeight + ' vspace=' + imgVspace + ' hspace=' + imgHspace + '\>');
	}

}



// Insert a random GIF image into the current page along with the appropriate URL and ALT tags. The
// individual URLs and ALT tags are separated in the string by the '|' character.

function insert_random_image_plus(imgType,imgPath,imgWidth,imgHeight,imgHspace,imgVspace,numImages,urls,alts)
{
	var imgNum,imgNumStr="";
	var altTag = "";
	var url = "";
	
	if (gImageHandling == "")
	{
		// The variable has not been set so do a check.
		check_flash();
	}
		
	if (gImageHandling.indexOf("none") == -1)
	{
		var urlArray = mysplit(urls,'|');
		var altArray = mysplit(alts,'|');
		
		imgNum = (Math.floor((new Date()).getTime()/10)%numImages) + 1;
		url = urlArray[imgNum-1];
		altTag = altArray[imgNum-1];
		imgNumStr = (new Number(imgNum)).toString();
		if (imgNum < 10) imgNumStr = '0' + imgNumStr;
		document.write('<a href="' + url + '"\><img src="' + imgPath + imgNumStr + '.' + imgType + '" width=' + imgWidth + ' height=' + imgHeight + ' vspace=' + imgVspace + ' hspace=' + imgHspace + ' alt="' + altTag + '" border=0\></a\>');
	}

}


function preloadImages()
{
  if (document.images)
  {
    var imgFiles = preloadImages.arguments;
    if (document.preloadArray==null) document.preloadArray = new Array();
    var i = document.preloadArray.length;
    with (document) for (var j=0; j<imgFiles.length; j++)
    {
		if (imgFiles[j].charAt(0)!="#")
		{
		  preloadArray[i] = new Image;
		  preloadArray[i++].src = imgFiles[j];
		}
  	}
  }
}

// MacroMedia Rollover Utilities - to replace Nykris functions for performance
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.0
  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 && document.getElementById) x=document.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];
		}
}

// PSE Rollover Functions - to replace Nykris functions for performance
function swapImagePermanent() { 
 
	var i,j=0,x,a=swapImagePermanent.arguments; 
	for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null)
			{
				if(!x.oSrc) 
					x.oSrc=a[i+2]; 
				
				x.src=a[i+2];
			}
}

// swap a named image with _norm in its path to the same path but with _over
function swapImageWithOver(button) {
	var i,j=0,x,imagepath;
	document.MM_sr=new Array; 
	if ((x=MM_findObj(button))!=null)
	{
		document.MM_sr[j++]=x; 
		if(!x.oSrc) 
			x.oSrc=x.src; 
		imagepath=x.src;
		if (x.src.indexOf('_norm') > 0)
			{
			imagepath = x.src.substring(0,x.src.indexOf('_norm')) + '_over.gif';
			}
		//alert('Setting Rollover for ' + button + ' to ' + imagepath);
		x.src=imagepath;
	}
}

function swapImageWithNorm(button) {
	var i,j=0,x,imagepath;
	document.MM_sr=new Array; 
	if ((x=MM_findObj(button))!=null)
	{
		document.MM_sr[j++]=x; 
		if(!x.oSrc) 
			x.oSrc=x.src; 
		imagepath=x.src;
		if (x.src.indexOf('_over') > 0)
			{
			imagepath = x.src.substring(0,x.src.indexOf('_over')) + '_norm.gif';
			}
		//alert('Setting Rollover for ' + button + ' to ' + imagepath);
		x.src=imagepath;
	}
}

function load_all_rollovers()
{
	var numImages = document.images.length;
	if (numImages > 0)
	{
		for (var i=0;i<numImages;i++)
		{
			if (document.images[i])
			{
				if (document.images[i].name)
				{ 
					if (document.images[i].src.indexOf('_norm') != -1)
					{
						var imagepath = document.images[i].src.substring(0,document.images[i].src.indexOf('_norm')-1) + '_over.gif';
						MM_preloadImages(imagepath);
						//alert('Preloading ' + '/images/' + document.images[i].name + '_over.gif');
					}
				}
			}
		}
	}
}

function rollover(button,state)
{
	if (state == 1)
	{
		swapImageWithOver(button);
		//alert('Rollover for ' + '/images/' + button + '_over.gif');
	}
	else 
		swapImageWithNorm(button);
		//alert('Rollover for ' + '/images/' + button + '_norm.gif');
}


function rollover_image(button,imageName)
{
	swapImagePermanent(button,'','/images/' + imageName + '.gif');
	//alert('rollover_image for ' + '/images/' + imageName + '.gif');
}

// Recursively look for an element, including searching the layers in Netscape. Doesn't support frames.

function find_element(name,layer)

{
  var element=null,i;
  if (!layer)
    {
    layer=document;
    }
  if (!(element=layer[name]) && layer.all)
    {
    element=layer.all[name];
    }
  for (i=0; !element && i<layer.forms.length; i++)
    {
    element=layer.forms[i][name];
    } 
  for (i=0; !element && layer.layers && i<layer.layers.length; i++)
    {
    element=find_element(name,layer.layers[i].document);
    }
  if (!element && document.getElementById)
    {
    element=document.getElementById(name);
    }
  if (!element && document.layers)
  	{
  	//alert("trying layers for "+name+":"+document.layers[name]);
  	element=document.layers[name];
  	}
  //if (!element) alert("Could not find " + name);
  return element;
}


var gStaticMenu = new Object();
var gRolloversPath = "/images/";

// Rollover Utilities
// disabled by PSE 04/03/04 for performance reasons - using MM functions for rollovers instead

/*
var gRollovers = new Object();

	
	



// This function locates a graphic element whose name parameter is set to "rollover_marker" and sets the
// path for rollovers to the path of that graphic element.

function set_rollovers_path()
{
	gRolloversPath = '';
	if (document.images)
	{
		var img = (!is_explorer()) ? find_element('rollover_marker',0) : document['rollover_marker'];
		if (img)
		{
			var src = img.src
			var lastOffset = src.lastIndexOf('/');
			if (lastOffset != -1)
			{
				gRolloversPath = src.substr(0,lastOffset+1);
			}
		}
	}
}


// Loads the normal and overlay versions of the button, but will not try to do so for the
// "rollover_marker" image or any image whose name begins with an underscore.

function load_rollover(button)
{
	//alert("Loading rollover images for " + gRolloversPath + button);
	if (document.images && button.charAt(0) != '_' && button.substr(0,15) != 'rollover_marker' && button.substr(0,2) != 'qu' && button.substr(0,6) != 'granny')
	{
		gRollovers[button] = new Object();
		gRollovers[button][0] = new Image();
		gRollovers[button][0].src = gRolloversPath + button + '_norm.gif';
		gRollovers[button][1] = new Image();
		gRollovers[button][1].src = gRolloversPath + button + '_over.gif';
	}
}


// Loops through all the named images in the page and attempts to load the rollover images for them

function load_all_rollovers()
{
	set_rollovers_path();
	if (document.images && gRolloversPath != '*')
		{
		var numImages = document.images.length;
		if (numImages > 0)
		{
			for (var i=0;i<numImages;i++)
			{
				if (document.images[i])
				{
					if (document.images[i].name)
					{ 
						if (document.images[i].src.indexOf('_norm') != -1)
							load_rollover(document.images[i].name);
					}
				}
			}
		}
		}
}



function rollover(button,state)
{
	if (document.images && gRolloversPath != '*')
	{
		if (gRollovers[button])
		{
			var img = (!is_explorer()) ? find_element(button,0) : document[button];
			if (img) img.src = gRollovers[button][state].src;
			return true;
		}
	}
	return false;
}



function rollover_image(button,imageName)
{
	//alert("1 " + button + "," + imageName);
	if (document.images && gRolloversPath != '*')
	{
		//alert("2 " + button + "," + imageName);
		var img = (!is_explorer()) ? find_element(button,0) : document[button];
		if (img)
		{
			if (gStaticMenu[imageName])
			{
				img.src = gStaticMenu[imageName].src;
			}
			else
			{
				//alert("3 " + button + "," + img + "," + gRolloversPath + imageName + '.gif');
				img.src = gRolloversPath + imageName + '.gif';
			}
			return true;
		}
	}
	return false;
}

*/

// Miscellaneous Javascript Utilities

var gPopup = "none";




// Open a window in either the topleft or topright screen corner

function openWin(url,name,w,h,loc)
{
	var secs = new Date();

	closeWin();

	if (loc == 'topright')
	{
		gPopup=window.open(url,'popup'+secs.getTime(),'left='+(screen.width.valueOf()-(w+100))+',top=100,width='+w+',height='+h+',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes')
	}
	else if (loc == 'topleft')
	{
		gPopup=window.open(url,'popup'+secs.getTime(),'left=100,top=100,width='+w+',height='+h+',toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes')
	}
}



// Open a window in either the topleft or topright screen corner

function openFixedWin(url,name,w,h,loc)
{
	var secs = new Date();

	closeWin();

	if (loc == 'topright')
	{
		gPopup=window.open(url,'popup'+secs.getTime(),'left='+(screen.width.valueOf()-(w+100))+',top=100,width='+w+',height='+h+',toolbar=no,location=no,status=no,scrollbars=no,resizable=no')
	}
	else if (loc == 'topleft')
	{
		gPopup=window.open(url,'popup'+secs.getTime(),'left=100,top=100,width='+w+',height='+h+',toolbar=no,location=no,status=no,scrollbars=no,resizable=no')
	}
}



// Close the popup window if it is open

function closeWin()
{	
	if (gPopup)
	{
		if (!is_macie4())
			{
				if (typeof(gPopup) == "object")
				{
					if (gPopup.closed == false)
					{
						gPopup.close();
					}
					gPopup="none";
				}
			}
		else
			{
				if (typeof(gPopup) == "object")
				{
					gPopup="none";
				}
			}
	}
}



// Change the URL of the main window, even if we are in the popup

function navigateMainWindow(url)
{
	if ((window.name.length >= 5) && (window.name.substring(0,5) == "popup"))
	{
		window.opener.location = url;
	}
	else
	{
		window.location = url;
	}
}



// Open a window for an image with optional caption either the topleft or topright screen corner

function openImageWin(title,imageUrl,imageW,imageH,caption,loc)
{
	var w = imageW+16;
	var h = imageH+16;
	var popupHTML = "";
	
	closeWin();
	   
	var lowerCaption = caption.toLowerCase();
	var captionArray = mysplit(lowerCaption,'<br>');
	var numberOfLines = captionArray.length;
	if (caption != "")
	{
		h += 8 + (14 * numberOfLines);
	}
	
	popupHTML += "<html><head><title>"+title+"</title>";
	if (caption != "")
	{
		popupHTML += "<style>";
		popupHTML += "<!--";
		popupHTML += ".small { color: #333; font-size: 10px; line-height: 14px; font-family: Arial, Helvetica, Geneva; }";
		popupHTML += ".body {color: #333; font-size: 11px; line-height: 14px; font-family: Arial, Helvetica, Geneva; }";
		popupHTML += ".body a:link { color: #666; text-decoration: none }";
		popupHTML += ".body a:active { color: #666; text-decoration: underline }";
		popupHTML += ".body a:visited { color: #666; text-decoration: none }";
		popupHTML += ".body a:hover { color: #666; text-decoration: underline }";
		popupHTML += "-->";
		popupHTML += "</style>";
	}
	popupHTML += "</head>";
	popupHTML += "<body bgcolor='#FFFFFF' leftmargin=8 topmargin=8 marhinheight=8 marginwidth=8>";
	popupHTML += "<table cellpadding=0 cellspacing=0 border=0>";
	popupHTML += "<tr><td><img src='" + imageUrl + "' width="+imageW+" height="+imageH+" border=0></td></tr>";
	if (caption != "")
	{
		popupHTML += "<tr><td valign=top align=left><table cellpadding=8 cellspacing=0 border=0><tr><td align=left valign=top class='body'>" + caption + "</td></tr></table></td></tr>";
	}
 	popupHTML += "</table>";
  	popupHTML += "</body></html>";
		
	var x = 0;
	var y = 0;
	
	// open the popup window
	if (loc == 'topright')
	{
		x=(screen.width.valueOf()-(w+100));
		y=100;
	}
	else if (loc == 'topleft')
	{
		x=100;
		y=100;
	}
	else if (loc == 'center')
	{
		x=Math.round((screen.width.valueOf()/2)-(w/2));
		y=100;
	}
	
	var secs = new Date();
			
	if (is_aol())
		{
		w = w+4;
		h = h+4;
		gPopup=window.open('','popup'+secs.getTime(),'width='+w+',height='+h+',toolbar=no,location=no,status=no,scrollbars=no,resizable=no');
		}
	else
		{
		gPopup=window.open('','popup'+secs.getTime(),'left='+x+',top='+y+',width='+w+',height='+h+',toolbar=no,location=no,status=no,scrollbars=no,resizable=no');
		gPopup.moveTo(x,y);
		}

	// write the contents into the window
	gPopup.document.open();
	gPopup.document.write(popupHTML);
	gPopup.document.close();
	gPopup.focus();
}



// Java 1.0 doesn't support a split method, so define one here

function mysplit (stringtoSplit, separator)
{
	var splitIndex = 0 ;
	var splitArray = new Array () ;

	//alert("Splitting "+stringtoSplit+" by " + separator);
	while ((stringtoSplit.length > 0) && (separator.length > 0))
	{
		var i = stringtoSplit.indexOf (separator) ;
		if ((!i) && (separator != stringtoSplit.substring (0, separator.length)))
			break ;
		if (i == -1)
		{
			splitArray [splitIndex++] = stringtoSplit ;
			break ;
		}
		splitArray [splitIndex++] = stringtoSplit.substring (0,i) ;

		stringtoSplit = stringtoSplit.substring (i+separator.length, stringtoSplit.length) ;
	}

	splitArray.length = splitIndex; 
	return splitArray ;
}


// Quiz answer routines for foodfitness

var gQuestion = 1;
var gAnswers = new Array(12);
var gScores = new Array(12);
var gNumQuestions = 0;
var gThreshold1 = 0;
var gThreshold2 = 0;
var gQuAnswers= new Array(6);
var gStillIniting = true;

function readCookie()
{
	var myCookie = document.cookie;
	//alert("cookie is '" + myCookie + "'");
	var cookieVariables = mysplit(myCookie,';');
	var cookieDataFound = false;
	//alert("There are " + cookieVariables.length + " variables");
	for (var i = 0; i < cookieVariables.length; i++)
	{
		//alert('Variable '+i+' is "' + cookieVariables[i] + '"');
		if (cookieVariables[i].indexOf('assessment') != -1)
		{
			equalMark = cookieVariables[i].indexOf('=');
			myCookie = cookieVariables[i].substring(equalMark+1,cookieVariables[i].length);
			//alert("Variables are "+myCookie);
			cookieDataFound = true;
			i = cookieVariables.length;
			var QuAnswers = mysplit(myCookie,'|');
			for (var j = 0; j < 6; j++)
			{
				gQuAnswers[j] = QuAnswers[j];
			}
		}
	}
	if (cookieDataFound == false)
	{
		clearCookie();
	}
}

function writeCookie()
{

	var now = new Date();
	var myCookie="assessment=";
	for (i = 0; i <= 5; i++)
	{
		myCookie = myCookie + gQuAnswers[i] + "|";
	}
	now.setTime(now.getTime() + (60 * 60 * 1000));
	//myCookie += "; expires=" + now.toGMTString();
	myCookie += "; path=/";
	//myCookie += "; domain=127.0.0.1";
	//alert("WriteCookie:'" + myCookie + "'");
	document.cookie = myCookie;
	
}

function clearCookie()
{
	gQuAnswers[0]="00000";
	gQuAnswers[1]="";
	gQuAnswers[2]="";
	gQuAnswers[3]="";
	gQuAnswers[4]="";
	gQuAnswers[5]="";
	writeCookie();
}


function initQuestions (questionNum,numQuestions,scores,threshold1,threshold2)
{
	var i,j;
 	var answers = "";
 	
	gStillIniting = true;
	if (document.images)
		{
 		gStaticMenu['quizradio_off'] = new Image();
 		gStaticMenu['quizradio_off'].src = gRolloversPath + 'quizradio_off.gif';
 		gStaticMenu['quizradio_on'] = new Image();
 		gStaticMenu['quizradio_on'].src = gRolloversPath + 'quizradio_on.gif';
		gStaticMenu['quizsidebarempty'] = new Image();
 		gStaticMenu['quizsidebarempty'].src = gRolloversPath + 'quizsidebarempty.gif';
		gStaticMenu['quizsidebarred'] = new Image();
 		gStaticMenu['quizsidebarred'].src = gRolloversPath + 'quizsidebarred.gif';
		gStaticMenu['quizsidebaramber'] = new Image();
 		gStaticMenu['quizsidebaramber'].src = gRolloversPath + 'quizsidebaramber.gif';
		gStaticMenu['quizsidebargreen'] = new Image();
 		gStaticMenu['quizsidebargreen'].src = gRolloversPath + 'quizsidebargreen.gif';
		gStaticMenu['granny_face_01'] = new Image();
 		gStaticMenu['granny_face_01'].src = gRolloversPath + 'granny_face_01.gif';
		gStaticMenu['granny_face_02'] = new Image();
 		gStaticMenu['granny_face_02'].src = gRolloversPath + 'granny_face_02.gif';
		gStaticMenu['granny_face_03'] = new Image();
 		gStaticMenu['granny_face_03'].src = gRolloversPath + 'granny_face_03.gif';
		}

	gQuestion = questionNum;
	gNumQuestions = numQuestions;
	gThreshold1 = threshold1;
	gThreshold2 = threshold2;
	
	readCookie();
	 	
 	answers=gQuAnswers[gQuestion];

	scoreArray = mysplit(scores,"|");
	for (i=1;i<=numQuestions;i++)
	{
		if (answers == "")
		{
			gAnswers[i]=0;
		}
		else
		{
			gAnswers[i]=parseInt(answers.substr(i-1,1));
		}
		theseScores = scoreArray[i-1];
		numScores = theseScores.length;
		gScores[i] = new Array(numScores);
		
		for (j=1;j<=numScores;j++)
		{
			gScores[i][j] = parseInt(theseScores.substr(j-1,1));
		}
	}
	updateAnswers();
	updateAssessment(gQuestion,gQuAnswers[0]);
	gStillIniting = false;
}

function updateAnswers ()
{
	var i;

	for (i=1;i<=gNumQuestions;i++)
	{
		if (gAnswers[i] != 0)
		{
			answerName = "qu" + i + "" + gAnswers[i];
			rollover_image(answerName,"quizradio_on");
		}
	}
}

function updateAssessment (question,assessment)
{
	var i;

	for (i=1;i<=5;i++)
	{
		resultName = "quizresult" + i;
		quizstatus = parseInt(assessment.substr(i-1,1));
		if (quizstatus == 0)
		{
			rollover_image(resultName,"quizsidebarempty");
		}
		else if (quizstatus == 1)
		{
			if (i == question)
			{
				displayDiv("answerred",true);
				rollover_image("granny","granny_face_02");
			}
			rollover_image(resultName,"quizsidebarred");
		}
		else if (quizstatus == 2)
		{
			if (i == question)
			{
				displayDiv("answeramber",true);
				rollover_image("granny","granny_face_01");
			}
			rollover_image(resultName,"quizsidebaramber");
		}
		else if (quizstatus == 3)
		{
			if (i == question)
			{
				displayDiv("answergreen",true);
				rollover_image("granny","granny_face_03");
			}
			rollover_image(resultName,"quizsidebargreen");
		}
	}
}

function clickAnswer (question, answer)
{
	var j;
	
	if (gStillIniting == false)
	{
		for (j=0;j<6;j++)
		{
			answerName = "qu" + question + "" + (j+1);
			rollover_image(answerName,"quizradio_off");
		}
		answerName = "qu" + question + "" +  answer;
		rollover_image(answerName,"quizradio_on");
		displayDiv("answergreen",false);
		displayDiv("answeramber",false);
		displayDiv("answerred",false);
		resultName = "quizresult" + gQuestion;
		rollover_image(resultName,"quizsidebarempty");
		rollover_image("granny","granny_face_01");
		gAnswers[question] = answer;
	}
}


function submitAnswers ()
{
	var i,unanswered = 0;
	var missing = "";
	var totalScore = 0;
	
	for (i=gNumQuestions;i>0;i--)
	{
		if (gAnswers[i] == 0)
		{
			unanswered += 1;
			if (unanswered == 1)
			{
				missing += i;
			}
			else if (unanswered == 2)
			{
				missing = i + " & " + missing;
			}
			else
			{
				missing = i + ", " + missing;
			}
		}
		else
		{
			totalScore += gScores[i][gAnswers[i]];
		}
	}
	if (unanswered == 1)
	{
		missing = "You need to answer question " + missing;
	}
	else
	{
		missing = "You need to answer questions " + missing;
	}
	if (unanswered > 0)
	{
		alert(missing);
	}
	else
	{
		resultName = "quizresult" + gQuestion;
		if (totalScore < gThreshold1)
		{
			displayDiv("answerred",true);
			rollover_image(resultName,"quizsidebarred");
			rollover_image("granny","granny_face_02");
			thisAssessment = "1";
		}
		else if (totalScore >= gThreshold1 && totalScore < gThreshold2)
		{
			displayDiv("answeramber",true);
			rollover_image(resultName,"quizsidebaramber");
			rollover_image("granny","granny_face_01");
			thisAssessment = "2";
		}
		else if (totalScore >= gThreshold2)
		{
			displayDiv("answergreen",true);
			rollover_image(resultName,"quizsidebargreen");
			rollover_image("granny","granny_face_03");
			thisAssessment = "3";
		}
		updateCookie(thisAssessment);
		window.scroll(0,0);
	}
}

function updateCookie(thisAssessment)
{
 	var answers = "";
 	var i;
 	
 	//alert("Updating cookie3");	
 	
 	for (i = 1; i<=gNumQuestions; i++)
 	{
 		answers = answers + gAnswers[i];
 	}
 	//alert("Answers: "+answers);
 	
 	gQuAnswers[gQuestion] = answers;
 	assessment = gQuAnswers[0];
 	
 	if (gQuestion == 1)
 	{
 		gQuAnswers[0] = thisAssessment + assessment.substr(1,4);
 	}
 	else if (gQuestion == 2)
 	{
 		gQuAnswers[0] = assessment.substr(0,1) + thisAssessment + assessment.substr(2,3);
 	}
 	else if (gQuestion == 3)
 	{
 		gQuAnswers[0] = assessment.substr(0,2) + thisAssessment + assessment.substr(3,2);
 	}
 	else if (gQuestion == 4)
 	{
 		gQuAnswers[0] = assessment.substr(0,3) + thisAssessment + assessment.substr(4,1);
 	}
 	else if (gQuestion == 5)
 	{
 		gQuAnswers[0] = assessment.substr(0,4) + thisAssessment;
 	}
	//alert("Write cookie");
	writeCookie();

}

function displayDiv(divName,visible)
{
	if ((layer=find_element(divName))!=null)
	{
	if (layer.style)
		{
		if (visible)
			{
			layer.style.display='block';
			}
		else
			{
			layer.style.display='none';
			}
		}
	else
		{
		if (visible)
			{
			layer.display='block';
			}
		else
			{
			layer.display='none';
			}
		}
	}
}


