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

  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);
  },

  title: function(obj,txt) {
    if(typeof obj == "string") obj = js.id(obj);
    if(!obj) return;
    if(obj.value==txt) obj.value = "";
    if(obj.value.trim()=="") {
      obj.className+= " entitled";
      obj.value = txt;
    }
    var focusfunc = function(obj) {
      if(obj.className.indexOf("entitled")!=-1) {
        obj.value = "";
        obj.className = obj.className.substr(0,obj.className.length-9);
      }
    };
    var blurfunc = function(obj) {
      if(obj.value.trim()=="") {
        obj.className+= " entitled"; obj.value = txt;
      }
    };
    var submitfunc = function(frm,o) {
      if(o.className.indexOf("entitled")!=-1) o.value = "";
    };
    js.addEvent(obj,"focus",focusfunc);
    js.addEvent(obj,"blur",blurfunc);
    if(obj.form) js.addEvent(obj.form,"submit",submitfunc,obj);
  },

  returnCalendar: function(tgt, y, m, d) {
    if(window.opener) {
      var fd = window.opener.document.getElementById(tgt+"_d");
      var fm = window.opener.document.getElementById(tgt+"_m");
      var fy = window.opener.document.getElementById(tgt+"_y");
      fd.selectedIndex = d-1;
      fm.selectedIndex = m-1;
      for(i in fy.options) if(fy.options[i] && fy.options[i].value==y) break;
      fy.selectedIndex = i;
      self.close();
    }
  },

  check_int: function(obj_id,min,max) {
    var obj = obj_id;
  	if(typeof obj == "string") obj = js.id(obj);
    var tmpval = obj.value.trim();
    if(tmpval=="-") return true;
    tmpval = parseInt(tmpval);
    if(isNaN(tmpval)) tmpval = "";
    if(typeof min != "undefined") if(tmpval<min) tmpval = min;
    if(typeof max != "undefined") if(tmpval>max) tmpval = max;
    obj.value = tmpval;
    return true;
  },
  
  flip: function(cond,obj) {
    obj.style.display = cond ? "inline" : "none";
  }
  
}

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, '');
}

