
/* 
-----------------------------------------------------------------
	trs
	trs.js
	
	Created 08.11.2007 by DS
	Last Updated: See SVN	
-----------------------------------------------------------------
*/

	window.onload = init;
	
	function init()
	{

		generate_er_display_elements();		
		prep_search_form_to_get_er_count();
		add_select_list_controls();			
	}

/* 
-----------------------------------------------------------------
	Ajax
-----------------------------------------------------------------
*/
	
	function get_http_object() 
	{
		// BP Ajax
		
		var xhr = false;
		if (window.XMLHttpRequest) // object detection
		{
			xhr = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
		}
		else if (window.ActiveXObject) 
		{ // ie, 
			try // Mac IE5 supports ActiveX but not XMLHTTP
			{
				xhr = new ActiveXObject("Msxml2.XMLHTTP"); // IE6+ (newer ActiveX object)
			} 
			catch(e) // catch the error so it's not displayed
			{
				try 
				{
					xhr = new ActiveXObject("Microsoft.XMLHTTP"); // IE5.5
				}
				catch (e) 
				{
					xhr = false;	
				}
			}
		}
		return xhr;
	}
	
	function prep_search_form_to_get_er_count()
	{
		if (!document.getElementById)
		{ // object detection
			return;	
		}
		
		if (!document.getElementById('teacher-search'))
		{ // check element exists
			return;	
		}
		
		var form_elements = document.getElementById('teacher-search').elements;

		for (var i=0; i<form_elements.length; i++)
		{
			form_elements[i].onclick = function()
			{
				var data = '';
				for (var i=0; i<form_elements.length; i++)
				{
					data += form_elements[i].name;
					data += '=';
					data += escape(form_elements[i].value);
					data += '&';
				}
				
				get_er_count(data, '/resources/teacher-search-result-count.php');
			}
		}	
	}
	
	function generate_er_display_elements()
	{
		if ((!document.getElementById) || (!document.createElement))
		{ // object detection
			return;	
		}
		
		if (!document.getElementById('teacher-search'))
		{ // check element exists
			return;	
		}		
		
		// er1 - top of form
		
		window['er1'] = new Object(); // global reference
		
		window['er1'].p = document.createElement('p');
		window['er1'].p.setAttribute('id', 'expected-results');
		window['er1'].p.className = 'note';
		
		window['er1'].span = document.createElement('span');
		/*
			edited by Trevor George 18/1/2008
			This functionality is no longer required as pagination has been set up.
			So I have removed the code to display the text
		
		*/
		window['er1'].spanText = document.createTextNode(' ');
		
		//window['er1'].spanText = document.createTextNode('Expected results about: ');
		window['er1'].span.appendChild(window['er1'].spanText);
		
		window['er1'].strong = document.createElement('strong');
		window['er1'].strongText = document.createTextNode('0');		
		window['er1'].strong.appendChild(window['er1'].strongText);
		
		window['er1'].br = document.createElement('br'); // clearing element
		
		window['er1'].p.appendChild(window['er1'].span);
		window['er1'].p.appendChild(window['er1'].strong);	
		window['er1'].p.appendChild(window['er1'].br);	
		
		// er2 - bottom of form		
		/*
			edited by Trevor George 18/1/2008
			This functionality is no longer required as pagination has been set up.
			So I have removed the code to display the text
		*/
		/*window['er2'] = new Object(); // global reference
		
		window['er2'].p = document.createElement('p');
		window['er2'].p.setAttribute('id', 'expected-results');
		window['er2'].p.className = 'note';
		
		window['er2'].span = document.createElement('span');
		
		
		window['er2'].spanText = document.createTextNode('Expected results about: ');
		window['er2'].span.appendChild(window['er2'].spanText);
		
		window['er2'].strong = document.createElement('strong');
		window['er2'].strongText = document.createTextNode('0');		
		window['er2'].strong.appendChild(window['er2'].strongText);
		
		window['er2'].br = document.createElement('br'); // clearing element
		
		window['er2'].p.appendChild(window['er2'].span);
		window['er2'].p.appendChild(window['er2'].strong);	
		window['er2'].p.appendChild(window['er2'].br);		
		
		// output er1

		form_note = document.getElementById('teacher-search').getElementsByTagName('p')[0];		
		form_note.parentNode.insertBefore(window['er1'].p, form_note);		
		
		// output er2
		
		form_search_div = document.getElementById('teacher-search').getElementsByTagName('div')[1];		
		form_search_div.parentNode.insertBefore(window['er2'].p, form_search_div);	*/		
	}
	
	function get_er_count(form_data, server_page)
	{
		var request = get_http_object(); // x-browser XMLHttprequest object	
		
		if (request)
		{ // if browser supports Ajax..
			
		
			request.onreadystatechange = function() 
			{ 
				display_er_count(request); // handler for event triggered by server. Anonymous function used to pass a parameter without running the function now.
			}; 
			
			

			
			request.open("POST", server_page, true); // true: asynchronous request - don't wait for result. name php page for db query
			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // tell the server that url-encoded data is being sent
			request.send(form_data); // send form data to php page

			return true;
		}	
		else
		{
			return false;
		}
	}
	
	function display_er_count(request)
	{
			
		if (request.readyState == 4)
		{ // the server has finished sending a response
			
			
			
			
			if (request.status == 200 || request.status == 304)
			{ // 200: response status code for ok, 304: 'not modified' code used by Opera
				

				// update er1
				window['er1'].strong.removeChild(window['er1'].strongText);	
				window['er1'].strongText = document.createTextNode(request.responseText);				
				window['er1'].strong.appendChild(window['er1'].strongText);	
				
				// update er1				
				window['er2'].strong.removeChild(window['er2'].strongText);	
				window['er2'].strongText = document.createTextNode(request.responseText);				
				window['er2'].strong.appendChild(window['er2'].strongText);					
			}
		}
	}
	
/* 
-----------------------------------------------------------------
	Select Control
	Add select all/none select options control, without changing template markup
-----------------------------------------------------------------
*/	
	
	function add_select_list_controls()
	{
		var select_lists = document.getElementsByTagName('select');
		
		for (var s=0; s<select_lists.length; s++)
		{
			if (select_lists[s].multiple == true) // if allows multiple selection
			{	
				select_lists[s].className += ' select-controlled'; // not sure what's on there by default.. there is a multi-select class
			
				create_select_list_controls(s);	
			}
		}
	}
	
	function get_previous_sibling(el)
	{
	//	http://www.thewatchmakerproject.com/journal/329/finding-html-elements-using-javascript-nextsibling-and-previoussibling
		prev_el = el.previousSibling;
		while(prev_el.innerHTML == null) // for IE, to bypass text nodes
		{
			prev_el = prev_el.previousSibling; 
		}	
		
		return prev_el;
	}
	
	function do_hover(el)
	{
		if (el.className.indexOf('last') != -1)
		{
			el.className = 'a last hover';
		}
		else if (el.className.indexOf('a') != -1)
		{
			el.className = 'a hover';
		}
	}

	function do_blur(el)
	{
		if (el.className.indexOf('last') != -1)
		{
			el.className = 'a last blur';
		}
		else if (el.className.indexOf('a hover') != -1)
		{
			el.className = 'a blur';
		}
	}	
	
	function create_select_list_controls(s)
	{
		// make into object/class so that hover/blur targets correct element
	
		var list_id = document.getElementsByTagName('select')[s].id;
		
		window['sc-' + s] = new Object();

		// p
		window['sc-' + s].p = document.createElement('p');				
		window['sc-' + s].pText = document.createTextNode('Select: ');
		window['sc-' + s].p.appendChild(window['sc-' + s].pText);
		
		// li
		window['sc-' + s].li1 = document.createElement('li');	
		window['sc-' + s].li1.className = 'a';			
		window['sc-' + s].li1Text = document.createTextNode('All');
		window['sc-' + s].li1.appendChild(window['sc-' + s].li1Text);			
		window['sc-' + s].li1.onclick = function()
		{
			selectListOptions(list_id, 1);
		}
		window['sc-' + s].li1.onmouseover = function()
		{
			do_hover(window['sc-' + s].li1);
		}
		window['sc-' + s].li1.onmouseout = function()
		{
			do_blur(window['sc-' + s].li1);
		}			
		
		// li
		window['sc-' + s].li2 = document.createElement('li');	
		window['sc-' + s].li2.className = 'a last';		
		window['sc-' + s].li2Text = document.createTextNode('None');
		window['sc-' + s].li2.appendChild(window['sc-' + s].li2Text);			
		window['sc-' + s].li2.onclick = function()
		{
			selectListOptions(list_id, 0);
		}	
		window['sc-' + s].li2.onmouseover = function()
		{
			do_hover(window['sc-' + s].li2);
		}
		window['sc-' + s].li2.onmouseout = function()
		{
			do_blur(window['sc-' + s].li2);
		}				
		
		// ul > li + li
		window['sc-' + s].ul = document.createElement('ul');			
		window['sc-' + s].ul.appendChild(window['sc-' + s].li1);
		window['sc-' + s].ul.appendChild(window['sc-' + s].li2);	
		
		// div > p + ul
		window['sc-' + s].div = document.createElement('div');	
		window['sc-' + s].div.className = 'select-control';			
		window['sc-' + s].div.appendChild(window['sc-' + s].p);
		window['sc-' + s].div.appendChild(window['sc-' + s].ul);			

		// insert into dom, after select
		// insertAfter(parent, node, referenceNode);
		
		insertAfter(document.getElementById(list_id).parentNode, window['sc-' + s].div, document.getElementById(list_id)); 			
	}
	
	function insertAfter(parent, newElement, referenceElement)
	{
		parent.insertBefore(newElement, referenceElement.nextSibling);
	} 

	
	function selectListOptions(listId, toggle)
	{
		var list = document.getElementById(listId);
	
		switch(toggle)
		{
			case 1:	
			
				for (var i=0; i<list.options.length; i++) 
				{				
					list.options[i].selected = true;
				}
				
				break;
				
			case 0:
				
				for (var i=0; i<list.options.length; i++) 
				{
					list.options[i].selected = false;
				}		
		
				break;
		}
	}		

