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

  cl: function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = document.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
      var classes = elem[i].className;
      if(myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
  },
  
  child: function(obj,tag,pos) {
  	if(typeof obj == "string") obj = document.getElementById(obj);
	  if(!obj.childNodes) return null;
	  var j = 1;
	  var childs = [];
	  for(i in obj.childNodes) {
	    if(isNaN(parseInt(i))) continue;
	    var child = obj.childNodes[i];
	    if(child.nodeType==3 && child.nodeValue.trim().length==0) continue;
	    if(tag && child.nodeName!=tag.toUpperCase()) continue;
	    if(pos==j) return obj.childNodes[i];
	    else if(!pos) childs.push(obj.childNodes[i]);
	    j++;
	  }
	  if(pos) return null;
	  return childs;
  },
  
  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;
  },
  
  addEvent: function(obj,ev,fn,par) {
    if(!obj) return;
    var wrapper = function(e) {
      if(!e) e = window.event;
      var result = fn(obj,par,e);
      if(result===false) {
        if(event.preventDefault) event.preventDefault();
        event.returnValue = false;
      }
    }; 
  	if(obj.addEventListener) obj.addEventListener(ev, wrapper, false);
  	else if(obj.attachEvent) obj.attachEvent("on"+ev, wrapper);
  },

  setStyle: function(objs,data) {
    if(objs) if(objs.length) for(i in objs) js.setStyle1(objs[i],data);
    else js.setStyle1(objs,data);
  },
  setStyle1: function(obj,data) {
    for(i in data) obj.style[i] = data[i];
  },
  
  makeNode: function(tag,data) {
    var obj = document.createElement(tag);
    if(data) for(i in data) obj.setAttribute(i,data[i]);
    return obj;
  }

}

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() {
  var result = this.valueOf();
  if(!(arguments && arguments.length)) return result;
  var p,r;
  for(var i=0; i<arguments.length; i++) {
    p = result.match(/%[sdf]/);
    if(!p) return result;
    r = arguments[i];
    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));
    else if(p=="%s") result = result.replace("%s",r);
  }
  return result;
}

var is = {
  ie: document.all && !window.opera,
  ie6: document.all && !window.XMLHttpRequest,
  opera: window.opera && true,
  chrome: window.chrome && true
}

