function pad_2(num_string) {
  if(num_string.length >= 2) {
    return num_string;
  } else {
    return "0"+num_string;
  }
}
function days_in_month(year, month) {
  month--; // To get 0-11 months
  month++; // Because we want to look at the next month!
  
  if(month>11) {
    month=0;
    year++;
  }
  start_of_next_month = new Date(year, month);
  end_of_this_month = new Date(start_of_next_month.getTime() - (1000*60*60*12));
  return end_of_this_month.getDate();
}
function set_max_day_from_value(name) {
  max_day=days_in_month(
    document.getElementById(name+".y").value, 
    document.getElementById(name+".m").value
  );
  for(i = 28; i<max_day+1; i++) {
    document.getElementById(name+".d."+i).disabled=false;
  }
  for(i = max_day+1; i<32; i++) {
    document.getElementById(name+".d."+i).disabled=true;
  }
  if(document.getElementById(name+".d").value > max_day)
    document.getElementById(name+".d").value = max_day;
}
function update_hidden_date(name) {
  document.getElementById(name+".h").value =
    document.getElementById(name+".y").value + "-" +
    pad_2(document.getElementById(name+".m").value) + "-" + 
    pad_2(document.getElementById(name+".d").value) +
    document.getElementById(name+".t").value;
}
function update_hidden_date_ym(name) {
  document.getElementById(name+".h").value =
    document.getElementById(name+".y").value + "-" +
    pad_2(document.getElementById(name+".m").value);
}
function find_element_root_offset(element) {
	x=0; y=0;
  while(element) {
    x+=element.offsetLeft;
    y+=element.offsetTop;
    element=element.offsetParent;
  }
	return [x,y];
}
function overlay_near_element(target_element, move_element_id, approx_width, approx_height) {
	target_position = find_element_root_offset(target_element);
	x = target_position[0];
	y = target_position[1];
  divstyle=document.getElementById(move_element_id).style;
  if(x>approx_width/2) {x-=approx_width/2} else {x=0}
  if(y>approx_height/2) {y-=approx_height/2} else {y=0}
  divstyle.left=x+"px";
  divstyle.top=y+"px";
  divstyle.visibility='visible';
}
function try_refresh(time, url) {
  location.href=url;
  setTimeout("try_refresh(" + time + ", '" + url + "')",time*1000);
}
