home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Source / Webmidas / xmldb.js < prev    next >
Text File  |  1999-08-11  |  23KB  |  779 lines

  1. //Copyright Inprise Corporation 1999, all rights reserved, to be used with Borlands Midas-technology only,
  2. //unless special permission has been given.
  3. var xmldb_vers="1.0";
  4. var xml_ready=false;
  5. var szRowState="RowState",szUni="string.uni",szNested="nested",szTrue="true",szFalse="false",szDel="2",szNew="4",szOrg="1",szMod="8",szDetUpd="64";
  6. var szErr_HasDets="Cannot delete master record with details.";
  7. var szErr_Invalid="Invalid action";
  8. var DecPoint=".";var s= (25/10).toString();var p=s.indexOf(".");DecPoint=p>0?".":",";
  9.  
  10. new xmlRowSet(null,null,null);
  11. xmlRowSet.prototype.parent=null;
  12. xmlRowSet.prototype.linkFld=null;
  13. xmlRowSet.prototype.pDets=null;
  14. xmlRowSet.prototype.noDel=0;xmlRowSet.prototype.noIns=0;xmlRowSet.prototype.noUpd=0;
  15. //private methods
  16. xmlRowSet.prototype.InitRowSet=InitRowSet;
  17. xmlRowSet.prototype.notify=function(reason){if(this.regobjs==null)return;var j;for(j=0;j<this.regobjs.length;j++)this.regobjs[j].refr(reason);}
  18. xmlRowSet.prototype.forcepost=DsForcePost;
  19. xmlRowSet.prototype.regobj=function(obj){if(this.regobjs==null)this.regobjs=new Array();this.regobjs[this.regobjs.length]=obj;}
  20. xmlRowSet.prototype.DeltaChanges=null;
  21. xmlRowSet.prototype.resetDets=resetDets;
  22. xmlRowSet.prototype.del=delRow;
  23. xmlRowSet.prototype.ins=insRow;
  24. xmlRowSet.prototype.upd=updRow;
  25. //public functions
  26. xmlRowSet.prototype.first=function(){return this.setPos(0);}
  27. xmlRowSet.prototype.next=function(){return this.setPos(this.pos+1);}
  28. xmlRowSet.prototype.prev=function(){return this.setPos(this.pos-1);}
  29. xmlRowSet.prototype.last=function(){return this.setPos(this.RowCnt-1);}
  30. xmlRowSet.prototype.setPos=function(pos){if(pos>=this.RowCnt||pos<0||this.RowCnt==0){this.resetDets();return 1;}if(this.pos!=pos){this.pos=pos;this.resetDets();} this.notify(0);return 0;}
  31. xmlRowSet.prototype.getRow=function(pos){return this.idx.row(pos);}
  32. xmlRowSet.prototype.makeRow=makeRow;
  33. xmlRowSet.prototype.RowState=RowState;
  34. xmlRowSet.prototype.insert=dsInsert;
  35. xmlRowSet.prototype.modify=dsModify;
  36. xmlRowSet.prototype.deletex=dsDelete;
  37. xmlRowSet.prototype.undo=dsUndo;
  38. xmlRowSet.prototype.sort=function(n){this.idx.sort(n);this.first();this.notify(1);}
  39. xmlRowSet.prototype.getDelta=function(){return this.DeltaChanges.make();}
  40. xmlRowSet.prototype.Apply=null;
  41. xmlRowSet.prototype.MakePermanent=function(){this.DeltaChanges.reset();this.notify(1);this.resetDets();}
  42. xmlRowSet.prototype.BeforePost=function(a,r){return r;}
  43. xmlRowSet.prototype.AfterPost=function(a,r){return r;}
  44. xmlRowSet.prototype.OnError=function(s){alert(s);return 1;}
  45. xmlRowSet.prototype.OnNewRow=function(){return null;}
  46.  
  47. function xmlRowSet(doc, parent, linkFld) 
  48. {
  49. this.doc=doc;
  50. if(doc==null){if(parent!=null)this.doc=parent.doc; else return null;}
  51. if(this.doc==""){if(parent==null)alert("Invalid XML-packet."); return null;}
  52. this.root=null;
  53. if(parent!=null){this.parent=parent;this.linkFld=linkFld;this.InitRowSet=InitDetailRowSet;}
  54. this.MetaData=null;this.RowData=null;this.FieldCnt=0;this.FieldData=null;
  55. //public:
  56. this.pos=0;this.RowCnt=0;
  57. this.Fields=new Fields();
  58. //Init
  59. this.InitRowSet();
  60. this.pos=-1;
  61. this.first();
  62. return this;
  63. }
  64.  
  65. function InitRowSet()
  66. {
  67. if(this.doc==null||this.doc=="")return;
  68. var i;
  69. this.root=this.doc.documentElement;
  70. if(this.root==null)return;
  71. if(this.root.tagName=="DATAPACKET"){
  72.  this.MetaData=this.root.childNodes.item(0); 
  73.  this.RowData=this.root.childNodes.item(1);
  74.  this.RowCnt=this.RowData.childNodes.length;
  75.  this.FieldData=this.MetaData.childNodes.item(0);
  76.  this.FieldCnt=this.FieldData.childNodes.length;
  77.  var des;
  78.  for(i=0;i<this.FieldCnt;i++){des=new FldDes(this,this.FieldData.childNodes.item(i));if(des)this.Fields.Add(des);} 
  79. }
  80. else{this.RowData=this.root;this.RowCnt=this.RowData.childNodes.length;}
  81.  
  82. var params=this.MetaData.childNodes.item(1);
  83.  
  84. if(params.getAttribute("READONLY")!=null){this.noDel=1;this.noIns=1;this.noUpd=1;}
  85. for(i=0;i<params.childNodes.length;i++)
  86. {var p=params.childNodes.item(i);var n=p.getAttribute("Name");var v=p.getAttribute("Value");
  87.  if(n!=null&&v!=null)if(n=="DISABLE_DELETES")this.noDel=1;else if(n=="DISABLE_INSERTS")this.noIns=1;else if(n=="DISABLE_EDITS")this.noUpd=1;
  88. }
  89. this.DeltaChanges=new DeltaChanges(this);
  90. this.Apply=dsApply;
  91. this.idx=new idx(this);
  92. }
  93.  
  94. function InitDetailRowSet()
  95. {
  96. var i;
  97. var parent=this.parent;
  98. var fd=parent.FieldData.childNodes;
  99. var l=fd.length; 
  100. this.FieldData=parent.Fields.Field[this.linkFld].node.childNodes.item(0);
  101. if(this.FieldData==null)return;
  102. this.FieldCnt=this.FieldData.childNodes.length ;  
  103. var des;
  104. for(i=0;i<this.FieldCnt;i++){des=new FldDes(this,this.FieldData.childNodes.item(i));if(des)this.Fields.Add(des);}
  105. if(parent.pDets==null)parent.pDets=new Array();
  106. parent.pDets[parent.pDets.length]=this;
  107. this.DeltaChanges=parent.DeltaChanges;
  108. parent.resetDets();
  109. this.idx=new idx(this);
  110. this.noDel=parent.noDel;this.noIns=parent.noIns;this.noUpd=parent.noUpd;
  111. }
  112.  
  113. function findcnode(n,t)
  114. {
  115. if(n==null)return null;
  116. var j,l=n.childNodes.length;
  117. for(j=0;j<l;j++)if(n.childNodes.item(j).tagName==t)return n.childNodes.item(j);
  118. return null;
  119. }
  120.  
  121. function resetDets()
  122. {
  123. if (this.pDets==null) return;
  124. var i,l=this.pDets.length;
  125. for(i=0;i<l;i++)
  126. {
  127.  var rsd=this.pDets[i];
  128.  var row=this.idx.row(this.pos);
  129.  var det=findcnode(row,rsd.linkFld);
  130.  rsd.RowData=det;
  131.  rsd.RowCnt=(det==null)?0:det.childNodes.length;
  132.  if(rsd.idx) rsd.idx.sort(null);
  133.  rsd.first();
  134. rsd.resetDets();
  135.  rsd.notify(2);
  136. }
  137. }
  138.  
  139. function dsApply(frm,el)
  140. {
  141. if(frm==null||el==null||this.forcepost()!=0||this.DeltaChanges.row.length==0)return;
  142. var delta=this.getDelta();
  143. if(delta.childNodes.item(1).childNodes.length==0) return;
  144. el.value=(delta.xml!=null)?delta.xml:delta.xmlstr();
  145. frm.submit();
  146. }
  147.  
  148. function dsInsert(prow,bFollow)
  149. {
  150. if (this.noIns){return this.OnError(szErr_Invalid);}
  151. prow=this.BeforePost(1,prow);
  152. if(prow==null) return 1;
  153. var spos=this.pos;
  154. var pN=this.ins();
  155. if(pN==null) return 1;
  156. var r=this.idx.row(this.pos);
  157. r.setAttribute(szRowState,szNew);
  158. this.upd(prow);
  159.  
  160. if(this.pDets){
  161. var i,l=this.Fields.Cnt;
  162. for(i=0;i<l;i++){if(this.Fields.Fieldx[i].Type==szNested){var el=this.doc.createElement(this.Fields.Fieldx[i].iname);r.appendChild(el);}}
  163. }
  164.  
  165. if (bFollow==0)this.pos=spos;
  166. this.resetDets();
  167. this.DeltaChanges.add(szNew,pN,null,this);
  168. this.idx.chg=1;
  169. this.AfterPost(1,prow);
  170. return 0;
  171. }
  172.  
  173. function dsModify(p,prow)
  174. {
  175. if(this.noUpd){return this.OnError(szErr_Invalid);}
  176. prow=this.BeforePost(3,prow);
  177. if(prow==null) return 1;
  178. var pO,pM; 
  179. var spos=this.pos; this.pos=p;
  180. var i,l;
  181. pM=this.idx.row(p);
  182. var rstate=pM.getAttribute(szRowState);
  183. if(rstate!=szNew){
  184.  pO=pM.cloneNode(0);l=pM.childNodes.length;
  185.  for(i=0;i<l;i++)pO.appendChild(pM.childNodes.item(i).cloneNode(0));
  186.  pM.setAttribute(szRowState,szMod);
  187.  this.upd(prow);
  188.  this.DeltaChanges.add(szMod,pM,pO,this);
  189. }else this.upd(prow); 
  190. this.pos=spos;
  191. this.idx.chg=1;
  192. this.AfterPost(3,prow);
  193. return 0;
  194. }
  195.  
  196. function dsDelete(p)
  197. {
  198. if(this.noDel){return this.OnError(szErr_Invalid);}
  199. var r=this.idx.row(p); 
  200. r=this.BeforePost(2,r);
  201. if(r==null)return 1;
  202. var spos=this.pos; this.pos=p;
  203. var i,l=r.childNodes.length;
  204. if(l&&this.pDets)
  205.  for(i=0;i<this.pDets.length;i++){var det=findcnode(r,this.pDets[i].linkFld);if(det&&det.childNodes.length>0)return this.OnError(szErr_HasDets);}
  206. r.setAttribute(szRowState,szDel);
  207. var pD=this.del();
  208. if(p<spos) this.pos=spos-1;
  209. this.resetDets();
  210. this.DeltaChanges.add(szDel,pD,null,this);
  211. this.idx.chg=1;
  212. this.AfterPost(2,null);
  213. return 0;
  214. }
  215.  
  216. function updRow(prow)
  217. var i,f,v,r=this.idx.row(this.pos);
  218. if(r==null)return r;
  219. for(i=0;i<this.Fields.Cnt;i++){f=this.Fields.Fieldx[i];v=prow[f.name];if (v!=null) f.put(r,v);}
  220. this.notify(1);
  221. return r;
  222. }
  223.  
  224. function makeRow()
  225. {
  226. var i,f,v,r=this.idx.row(this.pos);
  227. if(r==null)return r;
  228. var RowBuf=new Array();
  229. for(i=0;i<this.Fields.Cnt;i++){f=this.Fields.Fieldx[i];v=null;if(f.bAsAttr)v=r.getAttribute(f.iname);if(v!=null)RowBuf[f.name]=v;}
  230. return RowBuf;
  231. }
  232.  
  233. function insRow()
  234. {
  235. if(this.FieldData==null)return null;
  236. var rname="ROW";
  237. def=this.OnNewRow();
  238. if(this.linkFld)rname+=this.linkFld;
  239. var r=this.doc.createElement(rname);
  240. var i,f;
  241. for(i=0;i<this.Fields.Cnt;i++){    
  242.  var F;
  243.  f=this.Fields.Fieldx[i];
  244.  if(f.bAsAttr==0){var T=this.doc.createTextNode("");F=this.doc.createElement(f.iname);F.appendChild(T); r.appendChild(F);}
  245.  else if(def&&def[f.name]!=null)r.setAttribute(def[f.name]);
  246. }
  247. if(this.RowData==null)this.RowData=this.doc.createElement(this.linkFld);
  248. this.RowData.appendChild(r);
  249. this.RowCnt++;
  250. this.pos=this.RowCnt-1;
  251. this.idx.add(this.pos);
  252. return r;
  253. }
  254.  
  255. function delRow()
  256. {
  257. var r=this.idx.row(this.pos);
  258. if(r==null)return null;
  259. var pos=this.pos;
  260. this.RowData.removeChild(r);
  261. this.idx.rem(pos);
  262. if(pos==this.RowCnt-1&&pos>0)this.pos=pos-1;
  263. this.RowCnt--;      
  264. this.notify(1); 
  265. return r;
  266. }
  267.  
  268. function RowState(pos)
  269. {
  270. var r=this.idx.row(pos);
  271. if(r==null)return "";
  272. var st=r.getAttribute(szRowState);
  273. if(st==null||st=="")return "";
  274. var v=parseInt(st,10);
  275. if(v==2)st="D";if(v==4)st="I";if(v==8)st="M";if(v==64)st="DU";
  276. return st;
  277. }
  278.  
  279. function Fields()
  280. {
  281. //public:
  282. this.Field=new Array();
  283. this.Fieldx=new Array();
  284. this.Add=addFld;
  285. this.Cnt=0;
  286. }
  287.  
  288. function addFld(fldDes)
  289. {
  290. var n=fldDes.name;
  291. if(n!=""){this.Field[n]=fldDes;this.Fieldx[this.Cnt]=fldDes;this.Cnt++;}
  292. }
  293.  
  294. new FldDes(null,null);
  295. FldDes.prototype.bAsAttr=1;
  296. FldDes.prototype.name="";
  297. FldDes.prototype.iname="";
  298. FldDes.prototype.Type="string";
  299. FldDes.prototype.readonly=0;
  300. FldDes.prototype.required=0;
  301. FldDes.prototype.maxlength=0;
  302. FldDes.prototype.subtype=null;
  303. FldDes.prototype.decimals=null;
  304. FldDes.prototype.fixeddec=null;
  305. FldDes.prototype.currencySymbol=null;
  306. FldDes.prototype.minval=null;
  307. FldDes.prototype.maxval=null;
  308. FldDes.prototype.defval="";
  309. FldDes.prototype.minmax=minmax;
  310. FldDes.prototype.validate=Validate;
  311. FldDes.prototype.valtype=function(v){if((this.errNo=this.minmax(v,this.minval,this.maxval))!= 0) return 0;return v;}
  312. FldDes.prototype.valcomp=function(v1,v2){if(v1==v2) return 0;return (v1>v2)?1:-1;}
  313. FldDes.prototype.todisp=function(v){return v;}
  314. FldDes.prototype.frdisp=function(v){return v;}
  315. FldDes.prototype.Value=rsValue;
  316. FldDes.prototype.get=getValue;
  317. FldDes.prototype.put=putValue;
  318. FldDes.prototype.notNull=function(v){if(v=="")return null;return v;}
  319. FldDes.prototype.errNo=0;
  320.  
  321. function FldDes(rs, node)
  322. {
  323. this.rs=rs;
  324. if(node==null)return;
  325. this.node=node;
  326. var t=node.getAttribute("fieldname"),ti=node.getAttribute("attrname");
  327. if(ti==null){ti=node.getAttribute("tagname");this.bAsAttr=0};
  328. this.iname=ti;
  329. this.name=(t==null)?ti:t;
  330. t=node.getAttribute("fieldtype");
  331. if(t)this.Type=t;
  332. t=node.getAttribute("readonly");if(t)this.readonly=1;
  333. t=node.getAttribute("required");if(t&&this.readonly==0) this.required=1;
  334. t=node.getAttribute("WIDTH");if(t)this.maxlength=parseInt(t,10);
  335. t=node.getAttribute("SUBTYPE");if(t)this.subtype=t;
  336. t=node.getAttribute("DECIMALS");if(t)this.decimals=parseInt(t,10);
  337. if(this.subtype=="Money"){this.decimals=4;}
  338. if(this.Type=="fixed"){this.fixeddec=this.decimals;}
  339. if(this.subtype=="Text")this.maxlength=0;
  340.  
  341. if(this.Type=="string"||this.Type==szUni){this.notNull=function(v){return v;};}else
  342. if(this.Type=="i1"){this.valtype=valint; this.valcomp=cmpint; this.minval=parseInt("-128");this.maxval=parseInt("127"); }else
  343. if(this.Type=="i2"){this.valtype=valint; this.valcomp=cmpint; this.minval=parseInt("-32768");this.maxval=parseInt("32767");}else
  344. if(this.Type=="i4"){this.valtype=valint; this.valcomp=cmpint; this.minval=parseInt("-2147483648");this.maxval=parseInt("2147483647")}else
  345. if(this.Type=="r8"||this.Type=="fixed"){this.valtype=valfloat; this.valcomp=cmpfloat;this.todisp=dispfloat;this.frdisp=xmlfloat;}else
  346. if(this.Type=="date"){this.valtype=valdate;this.valcomp=cmpstr;this.todisp=dispdate;this.frdisp=xmldatetime}else
  347. if(this.Type=="dateTime"){this.valtype=valdatetime;this.valcomp=cmpstr;this.todisp=dispdatetime;this.frdisp=xmldatetime}else
  348. if(this.Type=="time"){this.valtype=valtime;this.todisp=disptime;}else
  349. if(this.Type=="boolean"){this.valtype=valbool;this.todisp=dispbool;this.frdisp=dispbool;}
  350.  
  351. t=node.getAttribute("MINVALUE");if(t)this.minval=t;
  352. t=node.getAttribute("MAXVALUE");if(t)this.maxval=t;
  353. for(i=0;i<node.childNodes.length;i++){
  354.  var p=node.childNodes.item(i);var n=p.getAttribute("Name");var v=p.getAttribute("Value");
  355.  if(n!=null&&v!=null)if(n=="MINVALUE") this.minval=v;else if(n=="MAXVALUE"){this.maxval=v;}
  356. }
  357. }
  358.  
  359. function valbool(v){return v;}
  360. function dispbool(v)
  361. {
  362. if(v=="")return v;
  363. if((v.toLowerCase()).indexOf(szTrue)>=0) v=szTrue;else v=szFalse;
  364. return v;
  365. }
  366.  
  367. function dispdatetime(v)
  368. {
  369. if(v.length>=8){var y=parseInt(v.substring(0,4),10),m=parseInt(v.substring(4,6),10)-1,d=parseInt(v.substring(6,8),10);
  370. var t=v.indexOf("T");
  371. var D;
  372. if(t==-1)D=new Date(y,m,d);else
  373. {
  374. var h=0,mi=0,s=0,ms=0;
  375. v=v.substring(t+1,v.length);
  376. t=v.indexOf(":");
  377. h=parseInt(v.substring(0,t),10);
  378. v=v.substring(t+1,v.length);
  379. t=v.indexOf(":");
  380. mi=parseInt(v.substring(0,t),10);
  381. v=v.substring(t+1,v.length);
  382. t=v.indexOf(":");
  383. s=parseInt(v.substring(0,2),10);
  384. ms=parseInt(v.substring(2,v.length),10);
  385. D=new Date(y,m,d,h,mi,s,ms);
  386. }
  387. return D.toLocaleString();
  388. }
  389. return "";
  390. }
  391.  
  392. function dispdate(v)
  393. {
  394. if (v.length<8)return "";
  395. var y=parseInt(v.substring(0,4),10);m=parseInt(v.substring(4,6),10)-1;d=parseInt(v.substring(6,8),10);
  396. var D;
  397. D=new Date(y,m,d);
  398. var s=D.toLocaleString();
  399. var st=s.indexOf("00:");
  400. if(st>0) s=s.substring(0,st);
  401. return s;
  402. }
  403.  
  404. function disptime(v){return v;}
  405. function cntchrs(v){var s=new Array(),l=v.length;s=v.split("&#");l-=(s.length-1)*5;return l>0?l:0;}
  406. function Validate(v)
  407. {
  408. var err="";
  409. if(this.required&&v==""){err=this.name +" :Value is required";}else
  410. if(this.maxlength&&v.length>this.maxlength){if(cntchrs(v)>this.maxlength) err=this.name +" :Value is too long";}
  411. if(err!=""){this.rs.OnError(err);return null;}
  412. v=this.valtype(v);
  413. if(v!=null){var s=this.frdisp(v);if(!s) return s;v=this.todisp(s);}
  414. return v;
  415. }
  416.  
  417. function cmpint(v1,v2)
  418. {
  419. if(v1==v2)return 0;
  420. var i1= parseInt(v1,10),i2=parseInt(v2,10);
  421. if(isNaN(i1)||isNaN(i2)) return cmpstr(v1,v2);
  422. return i1-i2;
  423. }
  424.  
  425. function cmpfloat(v1,v2)
  426. {
  427. if(v1==v2) return 0;
  428. var f1=parseFloat(v1),f2=parseFloat(v2);
  429. if(isNaN(f1)||isNaN(f2)) return cmpstr(v1,v2);
  430. return f1-f2;
  431. }
  432.  
  433.  
  434. function cmpstr(v1,v2)
  435. {
  436. if(v1==v2)return 0;
  437. if(v1==null)return -1;
  438. if(v2==null)return 1;
  439. return (v1>v2)?1:-1;
  440. }
  441.  
  442. function valint(v)
  443. {
  444. if(v!=""){
  445. var i=parseInt(v,10); 
  446. if(isNaN(i)) {this.errNo=3;this.rs.OnError(this.name+ " : Invalid integer");return null;}
  447. if((this.errNo=this.minmax(i,this.minval,this.maxval))!= 0) return null;
  448. v=i.toString();
  449. }
  450. this.errNo=0;
  451. return v;
  452. }
  453.  
  454. function minmax(i,imin,imax)
  455. {
  456. if(imin&&i<imin){this.rs.OnError(this.name+" : Value is out of range, "+ i + " < "+imin);return 4;}
  457. if(imax&&i>imax){this.rs.OnError(this.name+" : Value is out of range, "+ i+" > "+imax);return 4;}
  458. return 0;
  459. }
  460.  
  461. function valfloat(v)
  462. {
  463. if(v!="")
  464. {if(this.currencySymbol!=null){v=this.frdisp(v);}
  465.  var i;
  466.  if(this.Type=="r8") i=parseFloat(v);else i=Number(v);
  467.  if(isNaN(i)){this.errNo=3;this.rs.OnError(this.name+ " : Invalid number");return null;}
  468.  if((this.errNo=this.minmax(i,this.minval,this.maxval))!=0) return null;
  469.  v=this.todisp(i.toString());
  470. }
  471. this.errNo=0;
  472. return v;
  473. }
  474.  
  475. function dispfloat(n)
  476. {
  477. var f; if(this.Type=="r8")f=parseFloat(n);else f=Number(n);
  478. if(this.decimals!= null){var d=this.decimals;var p=Math.pow(10,d);f=(Math.round(f*p)/p);}
  479. n=f.toString();
  480. if(this.fixeddec!=null&&n.indexOf("e")==-1)
  481. {var j,i=n.indexOf(DecPoint);if(i==-1){n=n+DecPoint;i=0;}else i=n.length-i-1;
  482. for(j=i;j<this.fixeddec;j++) n=n+"0";
  483. }
  484. var c=this.currencySymbol;
  485. if(c!=null){if(n.charAt(0)=='-') n="("+c+n.substring(1)+")";else n=c+n;}
  486. return n;
  487. }
  488.  
  489. function xmlfloat(n)
  490. {
  491. var c=this.currencySymbol;if(c==null) return n;
  492. var s=n.indexOf("("),j=n.indexOf(c); 
  493. n=(j!=-1)? n.substring(j+c.length):n;
  494. if(s!=-1)  n="-"+n.substring(0,n.indexOf(")"));
  495. return n;
  496. }
  497.  
  498. function xmldatetime(v)
  499. {
  500. if(v=="") return v;
  501. var d=new Date(Date.parse(v));
  502. if(isNaN(d)){this.rs.OnError(this.name+" : Invalid date/time");return null;}
  503. var y=d.getFullYear();var m=(d.getMonth()+1);var da=d.getDate();
  504. var h=d.getHours();var mi=d.getMinutes();var sec=d.getSeconds();var ms=d.getMilliseconds();var s=y.toString();
  505. if(m<10) s=s+'0';
  506. s=s+m;
  507. if(da<10) s=s+'0';
  508. s=s+da;
  509. if(h||mi||sec||ms)
  510. {
  511. s=s+'T';
  512. if(h<10) s=s+'0';
  513. s=s+h+':';
  514. if(mi<10) s=s+'0';
  515. s=s+mi+':';
  516. if(sec<10) s=s+'0';
  517. s=s+sec +ms;
  518. }
  519. return s;
  520. }
  521.  
  522. function valdatetime(v)
  523. {
  524. if(v!=""){var s=this.frdisp(v);if(!s) return s;v=this.todisp(s);}
  525. return v;
  526. }
  527. function valdate(v){return v;}
  528. function valtime(v){return v;}
  529.  
  530.  
  531. function DeltaChanges(ds)
  532. {
  533. this.ds=ds;
  534. this.action=new Array();
  535. this.row=new Array();
  536. this.rowOrg=new Array();
  537. this.parents=new Array();
  538. this.rs=new Array();
  539. this.rem=RemFromLog;
  540. this.add=AddToLog;
  541. this.find=FindInLog;
  542. this.make=MakeDelta;
  543. this.fullpath=MakePath;
  544. this.reset=ResetLog;
  545. }
  546.  
  547. function AddToLog(Act,pRow,pRowOrg,rs)
  548. {
  549. var i,l=this.action.length;
  550. if(pRowOrg) for (i=0;i<l;i++) if(this.row[i]==pRow) return;
  551.  
  552. if(Act==szDel)
  553. for(i=0;i<l;i++)
  554. if(this.row[i]==pRow)
  555. {
  556.  if(this.action[i]==szNew){this.rem(i);return;}
  557.  else{pRow=this.rowOrg[i];this.rem(i);l--;break;}
  558. }
  559. this.action[l]=Act;
  560. this.row[l]=pRow;
  561. this.rowOrg[l]=pRowOrg;
  562. this.parents[l]=GetParents(rs);
  563. this.rs[l]=rs;
  564. }
  565.  
  566. function FindInLog(r)
  567. {
  568. var i,l=this.action.length;
  569. for(i=0;i<l;i++)if(this.row[i]==r) return i;
  570. return -1;
  571. }
  572.  
  573. function ResetLog()
  574. {
  575. var i,l=this.action.length;
  576. for(i=0;i<l;i++){this.row[i].removeAttribute(szRowState);}
  577. this.action.length=0;
  578. }
  579.  
  580. function RemFromLog(j)
  581. {
  582. var i,l=this.action.length;
  583. for(i=j;i<l-1;i++)
  584. {
  585.  this.action[i]=this.action[i+1];
  586.  this.row[i]=this.row[i+1];
  587.  this.rowOrg[i]=this.rowOrg[i+1];
  588.  this.parents[i]=this.parents[i+1];
  589.  this.rs[i]=this.rs[i+1];
  590. }
  591. this.action.length=l-1;
  592. this.row.length=l-1;
  593. this.rowOrg.length=l-1;
  594. this.parents.length=l-1;
  595. this.rs.length=l-1;
  596. }
  597.  
  598. function MakeDelta()
  599. {
  600. var ds=this.ds;
  601. var doc=ds.doc.createDocumentFragment();
  602. var e=ds.root.cloneNode(0);
  603. doc.appendChild(e);
  604.  
  605. var mdata=ds.MetaData.cloneNode(1);
  606. var params=mdata.childNodes.item(1);
  607. params.setAttribute("DATASET_DELTA","1");
  608. e.appendChild(mdata);
  609.  
  610. var RD=ds.doc.createElement("ROWDATA");
  611. e.appendChild(RD);
  612.  
  613. var l=this.action.length;
  614. var i;
  615.  
  616. for(i=0;i<l;i++)
  617. {
  618. var rs=this.rs[i];
  619. var pr=this.row[i].cloneNode(0);
  620. var po=this.rowOrg[i] != null ? this.rowOrg[i].cloneNode(1) :null;
  621. if(po)
  622. {
  623.  po.setAttribute(szRowState,szOrg);
  624.  var j,f,v1,v2,cnt=0;
  625.  for(j=0;j<rs.Fields.Cnt;j++)
  626.  {
  627.   f=rs.Fields.Fieldx[j];
  628.   v1=f.get(po);
  629.   v2=f.get(pr);
  630.   if (f.valcomp(v1,v2)==0) f.put(pr,null);else cnt++;
  631.  }
  632.  if(cnt==0){continue;}
  633. }
  634. pr.setAttribute(szRowState,this.action[i]);
  635. if(this.action[i]==szMod||this.action[i]==szNew||this.action[i]==szDel)
  636. {
  637.  var j,le=this.row[i].childNodes.length;
  638.  for(j=0;j<le;j++) pr.appendChild(this.row[i].childNodes.item(j).cloneNode(0));
  639. }
  640. this.fullpath(RD,po,pr,i);
  641. }
  642. return e;
  643. }
  644.  
  645. function MakePath(RD,po,pr,j)
  646. {
  647. var pp=this.parents[j];
  648. if(pp==null){if(po) RD.appendChild(po);RD.appendChild(pr);return;}
  649. var i,rs=this.rs[j];
  650. for(i=pp.length-1;i>=0;i--){
  651.  var pM=pp[i];
  652.  pM=pM.cloneNode(0);
  653.  pM.setAttribute(szRowState,szDetUpd);
  654.  var FldLink=rs.doc.createElement(rs.linkFld);
  655.  if(po){FldLink.appendChild(po);po=null;}
  656.  FldLink.appendChild(pr);
  657. var k,l=pp[i].childNodes.length;
  658. for(k=0;k<l;k++){if(pp[i].childNodes.item(k).tagName==rs.linkFld) pM.appendChild(FldLink);else pM.appendChild(pp[i].childNodes.item(k).cloneNode(0));}
  659.  pr=pM;
  660.  rs=rs.parent;
  661. }
  662. RD.appendChild(pr);
  663. return;
  664. }
  665.  
  666. function GetParents(rs)
  667. {
  668. var p=rs.parent;
  669. if(p==null)return null;
  670. var cnt=1,i;
  671. var pa=new Array();
  672. while(p.parent!=null){p=p.parent;cnt++;}
  673. p=rs.parent;
  674. for(i=0;i<cnt;i++){pa[cnt-i-1]=p.idx.row(p.pos);p=p.parent;}
  675. return pa;
  676. }
  677.  
  678. function dsUndo(p)
  679. {
  680. var s=this.RowState(p);//use attributes instead!!
  681. if(s==""){this.notify(1);return;}
  682. if(s=="I"){this.deletex(p);}
  683. else if (s=="M")
  684. {
  685. var pRowOrg,pRow=this.idx.row(p);
  686. var c=this.DeltaChanges.find(pRow);
  687. if(c==-1) return;
  688. pRowOrg=this.DeltaChanges.rowOrg[c];
  689. var i,f,v;
  690. for(i=0;i<this.Fields.Cnt;i++)
  691. {
  692.  f=this.Fields.Fieldx[i];
  693.  if(f.bAsAttr){v=pRowOrg.getAttribute(f.iname);f.put(pRow,v);}
  694. }
  695. pRow.removeAttribute(szRowState);
  696. this.idx.chg=1;
  697. this.DeltaChanges.rem(c);
  698. this.notify(1); //?
  699. }
  700. else if(s=="D"){}
  701. }
  702.  
  703. function DsForcePost()
  704. {if(this.regobjs==null)return 0;
  705. var j ;
  706. for(j=0;j<this.regobjs.length;j++)
  707. {
  708.  var o=this.regobjs[j];
  709.  if(o.post!=null&&o.post()!=0)return 1;
  710. }
  711. if(this.pDets==null)return 0;
  712. for(j=0;j<this.pDets.length;j++){var det=this.pDets[j];if(det.forcepost()!=0)return 1;}
  713. return 0;
  714. }
  715.  
  716. function idx(rs)
  717. {
  718. this.rs=rs;
  719. this.chg=0;
  720. this.add=idxadd;
  721. this.rem=idxrem;
  722. this.sort=idxsort;
  723. this.map=new Array();
  724. this.pos=idxpos;
  725. this.row=idxrow;
  726. this.fld=null;
  727. this.sort(null);
  728. }
  729. function idxadd(e){this.map[e]=this.map.length;}
  730. function idxpos(i){return this.map[i];}
  731. function idxrow(i){if(i>=this.rs.RowCnt) return null;i=this.pos(i);return this.rs.RowData.childNodes.item(i);}
  732. function idxrem(e){var i,l=this.map.length,v=this.map[e];for(i=e;i<l;i++) this.map[i]=this.map[i+1];this.map.length=l-1;for(i=0;i<l-1;i++)if(this.map[i]>v) this.map[i]--;}
  733.  
  734. function idxsort(name)
  735. {
  736. var i,s,cnt=this.rs.RowCnt;
  737. this.map.length=cnt;
  738. if(this.chg==0&&name&&name==this.fld){this.map.reverse();this.rs.first();this.rs.notify(2);return;}
  739. this.chg=0; 
  740. for(i=0;i<cnt;i++)this.map[i]=i;
  741. this.fld=name;
  742. if(name==null)return;
  743. var f=this.rs.Fields.Field[name];
  744. if(f==null)return;
  745. var sarray=new Array();
  746. this.fld=name;
  747. for(i=0;i<cnt;i++){s=f.Value(i);s=s+"&"+i.toString();sarray[i]=s;}
  748. sarray.sort(f.valcomp);
  749. for(i=0;i<cnt;i++){s=sarray[i].split("&");this.map[i]=parseInt(s[s.length-1],10);}
  750. this.rs.first();
  751. this.rs.notify(2);
  752. }
  753.  
  754. function rsValue(p)
  755. {
  756. var pos=p;
  757. if(isNaN(p))pos=this.rs.pos;
  758. var r=this.rs.idx.row(pos);
  759. if(r==null) return"";
  760. return this.get(r);
  761. }
  762.  
  763. function getValue(r)
  764. {
  765. if(this.bAsAttr){var v=r.getAttribute(this.iname);if(v!=null)return v;return "";}
  766. var p=findcnode(r,this.iname);
  767. if(p){p=p.childNodes;if(p.length)return p.item(0).data;}
  768. return "";
  769. }
  770.  
  771. function putValue(r,v)
  772. {
  773. if(this.bAsAttr){if(v!=null){r.setAttribute(this.iname,v);}else r.removeAttribute(this.iname);return;}
  774. if(v==null) v="";
  775. var p=findcnode(r,this.iname);
  776. if(p){if(p.childNodes.length==0){var T=this.rs.doc.createTextNode("");p.appendChild(T);}p.childNodes.item(0).data=v;}
  777. }
  778.