
function LoginControl (loggedIn) {
	var	cookieTest ;

	cookieTest = 1000 + Math.floor (Math.random () * 10000) ;
	Cookie.set ('session', cookieTest) ;
	this.cookiesEnabled = false ;
	if (Cookie.get ('session') == cookieTest) {
		this.cookiesEnabled = true ;
		this.user = new UserInfo () ;
	}

	if (this.cookiesEnabled && loggedIn) {
		UserInfo.Load (LoginControl.userInfoUrl, this, 'loadUserInfo') ;
	}
}

LoginControl.prototype.loadCaptcha = function (captcha) {
	if (captcha) {
		this.captcha = captcha ;
		if (this.form && this.form.captcha && this.form.captcha.imgElement && this.form.captcha.codeElement) {
			captcha.attach (this.form.captcha.imgElement, this.form.captcha.codeElement, this.form.captcha.inputElement) ;
		}
	} else {
		this.captcha = new Captcha () ;
	}
	this.captcha = captcha ;
	this.updateDisplay () ;
}

LoginControl.prototype.loadUserInfo = function (userInfo) {
	if (userInfo) {
		this.user = userInfo ;
	} else {
		this.user = new UserInfo () ;
	}
	this.updateDisplay () ;
}

LoginControl.prototype.updateDisplay = function () {
	if (this.loading) {
		if (!this.cookiesEnabled) {
			xb.HideElement (this.loading, this.info, this.form, this.join) ;
			xb.ShowElement (this.cookies) ;
		} else if (this.user) {
			if (this.info && !this.user.isGuest ()) {
				this.setElementText (this.info.username, this.user.name) ;
				this.setElementText (this.info.subscription, this.user.subscription) ;
				if (this.user.accountupgradeurl) {
					this.setLinkURL (this.info.upgrade.link, this.user.accountupgradeurl) ;
					xb.ShowElement (this.info.upgrade) ;
				}
				if (this.user.accountlookupurl) {
					this.setLinkURL (this.info.manage.link, this.user.accountlookupurl) ;
					xb.ShowElement (this.info.manage) ;
				}
				xb.HideElement (this.loading, this.cookies, this.form, this.join) ;
				xb.ShowElement (this.info) ;
			} else {
				if (this.form.captcha) {
					if (this.captcha) {
						if (this.captcha.code) {
							xb.ShowElement (this.form.captcha) ;
						} else {
							xb.HideElement (this.form.captcha) ;
						}
					} else {
						if (this.form.captcha.codeElement.value != '') {
							this.captcha = Captcha.Create (LoginControl.captchaUrl, this.form.captcha.imgElement, this.form.captcha.codeElement, this.form.captcha.inputElement) ;
							xb.ShowElement (this.form.captcha) ;
						} else {
							Captcha.Load (LoginControl.captchaUrl, this, 'loadCaptcha') ;
						}
					}
				}
				xb.HideElement (this.loading, this.cookies, this.info) ;
				xb.ShowElement (this.form, this.join) ;
			}
		}
	}
}

LoginControl.prototype.parentDIV = function (element) {
	if (element) {
		while (element = element.parentNode) {
			if ((element.nodeType == xb.ELEMENT_NODE) && (element.tagName.toLowerCase () == 'div')) {
				break ;
			}
		}
	}
	return element ;
}

LoginControl.prototype.loadPanel = function () {
	var	element ;

	this.loading = xb.ElementByID ('login-loading') ;
	this.cookies = xb.ElementByID ('login-cookies') ;
	this.form = xb.ElementByID ('login-form') ;
	if (this.form) {
		element = xb.ElementByID ('login-captcha-img') ;
		if (element) {
			this.form.captcha = this.parentDIV (element) ;
			this.form.captcha.imgElement = element ;
			this.form.captcha.codeElement = xb.ElementByID ('login-captcha-code') ;
			this.form.captcha.inputElement = xb.ElementByID ('login-captcha-input') ;
		}
	}
	this.join = xb.ElementByID ('login-join') ;

	element = xb.ElementByID ('login-username') ;
	if (element) {
		this.info = this.parentDIV (this.parentDIV (element)) ;
		this.info.username = element ;
		this.info.subscription = xb.ElementByID ('login-subscription') ;
		element = xb.ElementByID ('login-manage') ;
		if (element) {
			this.info.manage = this.parentDIV (element) ;
			this.info.manage.link = element ;
		}
		element = xb.ElementByID ('login-next-event-date') ;
		if (element) {
			this.info.nextEvent = this.parentDIV (element) ;
			this.info.nextEvent.eventDate = element ;
			this.info.nextEvent.eventType = xb.ElementByID ('login-next-event-type') ;
		}
		element = xb.ElementByID ('login-upgrade') ;
		if (element) {
			this.info.upgrade = this.parentDIV (element) ;
			this.info.upgrade.link = element ;
		}
	}

	element = xb.ElementByID ('login-logout') ;
	if (element) {
		xb.AddEventListener (element, 'click', this) ;
	}

	this.updateDisplay () ;
}

LoginControl.prototype.setElementText = function (element, text) {
	if (element) {
		element.innerHTML = text ;
	}
}

LoginControl.prototype.setLinkURL = function (element, url) {
	if (element) {
		element.href = url ;
	}
}

window.loginControl = new LoginControl () ;
