//<SCRIPT> // <- dummy script tag used to enable InterDev syntax coloring
//------------------------------------------------------------------------
// File:    sc_popup.js
// Author:  Al Yanchak
// Date:    
//
// Revised: 
//
// SmartCAT v3.0 library file
//
// Implements client-side objects for SmartCAT menu
//
// Copyright© 1999, I-Mark Inc. All Rights Reserved
//
// WARNING: The information in this file is protected by copyright law
// and international treaty provisions. Unauthorized reproduction or
// distribution of this file, or any portion of it, may result in severe
// criminal and civil penalties, and will be prosecuted to the maximum
// extent possible under the law.  Further, you may not reverse engineer,
// decompile, or disassemble the file.
//------------------------------------------------------------------------

POPUP_MSG_ID  = 0;
POPUP_MSG_URL = 1;

POP_MSG_MODE_NEW = 0;
POP_MSG_MODE_URGENT = 1;

POPUP_WINDOW_HEIGHT = 470;
POPUP_WINDOW_WIDTH  = 400;

//---------------------------------------------------------------------
// imkGetCookie
//
// gets the named cookie and returns its value
//---------------------------------------------------------------------
function imkGetCookie(str_cookieName) 
{
  var str_cookie = "";
  
  if (document.cookie.length > 0) // if there are any cookies
  {
    str_srch = str_cookieName + "="
    var pos1 = document.cookie.indexOf(str_srch) 
    if (pos1 != -1) 
    {
      pos1 += str_srch.length; 
      var pos2 = document.cookie.indexOf(";", pos1) 
      if (pos2 == -1) 
        pos2 = document.cookie.length

      str_cookie = unescape(document.cookie.substring(pos1, pos2));
    } 
  }
  return str_cookie;
}

//---------------------------------------------------------------------
// imkSetCookie
//
// sets the named cookie, value, and (optional) expiration
//---------------------------------------------------------------------
function imkSetCookie(str_cookieName, str_value, expire) 
{
  var str_exp;
  if(expire)
    str_exp = "; expires=" + expire.toGMTString()
  else
    str_exp = "";
  
  document.cookie = str_cookieName + "=" + 
                    escape(str_value) +
                    str_exp;
}


function scPopupManager()
{
  this.AddMsg       = scPopupManager_AddMsg;
  this.SetSeen      = scPopupManager_SetSeen;
  this.AddDisplay   = scPopupManager_AddDisplay;
  this.ShowMsg      = scPopupManager_ShowMsg;
  this.ClearMsgHist = scPopupManager_ClearMsgHist;
  this.SetMsgMode   = scPopupManager_SetMsgMode;
  this.GetMsgMode   = scPopupManager_GetMsgMode;
  
  this.arrDisplayList   = new Array;
  this.arrSeenIt        = new Array;
  this.arrSeenItLastVisit;
  this.CurrentIndex;
  this.MyPopupWindow = null;
  this.MsgMode = null;
  
  var str_SeenLast = imkGetCookie("SC_MSG_LIST");
  
  var str_mode = imkGetCookie("SC_MSG_MODE");

  if(str_mode == "")
  {
    // may be first time, try to set to default
    var today = new Date();
    var expires = new Date();
    expires.setTime(today.getTime() + 1000*60*60*24*365*2);
    imkSetCookie("SC_MSG_MODE", POP_MSG_MODE_NEW, expires);
    
    // if still null, cookies are disabled, set mode to "urgent"
    str_mode = imkGetCookie("SC_MSG_MODE");
    if(str_mode == "")
      str_mode = POP_MSG_MODE_URGENT;
  }
  
  this.MsgMode = parseInt(str_mode);
  this.arrSeenItLastVisit = str_SeenLast.split(",");
}

function scPopupManager_AddMsg(msgID, str_msgURL, isUrgent)
{
  if(this.MsgMode == POP_MSG_MODE_URGENT && isUrgent == false)
    return;
     
  var len = this.arrSeenItLastVisit.length;
  for(var i = 0; i < len; i++)
  {
    if(this.arrSeenItLastVisit[i] != "" &&
       this.arrSeenItLastVisit[i] == msgID)
    {
      this.SetSeen(msgID);
      return;
    }
  }
  
  this.AddDisplay(msgID, str_msgURL);
}

function scPopupManager_SetSeen(msgID)
{
  var len = this.arrSeenIt.length;
  for(var i = 0; i < len; i++)
  {
    if(this.arrSeenIt[i] == msgID)
      return;
  }
  
  this.arrSeenIt[len] = msgID;
   
  var today = new Date();
  var expires = new Date();
  expires.setTime(today.getTime() + 1000*60*60*24*365*2);
  imkSetCookie("SC_MSG_LIST", this.arrSeenIt, expires);
}

function scPopupManager_AddDisplay(msgID, str_msgURL)
{
  var len = this.arrDisplayList.length;
  for(var i = 0; i < len; i++)
  {
    if(this.arrDisplayList[i][POPUP_MSG_ID] == msgID)
      return;
  }
  
  this.arrDisplayList[len] = new Array;
  this.arrDisplayList[len][POPUP_MSG_ID] = msgID;
  this.arrDisplayList[len][POPUP_MSG_URL] = str_msgURL;
}

function scPopupManager_ShowMsg()
{
  var msgCount = this.arrDisplayList.length;
  if(msgCount > 0)
  {
    this.CurrentIndex = 0;
    var str_firstMsg = this.arrDisplayList[0][POPUP_MSG_URL];
    var popWin = window.open("../sc_app/sc_popupframe.asp?count=" + 
                             msgCount +
                             "&msgURL=" + escape(str_firstMsg),
                             "MessagePopup",
                             "HEIGHT=" + POPUP_WINDOW_HEIGHT +
                             ",WIDTH=" + POPUP_WINDOW_WIDTH);
    
    this.SetSeen(this.arrDisplayList[0][POPUP_MSG_ID]);
  }
  if(popWin)
    popWin.focus();
}

function scPopupManager_ClearMsgHist()
{
  imkSetCookie("SC_MSG_LIST", "");
}

function scPopupManager_SetMsgMode(msgMode)
{
  var today = new Date();
  var expires = new Date();
  expires.setTime(today.getTime() + 1000*60*60*24*365*2);
  imkSetCookie("SC_MSG_MODE", msgMode, expires); 
}

function scPopupManager_GetMsgMode()
{
  return imkGetCookie("SC_MSG_MODE"); 
}
