
storage = {};

storage.hasSupport = function() {
  try {
    if( 'localStorage' in window && window['localStorage'] !== null ) {
      return true;
    }
  } catch (e) {
  }

  if( $.browser.msie ) {

    if( storage.persistNode ) {
      return true;
    }

    $('body').append('<div id="iePersistence" class="userData" style="behavior:url(#default#userData);"></div>');
    storage.persistNode = $('#iePersistence');
    storage.hasLoadedStore = false;

    storage.getValue = function (key) {
      return storage.getValueIe.apply(storage, [key]);
    }
    storage.setValue = function (key, val) {
      return storage.setValueIe.apply(storage, [key, val]);
    }
    storage.remove = function (key) {
      return storage.removeIe.apply(storage, [key]);
    }
    storage.clear = function () {
      return storage.clearIe.apply(storage, []);
    }
    return true;
  }

  if( !storage.hasSessionSupport() ) {
    storage.setUnsafeTempValue = function (key, val) {
      return storage.setUnsafeTempValueIe.apply(storage, [key, val]);
    }


    storage.getUnsafeTempValue = function () {
      return storage.getUnsafeTempValueIe.apply(storage, []);
    }
  }

  return false;
};

storage.hasSessionSupport = function() {
  try {
    if( 'sessionStorage' in window && window['sessionStorage'] !== null ) {
      return true;
    }
  } catch (e) {
  }
}

storage.getValue = function(key){
  return localStorage.getItem(key);
};

storage.setValue = function(key, val){
  localStorage.setItem(key, val);
};

storage.remove = function(key){
  localStorage.removeItem(key);
};

storage.clear = function(){
  if( storage.hasSupport() ) {
    localStorage.clear();
  }
};

storage.sessionClear = function(){
  if( storage.hasSessionSupport() ) {
    sessionStorage.clear();
  }
};

storage.setUnsafeTempValue = function(key, val) {
  sessionStorage.setItem(key, val ? utils.base64Encode(val) : "");
};


storage.getUnsafeTempValue = function(key) {
  var val = sessionStorage.getItem(key);
  return val ? atob(val) : val;
};


storage.isLoggedIn = function () {
  var loggedIn = "0";
  /* First check if localStorage is set to either 1 or 0, if not check for sessionStorage */
  if ( storage.hasSupport() ) {
    loggedIn = storage.getValue("isLoggedIn");
    if( storage.hasSessionSupport() && loggedIn != "1" && loggedIn != "0") { 
      loggedIn = storage.getUnsafeTempValue("isLoggedIn") == "1";
    }
  }
  return loggedIn == "1";
}

storage.getValueIe = function(key){
  if( !this.hasLoadedStore ) {
    this.persistNode[0].load("simple");
    this.hasLoadedStore = true;
  }
  return this.persistNode.attr(key);
};

storage.setValueIe = function(key, val){
  if( !this.hasLoadedStore ) {
    this.persistNode[0].load("simple");
    this.hasLoadedStore = true;
  }
  this.persistNode.attr(key, val);
  this.persistNode[0].save("simple");
};

storage.removeIe = function(key){
  if( !this.hasLoadedStore ) {
    this.persistNode[0].load("simple");
    this.hasLoadedStore = true;
  }
  this.persistNode.removeAttr(key);
  this.persistNode.save("simple");
};

storage.clearIe = function(){
  if( storage.hasSupport() ) {
    var nodes = this.persistNode[0].XMLDocument.selectNodes("/*/@*");
    var len = nodes.length;
    for( var i = 0; i < len; i++ ) {
      this.persistNode.removeAttr(nodes[i].name);
    }
    this.persistNode[0].save("simple");
  }
};



storage.setUnsafeTempValueIe = function(key, val) {
  utils.setCookie(key, val ? utils.base64Encode(val) : "");
};

storage.getUnsafeTempValueIe = function(key) {
  var val = utils.getCookie(key);
  return val ? atob(val) : val;
};


storage.tempApply = function(f, o) {
  if( window.name && window.name != "" && window.name.indexOf(":") != -1 ) {
    var arr = window.name.split(':');
    for( var i = 0, len = arr.length; i < len; i++ ) {
      arr[i] = atob(arr[i]);
    }
    return f.apply(o, arr);
  }
  return null;
};


storage.tempStore = function(u, t) {
  window.name = utils.base64Encode(u) + ':' + utils.base64Encode(t);
}
