$(function(){

	$("select#ctlCategory").click(function(){

		$.getJSON("/scripts/select1.php",{id: $(this).val()}, function(j){

			var options = '';

			for (var i = 0; i < j.length; i++) {

				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

			}

			$("#ctlSecond").html(options);

			$('#ctlSecond option:first').attr('selected', 'selected');

			$("#ctlThird").html('<option value=0>&nbsp;</option>');

			$("#ctlFourth").html('<option value=0>&nbsp;</option>');

		})

	}),

	$("select#ctlSecond").click(function(){

		$.getJSON("/scripts/select2.php",{id: $(this).val(), searchType: $("select#ctlCategory").val()}, function(j){

			var options = '';

			for (var i = 0; i < j.length; i++) {

				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

			}

			$("#ctlThird").html(options);

			$('#ctlThird option:first').attr('selected', 'selected');

			$("#ctlFourth").html('<option value=0>&nbsp;</option>');

		})

	}),

	$("select#ctlThird").click(function(){

		$.getJSON("/scripts/select3.php",{id: $(this).val(), searchType: $("select#ctlCategory").val(), second: $("select#ctlSecond").val(), year: $("select#ctlThird").val()}, function(j){

			var options = '';

			for (var i = 0; i < j.length; i++) {

				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

			}

			$("#ctlFourth").html(options);

			$('#ctlFourth option:first').attr('selected', 'selected');

		})

	}),

	$("select#ctlFourth").click(function(){



		$.getJSON("/scripts/getDoc.php",{id: $(this).val(), searchType: $("select#ctlCategory").val()}, function(j){

			var doc_list = '';



			for (var i = 0; i < j.length; i++) {

				doc_list += j[i].optionValue;

			}

			if (j.length >= 1) {
				$("#docResults").html(doc_list);
			}
			else {
				$("#docResults").html("");
			}

		})

	})

})

