function key(number) {
	return Math.floor(Math.random() * number);
}

function fetchLogin(selector) {
	//$(selector).empty();
	//$(selector).append('Loading...');
	$.ajax({
		type	: "POST",
		url		: "/login.php",
		data	: "show=1" +
				  "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
			$("#authenticate").click(function () { 
				
				$.ajax({
					type	: "POST",
					url		: "/login.php",
					data	: "username=" + $("#username").val() + 
							  "&password=" + $("#password").val() + 
							  "&md5=" + md5($("#username").val() + $("#password").val()) + 
							  "&key=" + key(10000),
					success	: function(authenticated){
						
						if (authenticated == 1) {
							$(selector).empty();
							alert("Thank you. You are now logged in.");
							window.location = "http://www.nqtac.co.za/student.html";
						} else {
							alert("ERROR! Please enter a correct username and password.");
						}
						
					}
				});
			});
		}
	});
}

function fetchCourses() {
	var selector = '#courses';
	$(selector).empty();
	$(selector).append('Loading...');
	$.ajax({
		type	: "GET",
		url		: "/courses.php",
		data	: "key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}
