/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2008 Aimluck,Inc.
 * http://aipostyle.com/
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

dojo.provide("aipo.enquete");

dojo.require("aipo.widget.DropdownDatepicker");
dojo.require("aipo.widget.MemberNormalSelectList");
dojo.require("aipo.widget.GroupNormalSelectList");
dojo.require("aipo.enquete.utils");


aipo.enquete.onLoadEnqueteDialog = function(portlet_id){
     
        var obj = dojo.byId("name");
        if(obj){
           obj.focus();
        }
        
        
        var closeBtnCker = dojo.byId("dialogCloseButton");
        var handle
        if(closeBtnCker){
        handle = dojo.connect(closeBtnCker, "onclick", this , function() {
            aipo.enquete.utils.onModalDialogWide(false);
        });
        }else{
        if(handle){
            dojo.disconnect(handle);
        }
        
        }
        aipo.enquete.utils.onModalDialogWide(true);
        
       var mpicker = dijit.byId("membernormalselect");
       if(mpicker){
       		var select = dojo.byId('init_memberlist');
    	    var i;
    	    var s_o = select.options;
    	    if (s_o.length == 1 && s_o[0].value == "") return;
    	    for(i = 0 ; i < s_o.length; i ++ ) {
    	        mpicker.addOptionSync(s_o[i].value,s_o[i].text,true);
    	    }
        }
                      
}


aipo.enquete.onReceiveMessage = function(msg){
    if(!msg) {
        var arrDialog = dijit.byId("modalDialog");
        if(arrDialog){
            arrDialog.hide();
        }
        
        aipo.portletReload('enquete');
    } 
    if (dojo.byId('messageDiv')) {
        dojo.byId('messageDiv').innerHTML = msg;
    }
}

if (window.addEventListener) {
	window.addEventListener('click',function(e){
		window.event = e;
		window.event.srcElement = e.target;
	},true);
} 
aipo.enquete.deleteRow = function(){
	var bt = window.event.srcElement;
	var tr = bt.parentNode.parentNode;
	var tbod = tr.parentNode;
	tbod.deleteRow(tr.sectionRowIndex);
	
	var tbod2 = document.getElementById('enqueteitem_edit_table').tBodies[0];
	for (var i = 1, len = tbod2.rows.length ; i < len; i++) {
	  var tr2 = tbod2.rows[i];
		for (var j = 0, td; td = tr2.cells[j]; j++) {
			for (var k = 0, obj; obj = td.childNodes[k]; k++){
				var name = obj.name;
				if (name) {
				    if (name.indexOf('itemSum') == 0){
				    	var is = name.indexOf('itemSum',i);
						if(is != 'itemSum[]'){
							obj.name = 'itemSum[]';	
						}
					} else if (name.indexOf('choice_item_select') == 0 ) {
						var cis = name.indexOf('choice_item_select');
						if(cis != 'choice_item_select[' + i + ']'){
							obj.name = 'choice_item_select[' + i + ']';
						}
					} else if (name.indexOf('item_title') == 0){
						var it = name.indexOf('item_title');
						if(it != 'item_title['+ i + ']'){
							obj.name = 'item_title[' + i + ']';
						}
					} else if (name.indexOf('choice_item_title') == 0) {
						var cit = name.indexOf('choice_item_title');
						if(cit != 'choice_item_title[' + i + ']'){
							obj.name = 'choice_item_title[' + i + ']';
						}
					}
				}
			}
		}
	}
}

aipo.enquete.appendItemRow = function(){
	var tbase = document.getElementById('base_enqueteitem').tBodies[0];
	var tbod = document.getElementById('enqueteitem_edit_table').tBodies[0];
	var rcopy = tbase.rows[0].cloneNode(true);
	
	tbod.insertBefore(rcopy, null);
	for (var i = 1, len = tbod.rows.length ; i < len; i++) {
		var tr = tbod.rows[i];
		for (var j = 0, td; td = tr.cells[j]; j++) {
			for (var k = 0, obj; obj = td.childNodes[k]; k++){
				var name = obj.name;
				if (name) {
				    if (name.indexOf('itemSum[]') == 0) {
						obj.name = 'itemSum[]';
					} else if (name.indexOf('item_title[]') == 0) {
						obj.name = 'item_title[' + i + ']';
					} else if (name.indexOf('choice_item_select[]') == 0) {
						td.innerHTML = "<input name='choice_item_select[" +i+ "]' type='radio' value='one_select' checked onClick='aipo.enquete.text(" +i+',value'+ ")'>単一選択</input><br><input name='choice_item_select[" +i+ "]' type='radio' value='multi_select' onClick='aipo.enquete.text(" +i+',value'+ ")'>複数選択可</input><br><input name='choice_item_select[" +i+ "]' type='radio' value='free_text' onClick='aipo.enquete.text(" +i+',value'+ ")'>フリーテキスト</input>";
					} else if (name.indexOf('choice_item_title[]') == 0) {
						obj.name = 'choice_item_title[' + i + ']';
						obj.id = 'choice_item_title[' + i + ']';
					}
				}
			}
		}
	}
}


aipo.enquete.changeEnd = function(form) {
  if(form.deadline_date_hour.value == 24) {
    form.deadline_date_minute.value = 0;
  }
}



aipo.enquete.dumpObj = function(o){
  var str = "";
  for(var i in o) {
    str = str + "\n" + i + "\t"+ o[i];
  }
  alert(str);
}



aipo.enquete.text = function(count, value){
  obj = document.enqueteForm;
  if(value == 'free_text'){
    document.getElementById("choice_item_title[" + count +"]").disabled = true;
  } else{
      document.getElementById("choice_item_title[" + count + "]").disabled = false;
  }
}
