function formFocus(theForm, theCurrent, theDesired) {
	if (theForm.value == theCurrent) {
		theForm.value = theDesired;
	}
}

function ajaxLogin(username, password) {
	if (username.value == '' || password.value == '') {
		alert("Please enter your \"Username\" and \"Password\"");
	} else {
		$('right-login-text').innerHTML = "<p>Please wait while your login details are validated.</p><p class=\"align_center\"><img src=\"http://www.the-dma.org.uk/files/design/images/ajax-loader.gif\" /></p>";
		$('right-login-text').className = 'show';
		$('login-form').className = 'hide';
		new Ajax.Request('http://www.the-dma.org.uk/custom_asp/omlogin.asp', {
			method: 'post',
			parameters: { username: username.value, password: password.value },
			onComplete: function(transport) {
				var response = transport.responseText || "no response text";
				if (response == "Success") {
					$('right-login-text').innerHTML = "<p>You have been successfully logged in.</p><p><a href=\"http://www.the-dma.org.uk/custom_asp/omlogout.asp?response-page=" + window.location.href + "\" title=\"Logout\">Click here</a> to logout of the system. <a href=\"http://membership.the-dma.org.uk/organisation.aspx\" title=\"Change Details\">Click here</a> to change your details.</p>";
				} else {
					$('right-login-text').className = 'hide';
					$('login-form').className = 'show';
					alert("Login failed, your \"Username\" or \"Password\" is incorrect, please try again.");
				}
			}
		});
	}
}

//change the opacity for different browsers
function setOpacity(opacity, id) {
	opacity = (opacity == 100)?99.999:opacity;
	opacity = (opacity == 0)?0.999:opacity;
	var obj = document.getElementById(id);
	
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function runSlideShow() {
	blendimage('top-image', 'top-image-slide', preLoad[j].src, 2000)
	j = j + 1
	if (j > (images.length - 1)) j = 0
	t = setTimeout('runSlideShow()', slideShowSpeed)
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	setOpacity(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("setOpacity(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

var t; slideShowSpeed = 6000; j = 0; preLoad = new Array(); images = new Array()
function loadSlideShow(theSite) {
	switch(theSite) {
	case 'dma':
		images[0] = 'http://www.the-dma.org.uk/files/design/images/top-layer-sea.jpg';
		images[1] = 'http://www.the-dma.org.uk/files/design/images/top-layer-air.jpg';
		images[2] = 'http://www.the-dma.org.uk/files/design/images/top-layer-land.jpg';
		break;
	case 'appss':
		images[0] = 'http://www.appss.org.uk/files/design/images/APPSS-frame1.jpg';
		images[1] = 'http://www.appss.org.uk/files/design/images/APPSS-frame2.jpg';
		images[2] = 'http://www.appss.org.uk/files/design/images/APPSS-frame3.jpg';
		break;
	case 'eezing':
		images[0] = 'http://www.eezing.org.uk/files/design/images/EEZing-frame1.jpg';
		images[1] = 'http://www.eezing.org.uk/files/design/images/EEZing-frame2.jpg';
		images[2] = 'http://www.eezing.org.uk/files/design/images/EEZing-frame3.jpg';
		break;
	}
	
	for (var i = 0; i < images.length; i++) {
		preLoad[i] = new Image()
		preLoad[i].src = images[i]
	}

	runSlideShow();
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}