JavaLikeCalc.JavaScript
if(f_start) save_active = false;
progLbUpdt = progSelPrg = progSelCom = progSelTp = false;
var comOp = "";

//Events for commands process
for(ev_rez = "", off = 0; (evCur=event.parse(0,"\n",off)).length; ) {
	//SYS.messInfo(this.nodePath(),"Event: "+evCur);
	if(evCur == "ws_BtPress:/progAdd") {
		if(!progNm_value.length) progNm_value = tr("New program");
		SYS.BD.nodeAt(dbDB,".").SQLReq("INSERT INTO "+dbProgs+" (name) VALUES ('"+progNm_value+"');");
		lib_value = progNm_value;
		progLbUpdt = progSelPrg = progSelCom = true; prog_value = "";
	}
	else if(evCur == "ws_BtPress:/progCopy") {
		if(!progNm_value.length) progNm_value = tr("New program");
		rez = SYS.BD.nodeAt(dbDB,".").SQLReq("SELECT prgTxt FROM "+dbProgs+" WHERE name='"+lib_value+"';");
		if(rez.length > 1) {
			SYS.BD.nodeAt(dbDB,".").SQLReq("INSERT INTO "+dbProgs+" (name, prgTxt) VALUES ('"+progNm_value+"', '"+rez[1][0].replace("'","''")+"');");
			lib_value = progNm_value;
			progLbUpdt = progSelPrg = progSelCom = true; prog_value = "";
		}
	}
	else if(evCur == "ws_BtPress:/progRename") {
		SYS.BD.nodeAt(dbDB,".").SQLReq("UPDATE "+dbProgs+" SET name='"+progNm_value+"' WHERE name='"+lib_value+"';");
		lib_value = progNm_value;
		progLbUpdt = progSelPrg = progSelCom = true; prog_value = "";
	}
	else if(evCur == "dlg_Apply:/progDel" && lib_value.length) {
	//else if(evCur == "ws_BtPress:/progDel" && lib_value.length) {
		SYS.BD.nodeAt(dbDB,".").SQLReq("DELETE FROM "+dbProgs+" WHERE name='"+lib_value+"';");
		lib_value = "";
		progLbUpdt = progSelPrg = true;
	}
	else if(evCur == "ws_TreeChange:/lib")	{ progSelPrg = progSelCom = true; prog_value = ""; }
	else if(evCur == "ws_BtPress:/comAdd")	comOp = "add";
	else if(evCur == "ws_BtPress:/comIns")	comOp = "ins";
	else if(evCur == "ws_BtPress:/comInAdd")	comOp = "inAdd";
	else if(evCur == "ws_BtPress:/comDel") comOp = "del";
	else if(evCur == "ws_BtPress:/comUp")	comOp = "up";
	else if(evCur == "ws_BtPress:/comDwn") comOp = "dwn";
	else if(evCur == "ws_TreeChange:/prog"){ progSelCom = true; save_active = false; }
	else if(evCur == "ws_CombChange:/type"){ progSelTp = true; save_active = true; }
	else if(evCur == "ws_BtPress:/save" && prog_value.length) {
		rez = SYS.BD.nodeAt(dbDB,".").SQLReq("SELECT prgTxt FROM "+dbProgs+" WHERE name='"+lib_value+"';");
		if(rez.length > 1) {
			comTree = selNd = SYS.XMLNode("prg"); comTree.load(rez[1][0]);
			parentPath = "";
			for(offPV = 0; (vIt=prog_value.parsePath(0,offPV)).length; ) {
				if(offPV < prog_value.length) parentPath += "/"+vIt;
				selNd = selNd.childGet((i_c=vIt.toInt()));
			}
			if(selNd) {
				selNd.setAttr("id", type_value);
				selNd.setAttr("name", name_value);
				selNd.setAttr("descr", descr_value);
				selNd.setAttr("backgrnd", backgrnd_value);
				for(i_a = 1; i_a <= 5; i_a++) {
					argObj = this["arg"+i_a];
					argVal = argObj.attr("value");
					if(!argObj.attr("min").isEVal()) argVal = max(argVal,argObj.attr("min"));
					if(!argObj.attr("max").isEVal()) argVal = min(argVal,argObj.attr("max"));
					selNd.setAttr("arg"+i_a, argVal);
				}
				SYS.BD.nodeAt(dbDB,".").SQLReq("UPDATE "+dbProgs+" SET prgTxt='"+comTree.save().replace("'","''")+"' WHERE name='"+lib_value+"';");
				prog_value = parentPath+"/"+i_c+":"+(name_value.length?name_value:type_value);
				//SYS.messInfo(this.nodePath(),"prog_value: "+prog_value);
				progSelPrg = true;
			}
		}
		save_active = false;
	}
	else if(evCur == "ws_BtPress:/progExport" && lib_value.length) {
		rez = SYS.BD.nodeAt(dbDB,".").SQLReq("SELECT prgTxt FROM "+dbProgs+" WHERE name='"+lib_value+"';");
		if(rez.length > 1) {
			expTree = SYS.XMLNode("OpenSCADA_Prescr");
			expTree.childAdd("prg").load(rez[1][0]);
			expTree.childGet(0).setAttr("id",lib_value);
			progExport_value = tr("Prescription-Program")+" (*.prscr)|"+tr("Prescription file")+"|"+lib_value.replace("/","_")+".prscr\n"+expTree.save(0x05);
		}
	} 
	//else if(evCur == "ws_BtPress:/progImport")
	else if(evCur == "ws_BtLoad:/progImport" && progImport_value.length && (pCtx=progImport_value.indexOf("\n")) > 0)
	{
		expTree = SYS.XMLNode("OpenSCADA_Prescr");
		expTree.load(progImport_value.slice(pCtx+1));
		progImport_value = progImport_value.slice(0,pCtx);
		// Save all recors to DB
		for(i_p = 0; i_p < expTree.childSize(); i_p++) {
			treeNd = expTree.childGet(i_p);
			if(treeNd.name() != "prg") continue;
			if(lib_items.search(treeNd.attr("id")+"(\n|\\z)") < 0)
				SYS.BD.nodeAt(dbDB,".").SQLReq("INSERT INTO "+dbProgs+" (name,prgTxt) VALUES ('"+treeNd.attr("id")+"','"+treeNd.save().replace("'","''")+"');");
			else
				SYS.BD.nodeAt(dbDB,".").SQLReq("UPDATE "+dbProgs+" SET prgTxt='"+treeNd.save().replace("'","''")+"' WHERE name='"+treeNd.attr("id")+"';");	
		}
		progLbUpdt = progSelPrg = progSelCom = true;
	}
	else if(evCur == "ws_LnAccept:/name" || evCur == "ws_TxtAccept:/descr" || evCur == "ws_ChkChange:/backgrnd" || (rez=evCur.match("ws_LnAccept:\\/arg(\\d)")).length)	save_active = true;
	else if((rez=evCur.match("ws_FocusOut:\\/arg(\\d)")).length) {
		argObj = this["arg"+rez[1]];
		argVal = argObj.attr("value");
		if(!argObj.attr("min").isEVal()) argVal = max(argVal,argObj.attr("min"));
		if(!argObj.attr("max").isEVal()) argVal = min(argVal,argObj.attr("max"));
		argObj.attrSet("value",argVal);
	}
	else ev_rez += (evCur+"\n");
}
event = ev_rez;

//Get programms into library list
if(f_start || progLbUpdt) {
	rez = SYS.BD.nodeAt(dbDB,".").SQLReq("SELECT name FROM "+dbProgs+";");
	lib_items = "";
	for(i_p = 1; i_p < rez.length; i_p++)
		lib_items += ((i_p==1)?"":"\n")+rez[i_p][0];
	lib_items = lib_items.split("\n").sort().join("\n");
	progNm_cfg = lib_items;
}
progCopy_active = progRename_active = progDel_active = progExport_active = comAdd_active = lib_value.length;

//Command operations process
if(comOp.length && lib_value.length) {
	rez = SYS.BD.nodeAt(dbDB,".").SQLReq("SELECT prgTxt FROM "+dbProgs+" WHERE name='"+lib_value+"';");
	if(rez.length > 1) {
		comTree = selNd = SYS.XMLNode("prg");
		comTree.load(rez[1][0]);
		parentPath = "";
		if(prog_value.length) {
			for(offPV = 0; (vIt=prog_value.parsePath(0,offPV)).length; ) {
				if(offPV < prog_value.length) parentPath += "/"+vIt;
				selNd = selNd.childGet((i_c=vIt.toInt()));
			}
			selNd = selNd.parent();
		}
		if(comOp == "add") {
			selNd.childAdd("com").setAttr("id",tr("New command"));
			prog_value = parentPath+"/"+(selNd.childSize()-1)+":"+tr("New command");
		}
		else {
			// Get selected command node and position
			if(i_c < selNd.childSize()) {
				if(comOp == "inAdd") {
					selNd.childGet(i_c).childAdd("com").setAttr("id",tr("New command"));
					prog_value = parentPath+"/"+i_c+":"+selNd.childGet(i_c).attr("id")+"/"+(selNd.childGet(i_c).childSize()-1)+":"+tr("New command");
				}
				else if(comOp == "ins") {
					selNd.childIns(i_c,"com").setAttr("id",tr("New command"));
					prog_value = parentPath+"/"+i_c+":"+tr("New command");
				}
				else if(comOp == "del") {
					selNd.childDel(i_c);
					if(!selNd.childSize())	prog_value = parentPath;
					else {
						if(i_c >= selNd.childSize()) i_c = selNd.childSize()-1;
						prog_value = parentPath+"/"+i_c+":"+selNd.childGet(i_c).attr("id");
					}
				}
				else if((comOp == "up" && i_c) || (comOp == "dwn" && i_c < (selNd.childSize()-1))) {
					storeNd = selNd.childGet(i_c);
					selNd.childDel(i_c);
					if(comOp == "up") i_c--; else i_c++;
					selNd.childIns(i_c,storeNd);
					prog_value = parentPath+"/"+i_c+":"+storeNd.attr("id");
				}
			}
		}

		//Save changes
		SYS.BD.nodeAt(dbDB,".").SQLReq("UPDATE "+dbProgs+" SET prgTxt='"+comTree.save().replace("'","''")+"' WHERE name='"+lib_value+"';");
		progSelPrg = progSelCom = true;
	}
}

//Get commands of selected program into commands list
if(f_start || progSelPrg) {
	prog_items = "";
	if(lib_value.length) {
		rez = SYS.BD.nodeAt(dbDB,".").SQLReq("SELECT prgTxt FROM "+dbProgs+" WHERE name='"+lib_value+"';");
		if(rez.length > 1) {
			comTree = SYS.XMLNode("prg");
			comTree.load(rez[1][0]);
			curNd = comTree; curPos = 0; curPath = ""; comLevs = new Array();
			for( ; true; curPos++) {
				if(curPos >= curNd.childSize()) {
					if(!comLevs.length) break;
					stObj = comLevs.pop();
					curNd = curNd.parent();
					curPos = stObj.curPos; curPath = stObj.curPath;
					continue;
				}
				prog_items += (prog_items.length?"\n":"")+(wPath=curPath+"/"+curPos+":"+curNd.childGet(curPos).attr(curNd.childGet(curPos).attr("name").length?"name":"id"));
				if(curNd.childGet(curPos).childSize()) {
					curNd = curNd.childGet(curPos);
					comLevs.push((stObj=new Object()));
					stObj.curPos = curPos; stObj.curPath = curPath;
					curPos = -1; curPath = wPath;
					continue;
				}
			}
		}
	}
	labProg_arg0val = progNm_value = lib_value;
}
comIns_active = comInAdd_active = comDel_active = comUp_active = comDwn_active = prog_value.length;

//Get command properties
if(f_start || progSelCom || progSelTp) {
	if(!progSelTp) name_value = type_value = descr_value = "", backgrnd_value = false;
	for(i_a = 1; i_a <= 5; i_a++) {
		this["labArg"+i_a].attrSet("en",false).attrSet("text","");
		this["arg"+i_a].attrSet("en",false).attrSet("value","");
	}

	// Types/commands list load
	if(f_start && !prExtComLs.isEVal()) {
		type_items = descr_value = ""; backgrnd_value = false;
		for(var c_nm in prExtComLs)
			if(c_nm != "Error" && c_nm != "Stop")
				type_items += (type_items.length?"\n":"")+c_nm;
	}

	if(prog_value.length) {
		selNd = false;
		rez = SYS.BD.nodeAt(dbDB,".").SQLReq("SELECT prgTxt FROM "+dbProgs+" WHERE name='"+lib_value+"';");
		if(rez.length > 1) {
			selNd = SYS.XMLNode("prg"); selNd.load(rez[1][0]);
			for(off = 0; (vIt=prog_value.parsePath(0,off)).length; ) selNd = selNd.childGet(vIt.toInt());
		}
		if(!progSelTp && selNd) {
			name_value = selNd.attr("name");
			type_value = selNd.attr("id");
			descr_value = selNd.attr("descr");
			backgrnd_value = selNd.attr("backgrnd");
		}
		// Load command argument's names
		if(!prExtComLs.isEVal() && !prExtComLs[type_value].isEVal()) {
			if((progSelTp || !descr_value.length) && (descr_value=prExtComLs[type_value]["descr"]).isEVal()) descr_value = "";
			for(i_a = 1; i_a <= 5; i_a++) {
				argLab = prExtComLs[type_value]["arg"+i_a];
				if(argLab.isEVal() || !argLab.length)	continue;
				off = 0;
				this["labArg"+i_a].attrSet("en",true).attrSet("text",argLab.parse(0,"|",off));
				argBord = argLab.parse(0,"|",off);
				this["arg"+i_a].attrSet("min",argBord.length?argBord.toReal():EVAL_REAL);
				argBord = argLab.parse(0,"|",off);
				this["arg"+i_a].attrSet("max",argBord.length?argBord.toReal():EVAL_REAL);
				this["arg"+i_a].attrSet("en",true);
			}
		}

		// Load arguments' values
		if(!progSelTp && selNd)
			for(i_a = 1; selNd && i_a <= 5; i_a++) this["arg"+i_a].attrSet("value",selNd.attr("arg"+i_a));
	}
}
type_active = prog_value.length;