//var AssetName = new Array();

//https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
 Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/)
   {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0)
      return -1;

    var n = len;
    if (arguments.length > 1)
    {
      n = Number(arguments[1]);
      if (n !== n)
        n = 0;
      else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
    }

    var k = n >= 0
          ? Math.min(n, len - 1)
          : len - Math.abs(n);

    for (; k >= 0; k--)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }
    return -1;
  };
 
Array.prototype.filter = function(fn, obj) {
    for (var l = this.length, i = 0, a = []; i < l; i++) {
        fn.apply(obj, [this[i], i, this]) && a.push(this[i]);
    }
    return a;
}


Array.prototype.unique = function() {
    return this.filter(function(e, i, a) {
        return a.lastIndexOf(e) == i;
    });
}

function addSelectedAssets(id) {
    if (id) { selectedIds.length = 0; selectedIds.push(id); rowChecked(id, true); }
    if (selectedIds.length == 0) { alert(AssetResources.NoAssetSelected); return false; }
    selectedIds = selectedIds.unique();
    if ($('#selectmode').val().toLowerCase() == 'single' && selectedIds.length > 1) {
        id = selectedIds[0];
        var nameArray = selectedNames['id' + id];
        selectedNames = [];
        selectedNames['id' + id] = nameArray;
        selectedIds = [];
        selectedIds.push(id);
    }

    var result = new Array();
    result["selectedIds"] = selectedIds;
    result["selectedNames"] = selectedNames;    

    if (getURLParameter('msName') != null && getURLParameter('msName') != '') {
        result["msName"] = getURLParameter('msName');
    }

    if (self.callback == null) {
        if (getURLParameter('cbmode') != null && getURLParameter('cbmode') != '') {
            eval('opener.' + getURLParameter('cbmode') + '.assetsCallback(result)');
        } else {
            self.callback = opener.assetsCallback;
            self.callback(result);
        }
    } else {
        self.callback(result);
    }

    self.close();
    return false;
}

//function rowChecked(id, check) {
    
//    if ($('#an' + id).length > 0 && AssetName != null) {
//        AssetName['an' + id] = $.trim($('#an' + id).text());
//    }
//    return false;
//}


window.beforeApplyColumns = function() {
    if ($('#tb_mode').val() == 'tree') {
        var params = window.document.URL.toString().split('?')[1];
        params = params.replace('assetSelected', 'assetSelectedTree');
        window.location = base + 'asset.ivp/choose?root=true&mode=SELECTFROM&' + params;
        return false;
    }
    return true;
};