JavaLikeCalc.JavaScript
function messByLang(iMess) {
	if((tVl=iMess.match("^"+(this.ownerSess().reqLang().length?this.ownerSess().reqLang():SYS.lang())+":(.+)$","m")).length)
		return tVl[1];
	return iMess.parseLine(0);
}

if(f_start) {
	toUpdate = false;
	fMax = 5;
	btClassEdit_en = dataEditable && classEditable;
	if(classFix.length)	classNm_text = class = classFix;
	else classSel_en = true;
	btEdit_en = dataEditable;
	colVars = new Object();
	btClassEdit_value = btEdit_value = false;
	wUser = "";
}

if(wUser != this.ownerSess().reqUser()) {
	wUser = this.ownerSess().reqUser();

	btClassEdit_value = btEdit_value = false;
	toUpdate = true;
}

//Common events process
for(off = 0; (sval=event.parse(0,"\n",off)).length; ) {
	//this.messInfo("TEST 01: Event="+sval);
	if(sval == "ws_BtToggleChange:/btClassEdit") {
		classEd_en = !classFix.length && btClassEdit_value;
		classSel_en = !classFix.length && !btClassEdit_value;
		toUpdate = true;
		itDel_active = itCopy_active = false;
	}
	else if(sval == "ws_BtToggleChange:/btEdit")	toUpdate = true;
	else if(sval == "ws_CombChange:/classSel")	{ class = ((tVl=classSel_value.match("\\((.+)\\)$")).length) ? tVl[1] : classSel_value; toUpdate = true; colVars = new Object(); }
	else if(sval == "ws_LnAccept:/classEd")		{ class = classEd_value; toUpdate = true; colVars = new Object(); }
}

if((f_start || toUpdate) /*&& !classFix.length*/) {
	clsLs = SYS.BD.nodeAt(db,".").SQLReq("SELECT DISTINCT CLASS FROM `classes`;");
	classSel_items = "";
	for(iR = 1; iR < clsLs.length; iR++) {
		clsNm = SYS.BD.nodeAt(db,".").SQLReq("SELECT `NAME` FROM `classes` WHERE `CLASS`='"+clsLs[iR][0]+"' AND `ID`='NAME';");
		classSel_items = (classSel_items.length?"\n":"") + ((clsNm.length==2)?messByLang(clsNm[1][0]) + " ("+clsLs[iR][0]+")":clsLs[iR][0]);
	}
	classEd_cfg = classSel_items;

	clsNm = SYS.BD.nodeAt(db,".").SQLReq("SELECT `NAME` FROM `classes` WHERE `CLASS`='"+class+"' AND `ID`='NAME';");
	classNm_text = classSel_value = classEd_value = class;
	if(clsNm.length == 2)	classNm_text = classSel_value = messByLang(clsNm[1][0]) + " ("+class+")";

	dataTbl_geomY = btClassEdit_value ? lab_fltr_geomY : fltrCol0_geomY + fltrCol0_geomH;
	dataTbl_geomH = itDel_geomY - dataTbl_geomY + ((btClassEdit_value||btEdit_value)?0:itDel_geomH);

	//Apply filter vertical offset and size
	if(!btClassEdit_value) {
		for(iF = fMax-1; iF > 0 && !this["fltr"+iF].attr("en"); iF--) ;
		dataTbl_geomY = this["fltr"+iF].attr("geomY") + this["fltr"+iF].attr("geomH");
		dataTbl_geomH = itDel_geomY + (btEdit_value?0:itDel_geomH) - dataTbl_geomY;
	}

	btClassEdit_active = class.length;
	itCopy_en = !btClassEdit_value;
}

//The class edition mode
if(btClassEdit_value) {
	SYS.BD.nodeAt(db,".").SQLReq("CREATE TABLE IF NOT EXISTS `classes` (`CLASS` varchar(20) DEFAULT '', `ID` varchar(20) DEFAULT '', `NAME` text, `TP` varchar(100) DEFAULT '', `TBL` varchar(100) DEFAULT '', `FILTER` text, PRIMARY KEY (`CLASS`,`ID`));");

	//Updation the main table
	if(f_start || toUpdate) {
		// Requesting the table structure
		dataTbl = SYS.BD.nodeAt(db,".").SQLReq("SELECT `ID`,`NAME`,`TP`,`TBL`,`FILTER` FROM `classes` WHERE `CLASS`='"+class+"';");
		dataTbl_items = "<tbl sel='row' colsWdthFit='0' hHdrVis='1' vHdrVis='1'>\n";
		for(iR = 0; iR < dataTbl.length; iR++) {
			dataTbl_items += iR ? "<r>" : "<h>";
			for(iC = 0; iC < dataTbl[iR].length; iC++) {
				itVl = dataTbl[iR][iC];
				tNm = "s", opt = "";
				if(iR == 0)	 {
					opt += " edit='1'";
					if(itVl == "ID")				itVl = tr("Identifier");
					else if(itVl == "NAME")	itVl = tr("Name");
					else if(itVl == "TP")		itVl = tr("Type");
					else if(itVl == "TBL")		itVl = tr("Table properties");
					else if(itVl == "FILTER")	itVl = tr("Filter");
				}
				else {
				    if(iC == 1)	tNm = "t";
				    if(itVl == "<NULL>")	itVl = "";
				}
				dataTbl_items += "<"+tNm+opt+">"+SYS.strEncode(itVl,"HTML")+"</"+tNm+">";
			}
			dataTbl_items += iR ? "</r>\n" : "</h>\n";
		}
		dataTbl_items += "</tbl>";

		toUpdate = false;
	}

	//Class edition events process
	for(off = 0; (sval=event.parse(0,"\n",off)).length; ) {
		//this.messInfo("TEST 01: Event="+sval);
		itDel_active = itCopy_active = dataTbl_value.length;
		if(sval == "ws_BtRelease:/itAdd") {
			// Creation the empty data table with the default structure
			if(dataTbl.length == 1)
				SYS.BD.nodeAt(db,".").SQLReq("CREATE TABLE IF NOT EXISTS `sh_"+class+"` (`ID` INT AUTO_INCREMENT, `NAME` varchar(200) DEFAULT '', `DSCR` text, PRIMARY KEY (`ID`));");
			SYS.BD.nodeAt(db,".").SQLReq("INSERT INTO `classes` (`CLASS`,`ID`,`NAME`,`TP`) VALUES ('"+class+"','NewItem','"+tr("New item")+"','varchar(100)');");
			SYS.BD.nodeAt(db,".").SQLReq("ALTER TABLE `sh_"+class+"` ADD `SP_NewItem` varchar(100) DEFAULT '';");
			toUpdate = true;
		}
		else if(sval == "dlg_Apply:/itDel") {
		//else if(sval == "ws_BtRelease:/itDel") {
			SYS.BD.nodeAt(db,".").SQLReq("DELETE FROM `classes` WHERE `ID`='"+dataTbl_value+"';");
			if(dataTbl.length > 2)	SYS.BD.nodeAt(db,".").SQLReq("ALTER TABLE `sh_"+class+"` DROP `SP_"+dataTbl_value+"`;");
			else SYS.BD.nodeAt(db,".").SQLReq("DROP TABLE `sh_"+class+"`;");
			toUpdate = true;
		}
		else if(sval.slice(0,12) == "ws_TableEdit") {
			col = sval.parse(0,"_",13).toInt(); row = sval.parse(1,"_",13).toInt();
			colID = dataTbl[0][col];
			if(!SYS.BD.nodeAt(db,".").SQLReq("UPDATE `classes` SET `"+colID+"`='"+SYS.strEncode(dataTbl_set,"SQL")+"' WHERE `CLASS`='"+class+"' AND `ID`='"+dataTbl[row+1][0]+"';").err.length) {
				if(colID == "ID") SYS.BD.nodeAt(db,".").SQLReq("ALTER TABLE `sh_"+class+"` CHANGE `SP_"+dataTbl[row+1][0]+"` `SP_"+dataTbl_set+"` "+dataTbl[row+1][2].parse(0,":")+";");	
				else if(colID == "TP") SYS.BD.nodeAt(db,".").SQLReq("ALTER TABLE `sh_"+class+"` MODIFY `SP_"+dataTbl[row+1][0]+"` "+dataTbl_set.parse(0,":")+";");	
			}
			toUpdate = true;
		}
	}

	return;
}

//The data representing mode
if(f_start || toUpdate) {
	// Initiate the filter
	for(iF = 0; iF < fMax; iF++)
		this["fltrCol"+iF].attrSet("items", tr("Disabled")+"\n"+tr("Name")+" (NAME)\n"+tr("Description")+" (DSCR)");

	// Requesting the table structure
	clsLs = SYS.BD.nodeAt(db,".").SQLReq("SELECT `ID`,`NAME`,`TP`,`TBL`,`FILTER` FROM `classes` WHERE `CLASS`='"+class+"';");
	clsLsO = new Object();	
	for(iR = 1; iR < clsLs.length; iR++) {
		clsLsO[clsLs[iR][0]] = tO = new Object();
		tO.name = ((tO.name=clsLs[iR][1]) == "<NULL>" || !tO.name.length) ? clsLs[iR][0] : messByLang(tO.name);
		tO.tp = clsLs[iR][2];
		tO.tbl = clsLs[iR][3];
		if((tO.fltr=clsLs[iR][4]) == "<NULL>")	tO.fltr = "";

		for(iF = 0; iF < fMax; iF++)
			this["fltrCol"+iF].attrSet("items", this["fltrCol"+iF].attr("items")+"\n"+tO.name+" (SP_"+clsLs[iR][0]+")");
	}

	//Updation the main table
	// Where prepairing for the filter
	for(wherePart = "", iF = 0; iF < fMax; iF++) {
		if(!(tVl=this["fltr"+iF].attr("value")).length)	continue;
		itVl = this["fltrCol"+iF].attr("value").match("\\((.+)\\)")[1];
		wherePart += (wherePart.length?"AND":"") + " `"+itVl+"` REGEXP '"+tVl+"' ";
		if(!colVars[itVl].isEVal())	colVars[itVl]["<lock>"] = true;
	}
	for(var iC in colVars)
		if(colVars[iC]["<lock>"] == true)	delete colVars[iC]["<lock>"];
		else delete colVars[iC];
	// Same requesting
	dataTbl = SYS.BD.nodeAt(db,".").SQLReq("SELECT * FROM `sh_"+class+"` "+(wherePart.length?"WHERE"+wherePart:"")+"ORDER BY 'ID';");
	dataTbl_items = "<tbl sel='row' sortEn='"+(btEdit_value?0:1)+"' colsWdthFit='0' hHdrVis='1' vHdrVis='1'>\n";
	colTps = new Object();
	for(iR = 0; iR < dataTbl.length; iR++) {
		dataTbl_items += iR ? "<r>" : "<h>";
		for(iC = 0; iC < dataTbl[iR].length; iC++) {
			itVl = dataTbl[iR][iC];
			opt = "";
			if(iR == 0) {
				if(itVl == "ID")	opt += " width='0px'";
				else {
					opt += btEdit_value ? " edit='1'" : "";
					if(itVl == "NAME")	{ itVl = tr("Name"); opt += " width='170px' align='center'"; }
					else if(itVl == "DSCR")	{ colTps[iC] = "t"; itVl = tr("Description"); opt += " width='350px'"; }
					else if(itVl.slice(0,3) == "SP_" && !(tVl=clsLsO[itVl.slice(3)]).isEVal()) {
						if((tVl.fltr == "index" || tVl.fltr.indexOf("list") == 0) && colVars[itVl].isEVal()) {
							colVars[itVl] = new Object();
							colVars[itVl].ls = new Object();
							colVars[itVl].fltr = tVl.fltr;
						}
						itVl = tVl.name;
						if(tVl.tp.search("bool","i") >= 0)		colTps[iC] = "b";
						else if(tVl.tp.search("int","i") >= 0)	colTps[iC] = "i";
						else if(tVl.tp.search("(float|double)","i") >= 0) colTps[iC] = "r";
						else if(tVl.tp.search("text","i") >= 0)	colTps[iC] = "t";
						if((tVl2=tVl.tbl.parse(0,":")).length)	opt += " align='"+tVl2+"'";
						if((tVl2=tVl.tbl.parse(1,":")).length)	opt += " width='"+tVl2+"'";
						//this.messInfo("iC="+iC+"; tp="+tVl.tp+" = "+colTps[iC]+"; tVl.fltr="+tVl.fltr);
					}
				}
				dataTbl_items += "<s"+opt+">"+SYS.strEncode(itVl,"HTML")+"</s>";
			}
			else {
				if(itVl == "<NULL>")	itVl = "";
				if(!colTps[iC].isEVal())	dataTbl_items += "<"+colTps[iC]+opt+">"+SYS.strEncode(itVl,"HTML")+"</"+colTps[iC]+">";
				else dataTbl_items += "<s"+opt+">"+SYS.strEncode(itVl,"HTML")+"</s>";
				if(!(tVl2=colVars[dataTbl[0][iC]]).isEVal())	{
					if(tVl2.fltr == "index" && itVl.length)	tVl2.ls[itVl] = true;
					else if(tVl2.fltr.indexOf("list") == 0) {
						tLs = itVl.split(tVl2.fltr[4]);
						for(iLs = 0; iLs < tLs.length; iLs++)
							tVl2.ls[tLs[iLs].trim()] = true;
					}
				}
			}
		}
		dataTbl_items += iR ? "</r>\n" : "</h>\n";
	}
	dataTbl_items += "</tbl>";

	//Filter's selection list update
	for(iF = 0; iF < fMax; iF++) {
		iFCo = this["fltrCol"+iF]; iFo = this["fltr"+iF];
		fCol = iFCo.attr("value").match("\\((.+)\\)")[1];
		//this.messInfo("iF="+iF+"; fCol="+fCol+"; colVars[fCol]="+colVars[fCol]);
		if((tO=colVars[fCol]).isEVal())	continue;
		tO = tO.ls;
		fCfg = "";
		fCfgPref = iFo.attr("value");
		for(sumVl = "", tVl1 = "", off = 0; (tVl=fCfgPref.parse(0,"|",off)).length; tVl1 = tVl)
			if(tVl1.length) { sumVl += (sumVl.length?"|":"")+tVl1; fCfg += sumVl + "\n"; }
		for(var iFv in tO)
			if(iFv.length && fCfgPref.indexOf(iFv) < 0)
				fCfg += (fCfgPref.length?fCfgPref+"|":"") + iFv + "\n";
			//fCfg += iFv+"\n";
		iFo.attrSet("cfg", fCfg);
	}

	toUpdate = false;
}

//Representing events process
for(off = 0; (sval=event.parse(0,"\n",off)).length; ) {
	//this.messInfo("TEST 01: Event="+sval+" : "+dataTbl_value);
	itDel_active = itCopy_active = dataTbl_value.length;
	if(sval == "ws_BtRelease:/itAdd") {
		nmLs = vlLs = "";
		//Prepairing the filter values
		for(iF = 0; iF < fMax; iF++) {
			if(!(tVl=this["fltr"+iF].attr("value")).length)	continue;
			nmLs += (nmLs.length?",":"") + "`"+this["fltrCol"+iF].attr("value").match("\\((.+)\\)")[1]+"`";
			vlLs += (vlLs.length?",":"") + "'"+tVl+"'";
		}
		if(nmLs.indexOf("`NAME`") < 0)	{ nmLs += (nmLs.length?",":"") + "`NAME`"; vlLs += (vlLs.length?",":"") + "'"+tr("New item")+"'"; }
		SYS.BD.nodeAt(db,".").SQLReq("INSERT INTO `sh_"+class+"` ("+nmLs+") VALUES ("+vlLs+");");
		toUpdate = true;
	}
	else if(sval == "dlg_Apply:/itDel") {
	//else if(sval == "ws_BtRelease:/itDel") {
		SYS.BD.nodeAt(db,".").SQLReq("DELETE FROM `sh_"+class+"` WHERE `ID`='"+dataTbl_value+"';");
		toUpdate = true;
	}
	else if(sval == "ws_BtRelease:/itCopy") {
		for(colLs = "", iC = 1; iC < dataTbl[0].length; iC++)
			colLs += (colLs.length?", ":"") + "`"+dataTbl[0][iC]+"`";
		SYS.BD.nodeAt(db,".").SQLReq("INSERT INTO `sh_"+class+"` ("+colLs+") (SELECT "+colLs+" FROM `sh_"+class+"` WHERE `ID` = "+dataTbl_value+");");
		toUpdate = true;
	}
	else if(sval.slice(0,12) == "ws_TableEdit") {
		col = sval.parse(0,"_",13).toInt(); row = sval.parse(1,"_",13).toInt();
		SYS.BD.nodeAt(db,".").SQLReq("UPDATE `sh_"+class+"` SET `"+dataTbl[0][col]+"`='"+SYS.strEncode(dataTbl_set,"SQL")+"' WHERE `"+dataTbl[0][0]+"`='"+dataTbl[row+1][0]+"';");
		//toUpdate = true;	//!!!! To prevent the spare flicking
	}
	else if(sval.slice(0,22) == "ws_CombChange:/fltrCol") {
		fN = sval.slice(22).toInt();
		fAct = !this["fltrCol"+fN].attr("value").match("\\((.+)\\)")[1].isEVal();
		this["fltr"+fN].attrSet("active", fAct).attrSet("value", "").attrSet("cfg", "");
		if(fAct && (fN+1) < fMax && !this["fltrCol"+(fN+1)].attr("en")) {
			this["fltrCol"+(fN+1)].attrSet("en", true);
			this["fltr"+(fN+1)].attrSet("en", true).attrSet("active", false).attrSet("value", "");
		}
		else for(iF = fMax-1; !fAct && iF >= fN; iF--)
			if(iF < (fMax-1)) {
				this["fltrCol"+(iF+1)].attrSet("en", false).attrSet("value", tr("Disabled"));
				this["fltr"+(iF+1)].attrSet("en", false).attrSet("active", false).attrSet("value", "");
			}

		toUpdate = true;
	}
	else if(sval.slice(0,17) == "ws_LnAccept:/fltr")	toUpdate = true;
}
