
function printSoftwareTable() {
	//Disable print.css temporarily 
	$('link[@rel*=style]').each(function(i){	

		if (this.getAttribute("href").match("print.css")){
			this.disabled = true;
		}
		if (this.getAttribute("href").match("SafariPrint.css")){
			this.disabled = false;
		}
	});


	 var maxHeightPixels = 1180;  //Safari
	 var firstPageHeading = 290;
	 var minimumNumberOfItems = 2; //smallest item list to include on a page. 
	 var leftSideY = rightSideY = pageNumber= 0;

	 //This is the section to put everything which will be printed out
	 var printSection=document.createElement('div');
	 printSection.setAttribute('id','printSection');
	 $('body').append(printSection);

	 //use visible fields in table to create an array of unique locations.
	 var uniqueLocations = new Array();
	 $("#softwareTable tbody tr:visible").each(function(){
	 var locationText = $(this).find('td:nth-child(4)').text() + " - " +$(this).find('td:nth-child(3)').text();
	 var match = false;
	 for (x = 0;x < uniqueLocations.length; x++){
		 if (uniqueLocations[x] == locationText){ match = true; break; }
	 }
	 if (!match){uniqueLocations[uniqueLocations.length] = locationText;}
	 });
	 
	     
	 //Go through each location and place all software at that location
	 for(x = 0;x < uniqueLocations.length; x++){
		 
		 if (x == 0){
				//dynamically create a two column table for the first page
				   var pageTable=document.createElement('table');
				     pageTable.setAttribute('class','pageBreakTable');
				     pageTable.setAttribute('width','970');
				     pageTable.setAttribute('border','0'); 
				     var tbody1=document.createElement('tbody');
				     pageTable.appendChild(tbody1);
				     var tr1=document.createElement('tr');
				     tbody1.appendChild(tr1);
				     var heading=document.createElement('th');
				     heading.setAttribute('colspan','2');
				     tr1.appendChild(heading);
				     var headingText=document.createTextNode('Computer Labs - Software');
				     heading.appendChild(headingText);
				     var tr2=document.createElement('tr');
				     tbody1.appendChild(tr2);
				     var leftSide=document.createElement('td');
				     leftSide.setAttribute('width','50%');
				     tr2.appendChild(leftSide);
				     var rightSide=document.createElement('td');
				     tr2.appendChild(rightSide);
				     printSection.appendChild(pageTable);
				     
				   //This is used to find the height of completed tables to see if they will fit on the page
					 var testTable=document.createElement('table');
					 testTable.setAttribute('class','softwareTableForPrint');
					 testTable.setAttribute('id','testTable');
					 var testTBody=document.createElement('tbody');
					 testTable.appendChild(testTBody); 
					 var testRow=document.createElement('tr');
					 testTBody.appendChild(testRow);
					 var testCell=document.createElement('th');
					 testRow.appendChild(testCell);
					 var testHeaderText=document.createTextNode("This is header text");
					 testCell.appendChild(testHeaderText); 
					 var testRow2=document.createElement('tr');
					 testTBody.appendChild(testRow2);
					 var testCell2=document.createElement('td');
					 testRow2.appendChild(testCell2);
					 var testCellText=document.createTextNode("test item one text");
					 testCell2.appendChild(testCellText);
					 
					 leftSide.appendChild(testTable);
					 var headingAndOneItemHeight = testTable.clientHeight;
					 
					 var testRow3=document.createElement('tr');
					 testTBody.appendChild(testRow3);
					 var testCell3=document.createElement('td');
					 testRow3.appendChild(testCell3);
					 var testCellText2=document.createTextNode("test item two text");
					 testCell3.appendChild(testCellText2);
					 
					 var headingAndTwoItemsHeight = testTable.clientHeight;
					 
					 var reportText=document.createTextNode("headingAndOneItemHeight:"+headingAndOneItemHeight+" headingAndTwoItemsHeight:"+headingAndTwoItemsHeight);
					 testCell3.appendChild(reportText);

					 $("#testTable").hide();
				 }
		 
		     //create array of location and OS followed by items for product and version
		     softwareArray = new Array();
		     softwareArray[0]=uniqueLocations[x];
		     var softwareArrayNumber = 1;
			 $("#softwareTable tbody tr:visible").each(function(){
				 var matchlocationText = $(this).find('td:nth-child(4)').text() + " - " +$(this).find('td:nth-child(3)').text();	
				 if (matchlocationText == uniqueLocations[x]){
					 softwareArray[softwareArrayNumber]=$(this).find('td:nth-child(1)').text() + ' ' + $(this).find('td:nth-child(2)').text();
					 softwareArrayNumber++;
				 }
			 });
		     
			 
			 var numberOfPlacedItems = 0;
			 var placeOnLeftSide = true;
			 var spaceForThisManyItemsOnANewSide = Math.floor((maxHeightPixels - headingAndOneItemHeight)/(headingAndTwoItemsHeight - headingAndOneItemHeight) + 1);		 
			//Loop to place software items for one location
			 while ((softwareArray.length - 1) > numberOfPlacedItems){
			 var spaceForThisManyItemsOnLeft = Math.floor((maxHeightPixels - leftSideY - headingAndOneItemHeight)/(headingAndTwoItemsHeight - headingAndOneItemHeight) + 1);
			 var spaceForThisManyItemsOnRight = Math.floor((maxHeightPixels - rightSideY - headingAndOneItemHeight)/(headingAndTwoItemsHeight - headingAndOneItemHeight) + 1);
			 
			 
			 //Decide if the remaining items can be placed on the side which has the most space left
			 //if not we need a new page
			 
			 //Step 1 decide which side has more space remaining and prepare to place items there
			 var spaceOnPageForThisManyItems = 0;
			 if (spaceForThisManyItemsOnLeft >= spaceForThisManyItemsOnRight){
				 placeOnLeftSide = true;
				 spaceOnPageForThisManyItems = spaceForThisManyItemsOnLeft;
			 }else{
				 placeOnLeftSide = false;  //place on right side
				 spaceOnPageForThisManyItems = spaceForThisManyItemsOnRight;
			 }
			 
			 //figure out how many items could be placed without running out of space
			 var numberOfItemsRemainingForLocation = softwareArray.length - 1 - numberOfPlacedItems; // one less because first item of Array is table heading
			 var placeThisManyItems = numberOfItemsRemainingForLocation;
			 if (placeThisManyItems > spaceOnPageForThisManyItems){ placeThisManyItems = spaceOnPageForThisManyItems; }
			 
			 //Step 2 Make sure next location won't get stuck with less than minumum number left
			 var numberOfItemsForNextLocation = numberOfItemsRemainingForLocation - placeThisManyItems;
			 //Attempt to set to allow only the minumum number for next location
			 if (numberOfItemsForNextLocation != 0 & numberOfItemsForNextLocation < minimumNumberOfItems){		
				 placeThisManyItems = placeThisManyItems - minimumNumberOfItems + numberOfItemsForNextLocation;
				 
				 //test if the number of items to place in next location is less than minimum if so we need a new page
				 if (numberOfItemsForNextLocation < minimumNumberOfItems){
					 spaceOnPageForThisManyItems = 0;  //triger a new page to be made, not enough space left
				 }
			 }

			//Step 3 test if we need a new page or not
			 if (spaceOnPageForThisManyItems < minimumNumberOfItems){
				 //Make a new page
				 var table1=document.createElement('table');
				 table1.setAttribute('class','pageBreakTable');
				 table1.setAttribute('border','0');
			     table1.setAttribute('width','970');
				 var tbody1=document.createElement('tbody');
				 table1.appendChild(tbody1);
				 var tr1=document.createElement('tr');
				 tbody1.appendChild(tr1);
				 var leftSide=document.createElement('td');
				 leftSide.setAttribute('width','50%');
				 tr1.appendChild(leftSide);
				 var rightSide=document.createElement('td');
				 tr1.appendChild(rightSide);				 
				  
				 printSection.appendChild(table1);
				 table1.style.pageBreakBefore="always";
				 
				 //After first page pages don't have a header so they are longer
				 if (pageNumber == 0){maxHeightPixels = maxHeightPixels + firstPageHeading;}
				 
				 
				 leftSideY = rightSideY = 0;
				 pageNumber++;
				 spaceOnPageForThisManyItems = spaceForThisManyItemsOnANewSide;
				 placeOnLeftSide = true;
				 
				 
				 //figure out how many items could be placed without running out of space
				 numberOfItemsRemainingForLocation = softwareArray.length - 1 - numberOfPlacedItems; // one less because first item of Array is table heading
				 placeThisManyItems = numberOfItemsRemainingForLocation;
				 if (placeThisManyItems > spaceOnPageForThisManyItems){ placeThisManyItems = spaceOnPageForThisManyItems; }
				 
				 //Make sure next location won't get stuck with less than minumum number left
				 var numberOfItemsForNextLocation = numberOfItemsRemainingForLocation - placeThisManyItems;
				 //Attempt to set to allow only the minumum number for next location
				 if (numberOfItemsForNextLocation != 0 & numberOfItemsForNextLocation < minimumNumberOfItems){		
				 placeThisManyItems = placeThisManyItems - minimumNumberOfItems + numberOfItemsForNextLocation;
				 }
				 
			 }
			 
		     //create software table from softwareArray, spaceOnPageForThisManyItems and numberOfPlacedItems
			 var hTable=document.createElement('table');
			 hTable.setAttribute('class','softwareTableForPrint');
			 var tbody=document.createElement('tbody');
			 hTable.appendChild(tbody); 
			 var row=document.createElement('tr');
			 tbody.appendChild(row);
			 var cell=document.createElement('th');
			 row.appendChild(cell);
			 var text=document.createTextNode(softwareArray[0]);
			 //var text=document.createTextNode(leftSideY+" "+rightSideY+" - "+softwareArray[0]);
			 cell.appendChild(text);
			 var startAtThisElement = numberOfPlacedItems + 1;

			 for (var i=startAtThisElement; i <= (placeThisManyItems+numberOfPlacedItems); i++){
				 var row=document.createElement('tr');
				 tbody.appendChild(row);
				 var cell=document.createElement('td');
				 row.appendChild(cell);
				 var text=document.createTextNode(softwareArray[i]);
				 cell.appendChild(text);
			 }
			 numberOfPlacedItems = numberOfPlacedItems + placeThisManyItems;
			 
			 
			 if (placeOnLeftSide){
			 leftSide.appendChild(hTable);
			 leftSideY = hTable.offsetTop + hTable.clientHeight;
			 }else{
			 rightSide.appendChild(hTable);
			 rightSideY = hTable.offsetTop + hTable.clientHeight;
			 }
			 
		}//end of loop placing each item for one location
	 }//end of loop through each location
	
	  window.print();
	//remove the printSection from the document, 
	//or else it will still be there for the next time this function runs
		 $('#printSection').remove();

	//ReEnable print.css  
	$('link[@rel*=style]').each(function(i){	
		if (this.getAttribute("href").match("print.css")){
			this.disabled = false;
		}
		if (this.getAttribute("href").match("SafariPrint.css")){
			this.disabled = true;
		}
	});

}
