/*
	메인(non-popup-page) 또는 mylb(popup-page)에서 공통으로 사용하는 함수
*/

// 로그인 체크: non-popup-page와 popup-page에서 사용 가능
function checkLoginAsCombi(isPopup){
	if(isLogged() != 'true') {
		if(isPopup)
			PopupLogin('/mypage/who'+location.search);
		else
			GNBLogin();						
		return false;	
	} else {
		if(channel == 0 && user_typeCode == '3'){
			alert('체험계정입니다. 정회원으로 전환해 주세요.');
			return false;
		} else if (channel == 0 && user_typeCode == '8'){
			alert('서비스를 이용하시려면 추가 회원 정보가 필요합니다.');
			return false;
		} 
	}								
	
	return true;
}

// 게임 닉네임 체크
function checkGameNick(myGameNick) {
	if ((null == myGameNick) || ('' == myGameNick)) {
		alert('해당 서비스는 캐릭터 생성 후에 이용 가능합니다. \n 게임내에서 캐릭터를 먼저 생성해주세요.');
		return false;
	}
	return true;
}

// Ajax 응답 실패 프로세스
function ajaxOnFailProc(request, isPopup) {
	if (request.status == 401) {
		if (isPopup) PopupLogin('/mypage/who'+location.search);
		else GNBLogin();
	} else {
		alert("오류가 발생하였습니다. " + result);
	}
}

// 친구리스트 조회
function getBuddyList(page, command, isPopup) {
	if (null == page) page = 1;
	if (null == isPopup) isPopup = false;
	if (null == command) return;
		
	var buddyListLayer = document.getElementById('pnlBuddyList');	
	if (typeof buddyListLayer == "undefined" || buddyListLayer == null) {
		alert("[ERROR getBuddyList] buddyList display layer not found.");
		return;
	}
	
	var parameters = {};
	parameters.page = page;

	if (checkLoginAsCombi(isPopup)) {		
		new Ajax.Request(unescape(command), {
				method:'post',
				encoding: 'UTF-8',
				parameters: parameters,
				onSuccess: function(request) {					
					buddyListLayer.innerHTML = request.responseText;
				},
				onFailure: function(request) {
					ajaxOnFailProc(request, isPopup);
				}
			}
		);
	}	
}

// 팸원리스트 조회
function getFamList(page, command, isPopup) {
	if (null == page) page = 1;	
	if (null == isPopup) isPopup = false;
	if (null == command) return;
	
	var famListLayer = document.getElementById('pnlFamList');
	
	if (typeof famListLayer == "undefined" || famListLayer == null) {
		alert("[ERROR getFamList] famListLayer display layer not found.");
		return;
	}
	
	var parameters = '&page=' + page;
		
	if (checkLoginAsCombi(isPopup)) { 
		new Ajax.Request(unescape(command), {
				method:'post',
				encoding: 'UTF-8',
				parameters: parameters,
				onSuccess: function(request) {
					famListLayer.innerHTML = request.responseText;
				},
				onFailure: function(request) {
					ajaxOnFailProc(request, isPopup);
				}
			}
		);
	}	
}

function renderLayer(command, layerName) {
	new Ajax.Updater(layerName
		, command
		,{	method:'get'
			, encoding: 'UTF-8'
			, evalScripts: true
			, onFailure: function()
			{
				$(layerName).innerHTML = '현재 사용자가 많아 정보를 가지고 올 수 없습니다.';
			}
		}
	);			
}