<?php
    function checkThenGetNotesDiv($db, $userRow, $inAssociatedCategoryID, $inCategoryType, &$validationHTML=""){
        // Init all of the settings to the appropriate values
        $noteDiv = '';
        
        // Determine if the user is of the main company and appropriate level
        if ($userRow["CompanyID"]==1 &&
                ($userRow["UserLevel"]=="SU" ||
                 $userRow["UserLevel"]=="EXECUTIVE" ||
                 $userRow["UserLevel"]=="MANAGEMENT" ||
                 $userRow["UserLevel"]=="MID-MANAGEMENT")
            )
            {
                // get the div
                $noteDiv = retrieveNotesDiv($db, $userRow, $inAssociatedCategoryID, $inCategoryType, true, $validationHTML);
            }

        // Return the result
        return $noteDiv;
    }
    
    function getTool($db, $userRow, $toolNumber, $category){
        // Init all of the settings to the appropriate values
        $toolID = '';
        $toolTypeID = -1;
        $brand = 'Not Entered';
        $cuttingLength = 0;
        $diameter = 0;
        $fluteNumber = 0;
        $partNumber = 'Not Entered';
        $shankLength = 0;
        $totalLength = 0;
        $shoulderLength = 0;
        $angle = 0;
        $radius = 0;
        $taperAngle = 0;
        $shankDiameter = 0;
        $amount = 0;
        $lastReorderedDate = '';
        $averageLeadTimeInDays = 0;
        $notes = '';
        
        $checkFrequencyInDays = 7;
        $lowReorderLevel = 0;
        $highReorderLevel = 0;
        $lastAutoCheckedDate = null;
        $reorderNotificationSentDate = null;
        $vendor = '';

        $modifyType = 'add';

        // Determine if this is an update or not
        $toolAssocRow = null;
        if ($toolNumber){
            // Determine if the user editing is in the super company and of correct privileges
            if (
                    $userRow["CompanyID"]!=1 &&
                    ($userRow["UselLevel"]=="SU" ||
                     $userRow["UselLevel"]=="EXECUTIVE" ||
                     $userRow["UselLevel"]=="MANAGEMENT"
                    )
               ){
                // We should not even be in here if we are not a SU
                return;
            }

            // Create the query to get the the piece of tool
            $sql = 'SELECT * FROM tools WHERE ToolID = ' . $toolNumber . ';';

            // Attempt to get the tool row
            $toolRow = $db->queryDB($sql, $userRow, "tool_presentation Form occurance 1");

            // Attempt to get the tool row
            $toolAssocRow = $toolRow->fetch_assoc();

            // Set the things we can
            $toolID = $toolAssocRow["ToolID"];
            $toolTypeID = $toolAssocRow["ToolTypeID"];
            $name = $toolAssocRow["Name"];
            $brand = $toolAssocRow["Brand"];
            $cuttingLength = $toolAssocRow["CuttingLength"];
            $diameter = $toolAssocRow["Diameter"];
            $fluteNumber = $toolAssocRow["FluteNumber"];
            $partNumber = $toolAssocRow["PartNumber"];
            $shankLength = $toolAssocRow["ShankLength"];
            $totalLength = $toolAssocRow["TotalLength"];
            $shoulderLength = $toolAssocRow["ShoulderLength"];
            $angle = $toolAssocRow["Angle"];
            $radius = $toolAssocRow["Radius"];
            $taperAngle = $toolAssocRow["TaperAngle"];
            $shankDiameter = $toolAssocRow["ShankDiameter"];
            $amount = $toolAssocRow["Amount"];
            $lastReorderedDate = $toolAssocRow["LastReorderedDate"];
            $averageLeadTimeInDays = $toolAssocRow["AverageLeadTimeInDays"];
            $notes = $toolAssocRow["Notes"];
            
            $checkFrequencyInDays = $toolAssocRow["CheckFrequencyInDays"];
            $lowReorderLevel = $toolAssocRow["LowReorderLevel"];
            $highReorderLevel = $toolAssocRow["HighReorderLevel"];
            $lastAutoCheckedDate = $toolAssocRow["LastAutoCheckedDate"];
            $reorderNotificationSentDate = $toolAssocRow["ReorderNotificationSentDate"];
            $vendor = $toolAssocRow["VendorID"];

            $modifyType = 'update';
        }

        // Set the blanks validation script to nothing at first
        $noteValidationHTML="";
        
        return '
            <form action="' . $modifyType . 'Tool.php?c=' . $category . '&toid=' . $toolID . '" method="post" name="' . $modifyType . 'Tool" enctype="multipart/form-data">  
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr align="left" valign="top" width="100%">
                        <td style="width:48%;">
                            <table width="100%" border="0" cellpadding="0" cellspacing="0" class="usial">
                                <tr>
                                    <td height="39" colspan=1 width="55%" align="right" valign="middle">
                                        <strong>Type:</strong>&nbsp; &nbsp; 
                                    </td>
                                    <td height="39" colspan=1 align="left" valign="middle">' 
                                        . getToolTypeHTML($db, $userRow, 'toolTypeID', $toolTypeID, $category, $onChangeHTML, 0, "width: 95%;") .
                                    '</td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right" valign="middle">
                                        <strong>Name:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                        <input name="name" type="text" maxlength="100" class="intext" style="width: 95%; height: 19px" value="' . $name . '">
                                        <input type="hidden" name="toolID" value="' . $toolID . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                        <strong>Brand:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                        <input name="brand" type="text" maxlength="25" class="intext" style="width: 95%; height: 19px" value="' . $brand . '">
                                  </td>
                                </tr>
                                <tr>
                                    <td height="39" colspan=1 align="right"  valign="middle">
                                        <strong>Part Number:</strong>&nbsp; &nbsp; 
                                    </td>
                                    <td height="39" colspan=1 align="left"  valign="middle">
                                        <input name="partNumber" type="text" maxlength="20" class="intext" style="width: 95%; height: 19px" value="' . $partNumber . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td height="39" colspan=1 align="right"  valign="middle">
                                        <strong>Diameter:</strong>&nbsp; &nbsp; 
                                    </td>
                                    <td height="39" colspan=1 align="left"  valign="middle">
                                        <input name="diameter" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $diameter . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td height="39" colspan=1 align="right"  valign="middle">
                                        <strong>Cutting Length:</strong>&nbsp; &nbsp; 
                                    </td>
                                    <td height="39" colspan=1 align="left"  valign="middle">
                                        <input name="cuttingLength" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $cuttingLength . '">
                                    </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Number of Flutes:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="fluteNumber" type="text" maxlength="2" class="intext" style="width: 95%; height: 19px" value="' . $fluteNumber . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Shank Length:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="shankLength" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $shankLength . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Total Length:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="totalLength" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $totalLength . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Shoulder Length:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="shoulderLength" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $shoulderLength . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Angle:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="angle" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $angle . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Radius:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="radius" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $radius . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Taper Angle:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="taperAngle" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $taperAngle . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td height="39" colspan=1 align="right"  valign="middle">
                                    <strong>Shank Diameter:</strong>&nbsp; &nbsp; 
                                  </td>
                                  <td height="39" colspan=1 align="left"  valign="middle">
                                    <input name="shankDiameter" type="text" maxlength="17" class="intext" style="width: 95%; height: 19px" value="' . $shankDiameter . '">
                                  </td>
                                </tr>
                            </table>
                        </td>
                        <td style="width:4%;">&nbsp;</td>
                        <td style="width:48%;">
                            <table width="100%" border="0" cellpadding="0" cellspacing="0" class="usial">
                                <tr>
                                    <td colspan=5 height="39" align="center" valign="middle">
                                        <strong>Reorder Information: </strong> <br>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Current Amount:</strong>&nbsp;&nbsp; 
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <input id="amount" name="amount" type="text" maxlength="11" class="intext" value="' . $amount . '" style="height: 19px; width: 95%;">
                                    </td>
                                </tr>
                                <tr style="outline: thin solid">
                                    <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Reorder At:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=1 height="39" align="left" valign="middle">
                                        <input name="lowReorderLevel" type="text" maxlength="11" class="intext" value="' . $lowReorderLevel . '" style="height: 19px; width: 50px;">
                                    </td>
                                    <td colspan=1 height="39" align="center" valign="middle">
                                        <strong> - </strong>
                                    </td>
                                    <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Reorder To:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=1 height="39" align="left" valign="middle">
                                        <input name="highReorderLevel" type="text" maxlength="11" class="intext" value="' . $highReorderLevel . '" style="height: 19px; width: 50px;">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=5 height="39" align="top" valign="center">
                                        Set both to zero to disable auto-reordering.
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Last Reorder Date:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <input name="lastReorderedDate" id="lastReorderedDate" type="date" class="intext" value="' . $lastReorderedDate . '" style="height: 19px; width: 65%;">
                                        <button type="button" onClick="javascript:populateCurrentDateTime(\'lastReorderedDate\',\'yyyy-MM-dd\');" align="right">Now</button>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 width="52%" height="39" align="right" valign="middle">
                                        <strong>Reorder Notification Date:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <input name="reorderNotificationSentDate" type="date" class="intext" value="' . $reorderNotificationSentDate . '" disabled style="height: 19px; width: 95%;">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Avg Lead Time (Days):</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <input name="averageLeadTimeInDays" type="text" maxlength="11" class="intext" value="' . $averageLeadTimeInDays . '" style="height: 19px; width: 95%;">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Check Frequency (Days):</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <input name="checkFrequencyInDays" type="text" maxlength="17" class="intext" value="' . $checkFrequencyInDays . '" style="height: 19px; width: 95%;">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Last Auto Check Date:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <input name="lastAutoCheckedDate" type="date" class="intext" value="' . $lastAutoCheckedDate . '" disabled style="height: 19px; width: 95%;">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                          <strong>Vendor: </strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                    ' . 
                                    getVendorAsSelectOptions($db, $userRow, $vendor, "", "", "", "width:85%;")
                                    . '
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=5>
                                        <strong>Main Notes (1000 characters max):</strong><br> 
                                        <textarea name="mainNotes" id="mainNotes" maxlength="1000" class="intext" style="width: 95%; height: 130px; overflow: auto; resize:vertical">' .
                                            $notes .
                                        '</textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                ' 
                .
                checkThenGetNotesDiv($db, $userRow, $toolID, 'TOOL', $noteValidationHTML)
                .
                '
                <p align="right">
                    <input type="hidden" id="formSubmitted" name="formSubmitted" value="TRUE">
                    <input id="formSubmit" type="submit" style="cursor: hand; cursor: pointer;" value="' . $modifyType . ' Tool" />
                </p>
            </form>
            <script language="JavaScript">
                // Code for validating the form
                // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
                // for details
                var frmvalidator = new Validator("' . $modifyType . 'Tool", "formSubmit");
                frmvalidator.addValidation("name",
                                           "required",
                                           "Please enter the name of this tool.");
                frmvalidator.addValidation("diameter",
                                           "dec",
                                           "Please enter the diameter of this tool as a number.");
                frmvalidator.addValidation("diameter",
                                           "greaterthan=-1",
                                           "The diameter should be greater than or equal to zero.");
                frmvalidator.addValidation("cuttingLength",
                                           "dec",
                                           "Please enter the cutting length of this tool as a number.");
                frmvalidator.addValidation("cuttingLength",
                                           "greaterthan=-1",
                                           "The cutting length should be greater than or equal to zero.");
                frmvalidator.addValidation("fluteNumber",
                                           "num",
                                           "Please enter the Number of flutes of this tool as a number.");
                frmvalidator.addValidation("fluteNumber",
                                           "greaterthan=-1",
                                           "The Number of Flutes should be greater than or equal to zero.");
                frmvalidator.addValidation("shankLength",
                                           "dec",
                                           "Please enter the Shank Length of this tool as a number.");
                frmvalidator.addValidation("shankLength",
                                           "greaterthan=-1",
                                           "The Shank Length should be greater than or equal to zero.");
                frmvalidator.addValidation("totalLength",
                                           "dec",
                                           "Please enter the Total Length of this tool as a number.");
                frmvalidator.addValidation("totalLength",
                                           "greaterthan=-1",
                                           "The Total Length should be greater than or equal to zero.");
                frmvalidator.addValidation("shoulderLength",
                                           "dec",
                                           "Please enter the Shoulder Length of this tool as a number.");
                frmvalidator.addValidation("shoulderLength",
                                           "greaterthan=-1",
                                           "The Shoulder Length should be greater than or equal to zero.");
                frmvalidator.addValidation("angle",
                                           "dec",
                                           "Please enter the Angle of this tool as a number.");
                frmvalidator.addValidation("angle",
                                           "greaterthan=-1",
                                           "The Angle should be greater than or equal to zero.");
                frmvalidator.addValidation("radius",
                                           "dec",
                                           "Please enter the Radius of this tool as a number.");
                frmvalidator.addValidation("radius",
                                           "greaterthan=-1",
                                           "The Radius should be greater than or equal to zero.");
                frmvalidator.addValidation("taperAngle",
                                           "dec",
                                           "Please enter the Taper Angle of this tool as a number.");
                frmvalidator.addValidation("taperAngle",
                                           "greaterthan=-1",
                                           "The Taper Angle should be greater than or equal to zero.");
                frmvalidator.addValidation("shankDiameter",
                                           "dec",
                                           "Please enter the Shank Diameter of this tool as a number.");
                frmvalidator.addValidation("shankDiameter",
                                           "greaterthan=-1",
                                           "The Shank Diameter should be greater than or equal to zero.");
                frmvalidator.addValidation("amount",
                                           "num",
                                           "Please enter the amount of this material stock as a number.");
                frmvalidator.addValidation("amount",
                                           "greaterthan=-1",
                                           "The amount should be greater than or equal to zero.");
                frmvalidator.addValidation("averageLeadTimeInDays",
                                           "num",
                                           "Please enter the average lead time of this material stock as a number.");
                frmvalidator.addValidation("averageLeadTimeInDays",
                                           "greaterthan=-1",
                                           "The average lead time should be greater than or equal to zero.");
                frmvalidator.addValidation("checkFrequencyInDays",
                                           "num",
                                           "Please enter the check frequency of this material stock as a number.");
                frmvalidator.addValidation("checkFrequencyInDays",
                                           "greaterthan=-1",
                                           "The check frequency should be greater than or equal to zero.");
                frmvalidator.addValidation("lowReorderLevel",
                                           "num",
                                           "Please enter the low reorder level of this material stock as a number.");
                frmvalidator.addValidation("lowReorderLevel",
                                           "greaterthan=-1",
                                           "The low reorder level should be greater than or equal to zero.");
                frmvalidator.addValidation("highReorderLevel",
                                           "num",
                                           "Please enter the high reorder level of this material stock as a number.");
                frmvalidator.addValidation("highReorderLevel",
                                           "greaterthan=-1",
                                           "The high reorder level should be greater than or equal to zero.");
                '
                .
                $noteValidationHTML
                . 
                '
            </script>';
    }

    function getToolType($db, $userRow, $toolTypeNumber){
        // Init all of the settings to the appropriate values
        $toolTypeName = '';
        $toolTypeColor = 'a6a6a6';

        $modifyType = 'add';

        // Determine if this is an update or not
        $toolTypeAssocTask = null;
        if ($toolTypeNumber){
            // Determine if the user editing this is NOT an employee of the SU company
            if ($userRow["CompanyID"] != 1){
                // We should not even be in here if we are not in this company
                return;
            }
            
            // Create the query to get the the tool type
            $sql = 'SELECT * FROM tooltype WHERE ToolTypeID = ' . $toolTypeNumber . ';';

            // Attempt to get the companies
            $toolTypeRow = $db->queryDB($sql, $userRow, "tool_presentation Form occurance 1");

            // Attempt to get the tool type row
            $toolTypeAssocTask = $toolTypeRow->fetch_assoc();

            // Set the things we can
            $toolTypeName = $toolTypeAssocTask["Name"];
            $toolTypeColor = $toolTypeAssocTask["RowColor"];
            $category = $toolTypeAssocTask["CategoryName"];

            $modifyType = 'update';
        }

        return '
            <form action="' . $modifyType . 'ToolType.php" method="post" name="' . $modifyType . 'ToolType" enctype="multipart/form-data">  
                <input type="hidden" id="updateToolTypeID" name="updateToolTypeID" value="' . $toolTypeNumber . '">
                <table width=100%>
                    <tr width=100%>
                        <td colspan=2>
                            <strong>Tool Type Name: </strong>
                            <input name="toolTypeName" id="toolTypeName" type="text" maxlength="150" class="intext" style="width: 100%; height: 19px" value="' . $toolTypeName . '">
                        </td>
                    </tr>
                    <tr width=100%>
                        <td>
                            <br>
                            <strong>Tool Type Color: </strong>
                            <script type="text/javascript">        
                              $(document).ready(
                                function()
                                {
                                  $("#toolTypeRowColor").jPicker({window:{position:{x:"center",y:"center"},expandable: true,liveUpdate: true}});
                                });
                            </script>
                            <input id="toolTypeRowColor" name="toolTypeRowColor" type="text" maxlength="6" value="' . $toolTypeColor . '" style="width: 60px; height: 20px; overflow: auto; resize:none"/>
                        </td>
                        <td>
                            <br>
                            <strong>Tool Category: </strong> ' . 
                            $db->getTypeAsSelectOptions('tooltype', 'CategoryName', 'categoryName', $category, '', '', false, '') . '
                        </td>
                    </tr>
                </table>
                <p align="right">
                    <input type="hidden" id="formSubmitted" name="formSubmitted" value="TRUE">
                    <input id="formSubmit" type="submit" style="cursor: hand; cursor: pointer;" value="' . $modifyType . ' Tool Type" />
                </p>
            </form>
            <script language="JavaScript">
                // Code for validating the form
                // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
                // for details
                var frmvalidator = new Validator("' . $modifyType . 'ToolType", "formSubmit");
                frmvalidator.addValidation("toolTypeName",
                                           "required",
                                           "Please enter the name of this tool type.");
           </script>';
    }
    
    function retrieveToolTypesAsList($db, $userRow){
        // Initialize the return
        $result = 
            '<li class="ui-state-default ui-state-disabled Table">
                <div class="Heading">
                    <div class="cell toolTypeNameCell">
                        <p><b>Tool Type Name</b></p>
                    </div>
                    <div class="cell updateCell">
                        <p><b>Edit</b></p>
                    </div>
                </div>
            </li>';

        try {
            // Setup the query to be all tool types
            $sql='SELECT tt.*, t.AmountOnTools   
                  FROM tooltype tt  
                  LEFT OUTER JOIN 
                       (SELECT COUNT(*) AS AmountOnTools, ToolTypeID 
                        FROM tools GROUP BY ToolTypeID) t ON tt.ToolTypeID = t.ToolTypeID 
                  ORDER BY tt.Name;';

            // Query the database for the tool types 
            $rows = $db->queryDB($sql, $userRow, "tool_presentation Form occurance 1");

            // Loop through the returned results
            while ($row=mysqli_fetch_array($rows)){
                // Assign the delete button attributes as though deletion is an option
                $deleteButtonTextColor = 'LIMEGREEN';
                $deleteButtonTitle = 'Remove this tool type.';
                $deleteButtonOnClick = 'onclick="javascript:if (confirm(\'Are you sure you want to delete this tool type?\')) ' .
                                       '{window.location=\'redirectors/delete_tooltype.php?deleteToolTypeID=' . $row["ToolTypeID"] . '\';}"';

                // Determine if this company is assigned to any tasks
                if ($row["AmountOnTools"]!=null){
                    // Assign the delete buttons attributes such that deletion will not happen
                    $deleteButtonTextColor = 'RED';
                    $deleteButtonTitle = 'Check what tool or miscellaneous hardware this tool type may be assigned to.';
                    $deleteButtonOnClick = 'onclick="javascript:alert(\'This tool type is assigned to '. $row["AmountOnTools"] . 
                                           ' tools/miscellaneous hardware.\n\nRemove tool type from tools or miscellaneous before ' . 
                                           'attempting to delete again.\');"';
                }

                // Create a default html for the button to edit the tool type
                $toolTypeEditButtonHTML = '<input type="button" style="font-size:9px;" value="..." title="Edit this tool type." onclick="javascript:window.location=\'updateToolType.php?updateToolTypeID=' . $row["ToolTypeID"] . '\';"/>';

                // Determine what color this row should be
                $color='#' . $row["RowColor"];

                // Add this option to the select
                $order=1;
                $result = $result .
                    '<li id="ToolType_' . $order . '" class="ui-state-default Table">
                        <div class="Row" style="background-color: '. $color . ';">
                            <div class="cell toolTypeNameCell">
                                <span>
                                    <p>' . $row["Name"] . '</p>
                                </span>
                            </div>
                            <div class="cell updateCell">
                                <span>
                                    <table>
                                        <tr>
                                            <td>
                                                <input type="button" style="font-size:11px; color:' . $deleteButtonTextColor . ';" value="X" title="' . 
                                                $deleteButtonTitle . '" ' . $deleteButtonOnClick . '/>
                                            </td>
                                            <td>
                                            '
                                            .
                                            $toolTypeEditButtonHTML
                                            .
                                            '
                                            </td>
                                        </tr>
                                    </table>
                                </span>
                            </div>
                        </div>
                    </li>';
            }

        } catch (Exception $ex) {
            // Assign the output string as to what happened
            return $result;
        }

        // Return whatever we got
        return $result;
    }    
?>
