
// Set display attribute
function SetDisplay(objname, setting) {
	switch(navigator.family) {
		case 'nn4':
			document.layers[objname].display = setting;
			break;
		case 'ie4':
			document.all(objname).style.display = setting
			break;
		case 'gecko':
			document.getElementById(objname).style.display = setting;
			break;
		}
	}

// Retrieve display attribute
function CheckDisplay(objname) {
	switch(navigator.family) {
		case 'nn4':
			return document.layers[objname].display;
			break;
		case 'ie4':
			return document.all(objname).style.display;
			break;
		case 'gecko':
			return document.getElementById(objname).style.display;
			break;
		}
	}

// Replace html special chars
function ReplaceHTMLChars(field){
	var outputStr, txtStr;
	for (var i = 0;i > field.length ; i++ ) {
		txtStr = field.charAt(i);
		if (txtStr == " ") txtStr="&nbsp;";
		if (txtStr == "&") txtStr="&amp;";
		if (txtStr == "\"") txtStr="&quot;";
		if (txtStr == "<") txtStr="&lt;";
		if (txtStr == ">") txtStr="&gt;";
		outputStr = outputStr + txtStr;
		}
	return outputStr;
	}

//This function checks that a valid currency has been entered
function checkCurrency(field, fielddesc){
	if (checkMandatoryField(field, fielddesc) == false)
		return false;
	if (isNaN(parseFloat(field.value)) == true){
		alert(fielddesc + " should be a numeric value.");
		field.select();
		field.focus();
		return false;
		}
	if (field.value <= 0){
		alert(fielddesc + " should be a positive numeric value.");
		field.select();
		field.focus();
		return false;
		}
	return true;
	}

//This function checks that a mandatory field has been filled in
function checkMandatoryField(field, fielddesc){
	if (field.value.length == 0){
		alert(fielddesc + " should not be left blank. Please enter a value");
		field.select();
		field.focus();
		return false;
		}
	else
		return true
		}

//This function checks that a valid time has been entered
function checkTime(field, fielddesc){
	if (checkMandatoryField(field, fielddesc) == false)
		return false;
	if (field.value.length == 4)
		field.value = field.value.slice(0,2) + ":" + field.value.slice(2,4);
	if (isTime(field.value.slice(0,2),field.value.slice(2,3),field.value.slice(3,5)) == false) {
		alert(fielddesc + " should be in form HH:MM (24 hour clock).");
		field.select();
		field.focus();
		return false;
		}
	return true;
	}

// This function checks that a combination of hours, separator and minutes equals a valid time
 function isTime (hour, separator, minute) {
	if (isNaN(hour) == true) 
		return false;
	if (parseInt(hour) < 0) 
		return false;
	if (parseInt(hour) > 24)
		return false;
	if (separator != ":")
       return false;
	if (isNaN(minute) == true)
		return false;
	if (minute < 0) 
		return false;
	if (minute > 59)
		return false;
	return true;
   }

//This function checks that a valid date has been entered
function checkDate(field, fielddesc){
	if (checkMandatoryField(field, fielddesc) == false)
		return false;
	if (field.value.length == 8)
		field.value = field.value.slice(0,2) + "/" + field.value.slice(2,4) + "/" + field.value.slice(4,8);
	if (isDate(field.value.slice(6,10),field.value.slice(3,5),field.value.slice(0,2)) == false){
		alert(fielddesc + " should be in form DD/MM/YYYY.");
		field.select();
		field.focus();
		return false;
		}
	return true
	}

// This function validates a particular combination of year, month and day is a valid date
function isDate (year, month, day) {
   // month argument must be in the range 1 - 12
   month = month - 1;  // javascript month range : 0- 11
   var tempDate = new Date(year,month,day);
   if ( (tempDate.getFullYear() == year) &&
      (month == tempDate.getMonth()) &&
      (day == tempDate.getDate()) )
       return true;
   else
      return false
   }
  
//This function checks that a postcode is valid
function checkPostCode(field, fielddesc){
	var pattern = /^\d{4}/
	if (field.value.length != 0) {
		if (pattern.test(field.value) != true) {
			alert(fielddesc + " is not a valid postcode.");
			field.select();
			field.focus();
			return false;
			}
		}
	return true
	}

//This function checks that the email field has been filled in and has an @ symbol
function checkemail(field, fielddesc){
	if (checkMandatoryField(field, fielddesc) == false)
		return false;
	if (field.value.search("@") == -1) {
//	if (isEmail(field) == false) {
		alert(fielddesc + " is not a valid e-mail.");
		field.select();
		field.focus();
		return false;
		}
	else
		return true
		}

// Not currently used - haven't figured out how it works
function isEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) 
			supported = 1;
		}
	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
	}

//This function turns the 'other date' field on and off
function checkOtherDate(field) { 

	var FieldName = "Other"+field.name;
	FieldObj = field.form[FieldName];
	
	var DivName = "Other"+field.name+"Div";

	if (field.value == "other") {
		SetDisplay(DivName,"inline");
		FieldObj.select();
		FieldObj.focus();
		}
	else
		SetDisplay(DivName,"none");
}

//This function validates a phone number. NOTE - does not enforce mandatory entry. Do this in form checking
function checkPhone(field, fielddesc) {
	var valid = 1
	var GoodChars = "0123456789()+ "

	if (field.value.length != 0) {
		for (var i =0; i <= field.value.length -1; i++) {
			if (GoodChars.indexOf(field.value.charAt(i)) == -1) {
				alert(fielddesc + " is not a valid phone number. Valid characters - '01234567890()+' and spaces.");
				field.select();
				field.focus();
				return false;
				}
			}
		}
	return true;
	}

//This function updates the innerHTML for an object
function setInnerHTML(objname, newtext) {

	switch(navigator.family) {
		case 'nn4':
			var elementobj = nav4FindLayer(window.document, objname);
			elementobj.document.write(newtext);
			elementobj.document.close();
			break;
		case 'ie4':
			document.all(objname).innerHTML = newtext;
			break;
		case 'gecko':
			document.getElementById(objname).innerHTML = newtext;
			break;
		}
	}

function nav4FindLayer(doc, id) {
	var i;
	var subdoc;
	var obj;
  
	for (i = 0; i < doc.layers.length; ++i) {
		if (doc.layers[i].id && id == doc.layers[i].id)
			return doc.layers[i];
		subdoc = doc.layers[i].document;
		obj = nav4FindLayer(subdoc, id);
		if (obj != null)
			return obj;
		}
	return null;
	}

function changeDesc(menuOpt){
	if (menuOpt == "blank") {
		setInnerHTML("desctext","");
		}
	else
		setInnerHTML("desctext",HelpText[menuOpt]);
	}

function UpdateMenu(menuOpt) {

	SetDisplay("SubMenu3","none");
	SetDisplay("SubMenu6","none");
	SetDisplay("SubMenu9","none");
	SetDisplay("SubMenu12","none");
	SetDisplay("SubMenu17","none");

	SetDisplay("SubMenu"+menuOpt,"block");

	}

function ResizeNavBar() {

	if (getCookie("minmax") == "min") {
		SetImgSrc("minmax", "minimise.gif");
		SetDisplay("navbarmain","block");
		setCookie("minmax", "max");
		}	
	else {
		SetImgSrc("minmax", "maximise.gif");
		SetDisplay("navbarmain","none");
		setCookie("minmax", "min");
		}	

	}

function SetImgSrc(imgid, filename) {

	document.images(imgid).src = "/images/" + filename;

	}

function getCookie(name) {
  /*
    Scans through document.cookie until the the cookie 
    in question is found and then returns that  cookie's value.
    The scanning is done in the getValue function.
  */
	var cname = name + "=";
	var dc = document.cookie;
	return getCookieValue(dc, cname, ";");
	}


function setCookie(name, value) {
  /*
    Sets the cookie by updating document.cookie with the input
    values.
  */
	document.cookie=name + "=" + escape(value);
	}


function getCookieValue(str, key, delimiter) {
  /*
    This function scans through the input string str (the cookie or
    chip string) searching for the input key (cookie or chip name). 

    The delimiter input specifies the delimiter which follows the key
    (here ';' for cookies and "|" for chips)
  */     

	if (str == null) return null;

	if (str.length > 0) {
		begin = str.indexOf(key)
		if (begin != -1) {
			begin += key.length;
			end = str.indexOf(delimiter, begin);
			if (end == -1)
				end = str.length;
			return unescape(str.substring(begin, end));
			}
		}
	return null;
	}

function setCookieValue(str, key, value, delimiter) {
  /*
    This function builds a (cookie) string by inserting or updating
    the input key, input value pair in the input str (the cookie 
    string).
  */

	if (getCookieValue(str, key, "|") != null) {
		var start = str.indexOf(key);
		if (start != -1) {
			var end = str.indexOf('|', start);
			str = str.substring(0, start) + key + value + delimiter + str.substring(end + 1, str.length)
			}
		}
	else {
		if (str == null) str = "";
			str += key + value + delimiter;
	    }
    
	return str;    

	}

//This function turns the 'return time' field on and off
function checkReturn(form) { 

	if (getSelectedRadioButton(form.Return) == 1) {
		SetDisplay("ReturnTimeLabel","inline");
		SetDisplay("ReturnTimeDiv","inline");
		form.ReturnTime.select();
		form.ReturnTime.focus();
		}
	else {
		SetDisplay("ReturnTimeLabel","none");
		SetDisplay("ReturnTimeDiv","none");
		}

}

function getSelectedRadioButton(radioGroup) {

   for (var i = 0; i < radioGroup.length; i++)
      if (radioGroup[i].checked == true)
        return radioGroup[i].value;
}