var jsonurl = 'JSON-RPC';
var jsonrpc = null;

function initJsonrpcForFrontUserLoginBean(webroot){
	try {
		jsonrpc = new JSONRpcClient(webroot + jsonurl);
	} catch(e) {
		handleExceptionForFrontUserLoginBean('创建jsonrpc失败' , e);
	}
}

// ------------------------------------------------

/**
 * 前台用户登录
 */
function loginFrontUserForFrontUserLoginBean(form , type , formaction){
	var username = form.username.value;
	var password = form.password.value;
	var validationCode = form.validationCode.value;
	//效验用户名
	if(username == null || username == ''){
		alert('请输入用户名.');
		form.username.focus();
		return; 
	}
	//效验密码
	if(password == null || password == ''){
		alert('请输入密码.');
		form.password.focus();
		return; 
	}
	//效验验证码
	if(validationCode == null || validationCode == ''){
		alert('请输入验证码.');
		form.validationCode.focus();
		return;
	}
	
	//登录
	var model = null;
	try{
		model = jsonrpc.frontUserLoginBean.loginUser(username , password , validationCode , type == 'yg');
	}catch(e){
		handleExceptionForFrontUserLoginBean('用户登录失败' , e);
		return;
	}
	
	var loginErrorCode = model.loginErrorCode;
	if(loginErrorCode == 1){
		alert('验证码填写错误,请重新填写');
		return;
	}else if(loginErrorCode == 2){
		alert('用户名密码错误,请重新填写');
		return;
	}else if(loginErrorCode == 3){
		//需要填充义工信息
		window.location.href= webroot + 'people/regist.do?methodname=fillUserInfo&id=' + model.userProfile.id;
		return;
	}else{
		var logonname = model.user.logonname;
		var userProfileId = model.userProfile.id;
		//TODO HC_update display info
		logger.debug('logonname:' + logonname + '\n' +	'userProfileId:' + userProfileId);
		writeFrontLoginedUserInfo(logonname , userProfileId , type);
		
		var frontUserNotLoginedTable = document.getElementById('frontUserNotLoginedTable');
		var frontUserLoginedTable = document.getElementById('frontUserLoginedTable');
		frontUserNotLoginedTable.style.display = 'none';
		frontUserLoginedTable.style.display = 'block';
		
		//post form data
		loginBackFormSubmit(formaction , form.username.value , form.password.value);
		
		try{
			//清除form中的内容
			form.username.value = '';
			form.password.value = '';
			form.validationCode.value = '';
		}catch(e){
			handleExceptionForFrontUserLoginBean('清除form的内容异常' , e);
		}
	}
	
}

/**
 * 义工注册的模块,前台登录后,登录后台的脚本
 */
function loginBackFormSubmit(formaction , username , password){
	//post form data
	if(formaction != null && formaction != ''){
		try{
			var loginBackForm = document.getElementById('loginBackForm');
			loginBackForm.action = formaction;
			loginBackForm.username.value = username;
			loginBackForm.password.value = password;
			loginBackForm.submit();
		}catch(e){
			handleExceptionForFrontUserLoginBean('登录后打开后台登录界面异常,formaction:' + formaction , e);
		}
	}
}

function loginBackByCurrentLoginedUser(formaction){
	try{
		var model = jsonrpc.frontUserLoginBean.getLoginedUserProfileModel();
		var logiErrorCode = model.loginErrorCode;
		if(logiErrorCode == 4){
			//session invalid
			alert('当前用户已经注销,请重新登录!');
			logoutFrontUserForFrontUserLoginBean();
		}else{
			var username = model.user.logonname;
			var password = model.user.password;
			loginBackFormSubmit(formaction , username , password);
		}
	}catch(e){
		handleExceptionForFrontUserLoginBean('打开后台登录界面异常,formaction:' + formaction , e);
	}
}


/**
 * 写前台用户登录后的信息
 */
function writeFrontLoginedUserInfo(logonname , userProfileId , type){
	var showFrontLoginUserInfoTd = document.getElementById('showFrontLoginUserInfoTd');
	var showFrontLoginUserActionsTd = document.getElementById('showFrontLoginUserActionsTd');
	showFrontLoginUserInfoTd.innerHTML = 
		  '当前登录用户:' + logonname 
		+ '&nbsp;&nbsp;' 
		+ '<a href="javascript:void(0);" onclick="logoutFrontUserForFrontUserLoginBean();">[注销]</a>'
		+ '&nbsp;&nbsp;'
		+ '<a target="_blank" href="' + webroot + 'people/regist.do?methodname=showModifyPasswordForm">[修改密码]</a>'		
		;
	
	//根据类型显示不同的数据
	if(type == null || type =='')
		type = 'fkbj';
		
	if(type == 'fkbj'){
		//俯瞰北京
		showFrontLoginUserActionsTd.innerHTML = 
				'<a target="_blank" href="' + webroot + 'people/regist.do?methodname=showModifyForm&registType=2&id=' + userProfileId + '">[修改信息]</a>' 
			+	'&nbsp;&nbsp;' 
			+	'<a target="_blank" href="' + webroot + 'people/dssh/fkbj/fkbjIndexAction.do?methodname=initContribute&ck=PEOPLE_DSSH_FKBJ"><img src='+webroot+'people/images/wytg.JPG width="132" height="30"></a>'
			;
	}else if(type == 'yg'){
		//义工
		showFrontLoginUserActionsTd.innerHTML = 
			'<a target="_blank" href="' + webroot + 'people/regist.do?methodname=showModifyForm&registType=3&id=' + userProfileId + '">[修改信息]</a>' 
		+	'&nbsp;&nbsp;'
		+	'<a href="javascript:void(0);" onclick="loginBackByCurrentLoginedUser(\'' + webroot + 'admin/login.do?method=login\');" style="cursor:hand">[登录后台]</a>'
		;
	}else if(type == 'hyxx'){
		//海英信箱
		showFrontLoginUserInfoTd.innerHTML = 
		  '<font color="red">&nbsp;&nbsp;欢迎:' + logonname 
		+ '&nbsp;光临海英信箱</font><br/>'			
		+ '&nbsp;<a target="_blank" href="' + webroot + 'people/regist.do?methodname=showModifyPasswordForm">[修改密码]</a>'		
		+ '&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" onclick="logoutFrontUserForFrontUserLoginBean();">[退出登录]</a>'	
		;
		showFrontLoginUserActionsTd.innerHTML = 
				'&nbsp;<a target="_blank" href="' + webroot + 'people/regist.do?methodname=showModifyForm&registType=1&id=' + userProfileId + '">[修改信息]</a>' 
		;
		
		var hyxxMyMailBoxLink = document.getElementById('hyxxMyMailBoxLink')
		hyxxMyMailBoxLink.href = webroot + 'hyxx/userWriteLetterAction.do?methodname=showUserLetters';
		hyxxMyMailBoxLink.target = '_blank';
	}	
}

/**
 * 前台用户注销
 */
function logoutFrontUserForFrontUserLoginBean(){
	try{
		//注销
		jsonrpc.frontUserLoginBean.logoutUser();
	}catch(e){
		handleExceptionForFrontUserLoginBean('注销用户异常,用户可能已经注销过:' , e);
	}
	
	var frontUserNotLoginedTable = document.getElementById('frontUserNotLoginedTable');
	var frontUserLoginedTable = document.getElementById('frontUserLoginedTable');
	frontUserNotLoginedTable.style.display = 'block';
	frontUserLoginedTable.style.display = 'none';
	
	try{
		var hyxxMyMailBoxLink = document.getElementById('hyxxMyMailBoxLink')
		if(hyxxMyMailBoxLink != null ){
			hyxxMyMailBoxLink.href = 'javascript:alert("您还没有登录.");';
			hyxxMyMailBoxLink.target = '';
		}
	}catch(e){
		//alert(e);
	}
	
}



// ------------------------------------------------
/** 错误信息展现*/
function handleExceptionForFrontUserLoginBean(message , e){
	if(e.message)
	    logger.error(message + e.message);
	else
	    logger.error(message + e);
}
