var divUserInfo;

var user={
		'uid':0,
		'id':null,
		'name':null,
		'isAdmin':false
};
function get_status () {
	ajax_get (root+'status.moma', get_status_func, true);
}

function get_status_func (buf) {
	if (!buf) return;
	buf=JSON.parse (buf);
	user.uid=buf.uid;
	user.id=buf.id;
	user.name=buf.name;
	user.isAdmin=buf.isAdmin;
	
	display_user_info ();
}

function is_guest () {
	return (user.uid==0);
}

function is_admin () {
	return user.isAdmin;
}

function user_login () {
	var id=_I('fieldLoginId');
	var passwd=_I('fieldLoginPasswd');
	
	
	if (!check_id (id.value) && !check_email (id.value)) {
		alert ('Incorrect ID or Email address');
		id.focus ();
		return false;
	}
	if (!passwd.value) {
		alert ('Please type your password');
		passwd.focus ();
		return false;
	}
	return true;
}

function display_user_info () {
	var html='';
	if (is_guest ()) {
		html += '<form action="'+root+'login.moma" method="POST" onsubmit="return user_login ();">';
		html += '<img src="'+imageRoot+'userInfo.jpg" class="userInfoImg" /> ID or Email: <input id="fieldLoginId" type="text" name="id" value="" size="12" onmouseover="this.focus ();" /> ';
		html += 'Password: <input id="fieldLoginPasswd" type="password" name="passwd" value="" size="12" onmouseover="this.focus ();" /> ';
		html += '<input type="submit" value="Sign In" class="button" /> | New here? <a href="'+root+'registry.moma">Sign Up</a> ';
		html += ' | <a href="'+root+'passwd.moma">Forget Password?</a>';
		html += '</form>';
	} else {
		html += '<img src="'+imageRoot+'userInfo.jpg" class="userInfoImg" /> Hi, '+user.name+' ';
		html += ' | <a href="'+root+'login.moma">Sign Out</a>  | <a href="'+root+'profile.moma">My Profile</a> ';
		if (is_admin ())
			html += ' | <a href="'+root+'admin/">Management</a>';
		html += '';
	}
	divUserInfo.innerHTML=html;
}

function init_user () {
	if (typeof (noUserInfo)!='undefined'&&noUserInfo) return;
	divUserInfo =_I('divUserInfo');
	
	if (null==divUserInfo)
		return;

	get_status ();
}

add_onload_func  ('init_user');

