//logininfo

function info_formfocus() {
	setTimeout("document.getElementById('email').focus();",500);
}

function info_tryInfo() {
	if($("#email").val() == '') {
		alert('Please enter an email.');
		return;
	}
	$("#failureResult").html("Submitting...");
	$.post("ajaxInfoScript.php", {email: ""+$("#email").val()+"", mode: ""+$("#infoselect").val()+""},
		function(data){
			if(data == "fail") {
				$("#failureResult").html("Attempt failed. Please try again or register your email address.");
			}
			else {
				var response = "Your ";
				switch($("#infoselect").val()) {
				case '0':
					response += "username has ";
					break;
				case '1':
					response += "password has ";
					break;
				case '2':
					response += "username and password have ";
					break;
				default:
					response = "Attempt unexpectedly failed. Please reload this page and try again.\n\n";
					response += data;
					alert(response);
					return;
				}
				response += "been sent to the specified email address."
				alert(response);
				window.location='main.php';
			}
	});
}

function info_catchenter(myfield, e) {
	var keycode;

	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;
	else
		return true;

	if(keycode == 13) {
		info_tryInfo();
		e.cancelBubble=true;
		return false;
	}
	return true;
}

//login

function login_formfocus() {
	setTimeout("document.getElementById('username').focus();",500);
}

function login_trySubmit() {
	if($("#username").val() == '') {
		alert('Please enter a username.');
		return;
	}
	if($("#password").val() == '') {
		alert('Please enter a password.');
		return;
	}

	$("#failureResult").html("Submitting...");

	$.post("ajaxLoginScript.php", {name: ""+$("input#username").val()+"", pass: ""+$("input#password").val()+""},
		function(data){
			if(data == "fail") {
				$("#failureResult").html("Login failed. Please try again.");
			}
			else {
				window.location='main.php';
			}
	});
}

function login_catchenter(myfield, e) {
	var keycode;

	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;
	else
		return true;

	if(keycode == 13) {
		login_trySubmit();
	}
}

function register_trySubmit() {
	if($("#username").val() == '') {
		$("#failureResult").html('please provider a username.');
		return;
	}
	if($("#password").val() == '') {
		$("#failureResult").html('please provide a password.');
		return;
	}
	if($("#password").val() != $("#passwordconfirm").val()) {
		$("#failureResult").html('please ensure that passwords match.');
		return;
	}
	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test($("#email").val())) {
		$("#failureResult").html('please provide a valid email address.');
		return;
	}
	
	$("#failureResult").html("submitting...");
	
	$.post("ajaxRegisterScript.php", {user: ""+$("#username").val()+"", pass: ""+$("#password").val()+"", first: ""+$("#firstname").val()+"", last: ""+$("#lastname").val()+"", mail: ""+$("#email").val()+""},
		function(data){
			if(data != "success") {
				$("#failureResult").html(data);
			}
			else {
				$("#failureResult").html("you have been successfully registered; please check your email for a confirmation message.");
			}
	});	
}

function register_catchenter(myfield, e) {
	var keycode;

	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;
	else
		return true;

	if(keycode == 13) {
		register_trySubmit();
	}
}
