
/*以下、各種設定画面の表示オブジェクト*/
var _ =function(){
	var constructorF = function(){
		var unCommitChange = false;
		var _this = this;
		var main = _D.g("main");
		var colTbody;
		var uqTbody;
		var _prcName_;

		var tblData ;
		var colTable;
		var optTable ;
		var uqTable ;
		var autoNumData;
		var typeTbl;
		var colNameDisableChars ="[]\"\'";
		var colNameDisableName ="rowid";
		var autoNumUsableType ={
			1:"Integer",
			3:"Text"
		};
		var typeUsablePad = 3;
		var typeUsableOpt = 8;



		var init = function(){
			//まずはカレントの行をクリアする。
			_disp.rowId = null;
			//続いて、サーバから、ソート一覧を取得する。
			_sysData._loadChain(["tbl","col","opt","uq","auto"],{ db_id : _this.dbId }, disp )
		};
		this.init = init;


		var deleteTr = function( elmTr ){
			chgBtnEnable();
			_D.rC( elmTr );
		};

		var commit = function( ){
			_prcName_="COMMIT CHANGE";
			var reqValues = { db_id : _this.dbId , table_id:_this.tblId };

			//列定義部分のリクエストデータを取得
			for( var i = 0 ; _U.isIdx( i , colTbody.children ) ; i++ ){
				var colTr = colTbody.children[i];
				reqValues["column_id["+i+"]"] = colTr.dataset.id;
				reqValues["column_name["+i+"]"] = getColInputs( colTr , colInputsNames.name ).value;
				reqValues["type["+i+"]"] = getColInputs( colTr , colInputsNames.type ).value;
				reqValues["css_string["+i+"]"] = getColInputs( colTr , colInputsNames.css ).value;
				reqValues["pk["+i+"]"] = getColInputs( colTr , colInputsNames.pk ).checked;
				if( colTr.dataset.an == "true" && find( getColInputs( colTr , colInputsNames.type ).value , [1,3] ) != -1 ){ 
					reqValues["auto_num["+i+"]"] = ( colTr.dataset.an == "true" && getColInputs( colTr , colInputsNames.an ).checked );
					if( getColInputs( colTr , colInputsNames.an ).checked ){
						reqValues["prefix["+i+"]"] = getColInputs( colTr , colInputsNames.prfx ).value;
						reqValues["suffix["+i+"]"] = getColInputs( colTr , colInputsNames.sffx ).value;
						reqValues["length["+i+"]"] = getColInputs( colTr , colInputsNames.len ).value;
						reqValues["pad["+i+"]"] = getColInputs( colTr , colInputsNames.pad ).value;
					};
				};
				if( getColInputs( colTr , colInputsNames.type ).value == 8 ){ 
					reqValues["options["+i+"]"] = getColInputs( colTr , colInputsNames.txtOpt ).value;
				};
			};
			//一意制約定義部分のリクエストデータを取得
			for( var i = 0 ; _U.isIdx( i , uqTbody.children ) ; i++ ){
				var uqTr = uqTbody.children[i];
				reqValues["unique_id["+i+"]"] = uqTr.dataset.id;
				var slct = getUqInputs( uqTr , uqInputsNames.slctCol );
				for( var j = 0 ; _U.isIdx( j , slct.selectedOptions ) ; j ++ ){
					if( slct.selectedOptions[j].dataset.id == null ){
						reqValues["unique_col_id[" + i + "][" + j + "]"] = null;
					}else{
						reqValues["unique_col_id[" + i + "][" + j + "]"] = slct.selectedOptions[j].dataset.id;
					};
					reqValues["unique_col[" + i + "][" + j + "]"] = slct.selectedOptions[j].value;
				};
			};
			_disp.request( "php/" + _urlMap.action.chgCol , reqValues , init );
		};
		var chgBtnEnable = function(){
			unCommitChange = true;
			_D.g( "cmng_rollback" ).disabled = false;
			_D.g( "cmng_commit" ).disabled = false;
		};

		var trMove = function( tr , isUp ){
			var ibTarg;
			if( isUp ){
				ibTarg = _D.prE( tr );
			}else{
				var nx = _D.nxE( tr ); 
				if( nx != null ){
					ibTarg = _D.nxE( nx );
				}else{
					ibTarg = _D.fEC( _D.par( tr ) );
				};
			};
			if( ibTarg ){
				_D.iB( tr , ibTarg );
			}else{
				_D.aC( _D.par( tr ) ,tr );
			}
		};

		var chgUqCol = function( tr , colTable , uqTableChildren ){
			var ulCol = getUqInputs( tr , uqInputsNames.ulCol );
			var slctCol = getUqInputs( tr , uqInputsNames.slctCol );
			//一旦両エレメントの子要素を破棄する。
			ulCol.innerHTML = "";
			slctCol.innerHTML = "";
			slctCol.dataset.org = "";
			for( var i = 0 ; _U.isIdx( i , colTable ) ; i ++ ){
				var opt = _D.aCAndCN( slctCol , nodeDef.option );
				opt.value = colTable[ i ].id;
				opt.label = colTable[ i ].name;
				
				var uqCol = null;
				if( colTable[ i ].id != "" ){
					uqCol = _sysData._tblFind( uqTableChildren , [ { key : "col" , value : colTable[ i ].id } ] , true )[ 0 ];
				}else{
					opt.disabled = true;
				};
				if( uqCol ){
					opt.dataset.id = uqCol.id;
					opt.selected = true;
					var li = _D.aCAndCN( ulCol , nodeDef.li );
					li.innerHTML =  _U.htmlEscape( colTable[ i ].name );
					slctCol.dataset.org += opt.value+",";
				};
			};
			slctCol.size = colTable.length;
		};

		//列名の変更、行の移動、行の削除、行の追加時に呼び出すこと。
		//無効となるユニーク行の削除、ユニークの選択可能な列定義の変更を行う。
		var afterChangeColTbl = function(){
			//列定義のidとnameの一覧を取得する。
			var aryWkCol = [];
			for( var i = 0 ; _U.isIdx( i , colTbody.children ) ; i ++ ){
				var colTr = colTbody.children[ i ];
				aryWkCol.push( { id : colTr.dataset.id , name : getColInputs( colTr , colInputsNames.name ).value } )
			};
			//一意制約の行毎に処理する。
			for( var i = 0 ; _U.isIdx( i , uqTbody.children ) ; i ++ ){
				var uqTr = uqTbody.children[ i ];
				var slctCol = getUqInputs( uqTr , uqInputsNames.slctCol );

				var valueAndIds = [];
				var delFlg = false;
				//選択されている対象をチェックする。
				for( var j = 0 ; _U.isIdx( j , slctCol.selectedOptions ) ; j ++ ){
					var val = slctCol.selectedOptions[ j ].value;
					var wkCol = _sysData._tblFind( aryWkCol , [ { key : "id" , value : val } ] , true )[ 0 ];
					if( wkCol ){
						valueAndIds.push( { col: val , id : slctCol.selectedOptions[ j ].dataset.id } );
					}else{
						delFlg = true;
						break;
					};
				};
				if( delFlg ){
					_D.rC( uqTr );
					continue;
				};

				chgUqCol( uqTr , aryWkCol , valueAndIds );
			};
		};

		var colNameValidate = function( elmInput ){
			var validationErrMode ={
				n:"n",
				d:"d",
				c:"c",
				r:"r",
				ok:"ok"
			};
			var err = validationErrMode.ok;
			var strCName = elmInput.value;
			if( strCName == "" ){
				err = validationErrMode.n;
			};
			if( err == validationErrMode.ok  ){
				if(
					strCName.indexOf( "]" ) != -1 ||
					strCName.indexOf( "[" ) != -1 ||
					strCName.indexOf( "\'" ) != -1 ||
					strCName.indexOf( "\"" ) != -1
				){
					err = validationErrMode.c;
				};
				if(
					strCName == colNameDisableName
				){
					err = validationErrMode.r;
				};

			};
			if( err == validationErrMode.ok  ){
				for( var i = 0 ; _U.isIdx( i , colTbody.children ) ; i ++ ){
					var chk = getColInputs( colTbody.children[i] , colInputsNames.name );
					if( chk != elmInput && chk.value == strCName ){
						err = validationErrMode.d;
						break;
					}
				};
			};

			if( err != validationErrMode.ok  ){
				var msg = _msg.getMessage( "colmng_W0" );
				var mainMsgObj =  msg.main ;
				mainMsgObj.discription = msg.sub[ err ];
				_disp.showMessage( mainMsgObj , function(){ elmInput.focus(); } );
				return false;
			};
			return true;
		};

		//pk変更時に呼び出すこと。
		//唯一のPKがある場合に、該当列のdataset.anをtrueにする。
		var checkColUseAN = function(){
			var targTr = null;
			for( var i = 0 ; _U.isIdx( i , colTbody.children ) ; i ++ ){
				var tr = colTbody.children[i];
				var inpPk = getColInputs( tr , colInputsNames.pk );
				if( ! inpPk.checked ){
					delete tr.dataset.an;
				}else{
					if( targTr ){
						delete tr.dataset.an;
						delete targTr.dataset.an;
					}else{
						targTr = tr;
						targTr.dataset.an = true;
					};
				};
			};
		};
		var chgTypeCtrl = function( slctType ){
			var tr = _D.par( _D.par( slctType ) );
			tr.dataset.type = slctType.value;
		};

		var uqColValidate = function( elmInput ){
			var validationErrMode ={
				n:"n",
				d:"d",
				ok:"ok"
			};
			var err = validationErrMode.ok;

			if( elmInput.selectedOptions.length == 0 ){
				var err = validationErrMode.n;
			};
			if( err == validationErrMode.ok  ){
				for( var i = 0 ; _U.isIdx( i , uqTbody.children ) ; i ++ ){
					var slct = getUqInputs( uqTbody.children[i] , uqInputsNames.slctCol );
					if( slct == elmInput )continue;
					var big,sml;
					if( elmInput.selectedOptions.length >= slct.selectedOptions.length ){
						big = elmInput.selectedOptions;
						sml = slct.selectedOptions;
					}else{
						big = slct.selectedOptions;
						sml = elmInput.selectedOptions;
					};
					var contain = true;

					for( var j = 0 ; _U.isIdx( j , sml ) ; j ++ ){
						var find = false;
						for( var k = 0 ; _U.isIdx( k , big ) ; k ++ ){
							if( sml[ j ].value == big[k].value ){
								find = true;
								break;
							};
						};
						if( ! find ){
							contain = false;
							break;
						};
					};
					if( contain ){
						err = validationErrMode.d;
						break;
					};
				};
			};
			if( err != validationErrMode.ok  ){
				var msg = _msg.getMessage( "colmng_W2" );
				var mainMsgObj =  msg.main ;
				mainMsgObj.discription = msg.sub[ err ];
				_disp.showMessage( mainMsgObj , function(){ _D.rC(_D.par(_D.par(elmInput))); } );
				return false;
			};
			return true;
		};




		var chgEditOpt = function( btn ){
			var isOpt = true;
			if( btn.className == "_umng_btn_edit_" ){
				isOpt = false;
			};

			var objBl = {
				"_cmng_btn_opt_edit_":{
					"commit options":false,
					"edit options":true
				},
				"_umng_btn_edit_":{
					"commit":false,
					"edit":true
				}
			};
			var bl = objBl[ btn.className ][ btn.value ];

			if( isOpt ){
				var myColTr = _D.par( _D.par( _D.par( btn ) ) );
			}else{
				var myUqTr = _D.par( _D.par( btn ) );
			};
			//入力内容をulに反映させる処理を行う。
			if( ! bl ){
				if( isOpt ){
					var txt = getColInputs( myColTr , colInputsNames.txtOpt );
					var ul = getColInputs( myColTr , colInputsNames.ulOpt );
					ul.innerHTML = "";

					var strTxt = txt.value;
					//改行コードを\r\nから\nのみに統一。改行の連続を除去、先頭、末尾の改行を除去した上で、改行で区切り配列化する。
					var ary = strTxt.replace(/\r\n/g,"\n").replace(/\n+/g,"\n").replace(/\n$/,"").replace(/^\n/,"").split("\n");
					var str = "";
					for( var i = 0 ; _U.isIdx( i , ary ) ; i ++ ){
						var wkStr = ary[ i ].trim();
						if( wkStr == ""){
							continue;
						};
						if( str != "" ){
							str +="\n";
						};
						str += wkStr;
						_D.aCAndCN( ul , nodeDef.li ).innerHTML = _U.htmlEscape( wkStr );
					};
					txt.value = str;

					unCommitChange =  unCommitChange || txt.value != txt.dataset.org;
				}else{
					var slct = getUqInputs( myUqTr , uqInputsNames.slctCol );
					if( ! uqColValidate( slct ) ){
					};
					var ul = getUqInputs( myUqTr , uqInputsNames.ulCol );
					ul.innerHTML = "";
					

					for( var i = 0 ; _U.isIdx( i , slct.selectedOptions ) ; i ++ ){
						var col = null;
						if( slct.selectedOptions[ i ].value != "" ){
							for( var j = 0 ; _U.isIdx( j , colTbody.children ) ; j ++ ){
								if( colTbody.children[ j ].dataset.id == slct.selectedOptions[ i ].value ){
									col = colTbody.children[ j ];
									break;
								};
							};
						};
						if( col ){
							var li = _D.aCAndCN( ul , nodeDef.li );
							li.innerHTML =  _U.htmlEscape( getColInputs( col , colInputsNames.name ).value );
						};
					};

					if( ! unCommitChange ){
						var str = "";
						for( var i = 0 ; _U.isIdx( i , slct.selectedOptions ) ; i ++ ){
							str += slct.selectedOptions[i].value +",";
						};
						if( slct.dataset.org != str ){
							unCommitChange = true;
						};
					};
				};
			};
			//ボタン文言を差し替える。
			for( var key in objBl[ btn.className ] ){
				if( objBl[ btn.className ][ key ] != bl ){
					btn.value = key;
				};
			};
			if( ! isOpt ){
				_D.show( getUqInputs( myUqTr , uqInputsNames.slctCol ) , bl );
				_D.show( getUqInputs( myUqTr , uqInputsNames.ulCol ) , ! bl );
			};



			if( bl ){
				_D.g( "cmng_commit" ).disabled = true;
			}else{
				if( unCommitChange ){
					chgBtnEnable();
				};
			};
			for( var i = 0 ; _U.isIdx( i , colTbody.children ) ; i ++ ){
				var trCol = colTbody.children[ i ];
				var blSelf = isOpt && ( trCol == myColTr );

				for( var key in colInputsNames ){
					var inp = getColInputs( trCol , colInputsNames[key] );
					if( ! blSelf || _U.find( key , [ "txtOpt" , "btnOpt" ] ) == -1 ){
						inp.disabled = bl;
					};
				};
			};
			_D.g( "cmng_add" ).disabled = bl;
			
			//一意制約定義
			for( var i = 0 ; _U.isIdx( i , uqTbody.children ) ; i ++ ){
				var trUq = uqTbody.children[ i ];
				var blSelf = ! isOpt && ( trUq == myUqTr );

				for( var key in uqInputsNames ){
					var inp = getUqInputs( trUq , uqInputsNames[key] );
					if( ! blSelf || _U.find( key , [ "edit" , "slctCol" ] ) == -1 ){
						inp.disabled = bl;
					};
				};
			};
			_D.g( "umng_add" ).disabled = bl;
		};


		var eventFuncs = {
			rollbackOnClick:function(){
				_disp.showMessageEz( "cmmn_101" , null , function(){_prcName_="ROLL BACK"; init();} );
			},
			commitOnClick:function(){
				_disp.showMessageEz( "cmmn_100" , null , commit );
			},
			deleteUqOnClick:function(){
				deleteTr( _D.par( _D.par( this ) ) );
			},
			deleteColOnClick:function(){
				deleteTr( _D.par( _D.par( this ) ) );
				afterChangeColTbl();
			},
			addUqOnClick:function(){
				chgBtnEnable();
				var tr = addUqTr();
				getUqInputs( tr , uqInputsNames.edit ).onclick();
				afterChangeColTbl();
			},
			addColOnClick:function(){
				chgBtnEnable();
				var tr = addColTr();
				getColInputs( tr , colInputsNames.name ).focus();
			},
			upOnClick:function(){
				chgBtnEnable();
				trMove( _D.par( _D.par( this ) ) , true );
				afterChangeColTbl();
			},
			downOnClick:function(){
				chgBtnEnable();
				trMove( _D.par( _D.par( this ) ) , false );
				afterChangeColTbl();
			},
			editOptOnClick:function(){
				chgEditOpt(this);
			},
			editUqOnClick:function(){
				chgEditOpt(this);
			},
			changeType:function(){
				chgBtnEnable();
				chgTypeCtrl( this );
			},
			autonumClick:function(){
				chgBtnEnable();
			},
			prefixOnchange:function(){
				chgBtnEnable();
			},
			suffixOnchange:function(){
				chgBtnEnable();
			},
			lengthOnblur:function(){
				var val = this.value;
				var err = val == "" || parseInt( val , 10 ) != val || val < 0 ;

				if( err ){
					var targ = this;
					_disp.showMessageEz( "colmng_W1" , null , function(){targ.focus();} );
				}else{
					chgBtnEnable();
				};
			},
			txtColNameOnBlur:function(){
				if( colNameValidate( this ) ){
					if( unCommitChange || this.value != this.dataset.org ){
						chgBtnEnable();
						afterChangeColTbl();
					};
				};
			},
			chkPkOnclick:function(){
				chgBtnEnable();
				checkColUseAN();
			},
			txtCssOnChange:function(){
				chgBtnEnable();
			}
		};


		var nodeDef ={
			mainInner :{attr:{
				tagname:"div",
				className:"_vh_sc_auto_ _cmng_"
			},children:[
				{attr:{
					tagname:"h1",
					innerHTML:"Column Manage",
					className:"_titles_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						value:"ROLLBACK CHANGES",
						disabled:true,
						id:"cmng_rollback",
						onclick:eventFuncs.rollbackOnClick,
						className:"_btn_y_ _btn_in_title_"
					}},{attr:{
						tagname:"input",
						type:"button",
						value:"COMMIT CHANGES",
						disabled:true,
						id:"cmng_commit",
						onclick:eventFuncs.commitOnClick,
						className:"_btn_y_ _btn_in_title_"
					}}
				]},{attr:{
					tagname:"table",
					id:"tbl_cmng"
				},children:[
					{attr:{
						tagname:"thead",
						id:"thead_cmng"
					},children:[
						{attr:{
							tagname:"tr",
							className:"_thead_g_",
							id:"tr_cmng_head"
						},children:[
							{attr:{
								tagname:"th",
								id:"th_cmng_col_name"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Column Name"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_cmng_col_type"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Type"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_cmng_pk"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"PK"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_cmng_option"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"optional Setting"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_cmng_css"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"CSS String"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_cmng_act"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"manage action"
								}}
							]}
						]}
					]},{attr:{
						tagname:"tbody",
						id:"tbody_cmng"
					}},{attr:{
						tagname:"tfoot",
						id:"tfoot_cmng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_cmng_foot"
						},children:[
							{attr:{
								tagname:"td",
								className:"_td_dmy_"
							}},{attr:{
								tagname:"td",
								colSpan:5
							},children:[
								{attr:{
									tagname:"input",
									type:"button",
									value:"add",
									id:"cmng_add",
									onclick:eventFuncs.addColOnClick,
									className:"_btn_adjust_"
								}}
							]}
						]}
					]}
				]},{attr:{
					tagname:"h1",
					innerHTML:"Unique Constrains",
					className:"_titles_"
				}},{attr:{
					tagname:"table",
					id:"tbl_umng"
				},children:[
					{attr:{
						tagname:"thead",
						id:"thead_umng"
					},children:[
						{attr:{
							tagname:"tr",
							className:"_thead_g_",
							id:"tr_umng_head"
						},children:[
							{attr:{
								tagname:"th",
								id:"th_umng_num"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"No"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_umng_col"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Column Names"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_umng_act"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"manage action"
								}}
							]}
						]}
					]},{attr:{
						tagname:"tbody",
						id:"tbody_umng"
					}},{attr:{
						tagname:"tfoot",
						id:"tfoot_umng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_umng_foot"
						},children:[
							{attr:{
								tagname:"td",
								className:"_td_dmy_"
							}},{attr:{
								tagname:"td",
								colSpan:2
							},children:[
								{attr:{
									tagname:"input",
									type:"button",
									value:"add",
									id:"umng_add",
									onclick:eventFuncs.addUqOnClick,
									className:"_btn_adjust_"
								}}
							]}
						]}
					]}
				]}
			]},

			typeSlct:{attr:{
				tagname:"select",
				value:1,
				className:"_cmng_slct_type_",
				onchange : eventFuncs.changeType
			}},
			uqColSlct:{attr:{
				tagname:"select",
				className:"_cmng_slct_uq_col_ _disp_none_",
				multiple : true,
				title:"The new column can not be selected until you commit."
			},children:[
			]},

			padSlct:{attr:{
				tagname:"select",
				value:0,
				className:"_cmng_slct_pad_",
				onchange : eventFuncs.changeType
			},children:[
				{attr:{
					tagname:"option",
					value:"0",
					label:"none padding"
				}},
				{attr:{
					tagname:"option",
					value:"1",
					label:"0 padding"
				}},
				{attr:{
					tagname:"option",
					value:"-1",
					label:"space padding"
				}}
			]},
			optAutonum:{attr:{
				tagname:"filedset",
				className:"_cmng_fldst_autonum_"
			},children:[
				{attr:{
					tagname:"label",
					innerHTML : "Auto Number",
					className:"_cmng_lbl_autonum_"
				}},
				{attr:{
					tagname:"input",
					type:"checkbox",
					onclick:eventFuncs.autonumClick
				}},

				{attr:{
					tagname:"label",
					innerHTML : "Prefix",
					className:"_cmng_lbl_prefix_"
				}},
				{attr:{
					tagname:"input",
					type:"text",
					onchange:eventFuncs.prefixOnchange
				}},
				{attr:{
					tagname:"label",
					innerHTML : "Suffix",
					className:"_cmng_lbl_suffix_"
				}},
				{attr:{
					tagname:"input",
					type:"text",
					onchange:eventFuncs.suffixOnchange
				}},
				{attr:{
					tagname:"label",
					innerHTML : "Length",
					className:"_cmng_lbl_length_"
				}},
				{attr:{
					tagname:"input",
					type:"number",
					value: 3 ,
					onblur:eventFuncs.lengthOnblur,
					min:0,
					max:99
				}},
				{attr:{
					tagname:"label",
					innerHTML : "Padding",
					className:"_cmng_lbl_padding_"
				}}
//				nodeDef.padSlct
			]},
			optOpts:{attr:{
				tagname:"filedset",
				className:"_cmng_fldst_options_"
			},children:[
				{attr:{
					tagname:"label",
					innerHTML : "options",
					className:"_cmng_lbl_options_"
				}},
				{attr:{
					tagname:"input",
					type:"button",
					value:"edit options",
					onclick:eventFuncs.editOptOnClick,
					className:"_cmng_btn_opt_edit_"
				}},

				{attr:{
					tagname:"ul",
					className:"_cmng_ul_options_"
				},children:[
					
				]},
				{attr:{
					tagname:"textarea",
					className:"_cmng_txtar_options_"
				},children:[
				]}
			]},

			colTr :{attr:{
				tagname:"tr",
				className:"_cmng_tr_body_"
			},children:[
				{attr:{
					tagname:"td",
					className:"_cmng_td_col_name_"
				},children:[
					{attr:{
						tagname:"input",
						type:"text",
						onblur:eventFuncs.txtColNameOnBlur,
						className:"_cmng_txt_col_name_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_cmng_td_col_type_"
				},children:[
//					nodeDef.typeSlct
				]},{attr:{
					tagname:"td",
					className:"_cmng_td_pk_"
				},children:[
					{attr:{
						tagname:"input",
						type:"checkbox",
						onclick:eventFuncs.chkPkOnclick
					}}
				]},{attr:{
					tagname:"td",
					className:"_cmng_td_option_"
				},children:[
//					nodeDef.optAutonum,
//					nodeDef.optOpts
				]},{attr:{
					tagname:"td",
					className:"_cmng_td_css_"
				},children:[
					{attr:{
						tagname:"input",
						type:"text",
						onchange:eventFuncs.txtCssOnChange,
						className:"_cmng_txt_css_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_cmng_td_act_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						onclick : eventFuncs.deleteColOnClick,
						value:"delete",
						className:"_cmng_btn_del_ _btn_y_"
					}},
					{attr:{
						tagname:"input",
						type:"button",
						value:"up",
						onclick : eventFuncs.upOnClick,
						className:"_cmng_btn_up_"
					}},
					{attr:{
						tagname:"input",
						type:"button",
						onclick : eventFuncs.downOnClick,
						value:"down",
						className:"_cmng_btn_down_"
					}}
				]}
			]},
			uqTr :{attr:{
				tagname:"tr",
				className:"_umng_tr_body_"
			},children:[
				{attr:{
					tagname:"td",
					className:"_umng_td_num_"
				},children:[
				]},{attr:{
					tagname:"td",
					className:"_umng_td_col_"
				},children:[
					{attr:{
						tagname:"ul",
						className:"_cmng_ul_uq_col_",
					},children:[
					]},
//					nodeDef.uqColSlct
				]},{attr:{
					tagname:"td",
					className:"_umng_td_act_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						value:"edit",
						onclick : eventFuncs.editUqOnClick,
						className:"_umng_btn_edit_"
					}},
					{attr:{
						tagname:"input",
						type:"button",
						onclick : eventFuncs.deleteUqOnClick,
						value:"delete",
						className:"_umng_btn_del_ _btn_y_"
					}}
				]}
			]},
			li :{attr:{
				tagname:"li"
			}},
			option :{attr:{
				tagname:"option"
			}}
		};
		nodeDef.optAutonum.children.push( nodeDef.padSlct );
		nodeDef.colTr.children[ 1 ].children.push( nodeDef.typeSlct );
		nodeDef.colTr.children[ 3 ].children.push( nodeDef.optAutonum );
		nodeDef.colTr.children[ 3 ].children.push( nodeDef.optOpts );
		nodeDef.uqTr.children[ 1 ].children.push( nodeDef.uqColSlct );

		var wkIdIdx = 0;
		var disp = function( ){
			unCommitChange = false;

			//ここまででエラーがあった場合は既に画面通知済みのはず。
			main.innerHTML = "";
			wkIdIdx = 0;

			//タイプデータ取得
			typeTbl = _sysData._getData( "type" );

			//型選択selectの定義を作成する。
			nodeDef.typeSlct.children =[];
			for( var i = 0 ; _U.isIdx( i , typeTbl ) ; i ++ ){
				nodeDef.typeSlct.children.push( {attr:{
					tagname:"option",
					label:typeTbl[i].name,
					value:typeTbl[i].id
				}});
			};

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

			//tableIDから利用するシステム表のデータを取得
			tblData = _sysData._tblFind( _sysData._getData( "tbl" ) , [{ key : "id" , value : _this.tblId }] , true )[ 0 ];
			colTable = _sysData._tblFind( _sysData._getData( "col" ) , [ { key : "tableId" , value : _this.tblId } ] );
			optTable = _sysData._tblFind( _sysData._getData( "opt" ) , [ { key : "tableId" , value : _this.tblId } ] );
			uqTable = _sysData._tblFind( _sysData._getData( "uq" , _sysData._getMode.asWTable ) , [ { key : "Table" , value : _this.tblId } ] );
			if( tblData.autoID !== null ){
				autoNumData = _sysData._tblFind( _sysData._getData( "auto" ) , [ { key : "id" , value : tblData.autoID } ] , true )[ 0 ];
			}else{
				autoNumData = null;
			};

			//各表のtbodyを取得
			colTbody = _D.g("tbody_cmng");
			uqTbody = _D.g("tbody_umng");

			//colTbodyにcol毎の要素を作成する。
			for( var i = 0 ; _U.isIdx( i , colTable ) ; i ++ ){
				addColTr( colTable[ i ]);
			};

			//uqTbodyにuq毎の要素を作成する。
			for( var i = 0 ; _U.isIdx( i , uqTable ) ; i ++ ){
				addUqTr( uqTable[ i ]);
			};

			if( _prcName_ ){
				procFin();
			};
		};
		var procFin = function(){
			_disp.showMessageEz( "cmmn_10" , [_prcName_] , null );
			_prcName_ = null;
		};


		var colInputsNames ={
			"name" : "name",
			"type" : "type",
			"pk" : "pk",
			"css" : "css",
			"del" : "del",
			"up" : "up",
			"down" : "down",
			"tdOpt" : "tdOpt",
			"an" : "an",
			"prfx" : "prfx",
			"sffx" : "sffx",
			"len" : "len",
			"pad" : "pad",
			"ulOpt":"ulOpt",
			"txtOpt":"txtOpt",
			"btnOpt":"btnOpt"
		};
		var getColInputs = function( tr , itemName ){
			switch( itemName ){
				case colInputsNames.name :
					return _D.fEC( tr.children[0] );
				case colInputsNames.type :
					return _D.fEC( tr.children[1] );
				case colInputsNames.pk :
					return _D.fEC( tr.children[2] );
				case colInputsNames.css :
					return _D.fEC( tr.children[4] );
				case colInputsNames.del :
					return _D.fEC( tr.children[5] );
				case colInputsNames.up :
					return tr.children[5].children[1];
				case colInputsNames.down :
					return _D.lEC( tr.children[5] );
				case colInputsNames.tdOpt :
					return tr.children[3] ;
				case colInputsNames.an :
					return _D.fEC(tr.children[3]).children[1] ;
				case colInputsNames.prfx :
					return _D.fEC(tr.children[3]).children[3] ;
				case colInputsNames.sffx :
					return _D.fEC(tr.children[3]).children[5] ;
				case colInputsNames.len :
					return _D.fEC(tr.children[3]).children[7] ;
				case colInputsNames.pad :
					return _D.fEC(tr.children[3]).children[9] ;
				case colInputsNames.btnOpt :
					return _D.lEC(tr.children[3]).children[1] ;
				case colInputsNames.ulOpt :
					return _D.lEC(tr.children[3]).children[2] ;
				case colInputsNames.txtOpt :
					return _D.lEC(tr.children[3]).children[3] ;
			};
			return null;
		};
		var uqInputsNames ={
			"ulCol" : "ulCol",
			"slctCol" : "slctCol",
			"edit" : "edit",
			"delete" : "delete"
		};

		var getUqInputs = function( tr , itemName ){
			switch( itemName ){
				case uqInputsNames.ulCol :
					return _D.fEC( tr.children[1] );
				case uqInputsNames.slctCol :
					return _D.lEC( tr.children[1] );
				case uqInputsNames.edit :
					return _D.fEC( tr.children[2] );
				case uqInputsNames.delete :
					return _D.lEC( tr.children[2] );

			};
			return null;
		};

		var addColTr = function( colData ){
			var tr = _D.aCAndCN( colTbody , nodeDef.colTr );
			if( ! colData ){
				colData = {
					id : "",
					name:"new_column",
					type:1,
					pk:0,
					css:""
				};
			};
			tr.dataset.id = colData.id;
			var inpNm = getColInputs( tr , colInputsNames.name );
			inpNm.value = colData.name;
			inpNm.dataset.org = colData.name;
			var inpTp = getColInputs( tr , colInputsNames.type );
			inpTp.value = colData.type;
			inpTp.dataset.org = colData.type;
			tr.dataset.type = colData.type;

			if( colData.type == 8 ){
				var myOpt = [];
				if( colData.id != ""){
					myOpt = _sysData._tblFind( optTable , [ { key : "colId" , value : colData.id } ] );
				};
				var ulOpt =  getColInputs( tr , colInputsNames.ulOpt );
				var txtOpt =  getColInputs( tr , colInputsNames.txtOpt );
				for( var i = 0 ; _U.isIdx( i , myOpt ) ; i ++ ){
					_D.aCAndCN( ulOpt , nodeDef.li ).innerHTML = _U.htmlEscape( myOpt[ i ].value );
					if( i != 0 )txtOpt.value += "\n";
					txtOpt.value += myOpt[ i ].value;
				};
				txtOpt.dataset.org = txtOpt.value;
			};

			var inpPk = getColInputs( tr , colInputsNames.pk );
			inpPk.checked = ( colData.pk == 1 );
			if(  colData.pk == 1  ){
				var pkCols = _sysData._tblFind( colTable , [ { key : "pk" , value : 1 } ] );
				if( pkCols.length == 1 ){
					tr.dataset.an = true;
					if( autoNumData ){
						var inpAn = getColInputs( tr , colInputsNames.an );
						inpAn.checked = true;
						getColInputs( tr , colInputsNames.prfx ).value = autoNumData.prefix;
						getColInputs( tr , colInputsNames.sffx ).value = autoNumData.suffix;
						getColInputs( tr , colInputsNames.len ).value = autoNumData.length;
						getColInputs( tr , colInputsNames.pad ).value = autoNumData.zeroPad;
					};
				};
			};
			var inpCss = getColInputs( tr , colInputsNames.css );
			inpCss.value = colData.css;
			inpCss.dataset.org = colData.css;
			return tr;
		};
		var addUqTr = function( uqData ){
			if( ! uqData ){
				uqData = {
					id : "",
					_children:[{
						id:""
					}]
				};
				for( var i = 0 ; _U.isIdx( i , colTbody.children ) ; i ++ ){
					if( colTbody.children[i].dataset.id != "" ){
						uqData._children[0].col = colTbody.children[i].dataset.id;
						break;
					};
				};
			};
			var tr = _D.aCAndCN( uqTbody , nodeDef.uqTr );
			tr.dataset.id = uqData.id;

			chgUqCol( tr , colTable , uqData._children );

			return tr;
		};
	};

	constructorF.prototype = _protoEachDisp;
	_eachDisp.CMng = new constructorF();
}();
delete _;
