		/*
		 * Copyright(C) 2010 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 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.lunchorder");
		
		dojo.require("aipo.widget.DropdownDatepicker");
		dojo.require("aipo.lunchorder.utils");
	
		aipo.lunchorder.onLoadLunchOrderDialog = function(portlet_id){
	
		  var obj = dojo.byId('lunchorder_name');
		  if(obj){
		     obj.focus();
		  } 
		}
	
		aipo.lunchorder.onReceiveMessage = function(msg){
		//確認画面表示用
		    if(!msg) {//エラーメッセージが無ければダイアログを消して、確認画面ダイアログを表示させる。
		        var arrDialog = dijit.byId("modalDialog");
		        if(arrDialog){
		            arrDialog.hide();
		        }     
		    for(var index in ptConfigToConfirmation) {//pdConfig2はlunchorder.vm参照
		            if(ptConfigToConfirmation[index].group == 'lunchorder-confirmation') {
		      aipo.common.showDialog(ptConfigToConfirmation[index].initUrl, index);
		                break;
		            }
		    }
		    }
		    if (dojo.byId('messageDiv')) {
		        dojo.byId('messageDiv').innerHTML = msg;
		    }
		    
		}
			
		aipo.lunchorder.onInsert = function(msg){
	
		    if(!msg) {
		        var arrDialog = dijit.byId("modalDialog");
		        if(arrDialog){
		            arrDialog.hide();
		        }
		        //aipo.openDialog('lunchorderConfirmation')
		        aipo.portletReload('lunchorder');
		    }
		    if (dojo.byId('messageDiv')) {
		        dojo.byId('messageDiv').innerHTML = msg;
		    }
		    
		}
	
		aipo.lunchorder.onPreView = function(msg){
		//プレビューから戻る
		    if(!msg) {//ダイアログを消して、追加、編集ダイアログを表示させる。
		        var arrDialog = dijit.byId("modalDialog");
		        if(arrDialog){
		            arrDialog.hide();
		        }     
		    for(var index in ptConfigToPreView) {
		            if(ptConfigToPreView[index].group == 'lunchorder-preview') {
		      aipo.common.showDialog(ptConfigToPreView[index].initUrl, index);
		                break;
		            }
		    }
		    }
		    if (dojo.byId('messageDiv')) {
		        dojo.byId('messageDiv').innerHTML = msg;
		    } 
		     
		}
		
		aipo.lunchorder.onReturn = function(msg){
		//プレビューから戻る
		    if(!msg) {//ダイアログを消して、追加、編集ダイアログを表示させる。
		        var arrDialog = dijit.byId("modalDialog");
		        if(arrDialog){
		            arrDialog.hide();
		        }     
		    for(var index in ptConfigToReturn) {
		            if(ptConfigToReturn[index].group == 'lunchorder-return') {
		      aipo.common.showDialog(ptConfigToReturn[index].initUrl, index);
		                break;
		            }
		    }
		    }
		    if (dojo.byId('messageDiv')) {
		        dojo.byId('messageDiv').innerHTML = msg;
		    }  
		    
		}
		
		aipo.lunchorder.onListReceiveMessage = function(msg){
		  
		    if(!msg) {
		        var arrDialog = dijit.byId("modalDialog");
		        if(arrDialog){
		            arrDialog.hide();
		        }
		        aipo.portletReload('lunchorder');
		    }
		    if (dojo.byId('listmessageDiv')) {
		        dojo.byId('listmessageDiv').innerHTML = msg;
		    }
		    
		}
		
		
		if (window.addEventListener) {
		window.addEventListener('click',function(e){
			window.event = e;
			window.event.srcElement = e.target;
		},true);
		
	}
	
	aipo.lunchorder.appendRow = function(){
		var tbase = document.getElementById('base-lunchorder_item').tBodies[0];
		var tbod = document.getElementById('lunchorder_edit_table').tBodies[0];
		var rcopy = tbase.rows[0].cloneNode(true);
		
		tbod.insertBefore(rcopy, tbod.rows[tbod.rows.length - 1]);
	
		for (var i = 1, len = tbod.rows.length - 1; 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('item_id_') == 0) {
							obj.name = 'item_id_' + i;
						} else if (name.indexOf('item_name_') == 0) {
							obj.name = 'item_name_' + i;
						} else if (name.indexOf('item_price_') == 0) {
							obj.name = 'item_price_' + i;
						}else if (name.indexOf('item_comment_') == 0) {
							obj.name = 'item_comment_' + i;
						}else if (name.indexOf('item_min_order_flag_') == 0){
							obj.name = 'item_min_order_flag_' + i;
						}else if (name.indexOf('edit_length[]') == 0){
						obj.value = i;
						}else if (name.indexOf('add_value[]') == 0){
							
							obj.value = i;
						}	
					}
				}
			}
		}
	}
	
	aipo.lunchorder.deleteRow = function(){
		var bt = window.event.srcElement;
		var tr = bt.parentNode.parentNode;
		var tbod = tr.parentNode;
		tbod.deleteRow(tr.sectionRowIndex);
		for (var i = 1, len = tbod.rows.length - 1; 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('item_id_') == 0) {
							obj.name = 'item_id_' + i;
						} else if (name.indexOf('item_name_') == 0) {
							obj.name = 'item_name_' + i;
						} else if (name.indexOf('item_price_') == 0) {
							obj.name = 'item_price_' + i;
						}else if (name.indexOf('item_comment_') == 0) {
							obj.name = 'item_comment_' + i;
						}else if (name.indexOf('item_min_order_flag_') == 0){
							obj.name = 'item_min_order_flag_' + i;
						}else if (name.indexOf('add_value[]') == 0){
							
							obj.value = i;
						}	
					}
				}
			}
		}
	}