var _loginedNickname = "";
var _isLogined = false;
var _loginedId = "";

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){
	//alert("onSuccessLogin, name="+name+", id="+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("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.INITIALIZE) {
		alert(MSG.SERVER_BUSY);
	} else if (result==LoginConst.INVALID_PASSWORD) {		
		alert(MSG.INVALID_PASSWORD);
	} else if (result==LoginConst.INVALID_LOGIN) {		
		alert(MSG.INVALID_LOGIN);
	} else if (result==LoginConst.SUCCESS_LOGIN) {		
		onSuccessLogin(memberName,memberId);
		alert(MSG.SUCCESS_LOGIN );
		siteTrack('a_login');  // problem
		
		toUrl = gup("curUrl");
		toUrl = toUrl.replace(/_gp_/g,"?");
		toUrl = toUrl.replace(/_va_/g,"&");
		var sec = gup("sec");
		if ( (sec=="game")&&(toUrl.indexOf("section=game")==-1) ) {
			window.opener.location.href = toUrl+"?section=game";
		} else {
			window.opener.location.href = toUrl;
		}
		
		/*
		if (toUrl.indexOf("")==-1) {
			window.opener.location.href = toUrl;
		} else {	
			//callExternalInterface();
			var lc_id = gup("lc_id");
			alert("lc_id="+lc_id);
			thisMovie("myFlashMovie").hideLogin(lc_id);
			//thisMovie("myFlashMovie").hideLogin();
			//alert(thisMovie("myFlashMovie"));	
		}
		*/
		
		setTimeout("window.close();", 500);		
		return true;
	} else {	
		alert(MSG.SERVER_BUSY);
	}
	updateText(doc.getElementById("_loginLoadingDiv"), "&nbsp;", "hidden");
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}

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"].login.value;
	var password = document["loginForm"].password.value;
	
	if (login=="") {
		alert(MSG.EMPTY_LOGIN);
		return;
	}
	if (password=="") {
		alert(MSG.EMPTY_PASSWORD);
		return;
	}
	sendRequest('/Action/06_login/login.aspx?login='+login+'&password='+password+'&x='+Math.random(10000), 'content',document, onLoginReponse,document.getElementById('_loginLoadingDiv'));
}



function submitConfirmLoginRequest(){
	//alert("@submitConfirmLoginRequest");
	sendRequest('/Action/06_login/confirm_login.aspx?x='+Math.random(10000), 'content',document, onConfirmLoginReponse,null);
}

function submitLogoutRequest(){
	sendRequest('/Action/06_login/logout.aspx?x='+Math.random(10000), 'content',document, onLogoutReponse,null);
}
function showLoginBox(lc_id, sec){
	//alert("@showLoginBox, lc_id="+lc_id);
	thispage=window.location.href;
	var curUrl = thispage.replace(/\?/g, "_gp_");
	curUrl = curUrl.replace(/\&/g, "_va_");
	var getPara = "?curUrl="+curUrl+"&lc_id="+lc_id+"&sec="+sec;
	////////// HTTPS
	//_child_login = window.open('https://nissin09.e-crusade.com/06_login/06_login.html','mywindow','width=400,height=300');
	
	if (thispage.indexOf('nissin09.e-crusade.com') >-1) 
		_child_login = window.open('https://nissin09.e-crusade.com/06_login/06_login.html'+getPara,'mywindow','width=400,height=300','resizable=No');
	else if (thispage.indexOf('nissinfoods.com.hk:81') >-1)
		_child_login = window.open('https://www.nissinfoods.com.hk:8443/06_login/06_login.html'+getPara,'mywindow','width=400,height=300','resizable=No');
	else if (thispage.indexOf('nissinfoods.com.hk') >-1) 
		_child_login = window.open('https://www.nissinfoods.com.hk/06_login/06_login.html'+getPara,'mywindow','width=400,height=300','resizable=No');

	siteTrack('p_login');
}
/*
function showLoginBox(){
	////////// HTTPS
	//_child_login = window.open('https://nissin09.e-crusade.com/06_login/06_login.html','mywindow','width=400,height=300');
	thispage=window.location.href;
	if (thispage.indexOf('http://nissin09.e-crusade.com') >-1) 
		_child_login = window.open('https://nissin09.e-crusade.com/06_login/06_login.html','mywindow','width=400,height=300','resizable=No');
	else if (thispage.indexOf('nissinfoods.com.hk:81') >-1)
		_child_login = window.open('https://www.nissinfoods.com.hk:8443/06_login/06_login.html','mywindow','width=400,height=300','resizable=No');
	else if (thispage.indexOf('nissinfoods.com.hk') >-1) 
		_child_login = window.open('https://www.nissinfoods.com.hk/06_login/06_login.html','mywindow','width=400,height=300','resizable=No');
	/ *
	var l = document.getElementById("_loginBoxLayer");
	l.style.visibility = "visible";
	
	var b = document.getElementById("_loginBoxLayerBlock");
	b.style.visibility = "visible";
	* /
	siteTrack('p_login');
}
*/

function hideLoginBox(){
	//alert("@hideLoginBox");
	/*var l = document.getElementById("_loginBoxLayer");
	l.style.visibility = "hidden";
	
	var b = document.getElementById("_loginBoxLayerBlock");
	b.style.visibility = "hidden";
	*/
	
	var parameterList = new Array('onHideLoginBox');
	proxyFlashAnnounce(parameterList);
}

function updateLoginStautsDisplay(name,id){	
	_loginedNickname = name;
	//var l = document.getElementById("_loginStatusDiv");
	//l.innerHTML  = getLogoutStatusStr();	
}

function updateLogoutStautsDisplay(){	
	//var l = document.getElementById("_loginStatusDiv");
	//l.innerHTML  = getLoginStatusStr();
}

function alertFunc(){

	var test = window.location;
	return test;
}

function gup( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    
	return "";  
	else    
	return results[1];
}	