/* calcWeight.js v0.21
   (c) Tale 2008       */
var calcWeightVersion=0.21;var calcWeight_current="";var calcWeight_avg="";var calcWeight_trimAvg="";var calcWeight_trim=10;var calcWeight_moveAvg="";var calcWeight_move=12*7;var calcWeight_normAvg="";var calcWeight_min="",calcWeight_max="";var calcWeight_data=null;var calcWeight_start=null;var calcWeight_graph=null;var calcWeight_graphMax=900;function calcWeightId(id){if(document.all)return document.all.item(id);else if(typeof(document.getElementById)!="undefined")return document.getElementById(id);return null;}function calcWeightDate(str){var date=str.match(/(\w+)\s+(\d+),\s*(\d+)/);if(!(date&&date.length==4))return null;date=new Date(date[1]+" "+date[2]+" "+date[3]);if(isNaN(date))return null;date.setHours(0,0,0,0);return date;}function calcWeightSort(a,b){return a[0]-b[0];}function calcWeight(tableID,deathDate,normFrom,normTo){if(!tableID)tableID="weightTable";if(normFrom&&normTo){normFrom=calcWeightDate(normFrom).valueOf();normTo=calcWeightDate(normTo).valueOf();}var table=calcWeightId(tableID);if(!(table&&table.rows))return false;var data=new Array();var prevDate=null,prevWeight;var n,row,date,weight,totalWeight;var days,day,delta,estimate;var i,l,d,w;var normTotal=0,normDays=0;for(n=table.rows.length-1;n>=0;n--){if(!table.rows[n])continue;if(table.rows[n].all)row=table.rows[n].all.tags("TD");else if(typeof(table.rows[n].getElementsByTagName)!="undefined")row=table.rows[n].getElementsByTagName("TD");if(!(row&&row.length>=2&&typeof(row[0].innerHTML)!="undefined"))continue;if(row[0].innerHTML.indexOf("#VET")>=0)continue;date=row[0].innerHTML;weight=row[1].innerHTML;if(!(date&&weight))continue;if(typeof(date.match)=="undefined")return false;date=calcWeightDate(date);if(!date)continue;weight=1*weight.match(/\d+/);if(!weight)continue;if(prevDate){days=Math.round((date.valueOf()-prevDate.valueOf())/1000/60/60/24);if(days>0){delta=(weight-prevWeight)/days;for(day=1;day<=days;day++){estimate=prevWeight+day*delta;i=data.length;data[i]=[estimate,0];l=data.length>=calcWeight_move?calcWeight_move:data.length;w=estimate;for(d=1;d<l;d++)w+=data[i-d][0];data[i][1]=w/l;totalWeight+=estimate;if(normFrom&&normTo){prevDate.setDate(prevDate.getDate()+1);if(prevDate.valueOf()>=normFrom&&prevDate.valueOf()<=normTo){normTotal+=estimate;normDays++;}}}}}else{totalWeight=weight;data[0]=[weight,totalWeight/1,totalWeight/1];if(normFrom&&normTo&&date.valueOf()>=normFrom&&date.valueOf()<=normTo){normTotal+=weight;normDays++;}calcWeight_start=new Date(date.valueOf());}if((!calcWeight_min||weight<calcWeight_min)&&(prevDate||weight>5))calcWeight_min=weight;if(!calcWeight_max||weight>calcWeight_max)calcWeight_max=weight;prevDate=date;prevWeight=weight;}if(!data.length)return false;i=data.length-1;calcWeight_current=Math.round(data[i][0]);calcWeight_avg=Math.round(totalWeight/data.length);calcWeight_moveAvg=!deathDate?Math.round(data[i][1]):"";if(normFrom&&normTo){if(normDays)calcWeight_normAvg=Math.round(normTotal/normDays);else calcWeight_normAvg=calcWeight_avg;}calcWeight_trimAvg=calcWeight_avg;if(window.calcWeight_trim){d=data.slice(0);d.sort(calcWeightSort);days=Math.floor(d.length*calcWeight_trim/100);totalWeight=0;for(day=days;day<d.length-days;day++)totalWeight+=d[day][0];days=d.length-2*days;if(days!=0)calcWeight_trimAvg=Math.round(totalWeight/days);}if(window.Silverlight){var div=calcWeightId("silverWeight");if(div)calcWeight_data=data;}return true;}function calcWeightTable(){document.writeln('<H4><A NAME="STATS" CLASS="Heading">Statistics</A></H4>');document.writeln('<P><TABLE WIDTH="65%" BORDER=0 CELLPADDING=0 CELLSPACING=0'+(window.stripeVersion?' ID="stripeMe"':'')+'><COL WIDTH="65%"><COL WIDTH="35%" ALIGN=CENTER><TBODY>');if(calcWeight_moveAvg){var move=calcWeight_move;if(move>7&&move%7==0)move=(move/7)+" weeks";else move=move+" days";document.writeln('<TR><TD><NOBR><SMALL><B>Moving average </B>(over the past '+move+')<B>:</B></SMALL>&nbsp;</NOBR></TD><TD>'+calcWeight_moveAvg+' grams&nbsp;<BR></TD></TR>');}if(calcWeight_normAvg)document.writeln('<TR><TD><NOBR><SMALL><B>Normalized average </B>(during adulthood)<B>:</B></SMALL>&nbsp;</NOBR></TD><TD>&#177;'+Math.round(calcWeight_normAvg/10)*10+' grams&nbsp;<BR></TD></TR>');if(calcWeight_trimAvg)document.writeln('<TR><TD><SMALL><B>Total average </B>('+calcWeight_trim+'% trimmed)<B>:</B></SMALL>&nbsp;</TD><TD>'+calcWeight_trimAvg+' grams&nbsp;<BR></TD></TR>');if(calcWeight_min)document.writeln('<TR><TD><SMALL><B>Minimum:</B></SMALL>&nbsp;</TD><TD>'+calcWeight_min+' grams&nbsp;<BR></TD></TR>');if(calcWeight_max)document.writeln('<TR><TD><SMALL><B>Maximum:</B></SMALL>&nbsp;</TD><TD>'+calcWeight_max+' grams&nbsp;<BR></TD></TR>');document.write('</TABLE>');if(window.stripeVersion)stripeIt();document.writeln('</P>');var el=calcWeightId("currentWeight");if(el&&typeof(el.innerHTML)!="undefined")el.innerHTML='(<A HREF="#DATA" TITLE="'+calcWeight_current+' grams" CLASS="Heading">'+calcWeight_current+'</A>)';return true;}function calcWeightDrawGraph(){if(!calcWeight_data){window.setTimeout("calcWeightDrawGraph();",250);return;}if(calcWeight_max>calcWeight_graphMax)calcWeight_graphMax=Math.floor(calcWeight_max/100+1)*100;var host=calcWeight_graph.getHost();var bg=host.content.createFromXaml('<Rectangle Stroke="#FF000000" StrokeThickness="1" Fill="#80A8A8A9" />');var textTpl='<TextBlock FontSize="11.5" FontFamily="Verdana,sans-serif" Foreground="Black" />';var text=host.content.createFromXaml(textTpl);text["Text"]="9999";var margin=0.75*text["ActualHeight"];bg["Width"]=Math.round(host.width-text["ActualHeight"]-margin-text["ActualWidth"]-margin-0.67*text["ActualWidth"]);bg["Height"]=Math.round(host.height-0.5*text["ActualHeight"]-margin-text["ActualHeight"]-margin-text["ActualHeight"]);bg["Canvas.Left"]=Math.round(host.width-bg["Width"]-0.67*text["ActualWidth"]);bg["Canvas.Top"]=Math.round(0.5*text["ActualHeight"]);var from=new Date(calcWeight_start.valueOf());from.setDate(1);var to=new Date(calcWeight_start.valueOf());to.setDate(to.getDate()+calcWeight_data.length-1);to.setDate(1);var size,value=0;do{value+=1;size=(to.getFullYear()*12+to.getMonth()-from.getFullYear()*12-from.getMonth());if(size==0)size=1;size=bg["Width"]*value/size;}while(size<1.25*text["ActualWidth"]);var n,grid,gridTpl='<Rectangle Stroke="#37000000" StrokeThickness="1" />';for(n=0;n<bg["Width"];n+=2*size){grid=host.content.createFromXaml(gridTpl);grid["Width"]=Math.round(size)+1;grid["Height"]=bg["Height"];grid["Canvas.Left"]=bg["Canvas.Left"]+Math.round(n);grid["Canvas.Top"]=bg["Canvas.Top"];if(grid["Canvas.Left"]+grid["Width"]>bg["Canvas.Left"]+bg["Width"])grid["Width"]-=grid["Canvas.Left"]+grid["Width"]-bg["Canvas.Left"]-bg["Width"];calcWeight_graph.children.Add(grid);}n=0;while(from.valueOf()<=to.valueOf()){text=host.content.createFromXaml(textTpl);text["Text"]=(from.getMonth()+1)+"/"+from.getFullYear().toString().substring(2,4);text["Canvas.Left"]=bg["Canvas.Left"]+Math.round(n*size-text["ActualWidth"]/2);text["Canvas.Top"]=bg["Canvas.Top"]+bg["Height"]+margin;calcWeight_graph.children.Add(text);from.setMonth(from.getMonth()+value);n++;}value=0;do{value+=100;size=bg["Height"]*value/calcWeight_graphMax;}while(size<2*text["ActualHeight"]);for(n=bg["Height"];n>=0;n-=2*size){grid=host.content.createFromXaml(gridTpl);grid["Width"]=bg["Width"];grid["Height"]=Math.round(size)+1;grid["Canvas.Left"]=bg["Canvas.Left"];grid["Canvas.Top"]=bg["Canvas.Top"]+Math.round(n-size)-1;if(grid["Canvas.Top"]<bg["Canvas.Top"]){grid["Height"]-=bg["Canvas.Top"]-grid["Canvas.Top"];grid["Canvas.Top"]=bg["Canvas.Top"];}calcWeight_graph.children.Add(grid);}for(n=0;n<=calcWeight_graphMax;n+=value){text=host.content.createFromXaml(textTpl);text["Text"]=n.toString();text["Canvas.Left"]=bg["Canvas.Left"]-Math.round(text["ActualWidth"])-margin;text["Canvas.Top"]=bg["Height"]-Math.round(n*size/value);calcWeight_graph.children.Add(text);}calcWeight_graph.children.Add(bg);if(window.calcWeight_graphImg){var img=host.content.createFromXaml('<Rectangle Stroke="#00000000" StrokeThickness="1" Opacity="0.1"><Rectangle.Fill><ImageBrush ImageSource="'+calcWeight_graphImg+'" Stretch="UniformToFill" /></Rectangle.Fill></Rectangle>');img["Width"]=bg["Width"];img["Height"]=bg["Height"];img["Canvas.Left"]=bg["Canvas.Left"];img["Canvas.Top"]=bg["Canvas.Top"];calcWeight_graph.children.Add(img);}textTpl='<TextBlock FontSize="10" FontFamily="Verdana,sans-serif" FontWeight="SemiBold" Foreground="White" />';text=host.content.createFromXaml(textTpl);text["Text"]="Date (month/year)";text["Canvas.Left"]=bg["Canvas.Left"]+Math.round((bg["Width"]-text["ActualWidth"])/2);text["Canvas.Top"]=bg["Canvas.Top"]+bg["Height"]+margin+text["ActualHeight"]+margin;calcWeight_graph.children.Add(text);text=host.content.createFromXaml(textTpl);text["Text"]="Weight (grams)";text["Canvas.Left"]=0;text["Canvas.Top"]=bg["Canvas.Top"]+text["ActualWidth"]+Math.round((bg["Height"]-text["ActualWidth"])/2);text["RenderTransform"]=host.content.createFromXaml('<RotateTransform Angle="-90" />');calcWeight_graph.children.Add(text);var line,lineTpl='<Line Stroke="#FFFFF0C0" StrokeThickness="3" />';var step=1;while(calcWeight_data.length/(step+1)>bg["Width"])step++;for(n=step;n<calcWeight_data.length;n+=step){from=calcWeight_data[n-step];to=calcWeight_data[n];line=host.content.createFromXaml(lineTpl);line["Stroke"]="#36000000";line["X1"]=bg["Canvas.Left"]+(n-step)*bg["Width"]/calcWeight_data.length;line["Y1"]=bg["Canvas.Top"]+bg["Height"]-(from[1]*bg["Height"]/calcWeight_graphMax);line["X2"]=bg["Canvas.Left"]+n*bg["Width"]/calcWeight_data.length;line["Y2"]=bg["Canvas.Top"]+bg["Height"]-(to[1]*bg["Height"]/calcWeight_graphMax);calcWeight_graph.children.Add(line);line=host.content.createFromXaml(lineTpl);line["X1"]=bg["Canvas.Left"]+(n-step)*bg["Width"]/calcWeight_data.length;line["Y1"]=bg["Canvas.Top"]+bg["Height"]-(from[0]*bg["Height"]/calcWeight_graphMax);line["X2"]=bg["Canvas.Left"]+n*bg["Width"]/calcWeight_data.length;line["Y2"]=bg["Canvas.Top"]+bg["Height"]-(to[0]*bg["Height"]/calcWeight_graphMax);calcWeight_graph.children.Add(line);}textTpl='<TextBlock FontSize="10" FontFamily="Verdana,sans-serif" Foreground="Black" />';text=host.content.createFromXaml(textTpl);text["Text"]="Moving average";n=text["Canvas.Left"]=bg["Canvas.Left"]+bg["Width"]-text["ActualWidth"]-margin;text["Canvas.Top"]=bg["Canvas.Top"]+bg["Height"]-text["ActualHeight"]-margin;calcWeight_graph.children.Add(text);line=host.content.createFromXaml(lineTpl);line["Stroke"]="#36000000";line["X1"]=n-3.5*margin;line["X2"]=n-margin;line["Y1"]=line["Y2"]=text["Canvas.Top"]+text["ActualHeight"]/2;calcWeight_graph.children.Add(line);text=host.content.createFromXaml(textTpl);text["Text"]="Actual weight";text["Canvas.Left"]=n;text["Canvas.Top"]=bg["Canvas.Top"]+bg["Height"]-2*text["ActualHeight"]-1.5*margin;calcWeight_graph.children.Add(text);line=host.content.createFromXaml(lineTpl);line["X1"]=n-3.5*margin;line["X2"]=n-margin;line["Y1"]=line["Y2"]=text["Canvas.Top"]+text["ActualHeight"]/2;calcWeight_graph.children.Add(line);calcWeight_data=null;}function calcWeightLoadGraph(sender,args){calcWeight_graph=sender;calcWeightDrawGraph();}function calcWeightGraph(){if(!window.Silverlight)return false;var div=calcWeightId("silverWeight");if(!div)return false;Silverlight.createObject("/rats/calcWeight.xaml",div,"silverWeightObj",{width:'600',height:'450',inplaceInstallPrompt:false,background:'#00989899',isWindowless:'true',framerate:'24',version:'1.0'},{onError:null,onLoad:null},null,null);return true;}