// Non-Club Dependent Constant Definitions
var COOKIE_DOMAIN = 'aaa.com'; // Top Level Domain
var COOKIE_EXPIRATION = ''; // Session Only
var COOKIE_PATH = '/';
var NOL_COOKIE_NAME = "AAANoLogin";
var RP_COOKIE_NAME = "AAAReturnPage";

// Club Dependent Constant Definitions
var DEFAULT_CLUB_HOME_URL = "http://www.aaa.com/aaa/188/site/homepage.htm";
var LOGIN_URL = "/scripts/WebObjects.dll/AAAOnline?association=aaa&club=188&page=ExternalSignin&returnpoint=";
var LOGOUT_URL = "/scripts/WebObjects.dll/AAAOnline?association=aaa&club=188&page=signout";
var RETURN_POINT_URL = "http%3A%2F%2F" + document.domain + "%2Fscripts%2FWebObjects.dll%2FAAAOnline%3Fassociation%3DAAA%26club%3D188%26page%3DClubConfig68";

// Define a local variable for capturing the login key
var nolKey = "";

// This function is used to control the display of the 
// "Login/Logout" text
function displayLoginLogoutText()
{
  // The following will verify the user's login status and
  // display the appropriate Login/Logut button
  var imgLI = document.getElementById("spnLI");
  var imgLO = document.getElementById("spnLO");
  if (!isUserLoggedIn()) 
  {
    imgLI.style.display = "inline";
    imgLO.style.display = "none";
  }
  else 
  {
    // Display Logout Text 
    imgLI.style.display = "none"; 
    imgLO.style.display = "inline";
  }
}

// This function is used to initiate the login process
function doLoginRedirect()
{
  // Store the current page in a cookie
  setNOCookie(RP_COOKIE_NAME,document.location.href,COOKIE_EXPIRATION,COOKIE_PATH,COOKIE_DOMAIN);
    
  // Invoke the sign-in process
  document.location.href = LOGIN_URL + RETURN_POINT_URL;
}

// This function is used to initiate the logout process
function doLogoutRedirect()
{
  // Update the login status cookie
  setNOCookie(NOL_COOKIE_NAME,"false",COOKIE_EXPIRATION,COOKIE_PATH,COOKIE_DOMAIN);
  
  // Invoke the sign-out process
  document.location.href = LOGOUT_URL;
}

// This function will redirect the user to the page that
// they were on when the "Login" button was clicked. This
// function call needs to be placed on a club configurable
// page which is currently set to ClubConfig68
function doPostLoginRedirect()
{
  // The user successfully logged in so update the login
  // status cookie.
  setNOCookie(NOL_COOKIE_NAME,"true",COOKIE_EXPIRATION,COOKIE_PATH,COOKIE_DOMAIN);
  
  // Read the return page from the cookie
  var returnPage = readNOCookie(RP_COOKIE_NAME);
  
  // Check to ensure that the return page was stored in the
  // cookie. If there is an issue, simply redirect to the club's
  // home page.
  if (returnPage == null || returnPage == "")
  {
    returnPage = DEFAULT_CLUB_HOME_URL;
  }
  
  document.location.href = returnPage;
}

// This function is used to determine the login status of the user
function isUserLoggedIn()
{
  var nolStatus = "";  
  
  // Check for the login status via the cookie
  nolStatus = readNOCookie(NOL_COOKIE_NAME);
    
  if (nolStatus != null && nolStatus == "true")
  {
    return true;
  }
  
  // Check for the login status via the login key smart tag
  // since the login status cookie is not present
  else
  {
    if (nolKey != null && nolKey.length > 1)
    {
      // In this case, the user logged in via the Travel 2
      // Sign-In component, so update the login status cookie
      setNOCookie(NOL_COOKIE_NAME,"true",COOKIE_EXPIRATION,COOKIE_PATH,COOKIE_DOMAIN);
      return true;
    }
      
    return false;
  }
}

// This function is used to retrieve the specified data
// from the club's cookie
function readNOCookie(CookieName) {
  var search = CookieName + "=";
  if(document.cookie.length > 0) 
  {
    offset = document.cookie.indexOf(search);
    if(offset != -1) 
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);

      if(end == -1) 
      {
        end = document.cookie.length;
      }
      return document.cookie.substring(offset, end);
    }
  }
}

// This function is used to write the specified data
// to the club's cookie
function setNOCookie(name, value, expires, path, domain, secure)
{
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime(today.getTime());

  /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
  */
  if (expires) 
  {
    expires = expires * 1000 * 60 * 60 * 24;
  }
    
  var expires_date = new Date(today.getTime() + (expires));

  document.cookie = name + "=" + value +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ((path) ? ";path=" + path : "" ) +
    ((domain) ? ";domain=" + domain : "" ) +
    ((secure) ? ";secure" : "" );
}
