
////////////////////////////////////////////////////////////////////////////////////
// Preload all images used in the site.
////////////////////////////////////////////////////////////////////////////////////

/*****************************
linksOff = new Image();
linksOff.src = './images/buttons/links.gif';
linksOn = new Image();
linksOn.src = './images/buttons/linkso.gif';
******************************/

// Image toggler.
function setImage(imgName, setToImage, currentPageName, newPageName) {
   // Using preloaded images.
   if (document.images){
   
   	if (currentPageName == newPageName) {
   		// Ignore the request.   		
   	} else {
		document[imgName].src = setToImage.src;
	}
   }
}

var win;	// Popup window.
function closeWindow() {
	if (win && win.open && !win.closed) {
		win.close();
	}
}

function openWindow(url, width, height, target, offset) {

	var displaywidth = width; 
	var displayheight = height;
	var offsetWin = (offset)? offset: 0;
	var x = 200 + offsetWin;
	var y = 150 + offsetWin;

	var parms = ('height='+displayheight+',width='+displaywidth
			+ ',resizable=yes,status=no,toolbar=no,'
			+ 'menubar=no,location=no,scrollbars=yes, '
			+ 'screenx=' + x + ', screeny=' + y + ','
			+ 'left=' + x + ',top=' + y);
	closeWindow();	
	win=window.open(url, target, parms);
}

// ******************************//
function lTrim(str) {
	if (!str) return "";
	for (var i=0; i<str.length; i++){
		if (str.charAt(i) != " " && str.charAt(i) != "\n" && str.charAt(i) != "\r"){
			return str.substring(i);
		}
	}
	return "";	// All blanks or no string.
}

function rTrim(str) {
	if (!str) return "";
	for (var i=str.length; i>=0; i--){
		if (str.charAt(i - 1) != " " && str.charAt(i - 1) != "\n" && str.charAt(i - 1) != "\r"){
			return str.substring(0, i);
		}
	}
	return "";	// All blanks or no string.
}

function trim(str) {
	return lTrim(rTrim(str));
}

function replaceAll(strValue, pattern, replaceWith)
{
	// Replaces all occurences of pattern with replaceWith in strValue.
	var workValue = strValue;
	while (workValue != (strValue = strValue.replace(pattern, replaceWith))) {
		workValue = strValue;
	}
	return strValue;
}

function replaceIgnoreCase(strValue, pattern, replaceWith)
{
	// Replaces the first occurences of pattern with replaceWith in strValue,
	// ignoring the case of pattern.
	var pos = strValue.toUpperCase().indexOf(pattern.toUpperCase());
	var returnValue = strValue.substr(0,pos);
	returnValue += replaceWith;
	returnValue += strValue.substr(pos + pattern.length);
	return returnValue;
}

function upper(value) {

	return trim(value).toUpperCase();
}


function roundValue(value, precision)
{
	return Math.round(value * Math.pow(10, precision))/Math.pow(10, precision);
}

function roundAndCheckFormat(value, precision)
{
	if (isNaN(value)){
		return value;
	}
	var valueStr = String(value);
	// Ensure there is a leading zero if only decimals.
	if (valueStr.charAt(0) == "."){
		valueStr = ("0" + valueStr);
	}
	// If any decimals, round to required precision.
	var dotPos = valueStr.indexOf(".");
	if (dotPos == -1){	
		// Do nothing.
	}
	else{
		valueStr = String(roundValue(Number(valueStr), precision));
	}
	// Ensure there are trailing zeroes.
	if (precision > 0){	
	
		dotPos = valueStr.indexOf(".");
		if (dotPos == -1){
			valueStr += ".";
			dotPos = valueStr.length - 1;
		}		
		// Allowing for a maximum of 5 places, plus however many
		// places are already in the value.
		valueStr = (valueStr + "00000");
		// NB The extra one on the next line is for the decimal itself.		
		var requiredLength = Number(dotPos) + Number(precision) + 1;  
		valueStr = valueStr.substr(0, requiredLength);
	}
	return valueStr;
}

//***************************//
/// Form validator class    ///
//***************************//

function formValidator()
{
	// Data Members
	this.firstErrorElement = null;
	this.errorMsg = "At least one error was encountered:";
	this.errors = new Array();

	// Methods
	this.isFieldBlank = isFieldBlank;
	this.isEmailAddress = isEmailAddress;
	this.isFloat = isFloat;
	this.isInteger = isInteger;
	this.isCharNumeric = isCharNumeric;
	this.displayErrors = displayErrors;
	this.raiseError = raiseError;
	this.isError = isError;
	this.setFocusToFirstError = setFocusToFirstError;
	this.checkMaxLength = checkMaxLength;
	this.checkMinLength = checkMinLength;
}

function displayErrors()
{
	var cr = "\n";
	var msgToDisplay = this.errorMsg;
	for (var i=0; i<this.errors.length; i++) {
		msgToDisplay += (cr + this.errors[i]);
	}
	alert(msgToDisplay);
}

function raiseError(errorMsg, errorElement)
{
	this.errors[this.errors.length] = errorMsg;
	if (this.firstErrorElement == null) {
		this.firstErrorElement = errorElement;
	}
}

function isError()
{
	return (this.errors.length > 0);
}

function setFocusToFirstError()
{
	if (this.firstErrorElement != null) {
		this.firstErrorElement.focus();
	}
}

//**********************//
/// General utilities  ///
//**********************//

function checkMinLength(theField, minLen)
{
	if (theField.value.length < minLen){
		return false;
	}
	return true;
}

function checkMaxLength(theField, maxLen)
{
	if (theField.value.length > maxLen){
		return false;
	}
	return true;
}

function isFieldBlank(theField)
{
	if (theField.value.length == 0){
		return true;
	}
	for (i=0; i < theField.value.length; i++) {
		var tempChar = theField.value.charAt(i);
		if (tempChar != " "){
			return false;
		}
	}
	return true;
}

function isEmailAddress(theField)
{
	if (theField.value.length == 0){
		return false;
	}

	var strValue = String(theField.value);
	var atPos = strValue.indexOf("@");
	var dotPos = strValue.indexOf(".");
	// There must be a dot and an @ and there must be a dot after the @.
	if (atPos <= 0 || dotPos <= 0 || strValue.indexOf(".", atPos) <= 0){
		return false;
	}
	return true;
}

function isInteger(theField)
{
	if (theField.value.length == 0){
		return false;
	}

	var strValue = String(theField.value);
	for (i=0; i < strValue.length; i++) {
		var tempChar = strValue.charAt(i);
		if (!isCharNumeric(tempChar)){
			return false;
		}
	}
	return true;
}

function isFloat(theField)
{
	if (theField.value.length == 0){
		return false;
	}

	var strValue = String(theField.value);
	for (i=0; i < strValue.length; i++) {
		var tempChar = strValue.charAt(i);
		if (!isCharNumeric(tempChar) && tempChar != "."){
			return false;
		}
	}
	return true;
}

function isCharNumeric(charValue)
{
	if (charValue >= "0"
	 && charValue <= "9"){
		return true;
	}
	return false;
}

