﻿function Global() {
    this.moduleClientScriptID = "";
    this.LoginUrl = "http://www.hidoc.co.kr/member/login.aspx";

    this.EmailFormCheck = function(emailValue) {
        if (emailValue != "") {
            var chkEmail = emailValue.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);

            if (chkEmail == null)
                return false;
            else
                return true;
        }
        else
            return true;
    }


    // 필요한 이미지를 미리 로드 합니다. -------------------------------------------------------------------------------------------

    this.PreloadImage = 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];
        }
    }

    this.PopUp = function PopUp(url, width, height, positionTop, positionLeft, scrollbars) {
        window.open(url, 'popup', 'left='+positionLeft+', top='+positionTop+', width='+width+',height='+height+',scrollbars='+scrollbars+'');
    }

    // 해당 컨트롤을 찾습니다.
    this.FindControl = function(controlName) {
        var control = $get(controlName);

        if (control == null && this.moduleClientScriptID != "")
            control = $get(this.moduleClientScriptID + controlName);

        return control;
    }

    this.NeedLogin = function() {
        alert("먼저 로그인 해야 합니다.");
        location.href = this.LoginUrl;
        return false;
    }

    this.DialogOpen = function(url, name, width, height, enableScroll) {
       
        /// <summary>지정된 URL, 크기의 창을 중간에 표시 합니다.</summary>
        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.GetCookie = function(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 "";
    }

    this.SetCookie = function(name, value, expiredays) {
        var todayDate = new Date();
        todayDate.setDate(todayDate.getDate() + expiredays);
        document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";"
    }

    // 필수 항목의 값이 공백인지를 체크하고 경고문구를 출력합니다.
    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.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.GetQueryString = function(valuename) {
        var rtnval = "";
        var nowAddress = unescape(location.href);
        var parameters = (nowAddress.slice(nowAddress.indexOf("?") + 1, nowAddress.length)).split("&");

        for (var i = 0; i < parameters.length; i++) {
            var varName = parameters[i].split("=")[0];
            if (varName.toUpperCase() == valuename.toUpperCase()) {
                rtnval = parameters[i].split("=")[1];
                break;
            }
        }

        return rtnval;
    }

    // 주민번호 검사
    this.CheckRegNumber = function(regno1, regno2) {

        var NUM = "0123456789";

        var PID1 = this.FindControl(regno1).value;
        var PID2 = this.FindControl(regno2).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("주민번호가 올바르지 않습니다.");
            this.FindControl(regno1).select();
            return false//-1;
        }

        if (PID1.length != 6 || nMondth < 1 || nMondth > 12 || nDay < 1 || nDay > 31) {
            alert("주민번호가 올바르지 않습니다.");
            this.FindControl(regno1).select();
            return false//-1;
        }

        if (!IsValid(PID2, NUM)) {
            alert("주민번호가 올바르지 않습니다.");
            this.FindControl(regno1).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("주민번호가 올바르지 않습니다.");
            this.FindControl(regno1).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("주민번호가 올바르지 않습니다.");
                this.FindObject(regno1).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("주민번호가 올바르지 않습니다.");
                this.FindControl(regno1).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("주민번호가 올바르지 않습니다.");
                this.FindControl(regno1).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;
}

var global = new Global();
