﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />
/*
Common Script
Dependency : jquery-1.3.2.min.js, AjaxObj.js
*/

// Prototype
String.prototype.Trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

// is null or Empty
String.prototype.IsNullOrEmpty = function() {
    var strText = this.Trim();
    for (var i = 0; i < strText.length; i++) {
        if ((strText.charAt(i) != "\t") && (strText.charAt(i) != "\n") && (strText.charAt(i) != "\r")) { return false; }
    }
    return true;
};

// remove
String.prototype.remove = function(regix) {
    return (regix == null) ? this : eval("this.replace(/[" + regix.meta() + "]/g, \"\")");
};

// is Email
String.prototype.IsEmail = function() {
    return (/[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*@[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*/).test(this.remove(arguments[0]));
};

// is Phone
String.prototype.IsPhone = function() {
    var delimiter = arguments[0] ? arguments[0] : "";
    return eval("(/(0[1-9]{1}[0-9]{0,1})" + delimiter + "[1-9]{1}[0-9]{2,3}" + delimiter + "[0-9]{4}$/).test(this)");
};

// open popup window
// OpenSubWindow('../open/a.aspx', 'id', 100, 100, 'no', 'no');
function OpenSubWindow(url, id, width, height, scroll, resize) {
    var top = (document.documentElement.clientHeight - height) / 2;
    var left = (document.documentElement.clientWidth - width) / 2;
    var size = 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left;
    var etc = size + ',resizable=' + resize + ',scrollbars=' + scroll + ',status=no';
    window.top.open(url, id, etc);
};

// 쿠키 체크 팝업
function OpenPopUp(url, id, width, height, scroll, resize, name, value) {
    if (GetPopCookie(name) != value) {
        OpenSubWindow(url, id, width, height, scroll, resize);
    }
}

// 쿠키 체크
function GetPopCookie(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 MakeCookie(domain, name, value, addDate) {
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + addDate);
    document.cookie = name + "=" + escape(value) + "; path=/; domain=" + domain + " ; expires=" + expireDate.toGMTString() + ";";
}

// 유효성 체크
var CommonValidator = {
    // 해당 엘리먼트 값이 Null or Empty인지 여부
    CheckIsNullOrEmpty: function(element, msgId) {
        if ($('#' + element).val().IsNullOrEmpty()) {
            AJAXObj.AlertErrorMsg(msgId);
            $('#' + element).focus();
            return false;
        }
        return true;
    },

    // 검색 키워드 체크
    CheckSearchKeyword: function(keyword) {
        return this.CheckIsNullOrEmpty(keyword, 'in_Search_Keyword');
    },
    
    // 이메일 체크
    CheckEmail: function(email) {
        if (!this.CheckIsNullOrEmpty(email, 'in_Email')) {
            return false;
        } else {
            if (!$('#' + email).val().IsEmail()) {
                AJAXObj.AlertErrorMsg('in_Email_Valid');
                $('#' + email).focus();
                return false;
            }
        }
        return true;
    },

    // 개인 정보 수집 동의
    CheckPrivateInfoAgree: function(check) {
        if ($('#' + check + ':checked').length == 0) {
            AJAXObj.AlertErrorMsg('in_PrivateInfoAgree');
            $('#' + check).focus();
            return false;
        }
        return true;
    }
}