/**********************************************************************************/
/***		Client-side JavaScript function library																	***/
/***		Copyright Apache Solutions Ltd 2007																			***/
/***		(unauthorised use of this code will result in prosecution by law)				***/
/**********************************************************************************/

// capture mouse clicks.
document.onmousedown=HandleMouseClick;
document.oncontextmenu = function() {return false;};

//
//	Displays a pop up copyright message if the right mouse button is used:
//
function HandleMouseClick(e) {
	var message="All content is copyright 2007 Memotech, all rights reserved.";
	if (document.all) {
		if (event.button==2 || event.button==3) {
			alert(message);
			return false;
		}
	}
	else if (document.getElementById) {
		if (e.which == 3) {
			alert(message);
			return false;
		}
	}
}

//
//	Closes the current window without prompting:
//
function CloseWindow() {
	window.open('','_parent','');
	window.opener = top;
	window.close();
}

//
//	Hides or shows the given element:
//
function Toggle(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		el.style.visibility = (el.style.visibility == 'hidden' || el.style.visibility == '') ? 'visible' : 'hidden';
	} 
} 

//
//	Shows the first element and hides the other:
//
function Switch(selected_id, deselected_id) {
	if (document.getElementById) {
		document.getElementById(selected_id).style.display = 'block';
		document.getElementById(deselected_id).style.display = 'none';
	} 
} 

//
//	Validates the feedback form:
//
function ValidateFeedbackForm(form) {	
	if (!CheckMandatory(form.name, "Please enter your name.")) return false;
	if (!CheckMandatory(form.email, "Please enter your email address.")) return false;
	if (!CheckEmail(form.email)) return false;
	if (!CheckMandatory(form.feedback, "Please enter your feedback.")) return false;
	return true;
}

//
//	The following functions are usually in clilib.js, but as the form in the footer is simple
//	they have been placed here instead.
//

//
//	Returns true if the given variable is blank:
//
function IsBlank(value) {
	// cast to string.
	var str = "" + value;
	return ((str == "") || (str == " ") || (str == "undefined") || (str == "null") || (str == null));
}

//
//	Checks whether the user has entered data in the given field, displaying the given message,
//	focusing the field and returning false if not:
//
function CheckMandatory(field, msg) {
	if (IsBlank(field.value)) {
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}	

//
//	Checks whether the user has entered a valid email address in the given field, displaying an appropriate message,
//	focusing the field and returning false if not:
//
//	N.B Use with 'CheckMandatory' fn above if the field is also mandatory.
//
function CheckEmail(field) {
	if (!IsBlank(field.value)) {
		// ensure that only valid characters have been used.
		var ok = "1234567890qwertyuiopasdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
		for (i=0; i < field.value.length; i++) {
			if (ok.indexOf(field.value.charAt(i)) < 0) {
				alert("The email address you have entered contains an invalid character.\nIt should look something like 'name@company.com'");
				field.focus();
				return false;
			}
		}
		// on IE4+, perform additional format validation.
		if (document.images) {
	  	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	  	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			if (!(!field.value.match(re) && !(!field.value.match(re_two)))) {
				alert("The email address you have entered is invalid.\nIt should look something like 'name@company.com");
				field.focus();
				return false;
			}
	 	}
		// otherwise, just make sure the at sign isn't at the start or end.
		else {
			var atPos = field.value.indexOf('@');
			if (atPos < 1 || atPos == (field.value.length - 1)) {
				alert("The email address you have entered is invalid.\nIt should look something like 'name@company.com");
				field.focus();
				return false;
			}
		}
	}
	return true;	
}

//
// Script end.
//
