var questions = new Array(false, false, false, false);

$(document).ready(function() {
	//Bind the select field to populate the survey div every time it's changed.
	$('select#performance').livequery(function() {
		$(this).change(function() {
			$('div#surveyQuestions').load('/_splashContents/performanceSurvey/surveyQuestions.cfm?performanceIDs=' + $(this).val());
		});
	});

	//Each time an input is clicked, we want to see if the subit button should be enabled or not
	$('form#surveyForm input').livequery(function() {
		$(this).change(function() {
			//Index(1) of the input name is an integer 1-4 subtract 1 from that to get the array index
			//Using jQuery get a collection of inputs that match this name (radio buttons) and check if ATLEAST one is clicked
			questions[$(this).attr('name').charAt(1) - 1] = $("input[name=" + $(this).attr('name') + "]").is(':checked');
			//If all questions have been answered, turn on the submit button.
			if(questions[0] && questions[1] && questions[2] && questions[3])
				document.getElementById('surveyForm').submit.disabled = false;
			else
				document.getElementById('surveyForm').submit.disabled = true;
		});
	});
	
	$('form#surveyForm').livequery(function() {
		$(this).submit(function() {
			//Post the form via ajax, serialize the data, and display the correct message div depending on the return results
			$.post($(this).attr('action'), $(this).serialize(), function(data) {
				$('div#surveyQuestions').html('');				
				if(data.indexOf('true') != -1) {//Success :)
					$('div#successMessage').css('display', 'block');
					//Disable the option in the drop down list
					theDropDown = document.getElementById('performance');
					theDropDown.options[theDropDown.selectedIndex].disabled = true;
					//Reset the questions array to false
					questions = Array(false, false, false, false);
				}
				else //Fail :(
					$('div#failMessage').css('display', 'block');
			});
			return false;
		});											
	});
});
