 // BROWSER DETECTION
function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=new lib_bwcheck()

/* writes flash animation to screen 
 * PARAMS
 * 	src: the path of the flash file
 * 	id: id used for object (id), embed (id + _embed) and map (id + _map)
 * 	width: width of the swf
 * 	height: height of the swf
 * 	alternateSrc: path for alternate image
 * 	redirect: boolean for link to Macromedia
 * 	version: flash player version
 */
 
function writeFlash(src, id, width, height, alternateSrc, redirect, version) {
	if ((flashinstalled == 2) && (flashversion >= version)) {
		document.write('<object style="width: ' + width + '; height: ' + height + '" id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + ',0,0,0">');
		document.write('<param name="movie" value="' + src + '" />');
		document.write('<param name="allowScriptAccess" value="sameDomain" />');
		document.write('<param name="quality" value="high" />');
		document.write('<param name="wmode" value="transparent" />');
		document.write('<embed id="' + id + '_embed" wmode="transparent" swliveconnect="true" style="width: ' + width + '; height: ' + height + '" src="' + src + '" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"></embed>');
		document.write('</object>');
		return true;
	} else {
	 	if (redirect)
			document.write('<a href="http://www.macromedia.com/go/getflashplayer" target="_blank">');
		if (alternateSrc != "")
			document.write('<img src="' + alternateSrc + '" width="' + width + '" height="' + height + '" id="nav" name="nav" alt="" border="0" usemap="#' + id + '_map">');
	 	if (redirect)
			document.write('</a>');
		return false;
	}
}

// IFRAME RESIZE
function resize(div, top, padding) {
	parent.scrollTo(0, top);
	parent.document.getElementById(div).style.height = padding;
	parent.document.getElementById(div).style.height = window.document.body.scrollHeight + padding;
}

// IFRAME REDIRECT

function redirect(page, doRedirects) {
	if (doRedirects) {
		urlArray = new String(document.URL).split("/");
		urlArray = new String(urlArray[urlArray.length-1]).split("?");
		currentPage	= urlArray[0];

		if ((parent.frames.length == 0) && (currentPage != page) && (currentPage != "")) {
			urlArray = new String(document.URL).split("/");
			str = new String(urlArray[urlArray.length-1]).replace(/\?/, "%3F");
			str = new String(urlArray[urlArray.length-1]).replace(/\&/, "%26");
			document.location = page + "?redirect=" + str;
			return true;
		} else {
			return false;
		}
	}
}

// IMAGE ROLLOVER
var imgArray	= new Array();
var rollArray	= new Array();
var normArray	= new Array();
var setArray	= new Array();

function preloadPics() {
	for (i=0; i<arguments.length; i++) {
		imgArray[i] = new Image();
		imgArray[i].src = arguments[i];
	}
}

function rollerPics(key, normSrc, rollSrc) {
	normArray[key] = new Image();
	normArray[key].src = normSrc;
	rollArray[key] = new Image();
	rollArray[key].src = rollSrc;
}

function initSet(setName) {
	setArray[setName] = new Array();
}

function addToSet(imageID, setName) {
	setArray[setName].push(imageID);
}

function rollPic(imageID, srcID) {
	imageID.src = imgArray[srcID].src;
}

function setOff(setName, key) {
	for (i=0; i<setArray[setName].length; i++) {
		setArray[setName][i].src = normArray[key].src;
	}
}

function rollOn(imageID, key) {
	imageID.src = rollArray[key].src;
}

function rollOff(imageID, key) {
	imageID.src = normArray[key].src;
}

// POPUP WINDOW

function popupWin(url, name, width, height) {
	var left = (screen.width / 2) - width / 2;
	var top = (screen.height / 2) - height / 2;
	if (arguments[4] != "")
		attributes = ',' + arguments[4];
	else
		attributes = "";
	var newPopup = window.open(url, name, "width=" + width + ", height=" + height + ", left=" + left + ", top=" + top + attributes);
	newPopup.focus();
}

function validateForm(url, name, type, div, width, height, form, message, fields) {
	fieldArray	= new Array();
	var left = (screen.width / 2) - width / 2;
	var top = (screen.height / 2) - height / 2;
	if (arguments[8] != "")
		attributes = arguments[8];
	else
		attributes = "";

	if (type == "alert")
		breakTag = "\n";
	else
		breakTag = "<br><br>";

	var message;
	doSubmit = true;
	for (i=0;i<fields.length;i+=4) {
		fieldArray[fields[i]] = fields[i + 1];
		switch (fields[i + 2])
		{
		   case "select" :
			 if (form.elements[fields[i]].selectedIndex == 0) {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
		     break;
		   case "email" :
			 if (form.elements[fields[i]].value == "") {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 else {
				var emailCheck = validateEmailAddress(form.elements[fields[i]].value);
				if (emailCheck != "") {
					message += breakTag +  emailCheck;
					doSubmit = false;
				}
			  }
			  break;
		   case "text" :
			 if (form.elements[fields[i]].value == fields[i + 3]) {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		   case "num" :
			 if (form.elements[fields[i]].value == "" || (isNumeric(form.elements[fields[i]].value) == false)) {
				message += breakTag + fields[i + 1] + " must be a number";
				doSubmit = false;
			 }
			 break;
		   case "radio" :
			 if (form.elements[fields[i]].value == null) {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		   case "checkbox" :
			 if (!form.elements[fields[i]].checked) {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		   case "textarea" :
			 if (form.elements[fields[i]].value == "") {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		  default :
		  	if (form.elements[fields[i]].value != form.elements[fields[i + 2]].value) {
				message += breakTag + fields[i + 1] + ' <i>does not match</i> ' + fieldArray[fields[i + 2]];
				doSubmit = false;
			}
		   	break;
		}
	}
	if (doSubmit) {
		return true;
	} else {
		switch (type) {
			case "div":
				popupDiv(document.getElementById(div), width, height, html_validate(message, div));
				break;
			case "div_unsubscribe":
				popupDiv(document.getElementById(div), width, height, html_disclaimerValidate(message, div));
				break;
			case "popup":
				url += '&message=' + message + '';
				var newPopup = window.open(url, name, "width=" + width + ", height=" + height + ", left=" + left + ", top=" + top + attributes);
				newPopup.focus();
				break;
			default:
				alert(message);
		}
		return false;
	}
}

function validateEmailAddress(field) {
//	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	var emailMessage = "";
	/*
	for (i=0; i<invalidChars.length; i++) {
		if (field.indexOf(invalidChars.charAt(i),0) > -1) {
			emailMessage = "\nEmail address contains invalid characters";
		}
	}
	for (i=0; i<field.length; i++) {
	   if (field.charCodeAt(i)>127) {
			emailMessage = "\nEmail address contains non ascii characters.";
		}
	}
	*/
	var atPos = field.indexOf('@',0);
	if (atPos == -1) {
			emailMessage = "\nEmail address must contain an @";
	}
	if (atPos == 0) {
			emailMessage = "\nEmail address must not start with @";
	}
	if (field.indexOf('@', atPos + 1) > - 1) {
			emailMessage = "\nEmail address must contain only one @";
	}
	if (field.indexOf('.', atPos) == -1) {
			emailMessage = "\nEmail address is incomplete";
	}
	if (field.indexOf('@.',0) != -1) {
			emailMessage = "\nInvalid email address";
	}
	if (field.indexOf('.@',0) != -1){
			emailMessage = "\nInvalid email address";
	}
	if (field.indexOf('..',0) != -1) {
			emailMessage = "\nInvalid email address";
	}
	var suffix = field.substring(field.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
			emailMessage = "\nInvalid email address";
	}
	return(emailMessage);
}

// LAYER SWITCHER

function divObject(thediv) {
	e = this;
	e.theDiv = thediv;
}

function showRemote(aDiv, timer) {
	if (window_main.showDiv && document.loaded)
		window_main.showDiv(aDiv, timer);
}

function showDiv(aDiv, timer) {
	if (timer) { closeTimer = setTimeout("closeDiv(" + aDiv + ")", timeout); }
	if (openDiv) {
		divArray[openDiv - 1].theDiv.visibility = "hidden";
	}
	divArray[(aDiv - 1)].theDiv.visibility = "visible";
	openDiv = aDiv;
	special = false;
}

function closeDiv(aDiv) {
	divArray[aDiv - 1].theDiv.visibility = "hidden";
	openDiv = 0;
}

function closeRemote() {
	if (window_main.closeAll && document.loaded)
		window_main.closeAll();
}

function closeAll() {
	if (openDiv) {
		divArray[openDiv - 1].theDiv.visibility = "hidden";
	}
	openDiv = 0;
}

function resetTimer() {
	clearTimeout(closeTimer);
}

function buildDiv(num) {
	temp = document.getElementById("popup"+num);
	tempDiv = temp.style;
	divArray[num-1] = new divObject(tempDiv);
}

function setLoaded(val) {
	parent.document.loaded = val;
}

function remote(channel) {
	switch (channel) {
		case "validateForm":
			validateForm('contact_validator.php?', 'Validate', 'popup', 700, 260, document.contactForm, '<span class="text-brown">Please enter the following details before submitting your comments:</span>', new Array('name', 'Name', 'text', 'number', 'Number', 'text', 'email', 'Email Address', 'email', 'comment', 'Comment', 'textarea'));
			break;
		default:
			alert('invalid channel ' + channel + ' - please refer to owner\'s manual or contact technical support.');	
	}
}

// DIV COMPONENTS

function popupDiv(div, left, top, content) {
	div.style.visibility = 'visible';
	div.style.left = left;
	div.style.top = top;
	div.innerHTML = content;
}

function hideDiv(div) {
	div.style.visibility = 'hidden';
}

function unhideDiv(div) {
	div.style.visibility = 'visible';
}

// AJAX

var xmlHttp;

function getUrl(page, doThis) {
	var url= page;
	xmlHttp=GetXmlHttpObject(doThis);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

function postUrl(url, doThis, sendThis) {
	xmlHttp=GetXmlHttpObject(doThis);
	xmlHttp.open("POST", url , true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(sendThis);
}

function getFormData(form) {
	str = '';
	for (i=0; i<form.elements.length; i++) {
		if (i > 0)
			str += '&';

		name = form.elements[i].name + '';

		switch(form.elements[i].type) {
			case "checkbox":
			case "radio":
				if (!form.elements[i].checked) {
					value	= '';
					break;
				}
			default:
				value	= form.elements[i].value + '';
		}

		if (name.charAt(name.length - 1) == ']')
			name = name.substr(0, name.length - 2) + '%5B%5D';

		if (value != '')
			str += name + '=' + value;
	}
	return str;
}

function GetXmlHttpObject(handler) {
	var objXmlHttp = null;

	if (window.ActiveXObject) { // IE
		try {
			if (!objXmlHttp) {
				objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		} catch (e) {
			try {
				objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					objXmlHttp = new XMLHttpRequest();
				} catch (e) {
					alert(e);
				}
			}
		}
		if (handler)
			objXmlHttp.onreadystatechange = handler;

	} else if (window.XMLHttpRequest) { // Mozilla, Safari,...
		objXmlHttp = new XMLHttpRequest();
		if (handler) {
			objXmlHttp.onload = handler;
			objXmlHttp.onerror = handler;
		}
	}

	return objXmlHttp;
}

// AJAX FUNCTIONS

function mainDiv() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("main_div").innerHTML = xmlHttp.responseText 
	} 
}

function displayMessage() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById("message_div").innerHTML = xmlHttp.responseText 
	} 
}

// NEWSLETTER

function sendNext(offset) {
	parent.document.nlFrm.current.value = offset + 1;
	parent.document.nlFrm.submit();
}
