<?php
    function getMachineCheckboxes($inAirBlast, $inIsDown, $showOnWhiteboard){
        // Initialize the return
        $result = '';

        // Initialize the checkbox html values
        $airBlastHTML = '';
        $isDownHTML = '';

        // Determine if we have values for the 'air blast' flag
        if ($inAirBlast != null and $inAirBlast==true){
            // Set it to what we got in
            $airBlastHTML = 'checked';
        }

        // Determine if we have values for the 'is down' flag
        if ($inIsDown != null and $inIsDown==true){
            // Set it to what we got in
            $isDownHTML = 'checked';
        }

        // Determine if we have values for the 'show on machine' flag
        if ($showOnWhiteboard != null and $showOnWhiteboard==true){
            // Set it to what we got in
            $showOnWhiteboardHTML = 'checked';
        }

        try {
            // Send back the check boxes that are available
            $result='
                    <tr>
                        <td colspan=3 height="39" align="left" valign="middle">
                            <input name="airBlast" id="airBlast" type="checkbox" ' . $airBlastHTML . '>
                            <strong><label for="airBlast">Air Blaster</label></strong>
                        </td>
                        <td colspan=3 height="39" align="left" valign="middle">
                            <input name="isDown" id="isDown" type="checkbox" ' . $isDownHTML . '>
                            <strong><label for="isDown">Down</label></strong>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=6 height="39" align="left" valign="middle">
                            <input name="showOnWhiteboard" id="showOnWhiteboard" type="checkbox" ' . $showOnWhiteboardHTML . '>
                            <strong><label for="showOnMachine">Show On Whiteboard</label></strong>
                        </td>
                    </tr>';
        } catch (Exception $ex) {
            // Assign the output string as to what happened
            return $result;
        }

        // Return whatever we got
        return $result;
    }

    function getMachine($db, $userRow, $machineNumber){
        // Init all of the settings to the appropriate values
        $machineID = '';
        $name = '';
        $type = 'MILL';
        $description = '';
        $xTravel = 0;
        $yTravel = 0;
        $zTravel = 0;
        $dateAcquired = '';
        $maxRPM = 0;
        $airBlast = false;
        $isDown = false;
        $maxTools = 0;
        $showOnWhiteboard = 1;

        $modifyType = 'add';

        // Determine if this is an update or not
        $machineAssocRow = null;
        if ($machineNumber){
            // 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 machine
            $sql = 'SELECT * FROM machines WHERE MachineID = ' . $machineNumber . ';';

            // Attempt to get the machine row
            $machineRow = $db->queryDB($sql, $userRow, "machine_presentation Form occurance 1");

            // Attempt to get the machine row
            $machineAssocRow = $machineRow->fetch_assoc();

            // Set the things we can
            $machineID = $machineAssocRow["MachineID"];
            $name = $machineAssocRow["Name"];
            $type = $machineAssocRow["Type"];
            $description = $machineAssocRow["Description"];
            $xTravel = $machineAssocRow["XTravel"];
            $yTravel = $machineAssocRow["YTravel"];
            $zTravel = $machineAssocRow["ZTravel"];
            $dateAcquired = $machineAssocRow["DateAcquired"];
            $maxRPM = $machineAssocRow["MaxRPM"];
            $airBlast = $machineAssocRow["AirBlast"];
            $isDown = $machineAssocRow["IsDown"];
            $maxTools = $machineAssocRow["MaxTools"];
            $showOnWhiteboard = $machineAssocRow["ShowOnWhiteboard"];

            $modifyType = 'update';
        }

        return '
            <form action="' . $modifyType . 'Machine.php?updateMachineID=' . $machineID . '" method="post" name="' . $modifyType . 'Machine" 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=2 height="39" align="right" valign="middle">
                                        <strong>Machine Name:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                        <input name="name" type="text" maxlength="30" class="intext" style="width: 100%; height: 19px" value="' . $name . '">
                                        <input type="hidden" name="machineID" value="' . $machineID . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Max RPM:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                        <input name="maxRPM" type="text" maxlength="5" class="intext" style="width: 100%; height: 19px" value="' . $maxRPM . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                        <strong>Max Tools:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                        <input name="maxTools" type="text" maxlength="4" class="intext" style="width: 100%; height: 19px" value="' . $maxTools . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=2 height="39" align="right" valign="middle">
                                      <strong>Date Acquired:</strong>&nbsp;&nbsp;
                                    </td>
                                    <td colspan=4 height="39" align="left" valign="middle">
                                      <input name="dateAcquired" type="date" class="intext" style="width: 100%;" value="' . $dateAcquired . '">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan=6 height="39" align="center" valign="middle">
                                        <strong>Travel </strong> <br>
                                    </td>
                                </tr>
                                <tr style="outline: thin solid">
                                    <td height="39" align="right" valign="middle" width="5%">
                                        <strong>X: </strong>
                                    </td>
                                    <td height="39" align="left" valign="middle">
                                        <input name="xTravel" type="text" maxlength="4" class="intext" style="width: 100%; height: 19px" value="' . $xTravel . '">
                                    </td>
                                    <td height="39" align="right" valign="middle" width="5%">
                                        <strong>Y: </strong>
                                    </td>
                                    <td height="39" align="left" valign="middle">
                                        <input name="yTravel" type="text" maxlength="4" class="intext" style="width: 100%; height: 19px" value="' . $yTravel . '">
                                    </td>
                                    <td height="39" align="right" valign="middle" width="5%">
                                        <strong>Z: </strong>
                                    </td>
                                    <td height="39" align="left" valign="middle">
                                        <input name="zTravel" type="text" maxlength="4" class="intext" style="width: 100%; height: 19px" value="' . $zTravel . '">
                                    </td>
                                </tr>'
                                .
                                getMachineCheckboxes($airBlast,$isDown, $showOnWhiteboard)
                                .
                                '
                                <tr>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                       <button type="button" name="toolListButton" id="toolListButton" title="View Tool List." onclick="childWindow=window.open(\'machineToolList.php?toolsMachineID=' . $machineID . '\', \'_blank\');">View</button>                                            
                                        <strong><label for="toolListButton">Tool List</label></strong>
                                    </td>
                                    <td colspan=3 height="39" align="left" valign="middle">
                                        <button type="button" name="maintenanceButton" id="maintenanceButton" title="View Maintenance Checklist." onClick="javascript:alert(\'This feature is not yet added.\n\nCheck back later!\');">View</button>
                                        <strong><label for="maintenanceButton">Maintenance List</label></strong>
                                    </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 height="39" align="left" valign="top"><strong>Type: </strong>' 
                                        . $db->getTypeAsSelectOptions('machines', 'Type', 'type', $type, null, '', false, '') . 
                                    '</td>
                                </tr>
                                <tr>
                                    <td width="100%"><strong>Machine Notes (3000 characters)</strong><br> 
                                    <textarea name="description" maxlength="3000" class="intext" style="width: 100%; height: 290px; overflow: auto; resize:none">' .
                                        $description .
                                    '</textarea>
                                    </td>
                                </tr>
                            </table>
                        </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 . ' Machine" />
                </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 . 'Machine", "formSubmit");
                frmvalidator.addValidation("name",
                                           "required",
                                           "There must be a value for the the machine name.");
                frmvalidator.addValidation("xTravel",
                                           "num",
                                           "Please enter the X Travel of this machine as a number.");
                frmvalidator.addValidation("xTravel",
                                           "greaterthan=-1",
                                           "The X travel should be greater than or equal to zero.");
                frmvalidator.addValidation("yTravel",
                                           "num",
                                           "Please enter the Y Travel of this machine as a number.");
                frmvalidator.addValidation("yTravel",
                                           "greaterthan=-1",
                                           "The Y Travel should be greater than or equal to zero.");
                frmvalidator.addValidation("zTravel",
                                           "num",
                                           "Please enter the Z Travel of this machine as a number.");
                frmvalidator.addValidation("zTravel",
                                           "greaterthan=-1",
                                           "The Z travel should be greater than or equal to zero.");
                frmvalidator.addValidation("maxRPM",
                                           "num",
                                           "Please enter the maximum RPM of this machine as a number.");
                frmvalidator.addValidation("maxRPM",
                                           "greaterthan=-1",
                                           "The maximum RPM should be greater than or equal to zero.");
                frmvalidator.addValidation("maxTools",
                                           "num",
                                           "Please enter the maximum amount of tools of this machine as a number.");
                frmvalidator.addValidation("maxTools",
                                           "greaterthan=-1",
                                           "The maximum amount of tools should be greater than or equal to zero.");
            </script>';
    }


?>

