/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * http://aipostyle.com/
 * 
 * Copyright(C) 2011 avanza Co.,Ltd. All rights reserved.
 * http://www.avnz.co.jp/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

dojo.provide("aipo.widget.MemberAbNormalSelectList");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.declare("aipo.widget.MemberAbNormalSelectList", [dijit._Widget, dijit._Templated], {
    widgetId:"",
    memberFromId: "",
    memberFromUrl: "",
    memberFromOptionKey: "",
    memberFromOptionValue: "",
    memberToTitle: "",
    memberToId: "",
    selfishMemberToId: "",
    buttonAddId: "",
    buttonRemoveId: "",
    buttonAddSelfishId: "",
    buttonRemoveSelfishId: "",
    initGroupName: "",
    initParentGroupName: "",
    memberLimit: 100,
    groupSelectId: "",
    parentGroupSelectId: "",
    group1SelectPreOptionKey: "",
    group1SelectPreOptionValue: "",
    group2SelectPreOptionKey: "",
    group2SelectPreOptionValue: "",
    groupSelectOptionKey: "",
    groupSelectOptionValue: "",
    //memberGroupUrl: "",
    //memberParentGroupUrl: "",
    //changeGroupUrl: "",
    //changeParentGroupUrl: "",
    post1GroupUrl: "",
    changePost2GroupUrl: "",
    changePost1GroupUrl: "",
    
//    templateString:"<div id=\"${widgetId}\" widgetId=\"${widgetId}\"><table class=\"none\"><tr><td><div id=\"memberPopupDiv\"><div class=\"outer\"><div class=\"popup\" style=\"width:500px;\"><div class=\"clearfix\"><div class=\"memberlistToTop\" >${memberToTitle}</div><div class=\"memberlistFromTop\"><select size=\"1\" style=\"width:100%\" name=\"${groupSelectId}\" id=\"${groupSelectId}\" dojoAttachEvent=\"onchange:changeGroup\"></select></div></div><div class=\"clearfix\"><div class=\"memberlistToBody\">必須<br/><select size=\"5\" multiple=\"multiple\" style=\"width:70%\" name=\"${memberToId}\" id=\"${memberToId}\"></select><input id=\"${buttonAddId}\" name=\"${buttonAddId}\" type=\"button\" class=\"button\" value=\"\uff1c\" dojoAttachEvent=\"onclick:onMemberAddClick\"><br/>任意<br/><select size=\"5\" multiple=\"multiple\" style=\"width:100%\" name=\"${memberToId}\" id=\"${memberToId}\"></select></div><div class=\"memberlistFromBody\"><select size=\"12\" multiple=\"multiple\" style=\"width:100%\" name=\"${memberFromId}\" id=\"${memberFromId}\"></select></div></div><div class=\"clearfix\"><div class=\"memberlistToBottom\"><div class=\"alignright\"><input id=\"${buttonRemoveId}\" name=\"${buttonRemoveId}\" type=\"button\" class=\"button\" value=\"\u3000\u524a\u9664\u3000\"/ dojoAttachEvent=\"onclick:onMemberRemoveClick\"></div></div><div class=\"memberlistFromBottom\"><div style=\"display: none;\" id=\"${widgetId}-memberlist-indicator\" class=\"indicator alignleft\">読み込み中</div><div class=\"alignright\"><input id=\"${buttonAddId}\" name=\"${buttonAddId}\" type=\"button\" class=\"button\" value=\"\u3000\uff1c\u0020\u8ffd\u52a0\u3000\"/ dojoAttachEvent=\"onclick:onMemberAddClick\"></div></div></div></div></div></div></td></tr></table></div>\n",
    
    templateString:"<div id=\"${widgetId}\" widgetId=\"${widgetId}\">"
      + "<table class=\"none\"><tr><td>"
      + "<div id=\"memberPopupDiv\"><div class=\"outer\"><div class=\"popup\" style=\"width:500px;\"><div class=\"clearfix\">" 
      + "<div class=\"memberlistToTop\" >${memberToTitle}</div>"
      + "<div class=\"memberlistFromTop\">"
      + "<select size=\"1\" style=\"width:100%\" name=\"${parentGroupSelectId}\" id=\"${parentGroupSelectId}\" dojoAttachEvent=\"onchange:changeParentGroup\"></select>"
      + "<select size=\"1\" style=\"width:100%\" name=\"${groupSelectId}\" id=\"${groupSelectId}\" dojoAttachEvent=\"onchange:changeGroup\"></select>"
      + "</div></div>"
      + "<div class=\"clearfix\">"
      
      + "<div class=\"memberlistToBody\">"
      
      + "<div class=\"clearfix\">"
      + "必須"
      + "<table style=\"width:100%;\"><tbody><tr><td style=\"width:90%\">"
      + "<select size=\"5\" multiple=\"multiple\" style=\"width:100%\" name=\"${memberToId}\" id=\"${memberToId}\"></select>"
      + "</td>"
      + "<td>"
      + "<input id=\"${buttonAddId}\" name=\"${buttonAddId}\" type=\"button\" class=\"button\" value=\"<\" dojoAttachEvent=\"onclick:onMemberAddClick\">"
      + "<input id=\"${buttonRemoveId}\" name=\"${buttonRemoveId}\" type=\"button\" class=\"button\" value=\">\"/ dojoAttachEvent=\"onclick:onMemberRemoveClick\">"
      + "</td></tr></tbody></table>"
      + "</div>"
      
      + "<div class=\"clearfix\">"
      + "任意"
      + "<table style=\"width:100%;\"><tbody><tr><td style=\"width:90%\">"
      + "<select size=\"5\" multiple=\"multiple\" style=\"width:100%\" name=\"${selfishMemberToId}\" id=\"${selfishMemberToId}\"></select>"
      + "</td>"
      + "<td>"
      + "<input id=\"${buttonAddSelfishId}\" name=\"${buttonAddSelfishId}\" type=\"button\" class=\"button\" value=\"<\" dojoAttachEvent=\"onclick:onSelfishMemberAddClick\">"
      + "<input id=\"${buttonRemoveSelfishId}\" name=\"${buttonRemoveSelfishId}\" type=\"button\" class=\"button\" value=\">\"/ dojoAttachEvent=\"onclick:onSelfishMemberRemoveClick\">"
      + "</td></tr></tbody></table>"
      + "</div>"

      + "</div>" 
      
      + "<div class=\"memberlistFromBody\"><select size=\"16\" multiple=\"multiple\" style=\"width:100%\" name=\"${memberFromId}\" id=\"${memberFromId}\"></select></div></div>"
      
      + "<div class=\"clearfix\">"
      
      + "<div class=\"memberlistToBottom\">"
      + "<div class=\"alignright\">"
      + "</div>"
      + "</div>"
      + "<div class=\"memberlistFromBottom\">"
      + "<div style=\"display: none;\" id=\"${widgetId}-memberlist-indicator\" class=\"indicator alignleft\">読み込み中</div>"
      + "</div>"
      
      + "</div></div></div></div>"
      + "</td></tr></table></div>\n",

    ////////////////////////////////////////////////////////
    //初期表示処理
    postCreate: function(){
    
        this.id = this.widgetId;
        
        //メンバーリストボックス初期表示
        var url = this.memberFromUrl+"&mode=post1_group&groupname="+this.initGroupName;
        var params = {
          url: url,
          key: this.memberFromOptionKey,
          value: this.memberFromOptionValue,
          indicator: this.widgetId + "-memberlist-indicator"
        };
        aimluck.io.createOptions(this.memberFromId, params);
        
        //部署１プルダウンを表示
        params = {
          url: this.post1GroupUrl,
          key: this.groupSelectOptionKey,
          value: this.groupSelectOptionValue,
          selectedId: this.initParentGroupName,
          preOptions: { key:this.group1SelectPreOptionKey, value:this.group1SelectPreOptionValue }
        };
        aimluck.io.createOptions(this.parentGroupSelectId, params);
        
        //部署２プルダウンを表示
        url = this.changePost1GroupUrl+"&parent_group_name="+this.initParentGroupName;
        params = {
          url: url,
          key: this.groupSelectOptionKey,
          value: this.groupSelectOptionValue,
          selectedId: this.initGroupName,
          preOptions: { key:this.group2SelectPreOptionKey, value:this.group2SelectPreOptionValue }
        };
        aimluck.io.createOptions(this.groupSelectId, params);
        
    },
    addOption:function(select, value, text, is_selected) {
      aimluck.io.addOption(select, value, text, is_selected);
    },
    addOptionSync:function(value, text, is_selected) {
      var select = dojo.byId(this.memberToId);
      var select2 = dojo.byId(this.selfishMemberToId);
      if (this.memberLimit != 0 && (select.options.length + select2.options.length) >= this.memberLimit) {
        alert("参加者は１００名が上限です。");
        return;
      }
      if (document.all) {
        var option = document.createElement("OPTION");
        option.value = value;
        option.text = text;
        option.selected = is_selected;
        if (select.options.length == 1 && select.options[0].value == ""){
                select.options.remove(0);
          }
          select.add(option, select.options.length);
      } else {
        var option = document.createElement("OPTION");
        option.value = value;
        option.text = text;
        option.selected = is_selected;
        if (select.options.length == 1 && select.options[0].value == ""){
            select.removeChild(select.options[0]);
        }
        select.insertBefore(option, select.options[select.options.length]);
      }
    },
    addMember:function(select_member_from, select_member_to, refer_select_member_to) {
      if (document.all) {
            var f_o = select_member_from.options;
            var t_o = select_member_to.options;
            var r_o = refer_select_member_to.options;
            if (f_o.length == 1 && f_o[0].value == "") return;
            for(i = 0 ; i < f_o.length; i ++ ) {
            
                if(!f_o[i].selected) continue;
                
                var iseq = false;
                for( j = 0 ; j < t_o.length; j ++ ) {
                  if( t_o[j].value == f_o[i].value ) {
                    iseq = true;
                    break;
                  }
                }
                for( m = 0 ; m < r_o.length; m ++ ) {
                  if( r_o[m].value == f_o[i].value ) {
                    iseq = true;
                    break;
                  }
                }

                if(iseq) continue;
               
                var option = document.createElement("OPTION");
                option.value = f_o[i].value;
                option.text = f_o[i].text;
                option.selected = true;
            if (t_o.length == 1 && t_o[0].value == ""){
                    t_o.remove(0);
            }
               if (this.memberLimit != 0 && (select_member_to.options.length + refer_select_member_to.options.length) >= this.memberLimit){
                 alert("参加者は１００名が上限です。");
                 return;
               }
               
                t_o.add(option, t_o.length);
            }
      } else {
            var f_o = select_member_from.options;
            var t_o = select_member_to.options;
            var r_o = refer_select_member_to.options;
            if (f_o.length == 1 && f_o[0].value == "") return;
            for(i = 0 ; i < f_o.length; i ++ ) {
                if(!f_o[i].selected) continue;
                var iseq = false;

                for( j = 0 ; j < t_o.length; j ++ ) {
                  if( t_o[j].value == f_o[i].value ) {
                    iseq = true;
                    break;
                  }
                }
                
                for( m = 0 ; m < r_o.length; m ++ ) {
                  if( r_o[m].value == f_o[i].value ) {
                    iseq = true;
                    break;
                  }
                }

                if(iseq) continue;
                var option = document.createElement("OPTION");
                option.value = f_o[i].value;
                option.text = f_o[i].text;
                option.selected = true;
            if (select_member_to.options.length == 1 && select_member_to.options[0].value == ""){
                select_member_to.removeChild(select_member_to.options[0]);
            }
               if (this.memberLimit != 0 && (select_member_to.options.length + refer_select_member_to.options.length) >= this.memberLimit){
                 alert("参加者は１００名が上限です。");
                 return;
               }
                select_member_to.insertBefore(option, t_o[t_o.length]);
            }
      }
      // add start No.2 スケジュール案内送信
      // スケジュール追加時、ログインユーザー以外のメンバーが追加された場合
      // 「登録する」ボタンを「送信する」に変更する
      if (this.eventSubmitMode == "eventSubmit_doSchedule_insert") {
          dojo.byId(this.eventSubmitButtonId).value = "登録する";
          for(i = 0 ; i < t_o.length; i ++ ) {
              if (t_o[i].value != this.loginUserName) {
                  dojo.byId(this.eventSubmitButtonId).value = "送信する";
                  break;
              }
          }
          for(i = 0 ; i < r_o.length; i ++ ) {
              if (r_o[i].value != this.loginUserName) {
                  dojo.byId(this.eventSubmitButtonId).value = "送信する";
                  break;
              }
          }
      }
      // add end
    },
    removeAllMember:function(select) {
      if (document.all) {
        var t_o = select.options;
          for(i = 0 ;i < t_o.length; i ++ ) {
              if( t_o[i].selected ) {
              t_o.remove(i);
                i -= 1;
            }
            }
      } else {
        var t_o = select.options;
          for(i = 0 ;i < t_o.length; i ++ ) {
              if( t_o[i].selected ) {
                    select.removeChild(t_o[i]);
                i -= 1;
                }
            }
      }
    },
    removeMember:function(select) {
      if (document.all) {
        var t_o = select.options;
          for(i = 0 ;i < t_o.length; i ++ ) {
              if( t_o[i].selected ) {
              t_o.remove(i);
                i -= 1;
            }
            }
      } else {
        var t_o = select.options;
          for(i = 0 ;i < t_o.length; i ++ ) {
              if( t_o[i].selected ) {
                    select.removeChild(t_o[i]);
                i -= 1;
                }
            }
      }
    },
    // change start No.2 スケジュール案内送信
    //removeMemberSync:function(select_member_to) {
    removeMemberSync:function(select_member_to, other_select_member_to) {
    // change end
      var select = select_member_to;
      if (document.all) {
        var t_o = select.options;
          for(i = 0 ;i < t_o.length; i ++ ) {
            if( t_o[i].selected ) {
              t_o.remove(i);
                i -= 1;
            }
        }
      } else {
        var t_o = select.options;
          for(i = 0 ;i < t_o.length; i ++ ) {
              if( t_o[i].selected ) {
                select.removeChild(t_o[i]);
                i -= 1;
              }
          }
      }
      // add start No.2 スケジュール案内送信
      // スケジュール追加時、ログインユーザー以外の参加メンバーが存在する場合
      // 「登録する」ボタンを「送信する」に変更する
      if (this.eventSubmitMode == "eventSubmit_doSchedule_insert") {
          dojo.byId(this.eventSubmitButtonId).value = "登録する";
          for(i = 0 ; i < select.length; i ++ ) {
              if (select[i].value != this.loginUserName) {
                  dojo.byId(this.eventSubmitButtonId).value = "送信する";
                  break;
              }
          }
          select = other_select_member_to;
          for(i = 0 ; i < select.length; i ++ ) {
              if (select[i].value != this.loginUserName) {
                  dojo.byId(this.eventSubmitButtonId).value = "送信する";
                  break;
              }
          }
      }
      // add end
    },
    
    /////////////////////////////////////////////////////////
    //部署２プルダウン選択時処理
    changeGroup: function(select) {
      
      //部署2プルダウンの選択値を取得
      var group_name = select.target.options[select.target.selectedIndex].value;
      var url = "";
      if( group_name == "ALL" ) {
        //"すべて"を選択した場合、部署１の選択肢を取得して、表示処理
        url = this.changePost2GroupUrl+"&mode=post1_group&groupname="+dojo.byId(this.parentGroupSelectId).value;
      } else {     
        url = this.changePost2GroupUrl+"&mode=group&groupname="+group_name;
      }
      
      var params = {
        url: url,
        key: this.memberFromOptionKey,
        value: this.memberFromOptionValue,
        indicator: this.widgetId + "-memberlist-indicator"
      };
      aimluck.io.createOptions(this.memberFromId, params);
      
    },
    
    /////////////////////////////////////////////////////////
    //部署１プルダウン選択
    changeParentGroup: function(select) {
    /*
      var parent_group_name = select.target.options[select.target.selectedIndex].value;
      var url = this.changeParentGroupUrl+"&parent_group_name="+parent_group_name;
      this.groupSelectOptionKey = parent_group_name;
      var params = {
        url: url,
        key: this.groupSelectOptionKey,
        value: this.groupSelectOptionValue,
        preOptions: { key:this.groupSelectPreOptionKey, value:this.groupSelectPreOptionValue }
      };
      aimluck.io.createOptions(this.groupSelectId, params);
      */
      ////////////////////////
      //メンバーリストボックス表示
      ////////////////////////
      var group_name = select.target.options[select.target.selectedIndex].value;
      var url = this.changePost2GroupUrl+"&mode=post1_group&groupname="+group_name;
      var params = {
        url: url,
        key: this.memberFromOptionKey,
        value: this.memberFromOptionValue,
        indicator: this.widgetId + "-memberlist-indicator"
      };
      aimluck.io.createOptions(this.memberFromId, params);
    
      /////////////////////
      //部署２プルダウンを表示
      /////////////////////
      
      //部署1プルダウンの選択値を取得
      var parent_group_name = select.target.options[select.target.selectedIndex].value;
      
      //部署2プルダウン生成処理
      var url = this.changePost1GroupUrl+"&parent_group_name="+parent_group_name;
      var preOptionKey = "";
      var preOptionValue = "";
      if( parent_group_name == "groupsend" || parent_group_name == "groupsend_without_post" ) {
        preOptionKey = this.group2SelectPreOptionKey;
        preOptionValue = this.group2SelectPreOptionValue;
      }
      var params = {
        url: url,
        key: this.groupSelectOptionKey,
        value: this.groupSelectOptionValue,
        //selectedId: this.initGroupName,
        preOptions: { key:preOptionKey, value:preOptionValue }
      };
      aimluck.io.createOptions(this.groupSelectId, params);
    },
    onMemberAddClick: function(/*Event*/ evt){
      this.addMember(dojo.byId(this.memberFromId), dojo.byId(this.memberToId), dojo.byId(this.selfishMemberToId));
    },
    onMemberRemoveClick: function(/*Event*/ evt){
       // change start No.2 スケジュール案内送信
       //this.removeMemberSync(dojo.byId(this.memberToId));
       this.removeMemberSync(dojo.byId(this.memberToId), dojo.byId(this.selfishMemberToId));
       // change end
    },
    onSelfishMemberAddClick: function(/*Event*/ evt){
      this.addMember(dojo.byId(this.memberFromId), dojo.byId(this.selfishMemberToId), dojo.byId(this.memberToId));
    },
    onSelfishMemberRemoveClick: function(/*Event*/ evt){
       // change start No.2 スケジュール案内送信
       //this.removeMemberSync(dojo.byId(this.selfishMemberToId));
       this.removeMemberSync(dojo.byId(this.selfishMemberToId), dojo.byId(this.memberToId));
       // change end
    },
    addSelfishOptionSync:function(value, text, is_selected) {
      var select = dojo.byId(this.selfishMemberToId);
      var select2 = dojo.byId(this.memberToId);
      if (this.memberLimit != 0 && (select.options.length + select2.options.length) >= this.memberLimit) {
        alert("参加者は１００名が上限です。");
        return;
      }
      if (document.all) {
        var option = document.createElement("OPTION");
        option.value = value;
        option.text = text;
        option.selected = is_selected;
        if (select.options.length == 1 && select.options[0].value == ""){
                select.options.remove(0);
          }
          select.add(option, select.options.length);
      } else {
        var option = document.createElement("OPTION");
        option.value = value;
        option.text = text;
        option.selected = is_selected;
        if (select.options.length == 1 && select.options[0].value == ""){
            select.removeChild(select.options[0]);
        }
        select.insertBefore(option, select.options[select.options.length]);
      }
    }
});
