<?php
    function getMaterial($db, $userRow, $materialNumber){
        // Init all of the settings to the appropriate values
        $materialID = '';
        $materialName = '';
        $materialDescription = '';
        $materialCategory = 'Aluminum';
        $materialColor = 'FFFFFF';
        $modifyType = 'add';
        $newBlankDefault = false;
        
        $newBlankDefaultHTML = '';
        
        // Determine if this is an update or not
        $materialAssocTask = null;
        if ($materialNumber){
            // Determine if the user editing is part of the main company
            if ($userRow["CompanyID"]!=1){
                // We should not even be in here if we are not a main company employee
                return;
            }
            
            // Create the query to get the the material specified
            $sql = 'SELECT * FROM material WHERE MaterialID = ' . $materialNumber . ';';

            // Attempt to get the material
            $materialRow = $db->queryDB($sql, $userRow, "company_presentation Form occurance 1");

            // Attempt to get the company row
            $materialAssocTask = $materialRow->fetch_assoc();


            // Set the things we can
            $materialID = $materialAssocTask["MaterialID"];
            $materialName = $materialAssocTask["Name"];
            $materialDescription = $materialAssocTask["Description"];
            $materialCategory = $materialAssocTask["Category"];
            $materialColor = $materialAssocTask["MaterialRowColor"];
            $newBlankDefault = $materialAssocTask["NewBlankDropDownDefault"];
            
            // Determine if we have values for the closed flag
            if ($newBlankDefault != null and $newBlankDefault==true){
                // Set it to what we got in
                $newBlankDefaultHTML = 'checked';
            }

            $modifyType = 'update';
        }

        return '
            <form action="' . $modifyType . 'MaterialType.php?updateMaterialID=' . $materialNumber . '" method="post" name="' . $modifyType . 'Material" enctype="multipart/form-data">  
                <table width=100%>
                    <tr width=100%>
                        <td colspan=1 height="39" align="right" valign="middle">
                            <strong>Material Name:</strong>&nbsp;&nbsp; 
                        </td>
                        <td colspan=2 height="39" align="left" valign="middle">
                            <input name="materialName" type="text" maxlength="25" class="intext" style="width: 100%;" value="' . $materialName . '">
                        </td>
                    </tr>
                    <tr width=100%>
                        <td colspan=1 height="39" align="right" valign="middle">
                           <strong>Material Type:</strong>&nbsp;&nbsp;
                        </td>
                        <td colspan=2 height="39" align="left" valign="middle">
                           '. $db->getTypeAsSelectOptions('material', 'Category', 'category', $materialCategory, null, '', false, false) . '
                        </td>
                    </tr>
                    <tr width=100%>
                        <td colspan=1 height="39" align="right" valign="middle">
                            <strong>Material Color:</strong>&nbsp;&nbsp;
                        </td>
                        <td colspan=1 height="39" align="left" valign="middle">
                            <script type="text/javascript">        
                              $(document).ready(
                                function()
                                {
                                  $("#materialRowColor").jPicker({window:{position:{x:"center",y:"center"},expandable: true,liveUpdate: true}});
                                });
                            </script>
                            <input id="materialRowColor" name="materialRowColor" type="text" maxlength="6" value="' . $materialColor . '" style="width: 60px; height: 20px; overflow: auto; resize:none;"/>
                        </td>
                        <td colspan=1 height="39" align="center" valign="middle">
                            <input name="isNewBlankDropDownDefault" id="isNewBlankDropDownDefault" type="checkbox" ' . $newBlankDefaultHTML . '>
                            <strong><label for="isNewBlankDropDownDefault">Default New Blank Dropdown Selection</label></strong>
                        </td>
                    </tr>
                    <tr width=100%>
                        <td colspan=3 width="100%"><strong>Material Description (1000 characters)</strong><br> 
                            <textarea name="description" maxlength="1000" class="intext" style="width: 100%; height: 100px; overflow: auto; resize:vertical;">' .
                            $materialDescription .
                            '</textarea>
                        </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 . ' 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 . 'Material", "formSubmit");
                frmvalidator.addValidation("materialName",
                                           "required",
                                           "Please enter the material name.");
            </script>';
    }
?>

