/* -*- java -*- */

/****** public_html/advanced.js
 * NAME
 * advanced.js
 * SYNOPSIS
 * This Javascript file contains functions for manipulating the lists of reigns/sessions in the advanced search page.
 * The sessions/parliaments menu should update itself when the selection in the reigns menu changes, or the type changes.
 * AUTHOR
 * Swithun Crowe
 * CREATION DATE
 * 20090218
 ******
 */

/*
 maximum size of selects
*/
var selectSize = 8;
var type = "trans";
var delim = "-";

/****f* advanced.js/emptyList
 * NAME
 * emptyList
 * SYNOPSIS
 * Loop over all existing options in select menu, deleting them.
 * Then add one entry, Select a reign.
 * ARGUMENTS
 * * sel - select menu object to manipulate.
 * AUTHOR
 * Swithun Crowe
 * CREATION DATE
 * 20090218
 ******
 */
function emptyList(sel) //{{{
{
   /*
    * count down removing options
		*/
	 for (i = sel.length; i >= 0; -- i) 
		 {
				sel.options[i] = null;
		 }
	 /*
		* add dummy option
		*/
	 sel.options[0] = new Option("Select a reign", "");
	 sel.size = 1;
}
//}}}

/****f* advanced.js/populateList
 * NAME
 * populateList
 * SYNOPSIS
 * Given a select menu, populate it with the array of arrays passed.
 * When a reign is selected, the sessions for the reign must be loaded, so the sessions select menu object is passed, with an array of sessions for the reign/type.
 * ARGUMENTS
 * * sel - select menu object to manipulate.
 * * arr - associative array, holding sessions for chosen reign.
 * AUTHOR
 * Swithun Crowe
 * CREATION DATE
 * 20090218
 * NOTES
 * Needs access to the global variable, 'selectSize', which is the default/maximum size of the sessions/parliaments menu.
 ******
 */
function populateList(sel, arr) //{{{
{
	 if (!arr) {
			return;
	 }
	 
	 var offset = sel.options.length;
	 
	 if (1 == offset) 
		 {
				//sel.options[0] = new Option('All sessions', '');
				sel.options[0] = null;
				offset = 0;
		 }
	 
	 /*
		* add items from array
		*/
	 for (var i = 0; i < arr.length; ++ i) 
		 {
				sel.options[i + offset] = new Option(arr[i][1], arr[i][0]);
		 }
	 
	 /*
		* set size of select menu
		*/
	 sel.size = i + offset < selectSize ? i + offset : selectSize;
}
//}}}

/****f* advanced.js/updateParliaments
 * NAME
 * updateParliaments
 * SYNOPSIS
 * This function is called when the reign menu selection is updated.
 * First of all, the session select menu has all of its items deleted.
 * For each selected reign, of the selected type, the populateList function is called with the session (parliament) menu and the list of sessions.
 * A cookie is set to remember the selected reigns, so submitting the form, and then going back will reload the same selections as before.
 * ARGUMENTS
 * * reignsSel - select menu used by reigns.
 * * parliamentsSel - select menu used by sessions/parliaments.
 * AUTHOR
 * Swithun Crowe
 * CREATION DATE
 * 20090218
 * NOTES
 * Needs access to the global variable, 'delim', used as the delimiter in joining reigns together so they can be stored in a cookie string.
 ******
 */
function updateParliaments(reignsSel, parliamentsSel) //{{{
{
	 /*
		* empty parliament list first
		*/
	 emptyList(parliamentsSel);
	 var cookie_string = "";
	 
	 /*
		* get selected reigns
		*/
	 for (var i = 0; i < reignsSel.options.length; ++ i) 
		 {
				if (reignsSel.options[i].selected) 
					{
						 var reign = reignsSel.options[i].value;
						 if (reign) 
							 {
									populateList(parliamentsSel, reigns[type][reign]);
									cookie_string += reign + delim;
							 }
					}
		 }
	 
	 /*
	  * remember reigns selected
		*/
	 setCookie(cookie_string);
}
//}}}

/****f* advanced.js/updateType
 * NAME
 * updateType
 * SYNOPSIS
 * This gets called when the type selection menu changes.
 * The type value is remembered in a global variable, and the updateParliaments function is called, as this may require a refresh of the sessions menu.
 * ARGUMENTS
 * * typeSel - select menu used by types (trans/ms).
 * AUTHOR
 * Swithun Crowe
 * CREATION DATE
 * 20090218
 * NOTES
 * Needs access to, and writes to, the global variable, 'type'.
 ******
 */
function updateType(typeSel) //{{{
{
	 /*
		* set type
		*/
	 type = typeSel.options[typeSel.selectedIndex].value;
	 
	 /*
		* need to refresh sessions menu with new type
		*/
	 updateParliaments(document.getElementById('queryform').elements['reigns[]'], document.getElementById('queryform').elements['parliaments[]']);
}
//}}}

/****f* advanced.js/setCookie
 * NAME
 * setCookie
 * SYNOPSIS
 * Remember the reigns selected, to help with rebuilding page on a reload.
 * ARGUMENTS
 * * reign - string - the reigns to remember.
 * AUTHOR
 * Swithun Crowe
 * CREATION DATE
 * 20090218
 ******
 */
function setCookie(reign) { //{{{
	 document.cookie = "reign=" + reign + ";path=/";
}
//}}}

/****f* advanced.js/checkCookie
 * NAME
 * checkCookie
 * SYNOPSIS
 * Examines the cookie string and finds the reign value.
 * This must be split up using a delimiter and for each reign, the sessions/parliaments select menu gets populated.
 * AUTHOR
 * Swithun Crowe
 * CREATION DATE
 * 20090218
 ******
 */
function checkCookie() { //{{{
	 var splits = document.cookie.split(";");
	 for (var i = 0; i < splits.length; ++ i) {
			var s = splits[i];
			s = s.replace(/^\s+|\s+$/g, '');
			if (-1 != s.indexOf("reign=")) {
				 var reign = s.substring(6, s.length);
				 var reign_split = reign.split(delim);
				 for (var j = 0; j < reign_split.length; ++ j) 
					 {
							if (reign_split[j]) 
								{
									 populateList(document.getElementById('queryform').elements['parliaments[]'],
																reigns[type][reign_split[j]]);
								}
					 }
				 break;
			}
	 }
}
//}}}

