/**
 * Use this object to manage user log in/out or to register new account  
 * 
 * Examples:
 * instantiate the object :
 *     var loginManager = new LoginManager("${inOrOut}", "${param.inNoutLink}");
 *     
 * user login popup layer:
 *     loginManager.setOpenWindow(openLoginRegistrationFormFrame);
 *     loginManager.setPostLoginOrRegister(functionName);
 *     
 * call log in or out:
 *     loginManager.logInOrOut();
 *     
 * @author Billy Truong
 * @param inOrOut - process log in/out if a user clicks on Login/Logout link.
 * @param oldLoginURL - the login url to redirect to 
 * 			if it's not using the new VWM login layer
 */
function LoginManager(inOrOut, oldLoginURL) {
	this.LOG_IN = 'in';
	this.LOG_OUT = 'out';
	
	this.inOrOut = inOrOut;
	this.loginUrl = this.buildLoginUrl();
	this.postLoginOrRegister = null;
	this.openWindow = null;
	
	this.setPostLoginOrRegister = function(arg) {
		this.postLoginOrRegister = arg;
	}
	
	this.setOpenWindow = function(arg) {
		this.openWindow = arg;
	}
	
	this.setInOrOut = function(inOrOut) {
		this.inOrOut = inOrOut;
	}
}
LoginManager.prototype.logInOrOut = function() {
	if (this.inOrOut == this.LOG_IN && this.openWindow != null) {
		this.openWindow();
	} else if (this.inOrOut == this.LOG_OUT) {
		location.href = "/logout?linkTagger=header";
	} else if(this.loginUrl != null) {
		location.href = this.loginUrl;
	}
}
LoginManager.prototype.buildLoginUrl = function() {
	var url = parent.location.href;
	// if we're already on the login page then do nothing
	if ( url.indexOf('/loginRegForm') != -1 || url.indexOf('registration.jsp') != -1  ) {
		return null;
	}
	
	return '/loginRegForm?redirect='+escape(url)+'&linkTagger=header';
}
LoginManager.prototype.executePostLoginOrRegister = function() {
	if (this.postLoginOrRegister != null) {
		this.postLoginOrRegister();
	}		
}
LoginManager.prototype.getCookieUserId = function() {
	var cookie = new HTTP.Cookies;
	var userId = cookie.read('eviteAuth');
	return userId;
}
LoginManager.prototype.isLoggedIn = function() {
	var userId = this.getCookieUserId();
	return (userId != null && userId.length == 20);
}