/**********************************************************************************************
* Author:   Andrew Connick
* Date:     18/06/07
***********************************************************************************************/

/* Screen manipulation functions ***************************************************************/

function showElm(id) { try { document.getElementById(id).style.display = ""; } catch(e) { } }
function hideElm(id) { try { document.getElementById(id).style.display = "none"; } catch(e) { } }

function dspPopup(target, width, height) {
	popup = window.open(target, "popUp", "width=" + width + ",height=" + height + ",top=200,status=yes,resizable=yes");
	popup.focus();
}

/* Drop down menu functions (see http://www.alistapart.com/articles/dropdowns )*****************/

function setDropDown() {
	if (document.all&&document.getElementById) {    // identifies IE
		div = document.getElementById("topNav");
		if (div!=null) {
			for (i=0; i<div.childNodes.length; i++) {
				uList = div.childNodes[i];
				if (uList.nodeName=="UL") setDropDownList(uList);
			}
		}
	}
}

function setDropDownList(list) {
	for (i=0; i<list.childNodes.length; i++) {
		node = list.childNodes[i];
		if (node.nodeName=="LI") {
			node.onmouseover = function() { this.className = "over"; }
  		node.onmouseout = function() { this.className = ""; }
		}
	}
}

/* Validation functions ************************************************************************/

function isBlank(fld) {
	if (fld==null) return false;
	else if (fld.value == "") return true;
	else if (fld.value == " ") return true;
	else if (fld.value == "0") return true;
	else return false;
}

function vldEmail(e) {
	// define a regular expression to validate the email address
	r = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (r.test(e.value)) return true;                        // quit if the email is valid.
	// otherwise set an error.
	e.select();
	alert("Please enter a valid email address.");
	e.focus();
	return false;                                            // return false to halt processing.
}

function vldNumber(nbr) {
	// define a regular expression to validate number
	r = /^\d*.?\d*$/;
	if (r.test(nbr.value)) return true;                      // quit if valid.
	// otherwise set an error.
	alert("This item must be numeric.");
	nbr.focus();
	nbr.select();
	return false;
}

// Validate the basic format of a date 
function vldDate(dte) {
	// define a regular expression to validate date
	if (dte.value=="") return true;                          // quit if blank.
	r = /^\d*\/\d*\/\d*$/;
	if (r.test(dte.value)) return true;                      // quit if valid.
	// otherwise set an error.
	alert("Please enter a valid date (format dd/mm/yy).");
	dte.focus();
	dte.select();
	return false;
}

function vldPassword(pwd) {
	if (pwd.value.length==0) return true;										 // quit if blank.
	// define a regular expression to validate the password (letters & numbers only, at least 6)
	r = /^\w{6,}$/;
	if (r.test(pwd.value)) return true;                      // quit if the password is valid.
	// otherwise set an error.
	pwd.select();
	alert("Your password is invalid. It must be at least 6 characters. It can only contain letters or numbers.");
	pwd.focus();
	return false;                                            // return false to halt processing.
}

// Validate text for special characters
function vldText(txt) {
	if (txt.value.indexOf('>')>=0 || 
			txt.value.indexOf('<')>=0 ||
			txt.value.indexOf('"')>=0 ||
			txt.value.indexOf('\'')>=0 ||
			txt.value.indexOf('\n')>=0 ||
			txt.value.indexOf('\r')>=0 ||
			txt.value.indexOf('\r\n')>=0 ||
			txt.value.indexOf('&')>=0) {
		txt.select();
		alert("Text contains an invalid character \n < > & quotes newline \n are not allowed.");
		txt.focus();
		return false;
	}
	return true;
}

// Validate text for < > characters
function vldTextTag(txt) {
	if (txt.value.indexOf('>')>=0 || 
			txt.value.indexOf('<')>=0) {
		txt.select();
		alert("Text contains an invalid character < & > are not allowed.");
		txt.focus();
		return false;
	}
	return true;
}

/* Functions and attributes for text size ******************************************************/

var dftTextSize = 70;
var textSize;

// set the text size, using a value stored in cookie
function setTextSize() {
	textSize = dftTextSize;
	var pos = document.cookie.indexOf("textSize=");
	if (pos != -1) {
		var start = pos + 9;
		var end = document.cookie.indexOf(";", start);
		if (end == -1) end = document.cookie.length;
		// convert the cookie value to a number, then use the global variable for textsize
		textSize = Number(document.cookie.substring(start, end));
	}
//	alert("text size: " + textSize);
	document.body.style.fontSize = textSize + "%";                 // set the font size.
}

// process a user request to change the text size
function changeTextSize(direction) {
	if (direction=="0") textSize = dftTextSize;
	else if (direction=="+") textSize = textSize + 5;
	else { if (textSize > 20) textSize = textSize - 5; }
	expiryDate = new Date();
	expiryDate.setFullYear(2020);
	document.cookie = "textSize=" + textSize + "; path=/; expires=" + expiryDate.toGMTString();
	setTextSize();
	return false;
}

/* Other functions *****************************************************************************/

function dspFormatHelp() {
	fmtHelp = window.open("formathelp.html", "helpText", "width=800,height=600,top=30");
	fmtHelp.focus();
}

// function to fix IE bug
function setMinHeight(element) {
	var minContentHeight = 620;
	if (document.all&&document.getElementById) {    // identifies IE
		var h = document.getElementById(element).offsetHeight;
		if (h != 0 && h < minContentHeight) {
			h = minContentHeight - h;
		document.write("<div style='height: " + h + "px; '></div>");
		}
	}
}
