/***************************************************************
 * ------------Anglia Ruskin University javascripts----------- *
 * -------------------www.anglia.ac.uk------------------------ *
 *                                                             *
 * This .js file contains various scripts used throughout the  *
 * University's websites.                                      *
 *                                                             *
 * Many of the scripts were written bespokely, whilst some     *
 * come from the public domain. Credits have been left in      *
 * where appropriate.                                          *
 *                                                             *
 * Please feel free use use or modify any of these scripts,    *
 * but please do the authours the courtesy of leaving in their *
 * credits wherever they exist.                                *
 *                                                             *
 ***************************************************************/

/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.body.innerHTML = bodyText;
  return true;
}


/*
 * This displays a dialog box that allows a user to enter their own
 * search terms to highlight on the page, and then passes the search
 * text or phrase to the highlightSearchTerms function. All parameters
 * are optional.
 */
function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
{
  // This function prompts the user for any words that should
  // be highlighted on this web page
  if (!defaultText) {
    defaultText = "";
  }
  
  // we can optionally use our own highlight tag values
  if ((!textColor) || (!bgColor)) {
    highlightStartTag = "";
    highlightEndTag = "";
  } else {
    highlightStartTag = "<font style='color:" + textColor + "; background-color:" + bgColor + ";'>";
    highlightEndTag = "</font>";
  }
  
  if (treatAsPhrase) {
    promptText = "Please enter the phrase you'd like to search for:";
  } else {
    promptText = "Please enter the words you'd like to search for, separated by spaces:";
  }
  
  searchText = prompt(promptText, defaultText);

  if (!searchText)  {
    alert("No search terms were entered. Exiting function.");
    return false;
  }
  
  return highlightSearchTerms(searchText, treatAsPhrase, true, highlightStartTag, highlightEndTag);
}


/*
 * This function takes a referer/referrer string and parses it
 * to determine if it contains any search terms. If it does, the
 * search terms are passed to the highlightSearchTerms function
 * so they can be highlighted on the current page.
 */
function highlightGoogleSearchTerms(referrer)
{
  // This function has only been very lightly tested against
  // typical Google search URLs. If you wanted the Google search
  // terms to be automatically highlighted on a page, you could
  // call the function in the onload event of your <body> tag, 
  // like this:
  //   <body onload='highlightGoogleSearchTerms(document.referrer);'>
  
  //var referrer = document.referrer;
  if (!referrer) {
    return false;
  }
  
  var queryPrefix = "q=";
  var startPos = referrer.toLowerCase().indexOf(queryPrefix);
  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
    return false;
  }
  
  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }
  
  var queryString = referrer.substring(startPos + queryPrefix.length, endPos);
  // fix the space characters
  queryString = queryString.replace(/%20/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");
  // remove the quotes (if you're really creative, you could search for the
  // terms within the quotes as phrases, and everything else as single terms)
  queryString = queryString.replace(/%22/gi, "");
  queryString = queryString.replace(/\"/gi, "");
  
  return highlightSearchTerms(queryString, false);
}


/*
 * This function is just an easy way to test the highlightGoogleSearchTerms
 * function.
 */
function testHighlightGoogleSearchTerms()
{
  var referrerString = "http://www.google.com/search?q=javascript%20highlight&start=0";
  referrerString = prompt("Test the following referrer string:", referrerString);
  return highlightGoogleSearchTerms(referrerString);
}

/*
 * Takes the value sent from a form and does the highlight thingy
 */
function highlightFromForm()
{
  return highlightSearchTerms(document.highlightform.highlightphrase.value, false);
}

/*
 * Takes the value sent from the Prospectus Search form and does the highlight thingy
 */
function highlightFromProspSearch()
{
  return highlightSearchTerms(document.navForm2.theSearch.value, false);
}

/*
 * Spawns a new popup window. Well ain't that grand?
 */
function spawnwin(filename,width,height) 
{
	popupWindow = window.open('http://www.anglia.ac.uk'+filename,'NewWin','left=0,top=0,menubar=0,status=1,width='+width+',height='+height+',scrollbars=1,resizable=1,screenX=0,screenY=0,menubar=no,location=no,titlebar=no,toolbar=no,dependent=yes');
}

/*
 * Spawns a new popup window.
 */
function spawnextwin(exturl,width,height) 
{
	popupWindow = window.open(exturl,'NewWin','left=0,top=0,menubar=0,status=1,width='+width+',height='+height+',scrollbars=1,resizable=1,screenX=0,screenY=0,menubar=no,location=no,titlebar=no,toolbar=no,dependent=yes');
}

/* 
 * Virtual adviser form validation 
 */
function actionswitch(formname) {
	document.vapod2.method = "post";

	if (document.vapod2.urls[0].checked) {
		document.vapod2.action = "http://www.askadmission.net/apuug/aeresults.aspx";
	}
	if (document.vapod2.urls[1].checked) {
		document.vapod2.action = "http://www.askadmission.net/apupg/aeresults.aspx";
	}
	if (document.vapod2.urls[2].checked) {
		document.vapod2.action = "http://www.askadmission.net/apuint/aeresults.aspx";
	}
	if (document.vapod2.urls[3].checked) {
		document.vapod2.method = "get";
		document.vapod2.action = "http://emt.askadmissions.net/anglia_ac/aeresults.aspx";
	}

	if (document.vapod2.action) {
		return true;
	} else {
		alert('Please select the nature of your enquiry and enter a search term');
		return false;
	}
}
/* 
 * Virtual adviser form validation
 */
function actionswitch2(formname) {

	document.vapod3.method = "post";

	if (document.vapod3.urls[0].checked) {
		document.vapod3.action = "http://www.askadmission.net/apuug/aeresults.aspx";
	}
	if (document.vapod3.urls[1].checked) {
		document.vapod3.action = "http://www.askadmission.net/apupg/aeresults.aspx";
	}
	if (document.vapod3.urls[2].checked) {
		document.vapod3.action = "http://www.askadmission.net/apuint/aeresults.aspx";
	}

	if (document.vapod3.urls[3].checked) {
		document.vapod3.method = "get";
		document.vapod3.action = "http://emt.askadmissions.net/anglia_ac/aeresults.aspx";
	}

	if (document.vapod3.action) {
		return true;
	} else {
		alert('Please select the nature of your enquiry and enter a search term');
		return false;
	}
}
function vasf() {
	document.vapod2.quser.focus();
}

/* Prospectus Search Form */
function fpsearchsf() {
	document.fpcoursesearchform.theSearch.focus();
}
function cleardefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}


function goug(year, host, port, context) {
	box = document.courseform.ugselect;
	if (host=="www.anglia.ac.uk") {port="";}
	destination = "http://"+host+port+context+"/ruskin/en/home/prospectus/ugft"+year+box.options[box.selectedIndex].value;
	//destination = "http://www.anglia.ac.uk/ruskin/en/home/prospectus/ugft"+year+box.options[box.selectedIndex].value;
	if ((destination) && (box.options[box.selectedIndex].value!="#")) location.href = destination;
}

/* should really rationalise this one and the above */
function gopg(year, host, port, context) {
	box = document.courseform.pgselect;
	if (host=="www.anglia.ac.uk") {port="";}
	destination = "http://"+host+port+context+"/ruskin/en/home/prospectus/pgft"+year+box.options[box.selectedIndex].value;
	if ((destination) && (box.options[box.selectedIndex].value!="#")) location.href = destination;
}

/* UG 2007 */
function goug2007()
{
	// don't do anything with the first item in the list. 
	box = document.courseform.ugselect2007;
	if (box.selectedIndex > 0) {
		destination = "http://www.anglia.ac.uk/ruskin/en/home/prospectus/undergrad2007"+box.options[box.selectedIndex].value;
		if ((destination) && (box.options[box.selectedIndex].value!="#")) location.href = destination;
	}
}

/* IHSC 2006 */
function goihsc2006()
{
	box = document.courseform.ugselect2006;
	destination = "http://www.anglia.ac.uk/ruskin/en/home/prospectus/ihsc"+box.options[box.selectedIndex].value;
	if ((destination) && (box.options[box.selectedIndex].value!="#")) location.href = destination;
}

function checkProspectusRequestForm() {
	var vf = document.prospectusform;
	var alerter = "Please ensure that you have completed the following section(s) of the form:\n";

	if (vf.cFirstname.value=="") {
		alerter+="\nYour First name";
	}

	if (vf.cFamilyname.value=="") {
		alerter+="\nYour Family Name";
	}

	if ((!vf.cGender[0].checked && !vf.cGender[1].checked)) {
		alerter+="\nPlease specify your gender";
	}

	if (vf.cProspectus.selectedIndex==0) {
		alerter+="\nPlease select the type of prospectus required";
	}

    if (!(vf.cHouse) || vf.cHouse.value=="") {
		alerter+="\nYour House Name or Number";
	}

	if (!(vf.cStreet) || vf.cStreet.value=="") {
		alerter+="\nYour Street Name";
	}

	if (!(vf.cTown)  || vf.cTown.value=="") {
		alerter+="\nYour Town or City";
	}

	if (!(vf.cPostcode) || vf.cPostcode.value=="") {
		alerter+="\nYour Postcode";
	}

	if (alerter.length>76) {
		alert(alerter);
		return false;
	} else {
		//selectAll('resultsOptions');
		document.prospectusform.submit();
		return true;
	}
}

/***********************************************
* Image Thumbnail viewer- © Dynamic Drive (www.dynamicdrive.com)
* Last updated Sept 26th, 03'. This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
var ie=document.all
var ns6=document.getElementById&&!document.all
function ietruebody() {
	return (document.compatMode && document.compatMode!="BackCompat" && !window.opera)? document.documentElement : document.body
}

function enlarge(which, e, position, alt, imgwidth, imgheight) {
	if (ie||ns6) {
		crossobj=document.getElementById? document.getElementById("showimage") : document.all.showimage;
		if (position=="center") {
			pgyoffset=ns6? parseInt(pageYOffset) : parseInt(ietruebody().scrollTop);
			horzpos=ns6? pageXOffset+window.innerWidth/2-imgwidth/2 : ietruebody().scrollLeft+ietruebody().clientWidth/2-imgwidth/2;
			vertpos=ns6? pgyoffset+window.innerHeight/2-imgheight/2 : pgyoffset+ietruebody().clientHeight/2-imgheight/2;
			if (window.opera && window.innerHeight) {//compensate for Opera toolbar
				vertpos=pgyoffset+window.innerHeight/2-imgheight/2;
				vertpos=Math.max(pgyoffset, vertpos);
			}
		} else if (position=="tl") {
			var horzpos= 100;
			var vertpos= 70;
		} else if (position=="mouse") {
			var horzpos = 0;
			if (document.documentElement.clientWidth>imgwidth) {
				horzpos = (document.documentElement.clientWidth/2)-(imgwidth/2);
			}
			var vertpos = 110;
			if (document.documentElement.scrollTop>110) {
				vertpos = document.documentElement.scrollTop;
			}
		} else {
			var horzpos=ns6? pageXOffset+e.clientX : ietruebody().scrollLeft+event.clientX;
			var vertpos=ns6? pageYOffset+e.clientY : ietruebody().scrollTop+event.clientY;
		}
		crossobj.style.left=horzpos+"px";
		crossobj.style.top=vertpos+"px";

		crossobj.innerHTML='<div id="dragbar">Drag | <a href="javascript:closepreview();">Close</a></div><img src="'+which+'" onClick="closepreview()"/><div id="dragbar" style="width:'+imgwidth+'px;">'+alt+'</div>';
		crossobj.style.visibility="visible";
		return false;
	}
	else //if NOT IE 4+ or NS 6+, simply display image in full browser window
	return true;
}
function closepreview() {
	crossobj.style.visibility="hidden";
}

function drag_drop(e) {
	if (ie&&dragapproved) {
		crossobj.style.left=tempx+event.clientX-offsetx+"px";
		crossobj.style.top=tempy+event.clientY-offsety+"px";
	} else if (ns6&&dragapproved) {
		crossobj.style.left=tempx+e.clientX-offsetx+"px";
		crossobj.style.top=tempy+e.clientY-offsety+"px";
	}
	return false;
}
function initializedrag(e) {
	if (ie&&event.srcElement.id=="dragbar"||ns6&&e.target.id=="dragbar") {
		offsetx=ie? event.clientX : e.clientX;
		offsety=ie? event.clientY : e.clientY;

		tempx=parseInt(crossobj.style.left);
		tempy=parseInt(crossobj.style.top);

		dragapproved=true;
		document.onmousemove=drag_drop;
	}
}
document.onmousedown=initializedrag;
document.onmouseup=new Function("dragapproved=false");

/*****************************************************************
 *           Javascript to show or hide a nominated div
 *----------------------------------------------------------------
 * By:
 * Steve Arnott
 * Anglia Ruskin University
 * 16th March 2006
 * www.anglia.ac.uk
 * 
 * Usage:
 * You must have uniquely named divs, eg
 * <div id="ftcontent">
 * 
 * Use CSS to hide this div, eg
 * #ftcontent {
 *   display: none;
 * }
 * 
 * Create a show/hide controller somewhere on your page, eg
 * <a href="javascript:showhide('ftcontent')">Show/hide ftcontent</a>
 * 
 * Tips:
 * Keep your controls and the content in separate divs, so you can 
 * also show/hide the controls div if required, eg between noscript
 * tags for users who don't allow javascript, like this:
 * <noscript>
 * <style type="text/css">
 * #ftcontent {
 *   display: block;
 * }
 * #ftcontrols {
 * 	 display: none;
 * }
 * </style>
 * </noscript>
 * 
 * You can force a div to show onload by using an override.
 * Place this code at the bottom of your page, between <script> tags:
 * showhide('ftcontent', 'block')
 * 
 * You are welcome to use, distribute or modify this script, but
 * please leave this comment in.
 *****************************************************************/


// used for wrapping up a collection of paragraphs in a show/hide dhtml thing.
// written by Steve, July 2007.
function showhidepara(divid, showMoreText, showLessText) {
	var html = document.getElementById(divid);

	var control = document.getElementById(divid+"-control");

	if (html.style.display == "block") {
		html.style.display = "none";
		control.innerHTML = "<a href=\"javascript:showhidepara('" +divid+ "', '" +showMoreText + "', '"+showLessText + "')\">"+showMoreText + "</a>";
	} else {
		html.style.display = "block";
		//control.innerHTML = "<a href=\"javascript:showhidepara('"+divid+"')\">Hide details...</a>";
		control.innerHTML = "<a href=\"javascript:showhidepara('" +divid+ "', '" +showMoreText + "', '"+showLessText + "')\">"+showLessText + "</a>";
	}
}

//Used for debugging
function drawarray() {
	document.write('<table border="1">');
	for (var i=0; i<states.length; i++) {
		document.write('<tr><td>'+states[i][0]+'</td><td>'+states[i][1]+'</td></tr>');
	}
	document.write('</table>');
}

/* /Show or hide a nominated div */
//setCookie("big", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");

//alert(document.cookie);
function showhide(divid, override, imgref) {
	var thisstate;
	var alreadyhere = false;
	var thiscookie = divid.substring(0, divid.indexOf("content"));
	var currentstate = 1; //by default all divs are shown
	var newstate = 1;
	var swapstate = "";

	//Check to see is cookies work. If so, use them, otherwise use forgetful standard variables
	setCookie("test", "true");
	if (getCookie("test")=="true") {
		deleteCookie("test");
		eval("if(getCookie(\""+thiscookie+"\")==1){currentstate=1;newstate=0;} else if(getCookie(\""+thiscookie+"\")==0){currentstate=0;newstate=1;} else {currentstate=1;newstate=0;}");
	} else {
		eval("if (typeof "+thiscookie+"=='undefined') {"+thiscookie+"=1;}");
		eval("if("+thiscookie+"==1){currentstate=1;newstate=0;"+thiscookie+"=0;} else if("+thiscookie+"==0){currentstate=0;newstate=1;"+thiscookie+"=1;} else {currentstate=1;newstate=0;"+thiscookie+"=0;}");
	}
	//alert(document.cookie);

	//See if there's an override
	if (override.length>0) {
		if (override=='block') {
			newstate = 1;
		} else {
			newstate = 0;
		}

		if (override=='none') {
			newstate = 0;
		} else {
			newstate = 1;
		}
	}

	//Work out the toggle
	if (newstate==0) {
		swapstate = 'none';
		eval("setCookie(\""+thiscookie+"\", \"0\");");
	} else {
		swapstate = 'block';
		eval("setCookie(\""+thiscookie+"\", \"1\");");
	}

//alert("I'm going to: "+newstate+" "+divid+" with the following override: ["+override+"]");

	//Do the show/hide in all browser versions
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval("document.all."+divid+".style.display = swapstate");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[divid].display = swapstate;
	}
	if (document.getElementById &&!document.all) { //Everything else
		obj = document.getElementById(divid);
		obj.style.display = swapstate;
	}

	//Flip the image
	if (imgref.length>0) {
		flipper(imgref, newstate);
	}

	//Keep the status bar tidy
	var msg;
	if (newstate==1) {
		msg = 'shown';
	} else {
		msg = 'hidden';
	}
	window.status = 'Content '+msg;
}

/* Image swapper function for prospectus pages */
if (document.images) {
	flip = new Image();
	flip.src = "http://www.anglia.ac.uk/medianew/images/showhide.gif";
	flip2 = new Image();
	flip2.src = "http://www.anglia.ac.uk/medianew/images/showhide2.gif";
	triangle = new Image();
	triangle.src = "http://www.anglia.ac.uk/medianew/images/triangle.gif";
	triangle2 = new Image();
	triangle2.src = "http://www.anglia.ac.uk/medianew/images/triangle2.gif";
}
var ftflip = 0;
var ptflip = 0;
var othercoursesflip = 0;
function flipper(whichimg, state) {
	if (document.images){
		if (state==1) {
			document[whichimg].src=triangle2.src;
		} else {
			document[whichimg].src=triangle.src;
		}
	}
	return;
}




function showhide2(divid, override, imgref, thisNum, inPartTime) {
	var cookieStrReceived = "";

	var cookieName= (inPartTime==true ? "subs_pt": "subs");
	//alert("cookieName="+cookieName);

	if (getCookie(cookieName) && getCookie(cookieName).length > 0) {
		cookieStrReceived = getCookie(cookieName);
	}
	var newstate = 1;

	var thisStatus = 0;
	var cookieBefore = "";
	var maxLength = 0;
	var cookieAfter = "";
	if (cookieStrReceived.length > 0) {
		thisStatus = cookieStrReceived.substring(thisNum, thisNum+1);
		cookieBefore = cookieStrReceived.substring(0, thisNum);
		maxLength = cookieStrReceived.length;
		cookieAfter = cookieStrReceived.substring(thisNum+1, maxLength);
	}
	//alert("divid="+divid+"\n"+"override="+override+"\n"+"imgref="+imgref+"\n"+"cookieStrReceived="+cookieStrReceived+"\nthisNum="+thisNum+"\nthisStatus="+thisStatus);

	//See if there's an override
	if (override.length>0) {
		if (override=='block') {
			newstate = 1;
		}
		if (override=='none') {
			newstate = 0;
		}
	} else { //If there's no override, swap based on current status
		if (thisStatus == 1) {
			newstate = 0;
		} else {
			newstate = 1;
		}
	}	

	//Work out the toggle
	if (newstate==0) {
		swapstate = 'none';
	} else {
		swapstate = 'block';
	}

	//alert("I'm going to: "+newstate+" "+divid+" with the following override: ["+override+"]");

	//Debugging
	var cookieNew = cookieBefore + newstate + cookieAfter;
	//alert("thisNum="+thisNum+"\ncookieBefore="+cookieBefore+"\ncookieAfter="+cookieAfter+"\ncookieNew="+cookieNew+"\nmaxLength="+maxLength+"\ngetCookie(subs)="+getCookie('subs')+"\nthisStatus="+thisStatus);

	//Update the cookie
	setCookie(cookieName, cookieNew,null,"/");
	

	//Do the show/hide in all browser versions
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval("document.all."+divid+".style.display = swapstate");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[divid].display = swapstate;
	}
	if (document.getElementById &&!document.all) { //Everything else
		obj = document.getElementById(divid);
		obj.style.display = swapstate;
	}

	//Flip the image
	if (imgref.length>0) {
		flipper(imgref, newstate);
	}

	//Keep the status bar tidy
	var msg;
	if (newstate==1) {
		msg = 'shown';
	} else {
		msg = 'hidden';
	}
	window.status = 'Content '+msg;

	return;
}

/* Pause for sent number of milliseconds */
function pausecomp(millis) 
{
date = new Date();
var curDate = null;

do { var curDate = new Date(); } 
while(curDate-date < millis);
} 


/* show or hide */
function forceshowhide(divid, swapstate) {
	//Do the show/hide in all browser versions
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval("document.all."+divid+".style.display = swapstate");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[divid].display = swapstate;
	}
	if (document.getElementById &&!document.all) { //Everything else
		obj = document.getElementById(divid);
		obj.style.display = swapstate;
	}
}



/* shows/hides divs based on what's stored in the cookies */
function showhideinit2(allsubs, inPartTime) {
	var arrSubsSplit = allsubs.split(", ");
	var c = 0;
	var cookieStrReceived;
	var cookieName= (inPartTime==true ? "subs_pt": "subs");
	
//	forceshowhide('allSubjects', 'none');
//	forceshowhide('loading', 'block');
	
	setCookie("test", "foo");
	if (getCookie("test")=="foo") {
		deleteCookie("test");
		if (getCookie(cookieName) && getCookie(cookieName).length > 0) {
			cookieStrReceived = getCookie(cookieName);
			var r = 0;

			while (c<arrSubsSplit.length-1) {
				var thisNum = arrSubsSplit[c].substring(3, arrSubsSplit[c].length);
		//		eval('var thisStatus = "sub"+thisNum+"="+cookieStrReceived.substring('+thisNum+', '+thisNum+'+1);');
				eval('var thisStatus = cookieStrReceived.substring('+thisNum+', '+thisNum+'+1);');
		//		alert(thisStatus);

				if (thisStatus==0 || thisStatus=="") {
					eval("showhide2(\"sub"+thisNum+"content\", \"none\", \"sub"+thisNum+"flip\", "+thisNum+", "+inPartTime+");");
				} else {
					eval("showhide2(\"sub"+thisNum+"content\", \"block\", \"sub"+thisNum+"flip\", "+thisNum+", "+inPartTime+");");
				}
//				if (c == arrSubsSplit.length-2) {
					//Now that we're done, show the page
//					forceshowhide('loading', 'none');
//					forceshowhide('allSubjects', 'block');
//				}

				c++;
			}
		} else {
			while (c<arrSubsSplit.length-1) {
				var thisNum = arrSubsSplit[c].substring(3, arrSubsSplit[c].length);
				eval("showhide2(\"sub"+thisNum+"content\", \"none\", \"sub"+thisNum+"flip\", "+thisNum+", "+inPartTime+");");
				c++;
			}
		}
	}
	return;
}







function showhideinit(allsubs) {
	var cstring = "; "+document.cookie+"; ";
	var clength = document.cookie.length;
	var stepper = 0;
	var thisstart = 0;
	var thisend = 0;
	var thisvarname = "";
	var thisval = "";
	var gonnashow = "";

	//Check to see if cookies are allowed. If not, expand all
	setCookie("test", "true");
	if (getCookie("test")=="true") {
		deleteCookie("test");
		//Remember previous settings
		if (getCookie("returner")=="true") {
			do {
				thisstart = cstring.indexOf("; ", stepper);
				thisend = cstring.indexOf("; ", thisstart+1);
				thisvarname = cstring.substring(thisstart+2, cstring.indexOf("=", thisstart));
				thisval = cstring.substring(thisstart+thisvarname.length+3, cstring.indexOf("; ", thisstart+thisvarname.length+1));
				if (thisval==1) {
					//eval("showhide(\""+thisvarname+"content\", \"block\", \""+thisvarname+"flip\");");
					gonnashow += thisvarname+"\n";
				}
				if (thisval==0) {
					eval("showhide(\""+thisvarname+"content\", \"none\", \""+thisvarname+"flip\");");
					//alert("gonna hide "+thisvarname);
				}
				stepper = thisend;
			} while(stepper<clength);
		} else { //Contract all on first visit
			setCookie("returner", "true");
			collapseall(allsubs);
		}
	} else { //Expand all, as 'memory' will be missing
			collapseall(allsubs);
	}
	return;
}

function expandall(allsubs, inPartTime) {
	var arrSubsSplit = allsubs.split(", ");
	var c = 0;

	while (c<arrSubsSplit.length-1) {
		//alert("showhide2(\""+arrSubsSplit[c]+"content\", \"block\", \""+arrSubsSplit[c]+"flip\", "+c+", "+inPartTime+");");
		eval("showhide2(\""+arrSubsSplit[c]+"content\", \"block\", \""+arrSubsSplit[c]+"flip\", "+c+", "+inPartTime+");");
		c++;
	}
	return;
}

function collapseall(allsubs, inPartTime) {
	var arrSubsSplit = allsubs.split(", ");
	var c = 0;
	while (c<arrSubsSplit.length-1) {
		//alert("showhide2(\""+arrSubsSplit[c]+"content\", \"none\", \""+arrSubsSplit[c]+"flip\", "+c+", "+inPartTime+");");
		eval("showhide2(\""+arrSubsSplit[c]+"content\", \"none\", \""+arrSubsSplit[c]+"flip\", "+c+", "+inPartTime+");");
		c++;
	}
	return;
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
