// JavaScript Document

	window.onload = function(){
		var textInput = document.getElementById('lookupText');		
		textInput.focus(); // set focus to first searchbox on window load
		
		var spinWheel = document.createElement("img");
		spinWheel.setAttribute("id", "spinWheel");
		spinWheel.setAttribute("src", "/images/spinner.gif");
		spinWheel.style.visibility = "hidden";
		document.getElementById("spinner").appendChild(spinWheel);

		
		// add event handler function
		function prepSearch(){
			spinWheel.style.visiblity = "visible";
			var val = document.getElementById('lookupText').value;
			// remove leading and trailing whitespace
			val = val.replace(/^\s+||s+$/g,"");

			if(val.length > 1){
				sendRequest(val);
			}
		} // end onkeyup function

		// assign event handler
		textInput.onkeyup = prepSearch;
		document.getElementById('title').onclick = prepSearch;
		document.getElementById('desc').onclick = prepSearch;
		
		/*textInput.onblur = function(){
			this.value="";
		}*/

	} // end window.onload

		function sendRequest(searchText){
				r = new XMLHttpRequest();
								
				if (typeof(XMLHttpRequest) == "undefined") { 
					alert("XMLHttpRequest not supported");
		
					XMLHttpRequest = function() { 
						try { 
							return new ActiveXObject("Msxml2.XMLHTTP.6.0"); 
						} catch(e) {} try { 
							return new ActiveXObject("Msxml2.XMLHTTP.3.0"); 
						} catch(e) {} try { 
							return new ActiveXObject("Msxml2.XMLHTTP"); 
						} catch(e) {} try { 
							return new ActiveXObject("Microsoft.XMLHTTP"); 
						} catch(e) {}
		 
						alert("This browser does not support the course look up. Use the alphabetical links or another browser.");		
						throw new Error("This browser does not support the course look up. Use the alphabetical links."); 
					}; 
				} 
				
				var url =  "_programSearchResults.cfm?search=" + encodeURI(searchText);
				if(! document.getElementById('title').checked){
						url += "&t=n";
				}
				if(! document.getElementById('desc').checked){
						url += "&d=n";
				}
				//alert(url);
				r.open("GET", url, true);
				r.onreadystatechange = handleResponse;
				r.send(null);
				
				function handleResponse(){
					if(r.readyState == 4){
						if(r.status == 200){
							var resultsDiv = document.getElementById('results');
							resultsDiv.innerHTML = r.responseText;
						} else{ 
							alert("Error " + r.status + " searching courses. Please contact webmaster@vcc.ca.");
						}
					} 
					document.getElementById('spinWheel').style.visibility = "hidden";

				} // end function handleResponse
				
		} // end function sendRequest
	

