function trace (s) {
	if (!$('debug')) {
		var o = document.createElement('pre');
		o.id = 'debug';
		o.style.fontSize = '10pt';
		o.style.position = 'absolute';
		o.style.top = '0px';
		o.style.right = '0px';
		o.style.textAlign = 'right';
		o.style.color = '#fff';
		document.body.appendChild(o);
	}
	$('debug').innerHTML += s + '<br >';
	if (typeof(console) != 'undefined') {
		console.log(s);
	}
}

document.write('<style type="text/css">');
document.write('	#RightBanner img { visibility: hidden }');
document.write('	#LongBanner img { visibility: hidden }');
document.write('</style>');


Event.observe(window, 'load', function (evt) {
	var es = document.getElementsByClassName('images');
	var linksContainer = (es.length > 0) ? es[0] : null;
	var e = $('BannerCaption');
	if (e.getElementsByTagName('span').length > 0) {
		e = e.getElementsByTagName('span')[0];
	}

	if (randomImages && randomImages.length > 0) {
		//randomizeImage.init(randomImages, $('LongBanner') || $('RightBanner'), e);
	}
	var is = new ImageSet(linksContainer, $('LongBanner') || $('RightBanner'), e, {duration: 500});

	$A(document.getElementsByClassName('consultbutton')).each(function (e) {
		var formLinks = new Array();
		if (e.nodeName.toLowerCase() == 'a') {
			formLinks.push(e);
		}
		if (e.getElementsByTagName('a').length > 0) {
			formLinks.push(e.getElementsByTagName('a')[0]);
		}
		if ((formLinksLength = formLinks.length) > 0) {
			var i;
			for (i = 0; i < formLinksLength; i++) {
				formLinks[i].observe('click', function (ev) {
					if (ev) {
						Event.stop(ev);
					}
					var path = (window.location.href.indexOf('/html/') != -1) ? '../' : '';
					Popup.load(path + 'form.html');
				});
			}
		}
	});
});


Popup = {
	// moo.fx version
	overlay: 'popoverlay',
	outerCont: 'popouter',
	innerCont: 'popinner',
	intrans:	false,
	fadeDuration:	200, // milliseconds
	resizeDuration:	700, // milliseconds
	created:	false,
	content:	null,
	overlayOpacity: 0.8, // percent opaque
	create:	function () {
		var overlay = document.createElement('div');
		overlay.setAttribute('id', Popup.overlay);
		overlay = $(overlay);
		overlay.onclick = function (event) {
			Popup.hide();
		}
		overlay.setStyle({
			opacity: '0.7',
			position: 'absolute',
			background: '#000',
			left: '0px',
			top: '0px',
			width: '100%',
			height: '100%',
			display: 'none'
		});		

		var outer = document.createElement('div');
		outer.setAttribute('id', Popup.outerCont);
		outer = $(outer);
		outer.onclick = function (event) {
			Popup.hide();
		}
		outer.setStyle({
			position: 'absolute',
			top: '30px',
			width: '100%',
			textAlign: 'center',
			left: '0px',
			display: 'none'
		});

		var inner = document.createElement('div');
		inner.setAttribute('id', Popup.innerCont);
		inner = $(inner);
		inner.onclick = function (event) {
			Popup.cancelBubble(event);
		}
		inner.setStyle({
			marginRight: 'auto',
			marginLeft: 'auto',
			textAlign: 'left'
		});

		var sizes = Popup.getSizes();
		overlay.style.height = sizes.page.height + 'px';
		if (/msie/gi.test(navigator.userAgent.toLowerCase())) {
			overlay.style.width = sizes.page.width + 'px';
		}
		outer.appendChild(inner);

		document.getElementsByTagName('body')[0].appendChild(overlay);
		document.getElementsByTagName('body')[0].appendChild(outer);

		Popup.overlay = document.getElementById(Popup.overlay);
		Popup.outerCont = document.getElementById(Popup.outerCont);
		Popup.innerCont = document.getElementById(Popup.innerCont);

		Popup.created = true;
	},
	fadeInContent: function () {
		var content = Popup.innerCont.getElementsByTagName('div')[0];
		content = $(content);
		content.setStyle({
			position: '',
			visibility: 'vivisble',
			opacity: '0'
		});
	},
	showContent: function() {
		// resize the box

		var content = document.createElement('div');
		content = $(content);
		content.setStyle({
			position: 'absolute',
			visibility: 'hidden'
		});

		var str = Popup.oXML.responseText;
		var path = (window.location.href.indexOf('/html/') != -1) ? '../' : '';
		str = str.replace(/images\//, path + 'images/');
		content.innerHTML = str;

		// append the content to the document and get it's size
		document.getElementsByTagName('body')[0].appendChild(content);

		var oldDims = Popup.getDimensions(Popup.innerCont);
		var newDims = Popup.getDimensions(content);

		// remove the content from the document
		document.getElementsByTagName('body')[0].removeChild(content);

		// change the boxes height
		var heightFx = new Fx.Style(Popup.innerCont, 'height', {
			duration: Popup.resizeDuration,
			onComplete: function () {
				var widthFx = new Fx.Style(Popup.innerCont, 'width', {
					duration: Popup.resizeDuration,
					onComplete: function () {
						content.setStyle({
							position: '',
							visibility: 'hidden',
							width: newDims.width + 'px',
							height: newDims.height + 'px'
						});
						// append the content
						Popup.innerCont.appendChild(content);

						var fadeFx = new Fx.Style(content, 'opacity', {
							duration: Popup.fadeDuration,
							onComplete: function () {
								Popup.intrans = false;
							}
						});
						fadeFx.custom(0, 1);
					}
				});
				widthFx.custom(oldDims.width, newDims.width);
			}
		});
		Popup.delContent();
		heightFx.custom(oldDims.height, newDims.height);
	},
	showLoading: function () {
	    var con = Popup.xhcon();
	    if (!con) {
			return;
		}

		// show the loading gif
		var img = document.createElement('img');
		img = $(img);
		img.setStyle({margin: '30px'});
		img.setAttribute('src', Popup.path + 'images/loading.gif');
		Popup.delContent();
		Popup.innerCont.setStyle({
			width: '92px',
			height: '92px',
			background: '#fff'
		});
		Popup.innerCont.appendChild(img);
		Popup.outerCont.setStyle({visibility: 'visible', display: 'block'});

		con.connect(Popup.href, 'GET', Math.random(), function (oXML) {
			Popup.oXML = oXML;
			setTimeout('Popup.showContent()', 1000); // delay by 1 second, this is done on purpose
		});
	},
	showOverlay: function () {
		Popup.overlay.style.display = 'block';
		Popup.overlay.style.visibility = 'hidden';
		var overlayFx = new Fx.Style(Popup.overlay, 'opacity', {
			duration: Popup.fadeDuration,
			onComplete: function () {
				Popup.showLoading();
			}
		});
		overlayFx.custom(0, Popup.overlayOpacity);
	},
	show:	function () {
		if (!Popup.created) {
			Popup.create();
		}

		if (Popup.intrans) {
			return false;
		}

		var path = (window.location.href.indexOf('/html/') != -1) ? '../' : '';
		Popup.path = path;

		Popup.intrans = true;
		Popup.showOverlay();
	},
	hide:		function () {
		if (Popup.intrans) {
			return;
		}

		Popup.intrans = true;
		var overlayFx = new Fx.Style(Popup.overlay, 'opacity', {
			duration: Popup.fadeDuration * 2,
			onComplete: function () {
				Popup.intrans = false;
			}
		});
		var contentFx = new Fx.Style(Popup.innerCont, 'opacity', {
			duration: Popup.fadeDuration,
			onComplete: function () {
				Popup.outerCont.setStyle({display: 'none'});
				Popup.innerCont.setStyle({visibility: 'visible', opacity: '1'});
			}
		});
		Popup.delContent();
		contentFx.custom(1, 0);
		overlayFx.custom(Popup.overlayOpacity, 0);
		return false;
	},
	delContent:	function () {
		Popup.innerCont.innerHTML = '';
	},
	load:	function (href) {
		Popup.href = href;
		Popup.show();
	},
	resized:	function () {
		if (Popup.created && /msie/gi.test(navigator.userAgent.toLowerCase())) {
			var sizes = Popup.getSizes();
			overlay.style.height = sizes.page.height + 'px';
			overlay.style.width = sizes.page.width + 'px';
		}
	},
	cancelBubble:	function (eObj) {
		if (!eObj) var eObj = window.event;
		if (!eObj) return;
		eObj.cancelBubble = true;
		if (eObj.stopPropagation) {
			eObj.stopPropagation();
		}
	},
	getSizes: function () {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		}
		else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		}
		else {
			pageWidth = xScroll;
		}

		return { page: {width: pageWidth, height: pageHeight}, window: {width: windowWidth, height: windowHeight }};
	},
	getDimensions: function(element) {
		if (!element) {
			return false;
		}
		if (element.style.display != 'none') {
			return {width: element.offsetWidth, height: element.offsetHeight};
		}

		// All *Width and *Height properties give 0 on elements with display none,
		// so enable the element temporarily
		var els = element.style;
		var originalVisibility = els.visibility;
		var originalPosition = els.position;
		els.visibility = 'hidden';
		els.position = 'absolute';
		els.display = '';
		var originalWidth = element.clientWidth;
		var originalHeight = element.clientHeight;
		els.display = 'none';
		els.position = originalPosition;
		els.visibility = originalVisibility;
		return {width: originalWidth, height: originalHeight};
	},
	xhcon:		function () {
		var xmlhttp, bComplete = false;
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e) { try { xmlhttp = new XMLHttpRequest(); }
		catch (e) { xmlhttp = false; }}}
		if (!xmlhttp) {
			return null;
		}
		this.connect = function(sURL, sMethod, sVars, fnDone) {
			if (!xmlhttp) {
				return false;
			}
			bComplete = false;
			sMethod = sMethod.toUpperCase();
	
			try {
				if (sMethod == "GET") {
					xmlhttp.open(sMethod, sURL+"?"+sVars, true);
					sVars = "";
				}
				else {
					xmlhttp.open(sMethod, sURL, true);
					xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
					xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				}
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && !bComplete) {
						bComplete = true;
						fnDone(xmlhttp);
					}
				};
				xmlhttp.send(sVars);
			}
			catch(z) { return false; }
			return true;
		};
		return this;
	}
}


var ImageSet = Class.create();
ImageSet.prototype = {
	initialize: function (linksContainer, imageContainer, bannerContainer, options) {
		this.imageContainer = $(imageContainer);
		this.bannerContainer = $(bannerContainer);

		if (!this.imageContainer) {
			return false;
		}

		this.options = Object.extend({
			duration: 200
		}, options || {});

		this.eventImageLoaded = this.imageLoaded.bindAsEventListener(this);

		// fade in the current image
		var es = $A(this.imageContainer.getElementsByTagName('img'));
		if (es.length < 1) {
			return;
		}
		var i1 = $(es[es.length - 1]);
		i1.setStyle({
			position: 'absolute',
			top: '0px',
			left: '0px'
		});
		this.showImage(i1);

		// attach events for each of links
		linksContainer = $(linksContainer);
		if (!linksContainer) {
			return false;
		}
		this.links = $A(linksContainer.getElementsByTagName('a'));

		this.eventClick = this.click.bindAsEventListener(this);
		var is = this;
		this.links.each(function (e) {
			var imgPreload;
			e = $(e);
			Event.observe(e, 'click', is.eventClick);
			//preload images
			imgPreload = new Image();
			imgPreload.src = e.href;
		});
	},
	showImage: function (e) {
		var is = this;
		e = $(e);

		this.inmotion = false;

		// fade it in
		var showFx = new Fx.Style(e, 'opacity', {
			duration: this.options.duration,
			onComplete: function () {
				is.inmotion = false;
			}
		});
		showFx.custom(0, 1);
	},
	imageLoaded: function (i2, a) {
		var is = this;
		a = $(a);

		// insert the new img
		this.imageContainer.appendChild(i2);

		// change the link classes
		this.links.each(function (e) {
			$(e).removeClassName('on');
		});

		if (a) {
			a.addClassName('on');
		}

		this.showImage(i2);

		// change the banner caption
		this.bannerContainer.innerHTML = a.getAttribute('title');
	},
	click: function(ev) {
		Event.stop(ev);
		var a = Event.element(ev);

		if (this.inmotion == true) {
			return;
		}
		this.inmotion = true;

		// get the image to hide
		var es = $A(this.imageContainer.getElementsByTagName('img'));
		if (es.length < 1) {
			return;
		}
		var i1 = $(es[es.length - 1]);
		i1.setStyle({
			position: 'absolute',
			top: '0px',
			left: '0px'
		});

		// cleanse
		var is = this;
		var children = $A(this.imageContainer.childNodes);
		children.each(function (e) {
			e = $(e);
			if (e != i1) {
				is.imageContainer.removeChild(e);
			}
		});

		// get the image to show
		var i2 = $(document.createElement('img'));
		Event.observe(i2, 'load', function (ev) {
			is.imageLoaded(i2, a);
		});
		i2.setStyle({
			visibility: 'hidden',
			position: 'absolute',
			top: '0px',
			left: '0px'
		});
		i2.setAttribute('src', a.getAttribute('href'));
	}
}

var randomizeImage = {
	init: function(rdImages, imgParent, captionEl) {
		imgParent = $(imgParent);
		captionEl = $(captionEl);

		var nbOfImages = rdImages.length;
		var rand = Math.round(Math.random() * (nbOfImages - 1));

		while (imgParent.childNodes.length > 0) {
			imgParent.removeChild(imgParent.childNodes[0]);
		}
		while (captionEl.childNodes.length > 0) {
			captionEl.removeChild(captionEl.childNodes[0]);
		}
	
		var img = document.createElement('img');
		img.setAttribute('src', rdImages[rand].src);
		imgParent.appendChild(img);

		captionEl.innerHTML = rdImages[rand].caption;
		

		/*
		imgParent = $(imgParent);
		captionEl = $(captionEl);
		var nbOfImages = rdImages.length;
		var rand = Math.round(Math.random() * (nbOfImages - 1));
		$imgs = imgParent.getElementsByTagName('img');
		$imgs[0].src = rdImages[rand].src;
		captionEl.innerHTML = rdImages[rand].caption;
		*/
	}
}

function checkForm() {
	var errorMsg;
	var badFields = new Array();
	
	if (document.getElementById('first_name').value == '') {
		badFields.push({id: 'first_name', name: 'first name'});
	}
	if (document.getElementById('surname').value == '') {
		badFields.push({id: 'surname', name: 'surname'});
	}
	if (document.getElementById('email').value == '') {
		badFields.push({id: 'email', name: 'email'});
	}
	
	var badFieldsLength = badFields.length;
	if (badFieldsLength > 0) {
		errorMsg = 'The following fields are required:\n';
		for (i = 0; i < badFieldsLength; i++) {
			errorMsg += '- ' + badFields[i].name + '\n';
		}
		// damn FF bug 236791
		document.getElementById(badFields[0].id).setAttribute('autocomplete', 'off');
		document.getElementById(badFields[0].id).focus();
		document.getElementById(badFields[0].id).setAttribute('autocomplete', '');
		alert(errorMsg);
		return false;
	}
	else {
		return true;
	}
}