var init = function() {
	// add listener to the usernameIndex object
	// Event.observe("usernameIndex", "change", doUsernameIndexChange);
	YAHOO.util.Event.addListener("usernameIndex", "change", doUsernameIndexChange); 
	YAHOO.util.Event.addListener("movieStatus", "change", doMovieStatusChange); 
}

var checkMovieValue = function(o) {
	// we need to make sure 
	var oR = document.getElementById("filter_isRunning");
	var oU = document.getElementById("filter_isUpcoming");
	
	if (o[o.selectedIndex].id == 'isRunning') {
		oR.value = 1;
		oU.value = 0;
	}
	else if (o[o.selectedIndex].id == 'isUpcoming') {
		oR.value = 0;
		oU.value = 1;
	}
	else {
		oR.value = -1;
		oU.value = -1;
	}
	
	/*
	if (o[o.selectedIndex].id != "") {
		alert("This selection is not a valid search filter.\nPlease select a single movie or choose ALL to view all movies");
		o.focus();
	}
	*/
}

var doUsernameIndexChange = function() {
	var charIndex = this[this.selectedIndex].value;
	
	var doSuccess = function(o) {
		var theSelect = document.getElementById("select_userUUID");
		var u = o.responseXML.getElementsByTagName("user");
		// loop over the users returned and add them to the dtop down
		theSelect.length = 0;
		
		if (u.length == 0) {
			var theOption = document.createElement("option");
			theOption.text = "No users found";
			theOption.value = "";
			
			try {
				theSelect.add(theOption, null);	
			} 	
			catch (e) {
				theSelect.add(theOption);
			}
		}
		else {
			
			for (var i = 0; i <= u.length; i++){
				var theOption = document.createElement("option");
				theOption.text = u[i].getElementsByTagName("username")[0].firstChild.nodeValue;
				theOption.value = u[i].getElementsByTagName("userUUID")[0].firstChild.nodeValue;
				
				try {
					theSelect.add(theOption, null);	
				} 	
				catch (e) {
					theSelect.add(theOption);
				}
			}
		}		
	}
	var doFailure = function(o) {
		alert("failed");
	}
	
	var callback = { 
		success: doSuccess,
		failure: doFailure
	}
	var sUrl = "/service/getUsersByIndex.cfm?i=" + charIndex;
	var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
}

var doMovieStatusChange = function() {
	var status = this[this.selectedIndex].value;
	
	var doSuccess = function(o) {
		var theSelect = document.getElementById("muid");
		var u = o.responseXML.getElementsByTagName("movie");
		// loop over the users returned and add them to the dtop down
		theSelect.length = 0;
		
		if (u.length == 0) {
			var theOption = document.createElement("option");
			theOption.text = "No movies found";
			theOption.value = "";
			
			try {
				theSelect.add(theOption, null);	
			} 	
			catch (e) {
				theSelect.add(theOption);
			}
		}
		else {
			
			for (var i = 0; i <= u.length; i++){
				var theOption = document.createElement("option");
				theOption.text = u[i].getElementsByTagName("movieTitle")[0].firstChild.nodeValue;
				theOption.value = u[i].getElementsByTagName("movieUUID")[0].firstChild.nodeValue;
				
				try {
					theSelect.add(theOption, null);	
				} 	
				catch (e) {
					theSelect.add(theOption);
				}
			}
		}		
	}
	var doFailure = function(o) {
		alert("failed");
	}
	
	var callback = { 
		success: doSuccess,
		failure: doFailure
	}
	var sUrl = "/service/getMoviesByStatus.cfm?status=" + status;
	var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
}

/// add onload listener
Event.observe(window, 'load', init);

