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

dojo.require("aipo.widget.DropdownDatepicker");

aipo.cashflow.onLoadDialog = function(portlet_id){

  var obj = dojo.byId("client_name");
  if(obj){
     obj.focus();
  } 
  
  dojo.byId('category_id').onchange();
  dojo.byId('client_id').onchange();
}

aipo.cashflow.onLoadCategoryDialog = function(portlet_id){

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

}

aipo.cashflow.categoryOnChangeSelecter = function(portletId , url , values){

    var callbackArgs = new Array();
    aimluck.io.sendRawData(url + "&value=" + values + "&type=" + dojo.byId("type").value, values,aipo.cashflow.categorySetTemplate,callbackArgs);
    return false;
    
}

aipo.cashflow.categorySetTemplate = function(array,rtnData){

    var jsonData = eval( '('+rtnData["type"].toString().slice(4,-4)+')' );
    
    // サブカテゴリ
    var subcats = jsonData.subcats;
    var subcatsArray = subcats.split(",");
    var subcatsLength = (subcatsArray.length-1)/2;

    var subcatsDOM = dojo.byId('subCategory_id');
    while(subcatsDOM.lastChild) {
        subcatsDOM.removeChild(subcatsDOM.lastChild);
    }
    
    subcatsDOM.options[0] = new Option("(なし)", -1);
    
    for ( i = 0; i < subcatsLength ; i++ ){
        var val = subcatsArray[2*i];
        var disp = subcatsArray[2*i+1];
        var selval = dojo.byId('initSubCategory').value;
        var selected = (val == selval);
        subcatsDOM.options[i+1] = new Option(disp, val, false, selected);
    }

    // 科目
    aipo.cashflow.setItemCombo(jsonData.items);
}

aipo.cashflow.subcategoryOnChangeSelecter = function(portletId , url , values, catId){

    var callbackArgs = new Array();
    aimluck.io.sendRawData(url + "&value=" + values + "&value=" + catId + "&type=" + dojo.byId("type").value, values, aipo.cashflow.subcategorySetTemplate,callbackArgs);
    return false;
    
}

aipo.cashflow.subcategorySetTemplate = function(array,rtnData){

    var jsonData = eval( '('+rtnData["type"].toString().slice(4,-4)+')' );
    aipo.cashflow.setItemCombo(jsonData.items);
}

aipo.cashflow.setItemCombo = function(itemsCommaStr){
    var itemsArray = itemsCommaStr.split(",");
    var itemsLength = (itemsArray.length-1)/2;

    var itemsDOM = dojo.byId('item_id');
    while(itemsDOM.lastChild) {
        itemsDOM.removeChild(itemsDOM.lastChild);
    }
    
    for ( i = 0; i < itemsLength ; i++ ){
        var val = itemsArray[2*i];
        var disp = itemsArray[2*i+1];
        var selval = dojo.byId('initItem').value;
        var selected = (val == selval);
        
        itemsDOM.options[i] = new Option(disp, val, false, selected);
    }
}


aipo.cashflow.clientOnChangeSelecter = function(portletId , url , values){

    var callbackArgs = new Array();
    aimluck.io.sendRawData(url + "&value=" + values + "&type=" + dojo.byId("type").value, values, aipo.cashflow.clientSetTemplate,callbackArgs);
    return false;
}

aipo.cashflow.clientSetTemplate = function(array,rtnData){

    var jsonData = eval( '('+rtnData["type"].toString().slice(4,-4)+')' );
    
    var payClassName = jsonData.clientPayClassName;

    dojo.byId('client_pay_class').innerHTML = payClassName;
    
    aipo.cashflow.changeFormByPayClass(jsonData.clientPayClassId);
}


aipo.cashflow.formSwitchCategoryInput = function(button) {
    if(button.form.is_new_category.value == 'TRUE' || button.form.is_new_category.value == 'true') {
        button.value = '新しく入力する';
        aipo.cashflow.formCategoryInputOff(button.form);
    } else {
        button.value = '一覧から選択する';
        aipo.cashflow.formCategoryInputOn(button.form);
    }
}

aipo.cashflow.formCategoryInputOn = function(form) {
    dojo.byId('seikyuCategorySelectField').style.display = "none";
    dojo.byId('seikyuCategoryInputField').style.display = "";

    form.is_new_category.value = 'TRUE';
}

aipo.cashflow.formCategoryInputOff = function(form) {
    dojo.byId('seikyuCategoryInputField').style.display = "none";
    dojo.byId('seikyuCategorySelectField').style.display = "";
    
    form.is_new_category.value = 'FALSE';
}

aipo.cashflow.onReceiveMessage = function(msg){

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

aipo.cashflow.onListReceiveMessage = function(msg){
  
    if(!msg) {
        var arrDialog = dijit.byId("modalDialog");
        if(arrDialog){
            arrDialog.hide();
        }
        aipo.portletReload('todo');
        aipo.portletReload('schedule');
    }
    if (dojo.byId('listmessageDiv')) {
        dojo.byId('listmessageDiv').innerHTML = msg;
    }
    
}

/* Add　2011/02/21 小濵 Start */
aipo.cashflow.ajaxCheckAndSubmit = function(button) {

/* Del 2011/03/02 小濵　Start */
//	if(msgList.length == 0){
/* Del 2011/03/02 小濵　End */
	aimluck.io.setHiddenValue(button);
	button.form.submit();
/* Del 2011/03/02 小濵　Start */
//	} else {
//		dojo.byId('errmessageDiv').innerHTML = aipo.cashflow.encodeErrMessage(msgList);
//	}
/* Del 2011/03/02 小濵　End */

}

/* Del 2011/03/02 小濵　Start */
/*
aipo.cashflow.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;
	}
}
*/
/* Del 2011/03/02 小濵 End */
/* Add　2011/02/21 小濵 End */

aipo.cashflow.changeFormByPayClass = function(payClass) {
	if(payClass == "3"){
		dojo.byId('cashflowSelectField0').style.display="none";
		dojo.byId('cashflowSelectField1').style.display="none";
		// dojo.byId('cashflowSelectField2').style.display="";
		dojo.byId('cashflowSelectField3').style.display="";
		dojo.byId('cashflowSelectField4').style.display="";
		dojo.byId('cashflowSelectField5').style.display="";
		dojo.byId('cashflowSelectField6').style.display="";
		dojo.byId('cashflowSelectField7').style.display="";
		dojo.byId('cashflowSelectField8').style.display="";
		dojo.byId('cashflowSelectField9').style.display="";
		dojo.byId('cashflowSelectField10').style.display="";
		dojo.byId('cashflowSelectField11').style.display="";
		dojo.byId('cashflowSelectField12').style.display="";
		dojo.byId('cashflowSelectField13').style.display="";
		dojo.byId('cashflowSelectField14').style.display="";
		dojo.byId('cashflowSelectField15').style.display="";
	}else{
		dojo.byId('cashflowSelectField0').style.display="";
		dojo.byId('cashflowSelectField1').style.display="";
		// dojo.byId('cashflowSelectField2').style.display="none";
		dojo.byId('cashflowSelectField3').style.display="none";
		dojo.byId('cashflowSelectField4').style.display="none";
		dojo.byId('cashflowSelectField5').style.display="none";
		dojo.byId('cashflowSelectField6').style.display="none";
		dojo.byId('cashflowSelectField7').style.display="none";
		dojo.byId('cashflowSelectField8').style.display="none";
		dojo.byId('cashflowSelectField9').style.display="none";
		dojo.byId('cashflowSelectField10').style.display="none";
		dojo.byId('cashflowSelectField11').style.display="none";
		dojo.byId('cashflowSelectField12').style.display="none";
		dojo.byId('cashflowSelectField13').style.display="none";
		dojo.byId('cashflowSelectField14').style.display="none";
		dojo.byId('cashflowSelectField15').style.display="none";
	}
}

/* Add　2011/02/17 muramoto end */

/* Add　2011/03/02 小濵 Start */
aipo.cashflow.formCashflowOnTemp = function() {
    dojo.byId('cashflow_on_temp').style.display = "block";
    dojo.byId('cashflow_off_temp').style.display = "none";
}

aipo.cashflow.formCashflowOffTemp = function() {
    dojo.byId('cashflow_on_temp').style.display = "none";
    dojo.byId('cashflow_off_temp').style.display = "block";
}

window.onload = function(){
	if(document.getElementById("cashflow1") != null){
		document.getElementById("cashflow1").click()
	}
}
/* Add　2011/03/02 小濵 End */

