/***************************************************
	Commands to run immediately
***************************************************/
//addEvent(window, 'load', ShowDIVBordersOnMouse);


/***************************************************
	Email validation
***************************************************/
function ValidateEmail(strEmail) {
	var blnValid, re = /^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+$/ig;
	blnValid = re.test(strEmail);
	if (!blnValid)
		alert('Please enter a valid email address');
	return blnValid;
}



/***************************************************
	Photo Gallery
***************************************************/
var slideshowInitialised = false;
var totpics;
var picnum;
var totthumbs;
var filenamearray;
var thumboffset;
var thumbstart,thumbfin;	
var theTimeout;
var do_autorun;
var autoruninterval;
var imagepath;

function initialiseSlideshow(intPicCount, intThumbCount)  {
	filenamearray = document.getElementById('PictureList');
	totpics = intPicCount;
	totthumbs = Math.min(intThumbCount, totpics);
	picnum = 1;
	thumboffset = 0;
	thumbstart = 1;
	thumbfin = totthumbs;
	do_autorun = 0;
	autoruninterval = 4000;
	imagepath = '/images/photos/';
	slideshowInitialised = true;
}

// switch autorun on and off by clicking on start/stop button
function toggle_autorun() {
	do_autorun = 1 - do_autorun;
	if (do_autorun == 1) { 
		autorun(); 
	} else {
		stop_autorun();
	}
}

// run the slideshow !
function autorun()   {
	change_pic("next",picnum); 
	theTimeout = setTimeout("autorun();", autoruninterval);
	autorunbuttons();
}

// func needed when user clicks on main pic to stop autorun!
function stop_autorun()  {
	do_autorun = 0;
	clearTimeout(theTimeout);
	autorunbuttons();
}
function autorunbuttons() {
	var objPlay = document.getElementById('autorunplay');
	var objStop = document.getElementById('autorunstop');
	
	objPlay.style.display = (do_autorun == 1) ? 'none' : 'block';
	objStop.style.display = (do_autorun == 0) ? 'none' : 'block';
}

function change_pic(action, thepicnum)   {
	if (!slideshowInitialised) return;

	// deselect all buttons for old pic and hide old pic 
	var objCaption = document.getElementById('caption' + picnum);
	if (objCaption) objCaption.style.display = 'none';
	
	var objNumber = document.getElementById('bottom' + picnum);
	if (objNumber) objNumber.className = 'slide-number';

	// take offset into account for thumbnail and bracket display
	ipicnum = picnum - thumboffset;
	var objThumbFrame = document.getElementById('thumbframe' + ipicnum);
	if (objThumbFrame) objThumbFrame.className = 'thumb-frame';
	
	// generate new picnum
	switch(action) {
		case "prev" : picnum--; break;
		case "num"  : picnum = thepicnum; break;
		case "thumb": picnum = thepicnum + thumboffset; break;
		case "next" : picnum++;
	}
	if (picnum > totpics) picnum = 1;
	if (picnum < 1) picnum = totpics;

	// hide the 'prev' and 'next' buttons if necessary
	/*
	var objPrev = document.getElementById('prevbutton');
	var objNext = document.getElementById('nextbutton');
	objPrev.style.visibility = (picnum == 1) ? 'hidden' : 'visible';
	objNext.style.visibility = (picnum == totpics) ? 'hidden' : 'visible';
	*/
	
	// repopulate thumb images if new pic number is beyond current scope			
	if (picnum > thumbfin || picnum < thumbstart)   {
		if (picnum > thumbfin)   {
			thumbfin = picnum;
			thumbstart = picnum - (totthumbs - 1);
		} else if (picnum < thumbstart)   {
			thumbstart = picnum;
			thumbfin = picnum + (totthumbs - 1);
		}
		
		thumboffset = thumbstart - 1;
		for (x = 1; x <= totthumbs; x++) {
			newURL = imagepath + 'st_' + filenamearray.options[x + thumboffset - 1].text;
			var objThumb = document.getElementById('thumb' + x);
			if (objThumb) objThumb.src = newURL;
		}
	}

	var objMainPic = document.getElementById('mainpic');
	if (objMainPic) objMainPic.src= imagepath + 'ss_' + filenamearray.options[picnum-1].text;

	var objBottom = document.getElementById('bottom' + picnum);
	if (objBottom) objBottom.className = 'slide-number-current';

	objCaption = document.getElementById('caption' + picnum);
	if (objCaption) objCaption.style.display = 'block';

	ipicnum = picnum - thumboffset;

	objThumbFrame = document.getElementById('thumbframe' + ipicnum);
	if (objThumbFrame) objThumbFrame.className = 'thumb-frame-current';
}

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];}} }




/***************************************************
	Text field focus
***************************************************/
function AddTextFieldFocusBehaviour() {
	var a = new Array();

	for (var i = 0; i < a.length; i++) {
		var el = document.getElementById(a[i]);
		if (el) {
			addEvent(el, 'focus', TextFieldFocus);
			addEvent(el, 'blur', TextFieldBlur);
		}
	}
}

function TextFieldFocus(e) {
	var txt = SourceElement(e)

	if (txt && txt.value == txt.title) {
		txt.value = '';
	}
}
function TextFieldBlur(e) {
	var txt = SourceElement(e)

	if (txt && txt.value == '') {
		txt.value = txt.title;
	}
}



/***************************************************
	Popup support functions
***************************************************/
function PopupClick() {
	var aClasses;
	aClasses = this.className.toString().split(' ');
	
	for (var i = 0; i < aClasses.length; i++) {
		switch(aClasses[i]) {
		case 'popupLogo':
			popupLogoInfo(aClasses[i]); break;
		default:
			return false;
		}
	}
}

function popupnew(URL,winwidth,winheight,scroll, resizeable)  {
	nwinheight=Number(winheight);	
	day = new Date();
	id = day.getTime();
	pleft=(screen.width-winwidth)/2;
	ptop=(screen.height-nwinheight)/2;
	eval("page" + id + " = window.open('"+URL+"', '" + id + "', 'toolbar=0,scrollbars="+scroll+",location=0,statusbar=0,menubar=0,resizable="+resizeable+",width="+winwidth+",height="+nwinheight+",left="+pleft+",top="+ptop+"');");
	}


function popup(url, title, width, height, scroll) {
	var s = 'menubar=no,toolbar=no,location=yes,resizable=yes,status=yes,dependent=yes'
	s += ',scrollbars=' + scroll
	s += ',width=' + width + ',height=' + height;
	s += ',top=' + (screen.availHeight - height) / 2 + ',left=' + (screen.availWidth - width) / 2;

	//close existing window
	try {
		if (navigator.newwindow && navigator.newwindow.close) navigator.newwindow.close();
	}
	catch(e) {}
	
	//save new window object in navigator object and activate
	navigator.newwindow = self.open(url, title, s);
	navigator.newwindow.focus();
	
	return false;
}

function popupLogoInfo(strTarget) {
	return popup('', strTarget, 600, 400, 'yes')
}

function CloseWindow() {
	window.close();
	navigator.newwindow = null;
	if (window.opener)
		if (!window.opener.closed)
			window.opener.focus();
}

function addPopupEvents() {
	document.onkeydown = KeyDown;
	
	if (window.self.location != window.top.location) {
		addEvent(window, 'load', HideCloseLinks);
	}
}

function HideCloseLinks() {
	var objCloseLink;
	objCloseLink = document.getElementById('close-top');
	if (objCloseLink) objCloseLink.style.display = 'none';
	objCloseLink = document.getElementById('close-bottom');
	if (objCloseLink) objCloseLink.style.display = 'none';
}

function KeyDown(e) {
	if (e && e.which == 27) CloseWindow(); //Netscape
	else if (event && event.keyCode == 27) CloseWindow(); //IE
}


/***************************************************
	DIV borders showing on mouseover for testing
***************************************************/

function ShowDIVBordersOnMouse() {
	addEvent(document.body, 'mouseover', MouseOver);
	addEvent(document.body, 'mouseout', MouseOut);
}
function MouseOver(e) {
	var el
	if (e && e.target) el = e.target;
	else el = event.srcElement;
	if (el && el.tagName == 'DIV') {
		el.style.border = 'solid 1px green';
		window.status = 'id: ' + el.id + '; class: ' + el.className;
	}
}
function MouseOut(e) {
	var el
	if (e && e.target) el = e.target;
	else el = event.srcElement;
	if (el && el.tagName == 'DIV') {
		el.style.border = 'none';
		window.status = '';
	}
}


/***************************************************
	Support for adding multiple events to an object
***************************************************/
function addEvent(obj, evType, fn) {
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else {
		var r = fallbackAddEventListener(obj, evType, fn);
		return r
	}
	return false;
}

var Listeners;

function fallbackAddEventListener(obj, evType, fn) {
	var l = GetListener(obj, evType);

	if (!l) {
		l = new Listener(obj, evType);
		var intListener = Listeners.length;
		Listeners[intListener] = l;
		obj.listener = l;
		eval('obj.on' + evType + ' = function() {CallListeners(' + intListener + ')}');
	}

	if (l) {
		l.functions[l.functions.length] = fn;
		return true;
	}

	return false;
}

function GetListener(obj, evType) {
	var l, strName;
	
	if (!Listeners) Listeners = new Array();
	
	strName = ListenerName(obj, evType);
	
	for (var i = 0; i < Listeners.length; i++) {
		if (Listeners[i].name == strName) {
			l = Listeners[i];
			break;
		}
	}
	
	return l;
}

function Listener(obj, evType) {
	this.name = ListenerName(obj, evType);
	this.functions = new Array();
}

function ListenerName(obj, evType) {
	return '__' + obj.tagName + '_' + obj.id + '_' + obj.name + '_' + evType;
}

function CallListeners(intListener) {
	var l;
	
	l = Listeners[intListener];
	
	if (l) {
		for (var i = 0; i < l.functions.length; i++) {
			var func = l.functions[i];
			func();
		}
	}
}


function SourceElement(e) {
	var el
	if (e && e.target) el = e.target;
	else el = event.srcElement;
	return el;
}


/***************************************************
	showing / hiding rows
***************************************************/
function ShowHideDiv(divName,blnCloseOthers){
	
	//hide all divs
	if (blnCloseOthers){
		var arrayMenuitems = getElementsWithClass('*','showrow')
		var i;
		for ( i = 0; i < arrayMenuitems.length; i++ ){
			//close them all
			arrayMenuitems[i].className='hiderow';
		}	
	}	
	//show passed div
	if (divName!=''){
		document.getElementById(divName).className='showrow';
	}

}

function getElementsWithClass (tagName,className) {
  var all = document.all ? document.all : document.getElementsByTagName(tagName);
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
    if (all[e].className == className) elements[elements.length] = all[e];
  return elements;
}


function CheckPrizeForm(theform)   {
	var msg="";
	if (theform.surname.value=="") msg="Your Name is missing..\n";
	email=theform.email.value;
	if (email==""||((email.indexOf("@") == -1)||email.indexOf(".") == -1)) { msg=msg+"Your Email Address is either missing or in an invalid format...\n"; }
	if (msg!="")  {
		alert("Please correct the following:\n\n" + msg);
		return false;
		}
	else return true;
	}
