var last_msg = new Object();
var jAlert = {
   show: function(title, text, class_name)
         {
            if(!title)
            {
               return;
            }

            if(!$('#ajax_alert').attr('id'))
            {
               $('<div id="ajax_alert"></div>').appendTo(document.body);
            }

            $('<div class="item ' + class_name + '"><h2>' + title + '</h2>' + text + '</div>')
               .prependTo($("#ajax_alert"))
               .fadeIn("slow", function()
               {
                  $(this).animate({opacity: 0.9}, 4000, function()
                  {
                     $(this).fadeOut(2000, function()
                     {
                        $(this).remove();
                     });
                  });
               })
               .click(function()
                           {
                              $(this).stop();
                              $(this).fadeOut("slow");
                           })

            last_msg = {title: title, text: text, class_name: class_name};

            if(!$("#alert_history_id").text())
            {

               $("(<a href='javascript:Alert.history()' class='ajax_alert'>Show Last Message</a>)")
                  .appendTo("#alert_history_id");
            }
         },

   history: function()
         {
            jAlert.show(last_msg.title, last_msg.text, last_msg.class_name);
         },

   Result: function(nObj)
   {
      if(nObj.msg.length < 1)
      {
         return;
      }

      if(nObj.level == "error")
      {
         jAlert.show("Error", nObj.msg, "error");
      }
      else if(nObj.level == "notice")
      {
         jAlert.show("Notice", nObj.msg, "notice");
      }
      else if(nObj.level == "success")
      {
         jAlert.show("Success", nObj.msg, "message");
      }
   }
}

function ClassCache()
{
   var cacheArr = new Array();

   this.Add = function(key, value)
   {
      cacheArr[key] = value;
   }

   this.Get = function(key)
   {
      if(!cacheArr[key])
      {
         alert("Variable '" + key + "' is not defined!");
      }

      if(cacheArr[key])
      {
         return cacheArr[key];
      }
   }

   this.Delete = function(key)
   {
      delete cacheArr[key];
   }

   this.Exists = function(key)
   {
      var retFlag = false;
      if(cacheArr[key])
      {
         retFlag = true;
      }

      return retFlag;
   }
}

function GetBack(nUrl)
{
   window.location.href = nUrl;
   return false;
}

function GetFilesize(size)
{
   var count = 0;
   var format = new Array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");

   while((size / 1024) > 1 && count < 8)
   {
      size = size / 1024;
      count++;
   }

   var decimals = 0;
   if( size < 10 )
   {
      decimals = 1;
   }

   var nRet = number_format(size, decimals, '.' ,' ') + " " + format[count];
   return nRet;
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

function string20bject(string)
{
   eval("var result = " + unescape(string));
   return result;
}

function string2Array(string)
{
   eval("var result = " + unescape(string));
   return result;
}
