/*
 * キーボード入力のフィルタ処理を実装する。
 * $Date: 2004/10/30 06:20:09 $
 * $Revision: 1.2 $
 */
/*
 * キーボードイベントを処理する。
 */
function init() {
  var inputs = document.getElementsByTagName("p");
  for (var i = 0; i < inputs.length; i ++) {
    inputs.item(i).onkeypress=restriction;
  }
}

/*
 * テキストフィールドへの、入力制限
 */
function restriction(evnt) {
  evnt = MyEvent.createEvent(evnt);
  var isRestriction = false;
  if (evnt) {
    var targetClassValue = evnt.target.className;
    if (evnt.data < 0x20) {
      return;
    }
    if (targetClassValue.search(/restriction_Number/) != -1) {
      if (evnt.data >= 0x30 && evnt.data <= 0x39) {
        return;
      }
      else {
        isRestriction = true;
      }
    }
    if (targetClassValue.search(/restriction_Sign/) != -1) {
      if (evnt.data == 0x2D) {
        return;
      }
      else {
        isRestriction = true;
      }
    }
    if (targetClassValue.search(/restriction_UpperCase/) != -1) {
      if (evnt.data >= 0x41 && evnt.data <= 0x5A) {
        return;
      }
      else {
        isRestriction = true;
      }
    }
  }
  else {
  }
  if (isRestriction) {
    evnt.preventDefault();
    evnt.stopPropagation();
  }
}

/*
 * 独自のイベントオブジェクト コンストラクタ
 * 原則として、DOM Level3のTextEventと、同じインターフェイスを踏襲する。
 * 最低限の実装なので、
 */
function MyEvent(evnt) {
  if (evnt) {
    this.data = evnt.charCode;
    this.target = evnt.target;
  }
  else if (window.event) {
    evnt = event;
    this.data = evnt.keyCode;
    this.target = evnt.srcElement;
  }
  else {
    // throw new MyException();
  }
  this.evnt = evnt;
}

/*
 * イベントファクトリ
 */
MyEvent.createEvent = function (evnt) {
  if (evnt && evnt.initTextEvent) {
    // W3CのDOMイベントの場合は、そのまま返す。
    // 但し、現時点(2004/10)では、あり得ない。
    return evnt;
  }
  return new MyEvent(evnt);
}

/*
 * 最低限必要なメソッドのみ、実装する。
 */
MyEvent.prototype.preventDefault = function () {
  if (this.evnt.preventDefault) {
    this.evnt.preventDefault();
  }
  else {
    this.evnt.cancelBubble = true;
  }
}
MyEvent.prototype.stopPropagation = function () {
  if (this.evnt.stopPropagation) {
    this.evnt.stopPropagation();
  }
  else {
    this.evnt.returnValue = false;
  }
}

