var _loginedNickname = "";
var _isLogined = false;
var _loginedId = "";
var returnPath = "";

function getLoginParameterForFlash(flashCallbackName){
	var returnStr = "isLogined="+ _isLogined+ "&loginId=" + _loginedId; 
	if (flashCallbackName) {
		var parameterList = new Array(flashCallbackName,returnStr);
		proxyFlashAnnounce(parameterList);
	}
	return returnStr;
}


function getLoginStatusStr(){
	var _loginStatusStr = 
		'<a href="javascript:showLoginBox()"' + 
		'onmouseover="changeImages(\'site_10\', \'/images/site_10-over.gif\'); return true;"' +
		'onmouseout="changeImages(\'site_10\', \'/images/site_10.gif\'); return true;"' +
		'onmousedown="changeImages(\'site_10\', \'/images/site_10-over.gif\'); return true;"' +
		'onmouseup="changeImages(\'site_10\', \'/images/site_10-over.gif\'); return true;">' +
		'<img name="site_10" src="/images/site_10.gif" width="136" height="31" border="0" alt=""></a>';
	return _loginStatusStr;
}

function getLogoutStatusStr(){
	var _logoutStatusStr = 
		'<table width="136" height="31" border="0" cellpadding="0" cellspacing="0" background="/06_login/image/logout_bg.gif">' +
		'<tr><td width="6" height="31">&nbsp;</td><td width="130" align="left" valign="top">'+
		'<font color="#FF0000" size="1" face="Arial, Helvetica, sans-serif" class="logout">welcome, ' + _loginedNickname + '<br>' +
		'<a href="javascript:submitLogoutRequest()"><img src="/06_login/image/logout_btn.gif" width="37" height="11" border="0"></a> </font>' +
		'</td></tr></table>' ;
	return _logoutStatusStr;
}


function onSuccessLogin(name, id){
	_isLogined = true;
	_loginedId = id;
	hideLoginBox();
	updateLoginStautsDisplay(name,id);
	var parameterList = new Array('onSuccessLogin',id);
	proxyFlashAnnounce(parameterList);
}

function onSuccessLogout(){
	_isLogined = false;
	_loginedId = "";
	updateLogoutStautsDisplay();
	
	var parameterList = new Array('onSuccessLogout');
	proxyFlashAnnounce(parameterList);
}


function onLoginReponse(responseXML, doc)
{
	var result = responseXML.getElementsByTagName("login_result")[0].firstChild.data;
	
	if(result==2){
		alert(MSG.INVALID_ACTIVE);
	} else if(result==3){
		alert(MSG.INVALID_LOGIN);
	} else if(result==1){
		
		alert(MSG.SUCCESS_LOGIN );
		
		if (returnPath != "") {
			window.location = returnPath;
		} else {
			//window.location = '../forum/logging.php?action=login&loginsubmit=1&loginfield=username&username='+document["loginform"].memberloginname.value+'&password='+document["loginform"].passwordlogin.value;
			window.location = '../content/index.php';
			//$refererhost = parse_url($_SERVER['HTTP_REFERER']);
		}
		
		//siteTrack('a_login');
	}else{
		alert(MSG.SERVER_BUSY);
	}
	//updateText(doc.getElementById("_loginLoadingDiv"), "&nbsp;", "hidden");
}

function onConfirmLoginReponse(responseXML, doc) {
	var result = responseXML.getElementsByTagName("result")[0].firstChild.data;
	var memberId = responseXML.getElementsByTagName("memberId")[0].firstChild.data;
	var memberEmail = responseXML.getElementsByTagName("memberEmail")[0].firstChild.data;
	var memberName = responseXML.getElementsByTagName("memberName")[0].firstChild.data;

	if(result==LoginConst.SUCCESS_LOGIN){
		onSuccessLogin(memberName,memberId);
	}else{
		onSuccessLogout();
	}

	announcePageFinishLoading();
}

function onLogoutReponse(responseXML, doc) {

	var result = responseXML.getElementsByTagName("result")[0].firstChild.data;
	if(result==LoginConst.SUCCESS_LOGIN){
		onSuccessLogout();
		alert(MSG.SUCCESS_LOGOUT );
	}else{
		alert(MSG.SERVER_BUSY );
	}
}

function submitLoginRequest(){
	var login = document["loginform"].memberloginname.value;
	var password = document["loginform"].passwordlogin.value;

	if(login==""){
		alert(MSG.EMPTY_LOGIN);
		return;
	}
	if(password==""){
		alert(MSG.EMPTY_PASSWORD);
		return;
	}
	sendRequest('../content/login.php?login='+login+'&password='+password+'&x='+Math.random(10000), 'content',document, onLoginReponse,document.getElementById('_loginLoadingDiv'));
	
}


function submitForgetRequest(){
	var email = document["forgetform"].membermail.value;

	if(email==""){
		alert(MSG.EMPTY_EMAIL);
		return;
	}
	sendRequest('../content/forget.php?email='+email+'&x='+Math.random(10000), 'content',document, onForgetReponse,document.getElementById('_loginLoadingDiv'));

}

function onForgetReponse(responseXML, doc) {
	var result = responseXML.getElementsByTagName("result")[0].firstChild.data;
	if(result==0){
		alert(MSG.FORGET_PASSWORD_INVALID_EMAIL);
	}else{
		alert(MSG.FORGET_PASSWORD_SUCCESS);
		hideForgetBox();
	}
}


function submitConfirmLoginRequest(){
	sendRequest('../content/login.php?x='+Math.random(10000), 'content',document, onConfirmLoginReponse,null);
}

function submitLogoutRequest(){
	sendRequest('../content/login.php?x='+Math.random(10000), 'content',document, onLogoutReponse,null);
}

function showLoginBox(){
	var l = document.getElementById("_loginBoxLayer");
	l.style.visibility = "visible";
	
	var b = document.getElementById("_loginBoxLayerBlock");
	b.style.visibility = "visible";
	
	siteTrack('p_login');
}



function updateLoginStautsDisplay(name,id){	
	_loginedNickname = name;
	//var l = document.getElementById("_loginStatusDiv");
	//l.innerHTML  = getLogoutStatusStr();	
}

function updateLogoutStautsDisplay(){	
	//var l = document.getElementById("_loginStatusDiv");
	//l.innerHTML  = getLoginStatusStr();
}