 // Some basic javascript validation codes and so on


  // ==== Validity checking ====
  // Is this an integer?
function isInteger(value) {
  return(value == parseInt(value));
}
  // Is this a float?
function isFloat(value) {
  return(value == parseFloat(value));
}
  // Is this within some range?
function inRange(value, min, max) {
  if((value<min) || (value>max) || !isFloat(value)) {
    alert("Must be between "+min+" and "+max);
  }
  return( !(value<min) || (value>max) );
}
  // Are there between min and max responses (inclusive)
function numResponse(butname, min, max) {
  if(!butname) return(min==0);  // No buttons! OK if min==0
  var numresp=0;
  if(!butname[0]) {
    if(butname != "") numresp = 1;
  } else {
    for(var i=0; i<butname.length; i++) {
      if(butname[i].checked) numresp = numresp+1;
    }
  }
  var chk = (numresp>=min) && (numresp<=max);
  if(!chk) {
    if(min == max) {
      alert("Please select "+min+" options.");
    } else {
      alert("Please select between "+min+" and "+max+" options.");
    }
  }
  return(chk);
}

  // ==== Modifying form parameter values ====
function setParam(parname, val) {
  if(!parname) return; // No such parameter. Error
  parname.value = val;
  return;
}
  
  // ==== Dynamic images ====
function imgSwch(labl, img) {
  if(document.images[labl]) {
    if(document.images[labl].complete == true) {
      document.images[labl].src = img;
      return(0);
    } else {
      return(2);
    }
  }
  return(1);
}

  // ==== Icon Image ====
function iconImg(dsc) {
  img = "/pic/goobs/hdr/vp_" + dsc + ".jpg";
  labl = "goobicon";
  ret = imgSwch(labl, img);
     // If ret==2, then try again in 1 second
  if(ret == 2) {
    setTimeout("iconImg('"+dsc+"')", 1000);
  }
}

  // ==== Select list handling ====
function elementValue(elenm) {
  val = "";
  var ele;
  ele = eval(elenm);
  gotone = false;
  for(var i=0; i<ele.options.length; i++) {
    if(ele.options[i].selected) {
      if(gotone) val += " ";
      val += ele.options[i].value;
      gotone = true;
    }
  }
  return(val);
}

  // ==== Popping up new windows ====
var popup;
function popupWin(url, width, height) {
  popup = window.open(url, "goob_popup", 
	"width="+width+",height="+height+",status=no,toolbar=no,menubar=yes,scrollbars=yes");
}
function killPopupWin() {
  if(popup) {
    if(!popup.closed) popup.close();
  }
}
  // Different popup window for the "Help" window
var helpPopup;
function popupHelpWin(url, width, height) {
  helpPopup = window.open(url, "goob_help", 
	"width="+width+",height="+height+",status=no,toolbar=no,menubar=yes,scrollbars=yes");
}
function killPopupHelpWin() {
  if(helpPopup) {
    if(!helpPopup.closed) helpPopup.close();
  }
}
  // Different popup window for the "Working" window
var workPopup;
function popupWorkWin(url, width, height) {
  workPopup = window.open(url, "goob_work", 
	"width="+width+",height="+height+",status=no,toolbar=no,menubar=no");
}
function killPopupWorkWin() {
  if(workPopup) {
    if(!workPopup.closed) workPopup.close();
  }
}

  // ==== Date and time calculations ====

  // Calculate MJD given dd/mm/yyyy (ignore hr/min)
function cal2mjd(dy, mn, yr) {
  with(Math) {
    gcor = 1;
    if(yr < 1582) gcor = 0;
    if(yr <= 1582 && mn < 10) gcor = 0;
    if(yr <= 1582 && mn == 10 && dy < 5) gcor = 0;
    jd = -1 * floor(7 * (floor((mn+9) / 12) + yr) / 4);
    scor = 1;
    if((mn-9)<0) scor = -1;
    afac = abs(mn-9);
    j1 = floor(yr + scor * floor(afac/7));
    j1 = -1 * floor((floor(j1/100) + 1) * 3 / 4);
    jd = jd + floor(275 * mn / 9) + dy + (gcor*j1);
    jd = jd + 1721027 + 2 * gcor + 367 * yr - 1.0;
    mjd = jd - 2400000;  // Ignoring fractions of day, so take 12noon
  }
  return(mjd);
}

  // Calculate MJD given dd/mm/yyyy hh:mm:ss
function cal2mjdf(dy, mn, yr, hr, minu, sec) {
  with(Math) {
    mjdmid = cal2mjd(dy,mn,yr);
    nhrs = hr + (minu / 60.0) + (sec / 3600.0);
    mjd = mjdmid + (nhrs/24.0);
  }
  return(mjd);
}
