var checkItems  = new Object();
var registItems = new Object();
var error = new Object();

/*img1 = new Image();
img1.src = "/admin/images/balloon/input_right_ok.png";
img2 = new Image();
img2.src = "/admin/images/balloon/input_right_ng.png";*/

validate = Class.create();
validate.prototype = {
  initialize: function(category,value1,value2,value3,value4) {
    After = new requestAfter();
    switch(category){
      case 'check'    : this.validateCheck(value1);                      break;
      case 'delete'   : this.deleteValue(value1);                        break;
      case 'confirm'  : this.confirmCheck(value1,value2);                break;
      case 'show'     : this.toggleChange(value1,value2,true);           break;
      case 'hide'     : this.toggleChange(value1,value2,false);          break;
      case 'search'   : this.searchCheck(value1,value2);                 break;
      case 'location' : this.locationCheck(value1,value2,value3);        break;
      case 'checkbox' : this.boxCheck(value1,value2,value3,value4);      break;
      case 'checkboxT': this.boxCheckTable(value1);                      break;
      case 'fromto'   : this.fromToCheck(value1,value2);                 break;
      default: break;
    }
  },
  setElement: function(element) {
    e = element.name;
    chk = element.parentNode.nextSibling;
    if(chk.nodeType != 1) chk = chk.nextSibling;
    switch(element.type){
      case 'text'    :
      case 'textarea':
      case 'password': this.setBalloon(element); return element; break;
      case 'radio'   : After.insertOK(chk,e);                    break;
      default        : After.insertOK(chk,e);                    break;
    }
  },
  setBalloon: function(element){
    if(!checkItems[e]) {
      e = element.name;
      balloon = document.createElement('div');
      balloon.className = 'balloon';
      error[e] = document.createElement('p');
      chk.appendChild(balloon);
      balloon.appendChild(error[e]);
      checkItems[e] = true;
    }
  },
  validateCheck: function(element) {
    this.checkRequest(this.setElement(element));
  },
  deleteValue: function(target) {
    $(target).value = '';
    del = $(target).parentNode.nextSibling;
    if(del.nodeType != 1) del = del.nextSibling;
    if(del.className != 'input_right')
      After.insertNG(chk,e);
  },
  confirmCheck: function(element,target) {
    this.setElement(element);
    e = element.name;
    con = $(target).parentNode.nextSibling;
    if(con.nodeType != 1) con = con.nextSibling;
    if(element.value == ''){
      After.insertNG(chk,e);
      error[e].className = 'error_null';
    }else if(con.className != 'input_right_ok' && element.value != $F(target)){
      After.insertNG(chk,e);
      error[e].className = 'error_style';
    }else if(con.className != 'input_right_ok' || element.value != $F(target)){
      After.insertNG(chk,e);
      error[e].className = 'error_format';
    }else{
      After.insertOK(chk,e);
      error[e].className = '';
    }
  },
  searchCheck: function(element,target) {
    this.checkRequest(this.setElement(element),target);
  },
  toggleChange: function(element,value2,flag) {
    var count = $('formCheckCount').value;
    var cssName = flag == true ? "" : "hide";
    var target = value2.split(',');
      for(i=0;i<=target.length - 1;i++){
        var trElement = $(target[i]).parentNode.parentNode.parentNode;
        if(flag == false){
          if(trElement.className == ""){
            $(target[i]).name = $(target[i]).id.replace(/_./, '');
            $(target[i]).value = "";
            registItems[$(target[i]).id] = false;
            count = parseInt(count) - 1;
          }
        }else{
          if(trElement.className == "hide"){
            $(target[i]).name = $(target[i]).id;
            count = parseInt(count) + 1;
          }
        }
        trElement.className = cssName;
    }
    $('formCheckCount').value = count;
    this.validateCheck(element);
  },
  locationCheck: function(element,address,target) {
    this.setElement(element);
    add = $(address).parentNode.nextSibling;
    if(add.nodeType != 1) add = add.nextSibling;
    if(add.className == 'input_right_ok'){
      allAddress = $F(address) + ',' + element.value;
      regist = target;
      var AjaxRequest = new Ajax.Request('/ajax_validate',{
        method: 'post', parameters: element.name + '=' + allAddress, onSuccess: this.success, onFailure: this.failure } );
    }else{
      element.value = "";
      After.insertNG(chk,e);
    }
  },
  boxCheck: function(element,min,max,cnt) {
    var boxName = element.name.replace(/[[]]/,'');
    //this.setElement(element);
    if(cnt){
        var textId = element.name.replace(/[[]]/,'')+'_r';
        this.TextAreaChk(element,$(textId),cnt);
    }
    var count = document.form1.elements[element.id].length -1 ;
    var checkedCount = 0;
    e = element.id+'[]';
    chk = element.parentNode.nextSibling;
    if(chk.nodeType != 1) chk = chk.nextSibling;
    for(var i = 0; i <= count; i++){
      if(document.form1.elements[element.id][i].checked) checkedCount++;
    }
    if(checkedCount >= min && checkedCount <= max){
      After.insertOK(chk,e);
    }else{
      After.insertNG(chk,e);
    }
  },
  TextAreaChk: function(element,id,cnt) {
    this.setElement(id);
    
    if(element.checked){
        id.disabled="";
        $('formCheckCount').value = cnt;
        registItems[element.name] = true;
        After.insertNG(chk,e);
        error[e].className = 'error_null';
    }else{
        id.value = '';
        $('formCheckCount').value = cnt-2;
        id.disabled="disabled";
        registItems[element.name] = false;
        After.insertNormal(chk,e);
        error[e].className = '';
    }
  },
  boxCheckTable: function(element,min,max) {
    e = element.name;
    var boxName = element.name.replace(/[[]]/,'');
//    this.checkRequest(this.setElement(element));
    chk = element.parentNode.parentNode.parentNode.parentNode.nextSibling;
    if(chk.nodeType != 1) chk = chk.nextSibling;
    var count = document.form1.elements[element.name].length - 1;
    var checkedCount = 0;
    After.insertOK(chk,e);
  },
  fromToCheck: function(element,order) {
    this.setElement(element);
    var myValue = $F(e);
    var otherName = '';
    if (order == 1) {
        otherName = e.replace('From', 'To');
    } else if (order == 2) {
        otherName = e.replace('To', 'From');
        e = otherName;
    }
    var otherValue = $F(otherName);
    if (myValue.match(/[^0-9]/g) || otherValue.match(/[^0-9]/g)) {
        After.insertNG(chk,e);
        error[e].className = 'error_format';
        return null;
    } else if (myValue == '') {
        After.insertOK(chk,e);
        error[e].className = '';
        return null;
    }
    var res = 0;
    if (otherValue != '') {
        if (order == 1) {
            if (parseInt(myValue) > parseInt(otherValue)) res = 1;
        } else if (order == 2) {
            if (parseInt(myValue) < parseInt(otherValue)) res = 1;
        }
    }
    if (res != 1) {
        After.insertOK(chk,e);
        error[e].className = '';
    } else {
        After.insertNG(chk,e);
        error[e].className = 'error_format';
    }
/*
      var AjaxRequest = new Ajax.Request('/admin/ajax_validate',{
        method: 'post', parameters: element.name + '=' + myValue,
        otherParameters: otherName + '=' + otherValue,
        onSuccess: this.success, onFailure: this.failure } );
*/
  },
  checkRequest: function(element,target) {
    if(element){
      encode = encodeURIComponent(element.value);
      regist = target;
      var AjaxRequest = new Ajax.Request('/ajax_validate',{
        method: 'post', parameters: element.name + '=' + encode, onSuccess: this.success, onFailure: this.failure } );
    }
  },
  success: function(request) {
    var obj = eval('(' + request.responseText + ')');
    if(e != obj.key){
        e = obj.key;
        chk = $(obj.key).parentNode.nextSibling;
        if(chk.nodeType != 1) chk = chk.nextSibling;
    }
    
    if(obj.flag || obj.value == ''){
      After.insertNG(chk,e);
      switch(obj.flag){
        case -1  : error[e].className = 'error_null';  break;
        case -2  : error[e].className = 'error_style'; break;
        case -3  : error[e].className = 'error_format'; break;
        case -4  : error[e].className = ''; break;
        case -5  : error[e].className = ''; break;
        case -6  : error[e].className = ''; break;
        default  : $(regist).value = obj.flag;
                   result = $(regist).parentNode.nextSibling;
                   if(result.nodeType != 1) result = result.nextSibling;
                   error[e].className = '';
                   After.insertOK(chk,e);
                   if($(regist).name.indexOf('noval',0) == -1)
                     After.insertOK(result,$(regist).name);
        break;
      }
    }else{
      error[e].className = '';
      After.insertOK(chk,e);
    }
  },
  failure: function() {
    alert("ネットワークに接続できません");
  }
}

requestAfter = Class.create();
requestAfter.prototype = {
  initialize: function() {
    
  },
  insertOK: function(chk,e){
    chk.className = "input_right_ok";
    registItems[e] = true;
    var ck = this.registCheck(e);
  },
  insertNG: function(chk,e){
    chk.className = "input_right_ng";
    registItems[e] = false;
    this.registCheck();
  },
  insertNormal: function(elem,e){
    elem.className = "input_right";
    registItems[e] = false;
    //this.registCheck();
  },
  registCheck: function(e){
        var check = 0;
        var count = $('formCheckCount').value;
        for( var i in registItems ) {
            if(registItems[i] == true) check++;
        }

        if(check >= count){
            $('regist').disabled="";
        }else{
            $('regist').disabled="disabled";
        }
        return check;
  },
  repair: function(obj){
    var target = obj.previousSibling;
    if(target.nodeType != 1) target = target.previousSibling;
    target = target.firstChild;
    if(target.nodeType != 1) target = target.nextSibling;
      if(target.value != ''){
        registItems[target.name] = true;
        obj.className = "input_right_ok";
      }else{
        registItems[target.name] = false;
      }
  }
}

trans = Class.create();
trans.prototype = {
  initialize: function(path) {
    if(Ajax.activeRequestCount == "0"){
        document.form1.action = path;
        document.form1.submit();
    }
  }
}

reload = Class.create();
reload.prototype = {
  initialize: function() {
    After = new requestAfter();
    $$("div.input_right").each(function(obj){
      if($(obj).parentNode.parentNode.className == "hide"){
        var tagName = obj.previousSibling; 
        if(tagName.nodeType != 1) tagName = tagName.previousSibling;
        tagName = tagName.firstChild;
        if(tagName.nodeType != 1) tagName = tagName.nextSibling;
        if(tagName.value){
          $(obj).parentNode.parentNode.className = "";
          After.repair(obj);
        }
      }else{
        After.repair(obj);
      }
    });
    After.registCheck();
  }
}