<?php
    function getCreditInfo($userRow, $taskPrice, $allowCredit, $disabled){
        // Initialize the return
        $result = '';

        // Initialize the checkbox html values
        $allowCreditHTML = false;
        $disabledHTML='';

        // Determine if we have values for the allow credit flag
        if ($allowCredit != null and $allowCredit==true){
            // Set it to what we got in
            $allowCreditHTML = 'checked';
        }

        // Determine if we have values for the disabled flag
        if ($disabled != null and $disabled==true){
            // Set it to what we got in
            $disabledHTML='disabled';
        }

        try {
            // If the SU has signed in, then they can assign it to anyone
            if ($userRow["UserLevel"]=="SU"){
                $result='
                            <td height="39" align="center" valign="middle">
                                <input name="allowCreditPayment" id="allowCreditPayment" type="checkbox" ' . $disabledHTML . ' ' . $allowCreditHTML . '>
                                <strong><label for="allowCreditPayment" ' . $disabledHTML . ' >Credit Card Allowed</label></strong>
                            </td>
                        ';
            }
        } catch (Exception $ex) {
            // Assign the output string as to what happened
            return $result;
        }

        // Return whatever we got
        return $result;
    }

    function getCompany($db, $userRow, $companyNumber){
        // Init all of the settings to the appropriate values
        $companyID = '';
        $companyName = '';
        $companyColor = 'e2ddcf';
        $allowCredit = true;
        $modifyType = 'add';
        $goalFudgeFactor = 0;
        $percentToGoal = 0;
        $active = 1;
        
        // HTML for super company
        $goalHTML='';
        
        // Determine if this is an update or not
        $companyAssocTask = null;
        if ($companyNumber){
            // Determine if the user editing this is NOT a SU or not
            if ($userRow["UserLevel"]!='SU'){
                // We should not even be in here if we are not a SU
                return;
            }
            
            // Determine if this is the super company
            // Create the query to get the the company and the user that created the task
            $sql = 'SELECT * FROM companies WHERE CompanyID = ' . $companyNumber . ';';

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

            // Attempt to get the company row
            $companyAssocTask = $companyRow->fetch_assoc();

            // Set today as the quarter to check
            $quarterStart = new DateTime();

            // Set the things we can
            $companyID = $companyAssocTask["CompanyID"];
            $companyName = $companyAssocTask["CompanyName"];
            $companyColor = $companyAssocTask["CompanyRowColor"];
            $allowCredit = $companyAssocTask["AllowCredit"];
            $active = $companyAssocTask["active"];
            $mainSubCompany = $companyAssocTask["MainSubCompany"];
            $store = $companyAssocTask["Store"];
            $defaultTaskStatusCompletionFactor = $companyAssocTask["DefaultTaskStatusCompletionFactor"];
            $weekdayStartWorkingTime = $companyAssocTask["WeekdayStartWorkingTime"];
            $weekdayEndWorkingTime = $companyAssocTask["WeekdayEndWorkingTime"];
            $weekendStartWorkingTime = $companyAssocTask["WeekendStartWorkingTime"];
            $weekendEndWorkingTime = $companyAssocTask["WeekendEndWorkingTime"];
            $activeHTML = "";
            $mainSubCompanyHTML = "";
            $setAsStoreHTML = "";
            $modifyType = 'update';
            
            // Determine if this is the super company
            if ($companyNumber==1){
                $goalHTML=' <tr width=100%>
                                <td colspan=1 height="39" align="right" valign="middle">
                                    <strong>Default Task Status Completion Factor:</strong>&nbsp;&nbsp;
                                </td>
                                <td colspan=3 height="39" align="left" valign="middle">
                                    <input name="defaultTaskStatusCompletionFactor" id="defaultTaskStatusCompletionFactor" type="text" maxlength="15" class="intext" style="width: 100%;" value="' . $defaultTaskStatusCompletionFactor . '">
                                </td>
                            </tr>';
            }
        }

        // Determine if we have active flag
        if ($active != null and $active==1){
            // Set it to what we got in
            $activeHTML = 'checked';
        }

        // Determine if this is a sub company of the main compan
        if ($mainSubCompany != null and $mainSubCompany==1){
            // Set it to what we got in
            $mainSubCompanyHTML = 'checked';
        }
        
        // Determine if this is a store
        if ($store != null and $store==1){
            // Set it to what we got in
            $setAsStoreHTML = 'checked';
        }

        return '
            <form action="' . $modifyType . 'Company.php?updateCompanyID=' . $companyNumber . '" method="post" name="' . $modifyType . 'Company" enctype="multipart/form-data">  
                <table width=100%>
                    <tr width=100%>
                        <td colspan=1 height="39" align="right" valign="middle">
                            <strong>Company Name:</strong>&nbsp;&nbsp;
                        </td>
                        <td colspan=3 height="39" align="left" valign="middle">
                            <input name="companyName" id="companyName" type="text" maxlength="150" class="intext" style="width: 100%; height: 20px; overflow: auto; resize:none" value="' . $companyName . '">
                        </td>
                    </tr>
                    <tr width=100%>
                        <td colspan=1 height="39" align="right" valign="middle">
                            <strong>Company Color:</strong>&nbsp;&nbsp;
                        </td>
                        <td colspan=3 height="39" align="left" valign="middle">
                            <script type="text/javascript">        
                              $(document).ready(
                                function()
                                {
                                  $("#companyRowColor").jPicker({window:{position:{x:"center",y:"center"},expandable: true,liveUpdate: true}});
                                });
                            </script>
                            <input id="companyRowColor" name="companyRowColor" type="text" maxlength="6" value="' . $companyColor . '" style="width: 60px; height: 20px; overflow: auto; resize:none"/>
                        </td>
                    </tr>
                    '
                    .
                        $goalHTML 
                    .
                    '
                    <tr>
                        <td height="39" align="right" valign="middle">
                            <strong>Weekday Start Time:</strong>&nbsp;&nbsp;
                        </td>
                        <td height="39" align="left" valign="middle">
                            <input name="weekDayStartTime" id="weekDayStartTime" type="time" value="' . $weekdayStartWorkingTime . '">
                            <strong>CST</strong>&nbsp;&nbsp;
                        </td>
                        <td height="39" align="right" valign="middle">
                            <strong>Weekday End Time:</strong>&nbsp;&nbsp;
                        </td>
                        <td height="39" align="left" valign="middle">
                            <input name="weekdayEndTime" id="weekdayEndTime" type="time" value="' . $weekdayEndWorkingTime . '">
                            <strong>CST</strong>&nbsp;&nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td height="39" align="right" valign="middle">
                            <strong>Weekend Start Time:</strong>&nbsp;&nbsp;
                        </td>
                        <td height="39" align="left" valign="middle">
                            <input name="weekendStartTime" id="weekendStartTime" type="time" value="' . $weekendStartWorkingTime . '">
                            <strong>CST</strong>&nbsp;&nbsp;
                        </td>
                        <td height="39" align="right" valign="middle">
                            <strong>Weekend End Time:</strong>&nbsp;&nbsp;
                        </td>
                        <td height="39" align="left" valign="middle">
                            <input name="weekendEndTime" id="weekendEndTime" type="time" value="' . $weekendEndWorkingTime . '">
                            <strong>CST</strong>&nbsp;&nbsp;
                        </td>
                    </tr>
                    <tr>
                    ' . getCreditInfo($userRow, -1, $allowCredit, false) . '
                        <td height="39" align="center" valign="middle">
                            <input name="active" id="active" type="checkbox" ' . $activeHTML . '>
                            <strong><label for="active" ' . $disabledHTML . ' >Active Company</label></strong>
                        </td>
                        <td height="39" align="center" valign="middle">
                            <input name="mainSubCompany" id="mainSubCompany" type="checkbox" ' . $mainSubCompanyHTML . '>
                            <strong><label for="mainSubCompany" ' . $disabledHTML . ' >Sub Company of Main</label></strong>
                        </td>
                        <td height="39" align="center" valign="middle">
                            <input name="setAsStore" id="setAsStore" type="checkbox" ' . $setAsStoreHTML . '>
                            <strong><label for="setAsStore" ' . $disabledHTML . ' >Set as Store</label></strong>
                        </td>
                    </tr>
                    <tr width=100%>
                        <td colspan=4 width=100%>
                            </table>' .
                            retrieveCurrentAddresses($db, $userRow, $companyNumber, 'CompanyID') .
                            '<strong>New Addresses: </strong>
                            <div id="newAddresses" style="margin: 5 auto; width: 100%; ">
                            </div>
                        </td>
                    </tr>
                </table>
                <p align="right">
                    <input type="hidden" id="formSubmitted" name="formSubmitted" value="TRUE">
                    <input type="hidden" id="newAddressNumbers" name="newAddressNumbers" value="0">
                    <button type="button" onClick="addNewAddress_NicelyFormedRow(\'COMPANY\')" align="right">Add New Address</button>
                    <input id="formSubmit" type="submit" style="cursor: hand; cursor: pointer;" value="' . $modifyType . ' Company" />
                </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 . 'Company", "formSubmit");
                frmvalidator.addValidation("companyName",
                                           "required",
                                           "There must be a value for the company name.");
            </script>';
    }
?>

