jQuery.fn.extend({sheet:function(e){e=jQuery.extend({urlGet:"documentation.html table:first",urlSave:"save.html",title:"",editable:true,urlBaseCss:"jquery.sheet.css",urlTheme:"theme/jquery-ui-1.7.2.custom.css",urlMenu:"menu.html",urlMenuJs:"plugins/mbMenu.min.js",urlMenuCss:"plugins/menu.css",urlMetaData:"plugins/jquery.metadata.js",urlScrollTo:"plugins/jquery.scrollTo-min.js",urlScrollsync:"plugins/scrollsync.js",urlJGCharts:"plugins/jgcharts.pack.js",loading:"Loading Spreadsheet...",newColumnWidth:120,ajaxSaveType:"POST",buildSheet:false,calcOff:false,log:false,lockFormulas:false,parent:this,colMargin:18,fnBefore:function(){},fnAfter:function(){jS.obj.formula().focus().select()},fnSave:function(){jS.saveSheet()},fnOpen:function(){var f=prompt("Paste your table html here");if(f){jS.obj.pane().html(f);jS.openSheet("",false,true)}},fnClose:function(){},joinedResizing:false},e);jQuery.fn.sheet.settings=jS.s=e;e.fnBefore();if(jS.s.buildSheet){jS.s.urlGet=null}jS.getCss(e.urlBaseCss);var b=jQuery(this).find("table").hide();jQuery(this).html(jS.s.loading);e.width=jQuery(e.parent).width();e.height=jQuery(e.parent).height();if(jS.s.log){jQuery(jS.s.parent).after('<textarea id="'+jS.id.log+'" />')}jS.log("Startup");var d=jQuery('<div id="'+jS.id.ui+'"><table cellpadding="0" cellspacing="0" border="0" class="tableControl ui-corner-bottom" style="border-collapse:collapse; border-width: 1px ! important; padding: none ! important; margin: none ! important;"><tbody><tr><td id="'+jS.id.barCornerParent+'"><div style="height: '+jS.s.colMargin+"; width: "+jS.s.colMargin+';" id="'+jS.id.barCorner+'" class="ui-icon ui-icon-refresh" onclick="jS.cellEditAbandon();">&nbsp;</div></td><td><div style="overflow: hidden;" id="'+jS.id.barTopParent+'" /></td></tr><tr><td><div style="overflow: hidden;width: '+jS.s.colMargin+';" id="'+jS.id.barLeftParent+'" /></td><td><div id="'+jS.id.pane+'"></div></td></tr></tbody></table></div>');for(var a in cE.fn){var c=a.toLowerCase();if(c!=a){cE.fn[c]=cE.fn[a]}}jS.obj.parent().html(d);if(b){jS.obj.pane().html(b)}jS.openSheet(jS.s.urlGet,jS.s.buildSheet,true)}});var jS=jQuery.sheet={version:0.53,s:{},obj:{parent:function(){return jQuery(jS.s.parent)},ui:function(){return jQuery("#"+jS.id.ui)},sheet:function(){return jQuery("#"+jS.id.sheet)},bar:function(){return jQuery("."+jS.cl.bar)},barTop:function(){return jQuery("#"+jS.id.barTop)},barTopParent:function(){return jQuery("#"+jS.id.barTopParent)},barLeft:function(){return jQuery("#"+jS.id.barLeft)},barLeftParent:function(){return jQuery("#"+jS.id.barLeftParent)},barCorner:function(){return jQuery("#"+jS.id.barCorner)},barCornerParent:function(){return jQuery("#"+jS.id.barCornerParent)},barSelected:function(){return jQuery("."+jS.cl.barSelected)},cell:function(){return jQuery("."+jS.cl.cell)},controls:function(){return jQuery("#"+jS.id.controls)},formula:function(){return jQuery("#"+jS.id.formula)},label:function(){return jQuery("#"+jS.id.label)},fx:function(){return jQuery("#"+jS.id.fx)},pane:function(){return jQuery("#"+jS.id.pane)},log:function(){return jQuery("#"+jS.id.log)},menu:function(){return jQuery("#"+jS.id.menu)},title:function(){return jQuery("#"+jS.id.title)},uiDefault:function(){return jQuery("."+jS.cl.uiDefault)},uiActive:function(){return jQuery("."+jS.cl.uiActive)},uiBase:function(){return jQuery("."+jS.cl.uiBase)},uiCell:function(){return jQuery("."+jS.cl.uiCell)},toggle:function(){return jQuery("."+jS.cl.toggle)},tableBody:function(){return document.getElementById(jS.id.sheet)},title:function(){return jQuery("#"+jS.id.title)}},id:{sheet:"jSheet",ui:"jSheetUI",barTop:"jSheetBarTop",barTopParent:"jSheetBarTopParent",barLeft:"jSheetBarLeft",barLeftParent:"jSheetBarLeftParent",barCorner:"jSheetBarCorner",barCornerParent:"jSheetBarCornerParent",controls:"jSheetControls",formula:"jSheetControls_formula",label:"jSheetControls_loc",fx:"jSheetControls_fx",pane:"jSheetEditPane",log:"jSheetLog",menu:"jSheetMenu",title:"sheetTitle"},cl:{sheet:"jSheet",bar:"jSheetBar",cell:"jSheetCellActive",calcOff:"jSheetCalcOff",barSelected:"jSheetBarItemSelected",uiDefault:"ui-state-default",uiActive:"ui-state-active",uiBase:"ui-widget-content",uiParent:"ui-widget-content ui-corner-all",uiBar:"ui-widget-header",uiPane:"ui-widget-content",uiMenuUl:"ui-widget-header",uiMenuLi:"ui-widget-header",uiMenuHighlighted:"ui-state-highlight",uiControl:"ui-widget-header ui-corner-top",uiControlTextBox:"ui-widget-content",uiCell:"themeRoller_activeCell",uiCellHighlighted:"ui-state-highlight",toggle:"cellStyleToggle"},ERROR:function(){return cE.ERROR},tuneTableForSheetUse:function(b,a){b.addClass(jS.cl.sheet).attr("id",jS.id.sheet).attr("border","1px");b.find("."+jS.cl.uiCell).removeClass(jS.cl.uiCell);b.find("td").css("background-color","").css("color","").css("height","").attr("height","");if(a){return b}},attrH:{width:function(b,a){return jQuery(b).outerWidth()-jS.attrH.boxModelCorrection(a)},widthReverse:function(b,a){return jQuery(b).outerWidth()+jS.attrH.boxModelCorrection(a)},height:function(b,a){return jQuery(b).outerHeight()-jS.attrH.boxModelCorrection(a)},heightReverse:function(b,a){return jQuery(b).outerHeight()+jS.attrH.boxModelCorrection(a)},syncSheetWidthFromTds:function(b){var a=0;b=(b?b:jS.obj.sheet());b.find("tr:first").find("td").each(function(){a+=jQuery(this).width()});b.width(a)},boxModelCorrection:function(b){var a=0;if(jQuery.support.boxModel&&!b){a=2}return a},setHeight:function(b,g,e,f){var a=0;var d=0;var c;switch(g){case"cell":f=(f?f:jS.obj.barLeft().find("div").eq(b));d=jS.attrH.height(jQuery(jS.getTd(null,b+1,1)).parent().andSelf(),e);break;case"bar":f=(f?f:jQuery(jS.getTd(null,b+1,1)).parent().andSelf());d=jS.attrH.heightReverse(jS.obj.barLeft().find("div").eq(b),e);break}if(d){jQuery(f).height(d).css("height",d).attr("height",d)}return f}},makeBarItemLeft:function(c){jS.obj.barLeft().remove();var a=jQuery('<div border="1px" id="'+jS.id.barLeft+'" />').height("10000px");var b;if(c){b=function(d,e,f){f.height(parseInt(e.outerHeight())-jS.attrH.boxModelCorrection())}}else{b=function(d,e,f){f.height(parseInt(e.css("height").replace("px",""))-jS.attrH.boxModelCorrection())}}jS.obj.sheet().find("tr").each(function(d){var e=jQuery("<div>"+(d+1)+"</div>");jQuery(a).append(e);b(d,jQuery(this),e);jS.getResizeControl.height(e)});a.appendTo(jS.obj.barLeftParent())},makeBarItemTop:function(b){jS.obj.barTop().remove();var c=jQuery('<div id="'+jS.id.barTop+'" class="'+jS.cl.bar+'" />').width("10000px");c.height(jS.s.colMargin);var a;var d;if(b){a=jS.obj.pane().find("tr:first td");d=function(e){return jS.attrH.width(e)}}else{a=jS.obj.pane().find("col");d=function(e){return parseInt(jQuery(e).css("width").replace("px",""))-jS.attrH.boxModelCorrection()}}a.each(function(g){var f=cE.columnLabelString(g+1);var e=d(this);var h=jQuery("<div>"+f+"</div>").width(e).height(jS.s.colMargin);jS.getResizeControl.width(h);c.append(h)});jS.obj.barTopParent().append(c)},setTdIds:function(){jS.obj.sheet().find("tr").each(function(a){jQuery(this).find("td").each(function(b){jQuery(this).attr("id",jS.getTdId(a+1,b+1))})})},toggleHide:{row:function(a){if(!a){a=jS.obj.cell().parent().attr("rowIndex")}if(a){var b=jS.obj.barLeft().find("div").eq(a);if(b.is(":visible")){b.hide();jS.obj.sheet().find("tr").eq(a).hide()}else{b.show();jS.obj.sheet().find("tr").eq(a).show()}}else{alert("No row selected.")}},rowAll:function(){jS.obj.sheet().find("tr").show();jS.obj.barLeft().find("div").show()},column:function(a){if(!a){a=jS.obj.cell().attr("cellIndex")}if(a){var b=jS.obj.barTop().find("div").eq(a);if(b.is(":visible")){jS.obj.sheet().find("tbody tr").each(function(){jQuery(this).find("td").eq(a).hide()});b.hide();jS.obj.sheet().find("colgroup col").eq(a).hide();jS.toggleHide.columnSizeManage()}}else{alert("Now column selected.")}},columnAll:function(){},columnSizeManage:function(){var a=jS.obj.barTop().width();var b=0;var b=0;jS.obj.barTop().find("div").each(function(){var c=jQuery(this);if(c.is(":hidden")){b+=c.width()}});jS.obj.barTop().width(a);jS.obj.sheet().width(a)}},getResizeControl:{height:function(a){if(jS.s.editable){a.unbind("mousedown").unbind("dblclick").mousedown(function(b){jS.barResizer(b,jQuery(this),"row");return false}).dblclick(function(){jS.cellEditAbandon();var b=jQuery.trim(jQuery(this).text());b=parseInt(b);jS.cellSetActiveMultiRow(b)})}},width:function(a){if(jS.s.editable){a.unbind("mousedown").unbind("dblclick").mousedown(function(b){jS.barResizer(b,jQuery(this),"column");return false}).dblclick(function(){jS.cellEditAbandon();var b=cE.columnLabelIndex(jQuery.trim(jQuery(this).text()));b=parseInt(b);jS.cellSetActiveMultiColumn(b)})}}},makeControls:function(b){jS.obj.controls().remove();if(jS.s.editable){jS.obj.pane().find("td").mousedown(jS.cellOnMouseDown).click(jS.cellOnClick);var a=jQuery('<div id="'+jS.id.controls+'" />');var c=jS.sheetTitle(true);if(jS.s.urlMenuJs&&jS.s.urlMenuCss&&jS.s.urlMetaData&&jS.s.urlMenu){jQuery.getScript(jS.s.urlMetaData);jQuery.getScript(jS.s.urlMenuJs,function(){jS.getCss(jS.s.urlMenuCss);var d=jQuery("<div />").load(jS.s.urlMenu,function(){a.prepend(d.html());jS.obj.menu().buildMenu({additionalData:"pippo=1",menuWidth:100,openOnRight:false,menuSelector:".menuContainer",hasImages:false,fadeInTime:0,fadeOutTime:0,adjustLeft:2,minZindex:"auto",adjustTop:10,opacity:0.95,shadow:true,closeOnMouseOut:false,closeAfter:1000});jS.obj.menu().find("#titleHolder").append('<span id="'+jS.id.title+'">'+c+"</span>");jS.sheetSyncSize()})})}else{a.append('&nbsp;&nbsp;&nbsp;<span id="'+jS.id.title+'">'+c+"</span>")}a.append('<table style="width: 100%;"><tr><td style="width: 35px; text-align: right;" id="'+jS.id.label+'"></td><td style="width: 10px;" id="'+jS.id.fx+'">fx</td><td><textarea id="'+jS.id.formula+'"></textarea></td></tr></table>');if(jS.s.urlScrollTo){jQuery.getScript(jS.s.urlScrollTo)}a.keydown(function(d){return jS.formulaKeyDown(d)});jQuery(b).prepend(a);jS.sheetSyncSize()}else{jS.sheetSyncSize()}},sheetDecorate:function(){jS.sheetSyncSizeToCols();jS.formatSheet();jS.sheetDecorateRemove(jS.obj.sheet());jS.makeControls(jS.obj.ui())},formatSheet:function(){if(jS.obj.parent().find("tbody").length<1){jS.obj.sheet().wrap("<tbody />")}if(jS.obj.parent().find("colgroup").length<1){var a=jQuery("<colgroup />");jS.obj.sheet().find("tr:first").find("td").each(function(){jQuery("<col />").width(jS.s.newColumnWidth).css("width",jS.s.newColumnWidth).attr("width",jS.s.newColumnWidth).appendTo(a)});a.prependTo(jS.obj.sheet())}},getCss:function(a,b){jQuery("head").append('<link rel="stylesheet" type="text/css" href="'+a+'"></link>')},themeRoller:{start:function(){jS.getCss(jS.s.urlTheme);jS.obj.parent().addClass(jS.cl.uiParent);jS.obj.sheet().addClass(jS.cl.uiParent);jS.obj.barLeft().find("div").addClass(jS.cl.uiBar);jS.obj.barTop().find("div").addClass(jS.cl.uiBar);jS.obj.barCornerParent().addClass(jS.cl.uiBar);jS.obj.controls().addClass(jS.cl.uiControl);jS.obj.fx().addClass(jS.cl.uiControl);jS.obj.label().addClass(jS.cl.uiControl);jS.obj.formula().addClass(jS.cl.uiControlTextBox)},cell:function(a){jS.themeRoller.clearCell();if(a){jQuery(a).addClass(jS.cl.uiCellHighlighted).addClass(jS.cl.uiCell)}},clearCell:function(){jS.obj.uiActive().removeClass(jS.cl.uiActive);jS.obj.uiCell().removeAttr("style").removeClass(jS.cl.uiCellHighlighted).removeClass(jS.cl.uiCell)},newBar:function(a){jQuery(a).addClass(jS.cl.uiBar)},barTop:function(a){jS.obj.barTop().find("div").eq(a-1).addClass(jS.cl.uiActive)},barLeft:function(a){jS.obj.barLeft().find("div").eq(a-1).addClass(jS.cl.uiActive)},barObj:function(a){jQuery(a).addClass(jS.cl.uiActive)},clearBar:function(){jS.obj.barTop().find("."+jS.cl.uiActive).removeClass(jS.cl.uiActive);jS.obj.barLeft().find("."+jS.cl.uiActive).removeClass(jS.cl.uiActive)}},manageHtmlToText:function(a){a=jQuery.trim(a);if(a.charAt(0)!="="){a=a.replace(/&nbsp;/g," ").replace(/&gt;/g,">").replace(/&lt;/g,"<").replace(/\t/g,"").replace(/\n/g,"").replace(/<br>/g,"\r").replace(/<BR>/g,"\n");jS.log("from html to text")}return a},manageTextToHtml:function(a){a=jQuery.trim(a);if(a.charAt(0)!="="){a=a.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;").replace(/ /g,"&nbsp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/\n/g,"<br>").replace(/\r/g,"<br>");jS.log("from text to html")}return a},sheetDecorateRemove:function(b,a){jQuery(b).find("."+jS.cl.cell).removeClass(jS.cl.cell);jQuery(b).find("."+jS.cl.uiCellHighlighted).removeClass(jS.cl.uiCellHighlighted);jQuery(b).find("."+jS.cl.uiCell).removeClass(jS.cl.uiCell);jQuery("col",b).each(function(d){var c=jQuery(this).css("width")+"px";jQuery(b).find("col").eq(d).attr("width",c)});if(a){jQuery(b).find("td").andSelf().css("height","").attr("height","")}},cellIsEdit:false,sheetIsEdit:false,getCellClickFn:function(){return(jS.s.lockFormulas?jS.cellOnClickLocked:jS.cellOnClickReg)},cellClick:function(c){var b=0;var a=0;switch(c){case key.UP:a--;break;case key.DOWN:a++;break;case key.LEFT:b--;break;case key.RIGHT:b++;break}jQuery(jS.getTd(null,jS.cellEditLastLoc[1]+a,jS.cellEditLastLoc[2]+b)).click();return false},cellOnMouseDown:function(a){if(a.altKey){jS.cellSetActiveMulti(a);jQuery(document).mouseup(function(){jQuery(this).unbind("mouseup");var b=jS.obj.formula().val();jS.obj.formula().val(b+jS.getTdRange())})}else{jS.cellSetActiveMulti(a)}switch(!isNaN(a.target.cellIndex)){case true:jS.obj.formula().focus().select();return false}},cellOnClickLocked:function(a){if(!isNaN(a.target.cellIndex)){if(!jQuery(a.target).attr("formula")){jS.cellOnClickManage(jQuery(a.target))}}else{jSCellEditAbandon();jS.obj.formula().focus().select()}},cellOnClickReg:function(a){if(!isNaN(a.target.cellIndex)){jS.cellOnClickManage(jQuery(a.target))}else{jS.obj.formula().focus().select();return false}},cellOnClickManage:function(a){if(!isNaN(a[0].cellIndex)){if(!a.hasClass(jS.cl.cell)){jS.cellEdit(a);jS.log("click cell")}else{jS.cellIsEdit=true;jS.isSheetEdit=true;jS.cellTextArea(a,false,true);jS.log("click, textarea over table activated")}jS.followMe(a)}else{return false}},cellEdit:function(c){jS.cellEditDone();var b=jS.getTdLocation(c);jS.obj.label().html(cE.columnLabelString(b[1])+b[0]);var a=c.attr("formula");if(!a){a=jS.manageHtmlToText(c.html())}jS.obj.formula().val(a).focus().select();jS.cellSetActive(c,b)},cellSetActive:function(b,a){jS.cellEditLastLoc[0]=b;jS.cellEditLastLoc[1]=a[0];jS.cellEditLastLoc[2]=a[1];jS.themeRoller.cell(b);jS.themeRoller.barLeft(a[0]);jS.themeRoller.barTop(a[1]);b.addClass(jS.cl.cell);jS.obj.barLeft().find("div").eq(a[0]-1).addClass(jS.cl.barSelected);jS.obj.barTop().find("div").eq(a[1]-1).addClass(jS.cl.barSelected)},cellEditLastLoc:new Array(),cellEditDone:function(g){switch(jS.cellIsEdit){case true:var l=[jS.cellEditLastLoc[1],jS.cellEditLastLoc[2]];var d=jS.cellEditLastLoc[0];var m=false;if(d&&l&&d.hasClass(jS.cl.cell)){var q=jS.cellTextArea(d,true);var p=false;var i=false;var b=false;var a=false;var o=false;var h=false;var c=false;var k=false;var j=false;var n=false;var f=d.attr("formula");var e=d.attr("prevVal");if(q){if(q.charAt(0)=="="){if(q!=f){a=true;jS.log("edit, new formula, possibly had formula")}else{if(f){p=true;jS.log("no edit, has formula")}else{jS.log("no edit, has formula, unknown action")}}}else{if(f){h=true;jS.log("edit, new value, had formula")}else{if(!isNaN(parseInt(q))){if((q!=e&&q!=jS.obj.formula().val())||(d.text()!=q)){j=true;jS.log("edit, from number to number, possibly in function")}else{i=true;jS.log("no edit, is a number")}}else{c=true;jS.log("possible edit from textarea, has value")}}}}else{if(d.html().length>0&&f){o=true;jS.log("edit, null value from formula")}else{if(d.html().length>0&&f){k=true;jS.log("edit, null value from formula")}else{b=true;jS.log("no edit, null value")}}}d.removeAttr("prevVal");q=jS.manageTextToHtml(q);if(p){d.html(e)}else{if(a){m=true;d.attr("formula",q).html("")}else{if(o){m=true;d.removeAttr("formula").html(q)}else{if(h){m=true;d.removeAttr("formula").html(q)}else{if(c){d.html(q)}else{if(i){d.html(q)}else{if(b){d.html(q)}else{if(j){m=true;d.html(q)}else{if(k){m=true;d.removeAttr("formula").html("")}}}}}}}}}if(m){jS.calc(jS.obj.sheet())}if(g!=false){jS.sheetClearActive()}jS.obj.barLeft().find("div").eq(l[0]-1).height(jS.attrH.height(d.parent()));jS.obj.formula().focus().select();jS.cellIsEdit=false}break;default:jS.attrH.setHeight(jS.cellEditLastLoc[2]-1,"cell",false);jS.sheetClearActive()}},cellEditAbandon:function(b){jS.themeRoller.clearCell();jS.themeRoller.clearBar();if(!b){var a=jS.cellTextArea(jS.cellEditLastLoc[0],true);if(a){jS.cellEditLastLoc[0].html(jS.manageTextToHtml(a));jS.sheetClearActive();if(a.charAt(0)=="="){jS.calc(jS.obj.sheet())}}else{jS.sheetClearActive();jS.calc(jS.obj.sheet())}}jS.cellEditLastLoc[0]=jS.obj.sheet().find("td:first");jS.cellEditLastLoc[1]=0;jS.cellEditLastLoc[2]=1;jS.obj.label().html("");return false},keyDownHandler:{enterOnTextArea:function(a){if(a.ctrlKey){return jS.cellClick(key.DOWN)}else{return true}},enter:function(a){if(!jS.cellIsEdit&&!a.ctrlKey){return jS.cellClick()}else{return jS.cellClick(key.DOWN)}},tab:function(a){if(a.shiftKey){return jS.cellClick(key.LEFT)}else{return jS.cellClick(key.RIGHT)}},textAreaKeyDown:function(a){switch(a.keyCode){case key.ENTER:return jS.keyDownHandler.enterOnTextArea(a);break;case key.TAB:return jS.keyDownHandler.tab(a);break}},formulaKeyDown:function(a){switch(a.keyCode){case key.ESCAPE:jS.cellEditAbandon();break;case key.TAB:return jS.keyDownHandler.tab(a);break;case key.ENTER:return jS.keyDownHandler.enter(a);break;case key.LEFT:case key.UP:case key.RIGHT:case key.DOWN:return jS.cellClick(a.keyCode);break;default:jS.cellIsEdit=true}}},formulaKeyDown:function(a,b){return(b?jS.keyDownHandler.textAreaKeyDown(a):jS.keyDownHandler.formulaKeyDown(a))},cellStyleToggle:function(b,a){if(a){a=a.split(",");jQuery(a).each(function(){jS.obj.uiCell().removeClass(this)})}if(jS.obj.uiCell().hasClass(b)){jS.obj.uiCell().removeClass(b)}else{jS.obj.uiCell().addClass(b)}jS.obj.formula().focus().select();return false},context:{},calc:function(b,a){jS.log("Calculation Started");if(b&&!jS.s.calcOff){cE.calc(new jS.tableCellProvider(b.id),jS.context,a);jS.isSheetEdit=false}jS.log("Calculation Ended")},cellTextArea:function(i,d,a,e){var b;if(i){if(!a){var g=i.find("textarea");var f=g.val();if(f||jS.obj.formula().attr("disabled")){jS.log("Textarea value used");b=f;g.remove()}else{jS.log("Formula value used");b=jS.obj.formula().val()}jS.obj.formula().removeAttr("disabled")}else{if(e){b=e}else{b=jS.obj.formula().val()}jS.obj.formula().attr("disabled","true");var g=jQuery('<textarea id="tempText" />');var c=jS.attrH.height(i);i.parent().height(c+jS.attrH.boxModelCorrection());g.height(c<75?75:c).val(b).click(function(){return false}).keydown(function(h){return jS.formulaKeyDown(h,true)});if(i.attr("formula")){i.attr("prevVal",i.text()).removeAttr("formula")}i.html(g);g.focus().select()}if(d){return b}}},refreshLabelsColumns:function(){var a=0;jS.obj.barTop().find("div").each(function(b){jQuery(this).text(cE.columnLabelString(b+1));a+=jQuery(this).width()});return a},refreshLabelsRows:function(){jS.obj.barLeft().find("div").each(function(a){jQuery(this).text((a+1))})},addRowMulti:function(b){if(!b){b=prompt("How many rows would you like to add?")}if(b){for(var a=0;a<=b;a++){jS.addRow()}}jS.setTdIds()},addColumnMulti:function(b){if(!b){b=prompt("How many columns would you like to add?")}if(b){for(var a=0;a<=b;a++){jS.addColumn()}}jS.setTdIds()},addRow:function(f,e){if(!f){f=":last"}else{if(f==true){f=":eq("+(jS.cellEditLastLoc[1]-1)+")"}else{f=":eq("+(f-1)+")"}}jS.cellEditAbandon();var d=jS.obj.sheet().find("tr"+f);var c=d.clone();jQuery("td",c).html("").attr("class","").attr("formula","").css("background-color","").removeAttr("function").unbind("click").unbind("mousedown").mousedown(jS.cellOnMouseDown).click(jS.getCellClickFn());if(e){c.insertBefore(d)}else{c.insertAfter(d)}var b=jS.obj.barLeft().find("div"+f);var a=b.clone();jS.themeRoller.newBar(a);jS.getResizeControl.height(a.html(parseInt(b.text())+1).removeClass(jS.cl.uiActive).height(jS.attrH.height(c)));jS.log("New row at: "+(parseInt(b.text())+1));if(e){a.insertBefore(b)}else{a.insertAfter(b)}if(f){jS.obj.barLeft().find("div").each(function(g){jQuery(this).text(g+1)})}jS.setTdIds();jS.obj.pane().scroll()},addColumn:function(l,g){if(!l){l=":last"}else{if(l==true){l=":eq("+(jS.cellEditLastLoc[2]-1)+")"}else{l=":eq("+(l-1)+")"}}jS.cellEditAbandon();var f=jS.obj.barTop().find("div"+l);var b=jS.obj.sheet().find("col"+l);var h=f.clone().width(jS.s.newColumnWidth-jS.attrH.boxModelCorrection());jS.getResizeControl.width(h);var c=b.clone().width(jS.s.newColumnWidth);var e=jQuery("<td></td>");var i=cE.columnLabelIndex(f.text());var a=cE.columnLabelString(i+1);jS.log("New Column: "+i+", "+a);if(g){b.before(c);f.before(h)}else{b.after(c);f.after(h)}var d=0;var k;if(g){k=function(j){jQuery(j).find("td"+l).before(e.clone().mousedown(jS.cellOnMouseDown).click(jS.getCellClickFn()))}}else{k=function(j){jQuery(j).find("td"+l).after(e.clone().mousedown(jS.cellOnMouseDown).click(jS.getCellClickFn()))}}jS.obj.sheet().find("tr").each(function(j){k(this);d++});jS.log("Sheet length: "+d);if(l){jS.obj.barTop().find("div").each(function(j){jQuery(this).text(cE.columnLabelString(j+1))})}jS.attrH.syncSheetWidthFromTds();jS.setTdIds();jS.obj.pane().scroll()},deleteRow:function(){if(jS.obj.cell()[0]){var a=confirm("Are you sure that you want to delete that row? Fomulas will not be updated.");if(a){var b=jS.cellEditLastLoc[1]-1;jS.obj.barLeft().find("div").eq(b).remove();jS.obj.sheet().find("tr").eq(b).remove();jS.obj.formula().val("");jS.setTdIds();jS.refreshLabelsRows()}}jS.obj.pane().scroll()},deleteColumn:function(){if(jS.obj.cell()[0]){var b=confirm("Are you sure that you want to delete that column? Fomulas will not be updated.");if(b){var c=jS.cellEditLastLoc[2]-1;jS.obj.barTop().find("div").eq(c).remove();jS.obj.sheet().find("colgroup col").eq(c).remove();jS.obj.sheet().find("tr").each(function(d){jQuery(this).find("td").eq(c).remove()});jS.obj.formula().val("");var a=jS.refreshLabelsColumns();jS.setTdIds();jS.obj.sheet().width(a)}}jS.obj.pane().scroll()},sheetTitle:function(e,d){var c="";if(e){var a=new Array();a[3]=jS.s.title;a[2]=jS.obj.sheet().attr("sheettitle");a[1]=jS.obj.sheet().find('td:contains("sheettitle")').text().split(":")[1];a[0]="Untitled Spreadsheet";var b=a.length;while(!c){c=a[b];b--}return c}else{if(!d){d=prompt("What would you like the sheet's title to be?",jS.obj.title().text())}switch(d){case"":d="Untitled Spreadsheet";break;case null:d=jS.sheetTitle(true)}jS.obj.sheet().attr("sheettitle",d);jS.obj.title().html(d)}},viewSource:function(c){var d=jS.obj.sheet().clone()[0];jS.sheetDecorateRemove(d);if(c){var b=jS.HTMLtoPrettySource(d)}else{var b=jQuery("<div />").html(d).html()}var a=window.open();a.document.write("<html><body><xmp>"+b+"\n</xmp></body></html>");a.document.close();return false},saveSheet:function(){var a=jS.obj.sheet().clone()[0];jS.sheetDecorateRemove(a);var b=jQuery("<div />").html(a).html();jQuery.ajax({url:jS.s.urlSave,type:jS.s.ajaxSaveType,data:"s="+b,dataType:"html",success:function(c){alert("Success! - "+c)}})},HTMLtoCompactSource:function(f){var b="";if(f.nodeType==1){b+="<"+f.tagName;d=false;var h=f.attributes.length;for(var e=0,d=false;e<h;e++){var c=f.attributes[e].name;var g=f.getAttribute(c);if(g){if(c=="contentEditable"&&g=="inherit"){continue}if(c=="class"){d=true;jQuery(g).removeClass(jS.cl.cell)}if(typeof(g)=="string"){b+=" "+c+'="'+g.replace(/"/g,"'")+'"'}else{if(c=="style"&&g.cssText){b+=' style="'+g.cssText+'"'}}}}if(f.tagName=="TABLE"&&!d){b+=' class="jSheet"'}if(f.tagName=="COL"){b+="/>"}else{b+=">";var a="";jQuery(f.childNodes).each(function(){a+=jS.HTMLtoCompactSource(this)});b+=a;b+="</"+f.tagName+">"}}else{if(f.nodeType==3){b+=f.data.replace(/^\s*(.*)\s*$/g,"$1")}}return b},HTMLtoPrettySource:function(e,f){if(!f){f=""}var b="";if(e.nodeType==1){b+="\n"+f+"<"+e.tagName;var h=e.attributes.length;for(var d=0;d<h;d++){var c=e.attributes[d].name;var g=e.getAttribute(c);if(g){if(c=="contentEditable"&&g=="inherit"){continue}if(typeof(g)=="string"){b+=" "+c+'="'+g.replace(/"/g,"'")+'"'}else{if(c=="style"&&g.cssText){b+=' style="'+g.cssText+'"'}}}}if(e.childNodes.length<=0){b+="/>"}else{b+=">";var a="";var h=e.childNodes.length;for(var d=0;d<h;d++){a+=jS.HTMLtoPrettySource(e.childNodes[d],f+"  ")}b+=a;if(a.indexOf("\n")>=0){b+="\n"+f}b+="</"+e.tagName+">"}}else{if(e.nodeType==3){b+=e.data.replace(/^\s*(.*)\s*$/g,"$1")}}return b},barAdjustorCache:new Array(),barAdjustor:function(a){if(!a){if(!this.barAdjustorCache.length){this.barAdjustorCache[0]=jS.obj.pane();this.barAdjustorCache[1]=jS.obj.barTop();this.barAdjustorCache[2]=jS.obj.barLeft()}else{this.barAdjustorCache[1].stop().animate({left:"-"+this.barAdjustorCache[0].scrollLeft()},50);this.barAdjustorCache[2].stop().animate({top:"-"+this.barAdjustorCache[0].scrollTop()},50)}this.barAdjustorCache[3]=window.setTimeout("jS.barAdjustor()",500)}else{window.clearTimeout(this.barAdjustorCache[3]);this.barAdjustorCache=new Array()}},followMe:function(a){if(jS.s.urlScrollTo){jS.obj.pane().stop().scrollTo(a,{margin:true,axis:"xy",duration:100,offset:{top:-jS.s.height/3,left:-jS.s.width/5}})}},count:{rows:function(){return parseInt(jQuery.trim(jS.obj.barLeft().find("div:last").text()))},columns:function(){return parseInt(jS.columnLabelIndex(jQuery.trim(jS.obj.barTop().find("div:last").text())))}},openSheet:function(b,d,c){jS.obj.pane().unbind("mousedown").unbind("click");function a(e){if(e){jS.obj.pane().html(jQuery(e).show())}else{e=jS.obj.pane().find("table:first").show()}e=jS.tuneTableForSheetUse(e,true);jS.sheetDecorate();jS.makeBarItemTop(b);jS.makeBarItemLeft(b);jS.sheetTitle(true);if(jS.s.editable){jS.obj.pane().mousedown(jS.cellOnMouseDown).click(jS.getCellClickFn())}jS.themeRoller.start();jS.setTdIds();if(jS.s.urlScrollsync){jQuery.getScript(jS.s.urlScrollsync,function(){jS.obj.pane().add(jS.obj.barLeftParent()).scrollsync({axis:"y"});jS.obj.pane().add(jS.obj.barTopParent()).scrollsync({axis:"x"})})}else{jS.barAdjustor()}if(jS.s.urlJGCharts){jQuery.getScript(jS.s.urlJGCharts,function(){jS.calc(jS.obj.sheet())})}else{jS.calc(jS.obj.sheet())}jS.log("End startup")}if(c?true:confirm("Are you sure you want to open a different sheet?  All unsaved changes will be lost.")){jS.cellEditAbandon(true);if(!d){if(b){jQuery("<div />").load(b,function(){a(jQuery(this).find("table:first"))})}else{a()}}else{if(d.toLowerCase().match("x")){jS.s.title="";a(jS.buildSheet(d))}else{a()}}}},newSheet:function(){var a=prompt("What size would you like to make your spreadsheet? Example: '5x10' creates a sheet that is 5 columns by 10 rows.");if(a){jS.openSheet("",a)}},importRow:function(b){jS.addRow();var a="";jS.obj.sheet().find("tr:last td").each(function(c){jQuery(this).removeAttr("formula");try{if((b[c]+"").charAt(0)=="="){jQuery(this).attr("formula",b[c])}else{jQuery(this).html(b[c])}}catch(d){a+=d+";\n"}});if(a){alert(a)}jS.setTdIds();jS.calc(jS.obj.sheet())},importColumn:function(a){jS.addColumn();var b="";jS.obj.sheet().find("tr").each(function(c){var f=jQuery(this).find("td:last");try{if((a[c]+"").charAt(0)=="="){f.attr("formula",a[c])}else{f.html(a[c])}}catch(d){b+=d+";\n"}});if(b){alert(b)}jS.setTdIds();jS.calc(jS.obj.sheet())},buildSheet:function(j){if(!j){j=jS.s.buildSheet}j=j.toLowerCase().split("x");var b=parseInt(j[0]);var e=parseInt(j[1]);var h=jQuery('<table border="1px" class="'+jS.cl.sheet+'" id="'+jS.id.sheet+'"></table>');var a="<td> </td>";var c="";for(var d=b;d>=1;d--){c+=a}var g='<tr height="'+jS.s.colMargin+'" style="height: '+jS.s.colMarg+';">'+c+"</tr>";var f="";for(var d=e;d>=1;d--){f+=g}h.html("<tbody>"+f+"</tbody>");jS.attrH.syncSheetWidthFromTds(h);return h},sheetSyncSizeToDivs:function(){var a=0;jS.obj.barTop().find("div").each(function(){a+=parseInt(jQuery(this).outerWidth())});jS.obj.sheet().width(a)},sheetSyncSizeToCols:function(){var a=0;jS.obj.sheet().find("colgroup col").each(function(){a+=jQuery(this).width()});jS.obj.sheet().width(a)},sheetSyncSize:function(){var b=jS.s.height;if(!b){b=400}else{if(b<200){b=200}}var a=jS.s.width;jS.obj.pane().height((b-jS.attrH.height(jS.obj.controls()))-(jS.s.colMargin+6)).width(a);jS.obj.barLeftParent().height(jS.obj.pane().height());jS.obj.barTopParent().height(jS.s.colMargin).width(jS.obj.pane().width());jS.themeRoller.start()},columnResizer:{xyDimension:0,getIndex:function(a){return cE.columnLabelIndex(jQuery.trim(a.text()))-1},getSize:function(a){return jS.attrH.width(a,true)},setSize:function(b,a){b.width(a)},setDesinationSize:function(a){jS.sheetSyncSizeToDivs();jS.obj.sheet().find("col").eq(this.i).width(a).css("width",a).attr("width",a);jS.obj.pane().scroll()}},rowResizer:{xyDimension:1,getIndex:function(a){return parseInt(jQuery.trim(a.text()))-1},getSize:function(a){return jS.attrH.height(a,true)},setSize:function(b,a){if(a){b.height(a).css("height",a).attr("height",a)}return jS.attrH.height(b)},setDesinationSize:function(){jS.attrH.setHeight(this.i,"bar",true);jS.attrH.setHeight(this.i,"cell",false);jS.obj.pane().scroll()}},barResizer:function(b,d,c){var e=(c=="row"?jS.rowResizer:jS.columnResizer);var a={start:function(f){jS.log("start resize");e.offset=d.offset();e.tdPageXY=[e.offset.left,e.offset.top][e.xyDimension];e.startXY=[f.pageX,f.pageY][e.xyDimension];e.i=e.getIndex(d);e.srcBarSize=e.getSize(d);e.edgeDelta=e.startXY-(e.tdPageXY+e.srcBarSize);e.min=15;if(jS.s.joinedResizing){e.resizeFn=function(g){e.setDesinationSize(g);e.setSize(d,g)}}else{e.resizeFn=function(g){e.setSize(d,g)}}if(Math.abs(e.edgeDelta)<=e.min){jQuery(document).mousemove(a.drag).mouseup(a.stop)}},drag:function(f){var h=e.min;var g=e.srcBarSize+([f.pageX,f.pageY][e.xyDimension]-e.startXY);if(g>0){h=Math.max(g,e.min)}e.resizeFn(h);return false},stop:function(f){e.setDesinationSize(e.getSize(d));jQuery(document).unbind("mousemove").unbind("mouseup");jS.obj.formula().focus().select();jS.log("stop resizing")}};a.start(b)},cellFind:function(a){if(!a){a=prompt("What are you looking for in this spreadsheet?")}if(a){var b=jS.obj.sheet().find('td:contains("'+a+'")');if(b.length<1){b=jS.obj.sheet().find('td:contains("'+a.toLowerCase()+'")')}if(b.length<1){b=jS.obj.sheet().find('td:contains("'+a.toUpperCase()+'")')}b=b.eq(0);if(b.length>0){b.click()}else{alert("No results found.")}}},cellSetActiveMulti:function(a){var b={startRow:a.target.parentNode.rowIndex,startColumn:a.target.cellIndex};jS.obj.sheet().mousemove(function(c){b.endRow=c.target.parentNode.rowIndex;b.endColumn=c.target.cellIndex;for(var e=b.startRow;e<=b.endRow;e++){for(var d=b.startColumn;d<=b.endColumn;d++){var f=jS.getTd(jS.obj.tableBody(),e+1,d+1);jQuery(f).addClass(jS.cl.uiCell).addClass(jS.cl.uiCellHighlighted)}}}).mouseup(function(){jS.obj.sheet().unbind("mousemove").unbind("mouseup")});return false},cellSetActiveMultiColumn:function(a){jS.obj.sheet().find("tr").each(function(){var b=jQuery(this).find("td").eq(a-1);b.addClass(jS.cl.uiCell).addClass(jS.cl.uiCellHighlighted)});jS.themeRoller.barTop(a)},cellSetActiveMultiRow:function(a){jS.obj.sheet().find("tr").eq(a-1).find("td").addClass(jS.cl.uiCell).addClass(jS.cl.uiCellHighlighted);jS.themeRoller.barLeft(a)},sheetClearActive:function(){jS.obj.formula().val("");jS.obj.cell().removeClass(jS.cl.cell);jS.obj.barSelected().removeClass(jS.cl.barSelected)},getIndexTr:function(a){return a-1},getIndexTd:function(a){return a-1},getTdRange:function(){var b=jS.obj.uiCell().not("."+jS.cl.cell);var c=jS.getTdLocation(b.eq(0));var a=cE.columnLabelString(c[1])+c[0];var e=jS.getTdLocation(b.eq(b.length-1));var d=cE.columnLabelString(e[1])+e[0];return a+":"+d},getTdId:function(b,a){return"cell_c"+a+"_r"+b},getTd:function(f,e,c,b,d){var a=document.getElementById(jS.getTdId(e,c));return a},getTdLocation:function(c){var a=c[0].cellIndex+1;var b=c[0].parentNode.rowIndex+1;return[b--,a--]},tableCellProvider:function(a){this.tableBodyId=a;this.cells={}},tableCell:function(c,b,a){this.tableBodyId=c.id;this.row=b;this.col=a;this.indexTr=jS.getIndexTr(b);this.indexTd=jS.getIndexTd(a);this.value=jS.EMPTY_VALUE},EMPTY_VALUE:{},time:{now:new Date(),last:new Date(),diff:function(){return Math.abs(Math.ceil(this.last.getTime()-this.now.getTime())/1000).toFixed(5)},set:function(){this.last=this.now;this.now=new Date()},get:function(){return this.now.getHours()+":"+this.now.getMinutes()+":"+this.now.getSeconds()}},log:function(a){switch(jS.s.log){case true:jS.time.set();jS.obj.log().prepend(jS.time.get()+", "+jS.time.diff()+"; "+a+"<br />\n");break}},getChart:function(e,b,g,j,i,c){var d=new jGCharts.Api();function f(h){var k=new Array();jQuery(h).each(function(l){k[l]=jS.manageHtmlToText(h[l]+"")});return k}var a={};if(e){a.type=e}if(b){a.data=b}if(g){a.legend=f(g)}if(j){a.axis_labels=f(j)}if(i||c){a.size=i+"x"+c}return jQuery("<img>").attr("src",d.make(a))}};jS.tableCellProvider.prototype={getCell:function(d,b){if(typeof(b)=="string"){b=cE.columnLabelIndex(b)}var c=d+","+b;var a=this.cells[c];if(!a){var f=jS.obj.tableBody();if(f){var e=jS.getTd(f,d,b);if(e){a=this.cells[c]=new jS.tableCell(f,d,b)}}}return a},getNumberOfColumns:function(b){var c=jS.obj.tableBody();if(c){var a=c.rows[jS.getIndexTr(b)];if(a){return a.cells.length}}return 0},toString:function(){result="";jS.obj.sheet().find("tr").each(function(){result+=this.innerHTML.replace(/\n/g,"")+"\n"});return result}};jS.tableCell.prototype={getTd:function(){return jS.getTd(jS.obj.tableBody(),this.row,this.col,this.indexTr,this.indexTd)},setValue:function(a,b){this.error=b;this.value=a;this.getTd().innerHTML=(a?a:"")},getValue:function(){var a=this.value;if(a===jS.EMPTY_VALUE&&!this.getFormula()){a=this.getTd().innerHTML;a=this.value=(a.length>0?cE.parseFormulaStatic(a):null)}return(a===jS.EMPTY_VALUE?null:a)},getFormat:function(){return jQuery(this.getTd()).attr("format")},setFormat:function(a){jQuery(this.getTd()).attr("format",a)},getFormulaFunc:function(){return this.formulaFunc},setFormulaFunc:function(a){this.formulaFunc=a},getFormula:function(){return jQuery(this.getTd()).attr("formula")},setFormula:function(a){if(a&&a.length>0){jQuery(this.getTd()).attr("formula",a)}else{jQuery(this.getTd()).removeAttr("formula")}}};var key={BACKSPACE:8,CAPS_LOCK:20,COMMA:188,CONTROL:17,DELETE:46,DOWN:40,END:35,ENTER:13,ESCAPE:27,HOME:36,INSERT:45,LEFT:37,NUMPAD_ADD:107,NUMPAD_DECIMAL:110,NUMPAD_DIVIDE:111,NUMPAD_ENTER:108,NUMPAD_MULTIPLY:106,NUMPAD_SUBTRACT:109,PAGE_DOWN:34,PAGE_UP:33,PERIOD:190,RIGHT:39,SHIFT:16,SPACE:32,TAB:9,UP:38};var cE=jQuery.calculationEngine={TEST:{},ERROR:"#VALUE!",cFN:{SUM:function(a,b){return a+b},MAX:function(a,b){return a>b?a:b},MIN:function(a,b){return a<b?a:b},COUNT:function(a,b){return(b!=null)?a+1:a},CLEAN:function(a){if(typeof(a)=="string"){a=a.replace(cE.regEx.amp,"&").replace(cE.regEx.nbsp," ").replace(/\n/g,"").replace(/\r/g,"")}return a}},fn:{HTML:function(a){return jQuery(a)},IMG:function(a){return jQuery('<img src="'+a+'" style="border: ;"/>')},AVERAGE:function(b){var a=cE.foldPrepare(b,arguments);return cE.fn.SUM(a)/cE.fn.COUNT(a)},AVG:function(a){return cE.fn.AVERAGE(a)},COUNT:function(a){return cE.fold(cE.foldPrepare(a,arguments),cE.cFN.COUNT,0)},SUM:function(a){return cE.fold(cE.foldPrepare(a,arguments),cE.cFN.SUM,0,true)},MAX:function(a){return cE.fold(cE.foldPrepare(a,arguments),cE.cFN.MAX,Number.MIN_VALUE,true)},MIN:function(a){return cE.fold(cE.foldPrepare(a,arguments),cE.cFN.MIN,Number.MAX_VALUE,true)},ABS:function(a){return Math.abs(cE.fn.N(a))},CEILING:function(a){return Math.ceil(cE.fn.N(a))},FLOOR:function(a){return Math.floor(cE.fn.N(a))},INT:function(a){return Math.floor(cE.fn.N(a))},ROUND:function(a){return Math.round(cE.fn.N(a))},RAND:function(a){return Math.random()},RND:function(a){return Math.random()},TRUE:function(){return true},FALSE:function(){return false},NOW:function(){return new Date()},TODAY:function(){return Date(Math.floor(new Date()))},DAYSFROM:function(b,c,a){return Math.floor((new Date()-new Date(b,(c-1),a))/86400000)},IF:function(v,t,f){t=cE.cFN.CLEAN(t);f=cE.cFN.CLEAN(f);try{v=eval(v)}catch(e){}try{t=eval(t)}catch(e){}try{t=eval(t)}catch(e){}if(v=="true"||v==true||v>0||v=="TRUE"){return t}else{return f}},FIXED:function(g,b,h){if(b==null){b=2}var f=Math.pow(10,b);var k=String(Math.round(cE.fn.N(g)*f)/f);var a=k.indexOf(".");if(a<0){a=k.length;k+="."}for(var c=k.length-a-1;c<b;c++){k+="0"}if(h==true){return k}var e=k.replace("-","").split(".");var j=[];var d=true;while(e[0].length>0){if(!d){j.unshift(",")}j.unshift(e[0].slice(-3));e[0]=e[0].slice(0,-3);d=false}if(b>0){j.push(".");var d=true;while(e[1].length>0){if(!d){j.push(",")}j.push(e[1].slice(0,3));e[1]=e[1].slice(3);d=false}}if(g<0){return"-"+j.join("")}return j.join("")},TRIM:function(a){if(typeof(a)=="string"){a=jQuery.trim(a)}return a},HYPERLINK:function(b,a){return jQuery('<a href="'+b+'" target="_new">'+a+"</a>")},DOLLAR:function(b,a,d){if(a==null){a=2}if(d==null){d="$"}var c=cE.fn.FIXED(b,a,false);if(b>=0){return d+c}else{return"-"+d+c.slice(1)}},VALUE:function(a){return parseFloat(a)},N:function(a){if(a==null){return 0}if(a instanceof Date){return a.getTime()}if(typeof(a)=="object"){a=a.toString()}if(typeof(a)=="string"){a=parseFloat(a.replace(cE.regEx.n,""))}if(isNaN(a)){return 0}if(typeof(a)=="number"){return a}if(a==true){return 1}return 0},PI:function(){return Math.PI},POWER:function(a,b){return Math.pow(a,b)},LIST:function(c,g){var a=jQuery(jS.getTd(null,cE.calcState.row,cE.calcState.col-1));var b=cE.foldPrepare(c,arguments);var f=a.find("select").val();var h=jQuery('<select style="width: 100%;">'+(!g?'<option value="empty">Select a value</option>':"")+"</select>");var e="";for(var d=0;d<(b.length<=25?b.length:25);d++){var c="";if(e!="SELECTED"&&b[d]==f){c=e="SELECTED"}h.append("<option "+c+' value="'+b[d]+'">'+b[d]+"</option>")}return h},INPUTVAL:function(a){return jQuery(a).val()},CHECKBOX:function(c){var a=jQuery(jS.getTd(null,cE.calcState.row,cE.calcState.col-1));var b=cE.foldPrepare(c,arguments);var g=a.find("input");var d=g.val();var f="";if(g[0]){if(g[0].checked){f="CHECKED"}}var e=jQuery('<input type="checkbox" '+f+' onmousedown="this.checked = !this.checked" />').val(b);return e},CHART:{BAR:function(b,e,c,a,d){return this.CUSTOM(null,cE.foldPrepare(b,arguments),e,c,a,d)},BARH:function(b,e,c,a,d){return this.CUSTOM("bhg",cE.foldPrepare(b,arguments),e,c,a,d)},SBAR:function(b,e,c,a,d){return this.CUSTOM("bvs",cE.foldPrepare(b,arguments),e,c,a,d)},SBARH:function(b,e,c,a,d){return this.CUSTOM("bhs",cE.foldPrepare(b,arguments),e,c,a,d)},LINE:function(b,e,c,a,d){return this.CUSTOM("lc",cE.foldPrepare(b,arguments),e,c,a,d)},PIE:function(b,e,c,a,d){return this.CUSTOM("p",cE.foldPrepare(b,arguments),e,c,a,d)},CUSTOM:function(f,b,e,c,a,d){return jS.getChart(f,cE.foldPrepare(b,arguments),e,c,a,d)}}},calcState:{},calc:function(c,a,b){cE.calcState={cellProvider:c,context:(a!=null?a:{}),row:1,col:1,done:false,stack:[],calcMore:function(d){cE.calcState.fuel=d;return cE.calcLoop(cE.calcState)}};return cE.calcState.calcMore(b)},cell:function(){prototype:{getError=function(){return this.error},getValue=function(){return this.value},setValue=function(a,b){this.value=a;this.error=b},getFormula=function(){return this.formula},setFormula=function(a){this.formula=a},getFormulaFunc=function(){return this.formulaFunc},setFormulaFunc=function(a){this.formulaFunc=a},toString=function(){return"Cell:["+this.getFormula()+": "+this.getValue()+": "+this.getError()+"]"}}},columnLabelIndex:function(c){var a=0;for(var b=0;b<c.length;b++){var d=c.charCodeAt(b)-65+1;a=(a*26)+d}return a},parseLocation:function(b){if(b!=null&&b.length>0&&b!="&nbsp;"){for(var a=0;a<b.length;a++){if(b.charCodeAt(a)<=57){break}}return[parseInt(b.substring(a)),cE.columnLabelIndex(b.substring(0,a))]}else{return null}},columnLabelString:function(e){var d=(e-1).toString(26).toUpperCase();var g=[];for(var f=0;f<d.length;f++){var a=d.charCodeAt(f);if(f<=0&&d.length>1){a=a-1}if(a<=57){g.push(String.fromCharCode(a-48+65))}else{g.push(String.fromCharCode(a+10))}}return g.join("")},regEx:{n:/[\$,\s]/g,cell:/\$?([a-zA-Z]+)\$?([0-9]+)/g,range:/\$?([a-zA-Z]+)\$?([0-9]+):\$?([a-zA-Z]+)\$?([0-9]+)/g,amp:/&/g,gt:/</g,lt:/>/g,nbsp:/&nbsp;/g},str:{amp:"&amp;",lt:"&lt;",gt:"&gt;",nbsp:"&nbps;"},parseFormula:function(f,c){var b=null;var e=null;if(cE.calcState.cellProvider!=null){b=cE.calcState.cellProvider.nrows;e=cE.calcState.cellProvider.ncols}var d=f.replace(cE.regEx.range,function(j,l,i,o,k){var p=[];var h=cE.columnLabelIndex(l.toUpperCase());var s=parseInt(i);var n=cE.columnLabelIndex(o.toUpperCase());var m=parseInt(k);if(e!=null){n=Math.min(n,e)}if(b!=null){m=Math.min(m,b)}for(var g=s;g<=m;g++){for(var q=h;q<=n;q++){p.push(cE.columnLabelString(q)+g)}}return"["+p.join(",")+"]"});var a=d.replace(cE.regEx.cell,function(g,i,h){i=i.toUpperCase();if(c!=null){c[i+h]=[parseInt(h),cE.columnLabelIndex(i)]}return"(getCell(("+h+'),"'+i+'").getValue())'});return a},parseFormulaStatic:function(c){if(c==null){return null}else{var b=c.replace(cE.regEx.n,"");var a=parseFloat(b);if(isNaN(a)){a=parseInt(b)}if(isNaN(a)){a=(c.charAt(0)=="'"?c.substring(1):c)}return a}},calcLoop:function(){if(cE.calcState.done==true){return null}else{while(cE.calcState.fuel==null||cE.calcState.fuel>0){if(cE.calcState.stack.length>0){var a=cE.calcState.stack.pop();if(a!=null){a(cE.calcState)}}else{if(cE.calcState.cellProvider.formulaCells!=null){if(cE.calcState.cellProvider.formulaCells.length>0){var b=cE.calcState.cellProvider.formulaCells.shift();cE.visitCell(b[0],b[1])}else{cE.calcState.done=true;return null}}else{if(cE.visitCell(cE.calcState.row,cE.calcState.col)==true){cE.calcState.done=true;return null}if(cE.calcState.col>=cE.calcState.cellProvider.getNumberOfColumns(cE.calcState.row)){cE.calcState.row++;cE.calcState.col=1}else{cE.calcState.col++}}}if(cE.calcState.fuel!=null){cE.calcState.fuel-=1}}return cE.calcState.calcMore}},visitCell:function(r,c){var cell=cE.calcState.cellProvider.getCell(r,c);if(cell==null){return true}else{var value=cell.getValue();if(value==null){var formula=cell.getFormula();if(formula){var firstChar=formula.charAt(0);if(firstChar=="="){var formulaFunc=cell.getFormulaFunc();if(formulaFunc==null||formulaFunc.formula!=formula){formulaFunc=null;try{var dependencies={};var body=cE.parseFormula(formula.substring(1),dependencies);formulaFunc=function(){with(cE.calcState.cellProvider){with(cE.fn){with(cE.calcState.context){return eval(body)}}}};formulaFunc.formula=formula;formulaFunc.dependencies=dependencies;cell.setFormulaFunc(formulaFunc)}catch(e){cell.setValue(cE.ERROR+": "+e)}}if(formulaFunc){cE.calcState.stack.push(cE.makeFormulaEval(cell,r,c,formulaFunc));var dependencies=formulaFunc.dependencies;for(var k in dependencies){if(dependencies[k] instanceof Array&&cE.checkCycles(dependencies[k][0],dependencies[k][1])==true){cell.setValue(cE.ERROR+": cycle detected");cE.calcState.stack.pop();return false}}for(var k in dependencies){if(dependencies[k] instanceof Array){cE.calcState.stack.push(cE.makeCellVisit(dependencies[k][0],dependencies[k][1]))}}}}else{cell.setValue(cE.parseFormulaStatic(formula))}}}return false}},makeCellVisit:function(c,a){var b=function(){return cE.visitCell(c,a)};b.row=c;b.col=a;return b},makeFormulaEval:function(a,e,b,d){var c=function(){try{var f=d();switch(typeof(f)){case"string":f=f.replace(cE.regEx.amp,cE.str.amp).replace(cE.regEx.lt,cE.str.lt).replace(cE.regEx.gt,cE.str.gt).replace(cE.regEx.nbsp,cE.str.nbsp);case"object":f=jQuery("<div />").html(f).html()}a.setValue(f)}catch(g){}};c.row=e;c.col=b;return c},checkCycles:function(d,a){for(var b=0;b<cE.calcState.stack.length;b++){var c=cE.calcState.stack[b];if(c.row!=null&&c.col!=null&&c.row==d&&c.col==a){return true}}return false},foldPrepare:function(a,b){if(a!=null&&a instanceof Object&&a.length!=null){return a}else{return b}},fold:function(b,e,a,d){for(var c=0;c<b.length;c++){a=e(a,(d==true?cE.fn.N(b[c]):b[c]))}return a}};
