﻿// --------------- 쿠키 --------------------


function Global_GetCookie(name) {
    var nameOfCookie = name + "=";
    var x = 0;
    while (x <= document.cookie.length) {
        var y = (x + nameOfCookie.length);
        if (document.cookie.substring(x, y) == nameOfCookie) {
            if ((endOfCookie = document.cookie.indexOf(";", y)) == -1)
                endOfCookie = document.cookie.length;
            return unescape(document.cookie.substring(y, endOfCookie));
        }
        x = document.cookie.indexOf(" ", x) + 1;
        if (x == 0)
            break;
    }

    return "";
}

function Global_SetCookie(name, value, expiredays) {
    var todayDate = new Date();
    todayDate.setDate(todayDate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

// -------------- Ajax ----------------------

// XMLHttpRequest 생성
function newXMLHttpRequest() {
    var reqHttp;
    if (window.ActiveXObject) {	 // IE
        try {
            reqHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                reqHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e1) {
                reqHttp = null;
            }
        }
    } else if (window.XMLHttpRequest) {  // IE 이외
        try {
            reqHttp = new XMLHttpRequest();
        } catch (e) {
            reqHttp = null;
        }
    }
    if (reqHttp == null) errorMessage();   //XMLHttpRequest 생성 실패
    return reqHttp;
}

// 지원할 수 없는 브라우저 사용
function errorMessage() {
    alert("지원할 수 없는 브라우저입니다.");
}

// readyState와 status 체크 
function openSendStatus(getPost, urlFileAppl, trueFalse, sendData, cbFunction) {
    var xmlHttp = newXMLHttpRequest();               //XMLHttpRequest 생성
    xmlHttp.open(getPost, urlFileAppl, trueFalse);    //송신방법,URL,통신방법
    xmlHttp.onreadystatechange = function() {        //처리상태 변경 발생
        if (xmlHttp.readyState == 4) {                     //서버 처리 완료
            if (xmlHttp.status == 200) {                    //파일 수신 성공
                cbFunction(xmlHttp);                        //callback 함수
            } else {
                exceptionControl(xmlHttp);                //예외 처리
            }
        }
    }
    var conType = "application/x-www-form-urlencoded; charset=UTF-8";
    xmlHttp.setRequestHeader("Content-Type", conType);
    xmlHttp.send(sendData);      //처리 데이터 송신
}

// 예외 처리 (status != 200)
function exceptionControl(xmlHttp) {
    var exceptShow = "상태 코드: " + xmlHttp.status;
    exceptShow += ",  비정상으로 종료되었습니다.";
    alert(exceptShow);
}


// --------------- SSL 인증 ----------------

function Global_SetSSLForm(SSLUrl, ReturnUrl) {
	    var div = document.forms[0].childNodes[0];
    for (var i = 0; i < div.childNodes.length; i++) {
        if (div.childNodes[i].tagName == 'INPUT' && div.childNodes[i].type == 'hidden' && div.childNodes[i].name == '__VIEWSTATE')
        {
            div.removeChild(div.childNodes[i]);
        }
    }

    var newObj = document.createElement("INPUT");

    newObj.type = "hidden";
    newObj.name = "RedirectURL";

    if (ReturnUrl == "")
        ReturnUrl = location.href;
    else
        ReturnUrl = "http://" + location.host + ReturnUrl;

    newObj.value = ReturnUrl;

    document.forms[0].appendChild(newObj);

    document.forms[0].action = "https://Login.mcircle.co.kr" + SSLUrl;

    document.forms[0].method = "post";

    document.forms[0].submit();

}

// 해당 텍스트 컨트롤이 공백인지를 검사
function Global_ControlValueCheck(controlName, message, enableBadWordCheck) {
    if (document.all[controlName] != null) {
        if (Global_TrimSpaces(document.all[controlName].value) == '') {
            alert(message);
            document.all[controlName].value = '';
            document.all[controlName].focus();
            return false;
        }
        else {
			/*
            if (enableBadWordCheck) {
                // 불량단어 필터링이 걸릴경우 불량단어를 검사
                return Global_ChkeckBadWord(controlName);
            }
            else {
				*/
                return true;
            //}
        }
    }
    else {
        return true
    }
}

function Global_ViewCalender(controlObj) {
    var result = showModalDialog('/Library/Modules/calendar.htm', window, "scroll:no; status:no; dialogLeft:100px; dialogTop:100px; dialogWidth:310px; dialogHeight:350px;");

    if (result != null || result == "") {
        document.all[controlObj].value = result;
    }

}

function PreloadImage() {
    var img_list = PreloadImage.arguments;
    if (document.preloadlist == null)
        document.preloadlist = new Array();
    var top = document.preloadlist.length;
    for (var i = 0; i < img_list.length; i++) {
        document.preloadlist[top + i] = new Image;
        document.preloadlist[top + i].src = img_list[i + 1];
    }
}

function Global_GetFileExtension(controlName) {
    var contolValue = document.all[controlName].value;

    var index = contolValue.lastIndexOf(".")

    if ((Global_TrimSpaces(contolValue) != "") && (contolValue.length != 0)) {
        return contolValue.substring(index + 1).toLowerCase();
    }
    else {
        return '';
    }
}

function Global_ZipSearch() {
    var obj = window.open('/member/SearchZipCode.aspx', 'zip', 'scrollbars=1,width=505,height=280', false);
    if (obj == null)
        alert("먼저 팝업창 금지를 해제 하십시오.");
}

function Global_CheckID(controlName) {
    searchko = document.all[controlName].value;

    for (i = 0; i < searchko.length; i++) {
        sko = searchko.charAt(i);

        if ((sko < '0' || sko > '9') && (sko < 'a' || sko > 'z') && (sko < 'A' || sko > 'Z')) {
            alert('회원 ID는 영문과 숫자만 가능합니다.');
            document.all[controlName].value = "";
            document.all[controlName].focus();
            return false;
        }
    }

    return true;
}

function Global_EmailCheck(controlName) {
    if (document.all[controlName] != null) {
        if (Global_TrimSpaces(document.all[controlName].value) != "") {
            var chkEmail = document.all[controlName].value.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);

            if (chkEmail == null) {
                alert("메일 주소가 올바르지 않습니다.");
                document.all[controlName].value = "";
                document.all[controlName].focus();
                return false;
            }
            else {
                return true;
            }
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}

// HTML의 편집기의 공백을 체크
function Global_CheckHtmlEditor(controlName, enableBadWordCheck) {
    if (document.all[controlName] == null) {
        alert("내용입력에 필요한 HtmlEditor가 정의되지 않았습니다.");
        return false;
    }
    else {
        var editorObject = document.all["_" + controlName + "_editor"];
        var contentString = "";

        if (editorObject.tagName == "IFRAME") {
            contentString = editorObject.contentWindow.document.body.innerText;
        }
        else {
            contentString = editorObject.innerText;
        }

        if (Global_TrimSpaces(contentString) == '') {
            alert("내용을 입력해야 합니다.");
            editorObject.focus();
            return false;
        }
        else {/*
            // 금지단어에 대한 검사
            if (enableBadWordCheck) {
                return Global_ChkeckBadWord(controlName);
            }
            else {
            }*/
                return true;
        }
    }
}

// 금지단어를 검사
function Global_ChkeckBadWord(controlName) {
    //  이 함수를 사용하기 위해서는 불량 단어 변수 BadWord를 지정해야 합니다.
    var TempObj = document.all[controlName];

    if (BadWord == null) {
        alert("등록된 금지단어가 없습니다.");
        return;
    }

    var BadText = BadWord.split(",");

    if (BadText == "") {
        return false;
    }

    var swear_words_arr = new Array();

    for (var i = 0; i < BadText.length; i++) {
        swear_words_arr = swear_words_arr.concat(BadText[i]);
    }

    var swear_alert_arr = new Array;
    var swear_alert_count = 0;

    var compare_text = TempObj.value;

    for (var i = 0; i < swear_words_arr.length; i++) {
        for (var j = 0; j < (compare_text.length); j++) {
            if (swear_words_arr[i] == compare_text.substring(j, (j + swear_words_arr[i].length)).toLowerCase()) {
                swear_alert_arr[swear_alert_count] = compare_text.substring(j, (j + swear_words_arr[i].length));
                swear_alert_count++;
            }
        }
    }

    var alert_text = "";

    for (var k = 1; k <= swear_alert_count; k++) {
        alert_text += "\n" + "* " + swear_alert_arr[k - 1];
    }

    if (swear_alert_count > 0) {
        alert("금지된 단어를 사용하였습니다.\n_______________________________\n" + alert_text + "\n_______________________________");
        TempObj.focus();
        return false;
    }
    else {
        return true;
    }
}

// 해당 문자열의 공백을 제거 ---------------------------------------------------

function Global_TrimSpaces(text) {
    var temp = "";

    text = '' + text.toUpperCase();

    splitstring = text.split(" ");

    for (i = 0; i < splitstring.length; i++)

        temp += splitstring[i];

    return temp;
}

// 해당 문자열에 특정 태그를 제거
function Global_TrimBRTag(text) {
    var temp = "";

    text = '' + text.toUpperCase();

    splitstring = text.split("<BR>");
    for (i = 0; i < splitstring.length; i++)
        temp += splitstring[i];

    return temp;
}

function Global_TrimNbspTag(text) {
    var temp = "";

    text = '' + text.toUpperCase();

    splitstring = text.split("&NBSP;");
    for (i = 0; i < splitstring.length; i++)
        temp += splitstring[i];

    return temp;
}

//-------------------------------------------------------------------------------

function Global_CheckPID(controlName1, controlName2) {
    var NUM = "0123456789";

    var PID1 = document.all[controlName1].value;
    var PID2 = document.all[controlName2].value;

    var chk = 0;

    var nYear = PID1.substring(0, 2);

    var nMondth = PID1.substring(2, 4);

    var nDay = PID1.substring(4, 6);

    var nSex = PID2.charAt(0);

    if (!IsValid(PID1, NUM)) {
        alert("주민번호가 올바르지 않습니다.");
        document.all[controlName1].select();
        return false//-1;
    }

    if (PID1.length != 6 || nMondth < 1 || nMondth > 12 || nDay < 1 || nDay > 31) {
        alert("주민번호가 올바르지 않습니다.");
        document.all[controlName1].select();
        return false//-1;
    }

    if (!IsValid(PID2, NUM)) {
        alert("주민번호가 올바르지 않습니다.");
        document.all[controlName1].select();
        return false//1;
    }

    if (PID2.length != 7 || (nSex != 1 && nSex != 2 && nSex != 3 && nSex != 4 && nSex != 5 && nSex != 6 && nSex != 7 && nSex != 8)) {
        alert("주민번호가 올바르지 않습니다.");
        document.all[controlName1].select();
        return false//1;
    }

    if (nSex == 5 || nSex == 6 || nSex == 7 || nSex == 8) {
        var sum = 0;
        var odd = 0;
        var PID = PID1 + PID2;
        buf = new Array(13);
        for (i = 0; i < 13; i++) buf[i] = parseInt(PID.charAt(i));
        odd = buf[7] * 10 + buf[8];
        if (odd % 2 != 0) {
            alert("주민번호가 올바르지 않습니다.");
            document.all[controlName1].select();
            return false//-1;
        }

        multipliers = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5];
        for (i = 0, sum = 0; i < 12; i++) sum += (buf[i] *= multipliers[i]);
        sum = 11 - (sum % 11);
        if (sum >= 10) sum -= 10;
        sum += 2;
        if (sum >= 10) sum -= 10;
        if (sum != buf[12]) {
            alert("주민번호가 올바르지 않습니다.");
            document.all[controlName1].select();
            return false//-1;
        }
    }
    else {
        var i;
        for (i = 0; i < 6; i++) {
            chk += ((i + 2) * parseInt(PID1.charAt(i)));
        }

        for (i = 6; i < 12; i++) {
            chk += ((i % 8 + 2) * parseInt(PID2.charAt(i - 6)));
        }

        chk = 11 - (chk % 11);
        chk %= 10;

        if (chk != parseInt(PID2.charAt(6))) {
            alert("주민번호가 올바르지 않습니다.");
            document.all[controlName1].select();

            return false//-1;
        }
    }

    return true//0;
}

// 주민등록체크의 서브 함수로 사용됩니다.
function IsValid(s, spc) {
    var i;
    if (s.length < 1) return false;
    for (i = 0; i < s.length; i++) {
        if (spc.indexOf(s.substring(i, i + 1)) < 0) {
            return false;
        }
    }

    return true;
}
function setLeftMenu(src, width, Height, loop, quality, wmode, bgcolor, FlashVars, url) {
    if (location.pathname.toLowerCase() != url.toLowerCase()) {
        SetCommonFlashObject(src, width, Height, loop, quality, wmode, bgcolor, FlashVars);
    }
    else {
        SetCommonFlashObject('/images/submeun_on.swf', width, Height, loop, quality, wmode, bgcolor, FlashVars);
    }
}
function setLeftMenu2(src, width, Height, loop, quality, wmode, bgcolor, FlashVars, url) {
var show_name=location.pathname.toLowerCase();
var want_name_array=show_name.split("/");
var want_name=want_name_array[2];	

    if (want_name.substring(0,6) != url.toLowerCase()) {
        SetCommonFlashObject(src, width, Height, loop, quality, wmode, bgcolor, FlashVars);
    }
    else {
        SetCommonFlashObject('/images/submeun_on.swf', width, Height, loop, quality, wmode, bgcolor, FlashVars);
    }
}

