$(document).ready(function(){
    
	$('#zip').keyup(function () {
		zipCampus();
	});
	$('#campus').change(function () {
		getProgram();
	});
	$('#ed_level').change(function () {
		getProgram();
	});
	$('#program').change(function () {
		program_extras();
	});
	
	zipCampus();
	getProgram();
	program_extras();
});

function zipCampus() {
	var zip = $('#zip').val();
	var campus = $('#campus option:selected').val();
	if (zip.length == 5) {
		if (IsNumeric(zip)) {
			$.ajax({
				type: 'POST',
				url: '/ajax.php?action=KRU_culinary_zip_campus',
				data: 'zip='+zip,
				success: function(response){
					if (response == '<option value="">Select One</option>') {
						alert('There are no campuses available in that zip code range');
					} else if (response != '') {
						$('select#campus').html(response);
						$('#campus').val(campus);
					} 
				}
			});
		} else {
			alert('Please enter a valid zip code');
		}
	}	
}

function getProgram() {
	var campus = $('#campus option:selected').val();
	var ed_level = $('#ed_level option:selected').val();
	var program = $('#program option:selected').val();
	$.ajax({
		type: 'POST',
		url: '/ajax.php?action=KRU_culinary_program',
		data: 'campus='+campus+'&ed_level='+ed_level,
		success: function(response){
				if (response != '') {
			$('select#program').html(response);
				$('#program').val(program);
			}
		}
	});
}

function program_extras() {
	var program = $('#program option:selected').val();
	$.ajax({
			type: 'POST',
			url: '/ajax.php?action=KRU_culinary_right_content',
			data: 'selected='+program,
			success: function(response){
				if (response != '') {
					$('#right_content_update').html(response);
				}
			}
	});
}

function IsNumeric(sText) {
	var ValidChars = "0123456789";
	for (i = 0; i < sText.length; i++) {
		if (ValidChars.indexOf(sText.charAt(i)) == -1) {
			return false;
		}
	}
	return true;
}
