Event.observe(window, 'load', function(){
	// replace the logo image on hover
	if ($('mastheadImage')) {
		Event.observe('mastheadImage', 'mouseover', function(){
			$('mastheadImage').src = '/images/reuse/LogoOnHover.jpg';
		});
		Event.observe('mastheadImage', 'mouseout', function(){
			$('mastheadImage').src = '/images/reuse/logo.jpg';
		});
	}
	//show a message when someone signs up for the newsletter
	if ($("signupForm")) {
		Event.observe('signupForm', 'submit', function(e){
			if ($('name').value.length == 0) {
				alert('Please provide your name');
				Event.stop(e);
			} else if ($('email').value.length == 0) {
				alert('Please provide a valid email address');
				Event.stop(e);
			} else if (!isValidEmail($('email').value)) {
				alert('Please provide a valid email address');
				Event.stop(e);
			} else if (!confirm('By clicking OK you are agreeing that we can send you a newsletter. We will never share your details with any 3rd parties.')) {
				Event.stop(e);
			}
		});
	}

	// show the sub menu when hovering over 'Ask a Question' in the top nav
	if ($("li-questions")) {
		Event.observe('question-no-click-link', 'click', function(e){
			Event.stop(e);
		});
	}

	// show the preview of a question before submitting it
	if ($('questionForm')) {
		Event.observe('previewQuestion', 'click', function(e){
			Event.stop(e);
			if ($('question').value.length > 0) {
				$('showPreviewQuestion').innerHTML = $('question').value;
			}
		});
	}
});

/* Check that an email address is valid based on RFC 821 (?) */
function isValidEmail(address) {
	if (address != '' && address.search) {
		if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
			return true;
		} else return false;
	} else return true;
}
