/*
 * 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.book");

aipo.book.onLoadBookDialog = function(portlet_id){

  var obj = dojo.byId("book");
  if(obj){
     obj.focus();
  }  

}

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

aipo.book.ajaxCheckAndSubmit = function(button) {
	var msgList = aipo.book.validate();

	if(msgList.length == 0){
		aimluck.io.setHiddenValue(button);
    	button.form.submit();
	} else {
		dojo.byId('errmessageDiv').innerHTML = aipo.book.encodeErrMessage(msgList);
	}

}

aipo.book.validate = function(){
	var msgList = new Array();
	
	var start_year = parseInt(dojo.byId("start_year").value);
	var start_month = parseInt(dojo.byId("start_month").value);
	var end_year = parseInt(dojo.byId("end_year").value);
	var end_month = parseInt(dojo.byId("end_month").value);
	
	// 出力期間のチェック
	if(start_year*12 + start_month > end_year*12 + end_month){
		msgList.push("『終了年月』は『開始年月』以降の日付を指定してください。");
	}
	return msgList;
}

aipo.book.encodeErrMessage = function(list) {
	if(list == null || list.length == 0) {
		return "";
	} else {
		var errMsg = "";
		for(var i=0; i<list.length; i++){
			var msg = list[i];
			errMsg += "<li><span class='caution'>" + msg + "</span></li>";
		}
		return errMsg;
	}
}
