var allIsChecked = false;

function somethingIsChecked() {
  var result = false;
  var x = 0;

  while (!result && (x <=7)) {
    result = document.multisource.source.options[x].selected;
    x++;
  }
  return result;
}

function checkAll() {
     var x;
     if ((document.multisource.source.options[0].selected) && ! allIsChecked) {
      for (x = 1;x <= 7;x++) 
        document.multisource.source.options[x].selected = false;
      allIsChecked = true;
    } else {
       var somethingElseIsChecked = false;
       x = 1;
       while ((! somethingElseIsChecked) && (x <= 7)) {
          somethingElseIsChecked = document.multisource.source.options[x].selected;
          x++;
       }
      if (somethingElseIsChecked) {
         document.multisource.source.options[0].selected = false;
         allIsChecked = false;
      }
    }
}

function startPage(freq, efreq) {
  var h = "";
  var x = 1;
  freq = toRealCenterFreq(freq);
  efreq = toRealCenterFreq(efreq);
  if (document.multisource.source.options[0].selected)
    for (x = 1;x <= 7;x++) 
      document.multisource.source.options[x].selected = true;
  for (x = 1;x <= 7;x++)
    if (document.multisource.source.options[x].selected) 
      h += document.multisource.source.options[x].value + ', ';
  h = createHeader(h + (Math.round((freq-0.3)*100)/100) + ' to ' 
                               + (Math.round((efreq+0.3)*100)/100) + 'GHz');
  return h;
}

function oneGraph(freq,efreq,w,h) {
  var filenames = '';
  var x = 0;
  var names = "";
  freq = toRealCenterFreq(freq);
  efreq = toRealCenterFreq(efreq);
  if (document.multisource.source.options[0].selected)
    for (x = 1;x <= 7;x++) 
      document.multisource.source.options[x].selected = true;
  for (x = 1;x <= 7;x++) 
    if (document.multisource.source.options[x].selected) {
      names += document.multisource.source.options[x].value + ", ";
      for (z = Math.round(freq*1000);z <= Math.round(efreq*1000); z += 550) {
       filenames += 'data/text/' + document.multisource.source.options[x].value + '.'
         + z + '.txt ';
      }
    }
   var applet = startAppletTag(names,h,w) + 
                paramTag("background","faf0e6") +
                paramTag("pxgraphargs","-t '" + names + (Math.round((freq-0.3)*100)/100) + " to " + 
                      (Math.round((efreq+0.3)*100)/100) + "GHz' " + "-x 'Frequency (GHz)' -y 'Temp K' " + filenames) +
                endAppletTag();

  if (document.multisource.source.options[0].selected)
    for (x = 1;x <= 7;x++) 
      document.multisource.source.options[x].selected = false;
   return applet;
}


function manyGraphs(source,startFreq,endFreq,width,appletwidth,appletheight) {
    var doneone = false;
    startFreq = Math.round(toRealCenterFreq(startFreq)*1000);
    endFreq = Math.round(toRealCenterFreq(endFreq)*1000);
    var header = createHeader(source + ' from ' + (startFreq-300)/1000 +
      ' to ' + (endFreq+300)/1000 + ' GHz');
    startFreq /= 1000;
    endFreq /= 1000;
    var numSpec = Math.round((endFreq-startFreq)/0.55) + 1;
    var filenames = '';
    if (width == -1) width = numSpec;
    var height = Math.floor(numSpec/width);
    numSpec -= width*height;
    var titletext = source + ' from ' + (Math.round(startFreq*1000)-300)/1000 + ' to '; 
    var applets = '';
    for(var x = 0; x < height;x++) {      
      applets += startAppletTag(source,appletheight,appletwidth);
      applets += paramTag("background","faf0e6");
      for(var y = 0; y < width; y++) {
        filenames += "data/text/" + source + '.' + Math.round(startFreq*1000) + '.txt ';
        startFreq += 0.55;
      }
      titletext += (Math.round((startFreq-0.55)*1000)+300)/1000
      applets += paramTag("pxgraphargs","-t '" + titletext + "'  -x 'Frequency (GHz)' -y 'Temp K' " + filenames);
      applets += endAppletTag();
      titletext = source + ' from ' + (Math.round(startFreq*1000)-300)/1000 + ' to ';
      filenames = '';
      doneone = true;
    }
    if (numSpec != 0) {
      filenames = '';
      if (doneone) {
        appletwidth -= 150;
        appletwidth = Math.round(appletwidth*(numSpec*0.6-(numSpec-1)*0.05)/(width*0.6-(numSpec-1)*0.05)) + 150;
      }
      applets += startAppletTag(source,appletheight,appletwidth);
      applets += paramTag("background","faf0e6");
      for(y = 0; y < numSpec; y++) {
        filenames += "data/text/" + source + '.' + Math.round(startFreq*1000) + '.txt ';
        startFreq += 0.55;
      }
      titletext += (Math.round((startFreq - 0.55)*1000)+300)/1000;
      applets += paramTag("pxgraphargs","-t '" + titletext + "'  -x 'Frequency (GHz)' -y 'Temp K' " + filenames);
      applets += endAppletTag();
    }

    return applets;
  
}


function multiSourceCheckAndCreate() {
  var centFreq = 1.0*document.multisource.centFreq.value;
  var endFreq = 1.0*document.multisource.endFreq.value;
  if (endFreq == 0) {
    endFreq = 1.0*centFreq;
  }
  var height = 1*document.multisource.height.value;
  var width = 1*document.multisource.width.value;
  var numfiles = 1*document.multisource.numBlocks.value;
  var allinone = false;

  if (numfiles <= 0) {
     allinone = true;
  }

  if (!somethingIsChecked()) {
    alert("Please select one or more sources.");
    document.multisource.source.focus();
  } else if (!inRange(centFreq)) {
    document.multisource.centFreq.focus();
    document.multisource.centFreq.select();
  } else if (!inRange(endFreq)) {
    document.multisource.endFreq.focus();
    document.multisource.endFreq.select();
  } else if (endFreq < centFreq) {
    alert("The beginning frequency must be less than the end frequency.");
    document.multisource.centFreq.focus();
    document.multisource.centFreq.select();
  } else  if ((height <= 0) || (width <= 0) || ((height%1) != 0) ||  ((width%1) != 0)) {
    alert("Please enter positive integer values for the height and width!");
    document.browser.height.focus();
  } else {
    if (document.multisource.layout.options[0].selected) {
      var doneone = false;
      centFreq = toRealCenterFreq(centFreq);
      endFreq = toRealCenterFreq(endFreq);
      var tempend = centFreq + (numfiles-1)*0.55;
      if (allinone) tempend = endFreq;
      var stufftoplot = true;
      var hder = startPage(centFreq,endFreq);
   //  var scps = scriptTags("JavaScript","HTML.js","myMath.js","MultiSource.js") + startBody();
      var mdl = "";
      while (stufftoplot) {
        if (tempend >= endFreq) {
          stufftoplot = false;
          if (doneone) {
            width -=150;
            width = Math.round((endFreq - centFreq + 0.35)/(tempend - centFreq + 0.1)*width);
            width += 150;
          }
          tempend = endFreq;
        }
      centFreq = toRealCenterFreq(centFreq);
      tempend = toRealCenterFreq(tempend);
        mdl += oneGraph(centFreq,tempend,width,height);
        //alert(mdl);
       centFreq = tempend+0.55;
       tempend = centFreq + (numfiles-1)*0.55;
       if (! doneone) doneone = true;
      }
      var fter = createFooter();
      document.open();
      document.writeln(hder);
      document.writeln(mdl);
      document.writeln(fter);
      document.close();
    } else {
      centFreq = toRealCenterFreq(centFreq);
      endFreq = toRealCenterFreq(endFreq);
      if (allinone) numfiles = -1;
      var hder = startPage(centFreq,endFreq);
     //var scps = scriptTags("JavaScript","HTML.js","myMath.js","MultiSource.js") + startBody();
      var mdl = "";
     if (document.multisource.source.options[0].selected)
        for (x = 1;x <= 7;x++) 
          document.multisource.source.options[x].selected = true;
     for (x = 1;x <= 7;x++)
        if (document.multisource.source.options[x].selected) {
          mdl += manyGraphs(document.multisource.source.options[x].value,centFreq,endFreq,numfiles,width,height);
      }
      var fter = createFooter();
     if (document.multisource.source.options[0].selected)
        for (x = 1;x <= 7;x++) 
          document.multisource.source.options[x].selected = false;
      document.open();
      document.writeln(hder);
      document.writeln(mdl);
      document.writeln(fter);
      document.close();
    }
  }
}

