<?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 getMaterialHTML($db, $userRow, $selected, $onChangeHTML){
        // Initialize the return
        $result = '';

        try {
            // Init the material name
            $materialSelected=1;
            
            // If the $selected variable is -1, then this is in an add page.  
            if ($selected!=-1){
                // Set the selected material
                $materialSelected=$selected;
            }
            
            // Create the query to run
            $sql='SELECT * FROM material ORDER BY LOWER(Category), LOWER(Name);';

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

            // Ensure we got rows back.
            if ($rows == null){
                return $result;
            }
            else{
                // Loop through the returned results
                while ($row=mysqli_fetch_array($rows)){
                    // Determine if this is one that is to be selected
                    $selectedText='';
                    if ($materialSelected != null and $materialSelected==$row["MaterialID"]) {
                        // Create the selected html
                        $selectedText='selected';
                    }

                    // Add this option to the select
                    $result=$result . '<option value="' . $row["MaterialID"] . '" ' . $selectedText . '>' . $row["Category"] . ' - ' .$row["Name"] . '</option>';
                }

                // End the select
                $result=$result . '</select>';
                
                // Start the return with the beginning of the select
                $result= '<select name="materialID" id="materialID"  onchange="' . $onChangeHTML . '">' . $result;
            }
        } catch (Exception $ex) {
            // Assign the output string as to what happened
            return $result;
        }

        // Return whatever we got
        return $result;
    }

    function getMaterialStock($db, $userRow, $materialStockNumber){
        // Init all of the settings to the appropriate values
        $machineID = '';
        $materialID = -1;
        $name = '';
        $location = '';
        $type = 'Extrusion';
        $dimension1 = 0;
        $dimension2 = 0;
        $dimension3 = 0;
        $diameter = 0;
        $wallThickness = 0;
        $lowWarpValue = 0;
        $highWarpValue = 0;
        $amount = 0;
        $units = 'INCHES';
        $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
        $materialStockAssocRow = null;
        if ($materialStockNumber){
            // 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" ||
                     $userRow["UselLevel"]=="MID-MANAGEMENT"
                    )
               ){
                // We should not even be in here if we are not a of the right level
                return;
            }

            // Create the query to get the the machine stock
            $sql = 'SELECT * FROM materialstock WHERE MaterialStockID = ' . $materialStockNumber . ';';

            // Attempt to get the materialStock row
            $materialStockRow = $db->queryDB($sql, $userRow, "material_stock_presentation Form occurance 2");

            // Attempt to get the materialStock row
            $materialStockAssocRow = $materialStockRow->fetch_assoc();

            // Set the things we can
            $materialStockID = $materialStockAssocRow["MaterialStockID"];
            $materialID = $materialStockAssocRow["MaterialID"];
            $name = $materialStockAssocRow["Name"];
            $location = $materialStockAssocRow["Location"];
            $type = $materialStockAssocRow["Type"];
            $dimension1 = $materialStockAssocRow["Dimension1"];
            $dimension2 = $materialStockAssocRow["Dimension2"];
            $dimension3 = $materialStockAssocRow["Dimension3"];
            $diameter = $materialStockAssocRow["Diameter"];
            $wallThickness = $materialStockAssocRow["WallThickness"];
            $lowWarpValue = $materialStockAssocRow["LowWarpValue"];
            $highWarpValue = $materialStockAssocRow["HighWarpValue"];
            $amount = $materialStockAssocRow["Amount"];
            $units = $materialStockAssocRow["Units"];
            $lastReorderedDate = $materialStockAssocRow["LastReorderedDate"];
            $averageLeadTimeInDays = $materialStockAssocRow["AverageLeadTimeInDays"];
            $notes = $materialStockAssocRow["Notes"];
            
            $checkFrequencyInDays = $materialStockAssocRow["CheckFrequencyInDays"];
            $lowReorderLevel = $materialStockAssocRow["LowReorderLevel"];
            $highReorderLevel = $materialStockAssocRow["HighReorderLevel"];
            $lastAutoCheckedDate = $materialStockAssocRow["LastAutoCheckedDate"];
            $reorderNotificationSentDate = $materialStockAssocRow["ReorderNotificationSentDate"];
            $vendor = $materialStockAssocRow["VendorID"];
            
            $modifyType = 'update';
        }

        // Set the blanks validation script to nothing at first
        $noteValidationHTML="";
                
        return '
            <form action="' . $modifyType . 'StockedMaterial.php?updateStockedMaterialID=' . $materialStockID . '" method="post" name="' . $modifyType . 'StockedMaterial" enctype="multipart/form-data">  
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr align="left" valign="top">
                        <td width="48%">
                            <table width="100%" border="0" cellpadding="0" cellspacing="0" class="usial">
                                <tr>
                                  <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Name:</strong>&nbsp;&nbsp;
                                  </td>
                                  <td colspan=2 height="39" align="left" valign="middle">
                                        <input name="name" type="text" maxlength="100" class="intext" style="width: 100%; height: 19px" value="' . $name . '">
                                        <input type="hidden" name="materialStockID" value="' . $materialStockID . '">
                                        <input type="hidden" name="materialID" value="' . $materialID . '">
                                  </td>
                                </tr>
                                <tr>
                                  <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Location:</strong>&nbsp;&nbsp;
                                  </td>
                                  <td colspan=2 height="39" align="left" valign="middle">
                                        <input name="location" id="location" type="text" maxlength="100" class="intext" style="width: 100%; height: 19px" value="' . $location . '">
                                  </td>
                                </tr>
                                <tr>
                                    <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Type:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=2 height="39" align="left" valign="middle">
                                        ' . $db->getTypeAsSelectOptions('materialstock', 'Type', 'type', $type, null, '', false, '') . '
                                    </td>
                                </tr>
                                <tr width=100%>
                                    <td colspan=1 height="39" align="right" valign="middle">
                                       <strong>Category:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=2 height="39" align="left" valign="middle">'
                                       . getMaterialHTML($db, $userRow, $materialID, $onChangeHTML) . '
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Amount:</strong>&nbsp;&nbsp; 
                                    </td>
                                    <td colspan=1 height="39" align="left" valign="middle">
                                        <input id="amount" name="amount" type="text" maxlength="11" class="intext" style="width: 100%; height: 19px" value="' . $amount . '">
                                    </td>
                                    <td colspan=1 height="39" align="left" valign="middle">
                                        ' . $db->getTypeAsSelectOptions('materialstock', 'Units', 'units', $units, null, '', false, '') . '
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Wall Thickness:</strong>&nbsp;&nbsp; 
                                    </td>
                                    <td colspan=2 height="39" align="left" valign="middle">
                                        <input name="wallThickness" type="text" maxlength="15" class="intext" style="width: 100%; height: 19px" value="' . $wallThickness . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Diameter:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=2 height="39" align="left" valign="middle">
                                        <input name="diameter" type="text" maxlength="15" class="intext" style="width: 100%; height: 19px" value="' . $diameter . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <strong>Warp Values: </strong> <br>
                                    </td>
                                </tr>
                                <tr style="outline: thin solid">
                                    <td colspan=1 height="39" align="right" valign="middle">
                                        <strong>Low: </strong>
                                        <input name="lowWarpValue" type="text" maxlength="15" class="intext" style="width: 50%; height: 19px" value="' . $lowWarpValue . '">
                                    </td>
                                    <td colspan=1 height="39" align="center" valign="middle">
                                        <strong>TO</strong>
                                    </td>
                                    <td colspan=1 height="39" align="left" valign="middle">
                                        <strong>High: </strong>
                                        <input name="highWarpValue" type="text" maxlength="15" class="intext" style="width: 50%; height: 19px" value="' . $highWarpValue . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <strong>Dimensions: </strong> <br>
                                    </td>
                                </tr>
                                <tr style="outline: thin solid">
                                    <td colspan=1 height="39" align="center" valign="middle">
                                        <strong>1: </strong>
                                        <input name="dimension1" type="text" maxlength="15" class="intext" style="width: 50%; height: 19px" value="' . $dimension1 . '">
                                    </td>
                                    <td colspan=1 height="39" align="center" valign="middle">
                                        <strong>2: </strong>
                                        <input name="dimension2" type="text" maxlength="15" class="intext" style="width: 50%; height: 19px" value="' . $dimension2 . '">
                                    </td>
                                    <td colspan=1 height="39" align="center" valign="middle">
                                        <strong>3: </strong>
                                        <input name="dimension3" type="text" maxlength="15" class="intext" style="width: 50%; height: 19px" value="' . $dimension3 . '">
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td width="4%">&nbsp;</td>
                        <td width="48%">
                            <table width="100%" border="0" cellpadding="0" cellspacing="0" class="usial">
                                <tr>
                                    <td colspan=6 height="39" align="center" valign="middle">
                                        <strong>Reorder Information:</strong> <br>
                                    </td>
                                </tr>
                                <tr style="outline: thin solid">
                                    <td colspan=2 height="39" align="center" valign="middle">
                                        <strong>Reorder At: </strong> <br>
                                        <input name="lowReorderLevel" type="text" maxlength="11" class="intext" style="width: 50%; height: 19px" value="' . $lowReorderLevel . '">
                                    </td>
                                    <td colspan=2 height="39" align="center" valign="middle">
                                        <strong> - </strong>
                                    </td>
                                    <td colspan=1 height="39" align="right" width="25%" valign="middle">
                                        <strong>Reorder To:</strong><br>
                                    </td>
                                    <td colspan=1 height="39" align="left" valign="middle">
                                        <input name="highReorderLevel" type="text" maxlength="11" class="intext" style="width: 95%; height: 19px" value="' . $highReorderLevel . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=6 height="39" align="center" valign="middle">
                                        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=4 height="39" align="left" valign="middle">
                                        <input name="lastReorderedDate" id="lastReorderedDate" type="date" class="intext" style="width: 70%;" value="' . $lastReorderedDate . '">
                                        <button type="button" onClick="javascript:populateCurrentDateTime(\'lastReorderedDate\',\'yyyy-MM-dd\');" align="right">Now</button>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" width="55%" align="right" valign="middle">
                                        <strong>Reorder Notification Date:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                        <input name="reorderNotificationSentDate" type="date" class="intext" style="width: 100%;" value="' . $reorderNotificationSentDate . '" disabled>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan= height="39" align="right" valign="middle">
                                        <strong>Avg Lead Time (Days):</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                        <input name="averageLeadTimeInDays" type="text" maxlength="11" class="intext" style="width: 100%; height: 19px" value="' . $averageLeadTimeInDays . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan= height="39" align="right" valign="middle">
                                        <strong>Check Frequency (Days):</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                        <input name="checkFrequencyInDays" type="text" maxlength="11" class="intext" style="width: 100%; height: 19px" value="' . $checkFrequencyInDays . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Last Auto Check Date:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                        <input name="lastAutoCheckedDate" type="date" class="intext" style="width: 100%;" value="' . $lastAutoCheckedDate . '" disabled>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                          <strong>Vendor:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                    ' . 
                                    getVendorAsSelectOptions($db, $userRow, $vendor, "", "")
                                    . '
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=6 width="100%">
                                        <strong>Main Notes (1000 characters max):</strong><br> 
                                        <textarea name="permanentNotes" id="permanentNotes" maxlength="1000" class="intext" style="width: 100%; height: 80px; overflow: auto; resize:vertical;">' .
                                            $notes .
                                        '</textarea>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                ' 
                .
                checkThenGetNotesDiv($db, $userRow, $materialStockNumber, 'MATERIAL', $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 . ' Stocked Material" />
                </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 . 'StockedMaterial", "formSubmit");
                frmvalidator.addValidation("name",
                                           "required",
                                           "There must be a value for the the material stock name.");
                frmvalidator.addValidation("dimension1",
                                           "dec",
                                           "Please enter dimension 1 of this material stock as a decimal.");
                frmvalidator.addValidation("dimension1",
                                           "greaterthan=-0.000001",
                                           "Dimension 1 should be greater than or equal to zero.");
                frmvalidator.addValidation("dimension2",
                                           "dec",
                                           "Please enter dimension 2 of this material stock as a decimal.");
                frmvalidator.addValidation("dimension2",
                                           "greaterthan=-0.000001",
                                           "Dimension 2 should be greater than or equal to zero.");
                frmvalidator.addValidation("dimension3",
                                           "dec",
                                           "Please enter dimension 3 of this material stock as a decimal.");
                frmvalidator.addValidation("dimension3",
                                           "greaterthan=-0.000001",
                                           "Dimension 3 should be greater than or equal to zero.");
                frmvalidator.addValidation("diameter",
                                           "dec",
                                           "Please enter the diameter of this material stock as a decimal.");
                frmvalidator.addValidation("diameter",
                                           "greaterthan=-0.000001",
                                           "The dimension should be greater than or equal to zero.");
                frmvalidator.addValidation("wallThickness",
                                           "dec",
                                           "Please enter the wall thickness of this material stock as a decimal.");
                frmvalidator.addValidation("wallThickness",
                                           "greaterthan=-0.000001",
                                           "The wall thickness should be greater than or equal to zero.");
                frmvalidator.addValidation("lowWarpValue",
                                           "dec",
                                           "Please enter the low warp value of this material stock as a decimal.");
                frmvalidator.addValidation("lowWarpValue",
                                           "greaterthan=-0.000001",
                                           "The low warp value should be greater than or equal to zero.");
                frmvalidator.addValidation("highWarpValue",
                                           "dec",
                                           "Please enter the high warp value of this material stock as a decimal.");
                frmvalidator.addValidation("highWarpValue",
                                           "greaterthan=-0.000001",
                                           "The high warp value 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 levl 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>';
    }
?>

