(function(){
_processing = false;

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;
};
showError = function(errTxt) {
	var span;
	var row = document.getElementById('usernamerow');
	var span = 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);
	}
};

toggleForm = function() {
	_processing = !_processing;
};
hijackForm = function(evt) {
	//YAHOO.util.Event.stopEvent(evt);	
	if (this.username.value == '') {
		showError('请输入账号');
		return false;
	}
	if (this.password.value == '') {
		showError('请输入密码');
		return false;
	}
	var originalPw = this.password.value;
	/* Make sure a token was passed. This is for CHAP login */
	//这一段会导致提交的时候密码被清空的问题，暂时先去掉 by jonas
//	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() {

jQuery('memberloginForm').ready((
function() {
	loginForm = this;
	jQuery('submit').bind(hijackForm);
	jQuery("signInButton").bind('mouseover', function() {this.addClass(this,'hover');});
	jQuery("signInButton").bind('mouseout', function() {this.removeClass(this,'hover');});
	//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);
	//YAHOO.util.Event.addListener(this,'submit',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);
}));

//checkbox
jQuery(document).ready(function(){
	jQuery('#autologincheckbox').bind('click',function(){
		if(jQuery('#autologincheckbox').attr('class')=='autologincheck'){
			jQuery(this).attr('class','autologinchecksel');
			jQuery('#autologin').attr('checked','true');
		}
		else{
			jQuery(this).attr('class','autologincheck');
			jQuery('#autologin').attr('checked','');
		}
	});
});
