currentUsername = ""; //for use by otherLoginStatusCallbacks

function AjaxLogin() {
	AjaxLogin.baseConstructor.call(this);
	
	this.loginStatusCallback = undefined;
	
	this.scriptNameAndArguments = "/app/ajax/loginFormParser.php";
}

extend(AjaxLogin, AjaxInterface); //in Utilities.js, included in html file that includes this js file

//this is how you override a superclass' function
AjaxLogin.prototype.actOnResponse = function(responseText) {
	var action = getFunctionFromResponseText(responseText);
	var data = getDataFromResponseText(responseText);
	
	document.body.style.cursor = "default";
	
	switch(action) {
		case "login":
			if(data == "valid") {
				window.location = "/account/"; 
			}
			else if(data.indexOf("error") == 0){
				alert(responseText);
			}
			else {
				if(data.indexOf('Exception') != -1) {
					alert(responseText);
				}
				else {
					var loginError = document.getElementById('errorLogin');
					removeNodeChildren(loginError);
					var p = document.createElement('p');
					p.appendChild(document.createTextNode('Nope, wrong information. Try again!'));
					loginError.appendChild(p);
					loginError.style.display='block';
				}
			}	
			break;
		case "checkloginstatus":
			if(data != "false") {
				currentUsername = data;
			}
			
			this.data = data;
			this.loginStatusCallback();
			
			for(var i = 0; i < otherLoginStatusCallbacks.length; i++) {
				otherLoginStatusCallbacks[i]();
			}
			runPostAjaxOnload();
			break;
		case "dologout":
			currentUsername = data;
			for(var i = 0; i < otherLoginStatusCallbacks.length; i++) {
				otherLoginStatusCallbacks[i]();
			}
			break;
		case "passwordresetemail":
			if(data == "emailNotFound") {
				document.getElementById('errorEmail').style.display = 'block';
				document.getElementById('errorEmailNoExists').style.display = 'block';
			} else {
				var emailSentTo = document.getElementById('emailSentTo');
				removeNodeChildren(emailSentTo);
				emailSentTo.appendChild(document.createTextNode(document.getElementById("emailForPasswordReset").value));
				
				document.getElementById('emailSent').style.display = 'block';
			}
			break;
		default:
			alert("AJAX return action not recognized: *" + responseText + "*");
	}
};

function submitEnterLogin(event) {
	if(gotEnter(event)) {
		processLogin();
	}
}

function submitEnterEmail(event) {
	if(gotEnter(event)) {
		passwordResetEmail();
	}
}

function processLogin(){
	hideNotifications();
	
	var email = document.getElementById("email");
	var password = document.getElementById("password");
	
	document.body.style.cursor = "wait";
	var myAjaxLogin = new AjaxLogin();
	myAjaxLogin.data = "action=login&email=" + email.value + "&password=" + password.value;
	myAjaxLogin.send();
}

var otherLoginStatusCallbacks = new Array();
function addOtherLoginStatusCallback(theFunction){
	otherLoginStatusCallbacks[otherLoginStatusCallbacks.length] = theFunction;
}

function checkLoginStatus(callback){
	var myAjaxLogin = new AjaxLogin();
	myAjaxLogin.data = "action=checkloginstatus";
	myAjaxLogin.loginStatusCallback = callback;
	myAjaxLogin.send();
	return myAjaxLogin;
}

function doLogout(){
	var myAjaxLogin = new AjaxLogin();
	myAjaxLogin.data = "action=dologout";
	myAjaxLogin.send();
}

function passwordResetEmail(){
	document.body.style.cursor = "wait";
	hideNotifications();
	
	document.getElementById('errorEmailNoExists').style.display = 'none';
	document.getElementById('errorEmailInvalid').style.display = 'none';
	
	var email = document.getElementById("emailForPasswordReset").value;
	if(!isEmailValid(email)) {
		document.getElementById('errorEmailInvalid').style.display = 'block';
		document.getElementById('errorEmail').style.display = 'block';
		return;
	}

	var myAjaxLogin = new AjaxLogin();
	myAjaxLogin.data = "action=passwordresetemail&email=" + email;
	myAjaxLogin.send();
}