//
// required prototype.js
//

if (typeof(iads) == 'undefined') {
  iads = {};

  // form submit
  iads.formSubmit = function(formId, url){
    var fm = $(formId);
    if (fm == null) return false;

    if (url != null) {
      fm.action = url;
    }

    if (typeof(preFormSubmit) != 'undefined')
    {
      preFormSubmit(formId);
    }

    fm.submit();

    //ignore <A href=xxxx>
    return false;
  };

  //pnkz submit
  iads.pnkzSubmit = function(pnkzFormId, url, mainVsId, pnkzVsId, readId, readIdx){
    var pnkz = $(pnkzFormId);
    if (pnkz == null) return false;

    var mainVsId = $(mainVsId);
    if (mainVsId == null) return false;

    var pnkzVsId = $(pnkzVsId);
    if (pnkzVsId == null) return false;

    var readId = $(readId);
    if (readId == null) return false;

    pnkzVsId.value = mainVsId.value;
    readId.value = readIdx;

    return iads.formSubmit(pnkzFormId, url);
  };

  //pnkz submit with del
  iads.pnkzDelSubmit = function(pnkzFormId, url, mainVsId, pnkzVsId, delId, delIdx){
    var pnkz = $(pnkzFormId);
    if (pnkz == null) return false;

    var mainVsId = $(mainVsId);
    if (mainVsId == null) return false;

    var pnkzVsId = $(pnkzVsId);
    if (pnkzVsId == null) return false;

    var delId = $(delId);
    if (delId == null) return false;

    pnkzVsId.value = mainVsId.value;
    delId.value = delIdx;

    return iads.formSubmit(pnkzFormId, url);
  };

  // validation and form submit
  iads.validateSubmit = function(formId, url, valid) {
    iads.topmessage.clear();
    if (valid == null || valid.validate()) {
      return iads.formSubmit(formId, url);
    } else {
      iads.topmessage.addErrorMessage('入力内容に誤りがあります。');
      window.scrollTo(0,0);
      return false;
    }
  };

  // Execute niceforms NFFix function
  iads.nfFix = function() {
    // NFFix declared check
    if (typeof(NFFix) != 'undefined') {
      NFFix();
    }
  };

  iads.keyDownSubmit = function(e) {
    e = e || window.event;

    switch (e.keyCode) {
      case Event.KEY_RETURN:
        var t = window.document.activeElement || e.srcElement || e.target;
        if (t == undefined) return false;

        if ((t.tagName == 'A') || (t.tagName == 'TEXTAREA') || (t.tagName == 'BUTTON')) return true;

        switch (t.type) {
          case 'image':
          case 'button':
          case 'submit':
          case 'reset':
          case 'select-one':
            return true;
        }

        var noteObj = document.getElementsByClassName('defbutton');
        if (noteObj.length != 0 &&
            (noteObj[0].tagName == 'BUTTON' || (noteObj[0].tagName == 'INPUT' && noteObj[0].type == 'button')))
        {
          try {
            e.keyCode = null;
          }
          catch (ex) {}

          noteObj[0].click();

          return false;
        }
    }
    return true;
  }

  iads.focusDefbutton = function() {
    var noteObj = document.getElementsByClassName('defbutton');
    if (noteObj.length != 0 &&
        (noteObj[0].tagName == 'BUTTON' || (noteObj[0].tagName == 'INPUT' && noteObj[0].type == 'button')))
    {
      noteObj[0].focus();
    }
  }

  // top message utils
  if (typeof(iads.topmessage) == 'undefined') {
    iads.topmessage = {};

    iads.topmessage.clear = function() {
      var tm = $('topMessage');
      if (tm == null) return;
      tm.innerHTML = '';
      iads.nfFix();
    };

    iads.topmessage.addInfoMessage = function(m) {
      var tm = $('topMessage');
      if (tm == null) return;
      var msg = '<div class="msgInfo">' + m + '</div>';
      new Insertion.Bottom(tm, msg);
      iads.nfFix();
    };

    iads.topmessage.addWarnMessage = function(m) {
      var tm = $('topMessage');
      if (tm == null) return;
      var msg = '<div class="msgWarn">' + m + '</div>';
      new Insertion.Bottom(tm, msg);
      iads.nfFix();
    };

    iads.topmessage.addErrorMessage = function(m) {
      var tm = $('topMessage');
      if (tm == null) return;
      var msg = '<div class="msgErr">' + m + '</div>';
      new Insertion.Bottom(tm, msg);
      iads.nfFix();
    };
  }

  // utils
  if (typeof(iads.util) == 'undefined') {
    iads.util = {};

    iads.util.addComma = function (value){
      value = new String(value);
      var i;
      for(i = 0; i < value.length/3; i++){
          value = value.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2");
      }
      return value;
    };

    iads.util.removeComma = function (value) {
      return value.split(",").join("")
    };
  }
}
