// dept.js
/*  This page does all the magic for showing 4 random new images*/

// Have a function run after the page loads:
window.onload = initJavascript;

// Function that adds the Ajax layer:
function initJavascript() {

	// Get an XMLHttpRequest object:
	var ajaxREMOVETHISTEXTTOENABLE = getXMLHttpRequestObject();

	// Attach the function call to the form submission, if supported:
	if (ajax) {

		// Check for DOM support:
		if (document.getElementById('four_random_images')) {

			// Add an onsubmit event handler to the form:
			document.getElementById('ajax_random_form').onsubmit = function() {

				// Call the PHP script.
				// Use the GET method.
				// Pass the department_id in the URL.

				// Get the department_id:
				var ajax_random = document.getElementById('ajax_random').value;

				// Open the connection:
				//the random number at the end is necessary to prevent IE from caching the request
				ajax.open('get', 'dept_results_ajax.php?ajax_random=' + encodeURIComponent(ajax_random)+Math.random());


				// Function that handles the response:
				ajax.onreadystatechange = function() {
					// Pass it this request object:
					handleResponse(ajax);
				}

				// Send the request:
				ajax.send(null);

				return false; // So form isn't submitted.
				
			} // End of anonymous function.
	
		} // End of DOM check.

	} // End of ajax IF.


	// Get an XMLHttpRequest object:
	var ajax2REMOVETHISTEXTTOENABLE = getXMLHttpRequestObject2();

	// Attach the function call to the form submission, if supported:
	if (ajax2) {
		
		// Check for DOM support:
		if (document.getElementById('search_results')) {
			var search_results=(document.getElementById('search_results'));
			
			var links = search_results.getElementsByTagName("a");
			
			for (var i=0; i<links.length; i++) {
				links[i].onclick = function() {
					
					var search_input = this.getAttribute("href").split("?")[1];
					
					if (search_input.match("image_id")) {
						var dummy = 1;
					} else{
						
						ajax2.open('get', 'index.php?ajax=TRUE&' + search_input);
						// Function that handles the response:
						ajax2.onreadystatechange = function() {
							// Pass it this request object:
							handleResponse_search(ajax2);				
						}
					
						// Send the request:
						ajax2.send(null);
						return false; // So form isn't submitted.
					}
				}
			}
		} // End of DOM check.
	} // End of ajax_search IF.
	
	
	
	
}










//------------------------------------------------------------------------------------------------------------------------------------















// Function that handles the response from the PHP script:
function handleResponse(ajax) {

	// Check that the transaction is complete:
	if (ajax.readyState == 4) {

		// Check for a valid HTTP status code:
		if ((ajax.status == 200) || (ajax.status == 304) ) {
			// Put the received response in the DOM:
			var four_random_images = document.getElementById('four_random_images');
			var non_ajax_four_random_images = document.getElementById('non_ajax_four_random_images');

			four_random_images.innerHTML = ajax.responseText;
			// Make the results box visible:
			four_random_images.style.display = 'block';
			non_ajax_four_random_images.style.display = 'none';
			
		} else { // Bad status code, submit the form.
			document.getElementById('ajax_random_form').submit();
		}
	} // End of readyState IF.
}		





// Function that handles the response from the PHP script:
function handleResponse_search(ajax2) {
	// Check that the transaction is complete:
	if (ajax2.readyState == 4) {
		// Check for a valid HTTP status code:
		if ((ajax2.status == 200) || (ajax2.status == 304) ) {
			// Put the received response in the DOM:
			var search_results = document.getElementById('search_results');
			
			search_results.innerHTML = ajax2.responseText;
			
			// Make the results box visible:
			search_results.style.display = 'block';
			//make the old results box invisible
			
			var test = "test";
			var links = document.getElementById("search_results").getElementsByTagName("a")

			for (var i=0; i<links.length; i++) {
				links[i].onclick = function() {
				
					search_results.style.background = 'none';
					
					var search_input = this.getAttribute("href").split("?")[1];
					
					if (search_input.match("image_id")) {
						var dummy = 1;
					} else{
						
						
						ajax2.open('get', 'index.php?ajax=TRUE&' + search_input);
						// Function that handles the response:
						ajax2.onreadystatechange = function() {
							// Pass it this request object:
							handleResponse_search(ajax2);				
						}
					
						// Send the request:
						ajax2.send(null);
						return false; // So form isn't submitted.
					}

				}
			}

		} else { // Bad status code, submit the form.
			document.getElementById('search_form').submit();
		}
	}
}






