/*
*
* FORM input varidate javascripts
*
*/

/*** CHECK EVENT ***/
var EFM_VDTEVT_REAL = 1;
var EFM_VDTEVT_AFTER = 2;
var EFM_VDTEVT_ALL = EFM_VDTEVT_REAL | EFM_VDTEVT_AFTER;

/*** CHECK TYPE ***/
var EFM_VDT_NEED     = 1;
var EFM_VDT_EMAIL    = 2;
var EFM_VDT_NUMBER   = 4;
var EFM_VDT_TELNO    = 8;
var EFM_VDT_SAME     = 16;
var EFM_VDT_HALFCHAR = 32;
var EFM_VDT_STRLEN   = 64;
var EFM_VDT_RADIOCHK = 128;
var EFM_VDT_URL      = 256;

function initIVEvents() {
 var ios = null;
 if (document.all) {
  ios = document.all.tags('input');
 }
 else if (document.getElementsByTagName) {
  ios = document.getElementsByTagName('input');
 }
 if (ios != null) {
  for (var i = 0; i < ios.length; i++) {
   if (ios[i].className.match(/efmchk/i)) {
    Event.observe(ios[i], "focus", eOnEvent);
    Event.observe(ios[i], "keyup", eOnEvent);
    Event.observe(ios[i], "blur", eOnEvent);
   }
  }
 }
 var sos = null;
 if (document.all) {
  sos = document.all.tags('select');
 }
 else if (document.getElementsByTagName) {
  sos = document.getElementsByTagName('select');
 }
 if (sos != null) {
  for (var i = 0; i < sos.length; i++) {
   if (sos[i].className.match(/efmchk/i)) {
    Event.observe(sos[i], "focus", eOnEvent);
    // Event.observe(sos[i], "keyup", eOnEvent);
    Event.observe(sos[i], "change", eOnEvent);
    Event.observe(sos[i], "blur", eOnEvent);
   }
  }
 }

}

function setErrormsg(myid, msg, enm) {
 var divmsg = $(myid + "-msg");
 var divbak = $(myid + "-base");
 if (divmsg != undefined) {
  divmsg.innerHTML = msg;
  if (enm == 0) {
   divmsg.hide();
  }
  else {
   divmsg.show();
  }
 }
 if (divbak != undefined) {
  if (enm == 0) {
   divbak.style.background = "#ffffff";
  }
  else {
   divbak.style.background = "#fff0d4";
  }
 }
}

function axForm() {
 this.ids = null;
 
 /*** add Input ids ***/
 this.addInput = function (iid) {
  if (this.ids == null) {
   this.ids = new Array();
  }
  var _num = this.ids.length;
  this.ids[_num] = $(iid);
 }
 
 /*** Check all ***/
 this.varidate = function () {
  var _err = 0;
  if (this.ids == null) {
   return _err;
  }
  for (var i = 0; i < this.ids.length; i++) {
   if (this.ids[i].className.match(/efmchk/i)) {
    var chker = new efmChecker(this.ids[i].id);
    chker.init();
    chker.varidate(EFM_VDTEVT_ALL);
    _err += chker.err_num;
   }
  }
  return _err;
 }
 
 /*** POST DATA ***/
 this.getPostString = function () {
  if (this.ids == null) {
   return "";
  }
  var _num = this.ids.length;
  var _str = "";
  for (var i = 0; i < _num; i++) {
   if (_str != "") {
    _str += "&";
   }
   _str += this.ids[i].id;
   _str += "=";
   _str += encodeURIComponent($F(this.ids[i].id));
  }
  return _str;
 }
 
}

function eOnEvent(event) {
 var elm = Event.element(event);
 var chker = new efmChecker(elm.id);
 chker.init();
 var _evt = 0;
 if (event.type == "focus" || event.type == "keyup" || event.type == "change") {
  _evt = EFM_VDTEVT_REAL;
 }
 else if (event.type == "blur") {
  _evt = EFM_VDTEVT_ALL;
 }
 chker.varidate(_evt);
}

function efmChecker(myid) {
 /*** efm form cheker ***/
 this.myid = myid;
 this.baseid = "";
 this.chks = null;
 this.err_num = 0;
 this.res_msg = "";
 
 /*** init ***/
 this.init = function () {
  this.myid = myid;
  if ($(this.myid).className == null) {
   return;
  }
  var cary = $w($(this.myid).className);
  if (cary != null) {
   for (var i = 0; i < cary.length; i++) {
    this.setChecks(cary[i]);
   }
  }
 }
 /*** add param ***/
 this.setChecks = function (ctext) {
  var _cevt = 0;
  if (ctext.match(/^efmchkreal/i)) {
   _cevt = EFM_VDTEVT_REAL;
  }
  else if (ctext.match(/^efmchkafter/i)) {
   _cevt = EFM_VDTEVT_AFTER;
  }
  else if (ctext.match(/^efmbase/i)) {
   _baseid = ctext.replace(/efmbase:/i, "");
   this.baseid = _baseid;
   return;
  }
  
  /*** parse ***/
  if (ctext.match(/:need:/i)) {
   this.addPattern(_cevt, EFM_VDT_NEED, "");
  }
  if (ctext.match(/:email:/i)) {
   this.addPattern(_cevt, EFM_VDT_EMAIL, "");
  }
  if (ctext.match(/:number:/i)) {
   this.addPattern(_cevt, EFM_VDT_NUMBER, "");
  }
  if (ctext.match(/:telno:/i)) {
   this.addPattern(_cevt, EFM_VDT_TELNO, "");
  }
  if (ctext.match(/:same@/i)) {
   var _sameid = ctext.replace(/.*?same@/i, "");
   _sameid = _sameid.replace(/:.*?$/i, "");
   this.addPattern(_cevt, EFM_VDT_SAME, _sameid);
  }
  if (ctext.match(/:half:/i)) {
   this.addPattern(_cevt, EFM_VDT_HALFCHAR, "");
  }
  if (ctext.match(/:len@/i)) {
   var _lenstr = ctext.replace(/.*?len@/i, "");
   _lenstr = _lenstr.replace(/:.*?$/i, "");
   this.addPattern(_cevt, EFM_VDT_STRLEN, _lenstr);
  }
  if (ctext.match(/:radiochecked@/i)) {
   var _radiostr = ctext.replace(/.*?radiochecked@/i, "");
   _radiostr = _radiostr.replace(/:.*?$/i, "");
   this.addPattern(_cevt, EFM_VDT_RADIOCHK, _radiostr);
  }
  if (ctext.match(/:url:/i)) {
   this.addPattern(_cevt, EFM_VDT_URL, "");
  }
 }
 
 /*** add check param ***/
 this.addPattern = function (evt, pid, param) {
  if (this.chks == null) {
   this.chks = Array();
  }
  var _num = this.chks.length;
  this.chks[_num] = Array();
  this.chks[_num][0] = evt;
  this.chks[_num][1] = pid;
  this.chks[_num][2] = param;
 }
 
 /*** check ***/
 this.varidate = function (vdevt) {
  var _myval = $F(this.myid);
  if (this.chks != null) {
   var _num = this.chks.length;
   for (var n = 0; n < _num; n++) {
    if (this.chks[n][0] & vdevt) {
     if (this.chks[n][1] == EFM_VDT_NEED) {
      if (_myval == "") {
       this.addErrmsg("必ず入力してください。");
      }
     }
     if (this.chks[n][1] == EFM_VDT_EMAIL) {
      var reg_email=/[!#-9A-~]+@+[a-z0-9]+.+[^.]$/i;
      if (!_myval.match(reg_email)) {
       this.addErrmsg("メールアドレスを入力してください。");
      }
     }
     if (this.chks[n][1] == EFM_VDT_NUMBER) {
      for (var i = 0; i < _myval.length; i++) {
       var scode=_myval.charCodeAt(i);
       if ((48 > scode || scode > 57)) {
        this.addErrmsg("数字のみ入力可能です。");
        break;
       }
      }
     }
     if (this.chks[n][1] == EFM_VDT_TELNO) {
     }
     if (this.chks[n][1] == EFM_VDT_SAME) {
      var _sval = $F(this.chks[n][2]);
      if (_sval != "") {
       if (_sval != _myval) {
        this.addErrmsg("上の入力値と一致しませんでした。ご確認ください。");
       }
      }
     }
     if (this.chks[n][1] == EFM_VDT_HALFCHAR) {
      var _cstr = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      for (var i = 0; i < _myval.length; i++) {
       if (_cstr.indexOf(_myval.charAt(i)) == -1) {
        this.addErrmsg("半角英数字のみ入力可能です。");
        break;
       }
      }
     }
     if (this.chks[n][1] == EFM_VDT_STRLEN) {
      if (_myval	.length > 0) {
       var _slen = _myval.length;
       var _mm = this.chks[n][2].split(",");
       if (_mm[0] > 0) {
        if (_slen < _mm[0]) {
         this.addErrmsg(_mm[0] + "文字以上入力してください。");
        }
       }
       if (_mm[1] > 0) {
        if (_slen > _mm[1]) {
         this.addErrmsg(_mm[1] + "文字以内で入力してください。");
        }
       }
      }
     }
     if (this.chks[n][1] == EFM_VDT_RADIOCHK) {
      var _mm = this.chks[n][2].split(".");
      if (_mm.length == 2) {
       var _cs = Form.getInputs(_mm[0], 'radio', _mm[1]);
       var _checked = 0;
       if (_cs != null) {
        for(var i = 0; i < _cs.length; i++) {
         if (_cs[i].checked == true) {
          var _checked = 1;
         }
        }
        if (_checked == 0) {
         this.addErrmsg("該当する項目を選択してください。");
        }
       }
      }
     }
     if (this.chks[n][1] == EFM_VDT_URL) {
      var reg_url=/(http|ftp):\/\/.+/;
      if (!_myval.match(reg_url)) {
       this.addErrmsg("http://からのURLを入力してください。");
      }
     }
    }
   }
  }
  
  /*** auto update msgs (this code should change for adjusting your site. ***/
  if (this.chks != null) {
   setErrormsg(this.myid, this.res_msg, this.err_num);
   if (this.baseid != "") {
    setErrormsg(this.baseid, this.res_msg, this.err_num);
   }
   /*
   var divmsg = $(this.myid + "-msg");
   var divbak = $(this.myid + "-base");
   if (divmsg != undefined) {
    divmsg.innerHTML = this.res_msg;
    if (this.err_num == 0) {
     divmsg.hide();
    }
    else {
     divmsg.show();
    }
   }
   if (divbak != undefined) {
    if (this.err_num == 0) {
     divbak.style.background = "#ffffff";
    }
    else {
     divbak.style.background = "#fff0d4";
    }
   }
   */
  }
  /*** auto update msgs (this code should change for adjusting your site. ***/
 
 }
 
 /*** add msg ***/
 this.addErrmsg = function (msg) {
  if (this.res_msg != "") {
   this.res_msg += "<br/>";
  }
  this.res_msg += msg;
  this.err_num++;
 }
 
 
}



