//---------------- MISC

document.bodyLoaded = false;

function global() {
	orollover();
	saved_comments(savedComments);
	flyouts();
}



//---------------- OTHER ROLLOVERS
function orollover() {
	if (!is.win32||!is.ie) {
		for (i=0;(img=document.images[i]);i++) {
		if(img.getAttribute){
		if(img.stickonurl) if (location.href.match(img.stickonurl)) img.src=img.rollover;
		if(img.getAttribute("rollover")) {
		img.o=new Image();img.n=new Image();
		img.o.src=img.getAttribute("rollover");img.n.src=img.src;
		over_fn = function() { this.src=this.o.src; }
		out_fn = function() { this.src=this.n.src; }
		img.addEventListener("mouseover", over_fn, false);
		img.addEventListener("mouseout", out_fn, false); } }	}		
	}
}
//---------------- //



//---------------- SAVED COMMENTS
function saved_comments(sc, delay) {
	if (sc) {
		if (is.ie5up) {
			document.body.insertAdjacentHTML("beforeEnd", "<span id=my_sc style=\"display:none\" mydelay="+delay+">"+sc+"</span>");
		}else alert(sc);
	}
}
//---------------- //



//---------------- FLYOUTS V1.2
var current_flyout_o = null;
var flyout_over_bln = false;
var hide_flyout_interval = null;
function flyouts() {
	var menu_o = new get_o("menu");
	if (!is.nav4) {
		if (menu_o.length)
			for (var i=0;i<=(menu_o.length-1);i++) init_menu(menu_o[i]);
		else init_menu(menu_o);
		window.attachEvent ? window.attachEvent("onresize", emulateonresizeend) : window.onresize = emulateonresizeend;
	}
}

var emulateonresizeendiid;
function emulateonresizeend() {
	if (parseInt(emulateonresizeendiid)) clearInterval(emulateonresizeendiid);
	emulateonresizeendiid = setInterval("flyouts();clearInterval(emulateonresizeendiid)" ,100);
}

function init_menu(o) {
	var mpos, target_o, sticky_bln = false;
	//if (o.getAttribute("stickonurl")) if (location.href.match(o.getAttribute("stickonurl"))) sticky_bln = true;
	if (o.getAttribute("submenu") && sticky_bln == false) {
		p = new get_o(o.getAttribute("submenu"));
		if (p.style) {
			mpos = o.getAttribute("submenuposition").split(",");
			if (!parseInt(mpos[0])) {
				if (mpos[0].indexOf("this") != -1) mpos[0] = mpos[0].replace("this", "getposition(o,\"x\")");
				mpos[0] = eval(mpos[0]);
			}
			if (!parseInt(mpos[1])) {
				if (mpos[1].indexOf("this") != -1) mpos[1] = mpos[1].replace("this", "getposition(o,\"y\")");
				mpos[1] = eval(mpos[1]);
			}
			p.style.left = mpos[0];
			p.style.top = mpos[1];
		}
		
		o.parentNode.mytc = o;
		
		o.parentNode.onmouseover = function() {
			var o = new get_o(this.mytc.getAttribute("submenu"));
			if (current_flyout_o) current_flyout_o.style.visibility = "hidden";
			if (o.style) o.style.visibility = "visible";
			current_flyout_o = o;
			flyout_over_bln = true;
			if (parseInt(hide_flyout_interval)) clearInterval(hide_flyout_interval);
			hide_flyout_interval = setInterval("hide_flyout()", 1500);
		}

		o.parentNode.onmouseout = function() {
			getposition(this.mytc, "x");
			flyout_over_bln = false;
		}
		
		target_o = new get_o(o.getAttribute("submenu"));
		
		target_o.onmouseover = function() {
			flyout_over_bln = true;
		}
		
		target_o.onmouseout = function() {
			flyout_over_bln = false;
		}
	}
}

function hide_flyout() {
	var o = current_flyout_o;
	if (!flyout_over_bln) {
		if (hide_flyout_interval != null) clearInterval(hide_flyout_interval);
		hide_flyout_interval = null;
		if (o.style) {
			o.style.visibility = "hidden";
		}
	}
}

function get_o(v) {
	var p = null;
	if (is.ie5up && is.mac) p = eval("document.all."+v);
	else p = document.getElementsByName(v);

	if (p.length==1) p = document.getElementsByName(v)[0];
	else if (p.length==0) p = null;

	return p;
}

function getposition(o, t) {
	var p=0;
	while (o.tagName != "BODY") {
		if (is.mac && is.ie5up) t == "x" ? p += o.offsetLeft : p += o.offsetTop;
		else if (o.tagName != "TR" && o.tagName != "TBODY") t == "x" ? p += o.offsetLeft : p += o.offsetTop;
		o = o.parentNode;
	}
	return p;
}

//---------------- // FLYOUTS END



//---- DETECTION
function Is ()
{   // convert all characters to lowercase to simplify testing
	var agt=navigator.userAgent.toLowerCase();

	// *** BROWSER VERSION ***
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
				&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
				&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	this.nav2 = (this.nav && (this.major == 2));
	this.nav3 = (this.nav && (this.major == 3));
	this.nav4 = (this.nav && (this.major == 4));
	this.nav4up = (this.nav && (this.major >= 4));
	this.navonly      = (this.nav && ((agt.indexOf(";nav") != -1) ||
						  (agt.indexOf("; nav") != -1)) );
	this.nav6 = (this.nav && (this.major == 5));
	this.nav6up = (this.nav && (this.major >= 5));
	this.gecko = (agt.indexOf('gecko') != -1);
	this.ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	this.ie3    = (this.ie && (this.major < 4));
	this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
	this.ie4up  = (this.ie  && (this.major >= 4));
	this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
	this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
	this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
	this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
	this.ie6    = (this.ie && (this.major == 4) && (agt.indexOf("msie 6.")!=-1) );
	this.ie6up  = (this.ie  && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);

	this.opera = (agt.indexOf("opera") != -1);
	this.opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
	this.opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
	this.opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
	this.opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
	this.opera5up = (this.opera && !this.opera2 && !this.opera3 && !this.opera4);

	// *** PLATFORM ***
	this.win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
	this.win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1));
	this.win98 = ((agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1));
	this.winnt = ((agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1));
	this.win32 = (this.win95 || this.winnt || this.win98 || 
					((this.major >= 4) && (navigator.platform == "Win32")) ||
					(agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1));
	this.winme = ((agt.indexOf("win 9x 4.90")!=-1));
	this.win2k = ((agt.indexOf("windows nt 5.0")!=-1));
	this.mac    = (agt.indexOf("mac")!=-1);

}
var is;
is = new Is(); 

//---------------- // DETECTION END



//---------------- FORM VALIDATION
/*
[OVERVIEW]
Simple validation. After submitting, it alerts the user
with a list of missing elements, at the same time colours the
form fields needing attention.

[CALL]
This function takes two arguments. Firstly, the form object.
Secondly, an array of information needing validating. The format
for the array is simple.

var myArray = new Array ("myEmail,eml,Please enter your email address")
if (myvalidate(objForm,myArray)) objForm.submit();

1. Field Name
2. Validation Type*
3. Alert Message

*The validation type has a few pre set types to make life easy.

1. Required:	req		(Forces something to be entered)
2. Email:		eml		(Forces the '@' and '.' characters)
3. Number:		num		(Forces an integar type)

If some more custom needs validating, you can put that code in instead.
Example: "myPostcode,f.myPostcode.length!=4,You must have a 4 digit password"

[WRITTEN]
David Tsuji
*/
function myvalidate(f,vAr) {
	var errorColor = "#FF9900";
	var error='';
	for (i=0;(obj=vAr[i]);i++) {
		con='';arg='';objAr='';objAr=obj.split(",");
		if(objAr[0])obj=eval('document.'+f.name+'.'+objAr[0]);
		arg=objAr[1];alt=objAr[2];
		if(obj.style&&!obj.n) {
			objbc = obj.style.backgroundColor;
			if (objbc) obj.n=objbc;
			else obj.n="#FFFFFF";
			obj.e=errorColor; }else if(obj.style&&obj.n) obj.style.backgroundColor=obj.n;
		myerror=function(){error+='- '+alt+'\n';if(obj.style)obj.style.backgroundColor=obj.e;}
		if(arg.length>3) con=eval(objAr[1]);
		if(con) myerror();
		if(arg=='req') if(!obj.value) myerror();
		if(arg=='eml') if(!isMyEmail(obj.value)) myerror();
		if(arg=='num') if(isNaN(obj.value)||!obj.value) myerror();
	}if(error) {alert('Please correct the following fields:\n\n'+error);return false}
	else return true;
}

/*
[WRITTEN]
David Peterson
*/
function isMyEmail(eml) {
	var emailPat = /^(\".*\"|[A-Za-z0-9\_][A-Za-z0-9\.\-\_]*)@(\[\d{1,3}(\.\d{1,3}){3}]|([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,4})$/;
	var matchArray = eml.match(emailPat);
	if (matchArray == null) return false;
	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null) for(i=1;i<=4;i++) if (IPArray[i]>255) return false;
	return true;
}
//---------------- //



/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

//---------------- //