  /* help state */
	var showHelp = 0;
	var helpText = "Mouse over a field for help.";
	
	/* mouse coords */
	var mousex = 0;
	var mousey = 0;

	/* form vars */
	var numImages = 1;
	
	function update(e) {
		getMouseXY(e); // NS is passing (event), while IE is passing (null)
	}
	document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE

	 /*
	  * Grab the mouse coords on a certain element
	  */
	 function getMouseXY(e) // works on IE6,FF,Moz,Opera7
	 { 
		  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
		 
		  if (e)
		  { 
		    if (e.pageX || e.pageY)
		    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
		      mousex = e.pageX;
		      mousey = e.pageY;
		      algor = '[e.pageX]';
		      if (e.clientX || e.clientY) algor += ' [e.clientX] '
		    }
		    else if (e.clientX || e.clientY)
		    { // works on IE6,FF,Moz,Opera7
		      // Note: I am adding together both the "body" and "documentElement" scroll positions
		      //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
		      //         (example: IE6 in compatibility mode or strict)
		      //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
		      //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
		      //         (from info at http://www.quirksmode.org/js/doctypes.html)
		      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		      algor = '[e.clientX]';
		      if (e.pageX || e.pageY) algor += ' [e.pageX] '
		    }
		  }
		}
		
	/*
	 * Update the position of the help div
	 */
	 function updateHelpDiv() {
			var helpBox = document.getElementById("helpBox");
			getMouseXY();
			helpBox.style.left = mousex + 20;
			helpBox.style.top = mousey + 20;
	}

  /*
   * Useful function for swapping images around
   */
  function changeImages() {
    if (document.images && (preloadFlag == true)) {
      for (var i=0; i<changeImages.arguments.length; i+=2) {
        document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
      }
    }
  }
  
  /*
   * Open a new popup window in demand
   */
  function NewWindow(mypage, myname, w, h, scroll) { 
    var winl = (screen.width - w) / 2; 
    var wint = (screen.height - h) / 2; 
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',' 
    win = window.open(mypage, myname, winprops) 
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); } 
  }
  
  /*
   * Submit the login credentials to the login script
   */
   function submitLogin( ) {
     //validate login form
     document.forms.login.submit();
   }
   
  /*
   * Submit the logout request to the logout script
   */
   function submitLogout( ) {
     document.forms.logout.submit();
   }
	
   /*
	* Clear the login email address
	*/
	function clearLoginEmail() {
		document.getElementById("loginEmail").value = "";
	}
	
   /*
	* Clear the login password
	*/
	function clearLoginPassword() {
		document.getElementById("loginPassword").value = "";
	}

	/* 
	 * Reset the help text 
	 */
	function clearHelpText() {
		document.getElementById("helpBox").innerHTML = "<h4>&nbsp;Mouse over a field for help.</h4>";
	}
	 
	/*
	 * Update the help text
	 */
	function updateHelpText(newText,eventHandle) {
		// Disable event bubbling for this event, so that elements inside elements with mouseovers can also have mouseovers that work
		if (!eventHandle) var eventHandle = window.event;
        if (eventHandle) eventHandle.cancelBubble = true;
        if (eventHandle.stopPropagation) eventHandle.stopPropagation();
		
		document.getElementById("helpBox").innerHTML = "<h4>&nbsp;" + newText + "</h4>";
	}
	
	/*
	 * Toggle the hovering help box
	 */
	function toggleHelp() {
		var helpBox = document.getElementById("helpBox");
	
		updateHelpDiv();
		
		if (showHelp == 0) {
			helpBox.style.display = "block";
			showHelp = 1;
		} else {
			helpBox.style.display = "none";
			showHelp = 0;
		}
	}

	/*
	 * Check the length of a text area
	 */
	 function textAreaMaxLength(limitField, limitNum) {
	    if (limitField.value.length > limitNum) {
	        limitField.value = limitField.value.substring(0, limitNum);
	    } 
	 }
	
	function validatePaymentForm(formId) {
		var errorBox = document.getElementById('formErrors');

		// Reset the error list
		errorBox.innerHTML = '';
		
		switch(formId) {
			case 'paymentForm1':
				if(!radioValid(document.getElementsByName('membership_type'))) {
					errorBox.innerHTML += 'You must select a membership type<br/>';
				}
			break;
			case 'paymentForm2':
				if(!isValid(document.getElementById('address_1'))) {
					errorBox.innerHTML += 'You must enter an address<br/>';
				}
				if(!isValid(document.getElementById('state'))) {
					errorBox.innerHTML += 'You must select a state<br/>';
				}
				if(!isValid(document.getElementById('postcode'))) {
					errorBox.innerHTML += 'You must enter a postcode<br/>';
				}
				if(!checkboxValid(document.getElementById('agreement'))) {
					errorBox.innerHTML += 'You must accept the ProEMA terms and conditions before you can continue<br/>';
				}
			break;
			case 'paymentForm3': 
				if(document.getElementById('payment_details_credit').style.display != 'none') {
					if(!isValid(document.getElementById('card_holder'))) {
						errorBox.innerHTML += 'You must enter a cardholder name<br/>';
					}
					if(!isValid(document.getElementById('card_number'))) {
						errorBox.innerHTML += 'You must enter a valid card number<br/>';
					}
					if(!isValid(document.getElementById('card_cvv'))) {
						errorBox.innerHTML += 'You must enter a valid card cvv (<a href="http://en.wikipedia.org/wiki/Card_Security_Code" target="_blank">what is this?</a>)<br/>';
					}
					if(!isValid(document.getElementById('card_expiry_month')) || !isValid(document.getElementById('card_expiry_year'))) {
						errorBox.innerHTML += 'You must enter a valid card expiry date<br/>';
					}
				}				
			break;	
		}
		
		// Whack a line break on the end of the error div
		if(errorBox.innerHTML != '') {
			errorBox.innerHTML += '<br/>';
		}
		
		return (errorBox.innerHTML != '') ? false : true;
	}
	
	function radioValid(field) {
		var valid = false;
		for (var i in field) {
			if(field[i].checked == true) {
				valid = true;
			}
		}
		return valid;
	}
	
	function checkboxValid(field) {
		return field.checked;
	}
	
	function isValid(field) {
		return (field.value != '') ? true : false;
	}
	
	function emailValid(email){
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		return emailPattern.test(email.value);
	}

	function showLayers(elements) {
		for(var i in elements) {
			var element = document.getElementById(elements[i]);
			element.style.display = 'block';
		}
	}
	
	function hideLayers(elements) {
		for(var i in elements) {
			var element = document.getElementById(elements[i]);
			element.style.display = 'none';
		}
	}
