YAHOO.namespace('gaia.app.Login');
YAHOO.gaia.app.Login._processing = false;

YAHOO.gaia.app.Login.removeOldError = function() {
	var row = document.getElementById('usernamerow');
	var oldError = document.getElementById('loginerror');
	if (oldError) {
		YAHOO.util.Event.purgeElement(oldError);
		span = row.removeChild(oldError);
		var tmp;
		while (tmp = span.firstChild){
			span.removeChild(tmp);
		}
		return span;
	}
	return false;
};
YAHOO.gaia.app.Login.showError = function(errTxt) {
	var span;
	var row = document.getElementById('usernamerow');
	var span = YAHOO.gaia.app.Login.removeOldError();
	if (!span) {
		span = document.createElement('span');
		span.id = 'loginerror';
	}
	span.className = 'generalerror error';
	span.appendChild(document.createTextNode(errTxt));
	/**
	 * There's a bug in IE6 that will render content without a
	 * background specified heavily distorted. Since this is on
	 * a semi-transparent bg, we can't set its bg color here
	 */
	if (!(YAHOO.env.ua.ie==6)) {
		span.style.opacity = 0;
		span.style.filter = 'alpha(opacity = 0)';
		row.appendChild(span);
		var attributes = {opacity:{from:0,to:1}};
		var anim = new YAHOO.util.Anim(span,attributes,1);
		anim.animate();
	} else {
		row.appendChild(span);
	}
};

YAHOO.gaia.app.Login.toggleForm = function() {
	YAHOO.gaia.app.Login._processing = !YAHOO.gaia.app.Login._processing;
};
YAHOO.gaia.app.Login.hijackForm = function(evt) {
	YAHOO.util.Event.stopEvent(evt);	
	if (this.username.value == '') {
		YAHOO.gaia.app.Login.showError('Enter your username');
		return false;
	}
	if (this.password.value == '') {
		YAHOO.gaia.app.Login.showError('Enter your password');
		return false;
	}
	var originalPw = this.password.value;
	/* Make sure a token was passed. This is for CHAP login */
	if (this.token && this.token.value) {
		try {
			var pw = YAHOO.gaia.util.Dom.addSlashes(originalPw);
			this.password.value = '';
			if (!this.chap) {
				var chap = document.createElement('input');
				chap.name = 'chap';
				chap.id = 'chap';
				chap.type = 'hidden';
				this.appendChild(chap);
			}
			/* Make sure both values are strings, otherwise the MD5 will differ from PHP's */
			pw += '';
			this.token.value += '';
			pw = MD5.hex(pw);		
			this.chap.value = MD5.hex(pw + this.token.value);
		} 
		/* In case something with the CHAP fails, we still want to do the login */
		catch(e) {
			if (this.chap) {
				this.removeChild(this.chap);
			}
			this.password.value = originalPw;
		}
	}
	this.submit();
};
YAHOO.util.Event.onAvailable('memberloginForm',function() {
	YAHOO.gaia.app.Login.loginForm = this;
	YAHOO.util.Event.addListener(this,'submit',YAHOO.gaia.app.Login.hijackForm);
	YAHOO.util.Event.addListener(this.signInButton,'mouseover',function() {YAHOO.util.Dom.addClass(this,'hover');},this.signInButton);
	YAHOO.util.Event.addListener(this.signInButton,'mouseout',function() {YAHOO.util.Dom.removeClass(this,'hover');},this.signInButton);
});