//-----------------------------------------------------------------------------
// Copyright (c)2005-2007, Summerleaze Computer Services
// $Revision: 1.1 $
//-----------------------------------------------------------------------------

var errors = 0;
var debug  = 1;

var classname = "";
var submitting = false;

//window.onload = initMenu;

function imgWindow(img,txt) {
	w = open("",'image','width=600,height=600,toolbar=no,scrollbars=no,resizable=yes');	
	w.document.write("<HTML><HEAD><TITLE>:: ZOOM :: " + txt + "</TITLE></HEAD>");
	w.document.write("<SCRIPT type=\"text/javascript\" src=\"functions.js\"></SCRIPT>");
	w.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
	w.document.write("<IMG src='" + img.src + "' border=0>");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

function checksize() { 
	if (document.images[0].complete) {  
		window.resizeTo(document.images[0].width+5, document.images[0].height+5); 
		window.focus();
	} 
	else { 
		setTimeout('checksize()',100);
	}
}


function showpage(n) {
	var page = document.getElementById("page"+n);

	// Hide all the pages...	
	var aPages = document.getElementsByTagName("div");
	for(var i=0; i<aPages.length; i++) {
		if (aPages[i].className == "page") {
			aPages[i].style.display = "none";
		}
	}

/*
	// Add an arrow background to the selected button
	var j = 1;
	var aLinks = document.getElementsByTagName("a");
	for(var i=0; i<aLinks.length; i++) {
		//alert(i + ": " + aLinks[i].className + " - " + n);
		if (aLinks[i].className == "pagebutton") {
			alert(aLinks[i].className + " - " + aLinks[i].style.background);
			if(j==n) {
				aLinks[i].style.background = "url(images/arrow.gif) 100% 50% no-repeat";
			}
			else {
				aLinks[i].style.background = "";
			}
			++j;
		}
	}
*/

	page.style.display = "block";
}

var current_player = 0;

function next_player() {
	++current_player;
	playermenu();
}

function prev_player() {
	--current_player;
	playermenu();
}

function playermenu() {
	var aPlayers = document.getElementsByTagName("li");
	var nPlayers = aPlayers.length - 1;
	
	if(current_player<0) current_player = 0;
	if(current_player>aPlayers.length-3) current_player = nPlayers-2;
	
	for(var i=0; i<aPlayers.length; i++) {
		aPlayers[i].style.display = "none";
		if(i>=current_player && i<current_player+3) {
			aPlayers[i].style.display = "block";
		}
	}
}

function showplayer(el) {
	var aPlayers = document.getElementsByTagName("li");

	// use first <li> if none is specified
	if(el == undefined) el = aPlayers[0];
	
	for(var i=0; i<aPlayers.length; i++) {
		// reset all the players photo's to B/W
		aPlayers[i].firstChild.src = aPlayers[i].firstChild.src.replace(/(\w+)_over.jpg/, "$1.jpg");
		if(el == aPlayers[i]) {
			// Build list of all the player pages			
			var aPages = document.getElementById("player-pages").childNodes;
			var j;
						
			// Hide them all
			for(j=0; j<aPages.length; j++) {
				if(aPages[j].nodeName == "DIV") aPages[j].style.display = "none";
			}
			
			// Display the selected player's page
			var k = 0;
			for(j=0; j<aPages.length; j++) {
				if(aPages[j].nodeName == "DIV") {
					if(k == i) {
						aPages[j].style.display = "block";
					}
					++k;
				}				
			}
		}
	}
	// make selected player's photo colour
	el.firstChild.src = el.firstChild.src.replace(/(\w+).jpg/, "$1_over.jpg");
}



function setimg(o, i) {
	//alert(i);
	o.src = i;
}

function nav(v) {
	var view = document.getElementById('view');
	
	view.value = v;
	document.form.submit();
}

function doHover(el) {
	el.className = "nav_hover";
}

function noHover(el) {
	el.className = "";
}

function submitForm() {
	submitting = true;
	document.form.submit();
}

function backTo(page) {
	if (!submitting) {
		window.location = page.length > 0 ? page : "diary.php";
	}
	submitting = false;
}

function gotoPage(page) {
	window.location = page;
}

function showForm(form, params) {
	window.location = form + params;
}

function changeClass(el, sClass) {
	el.className = sClass;
}

function initMenu() {
	if (document.all && document.getElementById) {
		var navRoot = document.getElementById('dmenu');
		if (navRoot) {
			for(i=0; i<navRoot.childNodes.length; i++) {
				var node = navRoot.childNodes[i];
				if (node.nodeName == 'LI') {
					node.onmouseover = function() { this.className += ' over'; };
					node.onmouseout  = function() { this.className = this.className.replace(' over', ''); };
				}
			}
		}
	}
}

function menuOver(el) {
	for (i=0; i<el.childNodes.length; i++) {
		node = el.childNodes[i];
		if (node.nodeName=="LI") {
			node.className = "over";
		}
	}
}

function menuOut(el) {
	for (i=0; i<el.childNodes.length; i++) {
		node = el.childNodes[i];
		if (node.nodeName=="LI") {
			node.className = "";
		}
	}
}

function toggleMenu(el) {
	if (document.getElementById) {
		var mStyle = document.getElementById(el).style;
		if (mStyle.display == "block") {
			mStyle.display = "none";
		}
		else {
			mStyle.display = "block";
		}
		return false;
	}
	else {
		return true;
	}
}

function confirmChk(elem) {
	var chkbox = 0;

	// figure out which boxes are ticked
	for (var i=0; i<document.form.length; i++) {
		var el = document.form.elements[i];

		if (el.type == 'checkbox' && el.checked === true) {
			chkbox++;
		}
	}

	// Set value of a hidden input element - gives us a POST variable to trigger the operation
	switch(elem.name) {
		case 'delete':
			var msg = "Are you sure you want to delete the " + 
				(chkbox == 1 ? "" : chkbox + " ") + "selected " + 
				(chkbox == 1 ? "entry" : "entries");	
			if (chkbox > 0 && confirm(msg)) {
				document.getElementById('operation').value = 'delete';
				submitForm();
			}
			break;
		case 'edit':
			document.getElementById('operation').value = 'edit';
			submitForm();
			break;
		default:
			document.getElementById('operation').value = '';
			submitForm();
			break;
	}
}

function confirmDelete() {
	var chkbox = 0;
	var valid = true;
			
	for (var i=0; i<document.form.length; i++) {
		var el = document.form.elements[i];

		if (el.type == 'checkbox' && el.checked === true) {
			chkbox++;
		}
	}

	if (chkbox > 0 && confirm("Are you sure you want to delete the selected entries")) {
		// Set value of a hidden input element - gives us a POST variable to trigger the deletion(s)
		document.getElementById('delete').value = 'delete';
		submitForm();
	}
}

function confirmAcknowledge() {
	var chkbox = 0;
	var valid = true;
			
	for (var i=0; i<document.Form.length; i++) {
		var el = document.form.elements[i];

		if (el.type == 'checkbox' && el.checked === true) {
			chkbox++;
		}
	}

	if (chkbox > 0 && confirm("Are you sure you want to acknowledge the selected entries")) {
		// Set value of a hidden input element - gives us a POST variable to trigger the Acknowledgment(s)
		document.getElementById('acknowledge').value = 'acknowledge';
		submitForm();
	}
}

function sendEmail(el) {
	var email = prompt("Please enter the email address of the recipient(s)\n(seperate entries with a comma)");
	var isValid = false;
	
	if (email !== null) {
		if (email.indexOf(',') != -1) {
			isValid = validateEmailList(email);
		}
		else {
			isValid = validateEmail(email);
		}
		if (isValid) {
			el.href += "?email=" + email;
		}
	}
}

function showTitle(elem) {
	var pattern = /^(\w+)_(\d+)$/;

	if (pattern.test(elem.id)) {
		var name    = RegExp.$1;
		var journey = RegExp.$2;
	}
	else {
		alert("No Pattern Match!");
	}

	var limit = parseInt(document.getElementById('limit_'+elem.id).value);
	var s = '';
	switch(name) {
	case 'wheelchairs':
		s = 'Passengers with Wheelchairs';
		break;
	case 'bicycles':
		s = 'Passengers with Bicycles';
		break;
	case 'total':
	default:
		s = 'Passengers';
		break;
	}
	
	elem.title = isNaN(limit) ? '' : s + ' (Limit ' + limit + ')';
}

// Displays all the form's elements...
function showDOM() {
	for (var i=0; i<document.party.length; i++) {
		var el = document.party.elements[i];
			
		document.getElementById('debug').innerHTML += "el[" + i + "] => " + el.type + ", " + el.id + "<br />";
	}
}

// toggle the 'disabled' state for each argument
function toggleElement() {
	var state = (arguments[0].value.length === 0);
	for(i=1; i<arguments.length; i++) {
		document.getElementById(arguments[i]).disabled = state;
	}
}

function disableElement() {
	for(i=0; i<arguments.length; i++) {
		document.getElementById(arguments[i]).disabled = true;
	}
}

function enableElement() {
	for(i=0; i<arguments.length; i++) {
		document.getElementById(arguments[i]).disabled = false;
	}
}

//**************************************************************************************************
// Toggle the state of a panel
//**************************************************************************************************
function panelToggle(elem) {
	var i = 0;
	
	if (elem.className == "collapsed") {
		elem.className = "expanded";
	}
	else {
		elem.className = "collapsed";
	}
	// Expand / Collapse the list
	lists = document.getElementsByTagName('ul');
	if (lists.length > 0) {
		for (i = 0; i < lists.length; i++) {
			el = lists[i];
			if (el.id == elem.name) {
				if (el.className == "collapsed") {
					el.className = "expanded";
				}
				else {
					el.className = "collapsed";
				}
			}
		}
	}
	divs = document.getElementsByTagName('div');
	if (divs.length > 0) {
		for (i = 0; i < divs.length; i++) {
			el = divs[i];
			if (el.id == elem.name) {
				if (el.className == "collapsed") {
					el.className = "expanded";
				}
				else {
					el.className = "collapsed";
				}
			}
		}
	}
}

//**************************************************************************************************
// Useful Debugging Functions
//**************************************************************************************************

function showValue(el) {
	el.title = el.name + ' = ' + el.value;
}

function showClass(el) {
	el.title = el.id + ' = ' + el.className;
}

//**************************************************************************************************
// Macromedia functions...
//**************************************************************************************************

function _preload() {
  var d=document; 
  
  if(d.images){ 
    for(arg in _preload.arguments) {
    	if (arg.indexOf("#") !== 0) { 
    		var img = new Image();
    		img.src = arg;
    	}
    }
  }
}

/*
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; 
  
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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];
    	}
    }
  }
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  
  document.MM_sr = new Array; 
  
  for(i=0;i<(a.length-2);i+=3) {
  	if ((x=MM_findObj(a[i]))!==null) {
  		document.MM_sr[j++]=x; 
  		if(!x.oSrc) x.oSrc=x.src; 
  		x.src=a[i+2];
  	}
  }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
*/