
/*以下、各種設定画面の表示オブジェクト*/
var _ =function(){
	var constructorF = function(){
		var _this = this;
		var init = function(){
			//まずはカレントの表をクリアする。
			_disp.tblId = null;
			//続いて、サーバから、ソート一覧を取得する。
			_sysData._load("sort",{ db_id : _this.dbId }, disp )
		};
		var moveTr = function( elmTr , isUp ){
			chgBtnEnable();
			var targElm = null;
			//iA,iBともに、targElm!=nullである必要がある。
			if( isUp ){
				targElm = _D.prE( elmTr );
				if( targElm ){
					_D.iB( elmTr , targElm );
				}else{
					_D.aC( _D.par( elmTr ) , elmTr );
				};
			}else{
				targElm = _D.nxE( elmTr );
				if( targElm ){
					_D.iA( elmTr , targElm );
				}else{
					_D.iB( elmTr , _D.fEC( _D.par( elmTr ) ) );
				};
			};
		};
		var deleteTr = function( elmTr ){
			chgBtnEnable();
			_D.rC( elmTr );
		};
		var commit = function( ){
			_prcName_="COMMIT CHANGE";
			var reqValues = { db_id : _this.dbId };
			for( var i = 0 ; _U.isIdx( i , tbody.children ) ; i ++ ){
				var tr = tbody.children[i];
				reqValues["sort_name["+i+"]"] = _D.fEC( tr.children[0] ).value;
				reqValues["sort_string["+i+"]"] = _D.fEC( tr.children[1] ).value;
			};
			_disp.request( "php/" + _urlMap.action.chgSort , reqValues , init );
		};
		var chgBtnEnable = function(){
			_D.g( "smng_rollback" ).disabled = false;
			_D.g( "smng_rollback" ).style.opacity = "1";
			_D.g( "smng_commit" ).disabled = false;
			_D.g( "smng_commit" ).style.opacity = "1";

		};
		var validateSortStr = function( elm ){
			var strSort = elm.value;
			var arySort = strSort.split(",");
			if( strSort == "" ){ arySort = []; };
			var aryNulStrIdx = [];
			var objDuplicated = {};
			for( var i = 0 ; _U.isIdx( i , arySort ) ; i ++  ){
				if( arySort[i] == "" ){
					aryNulStrIdx.push( i );
					continue;
				};
				var maxIdx = _U.find( arySort[i] , arySort );
				if( maxIdx != -1 && maxIdx != i ){
					if( ! objDuplicated.hasOwnProperty( arySort[i] ) ){
						objDuplicated[ arySort[i] ] ={ };
						objDuplicated[ arySort[i] ][ maxIdx ] = maxIdx;
					};
					objDuplicated[ arySort[i] ][ i ] = i ;
				};
			};
			var hasKey = false;
			for( var key in objDuplicated ){ hasKey = true ; break ;};

			if( aryNulStrIdx.length == 0 && ! hasKey ){
				if( elm.value != elm.dataset.org ){
					chgBtnEnable();
				};
				return;
			};

			var msg = _msg.getMessage( "srtmng_W0" );
			var mainMsgObj =  msg.main ;
			var msgN = msg.sub.n;
			var msgD = msg.sub.d;
			for( var i = 0 ; _U.isIdx( i , aryNulStrIdx ) ; i ++ ){
				if( mainMsgObj.discription != "" ){
					mainMsgObj.discription += "\r\n";
				};
				mainMsgObj.discription += msgN.replace( /#1#/g , aryNulStrIdx[ i ] );
			};
			for( var key in objDuplicated ){
/*				for( var i = 0 ; _U.isIdx( i , objDuplicated[ key ] ) ; i ++ ){
					if( mainMsgObj.discription != "" ){
						mainMsgObj.discription += "\r\n";
					};
					mainMsgObj.discription += msgD.replace( /#1#/g , objDuplicated[ key ][ i ] ).replace( /#1#/g , arySort[ objDuplicated[ key ][ i ] ] );
				};
*/
				for( var key2 in objDuplicated[ key ] ){
					if( mainMsgObj.discription != "" ){
						mainMsgObj.discription += "\r\n";
					};
					mainMsgObj.discription += msgD.replace( /#1#/g , objDuplicated[ key ][ key2 ] ).replace( /#2#/g , key );
				};
			};
			_disp.showMessage( mainMsgObj , function(){ elm.focus(); } );
		};

		var eventFuncs = {
			rollbackOnClick:function(){
				_disp.showMessageEz( "cmmn_101" , null , function(){_prcName_="ROLL BACK"; init();} );
			},
			commitOnClick:function(){
				_disp.showMessageEz( "cmmn_100" , null , commit );
			},
			sortStrOnChange:function(){
				validateSortStr( this );
			},
			nameOnChange:function(){
				chgBtnEnable( this );
			},
			upOnClick:function(){
				moveTr( _D.par(_D.par(this)) , true );
			},
			downOnClick:function(){
				moveTr( _D.par(_D.par(this)) );
			},
			deleteOnClick:function(){
				var tr = _D.par(_D.par(this));
				_disp.showMessageEz( "srtmng_0" , [ _D.fEC( _D.fEC(_D.par( _D.par(this) ) ) ).value ] , function(){ 
					deleteTr(tr);}
				);
			},
			addOnClick:function(){
				addTr();
			}
		}

		var nodeDef ={
			mainInner :{attr:{
				tagname:"div",
				className:"_vh_sc_auto_ _smng_"
			},children:[
				{attr:{
					tagname:"h1",
					innerHTML:"Sort String Setting",
					className:"_titles_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						value:"ROLLBACK CHANGES",
						disabled:true,
						id:"smng_rollback",
						onclick:eventFuncs.rollbackOnClick,
						className:"_btn_y_ _btn_in_title_"
					}},{attr:{
						tagname:"input",
						type:"button",
						value:"COMMIT CHANGES",
						disabled:true,
						id:"smng_commit",
						onclick:eventFuncs.commitOnClick,
						className:"_btn_y_ _btn_in_title_"
					}}
				]},{attr:{
					tagname:"table",
					id:"tbl_smng"
				},children:[
					{attr:{
						tagname:"thead",
						id:"thead_smng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_smng_head"
						},children:[
							{attr:{
								tagname:"th",
								id:"th_smng_name"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Sort Name"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_smng_str"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Sort String"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_smng_act"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"manage action"
								}}
							]}
						]}
					]},{attr:{
						tagname:"tbody",
						id:"tbody_smng"
					}},{attr:{
						tagname:"tfoot",
						id:"tfoot_smng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_smng_foot"
						},children:[
							{attr:{
								tagname:"td",
								className:"_td_dmy_"
							}},{attr:{
								tagname:"td",
								colSpan:2
							},children:[
								{attr:{
									tagname:"input",
									type:"button",
									value:"add",
									id:"smng_add",
									onclick:eventFuncs.addOnClick,
									className:"_btn_adjust_"
								}}
							]}
						]}
					]}
				]}
			]},
			tableTr :{attr:{
				tagname:"tr",
				className:"_smng_tr_body_"
			},children:[
				{attr:{
					tagname:"td",
					className:"_smng_td_name_"
				},children:[
					{attr:{
						tagname:"input",
						type:"text",
						onchange:eventFuncs.nameOnChange,
						className:"_smng_txt_name_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_smng_td_str_"
				},children:[
					{attr:{
						tagname:"input",
						type:"text",
						onblur:eventFuncs.sortStrOnChange,
						className:"_smng_txt_str_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_smng_td_act_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						onclick:eventFuncs.upOnClick,
						value:"up"
					}},
					{attr:{
						tagname:"input",
						onclick:eventFuncs.downOnClick,
						type:"button",
						value:"down"
					}},
					{attr:{
						tagname:"input",
						onclick:eventFuncs.deleteOnClick,
						type:"button",
						value:"delete",
						className:"_smng_btn_del_ _btn_y_"
					}}
				]}
			]}
		};

		var main = _D.g("main");
		this.init = init;
		var tbody;
		var _prcName_;
		var disp = function( ){
			//ここまででエラーがあった場合は既に画面通知済みのはず。

			main.innerHTML = "";

			//ひとまず、基本の画面要素作成
			_D.aCAndCN( main , nodeDef.mainInner );

			_D.g("smng_rollback").style.opacity = "0.2";
			_D.g("smng_commit").style.opacity = "0.2";

			var sortTable = _sysData._getData("sort");


			//tbodyにDB毎の要素を作成する。
			tbody = _D.g("tbody_smng");
			for( var i = 0 ; _U.isIdx( i , sortTable ) ; i ++ ){
				var tr = _D.aCAndCN( tbody , nodeDef.tableTr );

				tr.dataset.id=sortTable[i].id;
				_D.fEC( tr.children[ 0 ] ).value = sortTable[i].name;
				_D.fEC( tr.children[ 1 ] ).value = sortTable[i].sortStr;
				_D.fEC( tr.children[ 1 ] ).dataset.org = sortTable[i].sortStr;
			};

			if( _prcName_ ){
				procFin();
			};
		};
		var procFin = function(){
			_disp.showMessageEz( "cmmn_10" , [_prcName_] , null );
			_prcName_ = null;
		};
		var addTr = function(){
			chgBtnEnable();
			var tr = _D.aCAndCN( tbody , nodeDef.tableTr );
		};
	};
	constructorF.prototype = _protoEachDisp;
	_eachDisp.SMng = new constructorF();
}();
delete _;
