Spry.Data.HTMLDataSet=function(a,b,c){this.sourceElementID=b;this.sourceElement=null;this.sourceWasInitialized=false;this.firstRowAsHeaders=true;this.useColumnsAsRows=false;this.columnNames=null;this.hideDataSourceElement=true;this.rowSelector=null;this.dataSelector=null;this.removeUnbalancedRows=true;
this.tableModeEnabled=true;Spry.Data.HTTPSourceDataSet.call(this,a,c);};Spry.Data.HTMLDataSet.prototype=new Spry.Data.HTTPSourceDataSet();Spry.Data.HTMLDataSet.prototype.constructor=Spry.Data.HTMLDataSet;Spry.Data.HTMLDataSet.prototype.getDataRefStrings=function(){var a=[];if(this.url){a.push(this.url);
}if(typeof this.sourceElementID=="string"){a.push(this.sourceElementID);}return a;};Spry.Data.HTMLDataSet.prototype.setDisplay=function(a,b){if(a){a.style.display=b;}};Spry.Data.HTMLDataSet.prototype.initDataSource=function(a){if(!this.loadDependentDataSets()){return;}if(!this.usesExternalFile()){this.setSourceElement();
if(this.hideDataSourceElement){this.setDisplay(this.sourceElement,"none");}}};Spry.Data.HTMLDataSet.prototype.setSourceElement=function(d){this.sourceElement=null;if(!this.sourceElementID){if(d){this.sourceElement=d;}else{this.hideDataSourceElement=false;this.sourceElement=document.body;}return;}var c=Spry.Data.Region.processDataRefString(null,this.sourceElementID,this.dataSetsForDataRefStrings);
if(!this.usesExternalFile()){this.sourceElement=Spry.$(c);}else{if(d){var b=false;var a=Spry.Utils.getNodesByFunc(d,function(e){if(b){return false;}if(e.nodeType!=1){return false;}if(e.id&&e.id.toLowerCase()==c.toLowerCase()){b=true;return true;}});this.sourceElement=a[0];}}if(!this.sourceElement){Spry.Debug.reportError("Spry.Data.HTMLDataSet: '"+c+"' is not a valid element ID");
}};Spry.Data.HTMLDataSet.prototype.getSourceElement=function(){return this.sourceElement;};Spry.Data.HTMLDataSet.prototype.getSourceElementID=function(){return this.sourceElementID;};Spry.Data.HTMLDataSet.prototype.setSourceElementID=function(a){if(this.sourceElementID!=a){this.sourceElementID=a;this.recalculateDataSetDependencies();
this.dataWasLoaded=false;}};Spry.Data.HTMLDataSet.prototype.getDataSelector=function(){return this.dataSelector;};Spry.Data.HTMLDataSet.prototype.setDataSelector=function(a){if(this.dataSelector!=a){this.dataSelector=a;this.dataWasLoaded=false;}};Spry.Data.HTMLDataSet.prototype.getRowSelector=function(){return this.rowSelector;
};Spry.Data.HTMLDataSet.prototype.setRowSelector=function(a){if(this.rowSelector!=a){this.rowSelector=a;this.dataWasLoaded=false;}};Spry.Data.HTMLDataSet.prototype.loadDataIntoDataSet=function(b){var c=b;c=Spry.Data.HTMLDataSet.cleanupSource(c);var d=document.createElement("div");d.id="htmlsource"+this.internalID;
d.innerHTML=c;this.setSourceElement(d);if(this.sourceElement){var a=this.getDataFromSourceElement();if(a){this.dataHash=a.dataHash;this.data=a.data;}}this.dataWasLoaded=true;d=null;};Spry.Data.HTMLDataSet.prototype.loadDependentDataSets=function(){if(this.hasDataRefStrings){var a=true;for(var b=0;b<this.dataSetsForDataRefStrings.length;
b++){var c=this.dataSetsForDataRefStrings[b];if(c.getLoadDataRequestIsPending()){a=false;}else{if(!c.getDataWasLoaded()){c.loadData();a=false;}}}if(!a){return false;}}return true;};Spry.Data.HTMLDataSet.prototype.loadData=function(){this.cancelLoadData();this.initDataSource();var b=this;if(!this.usesExternalFile()){this.notifyObservers("onPreLoad");
this.dataHash=new Object;this.data=new Array;this.dataWasLoaded=false;this.unfilteredData=null;this.curRowID=0;this.pendingRequest=new Object;this.pendingRequest.timer=setTimeout(function(){b.pendingRequest=null;var e=b.getDataFromSourceElement();if(e){b.dataHash=e.dataHash;b.data=e.data;}b.dataWasLoaded=true;
b.disableNotifications();b.filterAndSortData();b.enableNotifications();b.notifyObservers("onPostLoad");b.notifyObservers("onDataChanged");},0);}else{var c=Spry.Data.Region.processDataRefString(null,this.url,this.dataSetsForDataRefStrings);var a=this.requestInfo.postData;if(a&&(typeof a)=="string"){a=Spry.Data.Region.processDataRefString(null,a,this.dataSetsForDataRefStrings);
}this.notifyObservers("onPreLoad");this.dataHash=new Object;this.data=new Array;this.dataWasLoaded=false;this.unfilteredData=null;this.curRowID=0;var d=this.requestInfo.clone();d.url=c;d.postData=a;this.pendingRequest=new Object;this.pendingRequest.data=Spry.Data.HTTPSourceDataSet.LoadManager.loadData(d,this,this.useCache);
}};Spry.Data.HTMLDataSet.cleanupSource=function(a){a=a.replace(/<(img|script|link|frame|iframe|input)([^>]+)>/gi,function(e,d,f){return"<"+d+f.replace(/\b(src|href)\s*=/gi,function(g,c){return"spry_"+c+"=";})+">";});return a;};Spry.Data.HTMLDataSet.undoCleanupSource=function(a){a=a.replace(/<(img|script|link|frame|iframe|input)([^>]+)>/gi,function(e,d,f){return"<"+d+f.replace(/\bspry_(src|href)\s*=/gi,function(g,c){return c+"=";
})+">";});return a;};Spry.Data.HTMLDataSet.normalizeColumnName=function(a){if(a){a=a.replace(/(?:^[\s\t]+|[\s\t]+$)/gi,"");a=a.replace(/<\/?([a-z]+)([^>]+)>/gi,"");a=a.replace(/[\s\t]+/gi,"_");}return a;};Spry.Data.HTMLDataSet.prototype.getDataFromSourceElement=function(){if(!this.sourceElement){return null;
}var h;var q=(this.tableModeEnabled&&this.sourceElement.nodeName.toLowerCase()=="table");if(q){h=this.getDataFromHTMLTable();}else{h=this.getDataFromNestedStructure();}if(!h){return null;}if(this.useColumnsAsRows){var l=new Array;for(var b=0;b<h.length;b++){var r=h[b];for(var g=0;g<r.length;g++){if(!l[g]){l[g]=new Array;
}l[g][b]=r[g];}}h=l;}var c=new Object();c.dataHash=new Object;c.data=new Array;if(h.length==0){return c;}var p=0;for(var f=0;f<h.length;f++){var k=h[f].length;if(p<k){p=k;}}var o=new Array;var m=h[0];for(var g=0;g<p;g++){if(q&&this.firstRowAsHeaders){o[g]=Spry.Data.HTMLDataSet.normalizeColumnName(m[g]);
}if(!o[g]){o[g]="column"+g;}}if(this.columnNames&&this.columnNames.length){var d=(p<this.columnNames.length)?p:this.columnNames.length;for(var f=0;f<d;f++){if(this.columnNames[f]){o[f]=this.columnNames[f];}}}var j=0;var e=(q&&this.firstRowAsHeaders)?1:0;for(var b=e;b<h.length;b++){var r=h[b];if(this.removeUnbalancedRows&&o.length!=r.length){continue;
}var n={};for(var g=0;g<o.length;g++){var a=r[g];n[o[g]]=(typeof a=="undefined")?"":a;}n["ds_RowID"]=j++;c.dataHash[n["ds_RowID"]]=n;c.data.push(n);}return c;};Spry.Data.HTMLDataSet.getElementChildren=function(b){var a=[];var c=b.firstChild;while(c){if(c.nodeType==1){a.push(c);}c=c.nextSibling;}return a;
};Spry.Data.HTMLDataSet.prototype.getDataFromHTMLTable=function(){var b=this.sourceElement.tHead;var j=this.sourceElement.tBodies[0];var n=[];var q=[];if(b){n=Spry.Data.HTMLDataSet.getElementChildren(b);}if(j){q=Spry.Data.HTMLDataSet.getElementChildren(j);}var s=new Array;var o=n.concat(q);if(this.rowSelector){o=Spry.Data.HTMLDataSet.applySelector(o,this.rowSelector);
}for(var d=0;d<o.length;d++){var m=o[d];var p;if(s[d]){p=s[d];}else{p=new Array;}var k=0;var h=m.cells;if(this.dataSelector){h=Spry.Data.HTMLDataSet.applySelector(h,this.dataSelector);}for(var a=0;a<h.length;a++){var c=h[a];var l=a+k;while(p[l]){k++;l++;}var t=Spry.Data.HTMLDataSet.undoCleanupSource(c.innerHTML);
p[l]=t;var f=c.colSpan;if(f==0){f=1;}var u=k;for(var r=1;r<f;r++){k++;l=a+k;p[l]=t;}var v=c.rowSpan;if(v==0){v=1;}for(var e=1;e<v;e++){nextRowIndex=d+e;var g;if(s[nextRowIndex]){g=s[nextRowIndex];}else{g=new Array;}var i=u;for(var r=0;r<f;r++){l=a+i;g[l]=t;i++;}s[nextRowIndex]=g;}}s[d]=p;}return s;};
Spry.Data.HTMLDataSet.prototype.getDataFromNestedStructure=function(){var c=new Array;if(this.sourceElementID&&!this.rowSelector&&!this.dataSelector){c[0]=[Spry.Data.HTMLDataSet.undoCleanupSource(this.sourceElement.innerHTML)];return c;}var a=this;var f=[];if(!this.rowSelector){f=[this.sourceElement];
}else{f=Spry.Utils.getNodesByFunc(this.sourceElement,function(h){return Spry.Data.HTMLDataSet.evalSelector(h,a.sourceElement,a.rowSelector);});}for(var e=0;e<f.length;e++){var g=f[e];var b=[];if(!this.dataSelector){b=[g];}else{b=Spry.Utils.getNodesByFunc(g,function(h){return Spry.Data.HTMLDataSet.evalSelector(h,g,a.dataSelector);
});}c[e]=new Array;for(var d=0;d<b.length;d++){c[e][d]=Spry.Data.HTMLDataSet.undoCleanupSource(b[d].innerHTML);}}return c;};Spry.Data.HTMLDataSet.applySelector=function(f,b,c){var d=[];for(var a=0;a<f.length;a++){var e=f[a];if(Spry.Data.HTMLDataSet.evalSelector(e,c?c:e.parentNode,b)){d.push(e);}}return d;
};Spry.Data.HTMLDataSet.prototype.usesExternalFile=function(){return this.url!=null;};Spry.Data.HTMLDataSet.evalSelector=function(d,i,e){if(d.nodeType!=1){return false;}if(d==i){return false;}var k=e.split(",");for(var j=0;j<k.length;j++){var a=k[j].replace(/^\s+/,"").replace(/\s+$/,"");var c=null;var h=null;
var b=null;var g=true;if(a.substring(0,1)==">"){if(d.parentNode!=i){g=false;}else{a=a.substring(1).replace(/^\s+/,"");}}if(g){c=a.toLowerCase();if(a.indexOf(".")!=-1){var f=a.split(".");c=f[0];h=f[1];}else{if(a.indexOf("#")!=-1){var f=a.split("#");c=f[0];b=f[1];}}}if(g&&c!=""&&c!="*"){if(d.nodeName.toLowerCase()!=c){g=false;
}}if(g&&b&&d.id!=b){g=false;}if(g&&h&&d.className.search(new RegExp("\\b"+h+"\\b","i"))==-1){g=false;}if(g){return true;}}return false;};
