/*
Software License Agreement (BSD License):
http://taffydb.com/?oa=agree
Copyright (c) 2008
All rights reserved.
Version 1.2
*/
if(!this.TAFFY){TAFFY=function(obj){var raw=(TAFFY.isString(obj))?TAFFY.JSON.parse(obj):obj;var TAFFYObj=raw;var t=true,f=false;var findTests={regexppass:function(mvalue,mtest){return(mtest.test(mvalue))?t:f;},regexpfail:function(mvalue,mtest){return(!mtest.test(mvalue))?t:f;},lessthan:function(mvalue,mtest){return(mvalue<mtest)?t:f;},greaterthan:function(mvalue,mtest){return(mvalue>mtest)?t:f;},startswith:function(mvalue,mtest){return(mvalue.indexOf(mtest)===0)?t:f;},endswith:function(mvalue,mtest){return(mvalue.substring((mvalue.length-mtest.length))==mtest)?t:f;},like:function(mvalue,mtest){return(mvalue.indexOf(mtest)>=0)?t:f;},notlike:function(mvalue,mtest){return(mvalue.indexOf(mtest)===-1)?t:f;},equal:function(mvalue,mtest){return(mvalue==mtest)?t:f;},notequal:function(mvalue,mtest){return(mvalue!=mtest)?t:f;},arraycontains:function(mvalue,mtest){if(TAFFY.isArray(mvalue)){for(var TAFa=0;TAFa<mvalue.length;TAFa++){if(mvalue[TAFa]==mtest)return t;}}else{if(mvalue==mtest)return t;}return f;}};var makeIndexArray=function(iA,finderFunc){var rA=[];if(!TAFFY.isArray(iA)&&TAFFY.isNumber(iA)){rA[rA.length]=iA;}else if(TAFFY.isArray(iA)){rA=iA;}else if(TAFFY.isObject(iA)){rA=finderFunc(iA);}else if(!TAFFY.isArray(iA)&&!TAFFY.isNumber(iA)){for(var miAd=0;miAd<TAFFYObj.length;miAd++){rA[rA.length]=miAd;}}return rA;};var toLogicalArray=function(value){var rArray=[0],type="none";for(var n=0;n<value.length;n++){var c=value.slice(n,(n+1));if(TAFFY.IsNumeric(c)){if(type!='number'){rArray[rArray.length]=c;type='number';}else{rArray[(rArray.length-1)]=rArray[(rArray.length -1)]+""+c;}}else{if(type!='string'){rArray[rArray.length]=c;type='string';}else{rArray[(rArray.length-1)]=rArray[(rArray.length -1)]+c;}}}for(var n=0;n<rArray.length;n++){if(TAFFY.IsNumeric(rArray[n])){rArray[n]=parseFloat(rArray[n]);}}return rArray;};var buildSortFunction=function(sortobj){var custO=[],localO=[];if(TAFFY.isString(sortobj)){localO[0]=sortobj;}else if(TAFFY.isObject(sortobj)){localO=[sortobj];}else{localO=sortobj;}if(TAFFY.isArray(localO)){for(var sa=0;sa<localO.length;sa++){if(TAFFY.isString(localO[sa])){if(TAFFY.isString(TAFFYObj[0][localO[sa]])){custO[custO.length]={sortCol:localO[sa],sortDir:"asc",type:"string"};}else{custO[custO.length]={sortCol:localO[sa],sortDir:"asc",type:"number"};}}else if(TAFFY.isObject(localO[sa])){for(var sc in localO[sa]){if(TAFFY.isString(TAFFYObj[0][localO[sa].sortCol])){custO[custO.length]={sortCol:sc,sortDir:localO[sa][sc],type:"string"};}else{custO[custO.length]={sortCol:sc,sortDir:localO[sa][sc],type:"number"};}}}}};return function(a,b){var returnvar=0,r1=a,r2=b,x,y;for(var sa=0;sa<custO.length;sa++){if(returnvar===0){x=r1[custO[sa]["sortCol"]];y=r2[custO[sa]["sortCol"]];if(custO[sa].type=='string'){x=x.toLowerCase();y=y.toLowerCase();}if(custO[sa].sortDir=='desc'){if(x>y){returnvar=-1;}else if(x<y){returnvar=1;}}else if(custO[sa].sortDir=='logical'){x=toLogicalArray(x);y=toLogicalArray(y);for(var z=0;z<y.length;z++){if(x[z]<y[z]&&z<x.length){returnvar=-1;break;}else if(x[z]>y[z]){returnvar=1;break;}}if(x.length<y.length&&returnvar==0){returnvar=-1;}else if(x.length>y.length&&returnvar==0){returnvar=1;}}else if(custO[sa].sortDir=='logicaldesc'){x=toLogicalArray(x);y=toLogicalArray(y);for(var z=0;z<y.length;z++){if(x[z]>y[z]&&z<x.length){returnvar=-1;break;}else if(x[z]<y[z]){returnvar=1;break;}}if(x.length<y.length&&returnvar==0){returnvar=1;}else if(x.length>y.length&&returnvar==0){returnvar=-1;}}else{if(x<y){returnvar=-1;}else if(x>y){returnvar=1;}}}};return returnvar;};};return{raw:raw,length:TAFFYObj.length,lastModifyDate:new Date(),find:function(matchObj,findFilter){var findMatches=[],findMatchesLoaded=0,findIndex=0;for(var mi in matchObj){var matchType='equal',matchValue='',matchField=mi;if(TAFFY.isObject(matchObj[mi])){for(var fi in matchObj[mi]){switch(fi){case'equal':case'is':matchType='equal';break;case'notequal':case'not':matchType='notequal';break;case'startswith':case'starts':matchType='startswith';break;case'endswith':case'ends':matchType='endswith';break;case'greaterthan':case'gt':matchType='greaterthan';break;case'lessthan':case'lt':matchType='lessthan';break;case'regexppass':case'regex':matchType='regexppass';break;case'regexpfail':matchType='regexpfail';break;case'arraycontains':case'contains':matchType='arraycontains';break;case'like':case'notlike':matchType=fi;break;default:matchType='equal';break;}matchValue=matchObj[mi][fi];}}else{matchValue=matchObj[mi];}if(findMatchesLoaded==0){if(TAFFY.isArray(findFilter)){findMatches=findFilter;}else{for(var fd=0;fd<TAFFYObj.length;fd++){findMatches[findMatches.length]=fd;}}findMatchesLoaded=1;}var thisMatchArray=[];for(var d=0;d<findMatches.length;d++){if(TAFFY.isArray(matchValue)){for(var md=0;md<matchValue.length;md++){if(findTests[matchType](TAFFYObj[findMatches[d]][matchField],matchValue[md])){thisMatchArray[thisMatchArray.length]=findMatches[d];}}}else if(findTests[matchType](TAFFYObj[findMatches[d]][matchField],matchValue)){thisMatchArray[thisMatchArray.length]=findMatches[d];}}findMatches=thisMatchArray;};findMatches=TAFFY.gatherUniques(findMatches);return findMatches;},remove:function(removeIndex){var removeIndex=makeIndexArray(removeIndex,this.find);for(var di=0;di<removeIndex.length;di++){if(this.onRemove!=null){this.onRemove(TAFFYObj[removeIndex[di]]);}TAFFYObj[removeIndex[di]]='remove';}var removeRemaining=function(){for(var tdi=0;tdi<TAFFYObj.length;tdi++){if(TAFFYObj[tdi]==='remove'){return t;}}return f;};while(removeRemaining()){for(var tdi=0;tdi<TAFFYObj.length;tdi++){if(TAFFYObj[tdi]==='remove'){TAFFYObj.splice(tdi,1);this.lastModifyDate=new Date();}}}this.length=TAFFYObj.length;return removeIndex;},insert:function(newRecordObj){if(this.onInsert!=null){this.onInsert(newRecordObj);}TAFFYObj[TAFFYObj.length]=newRecordObj;this.lastModifyDate=new Date();this.length=TAFFYObj.length;return[TAFFYObj.length-1];},update:function(updateObj,updateIndex){var updateIndex=makeIndexArray(updateIndex,this.find),updateCount=0;for(var d=0;d<updateIndex.length;d++){updateDex=updateIndex[d];if(this.onUpdate!=null){this.onUpdate(updateObj,TAFFYObj[updateDex]);}TAFFYObj[updateDex]=TAFFY.mergeObj(TAFFYObj[updateDex],updateObj);updateCount++;}this.length=TAFFYObj.length;return updateIndex;},get:function(getIndex){var newTAFFYArray=[];var getIndex=makeIndexArray(getIndex,this.find);for(var d=0;d<getIndex.length;d++){newTAFFYArray[newTAFFYArray.length]=TAFFYObj[getIndex[d]];}return newTAFFYArray;},first:function(getIndex){var getIndex=makeIndexArray(getIndex,this.find);return(getIndex.length>0)?TAFFYObj[getIndex[0]]:false;},last:function(getIndex){var getIndex=makeIndexArray(getIndex,this.find);return(getIndex.length>0)?TAFFYObj[getIndex[(getIndex.length-1)]]:false;},stringify:function(getIndex){return TAFFY.JSON.stringify(this.get(getIndex));},orderBy:function(orderByObj){if(this.length>0){var customSort=buildSortFunction(orderByObj);TAFFYObj.sort(customSort);this.lastModifyDate=new Date();}},forEach:function(func2call,forIndex){var forIndex=makeIndexArray(forIndex,this.find);var row;for(var fe=0;fe<forIndex.length;fe++){row=TAFFYObj[forIndex[fe]];var nr=func2call(row);if(TAFFY.isObject(nr)){this.update(nr,forIndex[fe])}};},onUpdate:null,onRemove:null,onInsert:null};};TAFFY["typeOf"]=function(v){var s=typeof v;if(s==='object'){if(v){if(typeof v.length==='number'&&!(v.propertyIsEnumerable('length'))&&typeof v.splice==='function'){s='array';}}else{s='null';}}return s;};TAFFY["JSON"]=function(){function f(n){return n<10?'0'+n:n;}Date.prototype.toJSON=function(){return this.getUTCFullYear()+'-'+f(this.getUTCMonth()+1)+'-'+f(this.getUTCDate())+'T'+f(this.getUTCHours())+':'+f(this.getUTCMinutes())+':'+f(this.getUTCSeconds())+'Z';};var m={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};function stringify(value,whitelist){var a,i,k,l,r=/["\\\x00-\x1f\x7f-\x9f]/g,v;switch(typeof value){case'string':return r.test(value)?'"'+value.replace(r,function(a){var c=m[a];if(c){return c;}c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"':'"'+value+'"';case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}if(typeof value.toJSON==='function'){return stringify(value.toJSON());}a=[];if(typeof value.length==='number'&&!(value.propertyIsEnumerable('length'))){l=value.length;for(i=0;i<l;i+=1){a.push(stringify(value[i],whitelist)||'null');}return'['+a.join(',')+']';}if(whitelist){l=whitelist.length;for(i=0;i<l;i+=1){k=whitelist[i];if(typeof k==='string'){v=stringify(value[k],whitelist);if(v){a.push(stringify(k)+':'+v);}}}}else{for(k in value){if(typeof k==='string'){v=stringify(value[k],whitelist);if(v){a.push(stringify(k)+':'+v);}}}}return'{'+a.join(',')+'}';}}return{stringify:stringify,parse:function(text,filter){var j;function walk(k,v){var i,n;if(v&&typeof v==='object'){for(i in v){if(Object.prototype.hasOwnProperty.apply(v,[i])){n=walk(i,v[i]);if(n!==undefined){v[i]=n;}else{delete v[i];}}}}return filter(k,v);}if(/^[\],:{}\s]*$/.test(text.replace(/\\./g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof filter==='function'?walk('',j):j;}throw new SyntaxError('parseJSON');}};}();TAFFY["mergeObj"]=function(ob1,ob2){for(var n in ob2){if(ob2.hasOwnProperty(n))ob1[n]=ob2[n];}return ob1;};TAFFY["gatherUniques"]=function(a){var uniques=[];for(var z=0;z<a.length;z++){var d=true;for(var c=0;c<uniques.length;c++){if(uniques[c]==a[z])d=false;}if(d==true)uniques[uniques.length]=a[z];}return uniques;};(function(ts){for(var z=0;z<ts.length;z++){(function(y){TAFFY["is"+y]=function(c){return(TAFFY.typeOf(c)==y.toLowerCase())?true:false;}}(ts[z]))}}(["String","Number","Object","Array","Boolean","Null","Function","Undefined"]));TAFFY["IsNumeric"]=function(sT){var vC="0123456789";var IsN=true;for(var i=0;i<sT.length&&IsN==true;i++){if(vC.indexOf(sT.charAt(i))==-1)return false;}return IsN;};}
