var present = true;

// date campus name will change:
var changeoverDate = new Date("January 4, 2005");

// Author: Martyn A George
function dispTest()
{
	document.write("<p>TEST MESSAGE</p>");
}

// Author: Martyn A George
function sum(a, b)
{
	return a + b;
}

// Author: Martyn A George
// This function validates entry into the FindIt search text field in the home
// page navigation toolbar. 
function validateSearchString(form)
{
	if(form.pm_AFT.value < 1)
		alert("Search keywords are required to perform a search");
	else
		form.submit();
	return;
}

// Author: Martyn A George
// This function returns a date dependent message. If the date precedes the
// specified date ('day', 'month', 'year'), 'pastMsg' is returned, otherwise
// 'futureMsg' is returned instead.
// Arguments: 
//		day             - day portion of date for commencement of 'futureMsg'
//                           return
//      month        - month portion of date (Jan = 1, Dec = 12) for
//                          commencement of 'futureMsg' return
//      year           - year portion of date for commencement of 'futureMsg'
//                           return
//      pastMsg   - message returned if the current system date precedes
//                          the specified date
//      futureMsg - message returned if the current system date is equal
//                          to, or succeeds the specified date
// Returns: 'pastMsg' if the current system date precedes the specified
//            date, 'futureMsg' otherwise
function timedMsg(pastMsg, futureMsg, day, month, year)
{
	var triggerDate = new Date(year, month - 1, day);
	var today = new Date();
  if(Date.parse(today.toLocaleString()) < Date.parse(triggerDate.toLocaleString()))
		return pastMsg;
	else
		return futureMsg;
}


// Author: Martyn A George
// This function returns a specific date dependent message. If the date
// precedes "changeoverDate", pastMsg is returned, otherwise futureMsg is
// returned.
// Returns: pastMsg if the current system date precedes "changeoverDate",
//            futureMsg otherwise
// Note: For all library web pages, this function should be used to replace
// an existing Underdale related message to a corresponding Mawson Lakes
// related message, where appropriate. This will effect a message change
// for those occurrences on "changeoverDate".
function getCurrentMsg(pastMsg, futureMsg)
{
	return timedMsg(pastMsg, futureMsg, changeoverDate.getDate(),
		changeoverDate.getMonth() + 1, changeoverDate.getYear());
}


// Author: Martyn A George
// This function returns a campus name dependent upon the date. If the date
// precedes "changeoverDate", "Underdale" is returned, otherwise the
// specified campus name is returned
// Argument:
//		campus - the campus name after changeover
// Returns: "Underdale" if the current system date precedes "changeoverDate",
//            or the supplied argument otherwise.
// Note: For all library web pages, this function should be used to replace
// existing occurrences of "Underdale" with a new campus name, where
// appropriate. This will effect a change of name for those occurrences on
// "changeoverDate".
function getCurrentCampus(campus)
{
	return timedMsg("Underdale", campus, changeoverDate.getDate(),
		changeoverDate.getMonth() + 1, changeoverDate.getYear());
}


// Author: Martyn A George
// This function returns a specific date dependent message. If the date
// precedes that specified in the function body, "Underdale" is returned,
// otherwise "" is returned.
// Returns: "Underdale" if the current system date precedes the specified
//            date, "" otherwise
// Note: For all library web pages, this function effectively deletes
// associated occurrences of "Underdale" on the changeover date.
function getUnderdaleOrNil()
{
	return timedMsg("Underdale", "", changeoverDate.getDate(),
		changeoverDate.getMonth() + 1, changeoverDate.getYear());
}


// Author: Martyn A George
// This function returns a specific date dependent message. If the date
// precedes that specified in the function body, the prescribed message is
// returned, otherwise "" is returned.
// Argument:
//		pastMsg   - message returned if the current system date precedes
//                        the specified date
// Returns: 'pastMsg' if the current system date precedes the specified
//            date, 'futureMsg' otherwise
// Note: For associated web pages, this function effectively deletes the text
// defined by 'pastMsg'.
function getPastMsgOrNil(pastMsg)
{
	return timedMsg(pastMsg, "", changeoverDate.getDate(),
		changeoverDate.getMonth() + 1, changeoverDate.getYear());
}


// Author: Martyn A George
// This function sets the size of the web page according to the value contained
// in a cookie (small, smallest, medium, large, or largest). If the cookie
// doesn't exist, it is created and text size is initialised at medium.
function setPageSize()
{   
	var textSizeStr = getCookie("textSize");
	if(textSizeStr == "")
	{
		textSizeStr = "medium";
		setCookie("textSize", textSizeStr, 365);
	}
	switch(textSizeStr)
	{
		case "smallest":
			document.write("<style type=\"text/css\">@import url(\"http://www.library.unisa.edu.au/toolbox/styles/smallest_text.css\");</style>");
			break;
		case "small":
			document.write("<style type=\"text/css\">@import url(\"http://www.library.unisa.edu.au/toolbox/styles/small_text.css\");</style>");
			break;
		case "medium":
			// Use default stylesheet
			//document.write("<style type=\"text/css\">@import url(\"http://www.library.unisa.edu.au/toolbox/styles/medium_text.css\");</style>");
			break;
		case "large":
			document.write("<style type=\"text/css\">@import url(\"http://www.library.unisa.edu.au/toolbox/styles/large_text.css\");</style>");
			break;
		case "largest":
			document.write("<style type=\"text/css\">@import url(\"http://www.library.unisa.edu.au/toolbox/styles/largest_text.css\");</style>");
			break;
		default:
			// Use default stylesheet
			break;
	}
	return;
}


// Author: Martyn A George
// This function changes the value of the "textSize" cookie to the supplied
// string argument. This value is used by setPageSize(), and should be one of,
// "smallest", "small", "medium", "large", or "largest"
// Argument: 
//		day - string depicting desired text size
function setTextSize(sizeStr)
{
	var textSizeStr = sizeStr.toLowerCase();
	setCookie("textSize", textSizeStr, 365);
	window.location.reload(true);
	return;
}


// Author: Martyn A George
// This function changes the value of the "textSize" cookie to the value, 
// "smallest". This value is used by setPageSize() to set the size of the
// web page.
function setSmallestText()
{
	setTextSize("smallest");
	return;
}


// Author: Martyn A George
// This function changes the value of the "textSize" cookie to the value, 
// "small". This value is used by setPageSize() to set the size of the
// web page.
function setSmallText()
{
	setTextSize("small");
	return;
}


// Author: Martyn A George
// This function changes the value of the "textSize" cookie to the value, 
// "medium". This value is used by setPageSize() to set the size of the
// web page.
function setMediumText()
{
	setTextSize("medium");
	return;
}


// Author: Martyn A George
// This function changes the value of the "textSize" cookie to the value, 
// "large". This value is used by setPageSize() to set the size of the
// web page.
function setLargeText()
{
	setTextSize("large");
	return;
}


// Author: Martyn A George
// This function changes the value of the "textSize" cookie to the value, 
// "largest". This value is used by setPageSize() to set the size of the
// web page.
function setLargestText()
{
	setTextSize("largest");
	return;
}


// Author: Martyn A George
// This function creates a cookie with the specified name, value, and 
// duration. 
// Arguments:
//   name         - name of cookie
//   value        - value of cookie
//   daysDuration - number of days before cookie expires
function setCookie(name, value, daysDuration)
{
	var MS_PER_DAY = 86400000;
	var endDate = new Date();
	
	endDate.setTime(endDate.getTime() + MS_PER_DAY * daysDuration);
	document.cookie = name + "=" + escape(value) + "; expires=" + endDate.toGMTString();
	return;
}


// Author: Martyn A George
// This function returns the value of the specified cookie. If the cookie is
// not present, an empty string ("") is returned.
// Argument:
//   name - name of the cookie for which the value is required
// Returns: the cookie's value, or "" if the cookie doesn't exist
function getCookie(name)
{
	var cookie = "";
	var cookieSet = document.cookie;
	var cookieIndex = cookieSet.indexOf(name + "=");
	
	if(cookieIndex >= 0)
	{
		var valueStartIndex = cookieIndex + name.length + 1;
		var valueEndIndex = cookieSet.indexOf(";", valueStartIndex);
		if(valueEndIndex == -1)
			valueEndIndex = cookieSet.length;
		cookie = unescape(cookieSet.substring(valueStartIndex, valueEndIndex));
	}
	return cookie;
}


// Author: Martyn A George
// This function deletes the specified cookie.
// Argument:
//   name - name of the cookie to delete
function delCookie(name)
{
	setCookie(name, "", -1);
	return;
}

// Author: unspecified
//function that validates e-mail address syntax
function emailCheck (emailStr, alertflag) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")	
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null) {
			if (alertflag)
		alert("E-mail address seems incorrect (check @ and .'s).")
		return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]	

	// See if "user" is valid 

	if (user.match(userPat)==null) {
		// user is not valid
			if (alertflag) 
		alert("The username in your e-mail address doesn't seem to be valid.")
		return false
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
		  host name) make sure the IP address is valid. */	
	var IPArray=domain.match(ipDomainPat)

	if (IPArray!=null) {
		// this is an IP address
			for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			if (alertflag)
				alert("Destination IP address is invalid!")
			return false
			}
		}
		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)

	if (domainArray==null) {
			if (alertflag)
		alert("The domain in your e-mail address doesn't seem to be valid.")
		return false
	}	

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	musExp = /.museum/
	isMus = domain.search(musExp)

	if ( ( (domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 6) ) ||
		(domArr[domArr.length-1].length == 5) ||
		( (domArr[domArr.length-1].length == 6) && (isMus == -1) ) ) {
	  // the address must end in a two, three, or four letter word, or .museum.
		if (alertflag) 	
	   	alert("E-mail address must end in a three or four-letter domain, two-letter country code, or .museum.")
	   	return false
}
// Make sure there's a host name preceding the domain.
	if (len<2) {	
		  var errStr="This e-mail address is missing a hostname!"	

			if (alertflag) 	
		  alert(errStr)

		  return false

	}

	// If we've gotten this far, everything's valid!
	return true;

}

// Author: Martyn A George
// Date:   16/09/2004
// This function checks the email address suffix (after the '@' symbol) of
// the email address entered into the form to determine if it looks like a
// valid suffix for the specified patron type. If the suffix appears valid,
// processing continues normally, otherwise the patron is prompted to 
// determine suffix validity. No other validation of the email address is
// performed by this function.
// Arguments: email - email address entered by the patron
//            form  - the form containing the email input text field
// Returns:   true if the email address suffix is validated, false otherwise
// Rationale: Many patrons were incorrectly entering their email addresses
//            when completing the form. This function should help rectify
//            this problem by ensuring that the suffix is correct.

//function validateEmailSuffix(email, form)
//{
//	var indexOfAt = -1;
//	var emailPrefixIn = "";
//	var emailSuffixIn = "";
//	var emailSuffixNew = "";
//	indexOfAt = email.indexOf("@");
//	if(indexOfAt > -1)
//	{
//		emailPrefixIn = email.substring(0, indexOfAt + 1);
//            // RFC1035 specifies that the domain portion of an email
//            // address is case-insensitive:
//		emailSuffixIn = email.substring(indexOfAt + 1).toLowerCase();
//		switch(form.patron_type.selectedIndex)
//		{
//			case 1: // Patron type = "Offshore UniSA student"
//			case 2: // Patron type = "UniSA Under graduate student"
//			case 3: // Patron type = "UniSA Post graduate student"
//				emailSuffixNew = "students.unisa.edu.au";
//				break;
//			case 4: // Patron type = "UniSA Staff"
//				emailSuffixNew = "unisa.edu.au";
//				break;
//			case 5: // Patron type = "OLA student"
//			default: // Any other patron type
//				emailSuffixNew = emailSuffix; // assume correctly entered by user
//				break;
//		}
//		var oldEmail = emailPrefixIn + emailSuffixIn;
//		var newEmail = emailPrefixIn + emailSuffixNew.toLowerCase();
//		if(newEmail != oldEmail)
//		{
//			var confirmMsg = "Warning:\n\nYou entered the following unrecognised email" +
//				" address:\n\n" + email + "\n\nYou may have intended to enter the" +
//				" following email address:\n\n" + newEmail + "\n\nIf you are sure that" +
//				" the email address you entered is correct, select\n\'OK\' to submit" +
//				" your form, otherwise select \'Cancel\' and reenter\nthe correct" +
//				" email address.\n\nThankyou";
//			return(confirm(confirmMsg));			
//		}
//	}
//	return true;
//}
