
var oTable;var currentSelected;var isDirty=false;$(document).ready(function(){$(".drawer li").css("display","none");$(".drawer").click(function(){$(this).find("li").toggle();return false;});$("#btnSave").click(function(){Save(function(){Processing.Hide();});return false;});$("#btnCheckout").click(function(){Save(function(){document.location.href="/Checkout.aspx";});return false;});$("#deptSearchButton").click(function(){Save(function(){Search($("#deptSearchInput").val());});return false;});$("#deptSearchInput").focus(function(){this.select();});$("#deptSearchInput").blur(function(){Save(function(){Search($("#deptSearchInput").val());});return false;});$("#equipmentForm").submit(function(){Save(function(){Search($("#deptSearchInput").val());});return false;});$(".categoryDrawer").click(function(){SetSelected(this);var parms=this.getElementsByTagName("a")[0].href.match(/\?(\S*)/)[1].split('&');var newDepartment="";var newCategory="";window.location.hash=parms;p=parms;if(parms.length==2){newDepartment=unescape(parms[0].split("=")[1]).replace(/\+/g," ");newCategory=unescape(parms[1].split("=")[1]).replace(/\+/g," ");}
Save(function(){Api.Browse(newDepartment,newCategory,function(ret){equipmentCollection=ret;EquipmentCollectionToTable();Processing.Hide();});});return false;});oTable=$("#categoryTable");oTable.dataTable({oLanguage:{sZeroRecords:"<div class=\"ZeroRecords\"><p>Please browse a Department and Category or try a different keyword in the Search box.</p></div>"},"aaData":[],"aaSorting":[[1,"asc"]],"aoColumns":[{"sTitle":"Code","sWidth":"80px"},{"sTitle":"Name"},{"sTitle":"Quantity","sType":"numeric","sWidth":"60px"}],"bAutoWidth":false,"bLengthChange":false,"iDisplayLength":25,"sPaginationType":"two_button","bPaginate":true,"bFilter":false,"aaStripClasses":["categoryRow","categoryRow_Alt"]});});function Search(keyword){if(keyword.length==0){$(this).val("Please Enter Keyword").focus();return;}
Api.Search(form,keyword,function(ret){equipmentCollection=ret;EquipmentCollectionToTable();Processing.Hide();});}
var Processing={Show:function(){var table=$("#categoryTable_wrapper");$("#progressOverlay").css({background:"black url(/images/adbeast_standard_loader.gif) no-repeat 50% 50%",position:"absolute",top:0,left:0,width:$('#categoryTable_wrapper').width(),height:$('#categoryTable_wrapper').height()+$('#categoryTable_paginate').height(),margin:"0 18px",opacity:"0.2","z-index":"1001"}).show();},Hide:function(){$("#progressOverlay").hide();}}
function SetSelected(itemToSelect){if(currentSelected!=null){currentSelected.removeClass("selected");}
currentSelected=$(itemToSelect);currentSelected.addClass("selected");}
function EquipmentCollectionToTable(){var aaData=new Array();oTable.fnClearTable();for(var i=0;i<equipmentCollection.length;i++){var ta=new Array();ta.push(equipmentCollection[i].Code);ta.push("<a title=\""+equipmentCollection[i].DepartmentName+"::"+equipmentCollection[i].CategoryName+"\">"+equipmentCollection[i].Name+"</a>");ta.push("<input id=\""+equipmentCollection[i].Id+"\" alt=\""+equipmentCollection[i].DepartmentName+" - "+equipmentCollection[i].CategoryName+"\" name=\""+equipmentCollection[i].Code+"\" type=\"text\" class=\"qty\" value=\""+((equipmentCollection[i].Quantity==0)?"":equipmentCollection[i].Quantity)+"\"/>");aaData.push(ta);}
oTable.fnAddData(aaData);$(oTable.fnGetNodes()).find(":nth-child(3) :input").blur(function(){isDirty=true;var _this=$(this);});$("#categoryTable tbody tr td a").tipsy({gravity:"e",fade:false});if($(oTable).find("td :input")[0]){$(oTable).find("td :input")[0].focus();}
oTable.fnSettings().iDisplayStart=0;oTable.fnDraw();}
function Save(callback){Processing.Show();if(isDirty){var equipmentCollection=new Array();$(oTable.fnGetNodes()).each(function(i,node){var td=$(node).find("td :input")[0];var quantity=$(td).val();if(quantity>0){equipmentCollection.push(new Equipment(td.id,quantity));}});if(equipmentCollection.length>0){Api.Save(equipmentCollection,function(ret){if(ret){isDirty=false;callback();}
else{Processing.Hide();}});}
else{callback();}}
else{callback();}
return false;}
Equipment=function(id,quantity){this.Category=null;this.CategoryId=0;this.CategoryName="";this.Code="";this.Department=null;this.DepartmentId=0;this.DepartmentName="";this.FormId=0;this.Id=id;this.Name="";this.Quantity=quantity;this.Version=0;this.__type="Equipment"}