﻿


/* --- 숫자만 입력 가능 (onKeyDown 이벤트) --- */
function onlyNumberInput() {
    var code = window.event.keyCode;

    if ((code > 34 && code < 41) || (code > 47 && code < 58) || (code > 95 && code < 106) || code == 8 || code == 9 || code == 13 || code == 46) {
        window.event.returnValue = true;
        return;
    }
    window.event.returnValue = false;
}

/* --- 날짜 형식 (onKeyUp 이벤트) --- */
function dateFormat(obj, nextObj) {
    var str = onlyNum(obj.value);

    var leng = str.length;
    var nxObj = document.getElementById(nextObj);
    obj.value = str;
    if (leng > 8) {
        $("#MasterLayer2 .msg_center").html('날짜 형식이 아닙니다.');
        $("#MasterLayer2").jqmShow();
        obj.value = "";
        return false;
    }
    
    switch (leng) {
        case 1:
        case 2:
        case 3:
        case 4: obj.value = str; break;
        case 5:
        case 6: obj.value = str.substring(0, 4) + "-" + str.substring(4); break;
        case 7:
        case 8: obj.value = str.substring(0, 4) + "-" + str.substring(4, 6) + "-" + str.substring(6);
            if (chkDate(str, obj, nxObj)) {
                if (obj.value.length == 10) {
                    obj.onblur();
                }
            }
            break;          
    }
}

/* --- 숫자만 리턴 --- */
function onlyNum(val) {
    var num = val;
    var tmp = "";

    for (var i = 0; i < num.length; i++) {
        if (num.charAt(i) >= 0 && num.charAt(i) <= 9)
            tmp = tmp + num.charAt(i);
        else
            continue;
    }
    return tmp;
}

/* --- 날짜 유효성 검사 --- */
function chkDate(str, obj, nxObj) {
    if (str.length == 8) {
        vDate = new Date();
        vDate.setFullYear(str.substring(0, 4));
        vDate.setMonth(parseInt(str.substring(4, 6)) - 1);
        vDate.setDate(str.substring(6));

        if (vDate.getFullYear() != str.substring(0, 4) ||
   vDate.getMonth() + 1 != str.substring(4, 6) ||
   vDate.getDate() != str.substring(6)) {

            $("#MasterLayer2 .msg_center").html('날짜 형식이 아닙니다.');
            $("#MasterLayer2").jqmShow();
            obj.value = "";
            return false;
        }

        if (nxObj != null) {
            nxObj.focus();
        }
    }

    return true;
}

function OnBlurCheck(obj, strtobj, endobj) {

    if (obj.value.length != 10) {
        obj.value = "";
    }
    else {

        checkFromTo(strtobj, endobj);
    }
}

function checkFromTo(strtobj, endobj) {
    var stdt = document.getElementById(strtobj);
    var endt = document.getElementById(endobj);

    if (endt.value != '' && stdt.value > endt.value) {
        $("#MasterLayer2 .msg_center").html('종료일이 시작일보다 빠릅니다.\n\n다시 입력해 주십시오.');
        $("#MasterLayer2").jqmShow();
        endt.value = "";
        return false;
    }

    return true;
}

/// 날짜기능 제어용 스크립트 정의 

/// 전역변수
/// Type 1 : dd/MMM/yyyy
/// Type 2 : MM/dd/yyyy
/// Type 3 : yyyy-MM-dd
var myDateFormat;



/// ===============================================================================================
/// 설명 : 입력한 문자열을 지정한 타입에 맞는 날짜타입으로 반환
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function GetDateString(sDT) {
    var inDate;

    inDate = GetDate(sDT);

    if (inDate == null)
        return "Invalid Format";

    return GetDateFormat(inDate);
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : 입력한 문자열을 지정한 타입에 맞는 날짜타입으로 반환
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function GetDateStringForDB(sDT) {
    var inDate;

    inDate = GetDate(sDT);

    if (inDate == null)
        return "Invalid Format";

    return GetDateFormatForDB(inDate);
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : 두개의 날짜를 비교하여 비교 결과를 리턴한다.
///        null : 포맷이 잘못 되었을 경우
///        숫자 : 첫번째 날짜에서 두번째 날짜를 뺀 일자. 마이너스가 나올 수도 있음.
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function CompareDateString(sDT, sDT2) {
    var DT;
    var DT2

    DT = GetDate(sDT);
    DT2 = GetDate(sDT2);

    if (DT == null || DT2 == null)
        return null;

    var daysGap = (DT.getTime() - DT2.getTime()) / (1000 * 60 * 60 * 24);

    return Math.round(daysGap);
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : 문자열에서 지정한 타입에 맞는 년/월/일 정보를 추출하여 날짜객체를 생성하여 반환
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function GetDate(sDT) {
    var rtnDate;
    var year;
    var month;
    var day;

    // dd/MMM/yyyy 포맷의 경우
    if (typeof (sDT) == "string" && sDT.length == 11 && sDT.charAt(2) == "/" && sDT.charAt(6) == "/") {
        var arrDT = sDT.split("/");

        month = getMMM(arrDT[1]);

        if (arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(month) == true || isNaN(arrDT[2]) == true)
            return null;

        year = arrDT[2];
        day = arrDT[0];
    }
    // MM/dd/yyyy 포맷의 경우
    else if (typeof (sDT) == "string" && sDT.length == 10 && sDT.charAt(2) == "/" && sDT.charAt(5) == "/") {
        var arrDT = sDT.split("/");

        if (arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
            return null;

        year = arrDT[2];
        month = arrDT[0];
        day = arrDT[1];
    }
    // yyyy-MM-dd
    else if (typeof (sDT) == "string" && sDT.length == 10 && sDT.charAt(4) == "-" && sDT.charAt(7) == "-") {
        var arrDT = sDT.split("-");

        if (arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
            return null;

        year = arrDT[0];
        month = arrDT[1];
        day = arrDT[2];
    }
    // Invalid Format
    else {
        return null;
    }

    if (check_date(year, month, day) > 0)
        return null;

    rtnDate = new Date(year, month - 1, day);

    return rtnDate;
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : 영문식 월을 숫자타입으로 변경하여 반환.
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function getMMM(mmm) {
    if (mmm.toUpperCase() == "JAN")
        return "01";
    else if (mmm.toUpperCase() == "FEB")
        return "02";
    else if (mmm.toUpperCase() == "MAR")
        return "03";
    else if (mmm.toUpperCase() == "APR")
        return "04";
    else if (mmm.toUpperCase() == "MAY")
        return "05";
    else if (mmm.toUpperCase() == "JUN")
        return "06";
    else if (mmm.toUpperCase() == "JUL")
        return "07";
    else if (mmm.toUpperCase() == "AUG")
        return "08";
    else if (mmm.toUpperCase() == "SEP")
        return "09";
    else if (mmm.toUpperCase() == "OCT")
        return "10";
    else if (mmm.toUpperCase() == "NOV")
        return "11";
    else if (mmm.toUpperCase() == "DEC")
        return "12";
    else
        return " ";
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : 숫자타입의 월을 MMM타입으로 변경하여 반환. 함수내부에서 호출
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function getMonth(month) {
    if (month == "01")
        return "Jan";
    else if (month == "02")
        return "Feb";
    else if (month == "03")
        return "Mar";
    else if (month == "04")
        return "Apr";
    else if (month == "05")
        return "May";
    else if (month == "06")
        return "Jun";
    else if (month == "07")
        return "Jul";
    else if (month == "08")
        return "Aug";
    else if (month == "09")
        return "Sep";
    else if (month == "10")
        return "Oct";
    else if (month == "11")
        return "Nov";
    else if (month == "12")
        return "Dec";
    else
        return " ";
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : 날짜타입인지 체크함.
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function check_date(year, month, day) {
    var dat_option = year % 4;
    year = parseInt(year, 10);
    month = parseInt(month, 10);
    day = parseInt(day, 10);

    if (isNaN(year) == true) { return 1; }
    if (year < 1970) { return 1; }

    if (isNaN(month) == true) { return 2; }
    if (isNaN(day) == true) { return 3; }
    if (day < 1 || day > 31) { return 3; }

    if (month == 2) {
        if (((dat_option == 0) && (day > 29)) || ((dat_option != 0) && (day > 28))) { return 3; }
    } else if ((month == 4) ||
						(month == 6) ||
						(month == 9) ||
						(month == 11)) {
        if (day > 30) { return 3; }
    } else if ((month == 1) ||
						(month == 3) ||
						(month == 5) ||
						(month == 7) ||
						(month == 8) ||
						(month == 10) ||
						(month == 12)) {
        if (day > 31) { return 3; }
    } else {
        return 2;
    }

    return 0;
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : 지정한 타입에 맞는 날짜정보 문자열을 생성하여 반환
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function GetDateFormat(DT) {
    var year = DT.getFullYear().toString();
    var month = DT.getMonth() + 1;
    var day = DT.getDate();

    month = (month < 10 ? "0" : "") + month;
    var smonth = getMonth(month);
    day = (day < 10 ? "0" : "") + day;

    if (myDateFormat == "dd/MMM/yyyy")
        return day + "/" + smonth + "/" + year;
    else if (myDateFormat == "MM/dd/yyyy")
        return month + "/" + day + "/" + year;
    else if (myDateFormat == "yyyy-MM-dd")
        return year + "-" + month + "-" + day;
    else
        return "Invalid Format";
}
/// ===============================================================================================


/// ===============================================================================================
/// 설명 : DataBase 입력타입에 맞는 날짜정보 문자열을 생성하여 반환
/// ====================================
/// 변경일		  변경자		변경내용
/// 2008-05-13	  이상희		최초생성
function GetDateFormatForDB(DT) {
    var year = DT.getFullYear().toString();
    var month = DT.getMonth() + 1;
    var day = DT.getDate();

    month = (month < 10 ? "0" : "") + month;
    day = (day < 10 ? "0" : "") + day;

    return year + "-" + month + "-" + day;
}
/// ===============================================================================================
