﻿var $ = document.getElementById;

function Global() {
    this.moduleClientScriptID = "";

    this.SetClipBoard = function(elementName) {
        if (window.clipboardData) {
            var element = $(elementName);

            if (element != null) {
                var content = element.innerHTML;
                window.clipboardData.setData("Text", content);
                alert("해당 내용을 복사했습니다. 붙여넣기(Ctrl+V) 하십시오.");
            }
        }
    }

    this.CheckNumberFormat = function() {
        key = event.keyCode;

        if ((key >= 48 && key <= 57) // 키보드 상단 숫자키
           || (key >= 96 && key <= 105) // 키패드 숫자키
           || key == 8  // 백스페이스 키
           || key == 37 // 왼쪽 화살표 키
           || key == 39 // 오른쪽 화살표 키
           || key == 46 // DEL 키
           || key == 13 // 엔터 키
           || key == 9  // Tab 키
           ) {
            event.returnValue = true;
        }
        else {
            event.returnValue = false;
        }
    }

    // 필수 항목의 값이 공백인지를 체크하고 경고문구를 출력합니다.
    this.RequiredFieldValueCheck = function(controlName, message) {
        try {
            var control = this.FindControl(controlName);

            if (control != null) {
                if (this.TrimSpaces(controlName) == '') {
                    alert(message);
                    control.value = '';
                    control.focus();
                    return false;
                }
            }

            return true;
        }
        catch (exception) {
            alert(exception.description);
            return false;
        }
    }

    // 해당 컨트롤 존재 유무 체크
    this.ElementByIdCheck = function(controlName) {
        try {
            var control = this.FindControl(controlName);

            if (control != null) {
                return true;
            }
            else {
                return false;
            }
        }
        catch (exception) {
            alert(exception.description);
        }
    }

    this.FindControl = function(controlName) {
        var control = $(controlName);

        if (control == null && this.moduleClientScriptID != "")
            control = $(this.moduleClientScriptID + controlName);

        return control;
    }

    this.DialogOpen = function(url, name, width, height, enableScroll) {
        var properties = "width=" + width;
        properties += ", height=" + height;

        properties += ", left=" + ((document.body.clientWidth / 2) - (width / 2));
        properties += ", top=" + ((document.body.clientHeight / 2) - (height / 2));

        if (enableScroll)
            properties += ", scrollbars=1";

        var popWindow = window.open(url, name, properties);

        if (!popWindow)
            alert("팝업 대화상자가 차단되었습니다. 팝업 차단을 해제해 주시기 바랍니다.");
    }

    this.DialogOpen2 = function(url, name, width, height, enableScroll, resizable) {
        var properties = "width=" + width;
        properties += ", height=" + height;

        properties += ", left=" + ((document.body.clientWidth / 2) - (width / 2));
        properties += ", top=" + ((document.body.clientHeight / 2) - (height / 2));

        if (enableScroll)
            properties += ", scrollbars=1";
        if (resizable)
            properties += ", resizable=yes";

        var popWindow = window.open(url, name, properties);

        if (!popWindow)
            alert("팝업 대화상자가 차단되었습니다. 팝업 차단을 해제해 주시기 바랍니다.");
    }

    // 해당 문자열에 공백을 제거합니다.
    this.TrimSpaces = function(controlName) {
        try {
            var control = this.FindControl(controlName);

            var sourceText = control.value;

            var temp = "";

            sourceText = '' + sourceText;

            splitstring = sourceText.split(" ");

            for (i = 0; i < splitstring.length; i++)

                temp += splitstring[i];

            return temp;
        }
        catch (error) {
            alert("TrimSpaces 오류 : " + error.description);
            return false;
        }
    }

    // 로딩중 메시지를 출력합니다.
    this.ShowPreload = function(top) {
        try {
            if (!top)
                top = 500;

            // 메시지 파일을 만들려면 공통파일에 해당 객체를 넣어야 합니다.
            if ($("preloadingPannel") != null) {
                var preloadingPannel = $("preloadingPannel");

                preloadingPannel.style.display = "block";

                preloadingPannel.style.setExpression("posLeft", (document.body.clientWidth / 2) - (preloadingPannel.clientWidth / 2));
                preloadingPannel.style.setExpression("posTop", (top / 2) - (preloadingPannel.clientHeight / 2));
            }
        }
        catch (error) {
            alert(error.description);
            return false;
        }
    }

    // 메시지를 보냅니다.
    this.ShowSendMessageDialog = function(srno, id) {
        try {
            this.DialogOpen("/Library/Modules/Message/SendMessage.aspx?srno=" + srno + "&id=" + id, "sendMessage", 750, 740, false);
        }
        catch (error) {
            alert(error.description);
            return false;
        }
    }

    //=====================================================================================================
    // 숫자만 입력 (S) : Global_CheckInt(string)
    //=====================================================================================================
    this.CheckIntStripComma = function(data) {
        try {
            var flag = 1;
            var valid = "1234567890";
            var output = '';
            if (data.charAt(0) == '-') {
                flag = 0;
                data = data.substring(1);
            }

            for (var i = 0; i < data.length; i++) {
                if (valid.indexOf(data.charAt(i)) != -1)
                    output += data.charAt(i)
            }

            if (flag == 1)
                return output;
            else if (flag == 0)
                return ('-' + output);
        }
        catch (error) {
            alert(error.description);
            return false;
        }
    }

    this.CheckIntAddComma = function(what) {
        try {
            var flag = 1;
            var data = what;
            var len = data.length;

            if (data.charAt(0) == '-') {
                flag = 0;
                data = data.substring(1);
            }
            if (data.charAt(0) == '0' && data.charAt(1) == '-') {
                flag = 0;
                data = data.substring(2);
            }

            var number = this.CheckIntStripComma(data);
            number = '' + number;
            if (flag == 0)
                return ('-' + number);
            else
                return (number);
        }
        catch (error) {
            alert(error.description);
            return false;
        }
    }

    this.CheckIntReplace = function(str, original, replacement) {
        try {
            var result;
            result = "";
            while (str.indexOf(original) != -1) {
                if (str.indexOf(original) > 0)
                    result = result + str.substring(0, str.indexOf(original)) + replacement;
                else
                    result = result + replacement;
                str = str.substring(str.indexOf(original) + original.length, str.length);
            }
            return result + str;
        }
        catch (error) {
            alert(error.description);
            return false;
        }
    }

    this.CheckInt = function(what) {
        try {
            var data = what.value;

            if ((event.keyCode == 107) || (event.keyCode == 187)) {
                if ((data == "+") || (data == "0+") || (Math.floor(replace((this.CheckIntReplace(data, "+", "")), ",", "")) == 0)) {
                    dataval = "";
                }
                else {
                    dataval = data
                }
            }
            else {
                var dataval = data;
            }

            what.value = this.CheckIntAddComma(dataval);
        }
        catch (error) {
            alert(error.description);
            return false;
        }
    }

    this.OnResizedDoctorImageClick = function(fileURL, fileName) {
        var resizedImagePopUp = null;
        var width = 250;
        var height = 250;

        var screenX = (event.clientX) - (width / 2);
        var screenY = (event.clientY) + (height / 2) - 100;

        resizedImagePopUp = window.open('', 'resizedImage', 'top=' + screenY + ',left=' + screenX + ',width=350,height=350,resizable=1');

        resizedImagePopUp.document.write("<html>");
        resizedImagePopUp.document.write("<title>이미지 원본</title>");
        resizedImagePopUp.document.write("<body style='margin:0px'>");
        resizedImagePopUp.document.write("<img src='" + fileURL + fileName + "' onclick='self.close();' title='클릭하면 창을 닫습니다.' style='cursor:hand'>");
        resizedImagePopUp.document.write("</body>");
        resizedImagePopUp.document.write("</html>");
    }
}

var global = new Global();

// 타입확장
String.prototype.getValue = function() {
    try {
        return global.TrimSpaces(this);
    }
    catch (error) {
        alert("타입확장 오류 getValue() : " + error.description);
        return null;
    }
}
// 
String.prototype.setFocus = function() {
    try {
        if ($(this) != null) {
            return $(this).focus();
        }
        else if (global.moduleClientScriptID != "") {
            if ($(global.moduleClientScriptID + this) != null)
                return $(global.moduleClientScriptID + this).focus();
        }
    }
    catch (error) {
        alert("타입확장 오류 setFocus() : " + error.description);
        return null;
    }
}