﻿//-----------------------------------------------------------------------------
// XClient
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

//=============================================================================
// Constructor

function XClient()
{

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   //--------------------------------------------------------------------------
   // Initialization

}

XClient.objectClass = "XClient";

//=============================================================================
// Static Interface

XClient.PERSISTENT_COOKIE = true;
XClient.SESSION_COOKIE = false;

XClient.document = document;
XClient.window = window;

//-----------------------------------------------------------------------------

XClient.valueOf = function()
{

   return "XClient";
}

//-----------------------------------------------------------------------------

XClient.getRootXClient = function()
{

   var rootWindow = document.parentWindow;
   while (rootWindow.parent)
   {
      if (parent == rootWindow)
         break;
      rootWindow = parent;
   }

   return rootWindow.XClient;
}

//-----------------------------------------------------------------------------

XClient.getTextArg = function(
   name,
   defaultValue,
   castAs
)
{
   defaultValue = (defaultValue == null) ? null : defaultValue.toString();
   castAs = (castAs == null) ? null : castAs;

   var textArg = null;
   var search = XClient.window.location.search;
   if (XMatch(search, "[\\?&]" + name + "=([^&]*)", "i"))
      textArg = decodeURIComponent(XMatch.matches[1]);
   if (textArg == null)
      textArg = XClient.getCookie(name);

   return (textArg == null) ? defaultValue : (castAs == null) ? XUtils.convert(textArg, defaultValue) : castAs(textArg);
}

//-----------------------------------------------------------------------------

XClient.getOptionArg = function(
   name,
   defaultValue,
   castAs
)
{
   defaultValue = (defaultValue == null) ? null : defaultValue.toString();
   castAs = (castAs == null) ? null : castAs;

   var option = XClient.getTextArg(name);
   option = (option == null || option.length == 0 || (/undefined/i).test(option) || (/null/i).test(option)) ? null : XString(option).toLowerCase();

   return (option == null) ? defaultValue : (castAs == null) ? XUtils.convert(option, defaultValue) : castAs(option);
}

//-----------------------------------------------------------------------------

XClient.getArg = function(
   name,
   defaultValue,
   castAs
)
{
   defaultValue = (defaultValue == null) ? null : defaultValue;
   castAs = (castAs == null) ? null : castAs;

   var arg = XClient.getTextArg(name);

   return (arg == null) ? defaultValue : (castAs == null) ? XUtils.convert(arg, defaultValue) : castAs(arg);
}

//-----------------------------------------------------------------------------

XClient.MAX_COOKIE_LENGTH = 500;

XClient.setCookie = function(
   name,
   cookie,
   persist
)
{
   cookie = (cookie == null) ? "" : cookie;
   persist = (persist == null) ? XClient.SESSION_COOKIE : persist;

   XApp.logEvent(XApp.EVENT_SET_COOKIE, name);

   if (typeof(cookie) == "object")
      cookie = XArray(cookie).toString();

   var cookieValues = [];
   if (cookie.length < XClient.MAX_COOKIE_LENGTH)
      cookieValues.push(cookie);
   else
   {
      for (var i=0; i<cookie.length; i+=XClient.MAX_COOKIE_LENGTH)
         cookieValues.push(cookie.substr(i,XClient.MAX_COOKIE_LENGTH));
   }

   var expires = (new Date((new Date()).getTime() + 3600000*24*365)).toGMTString();

   for (var i=0; i<cookieValues.length; i++)
   {
      var cookieName = name + ((i==0) ? "" : i);
      var cookieValue = escape(cookieValues[i]) + (((i+1) < cookieValues.length) ? "+>+>" : "");
      document.cookie = cookieName + "=;expires=" + (new Date("1/1/2000")).toGMTString() + ";"; // Delete any old cookie at a higher level
      document.cookie = cookieName + "=" + cookieValue + ";" + ((persist) ? " expires=" + expires  + ";" : "") + " path=/";
      if (document.cookie == "") // Cookies not accepted
      {
         var rootXClient = XClient.getRootXClient();
         if (rootXClient.auxCookies == null)
            rootXClient.auxCookies = "";
         rootXClient.auxCookies = (";" + rootXClient.auxCookies).replace(RegExp(";" + cookieName + "=" + "[^;]*", "gi"), "").replace(/^\;/,"");
         rootXClient.auxCookies += cookieName + "=" + cookieValue + ";";
      }
   }

}

//-----------------------------------------------------------------------------

XClient.getCookie = function(
   name,
   defaultValue,
   castAs
)
{
   defaultValue = (defaultValue == null) ? null : defaultValue;
   castAs = (castAs == null) ? null : castAs;

   //--------------------------------------------------------------------------

   function getCookieValue(
      cookies,
      name
   )
   {

      for (var i=0; i<cookies.length; i++)
      {
         var cookieText = cookies[i];
         if (XMatch(cookieText, "^\\s*" + name + "=(.*)$"))
            return XMatch.matches[1];
      }

      return null;
   }

   //--------------------------------------------------------------------------

   XApp.logEvent(XApp.EVENT_GET_COOKIE, name);

   try
   {
      var cookie = null;
      var cookieString = XClient.document.cookie;
      if (cookieString.length == 0)
      {
         var rootXClient = XClient.getRootXClient();
         if (rootXClient.auxCookies)
            cookieString = rootXClient.auxCookies;
      }
      var cookies = cookieString.split(";");
      for (var i=0; i<20; i++)
      {
         var cookieName = name + ((i==0) ? "" : i);
         var cookieValue = getCookieValue(cookies, cookieName);
         if (cookieValue != null)
            cookie = ((cookie != null) ? cookie : "") + cookieValue;
         if (!cookie || !(/\+>\+>$/).test(cookie))
            break;
         cookie = cookie.replace(/\+>\+>$/, "");
      }
   }
   catch (error)
   {
      // Do not log this as an error
   }

   if (cookie != null)
      cookie = decodeURIComponent(cookie).replace(/\+/g, " ");

   if ((/^\[/).test(cookie) && (/\]$/).test(cookie))
      cookie = XArray(cookie).toArray();

   return (cookie == null) ? defaultValue : (castAs == null) ? XUtils.convert(cookie, defaultValue) : castAs(cookie);
}

//-----------------------------------------------------------------------------

XClient.deleteCookie = function(
   name
)
{

   try
   {

      XClient.document.cookie = name + "=Deleting; expires=Thu, 01-Jan-1980 00:00:01 GMT; path=/";

   }
   catch (error)
   {
      // Do not log this as an error
   }

}

//-----------------------------------------------------------------------------

XClient.reportMsg = function(
   error
)
{
   error = (error == null) ? "Unspecified error." : error;

   XMsg.report(error);

}

//-----------------------------------------------------------------------------

XClient.write = function(
   text
)
{
   text = (text == null) ? null : text.toString();

   if (XString(text).isSomething())
      XClient.document.write(text);

}

//-----------------------------------------------------------------------------

var m_loggingEvent = false;

XClient.logEvent = function(
   eventName,
   data1,
   data2,
   context
)
{
   eventName = (eventName == null) ? XApp.EVENT_UNKNOWN : eventName;
   data1 = (data1 == null) ? null : data1;
   data2 = (data2 == null) ? null : data2;
   context = (context == null) ? XApp.getCallingContext() : context;

   if (XApp.DEBUG)
   {
      if (data1 && typeof(data1) == "string" && (/LogEvent.asp/).test(data1))
         return;

      if (eventName == "error")
      {
         data1 = (typeof(data1) != "string") ? XMsg.getMsgText(data1) : data1;
         if ((/out of stack space/i).test(data1))
            debugger;
      }

      // Don't get in a loop logging an event while logging an event
      if (m_loggingEvent)
         return;

      try
      {
         m_loggingEvent = true;
         var logEventURL = "";
         logEventURL += "/app/pkgs/xfw/core/LogEvent.asp";
         logEventURL += "?eventName=" + encodeURIComponent(eventName);
         logEventURL += (XString(data1).isSomething()) ? "&data1=" + encodeURIComponent(data1) : "";
         logEventURL += (XString(data2).isSomething()) ? "&data2=" + encodeURIComponent(data2) : "";
         logEventURL += "&context=" + encodeURIComponent(context);
         var recordXDoc = XDoc(logEventURL);
      }
      catch (error)
      {
         throw XMsg("Cannot log event on client. Calling server failed.", error);
      }
      finally
      {
         m_loggingEvent = false;
      }
   }

}

//-----------------------------------------------------------------------------

XClient();

//=============================================================================

