<?php
    require_once ('../3rd_party/fpdf/fpdf.php');

    class setup_sheet_template extends FPDF
    {
        var $angle=0;

        private function Rotate($angle,$x=-1,$y=-1)
        {
            if($x==-1)
                $x=$this->x;
            if($y==-1)
                $y=$this->y;
            if($this->angle!=0)
                $this->_out('Q');
            $this->angle=$angle;
            if($angle!=0)
            {
                $angle*=M_PI/180;
                $c=cos($angle);
                $s=sin($angle);
                $cx=$x*$this->k;
                $cy=($this->h-$y)*$this->k;
                $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
            }
        }

        protected function _endpage()
        {
            if($this->angle!=0)
            {
                $this->angle=0;
                $this->_out('Q');
            }
            parent::_endpage();
        }


        private function RotatedText($x,$y,$txt,$angle)        {
            //Text rotated around its origin
            $this->Rotate($angle,$x,$y);
            $this->Text($x,$y,$txt);
            $this->Rotate(0);
        }

        private function MultiCellRotatedText($x,$y,$txt,$angle,$lineSize=9999999999)        {
            // Set a return variable
            $yFinal = $y;
            
            //Text rotated around its origin
            $this->Rotate($angle,$x,$y);
            
            // get the amount of lines to write
            $numberOfLinesToWrite = ceil(strlen($txt)/$lineSize);
            
            // Loop through the amount of lines needed
            for ($lineNumber = 0; $lineNumber < $numberOfLinesToWrite; $lineNumber++){
                // Get the string from this text
                $textToWrite = substr($txt, $lineNumber*$lineSize, $lineSize);
                
                // This is the y Final value
                $yFinal = $y+(4*$lineNumber);
                
                // Write the line
                $this->Text($x,$y+(4*$lineNumber),$textToWrite);
            }
            
            // Rotate back
            $this->Rotate(0);
            
            // Return the Y Final value
            return $yFinal;
        }

        private function RotatedImage($file,$x,$y,$w,$h,$angle)
        {
            //Image rotated around its upper-left corner
            $this->Rotate($angle,$x,$y);
            $this->Image($file,$x,$y,$w,$h);
            $this->Rotate(0);
        }
        
        private function WordWrap(&$text, $maxwidth, $inConcatSymbol=null, &$highlightingArray=null)
        {
            // Determine if there is any data to work with or not
            if (trim($text)===''){
                // Return the empty array
                return 0;
            }

            // Figure out the length of a space in current font
            $space = $this->GetStringWidth(' ');
            
            // get the amount of lines 
            $lines = explode("\n", $text);
            
            // Init the string to return what should be used to match the highlight array.
            $text = '';
            
            // The line count that the currently working with string is for
            $count = 0;

            // Start looping through the string's endlines
            foreach ($lines as $line)
            {
                // Split up the words by spaces
                $words = preg_split('/ +/', $line);
                
                // Create a variable to hold the width of the current line. 
                // This takes into account the font.
                $width = 0;

                // Loop through all words in this line
                foreach ($words as $word)
                {
                    // Get the width of this current word 
                    $wordwidth = $this->GetStringWidth($word);
                    
                    // Determine if this word is more that the max width we can go to.
                    // This is for suuuuuuuuuuuuper long words or very tight spaces.
                    if ($wordwidth > $maxwidth)
                    {
                        // Word is too long, we need to cut it
                        for($i=0; $i<strlen($word); $i++)
                        {
                            $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                            if($width + $wordwidth <= $maxwidth)
                            {
                                $width += $wordwidth;
                                $text .= substr($word, $i, 1);
                            }
                            else
                            {
                                $width = $wordwidth;
                                $text = rtrim($text)."\n".substr($word, $i, 1);
                                $count++;
                            }
                        }
                    }
                    elseif($width + $wordwidth <= $maxwidth)
                    {
                        $width += $wordwidth + $space;
                        $text .= $word.' ';
                    }
                    else
                    {
                        $width = $wordwidth + $space;
                        $text = rtrim($text)."\n".$word.' ';
                        $count++;
                    }
                }
                $text = rtrim($text)."\n";
                $count++;
            }
            $text = rtrim($text);
            return $count;
        }
        
        private function highlightLocations($startingXOffset=0, $startingY=0, $cellHeight=5, $text='', $inConcatSymbol=null)
        {
            // Get the page number that we are on
            $currentPage=$this->page;
            
            // Determine if there is any reason to start this
            if (trim($text)=='' || $inConcatSymbol==null)
                return false;
            
            // Expand the lines on end line at first
            $lines = explode("\n", $text);
            
            // Set the variable determine if we are currently highlighting to no at first.
            $doHighlighting=false;

            // Set the fill color to yellow for highlighting
            $this->SetFillColor(255,255,0);

            // Set the variable to start at the line position for Y as what was passed in
            $currentLoopingYPos = $startingY;

            // Loop through the lines
            foreach ($lines as $line)
            {
                // Set a variable for the recombined string as nothing
                $recombinedString='';

                // Move to the line in Y now
                $this->SetY($currentLoopingYPos);
                
                // Determine if there was a delimiter on the line or if we are 
                // currently on highlighting.
                if (strpos($line,'*')!==FALSE){
                    // Split this line on the delimiter passed in
                    $highlightSplit = explode($inConcatSymbol, $line);

                    // Loop through what we might have
                    for ($count=0; $count < count($highlightSplit); $count++) {
                        // Determine if we are already highlighting
                        if ($doHighlighting) {
                            // Get the current X position taking into account font size
                            $this->SetX($startingXOffset + $this->GetStringWidth($recombinedString)+1);
                            
                            // Get the current X position taking into account font size
                            $highlightStringWidth=$this->GetStringWidth($highlightSplit[$count]);

                            // This needs highlighting so try to overwrite what we have.
                             $this->Cell($highlightStringWidth,$cellHeight,"",0,0,'L',true);

                            // Determine if this is the last one or if there are more after this one
                            if ($count+1!=count($highlightSplit)){
                                // set as done highlighting 
                                $doHighlighting=false;
                            }
                        }
                        else {
                            // If there is only one segment left, then we are 
                            // done highlighting for this line.  Exit the loop.
                            if ($count+1==count($highlightSplit)){
                                // Break out of this loop
                                break;
                            }
                            
                            // set to do the highlighting on next round
                            $doHighlighting=true;
                        }

                        // Recombine the string
                        $recombinedString.= '*' . $highlightSplit[$count];
                    }
                }
                else if ($doHighlighting){
                    // Get the current X position at the beginning of the line
                    $this->SetX($startingXOffset+1);

                    // Get the width of the whole line for highlighting
                    $highlightStringWidth=$this->GetStringWidth($line);
                            
                    // This needs highlighting so try to overwrite what we have.
                    $this->Cell($highlightStringWidth,$cellHeight,"",0,0,'L',true);
                }
                
                // Move y to the next line
                $currentLoopingYPos+=$cellHeight;
            }

            // Get the page number that we are on
            $this->page=$currentPage;
            
            // Return to the position before highlighting started.
            //         TODO:  X doesn't work for some reason. Have to set it 
            //                after calling this method. Figure it out.
            $this->setX($startingXOffset);
            $this->setY($startingY);
            
            return true;
        }   
        
        private function ToolingInfo($db, $userRow, 
                                     $inTaskID, $inMachineID,
                                     &$allTools, &$newTools, 
                                     &$elTools, &$differentELTools, 
                                     &$howManyToolsUndefined,
                                     &$howManyTotalELTools, &$howManyDifferingELTools){
            // Set the return to false stating no tools
            $returnValue=false;
            
            // Initialize the variables in.  We do not want them to have any value to start.
            $allTools='';
            $newTools='';
            $elTools='';
            $differentELTools='';
            $howManyToolsUndefined=0;
            $howManyTotalELTools=0;
            $howManyDifferingELTools=0;
            
            // Create a query to get the tools used for this job on this machine and exposed length status
            $sql = 'SELECT tttl.*, asst.MachineID, t.Name, t.FluteNumber, Brand, PartNumber, tttl.Notes, tt.Name AS ToolCategory, ' . 
                   '       IF(asst.ExposedLength = tttl.ExposedLength, 1,0) AS ExposedLengthEqual, ' . 
                   '       IF(asst.CurrentToolID = tttl.ToolID, 1,0) AS ToolsMatch ' .
                   'FROM tooltotasklinker tttl ' .
                   'LEFT JOIN assignedtools asst ON tttl.ToolPosition = asst.Position ' .
                   'LEFT JOIN tools t ON tttl.ToolID = t.ToolID ' .
                   'LEFT JOIN tooltype tt ON t.ToolTypeID = tt.ToolTypeID ' .
                   'WHERE tttl.TaskID = ' . $inTaskID . ' ' .
                   'AND (asst.MachineID = ' . $inMachineID . ' ' . 
                   'OR asst.MachineID IS NULL) ' . 
                   'ORDER BY tttl.ToolPosition;';

            // Attempt to get all of the notes for this category type and ID
            $toolInformationQuery = $db->queryDB($sql, $userRow, "setup_sheet_template Form occurance 1");

            // Determine if there were rows
            if ($toolInformationQuery!=null){
                // Loop through the returned results
                $amount=0;
                while ($tool=mysqli_fetch_array($toolInformationQuery)){
                    // Increment the amount
                    $amount++;
                    
                    // Determine if this tool is NOT in the machine
                    if ($tool["MachineID"]==null){
                        // Increment the amount of tools undefined
                        $howManyToolsUndefined++;
                    }
                    else {
// TODO: Determine if we want to add more of this in there                        
//                        // Determine if the BRAND has been entered
//                        $brand="";
//                        $partNumber="";
//                        if ($tool["Brand"]!="Not Entered"){
//                            // Set the brand
//                            $brand="Brand: " . $tool["Brand"];
//                            
//                            // Determine if the Part Number has been entered
//                            if ($tool["PartNumber"]!="Not Entered"){
//                                // Set the part number
//                                $partNumber="  Part Number: " . $tool["PartNumber"];
//                            }
//                        }
//                        
//                        // Determine if there are flutes
//                        $fluteNumber="";
//                        if ($tool["FluteNumber"]>0){
//                            // Set the flutes
//                            $fluteNumber="  Flutes: " . $tool["FluteNumber"];
//                        }
//                        
                        // Determine if there is an exposed length set
                        $exposedLength="";
                        if ($tool["ExposedLength"]>0){
                            // Set the exposed length
                            $exposedLength="Exposed Length: " . $tool["ExposedLength"];
                        }
                        
                        // Determine if there are notes set
                        $toolNotes="";
                        if ($tool["Notes"]!=null && $tool["Notes"]!=""){
                            
                            // Set the notes
                            $toolNotes="-- Notes: *" . $tool["Notes"] . "*";
                        }
                        
                        // Put the added info together
                        $addedInfo=$exposedLength . "  " . $toolNotes;
                        
                        // Determine if we have any added info
                        if (trim($addedInfo)!=""){
                            // Add parens
                            $addedInfo=" (" . $addedInfo . ")";
                        }
                        
                        // Determine if it is of a category that we want to point it out to the setup person
                        $toolName = $tool["Name"];
                        if ($tool["ToolCategory"]=="BALL NOSE MILL") {
                            // Let them know that we are adding a BALL Nose
                            $toolName=$tool["Name"] . "  *(Ball Nose!)*";
                        }
                        else if ($tool["ToolCategory"]=="BULL NOSE MILL") {
                            // Let them know that we are adding a BALL Nose
                            $toolName=$tool["Name"] . "  *(Corner Radius!)*";
                        }
                        
                        // Add a new one to the all tools list
                        $allTools = $allTools . "T" . $tool["ToolPosition"] . " - " . $toolName .  $addedInfo . "\n";

                        // Determine if the tools DO NOT match
                        if ($tool["ToolsMatch"] == 0){
                            // Add a new one to the new tools list
                            $newTools = $newTools . "T" . $tool["ToolPosition"] . " - " . $toolName . $addedInfo . "\n";
                        }

                        // Determine if the tool actually has an extended length
                        if ($tool["ExposedLength"] > 0){
                            // Increment the amount
                            $howManyTotalELTools++;

                            // Add a new one to the tools with extended reach list
                            $elTools = $elTools . ' T' . $tool["ToolPosition"] . ',';
                        }

                        // Determine if they are NOT the same as what is in the machine
                        if ($tool["ExposedLengthEqual"] == 0 && 
                            $tool["ToolsMatch"]==1){
                            // Increment the amount
                            $howManyDifferingELTools++;

                            // Add a new one to the differing length tools list
                            $differentELTools = $differentELTools . ' T' . $tool["ToolPosition"] . ',';
                        }
                    }
                }

                // Determine if there is a string for all tools
                if ($allTools==''){
                    // Set NONE as the value
                    $allTools='NONE';
                }

                // Determine if there is a string for new tools
                if ($newTools==''){
                    // Set NONE as the value
                    $newTools='NONE';
                }

                // Determine if there is a string for extended length tools and remove last comma.
                if ($elTools!=''){
                    // Remove the last character
                    $elTools=substr($elTools, 0, -1);
                }
                else {
                    // Set NONE as the value
                    $elTools='NONE';
                }

                // Determine if there is a string for different extended length tools and remove last comma.
                if ($differentELTools!=''){
                    // Remove the last character
                    $differentELTools=substr($differentELTools, 0, -1);
                }
                else {
                    // Set NONE as the value
                    $differentELTools='NONE';
                }
                    
                // Set the return to be true indicating there were tools
                $returnValue=true;
            }
            
            // Return the return value
            return $returnValue;
        }
        
//        private function CreateAndSaveImage(){
//            // Create image with 2 line of text
//            $im = imagecreatetruecolor(600, 300);
//            $text_color = imagecolorallocate($im, 142, 011, 029);
//            imagestring($im, 10, 5, 5,  'Some Text String', $text_color);
//            imagestring($im, 10, 5, 50,  'Some Text String 2', $text_color);
//
//            // Set the content type header as image/jpeg
//            header('Content-Type: image/jpeg');
//
//            // Output the image
//            //imagejpeg($im);    //up-to this point its working well 
//
//            $name="test2";
//            $save = "images/fb/". strtolower($name) .".jpeg";
//            imagejpeg($im, $save, 100);   //Saves the image
//            imagejpeg($im);              //Displays the image
//
//            // Free up memory
//            imagedestroy($im);            
//        }
        
        function DocSetup(){
//            // Turn the auto pagebreak to on
//            $this->SetAutoPageBreak(true, 1);
            
            // Set the margins up
            $this->SetRightMargin(209 - $this->rMargin);
            $this->SetLeftMargin($this->lMargin+5);

            // Set the fill color to yellow for highlighting
            $this->SetFillColor(255,255,0);
        }
        
        private function Notes($db, $userRow, $inCategoryID, $category, $title="Notes", $checkFilename=true){
            // Create a return value to determine if there were any notes created and send back
            $creatednotes=false;
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);

            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);

            // Determine if we have a valid category ID
            if ($inCategoryID!=null && $inCategoryID>0){
                // Create the query to get the the notes and the user that created the notes
                $sql = 'SELECT n.*, u.FirstName, u.LastName, u.UserLevel ' .
                       'FROM notes n ' . 
                       'INNER JOIN users u ON n.EnteredByID = u.UserID ' .
                       'WHERE n.AssociatedCategoryID=' . $inCategoryID . ' ' .
                       'AND n.Category="' . $category . '" '. 
                       'ORDER BY n.NoteOrder, n.Date DESC;';

                // Attempt to get all of the notes for this category type and ID
                $taskNoteRows = $db->queryDB($sql, $userRow, "setup_sheet_template Form occurance 2");

                //*** Data Row 1 ***//

                // Determine if there were rows for the task notes
                if ($taskNoteRows != null){
                    // Determine if a task note was NOT passed in
                    $amountOfNotes=mysqli_num_rows($taskNoteRows); 
                    if ($amountOfNotes>0){
                        // Determine if we are starting the setups. If so and there are setups, add a page
                        if ($category=="SETUP_M00"){
                            // Start a new page for setup Instructions
                            $this->AddPage();
                        }
                        
                        // Determine if we are NOT making task notes
                        if ($category!="Task"){
                            // Add a Notes Label cell
                            $this->Cell(35,5, $title . ':',0,0);
                        }
                    }
                    
                    // Loop through the returned results
                    $amount=0;
                    while ($noteRow=mysqli_fetch_array($taskNoteRows)){
                        // Determine if it is a file extention that we do not look at
                        if ( strpos(strtoupper($noteRow["FileName"]), '.EDRW')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.DRW')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.SLDDRW')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.SLDPRT')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.SLDASM')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.STEP')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.DOCX')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.DOC')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.DOT')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.DOTM')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.XPS')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.XLSX')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.XLT')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.XLS')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.XLSB')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.XLSM')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.PPT')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.PPSX')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.PPTM')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.PPTX')!==FALSE || 
                             strpos(strtoupper($noteRow["FileName"]), '.PDF')!==FALSE){
                            // Next row
                            continue;
                        }
                        
                        // Determine if we are not supposed to look at ones with keyword filenames
                        if (($checkFilename && 
                             strpos(strtoupper($noteRow["FileName"]), 'DRAWING')===FALSE) || 
                             !$checkFilename   
                           ){
                            // Set the return value to say we attempted to create one
                            $creatednotes|=true;
                            
                            // Add a Line break
                            $this->ln(2);

                            // Increment the notes amount
                            $amount++;

                            // Set the x to be at the beginning of the line plus 15
                            $this->SetX($this->lMargin+18);

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a Note Number Label cell
                            $this->Cell(15,9,'Number ' . $amount . ':',0,1);

                            // Add a Line break
                            $this->ln(0);

                            //*** Data Row 2 ***//

                            // Set the x to be at the beginning of the line plus 25 plus the normal 3
                            $this->SetX($this->lMargin+28);

                            // Add a Created By Label cell
                            $this->Cell(25,9,'Created By:');

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Add a Notes created By cell
                            $this->Cell(40,9,htmlspecialchars_decode($noteRow["FirstName"] . ' ' . $noteRow["LastName"]));  

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a Created By Date Label cell
                            $this->Cell(25,9,'Created On:');

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Add a Notes created By date cell
                            $this->Cell(50,9,$noteRow["Date"],0,1);

                            //*** Data Row 3 ***//

                            // Set the x to be at the beginning of the line plus 35 plus the normal 3
                            $this->SetX($this->lMargin+28);

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a Title Label cell
                            $this->Cell(25,9,'Title:');

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Get the current X and Y values
                            $startingX=$this->GetX();
                            $startingY=$this->GetY();

                            // Highlight over the places that need it
                            $this->highlightLocations($startingX, $startingY, 5, htmlspecialchars_decode($noteRow["Title"]), '*');

                            // Set the x to be at the beginning of the line
                            $this->SetX($this->lMargin+53);
            
                            // Add a title cell
                            $this->Cell(130,9,htmlspecialchars_decode($noteRow["Title"]),0,1);

                            //*** Data Row 4 ***//

                            // Set the x to be at the beginning of the line plus 35 plus the normal 3
                            $this->SetX($this->lMargin+28);

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a Note Label cell
                            $this->Cell(25,9,'Note:');

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Set the note string
                            $note=htmlspecialchars_decode($noteRow["Note"]);

                            // Get the number of lines in the note
                            $largestNOL=($this->WordWrap($note, 120)==0)?1:$this->WordWrap($note, 120);

                            // Get the current X and Y values
                            $startingX=$this->GetX();
                            $startingY=$this->GetY();

                            // Highlight over the places that need it
                            $this->highlightLocations($startingX, $startingY, 5, $note, '*');

                            // Set the x to be at the beginning of the line
                            $this->SetX($this->lMargin+53);
            
                            // Add a Note cell
                            $this->MultiCell(130,5,$note);

                            // Add a Line break
                            $this->ln(2);

                            // Data Row 5

                            // Determine if there is datum
                            if ($noteRow["Datum"]!=null && 
                                strpos(strtoupper($noteRow["FileName"]), '.ZIP')==FALSE){
                                // Set the x to be at the beginning of the line plus 35 plus the normal 3
                                $this->SetX($this->lMargin+28);

                                // Set the font to a normal font bolded
                                $this->SetFont('Times','B',11);

                                // Add a Note Label cell
                                $this->Cell(25,9,'Attachment:');

                                // Set the font to a normal font non-bolded
                                $this->SetFont('Times','');

                                // Download the file into this temp directory
                                file_put_contents('./temp/' . $noteRow["FileName"], $noteRow["Datum"]);

                                // Determine if this is an image type
                                if (getimagesize('./temp/' . $noteRow["FileName"])){
                                    // Add image
                                    $w=165; $h=0;
                                    $this->Image('./temp/' . $noteRow["FileName"],$this->lMargin+13,null,$w, $h);
                                    
                                    // Determine if a setup was passed in
                                    if ($category=="SETUP_M00" && $amountOfNotes!=$amount){
                                        // Add a new page for this note in the setup
                                        $this->AddPage();
                                    }
                                }
                                else {
                                    // Set the note string
                                    $textDatum=htmlspecialchars_decode($noteRow["Datum"]);

                                    // Get the number of lines in the datum
                                    $oldTextDatum=$textDatum;
                                    $largestNOL=($this->WordWrap($textDatum, 125)==0)?1:$this->WordWrap($textDatum, 125);

                                    // Add a datum cell
                                    $this->MultiCell(125,5,$oldTextDatum,1);

                                    // Add a Line break
                                    $this->ln(0);
                                    
                                    // Add a dividing line between notes
                                    $this->Line($this->lMargin+18, $this->GetY(), $this->rMargin-3,  $this->GetY());
                                }

                                // Remove the file
                                unlink('./temp/' . $noteRow["FileName"]);
                           }
                        }
                    } 
                }
            }
            
            // Return the return value
            return $creatednotes;
        }
        
        private function getAppropriatePercentageToFitPage($image, $maxwidth, $maxHeight){
            // Get percentage to use.  Max width is 180 and max height is 275.
            // Set at 100% at first
            $percentageToUse=1;

            // Determine if the width of the image is less than the width we have.
            if ($image[0]<$maxwidth){
                // Need to increase the image size.
                $percentageToUse=$image[0]/$maxwidth;
            }
            else {
                // Need to decrease the image size.
                $percentageToUse=$maxwidth/$image[0];
            }

            // Now determine if the percentage makes the other dimension too big
            if ($percentageToUse*$image[1]>$maxHeight){
                // Determine if the height of the image is less than the height we have.
                if ($image[1]<$maxHeight){
                    // Need to increase the image size.
                    $percentageToUse=$image[1]/$maxHeight;
                }
                else {
                    // Need to decrease the image size.
                    $percentageToUse=$maxHeight/$image[1];
                }
            }
            
            // Return whatever we figured out
            return $percentageToUse;
        }
        
        function PrintImage ($imageSizeArray, $fileName, $noteRow, $informOfMultiplePages, $actualSavedImageToUse, $printNote=true){
            // set the image as a nothing drawing at first
            $drawingType='';

            // Determine if it is specified as landscaped
            if (strpos($fileName, 'LDRAWING')!==FALSE){
                // Set the type to landscape
                $drawingType='LANDSCAPE';
            }
            else if (strpos($fileName, 'PDRAWING')!==FALSE){
                // Set the type to portrait
                $drawingType='PORTRAIT';
            }
            else {
                // Determine which one is largest. If width is largest, do landscape.  Else do portrait.
                // Guess portrait at first.
                $drawingType='PORTRAIT';

                // Determine if I guessed wrong
                if ($imageSizeArray[0]>$imageSizeArray[1]){
                    // Set the type to landscape
                    $drawingType='LANDSCAPE';
                }
            }

            // Determine if I need to figure out the orientation or if one is specified
            $yFinal=0;
            $topY=0;
            if ($drawingType=='PORTRAIT'){
                // Determine if we want to print notes with this
                if ($printNote){
                    //*** Data Row 1 ***//

                    // Set the x to be at the beginning of the line plus 3
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Created By Label cell
                    $this->Cell(25,5,'Created By:');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a Notes created By cell
                    $this->Cell(45,5,htmlspecialchars_decode($noteRow["FirstName"] . ' ' . $noteRow["LastName"]));  

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Created By Date Label cell
                    $this->Cell(25,5,'Created On:');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a Notes created By date cell
                    $this->Cell(53,5,$noteRow["Date"],0,1);

                    //*** Data Row 2 ***//

                    // Set the x to be at the beginning of the line plus 3
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Title Label cell
                    $this->Cell(25,5,'Title:');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Get the current X and Y values
                    $startingX=$this->GetX();
                    $startingY=$this->GetY();

                    // Highlight over the places that need it
                    $this->highlightLocations($startingX, $startingY, 5, htmlspecialchars_decode($noteRow["Title"]), '*');

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+28);

                    // Add a title cell
                    $this->Cell(128,5,htmlspecialchars_decode($noteRow["Title"]),0,1);

                    //*** Data Row 3 ***//

                    // Set the x to be at the beginning of the line plus 3
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Note Label cell
                    $this->Cell(25,5,'Note:');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Set the note string
                    $note=htmlspecialchars_decode($noteRow["Note"] . $informOfMultiplePages);

                    // Get the number of lines in the note
                    $largestNOL=($this->WordWrap($note, 153)==0)?1:$this->WordWrap($note, 153);

                    // Get the current X and Y values
                    $startingX=$this->GetX();
                    $startingY=$this->GetY();

                    // Highlight over the places that need it
                    $this->highlightLocations($startingX, $startingY, 5, $note, '*');

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+28);

                    // Add a Note cell
                    $this->MultiCell(163,5,$note);

                    // Add a Line break
                    $this->ln(1);
                }

                // Data Row 5

                // Get percentage to use.  Max width is 200 and max height is 300.
                $percentageToUse=$this->getAppropriatePercentageToFitPage($imageSizeArray, 175, 275-$this->GetY());                                    

                // Get the height amount to fill the page, since that should be bigger of the two
                $height = intval($imageSizeArray[1] * $percentageToUse);

                // put all text and images back to normal portrait rotation
                $this->RotatedImage($actualSavedImageToUse,0,null,0,$height,0);
            }
            else if ($drawingType=='LANDSCAPE'){
                // Set the left margin to be at zero
                $this->setY(0);
                
                // Set the space taken by the note to be zero to start
                $spaceTakenByNote=0;
                
                // Determine if we want to print notes with this
                if ($printNote){
                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Created By Label cell
                    $this->RotatedText($this->lMargin,260,'Created By:',90);                                

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a Notes created By cell
                    $this->RotatedText($this->lMargin,235,htmlspecialchars_decode($noteRow["FirstName"] . ' ' . $noteRow["LastName"]),90);                                

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Created By Date Label cell
                    $this->RotatedText($this->lMargin,190,'Created On:',90);                                

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a Notes created By date cell
                    $this->RotatedText($this->lMargin,165,$noteRow["Date"],90);                                

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Title Label cell
                    $this->RotatedText($this->lMargin,112,"Title:",90);                                

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a title cell
                    $this->RotatedText($this->lMargin,87,htmlspecialchars_decode($noteRow["Title"]),90);                                

                    //*** Data Row 2 ***//

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a Note Label cell
                    $this->RotatedText($this->lMargin+4,260,"Note:",90);                                

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Set the note string
                    $note=htmlspecialchars_decode($noteRow["Note"] . $informOfMultiplePages);

                    // Set the note string
                    $yFinal = $this->MultiCellRotatedText($this->lMargin+4,235,$note,90,130);                                

                    // Get the amount that the note took
                    $spaceTakenByNote = $yFinal - 235;
                }

                // Data Row 3

                // Get percentage to use.  Max width is 260 and max height is 190 depending on the size of the note text.
                $percentageToUse=$this->getAppropriatePercentageToFitPage($imageSizeArray, 275, (175-$spaceTakenByNote));                                    

                // Get the height amount to fill the page, since that should be bigger of the two
                $height = intval($imageSizeArray[1] * $percentageToUse);

                // put all text and iamges back to normal landscape rotation
                $this->RotatedImage($actualSavedImageToUse,$this->getY()+$spaceTakenByNote+5,$topY+275,0,$height,90);                                    
            }
        }
        
        function FullPageNoteImages($db, $userRow, $inCategoryID, $category, $tableName="notes"){
            // Create a return value to determine if there were any notes created and send back
            $creatednotes=false;
            
            // Determine if we have a valid task ID
            if ($inCategoryID!=null && $inCategoryID>0){
                // Determine if we are not querying from the notes table
                if ($tableName=="attachments"){
                    // Create the query to get the the notes and the user that created the notes
                    $sql = 'SELECT "Parent Note" AS Title, a.Title AS FileName, a.Description AS Note, a.SubmitDate AS Date, "Added" AS FirstName, "Attachment" AS LastName, a.Datum ' .
                          'FROM attachments a ' . 
                          'WHERE a.TaskID=' . $inCategoryID . ' ' .
                          'AND ((Upper(a.Title) LIKE "%_SSInclusion%") OR (Upper(a.Title) LIKE "%_FP_%"));';
                }
                else{
                    // Create the query to get the the notes and the user that created the notes
                    $sql = 'SELECT n.*, u.FirstName, u.LastName, u.UserLevel ' .
                          'FROM notes n ' . 
                          'INNER JOIN users u ON n.EnteredByID = u.UserID ' .
                          'WHERE n.AssociatedCategoryID=' . $inCategoryID . ' ' .
                          'AND n.Category="' . $category . '" '. 
                          'AND ((Upper(n.FileName) LIKE "%DRAWING%") OR (Upper(n.FileName) LIKE "%_SSInclusion%") OR (Upper(n.FileName) LIKE "%_FP_%")) '. 
                          'ORDER BY n.NoteOrder, n.Date DESC;';
                }
                
                // Attempt to get all of the drawings for this category type and ID
                $taskNoteDrawingRows = $db->queryDB($sql, $userRow, "setup_sheet_template Form occurance 3");

                //*** Data Row 1 ***//

                // Determine if there were rows for the task note drawings
                if ($taskNoteDrawingRows != null){
                    // Loop through the returned results
                    $amount=0;
                    while ($noteRow=mysqli_fetch_array($taskNoteDrawingRows)){
                        // Set a name for the file
                        $currentFileName=strtoupper($noteRow["FileName"]);
                        
                        // Determine if it is a file extention that we do not look at
                        if ( strpos($currentFileName, '.EDRW')!==FALSE || 
                             strpos($currentFileName, '.DRW')!==FALSE || 
                             strpos($currentFileName, '.SLDDRW')!==FALSE || 
                             strpos($currentFileName, '.SLDPRT')!==FALSE || 
                             strpos($currentFileName, '.SLDASM')!==FALSE || 
                             strpos($currentFileName, '.STEP')!==FALSE || 
                             strpos($currentFileName, '.DOCX')!==FALSE || 
                             strpos($currentFileName, '.DOC')!==FALSE || 
                             strpos($currentFileName, '.DOT')!==FALSE || 
                             strpos($currentFileName, '.DOTM')!==FALSE || 
                             strpos($currentFileName, '.XPS')!==FALSE || 
                             strpos($currentFileName, '.XLSX')!==FALSE || 
                             strpos($currentFileName, '.XLT')!==FALSE || 
                             strpos($currentFileName, '.XLS')!==FALSE || 
                             strpos($currentFileName, '.XLSB')!==FALSE || 
                             strpos($currentFileName, '.XLSM')!==FALSE || 
                             strpos($currentFileName, '.PPT')!==FALSE || 
                             strpos($currentFileName, '.PPSX')!==FALSE || 
                             strpos($currentFileName, '.PPTM')!==FALSE || 
                             strpos($currentFileName, '.PPTX')!==FALSE){
                            // Next row
                            continue;
                        }
                        
                        // Increment the notes amount
                        $amount++;
                        
                        // Set the return value to say we attempted to create one
                        $creatednotes|=true;
                        
                        // Determine if there is datum
                        if ($noteRow["Datum"]!=null){
                            // Download the file into this temp directory
                            file_put_contents('./temp/' . $noteRow["FileName"], $noteRow["Datum"]);

                            $imageSizeArray=null;
                            
                            // Determine if this is a PDF
                            $actualSavedImageToUse = './temp/' . $noteRow["FileName"];
                            $informOfMultiplePages = "";
                            if (strpos($currentFileName, '.PDF')!==false){
                                // Create Objects
                                // ------------------------------
                                // $imagickObject = new imagick();

//                                 // Set the resolution so that we get better quality
//                                 // ------------------------------
//                                 $imagickObject->setResolution(300, 300);

//                                 // Grab Data from the PDF
//                                 // ------------------------------
//                                 $imagickObject->readImage($actualSavedImageToUse);

//                                 // Setup the file to have the correct parameters for making it to JPG
//                                 $imagickObject->setImageFormat("png");
//                                 $imagickObject->setFormat("png");

//                                 // Flatten against white to fix transparent colors becoming black
//                                 $imagickObject->setImageBackgroundColor('white');
// //                                $imagickObject->setImageAlphaChannel(11);
// //                                $imagickObject->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

//                                 // Loop through all of the pages in the PDF
//                                 $loopNumber=0;
//                                 foreach($imagickObject as $i=>$imagickObject) {
//                                     // Increment the loop counter
//                                     $loopNumber++;

//                                     // Create the temp filename
//                                     $currentFileName="./temp/_".$noteRow["FileName"]."_".($loopNumber) .".png";

//                                     // Write the temp file
//                                     $imagickObject->writeImage($currentFileName);

//                                     // Get the image size array
//                                     $imageSizeArray=getimagesize($currentFileName);

//                                     // Add a new page for this setup
//                                     $this->AddPage('', '', 0, null, false);

//                                     // Get the current Y after new page
//                                     $topY=$this->GetY();

//                                     // Print the image to the SS
//                                     $this->PrintImage ($imageSizeArray, $actualSavedImageToUse, $noteRow, $informOfMultiplePages, $currentFileName, false);

//                                     // Remove the temp image from the server
//                                     unlink ($currentFileName);                                
//                                 }
//                                 // Clean up the image file
//                                 $imagickObject->clear();
//                                 $imagickObject->destroy();

//                                 // Remove the PDF from the server
//                                 unlink ($actualSavedImageToUse);                                
                            }
                            else {
                                // Add a new page for this setup
                                $this->AddPage('', '', 0, null, false);
                                
                                // Get the current Y after new page
                                $topY=$this->GetY();

                                // Get the image size array
                                $imageSizeArray=getimagesize('./temp/' . $noteRow["FileName"]);
                                
                                // Print the image
                                $this->PrintImage ($imageSizeArray, $currentFileName, $noteRow, $informOfMultiplePages, $actualSavedImageToUse, false);
                            }
                            
//                             // Determine if this is not an image type
//                             if (!$imageSizeArray){
//                                 // Set the x to be at the beginning of the line plus 3
//                                 $this->SetX($this->lMargin+3);

//                                 // Set the note string
//                                 $textDatum=htmlspecialchars_decode($noteRow["Datum"]);

//                                 // Get the number of lines in the datum
//                                 $oldTextDatum=$textDatum;
//                                 $largestNOL=($this->WordWrap($textDatum, 180)==0)?1:$this->WordWrap($textDatum, 180);

//                                 // Add a datum cell
//                                 $this->MultiCell(180,5,$oldTextDatum,1);

//                                 // Add a Line break
//                                 $this->ln(0);
//                             }

//                             // Remove the files
//                             unlink('./temp/' . $noteRow["FileName"]);
//                             unlink($actualSavedImageToUse);
                      }
//                        // Add a dividing line between notes
//                        $this->Line($this->lMargin+28, $this->GetY(), $this->rMargin-3,  $this->GetY());
//
//                        // Add a Line break
//                        $this->ln(0);
                    }
                }
            }
            
            // Add a Line break
            $this->ln(0);
            
            // Return the return value
            return $creatednotes;
        }
        
        // Page header
        function Header($hexFillColor=null, $show=true)
        {
            // Determine if we want to sho the footer
            if ($show==true){
                // Determine if they sent in a fill color
                if ($hexFillColor===null){
                    // Just set the standard color
                    $this->SetFillColor(255,255,255);
                }
                else {
                    // Set the color passed in
                    $this->SetFillColorHex($hexFillColor);
                }

                // Add the EES Logo
                $w=30;
                $this->Image('../images/logo_EES_trans.png',10,6,$w);

                // Set the title font to Arial bold 15
                $this->SetFont('Arial','B',15);

                // Move to the right
                $this->Cell(35);

                // Add a title cell with label
                $this->Cell(110,10,'          Setup Sheet          ',0,0,'C', true);

                // Move to the right
                $this->Cell(25);

                // Set the page number font to Arial italic 8
                $this->SetFont('Arial','I',8);

                // Add the Page number
                $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'L');

                // Add a Line break
                $this->SetY(20);

                // Create and add a line
                $this->SetLineWidth(0.01);
                $this->Line($this->lMargin-5, 20, $this->rMargin + 5, 20);

                // Create a line break
                $this->Ln(2);

                // Set the font to a normal font 
                $this->SetFont('Times','',11);
            }
        }

        // Page footer
        function Footer($hexFillColor=null)
        {
            // Determine if they sent in a fill color
            if ($hexFillColor===null){
                // Just set the standard color
                $this->SetFillColor(255,255,255);
            }
            else {
                // Set the color passed in
                $this->SetFillColorHex($hexFillColor);
            }

//            // Position at 1.5 cm from bottom
//            $this->SetY(-15);
//
//            // Set font to Arial italic 8
//            $this->SetFont('Arial','I',8);
//
//            // Add the Page number
//            $this->Cell(190,10,'Page '.$this->PageNo().'/{nb}',0,1,'C');
//
//            // Set the font to a normal font 
//            $this->SetFont('Times','',11);
        }
        
        // Create the general task info
        function GeneralTaskInfo($db, $userRow, $assocTask, $assocChildrenLowerThanMachining){
            // Working with 180 between margins.  3 from the left side.
            
            // Set the title font to Arial bold 15
            $this->SetFont('Arial','IB',12);

            // Add a title cell with label
            $this->Cell(180,10,'Task Information',0,1,'C');

            // Set the draw color to a neutral one
            $this->SetDrawColor(111,111,111);
            
            // Create and add a line
            $this->SetLineWidth(1);
            $this->Line($this->lMargin, $this->GetY(), $this->rMargin,  $this->GetY());
            
            // Add a Line break
            $this->ln(3);
            
            // Set the draw color to black
            $this->SetDrawColor(0);
            
            // Set line width to default
            $this->SetLineWidth(.01);
            
            //*** Data Row 1 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Task ID Label cell
            $this->Cell(25,5,'Task ID:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Task ID cell
            $this->Cell(32,5,$assocTask["TaskID"],1,0,'C');

            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a completes task Label cell
            $this->Cell(23,5,'Parent ID:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // If it is NOT a parent, then we have to ask another question
            if ($assocTask["ParentID"]!=null &&
                $assocTask["ParentID"]>0){
                // Add a Parent Task ID cell
                $this->Cell(35,5,$assocTask["ParentID"],1,0,'C');
            }
            else {
                // Add a NONE to this cell
                $this->Cell(35,5,"NONE",1,0,'C');
            }
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a completes task Label cell
            $this->Cell(45,5,'Completes Parent Task:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // If it is NOT a parent, then we have to ask another question
            if ($assocChildrenLowerThanMachining!=null &&
                $assocChildrenLowerThanMachining["ChildrenUnderMachining"]>0){
                // Add a completes task cell
                $this->Cell(20,5,'NO',1,1,'C');
            }
            else {
                // Determine if there are no more children coming
                if ($assocTask["ParentAllPartsSetup"]==1){
                    // Add a completes task cell
                    $this->Cell(20,5,'YES',1,1,'C',true);
                }
                else {
                    // Add a completes task cell
                    $this->Cell(20,5,'NO',1,1,'C');
                }
            }
            
            //*** Data Row 2 ***//
            
            // Create the two texts with carriage returns
            $taskTitle=htmlspecialchars_decode($assocTask["Title"]);            
            $parentTitle=htmlspecialchars_decode($assocTask["ParentTitle"]); 
            
            // Get the highest number of lines between the two
            $firstNOL=($this->WordWrap($taskTitle, 55)==0)?1:$this->WordWrap($taskTitle, 55);
            $secondNOL=($this->WordWrap($parentTitle, 55)==0)?1:$this->WordWrap($parentTitle, 55);
            $largestNOL=($firstNOL>$secondNOL)?$firstNOL:$secondNOL;

            // Get the current Y value
            $y=$this->GetY();
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Task ID Label cell
            $this->Cell(25,5*$largestNOL,'Task Title:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');

            // Add a Task ID cell
            $this->MultiCell(60,5*($largestNOL/$firstNOL),$taskTitle,1,'C');

            // Set the y back to what it started as and the x adjusted appropriately
            $this->SetY($y);
            $this->SetX($this->lMargin+88);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a completes task Label cell
            $this->Cell(35,5*$largestNOL,'Parent Title:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there is a parent title
            if ($assocTask["ParentTitle"]==null){
                // Add a completes task cell
                $this->Cell(60,5*$largestNOL,'NONE',1, 1,'C');
            }
            else {
                // Add a completes task cell
                $this->MultiCell(60,5*($largestNOL/$secondNOL),$parentTitle,1,'C');
            }
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 3 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the hardware string
            $mainTaskNotes=htmlspecialchars_decode($assocTask["Entry"]);
            
            // Determine if the hardware is nothing
            if ($mainTaskNotes==null || $mainTaskNotes==''){
                // Set it to display none
                $mainTaskNotes='NONE';
            }
            
            // Get the number of lines in the customer name
            $largestNOL=($this->WordWrap($mainTaskNotes, 145)==0)?1:$this->WordWrap($mainTaskNotes, 145);

            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Hardware Label cell
            $this->Cell(25,5*$largestNOL,'Initial Notes:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Get the current X and Y values
            $startingX=$this->GetX();
            $startingY=$this->GetY();
            
            // Highlight over the places that need it
            $this->highlightLocations($startingX, $startingY, 5, $mainTaskNotes, '*');
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Hardware cell
            $this->MultiCell(155,5,$mainTaskNotes,1,'L');
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 4 ***//
            
            // Create the customer name with carriage returns
            $companyName=htmlspecialchars_decode($assocTask["CompanyName"]);            
            
            // Get the number of lines in the customer name
            $largestNOL=($this->WordWrap($companyName, 55)==0)?1:$this->WordWrap($companyName, 55);

            // Get the current Y value
            $y=$this->GetY();
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Customer Name Label cell
            $this->Cell(25,9*$largestNOL,'Customer:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');

            // Set the company color as highlight
            $this->SetFillColorHex($assocTask["CompanyRowColor"]);
            
            // Add a Customer Name cell
            $this->MultiCell(60,9,$companyName,1,'C',true);

            // Set the fill color to yellow so they pay attention
            $this->SetFillColor(255,255,0);

            // Set the y back to what it started as and the x adjusted appropriately
            $this->SetY($y);
            $this->SetX($this->lMargin+88);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a priority Label cell
            $this->Cell(35,9*$largestNOL,'Priority:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine what the expedite level is
            if ($assocTask["Expedite"]==0){
                // Add a priority cell as normal
                $this->Cell(60,9*$largestNOL,'NORMAL',1,1,'C');
            }
            else {
                // Add a priority cell as expedite
                $this->Cell(60,9*$largestNOL,'EXPEDITE',1,1,'C',true);
            }
            
            //*** Data Row 5 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Issued Date Label cell
            $this->Cell(25,5,'Issued Date:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Issued Date cell
            $this->Cell(60,5,$assocTask["IssuedToMachineDate"],1,0,'C');

            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a Due Date Label cell
            $this->Cell(35,5,'Due Date:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there is a due date setup
            if ($assocTask["DueDate"]!= null){
                // Add a Due Date cell
                $this->Cell(60,5,$assocTask["DueDate"],1,1,'C');
            }
            else {
                // Add a Due Date cell
                $this->Cell(60,5,'NONE',1,1,'C');
            }

            //*** Data Row 6 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the hardware string
            $hardware=htmlspecialchars_decode($assocTask["PartHardware"]);
            
            // Determine if the hardware is nothing
            if ($hardware==null || $hardware==''){
                // Set it to display none
                $hardware='NONE';
            }
            
            // Get the number of lines in the hardware name
            $largestNOL=($this->WordWrap($hardware, 150)==0)?1:$this->WordWrap($hardware, 150);

            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Hardware Label cell
            $this->Cell(25,5*$largestNOL,'Hardware:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Get the current X and Y values
            $startingX=$this->GetX();
            $startingY=$this->GetY();

            // Highlight over the places that need it
            $this->highlightLocations($startingX, $startingY, 5, $hardware, '*');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
    
            // Add a Hardware cell
            $this->MultiCell(155,5,$hardware,1,'L');

            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 7 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Create a secondary Status string
            $secondaryStatusString=htmlspecialchars_decode('Secondary Status: ' . $assocTask["SSName"] . ' - ' . $assocTask["SSDescription"]);
            
            // Determine if the secondary status is nothing
            if ($secondaryStatusString==null || $secondaryStatusString==''){
                // Set it to display none
                $secondaryStatusString='NONE';
            }
            
            // Get the number of lines in the hardware name
            $largestNOL=($this->WordWrap($secondaryStatusString, 150)==0)?1:$this->WordWrap($secondaryStatusString, 150);

            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a secondary status Label cell
            $this->Cell(25,5*$largestNOL,'Extra Info:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Get the current X and Y values
            $startingX=$this->GetX();
            $startingY=$this->GetY();

            // Highlight over the places that need it
            $this->highlightLocations($startingX, $startingY, 5, $secondaryStatusString, '*');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
    
            // Set the color passed in
            $this->SetFillColorHex($assocTask["SSRowColor"]);
                
            // Add a Secondary Status cell
            $this->MultiCell(155,5,$secondaryStatusString,1,'L',true);

            // Set the fill color back to yellow for highlighting
            $this->SetFillColor(255,255,0);
            
            // Add a Line break
            $this->ln(0);
            
            //*** Start of Task Verifications ***//

            // Add a Line break
            $this->ln(0);
            
            // Set the title font to Arial bold 15
            $this->SetFont('Arial','IB',12);

            // Add a title cell with label
            $this->Cell(180,10,'Task Verifications',0,1,'C');

            // Set the draw color to a neutral one
            $this->SetDrawColor(111,111,111);
            
            // Create and add a line
            $this->SetLineWidth(1);
            $this->Line($this->lMargin, $this->GetY(), $this->rMargin,  $this->GetY());
            
            // Add a Line break
            $this->ln(3);
            
            // Set the draw color to black
            $this->SetDrawColor(0);
            
            // Set line width to default
            $this->SetLineWidth(.01);
            
            //*** Data Row 8 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Machining cell
            $this->Cell(180,5,'Machining',0,1,'C');

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 9 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Machinist ID cell
            $this->Cell(60,5,'Ran By Id(s)',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'Measured By ID(s)',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'Deburred By ID(s)',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 10 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Machinist ID cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(3);
            
            //*** Data Row 11 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Machining cell
            $this->Cell(180,5,'QA',0,1,'C');

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 12 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a First Article Check cell
            $this->Cell(180,5,'First Article Checked By ID(s)',1,1,'C');

            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 13 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a First Article Check cell
            $this->Cell(180,5,'',1,1,'C');

            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 14 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Final Measurement ID cell
            $this->Cell(60,5,'Final Measurement By Id(s)',1,0,'C');

            // Add a Deburred By cell
            $this->Cell(60,5,'Deburred By ID(s)',1,0,'C');

            // Add a Tapped By cell
            $this->Cell(60,5,'Tapped By ID(s)',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 15 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Machinist ID cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 16 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Final Measurement Checked By ID cell
            $this->Cell(60,5,'Final Measurement Checked By Id(s)',1,0,'C');

            // Add a Deburred By ID cell
            $this->Cell(60,5,'Deburring Checked By ID(s)',1,0,'C');

            // Add a Tapped By ID cell
            $this->Cell(60,5,'Tapping Checked By ID(s)',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 17 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Machinist ID cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(3);
            
            //*** Data Row 18 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Machining cell
            $this->Cell(180,5,'Washing/Final Checks',0,1,'C');

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 19 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Machinist ID cell
            $this->Cell(60,5,'Washed By Id(s)',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'Deburr Checked By ID(s)',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'Tapping Checked By ID(s)',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 20 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Add a Machinist ID cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,0,'C');

            // Add a Machinist Measuring cell
            $this->Cell(60,5,'',1,1,'C');

            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+28);
            
            // Add a Line break
            $this->ln(0);
        }
        
        // Create the general task info
        function TaskNotes($db, $userRow, $assocTask){
            // Working with 180 between margins.  3 from the left side.
            
            //*** Start of Task Notes ***//

            // Create the query to get the the notes and the user that created the notes
            $sql = 'SELECT n.*, u.FirstName, u.LastName, u.UserLevel ' .
                   'FROM notes n ' . 
                   'INNER JOIN users u ON n.EnteredByID = u.UserID ' .
                   'WHERE n.AssociatedCategoryID=' . $assocTask["TaskID"] . ' ' .
                   'AND n.Category="TASK" '. 
                   'AND (n.FileName IS NULL OR UPPER(n.FileName) NOT LIKE "%DRAWING%") '. 
                   'ORDER BY n.NoteOrder, n.Date DESC;';
                
            // Attempt to get all of the notes for this category type and ID
            $taskNoteRows = $db->queryDB($sql, $userRow, "setup_sheet_template Form occurance 2");

            //*** Data Row 1 ***//

            // Determine if there were rows for the task notes
            if ($taskNoteRows != null && mysqli_num_rows($taskNoteRows)>0){
                // Add a new page for this setup
                $this->AddPage();

                // Add a Line break
                $this->ln(0);

                // Set the title font to Arial bold 15
                $this->SetFont('Arial','IB',12);

                // Add a title cell with label
                $this->Cell(180,10,'Task Notes',0,1,'C');

                // Set the draw color to a neutral one
                $this->SetDrawColor(111,111,111);

                // Create and add a line
                $this->SetLineWidth(1);
                $this->Line($this->lMargin, $this->GetY(), $this->rMargin,  $this->GetY());

                // Add a Line break
                $this->ln(3);

                // Set the draw color to black
                $this->SetDrawColor(0);

                // Set line width to default
                $this->SetLineWidth(.01);

                // Get the notes for the overall task
                $this->Notes($db, $userRow, $assocTask["TaskID"], "Task");
            }
        }
        
        function Blanks($db, $userRow, $inTaskID){
            // Determine if we have a valid task ID
            if ($inTaskID!=null && $inTaskID>0){
                // Create the query to get the the blanks for this task
                $sql = 'SELECT jb.*, m.Name, m.Category ' .
                       'FROM jobblanks jb ' .
                       'LEFT JOIN material m ON jb.MaterialID = m.MaterialID ' .
                       'WHERE jb.TaskID=' . $inTaskID . ';';
        
                // Attempt to get all of the blanks for this task ID
                $taskBlankRows = $db->queryDB($sql, $userRow, "setup_sheet_template Form occurance 4");

                // Set the top Y
                $topY=$this->GetY();
                
                // Set the x to be at the beginning of the line
                $this->SetX($this->lMargin+3);
            
                // Set the title font to Arial bold 15
                $this->SetFont('Arial','IB',12);

                // Add a title cell with label
                $this->Cell(180,10,'Blanks',0,1,'C');
            
                // Set the x to be at the beginning of the line
                $this->SetX($this->lMargin+3);

                // Determine if there were rows for the task blanks
                if ($taskBlankRows != null){
                    // Loop through the returned results
                    $amount=0;
                    while ($blankRow=mysqli_fetch_array($taskBlankRows)){
                        //*** Data Row 1 ***//

                        // Set the x to be at the beginning of the line
                        $this->SetX($this->lMargin+3);

                        // Set the font to a normal font bolded
                        $this->SetFont('Times','B',11);

                        // Add a machine name cell and highlight it.
                        $this->Cell(35,5,'Material:',1,0,'L');
                        
                        // Set the font to a normal font non-bolded
                        $this->SetFont('Times','');

                        // Add a machine name cell and highlight it.
                        $this->Cell(55,5,$blankRow["Name"],1,0,'C');

                        // Set the font to a normal font bolded
                        $this->SetFont('Times','B',11);

                        // Add a machine name cell and highlight it.
                        $this->Cell(35,5,'Blank Qty:',1,0,'L');
                        
                        // Set the font to a normal font non-bolded
                        $this->SetFont('Times','');

                        // Add a machine name cell and highlight it.
                        $this->Cell(55,5,$blankRow["Quantity"],1,1,'C');
                        
                        //*** Data Row 2 ***//

                        // Set the x to be at the beginning of the line
                        $this->SetX($this->lMargin+3);

                        // Set the font to a normal font bolded
                        $this->SetFont('Times','B',11);

                        // Add a machine name cell and highlight it.
                        $this->Cell(15,5,'(X):',1,0,'L');
                        
                        // Set the font to a normal font non-bolded
                        $this->SetFont('Times','');

                        // Determine if it is precise or clean
                        $defaultCut = 'ROUGH';
                        $highlight=false;
                        if ($blankRow["Dimension1Precise"]==1){
                            // Set highlighting to true
                            $highlight=true;
                            
                            // Set the fill color to red to denote important
                            $this->SetFillColor(255,0,0);
                            
                            // Set the descriptor to annotate precise
                            $defaultCut = 'PRECISE';
                        }
                        else if ($blankRow["Dimension1Clean"]==1){
                            // Set highlighting to true
                            $highlight=true;
                            
                            // Set the fill color to yellow so they pay attention
                            $this->SetFillColor(255,255,0);
                            
                            // Set the descriptor to annotate precise
                            $defaultCut = 'CLEAN';
                        }
                        
                        // Add a machine name cell and highlight it.
                        $this->Cell(45,5,$blankRow["Dimension1"] . ' ' . $defaultCut,1,0,'C',$highlight);

                        // Set the font to a normal font bolded
                        $this->SetFont('Times','B',11);

                        // Add a machine name cell and highlight it.
                        $this->Cell(15,5,'(Y):',1,0,'L');
                        
                        // Set the font to a normal font non-bolded
                        $this->SetFont('Times','');

                        // Determine if it is precise or clean
                        $defaultCut = 'ROUGH';
                        $highlight=false;
                        if ($blankRow["Dimension2Precise"]==1){
                            // Set highlighting to true
                            $highlight=true;
                            
                            // Set the fill color to red to denote important
                            $this->SetFillColor(255,0,0);
                            
                            // Set the descriptor to annotate precise
                            $defaultCut = 'PRECISE';
                        }
                        else if ($blankRow["Dimension2Clean"]==1){
                            // Set highlighting to true
                            $highlight=true;
                            
                            // Set the fill color to yellow so they pay attention
                            $this->SetFillColor(255,255,0);
                            
                            // Set the descriptor to annotate precise
                            $defaultCut = 'CLEAN';
                        }
                        
                        // Add a machine name cell and highlight it.
                        $this->Cell(45,5,$blankRow["Dimension2"] . ' ' . $defaultCut,1,0,'C', $highlight);

                        // Set the font to a normal font bolded
                        $this->SetFont('Times','B',11);

                        // Add a machine name cell and highlight it.
                        $this->Cell(15,5,'(Z):',1,0,'L');
                        
                        // Set the font to a normal font non-bolded
                        $this->SetFont('Times','');

                        // Determine if it is precise or clean
                        $defaultCut = 'ROUGH';
                        $highlight=false;
                        if ($blankRow["Dimension3Precise"]==1){
                            // Set highlighting to true
                            $highlight=true;
                            
                            // Set the fill color to red to denote important
                            $this->SetFillColor(255,0,0);
                            
                            // Set the descriptor to annotate precise
                            $defaultCut = 'PRECISE';
                        }
                        else if ($blankRow["Dimension3Clean"]==1){
                            // Set highlighting to true
                            $highlight=true;
                            
                            // Set the fill color to yellow so they pay attention
                            $this->SetFillColor(255,255,0);
                            
                            // Set the descriptor to annotate precise
                            $defaultCut = 'CLEAN';
                        }
                        
                        // Add a machine name cell and highlight it.
                        $this->Cell(45,5,$blankRow["Dimension3"] . ' ' . $defaultCut,1,1,'C', $highlight);
                        
                        //*** Data Row 3 ***//

                        // Get the amount of lines that name takes
                        $reason=htmlspecialchars_decode($blankRow["Reason"]);
                        $largestNOL=($this->WordWrap($reason, 155)==0)?1:$this->WordWrap($reason, 155);

                        // Set the x to be at the beginning of the line
                        $this->SetX($this->lMargin+3);

                        // Set the font to a normal font bolded
                        $this->SetFont('Times','B',11);

                        // Add a machine name cell and highlight it.
                        $this->Cell(20,5*$largestNOL,'Reason:',0,0,'L');
                        
                        // Set the font to a normal font non-bolded
                        $this->SetFont('Times','');

                        // Get the current X and Y values
                        $startingX=$this->GetX();
                        $startingY=$this->GetY();

                        // Highlight over the places that need it
                        $this->highlightLocations($startingX, $startingY, 5, $reason, '*');

                        // Set the x to be at the beginning of the line
                        $this->SetX($this->lMargin+23);
                
                        // Add a machine name cell and highlight it.
                        $this->MultiCell(160,5,$reason,0,'L');
                        
                        // Add a Line break
                        $this->ln(0);
                    }
                }
                else {
                    // Set the fill color to red to denote something necessary for the programmer to add
                    $this->SetFillColor(255,0,0);

                    // Add a machine name cell and highlight it.
                    $this->Cell(180,5,'NONE',0,1,'C', true);

                    // Set the standard fill color back to yellow for highlighting
                    $this->SetFillColor(255,255,0);
                }
                
                // Draw left and right borders
//                $this->Line($this->lMargin+3, $topY, $this->lMargin+3,  $this->GetY());
//                $this->Line($this->rMargin-1, $topY, $this->rMargin-1,  $this->GetY());
                
                // Draw bottom border
                $this->Line($this->lMargin+3, $this->GetY(), $this->rMargin-1,  $this->GetY());
            }
        }
        
        // Create the general task info
        function PostMachining($assocTask){
            // Working with 180 between margins.  3 from the left side.
            
            // Add a Line break
            $this->ln(5);
            
            // Set the top Y
            $topY=$this->GetY();

            // Set the title font to Arial bold 15
            $this->SetFont('Arial','IB',12);

            // Add a title cell with label
            $this->Cell(180,10,'Post Machining Operations',0,1,'C');

            // Set the draw color to a neutral one
            $this->SetDrawColor(111,111,111);
            
            // Create and add a line
            $this->SetLineWidth(1);
            $this->Line($this->lMargin+3.5, $this->GetY(), $this->rMargin-1.5,  $this->GetY());
            
            // Add a Line break
            $this->ln(3);
            
            // Set the draw color to black
            $this->SetDrawColor(0);
            
            // Set line width to default
            $this->SetLineWidth(.01);
            
            //*** Data Row 1 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a vacuum clean Label cell
            $this->Cell(50,5,'Vacuum Clean:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if it needs vacuum cleaning
            if ($assocTask["IsVacuumClean"]==0){
                // Add a vacuum cleaning cell
                $this->Cell(40,5,'NO',1,0,'C');
            }
            else {
                // Add a vacuum cleaning cell highlighted
                $this->Cell(40,5,'YES',1,0,'C',true);
            }
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a wash for anodize Label cell
            $this->Cell(50,5,'Wash For Anodize:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if it needs washed for anodizing
            if ($assocTask["IsWashForAnodize"]==0){
                // Add a wash for anodizing cell
                $this->Cell(40,5,'NO',1,1,'C');
            }
            else {
                // Add a wash for anodizing cell highlighted
                $this->Cell(40,5,'YES',1,1,'C',true);
            }
            
            //*** Data Row 2 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a cosmetic damage Label cell
            $this->Cell(50,5,'Allowable Cosmetic Damage:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if it slight cosmetic damage is allowed
            if ($assocTask["HasAllowableCosmeticDamage"]==0){
                // Add a slight cosmetic damage cell highlighted
                $this->Cell(130,5,'NO',1,1,'C', true);
            }
            else {
                // Add a slight cosmetic damage cell non-highlighted
                $this->Cell(130,5,'YES',1,1,'C');
            }
            
            //*** Data Row 3 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Get post tapping instructions
            $oldPostTappingInstructions=htmlspecialchars_decode($assocTask["PostMachiningTappingInstructions"]);
            $postTappingInstructions=htmlspecialchars_decode($assocTask["PostMachiningTappingInstructions"]);
            
            // Get the amount of lines that name takes
            $largestNOL=($this->WordWrap($postTappingInstructions, 85)==0)?1:$this->WordWrap($postTappingInstructions, 85);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
    
            // Add a Task ID Label cell
            $this->Cell(50,5*$largestNOL,'Post Tapping:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if it needs tapping after maching
            if ($assocTask["IsTappedPostMachining"]==0){
                // Add a post tapping cell
                $this->Cell(40,5*$largestNOL,'NO',1,0,'C');
            }
            else {
                // Add a post tapping cell highlighted
                $this->Cell(40,5*$largestNOL,'YES',1,0,'C',true);
            }
            
            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there are any instructions there 
            if ($oldPostTappingInstructions==''){
                // Add a post tapping cell annotating and set to none
                $this->Cell(90,5*$largestNOL,'NONE',1,1,'C');
            }
            else {
                // Get the current X and Y values
                $startingX=$this->GetX();
                $startingY=$this->GetY();

                // Highlight over the places that need it
                $this->highlightLocations($startingX, $startingY, 5, $postTappingInstructions, '*');

                // Set the x to be at the beginning of the line
                $this->SetX($this->lMargin+93);
    
                // Add a post tapping cell with instructions highlighted
                $this->MultiCell(90,5,$postTappingInstructions,1,'L');

                // Add a Line break
                $this->ln(0);
            }

            //*** Data Row 4 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Get post tapping instructions
            $oldSandblastingInstructions=htmlspecialchars_decode($assocTask["SandBlastingInstructions"]);
            $sandblastingInstructions=htmlspecialchars_decode($assocTask["SandBlastingInstructions"]);
            
            // Get the amount of lines it takes
            $largestNOL=($this->WordWrap($sandblastingInstructions, 85)==0)?1:$this->WordWrap($sandblastingInstructions, 85);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a sandblasting Label cell
            $this->Cell(50,5*$largestNOL,'Sandblasting:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if it needs sandblasting after maching
            if ($assocTask["IsSandblasted"]==0){
                // Add a sandblasting cell
                $this->Cell(40,5*$largestNOL,'NO',1,0,'C');
            }
            else {
                // Add a sandblasting cell highlighted
                $this->Cell(40,5*$largestNOL,'YES',1,0,'C',true);
            }
            
            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there are any instructions there 
            if ($oldSandblastingInstructions==''){
                // Add a sandblasting cell annotating set to none
                $this->Cell(90,5*$largestNOL,'NONE',1,1,'C');
            }
            else {
                // Get the current X and Y values
                $startingX=$this->GetX();
                $startingY=$this->GetY();

                // Highlight over the places that need it
                $this->highlightLocations($startingX, $startingY, 5, $sandblastingInstructions, '*');

                // Set the x to be at the beginning of the line
                $this->SetX($this->lMargin+93);

                // Add a sandblasting cell with instructions highlighted
                $this->MultiCell(90,5,$sandblastingInstructions,1,'L');

                // Add a Line break
                $this->ln(0);
            }
            
            //*** Data Row 5 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Get post machining notes
            $oldPostMachiningNotes=htmlspecialchars_decode($assocTask["PostMachiningNotes"]);
            $postMachiningNotes=htmlspecialchars_decode($assocTask["PostMachiningNotes"]);
            
            // Get the amount of lines that note takes
            $largestNOL=($this->WordWrap($postMachiningNotes, 125)==0)?1:$this->WordWrap($postMachiningNotes, 125);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a post machining Label cell
            $this->Cell(50,5*$largestNOL,'Post Machining Notes:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there are any post machining notes here 
            if ($oldPostMachiningNotes==''){
                // Add a post machining cell annotating none
                $this->Cell(130,5*$largestNOL,'NONE',1,1,'C');
            }
            else {
                // Get the current X and Y values
                $startingX=$this->GetX();
                $startingY=$this->GetY();

                // Highlight over the places that need it
                $this->highlightLocations($startingX, $startingY, 5, $postMachiningNotes, '*');

                // Set the x to be at the beginning of the line
                $this->SetX($this->lMargin+53);

                // Add a post machining cell with instructions
                $this->MultiCell(130,5,$postMachiningNotes,1,'L');

                // Add a Line break
                $this->ln(0);
            }

            //*** Data Row 6 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Get post machining feedback
            $oldPostMachiningFeedback=htmlspecialchars_decode($assocTask["PostMachiningFeedback"]);
            $postMachiningFeedback=htmlspecialchars_decode($assocTask["PostMachiningFeedback"]);
            
            // Get the amount of lines that note takes
            $largestNOL=($this->WordWrap($postMachiningFeedback, 125)==0)?1:$this->WordWrap($postMachiningFeedback, 125);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a post machining feedback Label cell
            $this->Cell(50,5*$largestNOL,'Post Machining Feedback:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there is any post machining feedback here 
            if ($oldPostMachiningFeedback==''){
                // Add a post machining feedback cell annotating none
                $this->Cell(130,5*$largestNOL,'NONE',1,1,'C');
            }
            else {
                // Get the current X and Y values
                $startingX=$this->GetX();
                $startingY=$this->GetY();

                // Highlight over the places that need it
                $this->highlightLocations($startingX, $startingY, 5, $postMachiningFeedback, '*');

                // Set the x to be at the beginning of the line
                $this->SetX($this->lMargin+53);
    
                // Add a post machining feedback cell with instructions
                $this->MultiCell(130,5,$postMachiningFeedback,1,'L');

                // Add a Line break
                $this->ln(0);
            }
        }
        
        // Create the general task info
        function MachiningSetup($db, $userRow, $assocTask){
            // Working with 180 between margins.  3 from the left side.
            
            // This should start on it's own page
            $this->AddPage();

            // Set the title font to Arial bold 15
            $this->SetFont('Arial','IB',12);

            // Add a title cell with label
            $this->Cell(180,10,'Machining Setup',0,1,'C');

            // Set the draw color to a neutral one
            $this->SetDrawColor(111,111,111);
            
            // Create and add a line
            $this->SetLineWidth(1);
            $this->Line($this->lMargin, $this->GetY(), $this->rMargin,  $this->GetY());
            
            // Add a Line break
            $this->ln(3);
            
            // Set the draw color to black
            $this->SetDrawColor(0);
            
            // Set line width to default
            $this->SetLineWidth(.01);
            
            //*** Data Row 1 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Get a fixture name
            $fixture=htmlspecialchars_decode(($assocTask["FixtureTitle"]>-1)?$assocTask["FixtureTitle"]:'NONE');
            
            // Get the amount of lines that name takes
            $largestNOL=($this->WordWrap($fixture, 50)==0)?1:$this->WordWrap($fixture, 50);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Task ID Label cell
            $this->Cell(35,5*$largestNOL,'Machine:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if the name is NOT null.  If they are printing setup sheets, it should not be.
            if ($assocTask["MachineName"]==null){
                // Set the fill color to red to denote something necessary for the programmer to add
                $this->SetFillColor(255,0,0);
                
                // Set the machine name to none
                $this->Cell(55,5*$largestNOL,'NONE',1,0,'C', true);

                // Set the standard fill color back to yellow for highlighting
                $this->SetFillColor(255,255,0);
            }
            else {
                // Add a machine name cell and highlight it.
                $this->Cell(55,5*$largestNOL,htmlspecialchars_decode($assocTask["MachineName"]),1,0,'C');
            }

            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a completes task Label cell
            $this->Cell(35,5*$largestNOL,'Fixture:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a completes task cell
            $this->MultiCell(55,5,$fixture,1,'C');

            // Add a Line break
            $this->ln(0);
            
            //*** Data Row 2 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Issued Date Label cell
            $this->Cell(35,5,'Programmer:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Issued Date cell
            $this->Cell(55,5,htmlspecialchars_decode($assocTask["ProgrammerFirstName"] . ' ' . $assocTask["ProgrammerLastName"]),1,0,'C');

            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a Due Date Label cell
            $this->Cell(35,5,'Checked By:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Due Date cell
            $this->Cell(55,5,htmlspecialchars_decode($assocTask["ProgramCheckedByFirstName"] . ' ' . $assocTask["ProgramCheckedByLastName"]),1,1,'C');

            //*** Data Row 3 ***//
            
            // Set some variables to be figured out in the loop function and returned
            $allTools='';
            $newTools='';
            $elTools='';
            $differentELTools='';
            $howManyToolsUndefined=0;
            $howManyTotalELTools=0;
            $howManyDifferingELTools=0;
            
            // Figure out the tooling
            $this->ToolingInfo($db, $userRow, 
                               $assocTask["TaskID"], $assocTask["MachineID"],
                               $allTools, $newTools, 
                               $elTools, $differentELTools, 
                               $howManyToolsUndefined,
                               $howManyTotalELTools, $howManyDifferingELTools);
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);

            // Get the highest number of lines
            $largestNOL=($this->WordWrap($allTools, 140)==0)?1:$this->WordWrap($allTools, 140);
            
            // Get the current Y value
            $y=$this->GetY();
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add an all tools Label cell
            $this->Cell(35,5*$largestNOL,'All Tools:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');

            // Determine if there are tools for the job.  Highlight if NONE.
            if ($allTools=='NONE'){
                // Set the fill color to red to denote something necessary for the programmer to add
                $this->SetFillColor(255,0,0);
                
                // Add an all tools cell for none. Need highlighted, because need tools to run parts.
                $this->Cell(145,5,'NONE',1,1,'L', true);

                // Set the standard fill color back to yellow for highlighting
                $this->SetFillColor(255,255,0);
            }
            else {
                        // Get the current X and Y values
                        $startingX=$this->GetX();
                        $startingY=$this->GetY();

                        // Highlight over the places that need it
                        $this->highlightLocations($startingX, $startingY, 5, $allTools, '*');

                        // Set the x to be at the beginning of the line
                        $this->SetX($startingX);
    
                        // Add a hold by cell with instructions
                        $this->MultiCell(145,5,$allTools,1,'L');

                        // Add a Line break
                        $this->ln(0);
            }

            //*** Data Row 4 ***//
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);

            // Get the highest number of lines
            $largestNOL=($this->WordWrap($newTools, 140)==0)?1:$this->WordWrap($newTools, 140);
            
            // Get the current Y value
            $y=$this->GetY();
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a new tools Label cell
            $this->Cell(35,5*$largestNOL,'New Tools:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there are new tools.  Highlight any new ones.
            if ($newTools=='NONE'){
                // Add a new tools cell for none
                $this->Cell(145,5,'NONE',1,1,'L');
            }
            else {
                // Add a new tools cell highlighting the new ones
                $this->MultiCell(145,5,$newTools,1,'L', true);
                
                // Add a Line break
                $this->ln(0); 
            }
            
            //*** Data Row 5 ***//
            
            // Get the highest number of lines between the two
            $firstNOL=($this->WordWrap($elTools, 50)==0)?1:$this->WordWrap($elTools, 50);
            $secondNOL=($this->WordWrap($differentELTools, 50)==0)?1:$this->WordWrap($differentELTools, 50);
            $largestNOL=($firstNOL>$secondNOL)?$firstNOL:$secondNOL;
            
            // Get the current Y value
            $y=$this->GetY();
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add an extended length tools Label cell
            $this->Cell(35,5*$largestNOL,'Using EL Tools:',1,0,'L');

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');

            // Determine if there are tools for the job.
            if ($elTools=='NONE'){
                // Add an extended tools cell for none. 
                $this->Cell(55,5*$largestNOL,'NONE',1,1,'C');
            }
            else {
                // Add an extended as multi cell tools cell normally
                $this->MultiCell(55,5*($largestNOL/$firstNOL),$elTools,1,'C', true);
            }

            // Set the y back to what it started as and the x adjusted appropriately
            $this->SetY($y);
            $this->SetX($this->lMargin+93);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a different el tools Label cell
            $this->Cell(35,5*$largestNOL,'Different EL Tools:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if there are different EL tools.  Highlight any when different.
            if ($differentELTools=='NONE'){
                // Add a different EL tools cell for none
                $this->Cell(55,5*$largestNOL,'NONE',1,1,'C');
            }
            else {
                // Add a new tools cell highlighting the new ones
                $this->MultiCell(55,5*($largestNOL/$secondNOL),$differentELTools,1,'C', true);
                
                // Add a Line break
                $this->ln(0);
            }
            
            //*** Data Row 6 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a total EL tools Label cell
            $this->Cell(35,5,'Using EL Tools #:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a total EL tools cell
            $this->Cell(55,5,$howManyTotalELTools,1,0,'C');

            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a different EL tools Label cell
            $this->Cell(35,5,'Different EL #:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Determine if How many EL tools are different. Highlight any when there are some.
            if ($howManyDifferingELTools==0){
                // Add a different EL tools amount cell for none
                $this->Cell(55,5,'NONE',1,1,'C');
            }
            else {
                // Add a different EL tools amount cell for amount as highlighted
                $this->MultiCell(55,5,$howManyDifferingELTools,1,'C');
                
                // Add a Line break
                $this->ln(0);
            }
            
            //*** Data Row 7 ***//
            
            // Determine if there are tools that are undefined
            if ($howManyToolsUndefined>0){
                // Set the x to be at the beginning of the line
                $this->SetX($this->lMargin+3);

                // Set the font to a normal font bolded
                $this->SetFont('Times','B',11);

                // Add a Hardware Label cell
                $this->Cell(35,5*$largestNOL,'Undefined Tools #:',1);

                // Set the fill color to red to denote something necessary for the programmer to add
                $this->SetFillColor(255,0,0);
                
                // Set the font to a normal font non-bolded
                $this->SetFont('Times','');

                // Add a Hardware cell
                $this->Cell(145,5,'     ' . $howManyToolsUndefined,1,1,'L',true);

                // Set the standard fill color back to yellow for highlighting
                $this->SetFillColor(255,255,0);
            }
            
            //*** Data Row 8 ***//
            
            // Set the x to be at the beginning of the line
            $this->SetX($this->lMargin+3);
            
            // Set the font to a normal font bolded
            $this->SetFont('Times','B',11);
            
            // Add a Issued Date Label cell
            $this->Cell(35,5,'Delivered Amount:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Issued Date cell
            $this->Cell(55,5,htmlspecialchars_decode($assocTask["DeliverableQuantity"]),1,0,'C');

            // Set the font to a normal font bolded
            $this->SetFont('Times','B');
            
            // Add a Due Date Label cell
            $this->Cell(35,5,'Total Amount:',1);

            // Set the font to a normal font non-bolded
            $this->SetFont('Times','');
            
            // Add a Due Date cell
            $this->Cell(55,5,htmlspecialchars_decode($assocTask["TotalQuantity"]),1,1,'C');
        }
        
        // Create the general task info
        function IndividualSetups($db, $userRow, $assocTask){
            // Working with 180 between margins.  3 from the left side.
            
            // Create the query to get the the blanks for this task
            $sql = 'SELECT ' . 
                   'ms.MachiningSetupID, ms.TaskID, ms.SetupOrder, ms.SetupByID, ms.SetupCheckedByID, ' . 
                   'ms.SecondaryMachineID, ms.SecondaryFixtureToUseID, ms.Side, ms.CoordinateSystem, ' . 
                   'ms.hasM00, ms.HoldBy, ms.Orientation, ms.xZeroLocation, ms.yZeroLocation, ms.zZeroLocation, ' . 
                   'ms.M00Instructions, ms.Notes, ms.GCodeFileName, ms.GCodeFileLinesOfCode, ms.GCodeRuntime, ' . 
                   'ms.GCodeFileType, ms.GCodeFileSize, ms.ToolDetails, ms.ToolStart, ms.ToolStartFileName, ' . 
                   'ms.ToolStartFileType, ms.LastModified, ms.ModdedToolDetails, ' .
                   'usb.FirstName AS SetupByFirstName, usb.LastName AS SetupByLastName, ' . 
                   'uscb.FirstName AS SetupCheckedByFirstName, uscb.LastName AS SetupCheckedByLastName ' . 
                   'FROM machiningsetups ms ' .
                   'LEFT JOIN users usb ON ms.SetupByID = usb.UserID ' .
                   'LEFT JOIN users uscb ON ms.SetupCheckedByID = uscb.UserID ' .
                   'WHERE ms.TaskID=' . $assocTask["TaskID"] . ' ' .
                   'ORDER BY ms.SetupOrder;'; 

            // Attempt to get all of the blanks for this task ID
            $taskSetupRows = $db->queryDB($sql, $userRow, "setup_sheet_template Form occurance 5");

            // Determine if there were rows for the task blanks
            if ($taskSetupRows != null){
                // Count the number of results
                $numResults = mysqli_num_rows($taskSetupRows);
                
                // Loop through the returned results
                $amount=0;
                while ($setupRow=mysqli_fetch_array($taskSetupRows)){
                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the title font to Arial bold 15
                    $this->SetFont('Arial','IB',12);

                    // Add a title cell with label
                    $this->Cell(180,10,'Setup Number ' . $setupRow["SetupOrder"],0,1,'C');

                    // Set the draw color to a neutral one
                    $this->SetDrawColor(111,111,111);

                    // Create and add a line
                    $this->SetLineWidth(1);
                    $this->Line($this->lMargin, $this->GetY(), $this->rMargin,  $this->GetY());

                    // Add a Line break
                    $this->ln(3);

                    // Set the draw color to black
                    $this->SetDrawColor(0);

                    // Set line width to default
                    $this->SetLineWidth(.01);

                    //*** Data Row 1 ***//

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a setup by Label cell
                    $this->Cell(30,5,'Setup By:',1);

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a setup by cell
                    $this->Cell(60,5,$setupRow["SetupByFirstName"] . ' ' . $setupRow["SetupByLastName"],1,0,'C');

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B');

                    // Add a completes task Label cell
                    $this->Cell(30,5,'Checked By:',1);

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a setup by cell
                    $this->Cell(60,5,$setupRow["SetupCheckedByFirstName"] . ' ' . $setupRow["SetupCheckedByLastName"],1,1,'C');
                    
                    //*** Data Row 2 ***//

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a side label cell.
                    $this->Cell(15,5,'Side:',1,0,'L');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a side cell.
                    $this->Cell(15,5,$setupRow["Side"],1,0,'C');

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a coordinate system label cell.
                    $this->Cell(35,5,'Coordinate ID:',1,0,'L');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a coordinate system cell.
                    $this->Cell(55,5,htmlspecialchars_decode($setupRow["CoordinateSystem"]),1,0,'C');

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a M00 label cell.
                    $this->Cell(15,5,'M00:',1,0,'L');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Determine if it is has any M00's
                    if ($setupRow["hasM00"]==0){
                        // Add a M00 set to no.
                        $this->Cell(45,5,'NO',1,1,'C');
                    }
                    else {
                        // Add a M00 set to yes.
                        $this->Cell(45,5,'YES',1,1,'C', true);
                    }
                    
                    //*** Data Row 3 ***//

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Get hold by information
                    $oldHoldBy=htmlspecialchars_decode($setupRow["HoldBy"]);
                    $holdBy=htmlspecialchars_decode($setupRow["HoldBy"]);

                    // Get the amount of lines that note takes
                    $largestNOL=($this->WordWrap($holdBy, 145)==0)?1:$this->WordWrap($holdBy, 145);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a hold by Label cell
                    $this->Cell(30,5*$largestNOL,'Hold By:',1);

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Determine if there are any hold by notes here 
                    if ($oldHoldBy==''){
                        // Add a hold by cell annotating none
                        $this->Cell(150,5*$largestNOL,'NONE',1,1,'C');
                    }
                    else {
                        // Get the current X and Y values
                        $startingX=$this->GetX();
                        $startingY=$this->GetY();

                        // Highlight over the places that need it
                        $this->highlightLocations($startingX, $startingY, 5, $holdBy, '*');

                        // Set the x to be at the beginning of the line
                        $this->SetX($this->lMargin+33);
    
                        // Add a hold by cell with instructions
                        $this->MultiCell(150,5,$holdBy,1,'L');

                        // Add a Line break
                        $this->ln(0);
                    }
                    
                    //*** Data Row 4 ***//

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Get orientation information
                    $oldOrientation=htmlspecialchars_decode($setupRow["Orientation"]);
                    $orientation=htmlspecialchars_decode($setupRow["Orientation"]);

                    // Get the amount of lines that note takes
                    $largestNOL=($this->WordWrap($orientation, 145)==0)?1:$this->WordWrap($orientation, 145);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a orientation Label cell
                    $this->Cell(30,5*$largestNOL,'Orientation:',1);

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Determine if there are any orientation notes here 
                    if ($oldOrientation==''){
                        // Add an orientation cell annotating none
                        $this->Cell(150,5*$largestNOL,'NONE',1,1,'C');
                    }
                    else {
                        // Get the current X and Y values
                        $startingX=$this->GetX();
                        $startingY=$this->GetY();

                        // Highlight over the places that need it
                        $this->highlightLocations($startingX, $startingY, 5, $orientation, '*');

                        // Set the x to be at the beginning of the line
                        $this->SetX($this->lMargin+33);
                        
                        // Add an orientation cell with instructions
                        $this->MultiCell(150,5,$orientation,1,'L');

                        // Add a Line break
                        $this->ln(0);
                    }
                    
                    // Data Row 5

                    // Determine if there is a tool start image
                    if ($setupRow["ToolStart"]!=null){
                        // Set the x to be at the beginning of the line plus 35 plus the normal 3
                        $this->SetX($this->lMargin+3);

                        // Set the font to a normal font bolded
                        $this->SetFont('Times','B',11);

                        // Add a Tool Start Label cell
                        $this->Cell(25,9,'Tool Start:');

                        // Set the font to a normal font non-bolded
                        $this->SetFont('Times','');

                        // Download the file into this temp directory
                        file_put_contents('./temp/' . $setupRow["ToolStartFileName"], $setupRow["ToolStart"]);

                        // Determine if this is an image type
                        if (getimagesize('./temp/' . $setupRow["ToolStartFileName"])){
                            // Add image
                            $w=145; $h=0;
                            $this->Image('./temp/' . $setupRow["ToolStartFileName"],$this->lMargin+33,null,$w, $h);
                        }

                        // Remove the file
                        unlink('./temp/' . $setupRow["ToolStartFileName"]);
                   }

                    // Add a dividing line between notes
                    $this->Line($this->lMargin+3, $this->GetY(), $this->rMargin-3,  $this->GetY());

                    // Add a Line break
                    $this->ln(2);
                    
                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the title font to Arial bold 15
                    $this->SetFont('Arial','I',11);

                    // Add a title cell with label
                    $this->Cell(180,10,'Zero Locations',1,1,'C');

                    // Data Row 6

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a X Zero cell.
                    $this->Cell(60,5,'X0',1,0,'C');

                    // Add a Y zero cell.
                    $this->Cell(60,5,'Y0:',1,0,'C');

                    // Add a Z xero cell.
                    $this->Cell(60,5,'Z0:',1,1,'C');

                    // Data Row 7

                    // Create the three texts with carriage returns
                    $x0=htmlspecialchars_decode($setupRow["xZeroLocation"]);            
                    $y0=htmlspecialchars_decode($setupRow["yZeroLocation"]); 
                    $z0=htmlspecialchars_decode($setupRow["zZeroLocation"]); 

                    // Get the highest number of lines between the three
                    $firstNOL=($this->WordWrap($x0, 55)==0)?1:$this->WordWrap($x0, 55);
                    $secondNOL=($this->WordWrap($y0, 55)==0)?1:$this->WordWrap($y0, 55);
                    $thirdNOL=($this->WordWrap($z0, 55)==0)?1:$this->WordWrap($z0, 55);
                    $largestNOL=$thirdNOL>(($firstNOL>$secondNOL)?$firstNOL:$secondNOL)?$thirdNOL:(($firstNOL>$secondNOL)?$firstNOL:$secondNOL);

                    // Get the current Y value
                    $currentDocY=$this->GetY();

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Get the X position
                    $currentDocX=$this->GetX();

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','',11);

                    // Count how many asterics there are
                    $amountOfAsterics=substr_count($x0, '*');
                    
                    // Determine if they want highlighting
                    $highlight=false;
                    if ($amountOfAsterics>1){
                        // highlight the field
                        $highlight=true;
                    }
                    
                    // Add a X Zero label cell.
                    $this->MultiCell(60,5*($largestNOL/$firstNOL),$x0,1,'C', $highlight);

                    // Set the Y Back
                    $this->SetY($currentDocY);

                    // Set the X appropriately
                    $this->SetX($currentDocX+60);

                    // Count how many asterics there are
                    $amountOfAsterics=substr_count($y0, '*');
                    
                    // Determine if they want highlighting
                    $highlight=false;
                    if ($amountOfAsterics>1){
                        // highlight the field
                        $highlight=true;
                    }
                    
                    // Add a Y zero label cell.
                    $this->MultiCell(60,5*($largestNOL/$secondNOL),$y0,1,'C', $highlight);

                    // Set the Y Back
                    $this->SetY($currentDocY);

                    // Set the X appropriately
                    $this->SetX($currentDocX+120);

                    // Count how many asterics there are
                    $amountOfAsterics=substr_count($z0, '*');
                    
                    // Determine if they want highlighting
                    $highlight=false;
                    if ($amountOfAsterics>1){
                        // highlight the field
                        $highlight=true;
                    }
                    
                    // Add a Z xero label cell.
                    $this->MultiCell(60,5*($largestNOL/$thirdNOL),$z0,1,'C', $highlight);

                    // Add a Line break
                    $this->ln(0);

                    //*** Data Row 8 ***//
                    
                    // Add a Line break
                    $this->ln(2);
                    
                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the title font to Arial bold 15
                    $this->SetFont('Arial','I',11);

                    // Add a title cell with label
                    $this->Cell(180,10,'Program Info',1,1,'C');

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a G-Code file name cell.
                    $this->Cell(20,5,'Name:',1,0,'L');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a G-Code file name cell.
                    $this->Cell(40,5,$setupRow["GCodeFileName"],1,0,'C');

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a G-Code lines of code label cell.
                    $this->Cell(20,5,'Lines:',1,0,'L');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a G-Code lines of code cell.
                    $this->Cell(40,5,$setupRow["GCodeFileLinesOfCode"],1,0,'C');

                    // Set the font to a normal font bolded
                    $this->SetFont('Times','B',11);

                    // Add a G-Code run time label cell.
                    $this->Cell(20,5,'Run Time:',1,0,'L');

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a G-Code run time cell.
                    $this->Cell(40,5,$setupRow["GCodeRuntime"],1,1,'C');
                        
                    //*** Data Row 9 ***//
                    
                    // Add a Line break
                    $this->ln(2);
                    
                    // Draw top border
                    $this->Line($this->lMargin+3, $this->GetY(), $this->rMargin-1,  $this->GetY());
                    
                    // Set the top Y
                    $topY=$this->GetY();

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the title font to Arial bold 15
                    $this->SetFont('Arial','I',11);

                    // Add a title cell with label
                    $this->Cell(180,10,'Tool Details',0,1,'C');

                    // Set the x to be at the beginning of the line
                    $this->SetX($this->lMargin+3);

                    // Set the font to a normal font non-bolded
                    $this->SetFont('Times','');

                    // Add a tool details cell.
                    $this->MultiCell(180,5,$setupRow["ModdedToolDetails"],0,'C');
                        
                    // Add a Line break
                    $this->ln(2);
                    
                    // Draw left and right borders
                    $this->Line($this->lMargin+3, $topY, $this->lMargin+3,  $this->GetY());
                    $this->Line($this->rMargin-1, $topY, $this->rMargin-1,  $this->GetY());
                    
                    // Draw bottom border
                    $this->Line($this->lMargin+3, $this->GetY(), $this->rMargin-1,  $this->GetY());
                    
                    // Add a Line break
                    $this->ln(2);

                    // Get the M00 Instructions for this setup
                    $this->Notes($db, $userRow, $setupRow["MachiningSetupID"], "SETUP_M00", 'M00 Instructions', false);

                    // Get the setup notes this setup
                    $this->Notes($db, $userRow, $setupRow["MachiningSetupID"], "SETUP_NOTE", 'Setup Notes', false);

                    // Determine if we are NOT at the last row and increment the counter
                    if (++$amount != $numResults) {
                        // Add a new page for this setup
                        $this->AddPage();
                    }
                }
            }
        }
        
        // Add any pink notes that there are
        function AddPinkNotes($db, $userRow, $copiedFromTaskID){
            // Working with 180 between margins.  3 from the left side.
            
            // Determine if we have a copied from Task ID before anything
            if ($copiedFromTaskID!=null){
                // Write a query to get the previous tasks feedback notes and post machining feedback
                $sql='SELECT t.PostMachiningFeedback, ms.SetupOrder, ms.Side, n.* ' .
                     'FROM tasks t ' .
                     'LEFT JOIN machiningsetups ms ON t.TaskID = ms.TaskID ' .
                     'LEFT JOIN notes n on ms.MachiningSetupID = n.AssociatedCategoryID ' .
                     'WHERE t.TaskID=' . $copiedFromTaskID . ' ' .
                     'AND n.Category="OPERATOR_FEEDBACK_NOTE" ' .
                     'ORDER BY ms.SetupOrder, n.NoteOrder;';

                // Query the database for the pink notes 
                $pinkNoteRows = $db->queryDB($sql, $userRow, "task_presentation Form occurance 21");

                // Determine if there were rows for pink notes
                if ($pinkNoteRows != null && mysqli_num_rows($pinkNoteRows)>0){
                    // Separate from the main page(s)
                    $this->AddPage();

                    // Add a Line break
                    $this->ln(7);

                    // Set the title font to Arial bold 15
                    $this->SetFont('Arial','IB',12);

                    // Set the company color as highlight
                    $this->SetFillColorHex("FF00FF");
            
                    // Add a title cell with label
                    $this->Cell(180,10,'Previous Post Machining Feedback Pink Note Information',0,1,'C',true);

                    // Set the fill color to yellow so they pay attention
                    $this->SetFillColor(255,255,0);
                    
                    // Set the draw color to a neutral one
                    $this->SetDrawColor(111,111,111);

                    // Create and add a line
                    $this->SetLineWidth(1);
                    $this->Line($this->lMargin, $this->GetY(), $this->rMargin,  $this->GetY());

                    // Add a Line break
                    $this->ln(3);

                    // Set the draw color to black
                    $this->SetDrawColor(0);

                    // Set line width to default
                    $this->SetLineWidth(.01);

                    // Loop through the returned results
                    $amount=0;
                    while ($pinkNoteRow=mysqli_fetch_array($pinkNoteRows)){
                        // Increment the amount
                        $amount++;

                        // Determine if we are on the first one and we have a value for the post machining setup feedback
                        if ($amount==1 && $pinkNoteRow["PostMachiningFeedback"]!=null && $pinkNoteRow["PostMachiningFeedback"]!=''){
                            // Determine if there is postmachining feedback notes
                            if ($pinkNoteRow["PostMachiningFeedback"]!=null && $pinkNoteRow["PostMachiningFeedback"]!=''){
                                //*** Data Row Post Machining Feedback ***//

                                // Set the x to be at the beginning of the line
                                $this->SetX($this->lMargin+3);

                                // Get the number of lines in the post machining feedback pink note
                                $largestNOL=($this->WordWrap($pinkNoteRow["Note"], 110)==0)?1:$this->WordWrap($pinkNoteRow["PostMachiningFeedback"], 110);

                                // Set the font to a normal font bolded
                                $this->SetFont('Times','B',11);

                                // Add a post machining feedback pink note Label cell
                                $this->Cell(60,5*$largestNOL,'Note:',1);

                                // Set the font to a normal font non-bolded
                                $this->SetFont('Times','');

                                // Get the current X and Y values
                                $startingX=$this->GetX();
                                $startingY=$this->GetY();

                                // Highlight over the places that need it
                                $this->highlightLocations($startingX, $startingY, 5, $pinkNoteRow["PostMachiningFeedback"], '*');

                                // Set the x to be at the beginning of the line
                                $this->SetX($this->lMargin+63);

                                // Add a post machining feedback pink note
                                $this->MultiCell(120,5,$pinkNoteRow["PostMachiningFeedback"],1,'L');

                                // Add a Line break
                                $this->ln(0);
                            }

                            // Determine if we have a setup feedback note for this first row
                            if ($amount==1 && $pinkNoteRow["SetupOrder"]!=null){
                                // Add a Line break
                                $this->ln(7);

                                // Set the title font to Arial bold 15
                                $this->SetFont('Arial','IB',12);

                                // Set the company color as highlight
                                $this->SetFillColorHex("FF00FF");

                                // Add a title cell with label
                                $this->Cell(180,10,'Setups Operator Feedback Pink Note(s)',0,1,'C',true);

                                // Set the fill color to yellow so they pay attention
                                $this->SetFillColor(255,255,0);

                                // Set the draw color to a neutral one
                                $this->SetDrawColor(111,111,111);

                                // Create and add a line
                                $this->SetLineWidth(1);
                                $this->Line($this->lMargin, $this->GetY(), $this->rMargin,  $this->GetY());

                                // Add a Line break
                                $this->ln(3);

                                // Set the draw color to black
                                $this->SetDrawColor(0);

                                // Set line width to default
                                $this->SetLineWidth(.01);
                            }
                        }

                        // Determine if we have a setup feedback note
                        if ($pinkNoteRow["SetupOrder"]!=null){
                            // Set the pink not title
                            
                            // Add a Line break
                            $this->ln(5);

                            // Set the title font to Arial bold 15
                            $this->SetFont('Times','B',11);

                            // Set the company color as highlight
                            $this->SetFillColorHex("FF00FF");

                            // Add a title cell with label
                            $this->Cell(40,5,'Pink Note Number ' . $amount,0,1,'L',true);

                            // Set the fill color to yellow so they pay attention
                            $this->SetFillColor(255,255,0);

                            //*** Data Row Pink Note Setup Order and Side ***//

                            // Set the x to be at the beginning of the line
                            $this->SetX($this->lMargin+3);

                            // Set the x to be at the beginning of the line
                            $this->SetY($this->GetY()+5);

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a Setup Order Title
                            $this->Cell(35,5,'Setup Order:',1,0,'L');

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Add a Setup Order Value
                            $this->Cell(55,5,$pinkNoteRow["SetupOrder"],1,0,'C');

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a side cell
                            $this->Cell(35,5,'Side:',1,0,'L');

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Add a machine name cell and highlight it.
                            $this->Cell(55,5,$pinkNoteRow["Side"],1,1,'C');

                            //*** Data Row Pink Note Title ***//

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a pink note title label cell
                            $this->Cell(35,5,'Title:',1);

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Add a pink note title cell with the value
                            $this->Cell(145,5,$pinkNoteRow["Title"],1,1,'L');

                            //*** Data Row Pink Notes Note ***//

                            // Get the number of lines in the pink note note
                            $largestNOL=($this->WordWrap($pinkNoteRow["Note"], 145)==0)?1:$this->WordWrap($pinkNoteRow["Note"], 145);

                            // Set the font to a normal font bolded
                            $this->SetFont('Times','B',11);

                            // Add a Pink Note Label cell
                            $this->Cell(35,5*$largestNOL,'Note:',1);

                            // Set the font to a normal font non-bolded
                            $this->SetFont('Times','');

                            // Get the current X and Y values
                            $startingX=$this->GetX();
                            $startingY=$this->GetY();

                            // Highlight over the places that need it
                            $this->highlightLocations($startingX, $startingY, 5, $pinkNoteRow["Note"], '*');

                            // Set the x to be at the beginning of the line
                            $this->SetX($this->lMargin+35);

                            // Add a pink note cell
                            $this->MultiCell(145,5,$pinkNoteRow["Note"],1,'L');

                            // Add a couple Line breaks
                            $this->ln(0);
                        }
                    }
                    
                    // Separate from the main page(s)
                    $this->AddPage();
                }
            }
        }
    }
?>