//
// updateEyepiece
//
function updateEyepiece() {
  var v = document.ep_calc_form.eyepiece.options[document.ep_calc_form.eyepiece.selectedIndex].value;
  if (v == "") {
    document.ep_calc_form.ep_focal_len.value = "";
    document.ep_calc_form.ep_apparent_field.value = "";
  } else {
    document.ep_calc_form.ep_focal_len.value = v.substr(0, v.indexOf(","));
    document.ep_calc_form.ep_apparent_field.value = v.substr(v.indexOf(",") + 1);
  }
}

//
// eyepieceCalc
//
function eyepieceCalc() {
  var tfl, ta, tfr;
  var efl, eaf;
  var mag, tf, ep;
  var invalid;
  
  tfl = parseFloat(document.ep_calc_form.tel_focal_len.value);
  ta = parseFloat(document.ep_calc_form.tel_aperture.value);
  tfr = parseFloat(document.ep_calc_form.tel_focal_ratio.value);
  efl = parseFloat(document.ep_calc_form.ep_focal_len.value);
  eaf= parseFloat(document.ep_calc_form.ep_apparent_field.value);
  
  if (document.ep_calc_form.tel_focal_len.value != "" && isNaN(tfl)) {
    alert("La distancia focal del telescopio no es valida.");
    return;
  }
  if (document.ep_calc_form.tel_aperture.value != "" && isNaN(tfl)) {
    alert("La apertura del telescopio no es valida.");
    return;
  }
  if (document.ep_calc_form.tel_focal_ratio.value != "" && isNaN(tfl)) {
    alert("La relacion focal del telescopio (f/#) no es valida.");
    return;
  }
  
  if (document.ep_calc_form.ep_focal_len.value == "") {
    alert("Introduzca la distancia focal del ocular.");
    return;
  }
  else if (isNaN(efl)) {
    alert("La distancia focal del ocular no es valida.");
    return;
  }
  if (document.ep_calc_form.ep_apparent_field.value == "") {
    alert("Introduzca el campo aparente del ocular.");
    return;
  }
  else if (isNaN(eaf)) {
    alert("El campo aparente del ocular es incorrecto.");
    return;
  }
  
  if (isNaN(tfl)) {
    tfl = ta * tfr;
  } else if (isNaN(ta)) {
    ta =  tfl / tfr;
  } else if (isNaN(tfr)) {
    tfr = tfl / ta;
  }
  
  mag = tfl / efl;
  tf = eaf / mag;
  ep = ta / mag;
  
  document.ep_calc_form.tel_focal_len.value = tfl;
  document.ep_calc_form.tel_aperture.value = ta;
  document.ep_calc_form.tel_focal_ratio.value = tfr;
  
  document.ep_calc_form.magnification.value = Math.round(mag);
  document.ep_calc_form.true_field.value = tf.toString().substring(0,5);
  document.ep_calc_form.exit_pupil.value = ep.toString().substring(0,5);
}


//
// updateCCD
//
function updateCCD() {
  var v = document.ccd_calc_form.ccd.options[document.ccd_calc_form.ccd.selectedIndex].value;
  var ps, cs;
  
  if (v == "") {
    document.ccd_calc_form.pixel_width.value = "";
    document.ccd_calc_form.pixel_height.value = "";
    document.ccd_calc_form.chip_width.value = "";
    document.ccd_calc_form.chip_height.value = "";
  } else {
 
 var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
 
    ps = v.substr(0, v.indexOf(","));
    cs = v.substr(v.indexOf(",") + 1);
    ps = ps.replace(/([0-9]+)\s?x\s?([0-9]\.[0-9]+).*/, "$1 $2");
    cs = cs.replace(/([0-9]+)\s?x\s?([0-9]+).*/, "$1 $2");

    document.ccd_calc_form.pixel_width.value = ps.substr(ps.indexOf(" ") + 1);
    document.ccd_calc_form.pixel_height.value = ps.substr(0, ps.indexOf(" "));
    document.ccd_calc_form.chip_width.value = cs.substr(cs.indexOf(" ") + 1);
    document.ccd_calc_form.chip_height.value = cs.substr(0, cs.indexOf(" "));
  }
}

//
// ccdCalc
//
function ccdCalc() {
  var pw, ph, cw, ch;
  var arcWidth, arcHeight;
  var pixelSize;
  var tfl;
  var binning;
  var reducer;
  
  tfl = parseFloat(document.ccd_calc_form.focal_len.value);
  pw = parseFloat(document.ccd_calc_form.pixel_width.value);
  ph = parseFloat(document.ccd_calc_form.pixel_height.value);
  cw = parseFloat(document.ccd_calc_form.chip_width.value);
  ch = parseFloat(document.ccd_calc_form.chip_height.value);
  binning = parseFloat(document.ccd_calc_form.binning.value);
  reducer = parseFloat(document.ccd_calc_form.reduce_extend.value);
  
  if (isNaN(tfl)) {
    alert("La distancia focal del telescopio no es valida.");
    return;
  }
  if (isNaN(pw)) {
    alert("El valor de anchura del pixel no es valido.");
    return;
  }
  if (isNaN(ph)) {
    alert("El valor de altura no es valido.");
    return;
  }
  if (isNaN(cw)) {
    alert("La anchura del chip no es valida.");
    return;
  }
  if (isNaN(ch)) {
    alert("La altura del chip no es valida.");
    return;
  }

  fovHeight = FOVSingleDimension(ph, ch);
  fovWidth  = FOVSingleDimension(pw, cw);
  
 // document.ccd_calc_form.field_of_view.value = (Math.round(fovHeight *100)/100) + '\' x ' + (Math.round(fovWidth *100)/100) + '\'';
  document.ccd_calc_form.field_of_view_h.value = (Math.round(fovHeight *100)/100);
  document.ccd_calc_form.field_of_view_w.value = (Math.round(fovWidth *100)/100);
  
  if (!isNaN(binning)) {
    pw *= binning;
    ph *= binning;
  }
  
  fovHeight = FOVSingleDimension(ph, ch);
  fovWidth  = FOVSingleDimension(pw, cw)
  
  arcHeight = fovHeight * 60/ch;
  arcWidth  = fovWidth * 60/cw;
  pixelSize = (arcHeight + arcWidth)/2;

  document.ccd_calc_form.arc_seconds.value = Math.round(pixelSize *100)/100;
}


//
// FOVSingleDimension
//
function FOVSingleDimension(pixsize, pixlength)
{
  var reducer;
  reducer = parseFloat(document.ccd_calc_form.reduce_extend.value);
  focalLength = parseFloat(document.ccd_calc_form.focal_len.value);

  if (isNaN(reducer))
    reducer = 1;

  if (parseInt(reducer) <= 0)
    return 0;

  fovsingle = parseFloat(2 * Math.atan(((pixsize*pixlength)/1000/2)/(focalLength * reducer)));
  fovsingle = parseFloat(fovsingle * 180/Math.PI);

  return Math.round(fovsingle * 60 * 100)/100; 
}
