// 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
 * 	width: width of the swf
 * 	height: height of the swf
 */
 
function writeFlash(src, width, height, alternateSrc, redirect) {
	if ((flashinstalled == 2) && (flashversion > 5)) {
		document.write('<object width="' + width + '" height="' + height + '" id="viewer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0">');
		document.write('<param name=movie value="' + src + '">');
		document.write('<param name="quality" value="high">');
		document.write('<param name="wmode" value="transparent">');
		document.write('<embed name="viewer" wmode="transparent" swliveconnect="true" 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 + '" alt="" border="0" usemap="#nav">');
	 	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;
		}
	}
}

// TEXTBOX FOCUS HANDLERS
function clearText(object, val) {
	if (object.value == val)
		object.value = "";
}

function restoreText(object, val) {
	if (object.value == "")
		object.value = val;
}

function setText(object, val) {
	object.value = val;
}

// 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, width, height, form, message, fields) {
	var left = (screen.width / 2) - width / 2;
	var top = (screen.height / 2) - height / 2;
	if (arguments[8] != "")
		attributes = arguments[8];
	else
		attributes = "";

	if (type == "popup")
		breakTag = "<br><br>";
	else if (type == "alert")
		breakTag = "\n";
		
	var message;
	doSubmit = true;
	for (i=0;i<fields.length;i+=3) {
		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 == "") {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		   case "radio" :
			 if (form.elements[fields[i]].value == null) {
				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 (doSubmit) {
		form.submit();
	} else {
		if (type == "popup") {
			url += '&message=' + message + '';
			var newPopup = window.open(url, name, "width=" + width + ", height=" + height + ", left=" + left + ", top=" + top + attributes);
			newPopup.focus();
		} else if (type == "alert")
			alert(message);
	}
}

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.');	
	}
}

// AJAX

var xmlHttp;

function getUrl(page, doThis) {
	var url= page;
	xmlHttp=GetXmlHttpObject(doThis);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

function GetXmlHttpObject(handler) {
	var objXmlHttp=null
	
	if (navigator.userAgent.indexOf("Opera")>=0) {
		alert("This example doesn't work in Opera") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0) { 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
			strName="Microsoft.XMLHTTP"
		} 
		try { 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} catch(e) { 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0) {
		objXmlHttp=new XMLHttpRequest()
		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 
	} 
}
