var LastSubmit;

var AQ = {};

/* Ajax アラート */
AQ.Alert = {};

AQ.Alert.Ajax = function(form, func) {
   if(!document.getElementById) return true;

   var data = new Array;
   for (var i = 0; i < form.elements.length; i++) {
      var elm = form.elements[i];
      if((elm.type != 'submit' && elm.type != 'radio' && elm.type != 'checkbox') || (elm.checked == true))
      data[elm.name] = elm.value;
   }

   if(LastSubmit) {
      data[LastSubmit] = 1;
      LastSubmit = '';
   }

   var xmlhttp = new AJAX.init();
   xmlhttp.load = new func(xmlhttp);
   return (xmlhttp.post(data)) ? true : false;
}

AQ.Alert.Back = function(link, hg) {
   if(!document.getElementById) return true;

   this.hg = hg;
   //AQ.Effect.hide('alert');

   var xmlhttp = new AJAX.init();
   xmlhttp.url = link.href;
   xmlhttp.load = new this._Show(xmlhttp);
   xmlhttp.get();

   return false;
}

AQ.Alert.Cancel = function() {
   if(!document.getElementById) return true;
   AQ.Effect.close('alert');
   AQ.Effect.baloon.Allclose();
   return false;
}

AQ.Alert._Show = function(xmlhttp) {
   return function() {
      var response = xmlhttp.getResponse();
      if(!response) return;
      var pos = response.indexOf("<async>");
      if(pos >= 0) {
         var end = response.indexOf("</async>");
         response = response.substr(pos+7, end-(pos+7));
         $('alertbody').innerHTML = response;
         AQ.Effect.show('alert', AQ.Alert.hg);
      } else {
         alert('処理に失敗しました。ページを読み込み直して、再度実行して下さい。');
      }
   }
}

AQ.Alert.Submit = function(form, hg) {
   this.hg = hg;
   //AQ.Effect.hide('alert');
   AQ.Effect.baloon.Allclose();
   return this.Ajax(form, this._Show);
}

AQ.Alert.CLKSubmit = function(btn, hg) {
   this.hg = hg;
   //AQ.Effect.hide('alert');
   AQ.Effect.baloon.Allclose();
   LastSubmit = btn.name;
   return this.Ajax(btn.form, this._Show);
}

AQ.Alert.Done = function(form, obj) {
   var responseFunc = function(xmlhttp) {
      return function() {
         var response = xmlhttp.getResponse();
         if(!response) return;
         var pos = response.indexOf("<done>");
         AQ.Alert.Cancel();
         if(pos >= 0) {
            var end = response.indexOf("</done>");
            response = response.substr(pos+6, end-(pos+6));
            if(response) {
               new AQ.Effect.EditLine('line_'+obj, response.split('||'));
            } else {
               new AQ.Effect.DelLine('line_'+obj);
               if($('line2_'+obj)) new AQ.Effect.DelLine('line2_'+obj);
            }
         } else {
            alert('処理に失敗しました。');
         }
      }
   }

   return this.Ajax(form, responseFunc);
}

AQ.Alert.Login = function(form, obj) {
   var responseFunc = function(xmlhttp) {
      return function() {
         var response = xmlhttp.getResponse();
         if(!response) return;
         var pos = response.indexOf("<alogin>");
         if(pos >= 0) {
            var end = response.indexOf("</alogin>");
            response = response.substr(pos+8, end-(pos+8));
            if(response.indexOf("admin.cgi") == 0) {
               location.href = response;
               return;
            }
            $('msg').innerHTML = response;
            form.login.disabled = false;
            new AQ.Effect.Swing('login');

            var high = new AQ.Effect.HighLight('msg');
            high.count = 2;
            high.speed = 800;
            high.color = ['FFDDDD', 'FFFFFF'];
            high.loop();
         } else {
            alert('処理に失敗しました。');
         }
      }
   }

   form.login.disabled = true;

   return this.Ajax(form, responseFunc);
}


/* ビジュアルエフェクト */
AQ.Effect = {};

AQ.Effect.HighLight = function(obj, func) {
   this.elm   = $(obj);
   this.color = [ 'CCCEEE', 'EEEFFF' ];
   this.func  = func;
   this._i    = 0;
   this.count = 3;
   this.speed = 150;

   this.loop = function() {
      this.elm.style.backgroundColor = '#'+this.color[this._i%2];
      this._i++;
      var _this = this;
      if(this._i >= this.count) {
         if(this.func) setTimeout(this.func, 500);
      } else {
         setTimeout(function() { _this.loop(); }, this.speed);
      }
   }
}

AQ.Effect.EditLine = function(obj, html) {
   var _high = new AQ.Effect.HighLight(obj);

   _high.func = function() {
      var list = _high.elm.getElementsByTagName('td');
      for (f in list) list[f].innerHTML = html[f];
      _high.elm.style.backgroundColor = 'FFFFFF';
   }
   _high.loop();
}

AQ.Effect.DelLine = function(obj) {
   var _high = new AQ.Effect.HighLight(obj);
   _high.color = [ 'FF9999', 'FFDDDD' ];
   _high.count = 5;
   _high.func = function() {
      _high.elm.style.display = 'none';
   }
   _high.loop();
}

AQ.Effect.show = function(obj, hg) {
   hg = hg||200;
   this.elm = $(obj);
   this.elm.style.opacity = 0;
   this.elm.style.filter  = 0;
   this.opacity = 0.2;

   var top = Util.winTop();

   this.elm.style.display = 'block';
   this.elm.style.top = hg+top+'px';

   this.loop = function() {
      if(this.opacity >= 1.0) {
         this.elm.style.opacity = null;
         this.elm.style.filter  = null;

         Colorful.setLast();
      } else {
         this.elm.style.opacity = this.opacity;
         this.elm.style.filter  = "alpha(opacity:"+this.opacity*100+")";
         this.opacity += 0.25;
         var _this = this;
         setTimeout(function() { _this.loop(); }, 22);
      }
   }

   this.loop();
}

AQ.Effect.close = function(obj, func) {
   this.elm = $(obj);
   this.func = func;
   this.opacity = 1;

   this.loop = function() {
      if(this.opacity < 0.2) {
         this.elm.style.display = 'none';
         this.elm.style.opacity = null;
         this.elm.style.filter  = null;
         if(this.func) setTimeout(this.func, 500);
      } else {
         this.elm.style.opacity = this.opacity;
         this.elm.style.filter  = "alpha(opacity:"+this.opacity*100+")";
         this.opacity -= 0.3;
         var _this = this;
         setTimeout(function() { _this.loop(); }, 8);
      }
   }

   this.loop();
}

AQ.Effect.Swing = function(obj) {
   AQ.Effect.DoSwing(obj, 30, 0.5,
      function() { AQ.Effect.DoSwing(obj, -60, 0.2,
         function() { AQ.Effect.DoSwing(obj, 60, 0.2,
            function() { AQ.Effect.DoSwing(obj, -60, 0.2,
               function() { AQ.Effect.DoSwing(obj, 30, 0.2) }
            ) }
         ) }
      ) }
   );
}

AQ.Effect.DoSwing = function(obj, left, up, last) {
   this.up      = up;
   this.elm     = $(obj);
   this.toLeft  = left;
   this.pos     = 0;
   this.last    = last;

   this.originalLeft       = parseFloat((this.elm.style.left) || '0');
   this.elm.style.position = "relative";

   this.loop = function() {
      if(this.pos >= 1) {
         if(this.last) this.last();
      } else {
         var left = this.toLeft * Math.sin(this.pos) + this.originalLeft;
         this.elm.style.left = left + "px";
         this.pos += this.up;
         var _this = this;
         setTimeout(function() { _this.loop(); }, 40);
      }
   }

   this.loop();
}

AQ.Effect.hide = function(obj) {
   elm = $(obj);
   elm.style.display = 'none';
}

// バルーンエラー
AQ.Effect.baloonIndex = 1;

AQ.Effect.baloon = function(form, msg) {
   var top  = Util.aOffsetTop(form)  - 25;
   var left = Util.aOffsetLeft(form) - 20 + form.offsetWidth;
   var id   = 'baloon_'+form.name;
   var elm;

   if(elm = $(id)) {
      elm = $(id);
   } else {
      elm = document.createElement('span');
      elm.className = 'baloon';
      elm.setAttribute('id', id);
      AQ.Effect.baloon.ls[AQ.Effect.baloon.ls.length] = this;
   }

   elm.innerHTML = '';
   elm.style.top  = top +'px';
   elm.style.left = left+'px';

   Util.addEvent(elm, 'click', function() { _this.ppindex(); });

   var _this = this;
   var func = function() { _this.close(); this.onfocus = ''; };
   var link = Util.createLink('×', func);
   form.onfocus = func;

   var image = document.createElement('img');
   image.src = 'img_common/baloon_right.gif';
   image.setAttribute('height', '34');
   image.setAttribute('width',  '10');
   image.setAttribute('align',  'middle');

   elm.appendChild(link);
   elm.appendChild(document.createTextNode(msg));
   elm.appendChild(image);

   document.body.appendChild(elm);
   this.elm = elm;
   this.ppindex();

   if(form.type != 'radio') { 
      var cFunc1 = function() { form.style.backgroundColor = '#FF6666'; }
      var cFunc2 = function() { form.style.backgroundColor = '#FFAAAA'; }
      window.setTimeout(cFunc1, 1);
      window.setTimeout(cFunc2, 80);
      window.setTimeout(cFunc1, 150);
      window.setTimeout(cFunc2, 300);
   }
}

AQ.Effect.baloon.prototype.close = function() {
   this.elm.style.top = -100+'px';
}

AQ.Effect.baloon.prototype.ppindex = function() {
   AQ.Effect.baloonIndex ++;
   this.elm.style.zIndex = AQ.Effect.baloonIndex;
}

AQ.Effect.baloon.ls = new Array();
AQ.Effect.baloon.Allclose = function() {
   for(var i in this.ls)
      this.ls[i].close();
}
AQ.Effect.baloon.Allcheck = function(btns) {
   for(var i in this.ls)
      if(parseInt(this.ls[i].elm.style.top) > 0) {
         for(var f in btns)
            new AQ.Effect.baloon(btns[f], '入力エラーがあります');
         return true;
      }
   return false;
}


/* 入力チェック */
AQ.Check = function(form, m, c, extra) {
   this.f = form;
   this.v = form.value;
   this.c = c;
   this.extra = extra;

   var response;

   if(!m||!m.match(/^!/)) response = this.unval();
   if(m) {
      if(!response && (!m.match(/^!/) || this.v != '')) response = this.count();
      if(!response && this.v) {
         m = m.replace(/^!/, '');
         response = this[m]();
      }
   }

   if(response)
   return new AQ.Effect.baloon(form, response);
}

AQ.Check.prototype.count = function() {
   if(this.c && this.c.constructor == Array) {
      if(this.c[0] == 0 && this.v.length > this.c[1])
         return this.c[1]+'文字以上入力する事は出来ません。';
      else if(this.c[1] == 0 && this.v.length < this.c[0])
         return this.c[0]+'文字以上入力する必要があります。';
      else if(this.c[0] > 0 && this.c[1] > 0 && (this.v.length < this.c[0] || this.v.length > this.c[1]))
         return this.c[0]+'から' + this.c[1]+'文字の間で入力して下さい。';
   }
}

AQ.Check.prototype.radio = function() {
   return '選択が必要です。';
}

AQ.Check.prototype.unval = function() {
   if(this.v == '') {
      return (this.f.type == 'select-one') ? '選択が必要です。' : '入力が必要です。';
   }
}

AQ.Check.prototype.password = function() {
   if(this.f.form[this.extra].value && this.v != this.f.form[this.extra].value) {
      return '入力が揃っていません。';
   }
}

AQ.Check.prototype.mail = function() {
   if(!this.v.match(/^[\x01-\x7F]+@((([-a-z0-9]+\.)*[a-z]+)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))$/)) {
      return 'メールアドレスの形式が正しくありません。';
   }
}

AQ.Check.prototype.numeric = function() {
   if(!this.v.match(/^[\d\.０１２３４５６７８９]+$/)) {
      return '数値で入力してください。';
   }
}

AQ.Check.prototype.kana = function() {
   for(var i = 0;i < this.v.length;i++){
      if(this.v.charAt(i) == ' ' || this.v.charAt(i) == '　') continue;
      if(this.v.charAt(i) < 'ァ' || this.v.charAt(i) > 'ヶ'){
         return '全角カタカナで入力してください。';
      }
   }
}

AQ.Check.prototype.lh_id = function() {
   if(!this.v.match(/^[a-zA-Z\-\d]+$/)) {
      return 'アルファベット、数字、- 以外は入力出来ません。';
   }
}

AQ.CheckAll = function(form, rdo) {
   AQ.Effect.baloon.Allclose();

   var btns = new Array;

   var i;
   for(i = 0; i < form.length; i++) {
      var elm = form[i];
      if(elm.onblur)
         elm.onblur();
      if(elm.type == 'submit')
         btns.push(elm);
   }

   // ラジオボタンに baloon 貼り付け
   for(i in rdo) {
      var flag = 0;
      var elm  = form[rdo[i]];
      for(var f = 0;f < elm.length;f++) {
         if(elm[f].checked) {
            flag = 1;
            break;
         }
      }
      if(!flag) {
         var baloon = new AQ.Check(elm[0], 'radio');
         for(var f = 0;f < elm.length;f++) {
            _closeBaloon(elm[f], baloon);
         }
      }
   }

   // 専用関数
   function _closeBaloon(elm, bln) {
      var func = function() { bln.close(); this.onfocus = ''; };
      elm.onfocus = func;
   }

   // 戻り値のチェック
   return (AQ.Effect.baloon.Allcheck(btns)) ? false : true;
}



