﻿/*
Authenticater:
Provides functionality for logging a user in and out.
*/
var Authenticater = Class.extend({

	init: function() {
	},

	/*
	Login: 
	Login a user on the website.
	
	Parameters:
		email: login e-mail address.
		password: login password.
		success: callback function that is called when the login is a success.
		failure: callback function that is called when the login fails.
	*/
	Login: function(email, password, success, failure) {
		if (email == '' || password == '') {
			jQuery.log("Emailaddress or password not provided.");
			failure("E-mail address and password are required.");
		} else {
			jQuery.ajax({
				url: location.protocol + "//" + location.hostname + "/login/?action=login",
				data: { loginEmail: email, loginPassword: password },
				type: "POST",
				dataType: "json",
				cache: false,
				success: function(msg) {
					if (msg == "loginSuccess") {
						success();
					} else {
						if (msg == "loginFailed") {
							failure("Incorrect login information, please try again.");
						} else {
							//'accountIsDisabled' is the only remaining possible state.
							failure("Your account is no longer active. Contact us if you want to enable your account again.");
						}
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					//ajax call failed. login could not be processed.
					failure("Your login could not be processed. Please try again later.")
				}
			});
		}
	},

	/*
	Logout:
	Logout the current user of the website.
	
	Parameters:
		success: callback function that is called when the logout is processed.
		failure: callback function that is called when logout fails.
	*/
	Logout: function(success, failure) {
		jQuery.ajax({
			url: location.protocol + "//" + location.hostname + "/login/?action=logout",
			type: "GET",
			dataType: "json",
			cache: false,
			success: function(msg) {
				if (msg == "logoutSuccess") {
					success();	
				} else {
					failure("Your logout request could not be processed. Please try again later.");
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				failure("Your logout request could not be processed. Please try again later.");
			}
		});
	},
	
	/*
	RequestPassword:
	Request the password (a new password) for a user in the system.
	
	Parameters:
		success: ..
		failure: ..
	*/
	RequestPassword: function(email, success, failure) {
		if (email == '') {
			failure("You need to provide your e-mail address.");
		} else {
			jQuery.ajax({
				url: location.protocol + "//" + location.hostname + "/login/?action=findPassword",
				type: "POST",
				data: { passwordEmail: email},
				dataType: "json",
				cache: false,
				success: function(msg) {
					if (msg == "passwordSent") {
						success();	
					} else {
						if (msg == "userNotFound") {
							failure("We could not find any account linked to the e-mail address you provided.");
						} else {
							failure("Your request could not be processed. Please try again later.");
						}
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					failure("Your request could not be processed. Please try again later.");
				}
			});
		}
	}
});
