
// ***************************************************************************
// APPLICATION/COOKIES/ISSUE_EDIT_ACTION.JS
// (C) 2007 Peter Newman
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 'Issue Expand/Collapse' cookie support...
// ***************************************************************************

//  ==========================================================================
//  kiwiparty__get_issue_edit_action__raw_but_clean
//  ==========================================================================

function kiwiparty__get_issue_edit_action__raw_but_clean() {

    //  ----------------------------------------------------------------------
    //  kiwiparty__get_issue_edit_action__raw_but_clean()
    //  - - - - - - - - - - - - - - - - - - - - - -
    //  Returns one of the following values:-
    //
    //      -1  :   There IS a COOKIE_NAME__ISSUE_EDIT_ACTION cookie
    //              - but it has an INVALID VALUE.
    //
    //       0  :   There IS NO COOKIE_NAME__ISSUE_EDIT_ACTION cookie.
    //
    //      'e' :   There IS a COOKIE_NAME__ISSUE_EDIT_ACTION cookie -
    //              and it has the VALID VALUE 'e' (= show the 'Edit'
    //              tab)
    //
    //      'p' :   There IS a COOKIE_NAME__ISSUE_EDIT_ACTION cookie -
    //              and it has the VALID VALUE 'p' (= show the 'Place On-Line'
    //              tab)
    //
    //      'd' :   There IS a COOKIE_NAME__ISSUE_EDIT_ACTION cookie -
    //              and it has the VALID VALUE 'd' (= show the 'Delete'
    //              tab)
    //
    //      'h' :   There IS a COOKIE_NAME__ISSUE_EDIT_ACTION cookie -
    //              and it has the VALID VALUE 'h' (= show the 'History'
    //              tab)
    //
    //  NOTE!
    //  -----
    //  This routine issues NO error messages.
    //  ----------------------------------------------------------------------

    //  ----------------------------------------------------------------------
    //  Local variables...
    //  ----------------------------------------------------------------------

    var no_such_cookie       =  0 ;
    var invalid_cookie_value = -1 ;

    //  ----------------------------------------------------------------------
    //  The current issue expand/collapse setting is given by the
    //  value of the:-
    //      COOKIE_NAME__ISSUE_EDIT_ACTION
    //
    //  cookie (if there is one)...
    //  ----------------------------------------------------------------------

/*
    //  ----------------------------------------------------------------------
    // quirksmode_readCookie( name )
    // - - - - - - - - - - - - - - -
    // To read out a cookie, call this function and pass the name of the
    // cookie.  Put the returned value in a variable.  Then check if this
    // variable has a value (if the cookie does not exist the variable
    // becomes null, which might upset the rest of your function), then
    // do whatever is necessary.
    //
    // For example:-
    //      var x = quirksmode_readCookie('ppkcookie1')
    //      if (x) {
    //          [do something with x]
    //          }
    //  ----------------------------------------------------------------------

    var issue_edit_action = quirksmode_readCookie(
                                        COOKIE_NAME__ISSUE_EDIT_ACTION
                                        ) ;

    //  ----------------------------------------------------------------------

    if ( !issue_edit_action ) {
        return no_such_cookie ;
        }
*/

    // ===========================================================================
    // function processCookie(
    //      cookieName  ,
    //      func        ,
    //      name        ,
    //      value       ,
    //      days
    //      )
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Do stuff with cookies.  In particular, support "stacked cookies" (=
    // multiple "name=value" pairs in the one cookies proper).
    //
    // NOTE!
    // -----
    // 1.   To create/use use a stacked cookie; make 'cookieName' and 'name'
    //      different.
    //
    // 2.   The operations available are:-
    //      o   Kill    (delete),
    //      o   Clear   (non-stacked - delete, stacked - remove name/value pair),
    //      o   Set     (add/update the name/value pair creating cookie if needs
    //                  be),
    //      o   Read    (return the value, if present, of the provided named
    //                  element)
    //
    //      cookieName  [required]  -   The name of the cookie.
    //
    //      func        [required]  -   What processing on the cookie is required,
    //                                  i.e. kill (delete), clear (non-stacked -
    //                                  delete, stacked - remove name/value pair),
    //                                  set (add/update the name/value pair creating
    //                                  cookie if needs be), read (return the value,
    //                                  if present, of the provided named element)
    //
    //      name        [required]  -   Identifies the name/value pair to be
    //                                  operated on. If the same as cookieName
    //                                  indicates a non-stacked cookie.
    //
    //      value       [optional]  -   Supplies the data the be associated with the
    //                                  identified named cookie element.
    //
    //      days        [optional]  -   In cases where a cookie is created or
    //                                  updated, this sets the period of validity.
    //                                  Defaults to 365 days.
    //
    // See: "Javascript/PHP Cookies"
    //      kos Haks
    //      http://cass-hacks.com/articles/discussion/js_php_cookies/
    //
    // RETURNS
    //      o   If func == 'read'
    //          - - - - - - - - -
    //          cookie value = Cookie read OK!
    //          null         = Read ERROR (NO error message issued)!
    //      o   Otherwise
    //          - - - - -
    //          true  = Cookie killed, cleared or set OK!
    //          false = ERROR (NO error message issued)!
    // ===========================================================================

    var issue_edit_action = processCookie(
                                'issues'                        ,   //  cookieName
                                'read'                          ,   //  func
                                COOKIE_NAME__ISSUE_EDIT_ACTION      //  name
                                ) ;

    //  ----------------------------------------------------------------------

    if ( issue_edit_action === null ) {
        return no_such_cookie ;
        }

    //  ----------------------------------------------------------------------
    //  Watch out for hackers (the value just obtained was supplied over the
    //  NET - and might have been supplied by a hacker wishing to attack
    //  the site).
    //
    //  So, if the value is illegal/invalid, we return:-
    //      $invalid_cookie_value
    //
    //  NOTE!
    //  -----
    //  A valid issue edit_action value is either 'e', 'p', 'd' or 'p'.
    //  ----------------------------------------------------------------------

    if (    issue_edit_action !== 'e'
            &&
            issue_edit_action !== 'p'
            &&
            issue_edit_action !== 'd'
            &&
            issue_edit_action !== 'h'
            ) {
        return invalid_cookie_value ;
        }

    // -----------------------------------------------------------------------
    // OK; the supplied cookie looks OK!
    //
    // So, we can return it's value...
    // -----------------------------------------------------------------------

    return issue_edit_action ;

    // -----------------------------------------------------------------------
    // That's that!
    // -----------------------------------------------------------------------

    }

//  ==========================================================================
//  kiwiparty__get_issue_edit_action__clean_and_simplified
//  ==========================================================================

function kiwiparty__get_issue_edit_action__clean_and_simplified() {

    //  ----------------------------------------------------------------------
    //  kiwiparty__get_issue_edit_action__clean_and_simplified()
    //  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    //  Returns the current issue edit action setting.  Unless that value
    //  isn't defined, or is invalid, in which case the default issue edit
    //  action value ('e' = Edit) will be returned.
    //
    //  The current selected issue edit action value is determined from the:-
    //  from the:-
    //      COOKIE_NAME__ISSUE_EDIT_ACTION
    //
    //  cookie (sent by the user's browser).
    //
    //  NOTE!
    //  ------
    //  This routine issues NO error messages.
    //  ----------------------------------------------------------------------

    var issue_edit_action = kiwiparty__get_issue_edit_action__raw_but_clean() ;

    //  ----------------------------------------------------------------------

    if ( typeof( issue_edit_action ) === 'string' ) {
        return issue_edit_action ;
        }

    //  ----------------------------------------------------------------------

    return 'e' ;
        // This is the default...

    //  ----------------------------------------------------------------------

    }

//  ==========================================================================
//  kiwiparty__set_issue_edit_action
//  ==========================================================================

function kiwiparty__set_issue_edit_action( issue_edit_action ) {

    //  ----------------------------------------------------------------------
    //  kiwiparty__set_issue_edit_action( issue_edit_action )
    //  - - - - - - - - - - - - - - - - - - - - - - - - - - -
    //  Sets the:-
    //      COOKIE_NAME__ISSUE_EDIT_ACTION
    //
    //  cookie.
    //
    //  'issue_edit_action' should be either:-
    //      'e' (= Show the 'Edit' tab),
    //      'p' (= Show the 'Place On-Line' tab),
    //      'd' (= Show the 'Delete' tab), or;
    //      'h' (= Show the 'History' tab).
    //
    //  RETURNS
    //      Nothing
    //  ----------------------------------------------------------------------

/*
    hunlock_setCookie(
        COOKIE_NAME__ISSUE_EDIT_ACTION      ,
        issue_edit_action                   ,
        0                                   ,   //  kill on browser close...
        COOKIE_PATH
        ) ;
*/

    processCookie(
        'issues'                        ,   //  cookieName
        'set'                           ,   //  func
        COOKIE_NAME__ISSUE_EDIT_ACTION  ,   //  name
        issue_edit_action               ,   //  value
        0                                   //  kill on browser close...
        ) ;

    // -----------------------------------------------------------------------
    // That's that!
    // -----------------------------------------------------------------------

    }

