/**
 * @author Lee Yeong Cheon
 * @version 1.00, 07/01/27
 * @since javascript 1.5
 */

  var Validate = {

    // 인자의 갯수가 1~2개가 아니면 경고메시지를 출력하고 null을 반환한다.
    // 인자의 갯수가 1개이고 첫번째 인자의 값이 null 이거나 값없는 문자열인 경우 true를 반환, 그렇지 않은 경우 false를 반환한다.
    // 인자의 갯수가 2개이고 첫번째 인자의 값이 null 이거나 값없는 문자열인 경우 두번째인자를 반환, 두번째 인자가 null 이면 값없는 문자열을 반환한다.
    // 인자의 갯수가 2개이고 첫번째 인자의 값이 null 이 아니고 값없는 문자열도 아닐 경우 첫번째 인자를 반환한다.
    isNull : function() {
      var retValue = null;
      switch(arguments.length) {
        case 1 :
          if(arguments[0] == null || typeof arguments[0] == Type.Undefined || (typeof arguments[0] == Type.String && arguments[0].length <= 0)) retValue = true;
          else retValue = false;
          break;
        case 2 :
          arguments[1] = this.isNull(arguments[1]) ? "" : arguments[1];
          retValue = this.isNull(arguments[0]) ? arguments[1] : arguments[0];
          break;
        default :
          alert("isNull 메소드 인자의 갯수는 1 또는 2개 이어야 합니다.");
      }
      return retValue;
    },

    // 공백문자로 이루어진 문자열인 경우만 true를 반환한다.
    isSpace : function() {
      return this.isNull(arguments[0].match(/[^\s]+/g));
    },

    // 공백문자가 포함되어 있는 문자열인 경우 true를 반환한다.
    hasSpace : function() {
      return !this.isNull(arguments[0].match(/[\s]+/g));
    },

    // 해당 문자열에서 공백을 삭제한 문자열을 반환한다.
    removeSpace : function() {
      return arguments[0].replace(/[\s]+/g,"");
    },

    // 숫자로 이루어진 문자열인 경우만 true를 반환한다.
    isNumber : function() {
      return this.isNull(arguments[0].match(/[^\d]+/g));
    },

    // 숫자가 포함되어 있는 문자열인 경우 true를 반환한다.
    hasNumber : function() {
      return !this.isNull(arguments[0].match(/[\d]+/g));
    },
    
    // 해당 문자열에서 숫자형 문자를 삭제한 문자열을 반환한다.
    removeNumber : function() {
      return arguments[0].replace(/[\d]+/g,"");
    },

    // 한글로 이루어진 문자열인 경우만 true를 반환한다.
    isKorean : function() { // \u3131-\u3163\uAC00-\uD743
      return this.isNull(arguments[0].match(/[^ㄱ-ㅣ가-힣]+/g));
    },

    // 한글이 포함되어 있는 문자열인 경우 true를 반환한다.
    hasKorean : function() {
      return !this.isNull(arguments[0].match(/[ㄱ-ㅣ가-힣]+/g));
    },

    // 해당 문자열에서 한글을 삭제한 문자열을 반환한다.
    removeKorean : function() {
      return arguments[0].replace(/[ㄱ-ㅣ가-힣]+/gi,"");
    },

    // 영문으로 이루어진 문자열인 경우만  true를 반환한다.
    isEnglish : function() {
      return this.isNull(arguments[0].match(/[^a-zA-Z]+/g));
    },

    // 영문이 포함되어 있는 문자열인 경우 true를 반환한다.
    hasEnglish : function() {
      return !this.isNull(arguments[0].match(/[a-zA-Z]+/g));
    },
    
    // 해당 문자열에서 영문을 삭제한 문자열을 반환한다.
    removeEnglish : function() {
      return arguments[0].replace(/[a-zA-Z]+/g,"");
    },

    // 문장 구성 문자(공백, 숫자, 한글, 영문)로 이루어진 문자열인 경우만 true를 반환한다.
    isWord : function() {
      return this.isNull(arguments[0].match(/[^\s\dㄱ-ㅣ가-힣a-zA-Z]+/g));
    },

    // 문장 구성 문자(공백, 숫자, 한글, 영문)가 포함되어 있는 문자열인 경우 true를 반환한다.
    hasWord : function() {
      return !this.isNull(arguments[0].match(/[\s\dㄱ-ㅣ가-힣a-zA-Z]+/g));
    },

    // 해당 문자열에서 문장 구성 문자(공백, 숫자, 한글, 영문)를 삭제한 문자열을 반환한다.
    removeWord : function() {
      return arguments[0].replace(/[\s\dㄱ-ㅣ가-힣a-zA-Z]+/g,"");
    },

    // 특수문자(문장 구성 문자 이외의 문자)로 이루어진 문자열인 경우만  true를 반환한다.
    isSpecial : function() {
      return this.isNull(arguments[0].match(/[\s\dㄱ-ㅣ가-힣a-zA-Z]+/g));
    },

    // 특수문자(문장 구성 문자 이외의 문자)가 포함되어 있는 문자열인 경우 true를 반환한다.  
    hasSpecial : function() {
      return !this.isNull(arguments[0].match(/[^\s\dㄱ-ㅣ가-힣a-zA-Z]+/g));
    },

    // 해당 문자열에서 특수문자(문장 구성 문자 이외의 문자)를 삭제한 문자열을 반환한다.
    removeSpecial : function() {
      return arguments[0].replace(/[^\s\dㄱ-ㅣ가-힣a-zA-Z]+/g,"");
    },

    // 유효한 주민등록번호인 경우만 true를 반환한다. [R]esident [R]egistration [N]umber
    isRRN : function() {
      var retValue = false;
      var weightCode = "234567892345";
      var keyValue = 0;
      if(!this.isNull(arguments[0]) && (arguments[0].length == 13) && this.isNumber(arguments[0])) {
        for(var i = 0; i < 12; i++) keyValue += (arguments[0].charAt(i) * weightCode.charAt(i));
        if(arguments[0].charAt(12) == (11-(keyValue%11))) retValue = true;
      }
      return retValue;
    },
    
    // arguments[0] : 정규식 패턴
    // arguments[1] : 유효성 검증 대상 문자열
    // 패턴에 부합되는 경우 true를 반환한다.
    isRegexp : function() {
      return !this.isNull(arguments[0].match(arguments[1]));
    },
  
    // 객체의 유효성을 검사하여 유요한 경우 true를 반환하고, 유효하지 않은 경우 경고 메시지를 출력 후 false를 반환한다.
    //
    // 속성                            설명
    // NotSpace         공백이 사용되었으면 예외발생
    // NotNumber        숫자가 사용되었으면 예외발생
    // NotKorean        한글이 사용되었으면 예외발생
    // NotEnglish       영문이 사용되었으면 예외발생
    // NotSpecial       공백, 숫자, 한글, 영문 이외의 문자가 사용되었으면 예외발생
    // Space            공백을 사용할 수 있음
    // Number           숫자를 사용할 수 있음
    // Korean           한글을 사용할 수 있음
    // English          영문을 사용할 수 있음
    // Special          특수문자를 사용할 수 있음
    run : function() {
      this.status = false;
      if(event.keyCode != Key.BACKSPACE && 
	       event.keyCode != Key.TAB && 
         event.keyCode != Key.ENTER && 
	       event.keyCode != Key.SHIFT && 
         event.keyCode != Key.LCTRL && 
	       event.keyCode != Key.LALT && 
         event.keyCode != Key.RALT && 
	       event.keyCode != Key.RCTRL && 
         event.keyCode != Key.ESC && 
	       event.keyCode != Key.PAGEUP && 
         event.keyCode != Key.PAGEDOWN && 
	       event.keyCode != Key.END && 
         event.keyCode != Key.HOME && 
         event.keyCode != Key.LEFT && 
         event.keyCode != Key.UP && 
         event.keyCode != Key.RIGHT && 
         event.keyCode != Key.DOWN && 
         event.keyCode != Key.INSERT && 
         event.keyCode != Key.DELETE
      ) {
        var title = arguments[0].title;
        var value = arguments[0].value;
        var replaceValue = null;
        try {
          var notSpace = !this.isNull(arguments[1].match(/notSpace/));
          var notNumber = !this.isNull(arguments[1].match(/notNumber/));
          var notKorean = !this.isNull(arguments[1].match(/notKorean/));
          var notEnglish = !this.isNull(arguments[1].match(/notEnglish/));
          var notSpecial = !this.isNull(arguments[1].match(/notSpecial/));
          var space = !this.isNull(arguments[1].match(/space/));
          var number = !this.isNull(arguments[1].match(/number/));
          var korean = !this.isNull(arguments[1].match(/korean/));
          var english = !this.isNull(arguments[1].match(/english/));
          var special = !this.isNull(arguments[1].match(/special/));
          if(notSpace && this.hasSpace(value)) {
            replaceValue = this.removeSpace(value);
            throw Exception("경고",Printf(Message.validation.NotSpace,title));
          }
          if(notNumber && this.hasNumber(value)) {
            replaceValue = this.removeNumber(value);
            throw Exception("경고",Printf(Message.validation.NotNumber,title));
          }
          if(notKorean && this.hasKorean(value)) {
            replaceValue = this.removeKorean(value);
            throw Exception("경고",Printf(Message.validation.NotKorean,title));
          }
          if(notEnglish && this.hasEnglish(value)) {
            replaceValue = this.removeEnglish(value);
            throw Exception("경고",Printf(Message.validation.NotEnglish,title));
          }
          if(notSpecial && this.hasSpecial(value)) {
            replaceValue = this.removeSpecial(value);
            throw Exception("경고",Printf(Message.validation.NotSpecial,title));
          }
          if(space || number || korean || english || special) {
            var specialRegExp = "";
            var regExp = "";
            var message = "";
            if(space) {
              regExp += "\\s";
              message += "공백";
            }
            if(number) {
              regExp += "\\d";
              if(message.length > 0) message += ", ";
              message += "숫자";
            }
            if(korean) {
              regExp += "ㄱ-ㅣ가-힣";
              if(message.length > 0) message += ", ";
              message += "한글";
            }
            if(english) {
              regExp += "a-zA-Z";
              if(message.length > 0) message += ", ";
              message += "영문";
            }
            if(special) {
              if(message.length > 0) message += ", ";
              message += "특수문자";
              if(!this.isNull(value.replace(/[^\s\dㄱ-ㅣ가-힣a-zA-Z]+/g,"").match(eval("/[^"+regExp+"]+/g"))) || 
                !this.isSpecial(value.replace(eval("/["+regExp+"]+/g"),""))
              ) {
                replaceValue = value.replace(eval("/["+(space?"":"\\s")+(number?"":"\\d")+(korean?"":"ㄱ-ㅣ가-힣")+(english?"":"a-zA-Z")+"]+/g"),"");
                throw Exception("경고",Printf(Message.validation.Other,title,message));
              }
            } else {
              if(!this.isNull(value.match(eval("/[^"+regExp+"]+/g")))) {
                replaceValue = value.replace(eval("/[^"+regExp+"]+/g"),"");
                throw Exception("경고",Printf(Message.validation.Other,title,message));
              }
            }
          }
        } catch(e) {
          alert("\n"+e.message);
          if(replaceValue != null) arguments[0].value = replaceValue;
//          if(typeof arguments[0].focus != Type.Undefined) arguments[0].focus();
//          if(typeof arguments[0].select != Type.Undefined) arguments[0].select();
          this.status = true;
          return false;
        }
      }
      this.status = true;
      return true;
    },

    // 필수항목 체크
    require : function() {
      var title = arguments[0].title;
      var value = arguments[0].value;
      try {
        if(this.status && this.isNull(value)) {
          throw Exception("경고",Printf(Message.validation.Require,title));
        }
      } catch(e) {
        alert("\n"+e.message);
//        if(typeof arguments[0].focus != Type.Undefined) arguments[0].focus();
//        if(typeof arguments[0].select != Type.Undefined) arguments[0].select();
        return false;
      }
      return true;
    },

    rrn : function() {
      var title = arguments[0].title;
      var value = arguments[0].value;
      try {
        if(this.status && !this.isNull(value) && !this.isRRN(value)) {
          throw Exception("경고",Printf(Message.validation.RRN,title));
        }
      } catch(e) {
        alert("\n"+e.message);
//        if(typeof arguments[0].focus != Type.Undefined) arguments[0].focus();
//        if(typeof arguments[0].select != Type.Undefined) arguments[0].select();
        return false;
      }
      return true;
    },

    // 해당 정규식 표현에 부합되지 않은 입력일 경우 예외발생
    regexp : function() {
      var title = arguments[0].title;
      var value = arguments[0].value;
      try {
        if(this.status && !this.isRegexp(value,arguments[1])) {
          throw Exception("경고",Printf(Message.validation.Regexp,title));
        }
      } catch(e) {
        alert("\n"+e.message);
//        if(typeof arguments[0].focus != Type.Undefined) arguments[0].focus();
//        if(typeof arguments[0].select != Type.Undefined) arguments[0].select();
        return false;
      }
      return true;
    },

    // argument[1] 최소 길이
    // arguemtn[2] 최대 길이
    // 입력문자 길이를 설정, argument[0] 문자열의 길이가 arguments[1] 값 이상, argument[2] 값 이하의 범위에 포함되지 않으면 예외발생
    // 최대 길이 값이 생략될 경우 무한대로 인정한다.
    length : function() {
      var title = arguments[0].title;
      var value = arguments[0].value;
      try {
        if(this.status) {
          switch(arguments.length) {
          case 2 :
            if(this.isNumber(arguments[1]) && parseInt(arguments[1],10) > 0) {
              if(!this.isNull(value) && value.length < parseInt(arguments[1],10)) throw Exception("경고",Printf(Message.validation.MinLength,title,arguments[1]));
            } else {
              throw Exception("경고",Printf(Message.validation.MinLengthRangeError,title));
            }
            break;
          case 3 :
            if(this.isNumber(arguments[1]) && parseInt(arguments[1],10) > 0 && this.isNumber(arguments[2]) && parseInt(arguments[2],10) >= parseInt(arguments[1],10)) {
              if(parseInt(arguments[2],10) == parseInt(arguments[1],10)) {
                if(!this.isNull(value) && value.length < parseInt(arguments[1],10)) throw Exception("경고",Printf(Message.validation.EquLength,title,arguments[1]));
              } else {
                if(!this.isNull(value) && value.length < parseInt(arguments[1],10)) throw Exception("경고",Printf(Message.validation.Length,title,arguments[1],arguments[2]));
              }
            } else {
              throw Exception("경고",Printf(Message.validation.LengthRangeError,title));
            }
            break;
          }
        }
      } catch(e) {
        alert("\n"+e.message);
//        if(typeof arguments[0].focus != Type.Undefined) arguments[0].focus();
//        if(typeof arguments[0].select != Type.Undefined) arguments[0].select();
        return false;
      }
      return true;
    },

    // 선행분자가 일치하지 않을 경우 예외발생
    prefix : function() {
      var title = arguments[0].title;
      var value = arguments[0].value;
      try {
        var regExp = eval("/^"+arguments[1]+"/g");
        if(this.status && !this.isNull(value) && this.isNull(value.match(regExp))) {
          throw Exception("경고",Printf(Message.validation.Prefix,title,arguments[1]));
        }
      } catch(e) {
        alert("\n"+e.message);
//        if(typeof arguments[0].focus != Type.Undefined) arguments[0].focus();
//        if(typeof arguments[0].select != Type.Undefined) arguments[0].select();
        return false;
      }
      return true;
    },
    
    // 문자열 끝이 일치하지 않을 경우 예외 발생
    suffix : function() {
      var title = arguments[0].title;
      var value = arguments[0].value;
      try {
        var regExp = eval("/"+arguments[1]+"$/g");
        if(this.status && !this.isNull(value) && this.isNull(value.match(regExp))) {
          throw Exception("경고",Printf(Message.validation.Suffix,title,arguments[1]));
        }
      } catch(e) {
        alert("\n"+e.message);
//        if(typeof arguments[0].focus != Type.Undefined) arguments[0].focus();
//        if(typeof arguments[0].select != Type.Undefined) arguments[0].select();
        return false;
      }
      return true;
    },

    // < > " ' 문자를 유니코드로 변경
    replaceTag2Uni : function() {
      arguments[0].value = arguments[0].value.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&#34;").replace(/\'/g,"&#39;");
    },
    
    status : true
  }
  
  // 브라우저 정보
  var Browser = {
    isMsie      : !Validate.isNull(navigator.userAgent.match(/MSIE/gi)),
    isMozes     : !Validate.isNull(navigator.userAgent.match(/Gecko/gi)),
    isOpera     : !Validate.isNull(navigator.userAgent.match(/Opera/gi)),
    isKhtml     : !Validate.isNull(navigator.userAgent.match(/KHTML/gi)),
    isSafari    : !Validate.isNull(navigator.userAgent.match(/AppleWebKit/gi)),
    isFirefox   : !Validate.isNull(navigator.userAgent.match(/Firefox/gi)),
    isNetscape  : !Validate.isNull(navigator.userAgent.match(/Netscape/gi)),
    name        : navigator.userAgent.match(/(MSIE)|(Opera)|(Safari)|(Firefox)|(Netscape)/gi).toString(),
    version     : navigator.userAgent.match(/(MSIE [\d\.]*)|(Opera\/[\d\.]*)|(Firefox\/[\d\.]*)|(Netscape\/[\d\.]*)/gi).toString().replace(/[^\d\.]*/gi,"")
  }