<?php 
    // continue the session
    session_start();
    
    // Add includes to PHP files that I will need to access
    include 'php_only_scripts/database.php';
    include 'php_only_scripts/global_functions.php';
    include 'php_only_scripts/presentation.php';
    include 'php_only_scripts/blank_presentation.php';
    include 'php_only_scripts/stocklist_presentation.php';
    include 'php_only_scripts/note_presentation.php';

    // init variables
    $userRow=null;
    $updateBlankID=-1;
    
    // Assume NOT gtg
    $gtg = false;

    // now determine if it was posted via get.  That will override it.
    if (isset($_GET['updateBlankID']) ){
        // Hold onto the Blank ID in a local variable
        $updateBlankID = $_GET['updateBlankID'];
    }
    
    // Create and Setup the database
    $db = new Database();
    $db->setupTmsDB();

    // Attempt to log in with this user
    $currentUser = $db->getLoggedInSessionUser(session_id());
    
    // If there is only one result for a username/password combo, then they are good to go.
    if ($currentUser == null){
        exit;
    }
    elseif (mysqli_num_rows($currentUser)==1){
        // Attempt to get the user row
        $userRow = $currentUser->fetch_assoc();
        
        // Determine if this user has access to this page
        if ($userRow["UserLevel"]=='TIMECARD ONLY' ||
            $userRow["SUEmployee"]!=1 || 
            $updateBlankID == -1 ||
            $userRow["AccountStatus"]!='ACTIVE'){
            // redirect to the invalid access page
            echo ('<script>window.location = "invalid_access.php";</script>');

            exit;
        }
        
        // Set the good to go until they log out
        $gtg = true;

        // Determine if BOTH the user name AND password has been set
        if (isset($_POST['formSubmitted']) && 
            htmlspecialchars($_POST['formSubmitted'])=='TRUE'){
            // Determine if this user can even modify
            if (!$userRow["CanModify"]){
                // redirect to the tool page
                echo ('<script>window.location = "blanks.php?c=' . $categoryOf . '";</script>');
            }
            else {
                // Change the form back to NOT being submitted
                $_SESSION['formSubmitted']='FALSE';

                // Set the things we can
                $materialID = htmlspecialchars($_POST['materialID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $taskID = htmlspecialchars($_POST['taskID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $parentTaskNumber = htmlspecialchars($_POST['parentTaskID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $createdByID = htmlspecialchars($_POST['createdByID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $type = htmlspecialchars($_POST['type'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $dimension1 = htmlspecialchars($_POST['dimension1'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $dimension2 = htmlspecialchars($_POST['dimension2'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $dimension3 = htmlspecialchars($_POST['dimension3'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $quantityDimension = htmlspecialchars($_POST['quantityDimension'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $wallThickness = htmlspecialchars($_POST['wallThickness'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $rush = (isset($_POST['rush'.$updateBlankID])?1:0);
                $submitted = (isset($_POST['submitted'.$updateBlankID])?1:0);
                $submittedByID = htmlspecialchars($_POST['submittedByID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $submittedDate=str_replace("T"," ",htmlspecialchars($_POST['submittedDate'.$updateBlankID]));
                $submittedTouched = $_POST['submittedTouched'.$updateBlankID];
                $quoteRequested = (isset($_POST['quoteRequested'.$updateBlankID])?1:0);
                $quoteRequestedByID = htmlspecialchars($_POST['quoteRequestedByID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $quoteRequestedTouched = $_POST['quoteRequestedTouched'.$updateBlankID];
                $quoteRequestedDate = htmlspecialchars($_POST['quoteRequestedDate'.$updateBlankID], ENT_QUOTES, 'UTF-8');;
                $blankReady = (isset($_POST['blankReady'.$updateBlankID])?1:0);
                $blankReadiedByID = htmlspecialchars($_POST['blankReadiedByID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $blankReadiedDate = htmlspecialchars($_POST['blankReadiedDate'.$updateBlankID], ENT_QUOTES, 'UTF-8');;
                $blankReadiedTouched = $_POST['blankReadiedTouched'.$updateBlankID];
                $isPendingStock = (isset($_POST['isPendingStock'.$updateBlankID])?1:0);
                $isPendingStockByID = htmlspecialchars($_POST['isPendingStockByID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $isPendingStockDate = htmlspecialchars($_POST['isPendingStockDate'.$updateBlankID], ENT_QUOTES, 'UTF-8');;
                $isPendingStockTouched = $_POST['isPendingStockTouched'.$updateBlankID];
                $reason = htmlspecialchars($_POST['reason'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $quantity = htmlspecialchars($_POST['quantity'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                $suggestStockID = htmlspecialchars($_POST['suggestStockID'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                
                // Set the good values flag to true
                $goodValues = true;
                
                // Init Set the bad values to show back to the user
                $badValuesString = '';
                
                // Set info about new stuff that could happen
                $newlyQuoteRequested = 0; 
                $newlyReadied = 0; 
                $newlySubmitted = 0; 
                
                // Check if the dimension 1 is good
                if (!is_numeric($dimension1)){
                    // Add to the string to show
                    $badValuesString = $badValuesString . ' Dimension 1 was not a decimal,';
                    
                    // Set it as a bad post
                    $goodValues=false;
                }
                
                // Check if the dimension 2 is good
                if (!is_numeric($dimension2)){
                    // Add to the string to show
                    $badValuesString = $badValuesString . ' Dimension 2 was not a decimal,';
                    
                    // Set it as a bad post
                    $goodValues=false;
                }
                
                // Check if the dimension 3 is good
                if (!is_numeric($dimension3)){
                    // Add to the string to show
                    $badValuesString = $badValuesString . ' Dimension 3 was not a decimal,';
                    
                    // Set it as a bad post
                    $goodValues=false;
                }
                
                // Check if the quantity is good
                if (!is_numeric($quantity) || fmod($quantity, (int)$quantity)!=0){
                    // Add to the string to show
                    $badValuesString = $badValuesString . ' Quantity was not an integer,';
                    
                    // Set it as a bad post
                    $goodValues=false;
                }
                
                
                // determine that the quote requested flag is not checked without the submit flag
                if ($quoteRequested && !$submitted){
                    // Add to the string to show
                    $badValuesString = $badValuesString . ' Blank cannot be in quote requested state without being in submitted state,';
                    
                    // Set it as a bad post
                    $goodValues=false;
                }
                
                // Determine if they are trying to ready and quote request at the same time
                if (((!is_numeric($quoteRequestedByID) || $quoteRequestedByID<=0) && $quoteRequested) &&
                    ((!is_numeric($blankReadiedByID  ) || $blankReadiedByID  <=0) && $blankReady    )){
                    // Add to the string to show
                    $badValuesString = $badValuesString . ' Blank cannot be set to quote requested and Readied state at the same time,';
                    
                    // Set it as a bad post
                    $goodValues=false;
                }
                
                
                // determine that the ready flag is not checked without the submit flag
                if ($blankReady && !$submitted){
                    // Add to the string to show
                    $badValuesString = $badValuesString . ' Blank cannot be in ready state without being in submitted state,';
                    
                    // Set it as a bad post
                    $goodValues=false;
                }
                
                // Determine if we have bad values for this blank
                if (!$goodValues && $badValuesString != ''){
                    // Remove the trailing comma and first blank
                    $badValuesString = substr($badValuesString, 0, -1);
                    $badValuesString = substr($badValuesString, 1);
                    
                    // Create the alert string
                    $alertString = 'There were bad values in an already saved blank ' .
                                   'with the reason of ' . $reason . '. The bad values ' .
                                   'were (' . $badValuesString . ').  This one was not ' .
                                   'saved. Try again.';
                    
                    // Send out the alert
                    echo '<script>alert("' . $alertString . '");</script>';

                }
                else {
                    // Determine if this one is set to submitted
                    if ($submitted){
                        // Determine if this is newly submitted
                        if (!is_numeric($submittedByID) || $submittedByID<=0){
                            // Set the flag to be that it is being newly submitted
                            $newlySubmitted=1;
                        }

                        // Determine if this has been touched by this user
                        if ($submittedTouched==1){
                            // Set the user ID to this one no matter what. Nulling it will be taken care of later
                            $submittedByID = $userRow["UserID"];

                            // Set the submitted date as the current date
                            $submittedDate = $currentDateString;
                        }
                    }
                    // Determine if this has been previously submitted and now is being unsubmitted
                    else{
                        // Submitted by ID should be removed
                        $submittedByID = 'NULL';

                        // Submitted date should be removed as well
                        $submittedDate = null;

                        // quote requested should be unchecked be unchecked, since it needs to be resubmitted
                        $quoteRequested = 0;

                        // Quote Requested by ID should be removed as well
                        $quoteRequestedByID = 'NULL';

                        // Set the quote requested date as nothing
                        $quoteRequestedDate = null;

                        // Blank ready should be unchecked, since it needs to be resubmitted
                        $blankReady = 0;

                        // Blank readied by ID should be removed as well
                        $blankReadiedByID = 'NULL';

                        // Set the readied date as nothing
                        $blankReadiedDate = null;
                    }

                    // Determine if quote requested is checked
                    if ($quoteRequested){
                        // Determine if this is newly request for quote
                        if (!is_numeric($quoteRequestedByID) || $quoteRequestedByID<=0){
                            // Set the flag to be that it is being newly request for quote
                            $newlyQuoteRequested=1;
                        }

                        // Determine if this has been touched by this user
                        if ($quoteRequestedTouched==1){
                            // Set the user ID to this one no matter what. Nulling it will be taken care of later
                            $quoteRequestedByID = $userRow["UserID"];

                            // Set the quote requested date as the current date
                            $quoteRequestedDate = $currentDateString;
                        }   
                    }
                    else{
                        // Quote Requested by ID should be removed as well
                        $quoteRequestedByID = 'NULL';

                        // Set the quote requested date as nothing
                        $quoteRequestedDate = null;
                    }
                            
                    // Determine if quote requested is checked
                    if ($blankReady){
                        // Determine if this is newly readied
                        if (!is_numeric($blankReadiedByID) || $blankReadiedByID<=0){
                            // Set the flag to be that it is being newly readied
                            $newlyReadied=1;
                        }

                        // Determine if this has been touched by this user
                        if ($blankReadiedTouched==1){
                            // Set the user ID to this one no matter what. Nulling it will be taken care of later
                            $blankReadiedByID = $userRow["UserID"];
                            
                            // Set the readied date as the current date
                            $blankReadiedDate = $currentDateString;
                        }
                    }
                    // Determine if the blank was previously checked readied and is now being reversed
                    else {
                        // Blank readied by ID should be removed as well
                        $blankReadiedByID = 'NULL';

                        // Set the readied date as nothing
                        $blankReadiedDate = null;
                    }

                    // Determine if Is Pending Stock is checked
                    if ($isPendingStock){
                        // Determine if this has been touched by this user
                        if ($isPendingStockTouched==1){
                            // Set the user ID to this one no matter what. Nulling it will be taken care of later
                            $isPendingStockByID = $userRow["UserID"];
                            
                            // Set the readied date as the current date
                            $isPendingStockDate = $currentDateString;
                        }
                    }
                    // Determine if the blank was previously checked readied and is now being reversed
                    else {
                        // Is Pending Stock ID should be removed as well
                        $isPendingStockByID = 'NULL';

                        // Set the is pending stock as nothing
                        $isPendingStockDate = null;
                    }

                    // Set the default values as not selected for all at first
                    $dimension1Precise = 0;
                    $dimension1Clean = 0;
                    $dimension2Precise = 0;
                    $dimension2Clean = 0;
                    $dimension3Precise = 0;
                    $dimension3Clean = 0;
                    
                    // Get the combo box values
                    $dimension1ComboValue = htmlspecialchars($_POST['dimension1Radio'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                    $dimension2ComboValue = htmlspecialchars($_POST['dimension2Radio'.$updateBlankID], ENT_QUOTES, 'UTF-8');
                    $dimension3ComboValue = htmlspecialchars($_POST['dimension3Radio'.$updateBlankID], ENT_QUOTES, 'UTF-8');

                    // Check the combo values to set which one is selected for dimension 1
                    switch ($dimension1ComboValue) {
                        case "Clean":
                            // Clean is selected so set it to true
                            $dimension1Clean = 1;

                            // break out 
                            break;
                        case "Precise":
                            // Precise is selected so set it to true
                            $dimension1Precise = 1;

                            // break out 
                            break;
                    }                

                    // Check the combo values to set which one is selected for dimension 2
                    switch ($dimension2ComboValue) {
                        case "Clean":
                            // Clean is selected so set it to true
                            $dimension2Clean = 1;

                            // break out 
                            break;
                        case "Precise":
                            // Precise is selected so set it to true
                            $dimension2Precise = 1;

                            // break out 
                            break;
                    }                

                    // Check the combo values to set which one is selected for dimension 3
                    switch ($dimension3ComboValue) {
                        case "Clean":
                            // Clean is selected so set it to true
                            $dimension3Clean = 1;

                            // break out 
                            break;
                        case "Precise":
                            // Precise is selected so set it to true
                            $dimension3Precise = 1;

                            // break out 
                            break;
                    }                

                    // Create the update query
                    $sql='UPDATE jobblanks SET';
                    if ($materialID!=null) {$sql = $sql . ' MaterialID=' . $materialID;}
                    if ($createdByID!=null) {$sql = $sql . ' ,CreatedByID=' . $createdByID;}
                    if ($suggestStockID!=null) {$sql = $sql . ' ,SuggestedMaterialStockID=' . $suggestStockID;}
                    if ($type!=null) {$sql = $sql . ' ,Type=\'' . $type . '\'';}
                    if ($dimension1!=null) {$sql = $sql . ' ,Dimension1=' . $dimension1;}
                    $sql = $sql . ' ,Dimension1Precise=' . $dimension1Precise;
                    $sql = $sql . ' ,Dimension1Clean=' . $dimension1Clean;
                    if ($dimension2!=null) {$sql = $sql . ' ,Dimension2=' . $dimension2;}
                    $sql = $sql . ' ,Dimension2Precise=' . $dimension2Precise;
                    $sql = $sql . ' ,Dimension2Clean=' . $dimension2Clean;
                    if ($dimension3!=null) {$sql = $sql . ' ,Dimension3=' . $dimension3;}
                    $sql = $sql . ' ,Dimension3Precise=' . $dimension3Precise;
                    $sql = $sql . ' ,Dimension3Clean=' . $dimension3Clean;
                    if ($quantityDimension!=null) {$sql = $sql . ' ,QuantityDimension=\'' . $quantityDimension . '\'';}
                    $sql = $sql . ' ,WallThickness=' . $wallThickness;
                    $sql = $sql . ' ,BlankReady=' . $blankReady;
                    if ($blankReadiedByID!=null) {$sql = $sql . ' ,BlankReadiedByID=' . $blankReadiedByID;}
                    $sql = $sql . ($blankReadiedDate==null?(' ,BlankReadiedDate=NULL '):(' ,BlankReadiedDate=\'' . $blankReadiedDate . '\' '));
                    $sql = $sql . ' ,Rush=' . $rush;
                    $sql = $sql . ' ,QuoteRequested=' . $quoteRequested;
                    if ($quoteRequestedByID!=null) {$sql = $sql . ' ,QuoteRequestedByID=' . $quoteRequestedByID;}
                    $sql = $sql . ($quoteRequestedDate==null?(' ,QuoteRequestedDate=NULL '):(' ,QuoteRequestedDate=\'' . $quoteRequestedDate . '\' '));
                    $sql = $sql . ' ,Submitted=' . $submitted;
                    if ($submittedByID!=null) {$sql = $sql . ' ,SubmittedByID=' . $submittedByID;}
                    $sql = $sql . ($submittedDate==null?(' ,SubmittedDate=NULL '):(' ,SubmittedDate=\'' . $submittedDate .'\' '));
                    $sql = $sql . ' ,IsPendingStock=' . $isPendingStock;
                    if ($isPendingStockByID!=null) {$sql = $sql . ' ,IsPendingStockByID=' . $isPendingStockByID;}
                    $sql = $sql . ($isPendingStockDate==null?(' ,IsPendingStockDate=NULL '):(' ,IsPendingStockDate=\'' . $submittedDate .'\' '));
                    if ($reason!=null) {$sql = $sql . ' ,Reason=\'' . $reason . '\'';}
                    if ($quantity!=null) {$sql = $sql . ' ,Quantity=' . $quantity;}
                    $sql=$sql .' WHERE JobBlanksID=' . $updateBlankID . ';';
                    
                    // Attempt to run the query and get the response
                    if ($db->queryDB($sql, $userRow, "UpdateBlank Form occurance 1.")){
                        // Create the visualized task orientation as though this IS the parent
                        $firstFour = str_pad($taskID,4,"0",STR_PAD_LEFT);
                        $lastFour = '0000';

                        // Determine if this is a parent or a child so we can make all of the status changes to the children as well.
                        if ($parentTaskNumber != 0){
                            // Create the visualized task orientation as though this IS the parent
                            $firstFour = str_pad($parentTaskNumber,4,"0",STR_PAD_LEFT);
                            $lastFour = str_pad($taskID,4,"0",STR_PAD_LEFT);
                        }

                        // Determine if it has been newly submitted or newly readied or new quote requested
                        if ($newlySubmitted || $newlyReadied || $newlyQuoteRequested){
                            // Determine if this is a rush part
                            $rushSubjectString = '';
                            $rushHeaderString = 'NOT rushed. ';
                            if ($rush){
                                // Create the rush strings
                                $rushSubjectString = "** RUSH **";
                                $rushHeaderString = " a RUSH job. ";
                            }

                            // We need to query the material
                            $materialSQL="SELECT * FROM material WHERE MaterialID = " . $materialID . ";";
                            $row = $db->queryDB($materialSQL, $userRow, "UpdateBlank Form occurance 2");
                            $assocMaterial = $row->fetch_assoc();

                            // Create blank specs
                            $blankSpecs = $quantity . " blanks of " . 
                                          $dimension1 . " (" . $dimension1ComboValue . 
                                          ") x " . $dimension2 . " (" . $dimension2ComboValue . 
                                          ") x " . $dimension3 . " (" . $dimension3ComboValue . ") " .
                                          $assocMaterial["Category"] . " " . $assocMaterial["Name"] . 
                                          " " . $type;

                            // Init the quoted string
                            $quotedString = '';

                            // Determine if this blank is newly quoted and set a default message for that
                            if ($newlyQuoteRequested){
                                // Set the default to be as though this was NOT the only thing done
                                $quotedString = '<br><br>Quote Requested:<br> This blank has just had QUOTE REQUESTED. ' . 
                                                ' We must wait for quote and delivery before readying blank.<br><br>';
                            }

                            // Determine if it is newly submitted
                            if ($newlySubmitted){
                                // Create the generic subject for just a submitted blank
                                $subject = $rushSubjectString . " TaskID " . $taskID . ": " . 
                                           $blankSpecs . " SUBMITTED. " . $rushSubjectString;

                                // Determine if this blank has a recommended stock ID and change message appropriately
                                $blankInformation="";
                                if ($suggestStockID!=null && $suggestStockID>0){
                                    // Only show the the blank that was suggest information to get
                                    $blankInformation=ShowSuggested($db, $userRow, $suggestStockID);
                                }
                                else {
                                    // Show them any recommendations that we can figure out
                                    $blankInformation=RecommendBlankStock($db, $loopNumber, $userRow);
                                }

                                // Create the generic message for just a submitted blank
                                $messageToSend= 'There has been a blank creation SUBMITTED by ' . 
                                        $userRow['FirstName'] . ' ' . $userRow['LastName'] . 
                                        ' for task ID ' . $taskID . '<a href="https://www.eestms.com/' . 
                                        getTMSHome() . '/updateTask.php?updateTaskID=' . 
                                        $taskID . '&parentTaskId=' . $parentTaskNumber . 
                                        '">     (Click To View)</a>' . '. ' . $quotedString . 
                                        'This blank is ' . $rushHeaderString . '.<br><br>The specs are:<br>' . 
                                        $blankSpecs . $blankInformation . 
                                        '<br><br>Cut Dimension is:<br>' . $quantityDimension . '<br><br>' . 
                                        '<br><br>Reason is:<br>' . $reason . '<br><br>' . 
                                        'If you would like to check on this blank, you can ' .
                                        'do so at www.eestms.com/' . getTMSHome() . '.<br><br>' .
                                        'If you need help with your user name or password, you can ' .
                                        'reply to this e-mail or call us at (720)552-7997.<br><br>' .
                                        'Regards, <br><br>Evolve Engineering and Software Support Team<br>' .
                                        '(720)552-7997';

                                // Add the condition to not include submittor
                                $extraConditionalSQL = ' AND   UserID <> ' . $submittedByID . ' ';

                                // Determine if this blank is newly quoted and set a default message for that
                                if ($newlyQuoteRequested){
                                    // Add the condition to not include quoter
                                    $extraConditionalSQL .= ' AND   UserID <> ' . $quoteRequestedByID . ' ';
                                }

                                // Determine if the submittor also readied the blank
                                if ($newlyReadied){
                                    // Create the subject for just a submitted and readied blank
                                    $subject = $rushSubjectString . " TaskID " . $taskID . ": " . 
                                               $blankSpecs . " to be HAS BEEN READIED. " . $rushSubjectString;

                                    // Create the generic message for just a submitted blank
                                    $messageToSend= 'There has been a blank creation SUBMITTED by ' . 
                                            $userRow['FirstName'] . ' ' . $userRow['LastName'] . 
                                            ' for task ID ' . $taskID . '<a href="https://www.eestms.com/' . 
                                            getTMSHome() . '/updateTask.php?updateTaskID=' . 
                                            $taskID . '&parentTaskId=' . $parentTaskNumber . 
                                            '">  (Click To View)</a>' . '.  This blank has already ' . 
                                            'been cut.<br><br>The specs were:<br>' . $blankSpecs . 
                                            '<br><br>Cut Dimension is:<br>' . $quantityDimension . '<br><br>' . 
                                            '<br><br>Reason is: ' . $reason . '<br><br>' . 
                                            'If you would like to check on this blank, you can ' .
                                            'do so at www.eestms.com/' . getTMSHome() . '.<br><br>' .
                                            'If you need help with your user name or password, you can ' .
                                            'reply to this e-mail or call us at (720)552-7997.<br><br>' .
                                            'Regards, <br><br>Evolve Engineering and Software Support Team<br>' .
                                            '(720)552-7997';
                                }
                            }
                            else{
                                // Determine if this blank is newly quoted and set a default message for that
                                if ($newlyQuoteRequested){
                                    // Set the default to be as though this was NOT the only thing done
                                    $quotedString = '<br><br>Quote Requested:<br> This blank has just had QUOTE REQUESTED. ' . 
                                                    ' We must wait for quote and delivery before readying blank.<br><br>';

                                    // Create the subject for just a Quoted
                                    $subject = " Blank for TaskID " . $taskID . ": With specs of - " . 
                                               $blankSpecs . " HAS BEEN QUOTED. ";

                                    // Create the generic message for just a quoted blank
                                    $messageToSend= 'There has been a QUOTE REQUESTED by ' . 
                                            $userRow['FirstName'] . ' ' . $userRow['LastName'] . 
                                            ' for task ID ' . $firstFour . '-' . $lastFour . 
                                            '<a href="https://www.eestms.com/' . 
                                            getTMSHome() . '/updateTask.php?updateTaskID=' . 
                                            $taskID . '&parentTaskId=' . $parentTaskNumber . 
                                            '">     (Click To View)</a>' . '.<br><br>The specs were:<br>' . 
                                            $blankSpecs . '<br><br>Reason is:<br>' . $reason . '<br><br>' . 
                                            'If you would like to check on this blank, ' . 
                                            'you can do so at www.eestms.com/' . getTMSHome() . '.<br><br>' .
                                            'If you need help with your user name or password, you can ' .
                                            'reply to this e-mail or call us at (720)552-7997.<br><br>' .
                                            'Regards, <br><br>Evolve Engineering and Software Support Team<br>' .
                                            '(720)552-7997';

                                    // Add the condition to not include quoter
                                    $extraConditionalSQL = ' AND   UserID <> ' . $quoteRequestedByID . ' ';
                                }
                                else {
                                    // Create the subject for just a readied blank
                                    $subject = " Blank for TaskID " . $taskID . ": With specs of - " . 
                                               $blankSpecs . " HAS BEEN readied. ";

                                    // Create the generic message for just a submitted blank
                                    $messageToSend= 'There has been a blank READIED by ' . 
                                            $userRow['FirstName'] . ' ' . $userRow['LastName'] . 
                                            ' for task ID ' . $firstFour . '-' . $lastFour . 
                                            '<a href="https://www.eestms.com/' . 
                                            getTMSHome() . '/updateTask.php?updateTaskID=' . 
                                            $taskID . '&parentTaskId=' . $parentTaskNumber . 
                                            '"> (Click To View)</a>' . '.<br><br>The specs were:<br>' . 
                                            $blankSpecs . '<br><br>Reason is: ' . $reason . '<br><br>' . 
                                            'If you would like to check on this blank, ' . 
                                            'you can do so at www.eestms.com/' . getTMSHome() . '.<br><br>' .
                                            'If you need help with your user name or password, you can ' .
                                            'reply to this e-mail or call us at (720)552-7997.<br><br>' .
                                            'Regards, <br><br>Evolve Engineering and Software Support Team<br>' .
                                            '(720)552-7997';

                                    // Add the condition to not include submittor
                                    $extraConditionalSQL = ' AND   UserID <> ' . $blankReadiedByID . ' AND SUEmployee=1 AND AccountStatus="Active" ';
                                }
                            }

                            // Add Headers
                            $headers = "From: EES Mail Admin"." <admin@eestms.com>\r\n"; 
                            $headers .= "MIME-Version: 1.0\r\n";
                            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

                            // Let's create and get the users to inform
                            $sql='SELECT * 
                                  FROM users 
                                  WHERE (UserID = ' . $createdByID . '  
                                         OR ReceiveBlankCutNotificationUpdate = 1) 
                                  AND UserTemplateOnly=0 '
                                  .
                                  $extraConditionalSQL
                                  . ';';
                            $usersToNotify = $db->queryDB($sql, $userRow, "UpdateBlank Form occurance 3.");

                            // Loop through the returned results
                            while ($userToNofity=mysqli_fetch_array($usersToNotify)){
                                try {
                                    // Attempt to mail it
                                    mail($userToNofity["EMail"],$subject,$messageToSend,$headers);
                                } 
                                catch (Exception $e) {
                                    echo("The notification was not sent to " . $assocUser["EMail"] .
                                         " because of an error.");
                                }
                            }
                        }
                    }
                }
                
                // redirect to the blanks page
                echo ('<script>window.location = "blanks.php";</script>');
            }
        }
        else {
            // Set the form submitted session variable to FALSE
            $_POST['formSubmitted']='FALSE';
        }
    }
?>
        
﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Task Management System - Update Blank</title>

    <meta name="keywords" content="GIS,data,testing,IT" />
    <meta name="description" content="EES Divisions." />

    <script src="./SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
    <script src="javascript/general.js" type="text/javascript"></script>
    <script src="javascript/notes.js" type="text/javascript"></script>
    <script src="scripts/jpicker-1.1.6/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script src="scripts/jpicker-1.1.6/jpicker-1.1.6.min.js" type="text/javascript"></script>
    <script language="JavaScript" src="scripts/gen_validatorv4.js" type="text/javascript"></script>
    
    <link rel="icon" href="images/ees_ico.ico"/> 
    
    <link href="scripts/jpicker-1.1.6/css/jPicker-1.1.6.css" rel="stylesheet" type="text/css"/>
    <link href="./SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
    <link href="./SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
    <link rel = "stylesheet" type="text/css" href="./css/contentpage.css" >

    <style type="text/css">
        /* Simulate the disabled atribute, but still pass the values */
        .disable{
            pointer-events:none;
            background:lightgrey;
            /* border: solid red 1px; */
        }        
        
        /* Simulate the disabled atribute, but still pass the values */
        .disableaslabel{
            pointer-events:none;
            background: rgba(0, 0, 0, 0);
            border: none;
        }        
        /* Style tab links */
        .tablink {
          float: left;
          border: 1px solid gray;
          outline: none;
          cursor: pointer;
          padding: 5px 5px;
          font-size: 11px;
          width: 25%;
        }
        .tablink:hover {
          background-color: #777;
        }
        /* Style the tab content (and add height:100% for full page content) */
        .tabcontent {
          border: 1px solid gray;
          padding: 5px 5px;
        }
        .childtablink {
          float: left;
          border-left: 1px solid gray;
          border-right: 1px solid gray;
          border-top: 1px solid gray;
          border-bottom: 2px solid forestgreen;
          outline: none;
          cursor: pointer;
          padding: 5px 5px;
          font-size: 11px;
          width: 16.66666666%;
        }
        .childtablinkblack {
          color:black;
          font-size: 8px;
        }
        .childtablinkred {
          color:red;
          font-size: 8px;
        }
        .childtablinkgreen {
          color:green;
          font-size: 8px;
        }
        .childtablink:hover {
          background-color: #777;
        }
        .childtabcontent {
          border-left: 2px solid forestgreen;
          border-right: 2px solid forestgreen;
          border-top: none;
          border-bottom: 2px solid forestgreen;
          text-align: left;
        }
    </style>
        
    <script type="text/javascript">
        $(function()
          {
            $.fn.jPicker.defaults.images.clientPath='scripts/jpicker-1.1.6/images/';
          });
    </script>
</head>

<body class="thrColElsHdr">

<div id="container">
  
<!-- start of banner wrap --->

<div id="topContent">
  
<div>
   <div class="MenuBarHorizontal" id="navBar">  <ul id="TopNav" class="MenuBarHorizontal">

    <script type="text/javascript">
    <!--
    var MenuBar2 = new Spry.Widget.MenuBar("TopNav", {imgDown:"./SpryAssets/SpryMenuBarDownHover.gif", imgRight:"./SpryAssets/SpryMenuBarRightHover.gif"});
    //-->
    </script>

    </div>
  </div>
</div>
<!-- end of banner wrap --->

  <!-- end #header -->
  <br class="clearfloat" />
  <div id="sidebar1">
      <?php 
        // Determine if we are GTG
        if ($gtg){
          // Render the appropriate presentation for the tasks sidebar
          getSidebar($userRow, 'equipment');
        }
      ?>       
      <br class="clearfloat" />
  <!-- end #sidebar1 -->
  </div>
  
  <!-- start #mainContent -->
  <div id="mainContent">
    <div class="maintitle">Task Management System - Update Blank</div>
    <div class="breadcrumb"> <a href="../index.html"> Home </a> > <a href="blanks.php"> Task Management System - Blanks </a> > <a href=
        "<?php echo ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'?"https" : "http") . "://" . $_SERVER['HTTP_HOST'] .  $_SERVER['REQUEST_URI']); ?>"> Update Tool </a></div>

    <br>
    <?php
        // Determine if they are GTG
        if ($gtg){
            echo retrieveBlank($db, $userRow, $updateBlankID);
        }
        else {
            // Set the login failed to true
            $_SESSION['failedLogin']='TRUE';

            // Add a session variable that is this page URL
            $_SESSION['pageToGoTo']=$_SERVER['REQUEST_URI'];

            // redirect to the login page
            echo ('<script>window.location = ".";</script>');
        }
    ?>       
    
  <!-- end #mainContent -->
  
  <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
  <br class="clearfloat" />
  
  <!-- start #footer --></div>
   <div id="footer">
    <p>© 2018 Evolve Engineering and Software. EES, Evolve Engineering and Software, EES Logo are registered marks of Evolve Engineering and Software.</p>
  <!-- end #footer -->
   </div>
<!-- end #container -->
</div>

</body>

</html>