﻿/**** Ajax 관련 함수 시작 ****/
var ajax = {};
ajax.xhr = {};

ajax.xhr.Request = function(url, params, callback, method, seq) {
    this.url = url;
    this.params = params;
    this.callback = callback;
    this.method = method;
    this.send();
    this.seq = seq
}
ajax.xhr.Request.prototype = {
    getXMLHttpRequest: function() {
        if (window.ActiveXObject) {
            try {
                return new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e1) { return null; }
            }
        } else if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else {
            return null;
        }
    },
    send: function() {
        this.req = this.getXMLHttpRequest();

        var httpMethod = this.method ? this.method : 'GET';
        if (httpMethod != 'GET' && httpMethod != 'POST') {
            httpMethod = 'GET';            
        }
        var httpParams = (this.params == null || this.params == '') ?
		                 "TimeStamp=" + new Date().getTime() : this.params + "&TimeStamp=" + new Date().getTime();

        var httpUrl = this.url;
        if (httpMethod == 'GET' && httpParams != null) {
            httpUrl = httpUrl + "?" + httpParams;
        }
        this.req.open(httpMethod, httpUrl, true);
        this.req.setRequestHeader(
			'Content-Type', 'application/x-www-form-urlencoded');
        var request = this;
        this.req.onreadystatechange = function() {
            request.onStateChange.call(request);
        }
        this.req.send(httpMethod == 'POST' ? httpParams : null);

    },
    onStateChange: function() {
        this.callback(this.req, this.seq);
    }
}
/**** Ajax 관련 함수 끝 ****/

/**** 문자열 공백 제거 ****/
String.prototype.trim = function() {
       return this.replace(/^\s+|\s+$/g,"");   
}

/**** 비로그인 & 로그인 박스 포커싱 ****/
function IsNotLoginBoard() {
    alert('로그인을 하셔야 게시물을 작성할 수 있습니다.');
    document.getElementById("txtUserID").focus();
}

function IsNotLoginComment() {
    alert('꼬릿글 작성을 위해 먼저 로그인을 해 주세요.');
    document.getElementById("txtUserID").focus();
}

/**** 한글 바이트 체크 하기 ****/
function fnByteLength(str){
    return str.replace(/[^\x00-\x7F]/g,"xx").replace(/([<\x0D])/g,function($0,$1){ return ($1 == "<")?"&lt;":"<br>"+$1;}).length;
}

String.prototype.cut = function(len, tail) 
{
    var str = this;
    var l = 0;
    for (var i=0; i<str.length; i++) 
    {
        l += (str.charCodeAt(i) > 128) ? 2 : 1;
        if (l > len) return str.substring(0,i) + tail;
    }
    return str;
}

// 쿠키 설정
function SetCookie( name, value, expiredays )
{
	var todayDate = new Date();
	todayDate.setHours( todayDate.getHours() + expiredays );
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

// 쿠키 불러오기
function GetCookie(name){
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}


/**** 메뉴 링크 ****/
function flash_link(link_value)
{
	switch( link_value.toString() )
	{
		case "10"	:	location.href = "/Main/HomeMain.aspx";                // 홈
				    			break;	
		case "50"	:	RunGameStart();               // 게임시작버튼
				    			break;	
        case "60"	:	location.href = "/Download/Download.aspx";    // 게임 다운로드
				    			break;	
        case "70"	:	location.href = "/Guide/Default.aspx";                // 게임 가이드
				    			break;	

		case "100"	:   		
        case "110"	:	location.href = "/News/NewsList.aspx";                      // 새소식
				    			break;	
        case "120"	:	location.href = "/News/NewsList.aspx?noticetype=1";     // 공지사항
				    			break;
        case "130"	:	location.href = "/News/NewsList.aspx?noticetype=2";     // 업데이트
				    			break;
        case "140"	:	location.href = "/News/NewsList.aspx?noticetype=4";     // 점검
				    			break;
        case "150"	:	location.href = "/News/Event/EventList.aspx";               // 이벤트
				    			break;				    							    							    			

        case "200"	:					    			
        case "210"	:	location.href = "/Community/FreeBoard/FreeBoardList.aspx";        // 자유게시판
				    			break;
        case "220"	:	location.href = "/Community/MediaBoard/MediaBoardList.aspx";    // 자유게시판
				    			break;	
        case "230"	:	location.href = "/Community/Strategy/StrategyBoardList.aspx";          // 공략게시판
				    			break;
        case "240"	:	location.href = "/KinBoard/Default.aspx";                               // 지식가이드
				    			break;
        case "250"	:	location.href = "/Community/GMBoard/GMBoardList.aspx";          // GM통신소
				    			break;
        case "260"	:	location.href = "/Community/Poll/PollList.aspx";                        // 설문조사
				    			break;
        
        case "300"	:	location.href = "/Guide/Default.aspx";                                    // 가이드 메인
				    			break;
        case "310"	:	window.open("/Guide/Feature/GuideSynopsis.aspx", "feature", "width=900,height=650");       // 게임특징
				    			break;
        case "320"    :   window.open("/Guide/GameStart/GuideGameStart.aspx", "feature", "width=900,height=650");  // 게임시작
				    			break;
        case "330"    :   window.open("/Guide/Interface/GuideLobby.aspx", "feature", "width=900,height=650");         // 인터페이스
				    			break;
        case "340"    :   window.open("/Guide/Mission/GuideExplode.aspx", "feature", "width=900,height=650");        // 게임모드
				    			break;
        case "350"    :   window.open("/Guide/Control/GuideControl.aspx", "feature", "width=900,height=650");         // 조작방법
				    			break;

        case "400"    :   location.href = "/Download/Download.aspx";       // 다운로드
				    			break;
        case "410"    :   location.href = "/Download/Download.aspx";       // 다운로드
                                break;
        case "420"	:	location.href = "/Download/Media.aspx";            // 동영상
				    			break;	
        case "430"	:	location.href = "/Download/Wallpaper.aspx";       // 월페이퍼
				    			break;	
        
        case "500"	:					    			
        case "510"	:	location.href = "/Support/Faq/FaqList.aspx";        // FAQ
				    			break;
        case "520"	:	location.href = "/Support/Inquiry/InquiryWrite.aspx";    // 1:1문의
				    			break;
        case "530"	:	location.href = "/Support/Inquiry/InquiryWrite.aspx?section=bug"; // 버그신고 / 건의
				    			break;
        case "540"	:	location.href = "/Support/Policy/Policy.aspx";           // 운영정책
				    			break;
				    					
		default : alert(link_value);		
	}
}

function RunGameStart() {
    var isLogin = GetCookie("DAUTH");
    var isTester = GetCookie("isTester");
    var isPCB = GetCookie("isPCB");
    if (isLogin == "" || isLogin == null) {
        alert('로그인 후 이용 가능합니다.');
        document.getElementById("txtUserID").focus();
        return false;
    }
    else {
        //if (isPCB == 1 || isPCB == 2 || isTester == 1 || isTester == 99) {
            var Installed = false;
            try {
                var xObj = new ActiveXObject("DragonflyControl.DragonflyCtrl.1");
                if (xObj)
                    Installed = true;
            }
            catch (ex) {
            }

            if (Installed) { // 설치된경우
                try {
                    frames['ifrmGame'].location.href = "/Main/GameOn/GameStart.aspx";
                }
                catch (ex) {
                }
            }
            else {
                top.location.href = "/Main/GameOn/ActiveXGuide.aspx";
            }
        //} else {
        //    alert('베타 테스터가 아닙니다.\r\n다음 테스트를 기대해 주세요.\r\nTip : 벙커 PC방 가맹점에서는 테스터가\r\n아니어도 참여가 가능합니다.');
        //}
    }
}