var js = {
  id: function(id) {
  	var obj = document.getElementById(id);
  	if(typeof obj == "undefined") notice('js.id: object id='+id+' undefined');
  	return obj;
  },

  randomString: function(length,prefix,charset) {
    if(length == null) length = 6;
    if(prefix == null) prefix = "x";
    if(charset == null) charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  	var result = "";
  	for(i=0; i<length; i++) {
  		var rnum = Math.floor(Math.random() * charset.length);
  		result+= charset.substring(rnum,rnum+1);
  	}
  	return prefix+result;
  },

  load: function() {
    if(document.getElementsByClassName) return js.cl = document.getElementsByClassName;
    js.cl = function(cls) {
      var retnode = [];
      var myclass = new RegExp('\\b'+cls+'\\b');
      var elem = this.getElementsByTagName('*');
      for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)) retnode.push(elem[i]);
      }
      return retnode;
    };

    var links = document.getElementsByTagName('a');
    for(var i=0; i<links.length; i++) {
      if(links[i].rel == '_blank') {
        links[i].onclick = function() {
            window.open(this.href);
            return false;
        };
      }
    }
  }

}


function popup(tgt,width,height) {
  var wwidth = parseInt((window.screen.width) - (width + 10));
  var wheight = parseInt((window.screen.height) - (height + 50));
  var win2 = window.open(tgt,"win2","status=no,height="+height+",width="+width+",resizable=yes,left="+wwidth+ ",top="+wheight+",screenX="+wwidth+ ",screenY="+wheight+",toolbar=1,menubar=1,scrollbars=1,location=0,directories=0");
  win2.focus();
  return true;
}

var select = {
  value: function(obj) {
    if(typeof obj == "string") obj = js.id(obj);
    return obj.options[obj.selectedIndex].value;
  }
};

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.sprintf = function() {
  if(!(arguments && arguments.length)) return null;
  var p,r;
  var result = this.valueOf();
  for(i=0; i<arguments.length; i++) {
    r = arguments[i];
    p = result.match(/%[sdf]/);
    if(p=="%s") result = result.replace("%s",r);
    if((p=="%d" || p=="%f") && isNaN(r)) r = 0;
    if(p=="%d") result = result.replace("%d",parseInt(r));
    else if(p=="%f") result = result.replace("%f",parseFloat(r));
  }
  return(result);
}

if(window.addEventListener) window.addEventListener("load", js.load, false);
else if(window.attachEvent) window.attachEvent("onload", js.load);

