
// ***************************************************************************
// APPLICATION/COOKIES/QUESTION_EMERGENCY_LAYOUT_TABLES.JS
// (C) 2007 Peter Newman
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 'Emergency Layout Table' cookie support...
// ***************************************************************************

/*
//  ==========================================================================
//  COOKIE_NAME__QUESTION_ELT
//  ==========================================================================

    define( 'COOKIE_NAME__QUESTION_ELT' , 'elt' ) ;
        //  elt = 0     ==> ELT not used
        //  elt = 1     ==> ELT used
        //
        //  If this cookie DOESN'T exist, then elt = 1 (use ELT) should be
        //  assumed.
*/

//  ==========================================================================
//  webos__get_question_elt__raw_but_clean
//  ==========================================================================

function webos__get_question_elt__raw_but_clean() {

    //  ----------------------------------------------------------------------
    //  webos__get_question_elt__raw_but_clean()
    //  - - - - - - - - - - - - - - - - - - - -
    //  Returns the value of the:-
    //      COOKIE_NAME__QUESTION_ELT
    //
    //  cookie.
    //
    //  This will be one of the following values:-
    //
    //      -2  :   There IS a COOKIE_NAME__QUESTION_ELT cookie - but it
    //              has an INVALID VALUE (neither 0 nor 1).
    //
    //      -1  :   There IS NO COOKIE_NAME__QUESTION_ELT cookie.
    //
    //       0  :   There IS a COOKIE_NAME__QUESTION_ELT cookie - and it has
    //              the VALID VALUE 0:-
    //              ==>     ELT OFF
    //
    //       1  :   There IS a COOKIE_NAME__QUESTION_ELT cookie - and it has
    //              the VALID VALUE 1:-
    //              ==>     ELT ON
    //
    //  NOTE!
    //  -----
    //  This routine issues NO error messages.
    //  ----------------------------------------------------------------------

    //  ----------------------------------------------------------------------
    //  The 'currently selected content type' is that defined by the:-
    //      COOKIE_NAME__QUESTION_ELT
    //
    //  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 question_elt = quirksmode_readCookie(
                            COOKIE_NAME__QUESTION_ELT
                            ) ;

    //  ----------------------------------------------------------------------

    if ( !question_elt ) {
        return -1 ;
        }
*/

    // ===========================================================================
    // 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 question_elt = processCookie(
                            'general'                   ,   //  cookieName
                            'read'                      ,   //  func
                            COOKIE_NAME__QUESTION_ELT       //  name
                            ) ;

    //  ----------------------------------------------------------------------

    if ( question_elt === null ) {
        return -1 ;
        }

    //  ----------------------------------------------------------------------
    //  Watch out for hackers...
    //
    //  In other words, if the value is illegal/invalid, return -2...
    //
    //  NOTE!
    //  -----
    //  A valid content type name is numeric 0 or 1 only.
    //  ----------------------------------------------------------------------

    if ( question_elt !== '0' &&  question_elt !== '1' ) {
        return -2 ;
        }

    // -----------------------------------------------------------------------
    // OK; the supplied cookie looks OK!
    //
    // So, we can return it's value...
    // -----------------------------------------------------------------------

    return question_elt ;

    // -----------------------------------------------------------------------
    // That's that!
    // -----------------------------------------------------------------------

    }

//  ==========================================================================
//  webos__get_question_elt__clean_and_simplified
//  ==========================================================================

function webos__get_question_elt__clean_and_simplified() {

    //  ----------------------------------------------------------------------
    //  webos__get_question_elt__clean_and_simplified()
    //  - - - - - - - - - - - - - - - - - - - - - - - -
    //  Returns a flag indicating whether or not the user wants to use ELT.
    //
    //  This will be determined from the:-
    //      COOKIE_NAME__QUESTION_ELT
    //
    //  cookie (sent by the user's browser).
    //
    //  But if there is no such cookie - or if that cookie has an invalid
    //  value - we default to TRUE.
    //
    //  NOTE!
    //  ------
    //  This routine issues NO error messages.
    //  ----------------------------------------------------------------------

    var question_elt = webos__get_question_elt__raw_but_clean() ;

    //  ----------------------------------------------------------------------

    if ( question_elt === '0' ) {
        return false ;
        }

    //  ----------------------------------------------------------------------

    return true ;

    //  ----------------------------------------------------------------------

    }

//  ==========================================================================
//  webos__set_question_elt
//  ==========================================================================

function webos__set_question_elt( question_elt ) {

    //  ----------------------------------------------------------------------
    //  webos__set_question_elt( true|false )
    //  - - - - - - - - - - - - - - - - - - -
    //  Sets the:-
    //      COOKIE_NAME__QUESTION
    //
    //  cookie to either:-
    //      false ==> 0, or;
    //      true  ==> 1.
    //
    //  RETURNS
    //      Nothing
    //  ----------------------------------------------------------------------

    var new_cookie_value ;

    if ( question_elt ) {
        new_cookie_value = '1' ;

    } else {
        new_cookie_value = '0' ;

        }

    // -----------------------------------------------------------------------

/*
    hunlock_setCookie(
        COOKIE_NAME__QUESTION_ELT   ,
        new_cookie_value            ,
        30                          ,   //  30 days = 1 month
        COOKIE_PATH
        ) ;
*/

    processCookie(
         'general'                   ,   //  cookieName
         'set'                       ,   //  func
         COOKIE_NAME__QUESTION_ELT   ,   //  name
         new_cookie_value            ,   //  value
         30                              //  days = 1 month
         ) ;

    // -----------------------------------------------------------------------
    // That's that!
    // -----------------------------------------------------------------------

    }

