/////////////////////////////////////////
//
//  Terry Nelson - 2001
//  --------------    
//  Mostly taken from MMI dhtml_home
//  library.  Used for Webtoolkit apps
//
/////////////////////////////////////////

function stylesheetchooser()
{
  if(navigator.userAgent.indexOf('Mac') == 1)
  { 
	document.write ('<link rel="stylesheet" href="http://hp1.gcal.ac.uk/macnet.css">');
  }

  else if(navigator.userAgent.indexOf('Mac') != 1)
  {
	if (navigator.appName == "Netscape")
	{
		document.write ('<link rel="stylesheet" href="http://hp1.gcal.ac.uk/macnet.css">');
	}
		else
	{
		document.write ('<link rel="stylesheet" href="http://hp1.gcal.ac.uk/style.css">');
	}
  }
}

function return_to_menu()
{
var cookie1 = ""
cookie1 = getCookie("version") 

	if (cookie1 == 'flash')
	{
				parent.document.location.href = "http://www.gcal.ac.uk/home.html";
	}
		else
	{
				parent.document.location.href = "http://www.gcal.ac.uk/home_dhtml.html";
	}
}

function popwin(page,wid,hght,winname)
{
	w=window.open(page, winname, "width="+wid+",height="+hght+",scrollbars=no");
	w.focus();
}

function popwins(page,wid,hght,winname)
{
	w=window.open(page, winname, "width="+wid+",height="+hght+",scrollbars=yes");
	w.focus();
}

function getCookie(name) 
{
    name += "="; // append '=' to name string
    var i = 0; // index of first name=value pair

    while (i < document.cookie.length) 
	{
      		var offset = i + name.length;
      		if (document.cookie.substring(i, offset) == name) 
		{ 		
        		var endstr = document.cookie.indexOf(";", offset);
        		if (endstr == -1) endstr = document.cookie.length;
          		return unescape(document.cookie.substring(offset, endstr)); 
        	}
        i = document.cookie.indexOf(" ", i) + 1; // move i to next name=value pair
        if (i == 0) break; // no more values in cookie string
      }

    return null;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

/* Functions that swaps images. */
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/* Functions that handle preload. */
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/////////////////////////////////////////
//
//  Data validation functions
//
/////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// Date validation function
//
function ValidateDateField(s,desc)
{
	// Test for a string
	if (s.value.length > 0)
	{
		// Create an array to split the date into (dd/mm/yyyy)
		strarr = new Array ()
		strarr = s.value.split("/");
		
		// 3 array elements means day, month, and year
		if (strarr.length == 3)
		{
			// Test the value of each element falls in an acceptable range
			if ((parseInt(strarr[0]) < 0) || (parseInt(strarr[0]) >31) || isNaN(parseInt(strarr[0]))){
				alert(desc + " is invalid, please re-enter in the format dd/mm/yyyy")
				s.focus();
				return false;
			}
			if ((parseInt(strarr[1]) < 0) || (parseInt(strarr[1]) >12) || isNaN(parseInt(strarr[1]))){
				alert(desc + " is invalid date, please re-enter in the format dd/mm/yyyy")				
				s.focus();
				return false;
			}
			
			// Ensure all characters in the year portion of the date are numeric (the isNaN doesn't work
			// if the year begins with numbers and ends with characters)
						
			for(i=0;i<strarr[2].length;i++){
				if (isNaN(parseInt(strarr[2].substr(i,1)))){
					alert(desc + " is invalid date, please re-enter in the format dd/mm/yyyy")				
					s.focus();
					return false;
				}
			}
			if ((parseInt(strarr[2]) <= -4712) || (parseInt(strarr[2]) > 4712) || isNaN(parseInt(strarr[2]))){
				alert(desc + " is invalid date, please re-enter in the format dd/mm/yyyy")				
				s.focus();
				return false;
			}
			return true;
		}
		alert(desc + " is invalid date, please re-enter in the format dd/mm/yyyy")		
		s.focus();
		return false;
	}
	alert(desc + " is invalid date, please re-enter in the format dd/mm/yyyy")
	s.focus();
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Text validation function
//
function validateText(s,desc)
{
	// test for a string
	if (s.value.length > 0)
	{
		return true;
	}
	alert(desc + " is a required field...")				
	s.focus();		
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Number validation function
//
function validateNum(s, desc)
{
	// Test to see if the value converts to a number
	if (parseInt(s.value) >= 0)
	{
		return true;
	}
	alert(desc + "is an invalid Number, please re-enter")				
	s.focus();	
	return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// E-Mail address validation function
//
function validateEmail(s)
{
	// Test for a string
	if (s.value.length > 0)
	{
		// Return false if e-mail field does not contain a '@' and '.' .
		if (s.value.indexOf ('@',0) == -1 || s.value.indexOf ('.',0) == -1)
      			{
			alert("Invalid EMail Address, please re-enter")				
			s.focus();	
			return false;			
			}
		return true;
	}
	alert("Invalid EMail Address, please re-enter")				
	s.focus();	
	return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// In string function to test for valid substring, accomodates JavaScripts lack of VBScripts InStr() function
//
function own_instring(c)
{
	var checkOK = "0123456789-+-. ()\t\r\n\f";
	var ret  = false;

  		for (j = 0;  j < checkOK.length;  j++)
		{
      			if (c != checkOK.charAt(j))
			{
			continue;
			}
			else
			{
			ret = true;
			break;
			}
		}
	return ret;
}
