top.requireJS("utils","message-area");
// function showMessageArea from message-area.js is used here

var cache = new Object();
function addCacheEntry(key, value)
{
   cache[key] = value;
}

var user = new Object();
function setUserProperty(key, value)
{
   user[key] = value;
}

function getUserData(key)
{
   return user[key];
}

function dump()
{
}

function isAuthorizedCheck(action, operation)
{
   var authorization = checkAuthorization(action, operation);
   if (!authorization)
   {
      //top.debug("authorization fialed on :"+action+", "+operation)
   }
   return authorization;
}


function isAuthorized(action, operation)
{
   var authorization = isAuthorizedCheck(action, operation);
   if (!authorization)
      top.showMessageArea(3000, 'You are not allowed to perform this operation');

   return authorization;
}

function checkAuthorization(action, operation)
{
//   debug('checkAuthorization[' + action + ', ' + operation + ']');

   var key;
   if (action)
   {
      if (operation)
      {
         key = action + '.' + operation + '.' + user.userType;
         if (cache[key] != null)
            return cache[key];

         key = action + '.' + operation + '.ALL';
         if (cache[key] != null)
            return cache[key];
      }

      key = action + '.' + user.userType;
      if (cache[key] != null)
         return cache[key];

      key = action + '.ALL';
      if (cache[key] != null)
         return cache[key];
   }

   key = 'ALL.' + user.userType;
   if (cache[key] != null)
      return cache[key];

   key = 'ALL.ALL';
   if (cache[key] != null)
      return cache[key];

   // We assume as default that if there is no information at all in the DB, the book is open to all
   return true;
}
