<?php
require_once(SCRIPT_DIR . 'db_calls.php');
//$acctval  = null;
$account  = (isset($_SESSION['account'])) ? $_SESSION['account'] : null;
//$locval   = null;
$location = (isset($_SESSION['location'])) ? $_SESSION['location'] : null;
//if (isset($_SESSION['account'])) {
$qs = '?';
if (isset($loggedInUser)) {
   // $qs .='&loggedInUser=true';
    $acctArray = fetchUserDetails(NULL, NULL, $loggedInUser->user_id);
    $acct_name = $acctArray['user_name'];

    //$account = null;
    if ($acct = getAccountByName($acct_name)) { // check for valid account value
        if (!$acct['results']) {
            if ($acct['error']) {
                $qs .= '&getAccountByNameReturnedError_' .  $acct['error'] . '_&query=' . $acct['query'];
            }
        }
        else {
            $useraccount = $acct['results']['id'];
            $qs .= "&getAccountByName=$account";
            if (!$account) {
                $_SESSION['account'] = $useraccount;
                $account = $_SESSION['account'];
            }



            if ($location) {
                if ($l = getLocation($location, $account)) { // check for valid location value
                    // $qs .= '&getLocationWorked';
                    //$location = $_SESSION['location'];
                    if (!$l['results']) {
                        if ($l['error']) {
                            $qs .= '&getLocationReturnedError_' .  $l['error'] . '_&query=' . $l['query'];
                            //echo "<!--" .    $qs .= '&getLocationReturnedError_' .  $l['error'] . '_&query=' . $l['query'] . '-->';
                        }
                        else {
                            //echo "<!-- no error -->";
                        }
                    }
                    else {
                        //echo "<!-- getLocation returned results -->";

                        $qs .= '&getLocationReturnedResults';
                        $start_time      = $l['results']['start_time'];
                        $hourMin         = substr($start_time,0,2);
                        $minuteMin       = substr($start_time,2,2);
                        $end_time	     = $l['results']['end_time'];
                        $hourMax         = substr($end_time,0,2);
                        $minuteMax       = substr($end_time,2,2);
                        $appt_increments = $l['results']['appt_increments'];

                    }
                }
                else {
                      $qs .= '&getLocationFailed';
                    //echo "<!-- getLocation failed -->";
                }
            }
            else {
                $qs .= '&noLocationFound';
                //echo "<!-- no location found -->";
            }
        }

    }
    else {
        //echo "<p>Invalid account</p>";
    }
}
else {
    $account = null;
}
//require_once('./scripts/email_details.php');


//$defaultEmailName = 'Email of ' . date("M-d");
//$submitText = "Create Email Text";
$setApptText = "Set Appointment";
//$locID = (isset($_GET['location'])) ? trim($_GET['location']) : null;
//$eid = (isset($_GET['email_id'])) ? trim($_GET['email_id']) : null;
// = (isset($_GET['appt_id'])) ? trim($_GET['appt_id']) : null;




if (!isset($_GET['formAction'])) {
   // echo '<!-- nothing submitted -->';
    /*#######################################################
    THIS SECTION IS WHAT HAPPENS AFTER AN ACTION IS DONE AND
    THE PAGE IS REDIRECTED BACK TO ITSELF WITH NO FORM ACTION
    #######################################################*/
    //$qs = '?NOformactionset&';
    echo "<!-- $qs -->";
    //require_once('./scripts/loadForms.php');
}
else {
    /*#######################################################
    THIS  'ELSE' BLOCK IS THE CODE THAT FIRES WHEN THE USER
    SUBMITS THE FORM TO DO SOMETHING. AFTER THE ACTION IS COMPLETED
    AND (OPTIONALLY) 'email' PARAMETERS}. THIS IS TO PREVENT
    RE-SUBMIT PROBLEMS.
    #######################################################*/
    $action = trim($_GET['formAction']);
    $script = $_SERVER['SCRIPT_NAME'];
   // $qs = '?formactionset&';
    //echo 'action: ' . $action;
    switch($action) {
        case 'loginAccount':
            include_once('scripts/login.php');
            break;
        case 'logout':
            include_once('scripts/logout.php');
            break;
        case 'chooseLocation':
            include_once('scripts/chooseLocation.php');
            break;
        case 'chooseEmail':
            include_once('scripts/chooseEmail.php');
            break;
        case 'deleteEmail':
            include_once('scripts/deleteEmail.php');
            break;
        case 'saveEmail':
            include_once('scripts/saveEmail.php');
            break;
        case 'setAppointment':
            include_once('scripts/setAppointment.php');
            break;
        case 'deleteAppts':
            include_once('scripts/deleteAppointment.php');
            break;
        default:
            break;
    }
   /* redirect to same page but without any actions.*/
   header( 'Location: ' .  $script . $qs ) ;
}
//mysql_close($link);
