/*
	summary:
		javascript functions that control majority of the page. Can get values from page, make the page
		grow/shrink elements, makes the HTTP requests and handles the output.
	author: Kim Walls

	copyright:
		(&copy;) Copyright 2006 by GATS Inc.
		11864 Canon Blvd., Suite 101, Newport News, VA 23606

		All Rights Reserved. No part of this software or publication may be
		reproduced, stored in a retrieval system, or transmitted, in any form
		or by any means, electronic, mechanical, photocopying, recording, or
		otherwise without the prior written permission of GATS Inc.
*/

//------------------------------global variables--------------------------------

//variables are used to make the http requests. each request has its own variable.
var get_gas_http = "";
var get_iso_http = "";
var graph_http = "";
var time_http = "";
var ps_http = "";
var saved_http = "";
var saved_http2 = "";
var saved_http3 = "";
var sp_http = "";

var ll_http = "";
var spectra_http = "";
//following variables are used to control the progress bar/div
var blocksize=(250-2)/100;
var loaded=0;

//var bgClr = "#f7f7f7;";
var bgClr = "#ffffff;";
//used to control user functionality if logged in/out
var bandpass_range = 100;
var remain_days = 1;
//-------------------------end global variables-------------------------------------
/*used to determine if user presses return key.
 *if so call calculate button on tool they are using.
 * from http://www.java-scripts.net/javascripts/Secret-Alert-Popup.phtml
  */
var nav=navigator.appName;
var ns=(nav.indexOf("Netscape")!=-1);

//determine action if user hits return/enter key
if(ns) {
 if(document.layers) {
  document.captureEvents(Event.KEYPRESS);
  document.onkeypress = getkey;
 }
}
else {
 document.onkeypress = getkey;
}

function getkey(keyStroke) {
 var eventChooser = (ns)?keyStroke.which: event.keyCode;
 if(eventChooser == '13') {
    //do nothing.
 }
}
function open_abund() {
 abWN = window.open ("abundances.php","abund_window","statusbar=0,location=0,menubar=0,scrollbars=1,resizable=1,width=850,height=500");
 abWN.focus();
}

function check_status(chk_func) {
 var user_status = get_user_status();
 var user_name = get_user_name();
 var user_id = get_user_id();
 var err_div = document.getElementById("error_div");
 var err_str = "";
 var error = "false";
 document.getElementById("login_err_div").innerHTML = "";
 if(user_status == "on") {
  if(chk_func == "GAS") {
    update_gases();
  }
  else if(chk_func == "CELL") {
    update_cells();
  }
  else if(chk_func == "DB_TBL") {
   populate_cell_gases();
  }
  else if (chk_func == "UNITS") {
   unit_chg();
  }
  err_div.innerHTML = "";
  return "no_error";
 } //end if user_status = on
 else if((user_status == "limited_reg") || (user_status == "off")) {
  var num_gases = document.getElementById("num_of_gases").value;
  var num_cells = document.getElementById("num_of_cells").value;
  var instr = document.getElementById("instr_sel").value;
  var line_pos = document.getElementById("ticks_box").checked;
  var gas_db = document.getElementById("calc_gas_db").value;
  var units = document.getElementById("calc_units").value;
  var log_lin = document.getElementById("log_lin_box").checked;
  var annt = document.getElementById("calc_annotate").checked;
  var ts = document.getElementById("calc_timestamp").checked;
  var err_kw = "users with an active subscription.<br>";
  var tran_or_rad = "";
  for(var a=0; a < 3; a++) {
  if(document.show_info_form.compute[a].checked) {
   tran_or_rad = document.show_info_form.compute[a].value;
  }
 } 
  
  if(gas_db != "hitran2004") {
   error = "true";
   err_str+= "Calculations involving databases other than HITRAN2004 are only available to " + err_kw;
   gas_db = document.getElementById("calc_gas_db").selectedIndex=1;
   populate_cell_gases();   
  }
  if(num_gases != "1")  {
   error = "true";
   err_str+= "Calculations involving more than 1 gas are only available to " + err_kw;
   document.getElementById("num_of_gases").selectedIndex=0;
   update_gases();
  }
  if(num_cells != "1")  {
   error = "true";
   err_str+= "Calculations involving more than 1 cell are only available to " + err_kw;
   document.getElementById("num_of_cells").selectedIndex=0;
   update_cells();   
  }
  if(tran_or_rad == "radiance")  {
   error = "true";
   err_str+= "Emission/radiance plots are only available to  " + err_kw;
   document.getElementById("compute").checked = true;
   //log_div_on_off();   
  }
  if(instr != "none")  {
   error = "true";
   err_str+= "Calculations involving instrument functions are only available to  " + err_kw;
   document.getElementById("instr_sel").selectedIndex=0;
  }
  if(line_pos == true)  {
   error = "true";
   err_str+= "Line intensities are only available to  " + err_kw;
   document.getElementById("ticks_box").checked = false;
  }
  if(units == "microns") {
   error = "true";
   err_str+= "Using units of microns are only available to " + err_kw;
   document.getElementById("calc_units").selectedIndex=0;
   unit_chg()
  }
  if(annt == true) {
   error = "true";
   err_str+= "The annotate feature is only available to " + err_kw;
   document.getElementById("calc_annotate").checked = false;
  }
  if(ts == true) {
   error = "true";
   err_str+= "The timestamp feature is only available to " + err_kw;
   document.getElementById("calc_timestamp").checked = false;
  }
  if(error == "true")  {
   document.getElementById("login_err_div").innerHTML = "";
   if(user_status == "limited_reg") {
    err_str+= "</font>Please login or <a href='../info/plans.php' target='_blank' class='logout_out' onmouseover=className='logout_over'; onmouseout=className='logout_out';>Subscribe/Renew</a><br><br>";
    err_div.innerHTML = "<font color='red'>Your subscription is inactive.<br>" + err_str;
   }
   else   {
    err_div.innerHTML = "<font color='red'>" + err_str + "</font> Please login or <a href='../info/plans.php' target='_blank'>Subscribe</a>";
   }
       return "error";
  }
  else {
   err_div.innerHTML = "";
       return "no_error";
  }
 } //end else user_status = limited_reg or off. 
} //end function check_status

//change the number of cells being displayed on the page according to the selected number of cells.
function update_cells() {
 var num_cells = document.getElementById("num_of_cells").value;
 var label_str = "";
 var press_str = "";
 var temp_str = "";
 var len_str = "";
 var vmr_str = "";
 var update_label = "";
 var update_press = "";
 var update_temp = "";
 var update_len = "";
 var update_vmr = "";
 if(num_cells != 1) {
 document.getElementById("copy_cell_div").className="cell_show";
 var copy_from = document.getElementById("copy_from");
 var copy_to = document.getElementById("copy_to");
 nullOptions(copy_from);
 nullOptions(copy_to);
 with (copy_from) {
  for (var z = 0; z <num_cells; z++)  {
   options[z] = new Option(z+1,z+1);
  }
 } //end with
  with (copy_to) {
  for (var z = 0; z <num_cells; z++)  {
   options[z] = new Option(z+1,z+1);
  }
   options[1].selected = true;
 } //end with
  document.getElementById("enb").className="cell_show";
  document.getElementById("cell1_label_div").className="cell_show";
 }
 else {
  document.getElementById("copy_cell_div").className="cell_hide"; 
  document.getElementById("enb").className="cell_hide";
  document.getElementById("cell1_label_div").className="cell_hide";
  document.getElementById("cell1_box").checked = true;
 }
 var vmr_td = document.getElementById("vmr_td");
 vmr_td.setAttribute("colspan",num_cells);
 for(var up = 2; up < 7; up++) {
  label_str = "cell" + up + "_label_div";
  press_str = "cell" + up + "_press_div";
  temp_str = "cell" + up + "_temp_div";
  len_str = "cell" + up + "_len_div";
  update_label = document.getElementById(label_str);
  update_temp =document.getElementById(temp_str);
  update_press = document.getElementById(press_str);
  update_len =document.getElementById(len_str);
  if(up <= num_cells) {
   update_label.className = "cell_show";
   update_press.className = "cell_show";
   update_temp.className = "cell_show";
   update_len.className = "cell_show";
  }
  else {
   update_label.className = "cell_hide";
   update_press.className = "cell_hide";
   update_temp.className = "cell_hide";
   update_len.className = "cell_hide";
  }
 }  //end for loop
 update_gases();
} //end function update_cells

//show/hide the information in each tab based upon which tab is selected.
function show_tab(cell_number) {
 var div_id_num = "";
 var div_id = "";
 var cell_tab = "";
 var cell_tab_div = "";
 var cell_label = document.getElementById("cell_label");
 var tab_array = new Array("observer_tab", "cell_tab", "source_tab", "options_tab", "saved_tab");
 var tab_desc = new Array("Observer", "Gas Cell Description", "Source", "Plot Options", "My Settings");
 var tab_on = new Array();
 tab_on[0] = new Image(85,28);
 tab_on[0].src = "../images/active_tab_obsv.png";
 tab_on[1] = new Image(85,28);
 tab_on[1].src = "../images/active_tab_cell.png";
 tab_on[2] = new Image(85,28);
 tab_on[2].src = "../images/active_tab_source.png";
 tab_on[3] = new Image(85,28);
 tab_on[3].src = "../images/active_tab_options.png";
 tab_on[4] = new Image(85,28);
 tab_on[4].src = "../images/active_tab_set.png";
 var tab_off = new Array();
 tab_off[0] = new Image(85,28);
 tab_off[0].src = "../images/inactive_tab_obsv.png";
 tab_off[1] = new Image(85,28);
 tab_off[1].src = "../images/inactive_tab_cell.png";
 tab_off[2] = new Image(85,28);
 tab_off[2].src = "../images/inactive_tab_source.png";
 tab_off[3] = new Image(85,28);
 tab_off[3].src = "../images/inactive_tab_options.png"; 
 tab_off[4] = new Image(85,28);
 tab_off[4].src = "../images/inactive_tab_set.png"; 
 for(var c=0; c < 5; c++) {
  div_id_num = "cell" + c + "_div";
  div_id = document.getElementById(div_id_num);
 // cell_tab = "cell" + c + "_tab";
 // cell_tab_div = document.getElementById(cell_tab);
  if(c == cell_number) {
   document.images[tab_array[c]].src = tab_on[c].src;
   div_id.className = "cell_show";
   if(c == 2) {
    var src_sel = document.getElementById("source_select");
    var src_div_err = document.getElementById("source_div_err");
    var src_div = document.getElementById("source_div");
    var tran_rad = "";
    for(var a=0; a < 3; a++) {
     if(document.show_info_form.compute[a].checked) {
     tran_rad = document.show_info_form.compute[a].value;
     }
    }
    if(tran_rad == "radiance") {
     src_div_err.className="cell_hide";
     src_div.className="cell_show";
     src_sel.disabled = false;
     source_change();
    }
    else {
     src_div_err.className="cell_show";
     src_div.className="cell_hide";
     src_sel.selectedIndex=0;
     src_sel.disabled = true;
    }
  } //end if c == 2
  else if(c == 4) {
   var user_status = document.getElementById("user_status").value;
   if(user_status == "on") {
    get_saved_calculations();
   }
   else {
    document.getElementById("saved_calc_div").innerHTML = "<font color='red'>Saved settings are only available to active subscribers.  Please login above, or <a href='../info/plans.php' target='_blank'>Subscribe</a><br>";
  } 
 } //end if c == 4
   cell_label.innerHTML = tab_desc[c];
 } //end if c = cell_number
 else {
  document.images[tab_array[c]].src = tab_off[c].src;
  div_id.className = "cell_hide";
 }
 } //end for loop
} //end show_tab

//show/hide gases based upon number of gases selected
function update_gases() {
 var gas_div_str = "";
 var gas_div = "";
 var vmr_div_str = "";
 var vmr_div = "";
 var gas_err_div_str = "";
 var gas_err_div = "";
 var num_cells = document.getElementById("num_of_cells").value;
 var num_gases = document.getElementById("num_of_gases").value;
 for(var ng=1; ng <=6; ng++) {
  gas_div_str = "cell_gas" + ng;
  gas_div = document.getElementById(gas_div_str);
  if(ng <= num_gases) {
   gas_div.className = "cell_show";
  }
  else {
   gas_err_div_str = "cell_gas" + ng + "_err";
   gas_err_div = document.getElementById(gas_err_div_str);
   gas_div.className = "cell_hide";
   gas_err_div.innerHTML = "";
  }
  for(var nc=1; nc<=6; nc++) {
   vmr_div_str = "cell" + nc + "_vmr_div" + ng;
   vmr_div = document.getElementById(vmr_div_str);
   if((nc <= num_cells) && (ng <= num_gases)) {
    vmr_div.className="cell_show";
   }
   else { 
    vmr_div.className="cell_hide";
   }
  }
 }
} //end function update_gases

//calls a php file to get a list of gases and isotopes to display in the gas/isotope menus.
 function populate_cell_gases() {
 var gas_db = document.getElementById("calc_gas_db").value;
 var params = "&table_name=" + gas_db + "&plot_type=calc";
 get_gas_http = getHTTPObject();
 try {
 get_gas_http.open("POST","../getListOfGases.php",true);
 }
catch (err) {
 get_gas_http.close;
 get_gas_http.open("POST","../getListOfGases.php",true);
 }
 get_gas_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 get_gas_http.setRequestHeader("Content-length", params.length);
 get_gas_http.setRequestHeader("Connection", "close");
 get_gas_http.onreadystatechange = handleGasList;
 get_gas_http.send(params);
} //end populate_cell_gases() function

//put the gas & isotope results in a select menu and put them in the correct spot on the page.
function handleGasList() {
if (get_gas_http.readyState == 4)  {
  var GasXMLDocument = get_gas_http.responseXML;
  var counter = 0;
  var mol_tag = GasXMLDocument.getElementsByTagName('molecules').item(0);
  var numMol = mol_tag.childNodes.length;
  var num_gases = GasXMLDocument.getElementsByTagName('num_gases').item(0).firstChild.data;
  var num_cells = document.getElementById("num_of_cells").value;
 //change the number of gases available in the "No. of Gases drop down menu.
  var no_gas_list = document.getElementById("num_of_gases");
  if(num_gases != no_gas_list.length) {
  nullOptions(no_gas_list);
  with (no_gas_list) {
   for (var z = 0; z < num_gases; z++) {
    var zp1 = parseInt(z) + 1;
    try {
    options[z] = new Option(zp1,zp1);
     }
    catch(err) { 
    }
   }
   options[0].selected = true;
  } //end with
} //end if num_gas != no_gas_list
  var gas_menu = "";
  var iso_menu = "";
  //populate the list of gases in the gas menus.
  for(var i=1; i<=num_gases; i++) {
   gas_menu = document.getElementById("gaslist_" + i);
   nullOptions(gas_menu);
   var temp_gas_id = "";
   var temp_gas_name = "";
   var temp_gas_formula = "";
   with (gas_menu){   
    for(var x=0; x<numMol; x++) {
     temp_gas_formula = mol_tag.getElementsByTagName("molecule").item(x).getAttribute("formula");
     temp_gas_id = mol_tag.getElementsByTagName("molecule").item(x).getAttribute("id");
     temp_gas_name = mol_tag.getElementsByTagName("molecule").item(x).firstChild.data;
     options[x] = new Option(temp_gas_formula,temp_gas_id);
     if(counter == x) {
      options[x].selected = true;    
     }
    } //end for x < num_gases_in_db
   } //end with    
    var iso_tag = GasXMLDocument.getElementsByTagName("isotope" + i).item(0);
    var numIso = iso_tag.childNodes.length;
    iso_menu = document.getElementById("isotope" + i);
    var iso_num = "";
    var iso_formula = "";
    var iso_out = "";
    nullOptions(iso_menu);
    with (iso_menu){
    options[0] = new Option("All", "0");
     for(var q=0; q < numIso; q++)
     {
      iso_num = iso_tag.getElementsByTagName("isotope").item(q).getAttribute("number");
      iso_formula = iso_tag.getElementsByTagName("isotope").item(q).firstChild.data;
      iso_out = iso_num + "   " + iso_formula;
      options[q+1] = new Option(iso_out, iso_out);    
     }
    } //end with that updates isotope menu.
    counter = counter + 1;
   } //end for i < num_gases
   update_gases();
 } //end if readystate = 4 
} //end function handleGasList


//this function copys the values from the selected cell and places them in the currently viewed cell.
function copy_cell() {
 var from_cell_num = document.getElementById("copy_from").value;
 var to_cell_num = document.getElementById("copy_to").value;
 var to_len_str = "cell" + to_cell_num + "_length";
 var from_len_str = "cell" + from_cell_num + "_length";
 var from_len= document.getElementById(from_len_str).value;
 var to_len = document.getElementById(to_len_str).value = from_len;
 var to_temp_str = "cell" + to_cell_num + "_temperature";
 var from_temp_str = "cell" + from_cell_num + "_temperature";
 var from_temp= document.getElementById(from_temp_str).value;
 var to_temp = document.getElementById(to_temp_str).value = from_temp;
 var to_press_str = "cell" + to_cell_num + "_pressure";
 var from_press_str = "cell" + from_cell_num + "_pressure";
 var from_press= document.getElementById(from_press_str).value;
 var to_press = document.getElementById(to_press_str).value = from_press;
 // var to_dopp_str = "cell" + to_cell_num + "_doppler";
 // var from_dopp_str = "cell" + from__cell_num + "_doppler";
 // var from_dopp= document.getElementById(from_dopp_str).value;
 // var to_dopp = document.getElementById(to_dopp_str).value = from_dopp;
 var num_gases = document.getElementById("num_of_gases").value;
 //this for loop counts the number of gases so we know how many VMR values to copy from the cell
 var from_cell_vmr_str = "";
 var from_cell_vmr = "";
 var to_cell_vmr_str = "";
 var to_cell_vmr = "";
 for (var ng = 1; ng <= num_gases; ng++) {
  from_cell_vmr_str = "cell" + from_cell_num + "_vmr" + ng;
  from_cell_vmr = document.getElementById(from_cell_vmr_str).value;
  to_cell_vmr_str = "cell" + to_cell_num + "_vmr" + ng;
  to_cell_vmr = document.getElementById(to_cell_vmr_str).value = from_cell_vmr;
 }
} //end function copy_cell

/*this function checks to make sure at least one cell is not ignored.
 * If at least one cell is selected, call the err_chk_calc function, if not display error. */
function check_ignore(chk_action) {
 var cb_str = "";
 var cb = "";
 var cb_count = 0;
 var num_cells_showing = document.getElementById("num_of_cells").value;
 for(var cbox=1; cbox<=num_cells_showing; cbox++) {
  cb_str = "cell" + cbox + "_box";
  cb = document.getElementById(cb_str).checked;
  if(cb == true) {
   cb_count = parseInt(cb_count) + 1;
  }
 } //end for
 if(cb_count > 0) {
  err_chk_calc(chk_action);
 }
 else {
  var dg = document.getElementById("error_div");
  dg.innerHTML =  "<br><font color='red'><b>ERRORS:</b><br>You must include at least one cell</font>";
 }
} //end function check_ignore

//this function gets all of the input variables, does error checking and calls a php file
function err_chk_calc(chk_action) {
 var user_status = get_user_status();
 var user_name = get_user_name();
 var user_id = get_user_id();
 document.getElementById("login_err_div").innerHTML = "";
 var err_str = "";
 var gas_db = document.getElementById("calc_gas_db").value;
 var num_gases = document.getElementById("num_of_gases").value;
 var num_cells = document.getElementById("num_of_cells").value;
 var total_num_gases = parseInt(num_gases) * parseInt(num_cells);
 var gas = "";
 var gas_str = "";
 var iso_str = "";
 var iso_sub = "";
 var iso = "";
 var llmt = document.getElementById("lower_bandpass").value;
 if(llmt.indexOf('+') != -1) {
  llmt = llmt.replace('+', "");
 }
 llmt = llmt *1;
 var ulmt = document.getElementById("upper_bandpass").value;
 if(ulmt.indexOf('+') != -1) {
  ulmt = ulmt.replace('+', "");
 } 
 ulmt = ulmt *1;
 var lower_limit = 0;
 var upper_limit = 0;
 var instr_width = 0;
 var instrument = document.getElementById("instr_sel").value;
 var i_w = document.getElementById("instr_width").value;
 if(i_w.indexOf('+') != -1) {
  i_w = i_w.replace('+', "");
 } 
 i_w = i_w *1;
 var tran_or_rad = "";
 for(var a=0; a < 3; a++) {
  if(document.show_info_form.compute[a].checked) {
   tran_or_rad = document.show_info_form.compute[a].value;
  }
 } 
 var up_min_low = 0;
 var lower_err = "none";
 var upper_err = "none";
 var lower_div = document.getElementById("lower_div");
 var upper_div = document.getElementById("upper_div");
 var width_err = "none";
 var width_div = document.getElementById("width_div");
 var err = "none";
 var gas_array = new Array();
 var iso_array = new Array();
 var gas_err = "none";
 var gas_div = "";
 var gas_err_array = new Array("0","none","none","none","none","none","none");
 var units = document.getElementById("calc_units").value;
 var src_sel = document.getElementById("source_select").value;
 if((tran_or_rad == "radiance") && (src_sel == "blackbody")) {
  var source_temp = document.getElementById("source_temp").value;
  if(source_temp.indexOf('+') != -1) {
   source_temp = source_temp.replace('+', "");
  }
  var source_emiss = document.getElementById("source_emissivity").value;
  if(source_emiss.indexOf('+') != -1) {
   source_emiss = source_emiss.replace('+', "");
  }
  if((parseFloat(source_temp) < 0.1 ) || (parseFloat(source_temp) >20000)) {
   document.getElementById("src_temp").innerHTML = "<font color='red'>X</font>Temperature";
   err_str+= "<br>Source temperature must be between 0.1K and 20000K";
   err = "error";
  }
  else if(isNaN(parseFloat(source_temp))) {
   document.getElementById("src_temp").innerHTML = "<font color='red'>X</font>Temperature";
   err_str+= "<br>Source temperature must be a numeric value";
   err = "error";
  }
  else {
   document.getElementById("src_temp").innerHTML = "Temperature";
  }
  if((parseFloat(source_emiss) <  0) || (parseFloat(source_emiss) >1)) {
   document.getElementById("src_emiss").innerHTML = "<font color='red'>X</font>Emissivity";
   err_str+= "<br>Source emissivity must be between 0 and 1";
   err = "error";
  }
  else if(isNaN(parseFloat(source_emiss))) {
   document.getElementById("src_emiss").innerHTML = "<font color='red'>X</font>Emissivity";
   err_str+= "<br>Source emissivity must be a numeric value";
   err = "error";
  }
  else {
   document.getElementById("src_emiss").innerHTML = "Emissivity";
  }
 } //end tran_or_rad == Radiance;
 if(units == "microns") {
  var m1 = 10000/parseFloat(ulmt);
  var m2 = 10000/parseFloat(llmt);
  lower_limit = m1;
  upper_limit = m2;
  var temp1 = (parseFloat(ulmt) + parseFloat(llmt))/2;
  var temp2 = (10000/Math.pow(parseFloat(temp1),2))*parseFloat(i_w);
  instr_width = parseFloat(temp2);
 }
 else  {
  lower_limit = llmt;
  upper_limit = ulmt;
  instr_width = i_w;  
 }
up_min_low = parseFloat(upper_limit) - parseFloat(lower_limit);
 if(instrument != "none") {
  if((parseFloat(instr_width) < 0) || (parseFloat(instr_width) > 250) || (parseFloat(instr_width) > (parseFloat(up_min_low)/2))) {
   err_str+= "<br>Instrument width too large. Must be:<br>";
   err_str+= "&nbsp;&nbsp;&nbsp;*between 0 and 250 cm<sup>-1</sup><br>";
   err_str+= "&nbsp;&nbsp;&nbsp;*no more than 1/2 the wave band.<br>";   
   width_div.innerHTML = "<font color='red'>X</font>&nbsp;Width";
   err = "error";
  }
  else {
   width_div.innerHTML = "Width";
  }
 } //end if instrument != none

 gas_err_array[1] = "none"; 
 var temp_div = "";
 var temp_str = "";
 for(var ng = 1; ng <=num_gases; ng++) {
  gas_str = "gaslist_" + ng;
  gas = document.getElementById(gas_str).value;
  iso_str = "isotope" + ng;
  iso_sub = document.getElementById(iso_str).value;
  iso = iso_sub.substring(0,1);
  gas_array[ng] = gas;
  iso_array[ng] = iso;
  for(var m=1; m < ng; m++) {
   if((gas_array[m] == gas) && (iso_array[m] == iso))
   {
    gas_err_array[ng] = "error";
    gas_err = "error";
    err = "error";
   } //end if
   else
   {
   gas_err_array[ng] = "none";
   }
  } //end for loop
  temp_str = "cell_gas" + ng + "_err";
  temp_div = document.getElementById(temp_str);
  if(gas_err_array[ng] == "error") {
   temp_div.innerHTML = "<font color='red'>X</font>";
  }
  else {
  temp_div.innerHTML = "";
  }
 } //end for loop ng=1;
 if(gas_err == "error") {
  err_str+= "<br>Duplicate Gas/Isotopologue combinations are not allowed<br>";
 }
 var temp_div = "";
 var temp_str = "";
 for(var ge=1; ge <=num_gases; ge++) {
  temp_str = "cell_gas" + ge + "_err";
  temp_div = document.getElementById(temp_str);
  if(gas_err_array[ge] == "error") {
   temp_div.innerHTML = "<font color='red'>X</font>";
  }
  else {
  temp_div.innerHTML = "";
  }
 } //end for loop
 //check the values of length, pressure, temperature, doppler, and vmr in each individual cell.
 var cell_cb = "";
 var cell_temp = 0;
 var cell_pressure = 0;
 var lowest_pressure = 0;
 var cell_length = "";
 var length_div = "";
 var press_div = "";
 var temp_div = "";
 var length_err = "";
 var press_err = "";
 var temp_err = "";
 var cell_vmr = "";
 var vmr_ttl = 0;
 var vmr_err_div = "";
 var vmr_err_str = "";
 var vmr_err = "none";
 var cell_cnt = 0;
 for(var cbox=1; cbox<=num_cells; cbox++) {
  length_err = "none";
  press_err = "none";
  temp_err = "none";
  vmr_err = "none";
  cell_cb = document.getElementById("cell" + cbox + "_box").checked;
  //if checkbox is checked--include the cell
  if(cell_cb == true) {
   cell_cnt = cell_cnt + 1;
   cell_temp = document.getElementById("cell" + cbox + "_temperature").value;
   if(cell_temp.indexOf('+') != -1) { 
    cell_temp = cell_temp.replace('+', "");
   } 
   cell_temp = cell_temp * 1;
   cell_pressure = document.getElementById("cell" + cbox + "_pressure").value;
   if(cell_pressure.indexOf('+') != -1) { 
    cell_pressure = cell_pressure.replace('+', "");
   } 
   if(parseFloat(lowest_pressure) == 0) {
     lowest_pressure = cell_pressure;
   }
   if(parseFloat(cell_pressure) < parseFloat(lowest_pressure)) {
    lowest_pressure = cell_pressure;
   }
   cell_pressure = cell_pressure * 1;
   cell_length = document.getElementById("cell" + cbox + "_length").value;
   if(cell_length.indexOf('+') != -1) {
    cell_length = cell_length.replace('+', "");
   }
   cell_length = cell_length * 1;
   if((parseFloat(cell_length) <  0.01) || (parseFloat(cell_length) > 100000000)) {
    err_str+= "<br>Cell" + cbox + " length has to be between 0.01 and 10^8";
    length_err = "error";
    err = "error";
   }
   if(isNaN(parseFloat(cell_length))) {
    err_str+= "<br>Cell" + cbox + " length has to be a numeric value";
    length_err = "error";
    err = "error";
   }
   if((parseFloat(cell_pressure) < 0.0000000001) || (parseFloat(cell_pressure) > 200000)) {
    err_str+= "<br>Cell" + cbox + " pressure has to be between 1e-10 and 200,000";
    press_err = "error";
    err = "error";
   }
   if(isNaN(parseFloat(cell_pressure))) {
    err_str+= "<br>Cell" + cbox + " pressure has to be a numeric value";
    press_err = "error";
    err = "error";
   }
   if((parseFloat(cell_temp) < 70) || (parseFloat(cell_temp) > 3000)) {
    err_str+= "<br>Cell" + cbox + " temperature has to be between 70 and 3000";
    temp_err = "error";
    err = "error";
   }
   if(isNaN(parseFloat(cell_temp))) {
    err_str+= "<br>Cell" + cbox + " temperature has to be a numeric value";
    temp_err = "error";
    err = "error";
   }
   //if any value contains an error put a red x next to that input field.
   length_div = document.getElementById("length_div" + cbox);
   press_div = document.getElementById("pressure_div" + cbox);
   temp_div = document.getElementById("temperature_div" + cbox);
   if(length_err == "error") {
    length_div.innerHTML = "<font color='red'>X</font>";
   }
   else {
    length_div.innerHTML = "&nbsp;&nbsp;";
   }
   if(press_err == "error") {
    press_div.innerHTML = "<font color='red'>X</font>";
   }
   else {
    press_div.innerHTML = "&nbsp;&nbsp;";
   }
   if(temp_err == "error") {
    temp_div.innerHTML = "<font color='red'>X</font>";
   }
   else {
    temp_div.innerHTML = "&nbsp;&nbsp;";
   }
   var vmr_array = new Array(num_gases);
   for(var cv = 1; cv <= num_gases; cv++) {
    vmr_array[cv] = "no_error";
    vmr_str = "cell" + cbox + "_vmr" + cv;
    cell_vmr = document.getElementById(vmr_str).value;
    if(cell_vmr.indexOf('+') != -1) {
     cell_vmr = cell_vmr.replace('+', "");
    }
    cell_vmr = cell_vmr * 1;
    vmr_ttl = parseFloat(vmr_ttl) + parseFloat(cell_vmr);
    if((parseFloat(cell_vmr) >1) || (parseFloat(cell_vmr) < 0) || (isNaN(parseFloat(cell_vmr)))) {
     vmr_array[cv] = "error";
     if(vmr_err == "none") {
      err_str+= "<br>VMR values in Cell" + cbox + " have to be between 0 and 1";
      vmr_err = "error";
      err = "error";
     }
    } //end if that checks individual vmr values.
   } //end for loop.
   if((vmr_ttl > 1) || (vmr_ttl <=0)) {
    err_str+= "<br>The sum of all VMR values in Cell" + cbox + " have to be greater than 0 and less than/equal to 1";
    err = "error";
    vmr_err = "error";
    for(var vt=1; vt <= num_gases; vt++){
     vmr_array[vt] = "error";
    }
   }
   for(var ve=1; ve <=num_gases; ve++) {
    vmr_err_str = "cell" + cbox + "_vmr" + ve + "_err";
    vmr_err_div = document.getElementById(vmr_err_str);
    if(vmr_array[ve] == "error") {
      vmr_err_div.innerHTML = "<font color='red'>X</font>";
    }
    else {
    vmr_err_div.innerHTML = "&nbsp;";
    }
   }
    vmr_err = "none";
    vmr_ttl = 0;
  } //end cell_cb = true
 } //end for loop

 if((remain_days < 1) && (user_status != "off")) {
  user_status = "limited_reg";
  check_status("ALL");
  bandpass_range = 100;
 }
 //figure out the bandpass range based on number of gases, pressure & lower wavenumber
if(user_status != "on") {
 bandpass_range = 100;
 if(gas_db != "hitran2004") {
  err = "error";
  err_str+= "<br>Calculations involving databases other than HITRAN2004 are only available to users with an active subscription.";
  gas_db = document.getElementById("calc_gas_db").selectedIndex=1;
  populate_cell_gases();
 }
}
else if(gas_db == "hitemp_2008") {
bandpass_range = 50;
}
else { //if user is active and not using hitemp, determine bandpass range.
 bandpass_range = 5000;
} //end else (user is active and not using hitemp)
 var min_diff = parseFloat(lower_limit)/10000;
if(lower_limit == 0) {
  lower_limit = 1e-6;
 }
 if((parseFloat(up_min_low) > parseFloat(bandpass_range))|| (parseFloat(up_min_low) < parseFloat(min_diff))) {
  if(gas_db == "hitemp_2008") {
   err_str+= "<br>Waveband intervals wider than 50 cm<sup>-1</sup> are not available for the HITEMP 2008 database.";
  } //end if db = hitemp
  else if(user_status != "on") {
   if(parseFloat(up_min_low) < parseFloat(min_diff)) {
    err_str+= "<br>(Upper Limit - Lower Limit) has to be greater than/equal to "+ min_diff + " cm<sup>-1</sup>";
   } //end if
   else {
    err_str+= "<br>Waveband intervals wider than 100 cm<sup>-1</sup> are only available with a subscription. <a href='../info/plans.php' target='_blank'>Subscribe/Renew</a>";
   } //end else 
  } //end else user_status != on
  else {
   err_str+= "<br>(Upper Limit - Lower Limit) has to be greater than/equal to " + min_diff + " cm<sup>-1</sup> and less than/equal to " + bandpass_range + " cm<sup>-1</sup>";
   err_str+="<br>Click <a href='../info/help.php#12' target='_blank'>here</a> for more information. ";
  } 
  lower_err = "error";
  upper_err = "error";
  err = "error";
 }
 if((parseFloat(lower_limit) < 0) || (parseFloat(lower_limit) > 60000)) {
  err_str+= "<br>Lower Limit has to be greater than/equal to 0 cm<sup>-1</sup> and less than/equal to 60000 cm<sup>-1</sup>";
  lower_err = "error";
  err = "error";
 } //end error checking for lower limit.
 if((parseFloat(upper_limit) < 0) || (parseFloat(upper_limit) > 60000)) {
  err_str+= "<br>Upper Limit has to be greater than/equal to 0 cm<sup>-1</sup> and less than/equal to 60000 cm<sup>-1</sup>";
  upper_err = "error";
 } //end error checking for lower limit.
 if(isNaN(parseFloat(upper_limit))) {
  err_str+= "<br>Upper Limit has to be a numeric value";
  upper_err = "error";
  err = "error";
 }
 if(isNaN(parseFloat(lower_limit))) {
  err_str+= "<br>Lower Limit has to be a numeric value";
  lower_err = "error";
  err = "error";
 }
  if(lower_err == "error") {
  lower_div.innerHTML = "<font color='red'>X</font>&nbsp;Lower Limit";
 }
 else {
  lower_div.innerHTML = "Lower Limit";
 }
  if(upper_err == "error") {
  upper_div.innerHTML = "<font color='red'>X</font>&nbsp;Upper Limit";
 }
 else {
  upper_div.innerHTML = "Upper Limit";
 }
 
 if(err == "none") {
  document.getElementById("error_div").innerHTML = "";
  create_xml(chk_action);
 } //end if err = none
 else {
  var dg = document.getElementById("error_div"); 
  var err_out = "<font color='red'><b>ERRORS:</b>";
  err_out+= err_str + "</font><br><br>";
  dg.innerHTML = err_out;
 }
} //end function err_chk_calc

function create_xml(chk_action) {
 var user_status = get_user_status();
 var user_name = get_user_name();
 var user_id = get_user_id();
 var gas_db = document.getElementById("calc_gas_db").value;
 var num_gases = document.getElementById("num_of_gases").value;
 var num_cells = document.getElementById("num_of_cells").value;
 var gas = "";
 var gas_str = "";
 var iso_str = "";
 var iso_sub = "";
 var iso = "";
 var out = "<calc_info>";
 var llmt = document.getElementById("lower_bandpass").value;
 if(llmt.indexOf('+') != -1) {
  llmt = llmt.replace('+', "");
 }
 var ulmt = document.getElementById("upper_bandpass").value;
 if(ulmt.indexOf('+') != -1) {
  ulmt = ulmt.replace('+', "");
 } 
 var lower_limit = 0;
 var upper_limit = 0;
 var instr_width = 0;
 var instrument = document.getElementById("instr_sel").value;
 var i_w = document.getElementById("instr_width").value;
 if(i_w.indexOf('+') != -1) {
  i_w = i_w.replace('+', "");
 }  
 var user_plan = document.getElementById("user_plan").value;
if(chk_action == "genXML" ) {
  if((user_plan.toUpperCase()) != "GRP_GATS") {
   err_str+= "<br>This feature is not allowed for your user.";
   err = "error";
  }
}
 out+="<which_exec>public</which_exec>";
 out+="<which_action>" + chk_action + "</which_action>";
 var tran_or_rad = "";
 for(var a=0; a < 3; a++) {
  if(document.show_info_form.compute[a].checked) {
   tran_or_rad = document.show_info_form.compute[a].value;
  }
 } 
 var units = document.getElementById("calc_units").value;
 var src_sel = document.getElementById("source_select").value;
 if(tran_or_rad == "transmission") {
   out+= "<compute>transmission</compute><log_lin>nolog</log_lin>";
 }
 else {
  out+= "<compute>" + tran_or_rad + "</compute>";
  var log_lin = document.getElementById("log_lin_box").checked;
  if(log_lin == true) {
   out+= "<log_lin>log</log_lin>";
  }
  else {
   out+= "<log_lin>nolog</log_lin>";
  }
 }
 if(tran_or_rad == "radiance") {
  if(src_sel == "user_blackbody") {
   out+= "<blackbody>user_blackbody</blackbody>";
   var source_temp = document.getElementById("source_temp").value;
   if(source_temp.indexOf('+') != -1) {
    source_temp = source_temp.replace('+', "");
   }
   if((parseFloat(source_temp) < 0.1 ) || (parseFloat(source_temp) >20000) || (isNaN(parseFloat(source_temp)))) {
    document.getElementById("src_temp").innerHTML = "<font color='red'>X </font>Temperature";
    err_str+= "<br>Source temperature must be between 0.1K and 20000K";
    error = true;
   }
   else {
    document.getElementById("src_temp").innerHTML = "Temperature";
   }
   var source_emiss = document.getElementById("source_emissivity").value;
   if(source_emiss.indexOf('+') != -1) {
    source_emiss = source_emiss.replace('+', "");
   }
   if((parseFloat(source_emiss) <  0) || (parseFloat(source_emiss) >1) || (isNaN(parseFloat(source_emiss)))) {
    document.getElementById("src_emiss").innerHTML = "<font color='red'>X </font>Emissivity";
    err_str+= "<br>Source emissivity must be between 0 and 1";
    error = true;
   }
   else {
    document.getElementById("src_emiss").innerHTML = "Emissivity";
   }
  out+= "<source_temp>" + source_temp + "</source_temp><source_dopp>0</source_dopp><source_emm>" + source_emiss + "</source_emm>";

  } //end if src_sel = user_blackbody
  else if(src_sel == "noblackbody") {
  out+= "<blackbody>noblackbody</blackbody>";
  }
  else {
  out+= "<blackbody>default</blackbody>";
  }
 } //end tran_or_rad == Radiance;
 else {
 out+= "<blackbody>noblackbody</blackbody>";
 } //end else.
 if(units == "microns") {
  if(llmt == 0) {
   out+= "<units>" + units + "</units><LL>1e-6</LL><UL>" + ulmt + "</UL>";
  }
  else {
   out+= "<units>" + units + "</units><LL>" + llmt + "</LL><UL>" + ulmt + "</UL>";
  }
  var m1 = 10000/parseFloat(ulmt);
  var m2 = 10000/parseFloat(llmt);
  lower_limit = m1;
  upper_limit = m2;
  var temp1 = (parseFloat(ulmt) + parseFloat(llmt))/2;
  var temp2 = (10000/Math.pow(parseFloat(temp1),2))*parseFloat(i_w);
  instr_width = parseFloat(temp2);
  out+= "<CLL>" + lower_limit + "</CLL><CUL>" + upper_limit + "</CUL><CIW>" + instr_width + "</CIW>"; 
 }
 else  {
  lower_limit = llmt;
  upper_limit = ulmt;
  instr_width = i_w;  
  if(lower_limit == 0) {
  lower_limit = 1e-6;
   out+= "<units>" + units + "</units><LL>1e-6</LL><UL>" + upper_limit + "</UL>";
  }
  else {
   out+= "<units>" + units + "</units><LL>" + lower_limit + "</LL><UL>" + upper_limit + "</UL>";
  }
 }
 out+= "<instr>" + instrument + "</instr><IW>" + i_w + "</IW>";
 out+= "<num_gases>" + num_gases + "</num_gases><db_table>" + gas_db + "</db_table>";
 var selInd = "";
 for(var ng = 1; ng <=6; ng++) {
  gas_str = "gaslist_" + ng;
  gas = document.getElementById(gas_str).value;
  iso_str = "isotope" + ng;
  iso_sub = document.getElementById(iso_str).value;
  iso = iso_sub.substring(0,1);
  selInd = document.getElementById(gas_str).selectedIndex;
   out+= "<gas iso='" + iso + "' selInd='" + selInd + "'>"+ gas + "</gas>";
 } //end for loop ng=1;

 //check the values of length, pressure, temperature, doppler, and vmr in each individual cell.
 var cell_cb = "";
 var cell_cb_str = "";
 var cell_temp = "";
 var cell_pressure = "";
 var cell_length = "";
 var cell_vmr = "";
 out+= "<num_cells>" + num_cells + "</num_cells>";
 for(var cbox=1; cbox<=6; cbox++) {
  cell_cb_str = "cell" + cbox + "_box";
  cell_cb = document.getElementById(cell_cb_str).checked;
  //if checkbox is checked--include the cell
   cell_temp = document.getElementById("cell" + cbox + "_temperature").value;
   if(cell_temp.indexOf('+') != -1) { 
    cell_temp = cell_temp.replace('+', "");
   } 
   cell_pressure = document.getElementById("cell" + cbox + "_pressure").value;
   if(cell_pressure.indexOf('+') != -1) { 
    cell_pressure = cell_pressure.replace('+', "");
   } 
   cell_length = document.getElementById("cell" + cbox + "_length").value;
   if(cell_length.indexOf('+') != -1) {
    cell_length = cell_length.replace('+', "");
   }
   out+= "<cell value='" + cell_cb + "' num='" + cbox + "'><cell_temp>" + cell_temp + "</cell_temp>";
   out+= "<cell_press>" + cell_pressure + "</cell_press><cell_length>" + cell_length + "</cell_length><cell_vmr>";
   for(var cv = 1; cv <= 6; cv++) {
    vmr_str = "cell" + cbox + "_vmr" + cv;
    cell_vmr = document.getElementById(vmr_str).value;
    if(cell_vmr.indexOf('+') != -1) {
     cell_vmr = cell_vmr.replace('+', "");
    }
    out+= cell_vmr;
    if(cv != 6) {
     out+= ":";
    }
   } //end for loop.
   out+= "</cell_vmr></cell>";
 } //end for loop

 var line_pos = document.getElementById("ticks_box").checked;
 if(line_pos == true) {
  out+= "<plot_lp>sticks</plot_lp>";
 }
 else {
  out+= "<plot_lp>nosticks</plot_lp>";
 }
 if(user_status == "on") {
 out+= "<user_status>on</user_status><user_name>" + user_name + "</user_name>";
 }
 else {
 out+= "<user_status>off</user_status><user_name>lim_user</user_name>";
 }
  var div1 = document.getElementById("progress_div");
  var remote_addr = document.getElementById("calc_remote_addr").value;
  var remote_host = document.getElementById("calc_remote_host").value;
  var user_agent = document.getElementById("calc_user_agent").value;
  var annotate = document.getElementById("calc_annotate").checked;
  if(annotate == true) {
   out+= "<annotate>annotate</annotate>";
  }
  else {
   out+= "<annotate>noannotate</annotate>";
  }
  var ts = document.getElementById("calc_timestamp").checked;
  if(ts == true) {
    out+= "<ts>timestamp</ts>";
  }
  else {
    out+= "<ts>notimestamp</ts>";
  }
  out+= "<r_addr>" + remote_addr + "</r_addr><r_host>" + remote_host + "</r_host><user_agent>" + user_agent + "</user_agent>";
  document.body.style.cursor = 'wait';
  document.getElementById("calculate_submit").disabled = true;
//  <a onClick=''>Cancel</a>
  var div_string = "<center><img src='../images/loading.gif'><br><font color='#004D8F' size='+1'><b>Calculating</b><br></font></center>";
//  div_string+="<a onClick='cancel_request();'>Cancel</a>";
  div1.innerHTML = "";
  div1.innerHTML = div_string;
  var start_now=new Date();
  out = "&calc_xml=" + out + "</calc_info>";
  graph_http = getHTTPObject();
  graph_http.open("POST", "get_plot.php", true);
  graph_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  graph_http.setRequestHeader("Content-length", out.length);
  graph_http.setRequestHeader("Connection", "close");
  graph_http.onreadystatechange = handle_plot;
  graph_http.send(out);
} //end function create_xml

function cancel_request() {
// window.alert("cancel_request");
 graph_http.abort();
 console.log("aborted");
  document.body.style.cursor = 'default';
  document.getElementById("calculate_submit").disabled = false;
}
//remove the progress bar and display the resulting plot.
function handle_plot() {
 if(graph_http.readyState == 4){ 
 var user_status = get_user_status();
 var user_name = get_user_name();
 var user_id = get_user_id();
 var end_now=new Date();
 document.body.style.cursor = 'default';
 document.getElementById("calculate_submit").disabled = false;
 document.getElementById("progress_div").innerHTML = "";
 var graph_div = document.getElementById("display_graph");
 var plot_xml = graph_http.responseXML;
 var plot_action = plot_xml.getElementsByTagName("which_action").item(0).firstChild.data;
 if(plot_action == "genXML") {
  var fn = plot_xml.getElementsByTagName('fn').item(0).firstChild.data;
  var fnstr = "xml_files/" + fn + ".xml";
  window.open(fnstr);

 } 
 else {
 var calc_err = plot_xml.getElementsByTagName('ERROR').item(0).firstChild.data;
 var fn = plot_xml.getElementsByTagName('fn').item(0).firstChild.data;
 var out_str = "";
 var time_key = plot_xml.getElementsByTagName('L_ID').item(0).firstChild.data;
 var annotate = plot_xml.getElementsByTagName('annotate').item(0).firstChild.data;
 var ts = plot_xml.getElementsByTagName('ts').item(0).firstChild.data;
 var ptype = plot_xml.getElementsByTagName('ptype').item(0).firstChild.data;
 var remind =  plot_xml.getElementsByTagName('remind').item(0).firstChild.data;
 var rnum =  plot_xml.getElementsByTagName('rnum').item(0).firstChild.data;
 if(calc_err == "none") {
  if(user_status == "on") {
   out_str = "<br><img src='plots/" + fn + ".png'><br><br>";
   remain_days = plot_xml.getElementsByTagName('remain_days').item(0).firstChild.data;
   var incl_ticks = plot_xml.getElementsByTagName('TICKS').item(0).firstChild.data;
   var log_nolog = plot_xml.getElementsByTagName('LOG').item(0).firstChild.data;
   var plot_db = plot_xml.getElementsByTagName('plot_db').item(0).firstChild.data;
   var plot_units = plot_xml.getElementsByTagName('plot_units').item(0).firstChild.data;
   var txt_size = plot_xml.getElementsByTagName('fsz').item(0).firstChild.data;
   var psanchor = "<font color='blue'><u><a id=mya onClick=\"plot_ps('ps', '";
   psanchor+= ptype + "', '"+ incl_ticks + "', '" + fn + "', '" + log_nolog + "','" + user_status + "','" + plot_db + "','" + plot_units + "','" + annotate + "','" + ts + "', '" + time_key + "'); return false;\"> ";
   var postscript_img = psanchor + "Generate Postscript</a></u></font>\n";
   var pdfanchor = "<font color='blue'><u><a id=mya onClick=\"plot_ps('pdf','" + ptype + "', '";
   pdfanchor+= incl_ticks + "', '" + fn + "', '" + log_nolog + "','" + user_status + "','" + plot_db + "','" + plot_units + "','" + annotate + "','" + ts + "', '" + time_key + "'); return false;\"> ";
   var pdf_img = pdfanchor + "Generate PDF</a></u></font>\n"; 
  if(plot_units == "microns") {
    out_str+= "<br>" + postscript_img + "&nbsp;&nbsp;&nbsp;&nbsp;" + pdf_img;
    out_str+= "&nbsp;&nbsp;&nbsp;&nbsp;<a href='plots/" + fn + "_microns.txt' target='_blank' onmouseover=\"popUp(event,'ascii')\" onmouseout=\"popUp(event,'ascii')\">Text File (" + txt_size + ")</a>\n";
   } //end units = microns 
   else {
   out_str+= "<br>" + postscript_img + "&nbsp;&nbsp;&nbsp;&nbsp;" + pdf_img;
   out_str+= "&nbsp;&nbsp;&nbsp;&nbsp;<a href='plots/" + fn + ".txt' target='_blank' onmouseover=\"popUp(event,'ascii')\" onmouseout=\"popUp(event,'ascii')\">Text File (" + txt_size + ")</a>\n";
   } //end else
    out_str+= "&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'><u><a onClick='window.open(\"../print.php?fn=" + fn + "&tool=calc&units=" + plot_units + "\")' onmouseover=\"popUp(event,'print')\" onmouseout=\"popUp(event,'print')\">Print</a></u></font>\n";
   out_str+= "&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'><u><a onClick=\"save_profile('save', '" + time_key + "');\">Save setup</a></u></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'><u>";
   out_str+= "<a onClick=\"save_spectra('save', '" + fn + "', '" + time_key  + "', '" + ptype +  "', 'calc');\">Save Spectra</a></u></font>";
   out_str+="<br><div id='prof_info_" + time_key + "'></div>";

   if(remain_days < 0) {
    document.getElementById("user_status").value = "limited_reg";
   } //end if
  } //end if user_status = on
  else {
   if(rnum >= 50) {
    document.getElementById("calculate_submit").disabled = true;
    document.getElementById("clear_graph").disabled = true;
    popUp2("daily_max", "spec_calc");
   }  //end if rnum >=50
   else {
    out_str = "<br><img src='plots/" + fn + ".png'><br><br>";
    out_str+= "<font color='blue'><u><a onClick='ps_acsii_err(\"Postscript images are\", \"" + time_key + "\");'>Generate Postscript</a></u></font>&nbsp;&nbsp;&nbsp;&nbsp;";
    out_str+= "<font color='blue'><u><a onClick='ps_acsii_err(\"PDF images are \", \"" + time_key + "\");'>Generate PDF</a></u></font>&nbsp;&nbsp;&nbsp;&nbsp;\n";
    out_str+= "&nbsp;&nbsp;<font color='blue'><u><a onClick='ps_acsii_err(\"ASCII data are\", \"" + time_key + "\");' onmouseover=\"popUp(event,'ascii')\" onmouseout=\"popUp(event,'ascii')\">Text File</a></u></font>\n";
    out_str+= "&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'><u><a onClick='ps_acsii_err(\"The print feature is\", \"" + time_key + "\");' onmouseover=\"popUp(event,'print')\" onmouseout=\"popUp(event,'print')\">Print</a></u></font>\n";
    out_str+= "&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'><u><a onClick=\"save_profile('save', '" + time_key + "');\">Save setup</a></u></font>";
    out_str+= "&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'><u><a onClick='ps_acsii_err(\"Saved spectra are\", \"" + time_key + "\");'>Save Spectra</a></u></font>"; 
    out_str+= "<br><div id='prof_info_" + time_key + "'></div>"; 
    if(remind == "yes") { 
    popUp2("remind", "spec_calc");
    document.getElementById("calculate_submit").disabled = true;
   // document.getElementById("reset_values").disabled = true;
    document.getElementById("clear_graph").disabled = true;
    } //end remind = yes
   } //end else  rnum < 50
  } //end else
    out_str+= "<br><span id='ps_ascii_div" + time_key + "'></span>";
  graph_div.innerHTML = out_str + "<BR><BR>" + graph_div.innerHTML ;
 } //end calc_err= none
 else if (calc_err == "no_lines") {
  out_str = "<br><br><font color='ff0000'><br><br>Error: No absorption lines were found in this spectral bandpass for these molecules.<br>Please choose different molecules or adjust your bandpass limits.<br><br></font>";
  graph_div.innerHTML = out_str + "<BR><BR>" + graph_div.innerHTML ;
 } //end else if calc_err = no_lines
 else if (calc_err == "too_many_pts") {
   var req_pts = plot_xml.getElementsByTagName('req_pts').item(0).firstChild.data;
   var allow_pts = plot_xml.getElementsByTagName('allow_pts').item(0).firstChild.data;
   out_str = "<br><br><font color='ff0000'><br><br>Error: Spectrum requires " + req_pts + " points, exceeding " + allow_pts + " point limit.<br>";
   out_str+="Please adjust waveband, pressure, or other parameters to reduce the spectrum size.<br>Click <a href='../info/help.php#29' target='_blank'>here</a> for more details.";
  graph_div.innerHTML = out_str + "<BR><BR>" + graph_div.innerHTML ;
 }//end else too_many_pts
}
}
}//end function handle_plot

function ps_acsii_err( which_link, pad_num ) {
 document.getElementById("ps_ascii_div" + pad_num).innerHTML = "<font color='red'>" + which_link + " only available to active subscribers. Please login above, or <a href='../info/plans.php' target='_blank'>Subscribe</a><br>";
} //end function ps_ascii_err

function handleTimeInsert() {
//do nothing.
}

function plot_ps(plot_type, comp, ticks_val, text_file, log_val, usr_status, plt_db, plt_units, annt, ts, pad_num) {
 var ps_params = "action=" + plot_type + "&compute=" + comp + "&lp=" + ticks_val+ "&log_lin=" + log_val + "&textfile=" + text_file;
 ps_params+= "&units=" + plt_units + "&use_db=" + plt_db + "&annotate=" + annt + "&ts=" + ts + "&pn=" + pad_num;
 if(usr_status == "on") {
 ps_params = ps_params  + "&logo=nologo";
 }
 else {
 ps_params = ps_params  + "&logo=logo";
 }
 document.getElementById("ps_ascii_div" + pad_num).innerHTML = "<center><img src='../images/loading.gif'>&nbsp;&nbsp;<b><font color='#004D8F' >Generating</font></b><br></center>";
 ps_http = getHTTPObject();
 ps_http.open("POST", "get_ps_pdf.php", true);
 ps_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 ps_http.setRequestHeader("Content-length", ps_params.length);
 ps_http.setRequestHeader("Connection", "close");
 ps_http.onreadystatechange = show_ps;
 ps_http.send(ps_params);
}

function show_ps() {
 if(ps_http.readyState == 4) {
  var ps_file = ps_http.responseText;
  var file_info = ps_file.split(':');
  var file_type = file_info[0];
  var file_name = file_info[1];
  var pad_num = file_info[2];
  var file_link = "plots/" + file_name;
  var outstr = "<a href='" + file_link + "' target='_blank'>" + file_type + " Image</a>";
  document.getElementById("ps_ascii_div" + pad_num).innerHTML = outstr;
 }
} //end show_ps function
function close_popup() {
 document.getElementById("calculate_submit").disabled = false;
// document.getElementById("reset_values").disabled = false;
 document.getElementById("clear_graph").disabled = false;
 popUp2("remind", "spec_calc");
} //end close popup

function close_popup_go_home() {
  popUp2("daily_max", "spec_calc");
  window.location = "../info/about.php";
} //end close popup go home



function load_prof(num_prof) {
 var p2 = "";
 for(var i=0; i < num_prof; i++)
 {
  p2 = document.getElementById("prof_rad" + i);
  if(p2.checked == true)
  {
   document.getElementById("prof_load_" + i).innerHTML = "<font color='red'>Loading...</font>";
   var saved_params = "tool=calc&audId=" + p2.value + "&action=load&load_num=" + i; 
   saved_http2 = getHTTPObject();
   saved_http2.open("POST", "../saved_calcs.php", true); 
   saved_http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   saved_http2.setRequestHeader("Content-length", saved_params.length);
   saved_http2.setRequestHeader("Connection", "close");
   saved_http2.onreadystatechange = load_prof_result;
   saved_http2.send(saved_params);
  }
  else {
     document.getElementById("prof_load_" + i).innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;";
  }
 }
} //end function load_prof


function load_prof_result() {
 if(saved_http2.readyState == 4) {
  var prof_xml = saved_http2.responseXML;
  var prof_act = prof_xml.getElementsByTagName("cact").item(0).firstChild.data;
  var num_prof = 1;
  var load_num = 0;
  if(prof_act == "loadprof") {
   num_prof = prof_xml.getElementsByTagName('num_prof').item(0).firstChild.data;
   load_num = prof_xml.getElementsByTagName('load_num').item(0).firstChild.data;
  }
  else if(prof_act == "lastload") {
   var calc_ext = prof_xml.getElementsByTagName('calc').item(0).firstChild.data;
   if(calc_ext == "None") {
    num_prof = 0;
    populate_cell_gases(); 
   } 
  }
  if(num_prof > 0) {
   var db_table = prof_xml.getElementsByTagName('db_table').item(0).firstChild.data;   
   var db_list = document.getElementById("calc_gas_db");
   for(var d=0; d < db_list.length; d++) {
    if(db_list[d].value == db_table) {
     db_list.selectedIndex = d;
     break
    }
   }
   var mol_tag = prof_xml.getElementsByTagName('molecule');
   var mol_id = "";
   var mol_form = "";
   var ml = "";
   var mol_Iarray = new Array();
   var mol_Farray = new Array();
   for(var w=0; w < mol_tag.length; w++) {
    mol_id = mol_tag.item(w).getAttribute("id");
    mol_Iarray[w] = mol_id;
    mol_form = mol_tag.item(w).getAttribute("formula"); 
    mol_Farray[w] = mol_form;
   }
   var mstr = "";
   var z2 = 0;
   for(var z=0; z < 6; z++)
   {
    z2 = z +1;
    mstr = "gaslist_" + z2;
    ml = document.getElementById(mstr);
    nullOptions(ml);
    with (ml){
     for (var v = 0; v < mol_tag.length; v++) {
      options[v] = new Option(mol_Farray[v],mol_Iarray[v]);
     }
    } //end with
   } 
   var compute = prof_xml.getElementsByTagName('compute').item(0).firstChild.data;
   var log_lin = prof_xml.getElementsByTagName('log_lin').item(0).firstChild.data;
   if(log_lin == "nolog") {
    document.getElementById("log_lin_box").checked = false;
   } 
   else {
    document.getElementById("log_lin_box").checked = true;
   }
   if(compute == "transmission") {
    document.show_info_form.compute[0].checked = true;
   }
   else if (compute == "absorption") {
    document.show_info_form.compute[1].checked = true;
   }
   else {
    document.show_info_form.compute[2].checked = true;
    var blackbody = prof_xml.getElementsByTagName('blackbody').item(0).firstChild.data;  
     document.getElementById("source_select").disabled = false;  
    if(blackbody == "default") {
     document.getElementById("source_select").selectedIndex = 0;
     source_change();
    }
    else if(blackbody == "noblackbody") {
     document.getElementById("source_select").selectedIndex = 2;
     source_change();
    }    
    else {
     document.getElementById("source_select").selectedIndex = 1;
     var bb_temp = prof_xml.getElementsByTagName("source_temp").item(0).firstChild.data;
     var bb_ems = prof_xml.getElementsByTagName("source_emm").item(0).firstChild.data;    
     document.getElementById("source_temp").value = bb_temp;
     document.getElementById("source_emissivity").value = bb_ems; 
    } //end else 
   } //end else compute = radiance
   var units = prof_xml.getElementsByTagName('units').item(0).firstChild.data;  
   var span_txt = "";
   if(units == "wavenumber") {
    document.getElementById("calc_units").selectedIndex=0; 
    span_txt = "<a onClick='popUp(event,\"wavenumber\")'><font color='blue'>cm<sup>-1</sup></font></a>";
   }
   else {
    document.getElementById("calc_units").selectedIndex=1;
   span_txt = "&#181;m";
   }
   document.getElementById("calc_ll_unit").innerHTML = span_txt;
   document.getElementById("calc_ul_unit").innerHTML = span_txt; 
   document.getElementById("calc_iw_units").innerHTML = span_txt;
   var ll = prof_xml.getElementsByTagName('LL').item(0).firstChild.data;
   document.getElementById("lower_bandpass").value = ll;
   var ul = prof_xml.getElementsByTagName('UL').item(0).firstChild.data;  
   document.getElementById("upper_bandpass").value = ul;
   var iw = prof_xml.getElementsByTagName('IW').item(0).firstChild.data; 
   document.getElementById("instr_width").value = iw;
   var instr_val = prof_xml.getElementsByTagName('instr').item(0).firstChild.data; 
   if(instr_val == "none") {
    document.getElementById("instr_sel").selectedIndex = 0;
   }
   else if(instr_val == "sinc") {
    document.getElementById("instr_sel").selectedIndex = 1;   
   }
   else if(instr_val == "triangle"){
    document.getElementById("instr_sel").selectedIndex = 2;   
   }
   else if(instr_val == "square"){
    document.getElementById("instr_sel").selectedIndex = 3;   
   }   
   else if(instr_val == "gaussian"){
    document.getElementById("instr_sel").selectedIndex = 4;   
   }   
   var num_gases = prof_xml.getElementsByTagName('num_gases').item(0).firstChild.data;  
   var ng2 = parseInt(num_gases) - 1;
   document.getElementById("num_of_gases").selectedIndex = ng2;
   update_gases();
   var num_cells = prof_xml.getElementsByTagName('num_cells').item(0).firstChild.data;  
   var nc2 = parseInt(num_cells) -1; 
   document.getElementById("num_of_cells").selectedIndex = nc2;
   update_cells();
   var annotate = prof_xml.getElementsByTagName('annotate').item(0).firstChild.data;  
   if(annotate == "annotate") {
    document.getElementById("calc_annotate").checked = true;
   }
   else {
    document.getElementById("calc_annotate").checked = false;
   }
   var ts = prof_xml.getElementsByTagName('ts').item(0).firstChild.data;  
   if(ts == "timestamp") {
    document.getElementById("calc_timestamp").checked = true;
   }
   else {
    document.getElementById("calc_timestamp").checked = false;
   } 
   var plot_lp = prof_xml.getElementsByTagName('plot_lp').item(0).firstChild.data;  
   if(plot_lp == "sticks") {
    document.getElementById("ticks_box").checked = true;
   }
   else {
    document.getElementById("ticks_box").checked = false;
   }    
   var gas_tag = prof_xml.getElementsByTagName('gas');
   var gas_val = "";
   var gas_iso = "";
   var g2 = 0;
   var q2 = 0;
   var iso_str2 = "";
   var iso_tag = "";
   var iso_tag2 = "";
   var iso_form = "";
   var iso_id = "";
   var iso_array = new Array();
   for(var q=0; q < gas_tag.length; q++) {
    gas_val = gas_tag.item(q).firstChild.data;
    gas_iso = gas_tag.item(q).getAttribute("iso");
    q2 = q +1;
    g2 = gas_tag.item(q).getAttribute("selInd"); 
    isostr2 = "iso" + q;
    iso_tag = prof_xml.getElementsByTagName(isostr2);
    for(var f=0; f < iso_tag.length; f++) {
     iso_form = iso_tag.item(f).getAttribute("formula");
     iso_id = iso_tag.item(f).firstChild.data;
     iso_array[f+1] = iso_id + "    " + iso_form;
    }
    var update_list = "isotope" + q2;
    var list_menu = document.getElementById(update_list);
    update_isotopes_list(list_menu, iso_tag.length ,iso_array);
    if(q < mol_tag.length){
     document.getElementById("gaslist_" + q2).selectedIndex = g2;
     document.getElementById("isotope" + q2).selectedIndex = gas_iso;   
    }
   } //end for loop
   var cell_tag = prof_xml.getElementsByTagName('cell');
   var cell_temp = "";
   var cell_press = "";
   var cell_len = "";
   var cell_vmr = "";
   var cell_vmr2 = "";
   var cell_val = "";
   var cell_num = "";
   var cv_str = "";
   for(var j=0; j < cell_tag.length; j++) {
    cell_val = cell_tag.item(j).getAttribute("value");
    cell_num = cell_tag.item(j).getAttribute("num");
    if(cell_val == "true"){
     document.getElementById("cell" + cell_num + "_box").checked = true;
    }
    else {
    document.getElementById("cell" + cell_num + "_box").checked = false;
    }
    cell_temp = cell_tag.item(j).getElementsByTagName("cell_temp").item(0).firstChild.data;
    cell_press = cell_tag.item(j).getElementsByTagName("cell_press").item(0).firstChild.data;
    cell_len = cell_tag.item(j).getElementsByTagName("cell_length").item(0).firstChild.data;
    cell_vmr = cell_tag.item(j).getElementsByTagName("cell_vmr").item(0).firstChild.data;   
    document.getElementById("cell" + cell_num + "_length").value = cell_len;
    document.getElementById("cell" + cell_num + "_pressure").value = cell_press;
    document.getElementById("cell" + cell_num + "_temperature").value = cell_temp;
    cell_vmr2 = cell_vmr.split(':');
    for(var h=1; h < 7; h++) {
     cv_str = "cell" + cell_num + "_vmr" + h;
     document.getElementById(cv_str).value = cell_vmr2[h-1];
    } //end for loop
   }
   if(prof_act == "loadprof") {
    document.getElementById("prof_load_" + load_num).innerHTML = "<font color='red'>Loaded</font>";
   }
  }
  else {
   if(prof_act == "loadprof") {  
    window.alert("An error has occurred, please load the setting again.");
   }
  }
 }
}

//this function will get the last calculation done during this session and load it on the page.
function load_last_calc() {
 var remote_addr = document.getElementById("calc_remote_addr").value; 
 var out = "tool=calc&r_addr=" + remote_addr;
 saved_http2 = getHTTPObject();
 saved_http2 = getHTTPObject();
 saved_http2.open("POST", "../last_calc.php", true);
 saved_http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 saved_http2.setRequestHeader("Content-length", out.length);
 saved_http2.setRequestHeader("Connection", "close");
 saved_http2.onreadystatechange = load_prof_result;
 saved_http2.send(out);
}

function resetBTNS() {
 document.show_info_form.compute[0].checked = true;
 document.getElementById("log_lin_box").checked = false;
 document.getElementById("source_select").selectedIndex = 0;
 document.getElementById("source_select").disabled = true;
 document.getElementById("source_temp").value = "300";
 document.getElementById("source_emissivity").value = "1"; 
 document.getElementById("calc_units").selectedIndex=0; 
 var span_txt = "<a onClick='popUp(event,\"wavenumber\")'><font color='blue'>cm<sup>-1</sup></font></a>";
 document.getElementById("calc_ll_unit").innerHTML = span_txt;
 document.getElementById("calc_ul_unit").innerHTML = span_txt; 
 document.getElementById("calc_iw_units").innerHTML = span_txt;
 document.getElementById("lower_bandpass").value = "2000";
 document.getElementById("upper_bandpass").value = "2100";
 document.getElementById("instr_width").value = "0.1";
 document.getElementById("instr_sel").selectedIndex = 0;
 document.getElementById("num_of_gases").selectedIndex = 0;
 update_gases();
 document.getElementById("num_of_cells").selectedIndex = 0;
 update_cells();
 var user_status = get_user_status();
 if(user_status == "on") {
  document.getElementById("calc_annotate").checked = true;
  document.getElementById("calc_timestamp").checked = true;
  document.getElementById("ticks_box").checked = true;
 }
 else {
  document.getElementById("calc_annotate").checked = false;
  document.getElementById("calc_timestamp").checked = false;
  document.getElementById("ticks_box").checked = false;
 }
 var cv_str = "";
 for(var j=1; j < 7; j++) {
  document.getElementById("cell" + j + "_box").checked = true;
  document.getElementById("cell" + j + "_length").value = "10";
  document.getElementById("cell" + j + "_pressure").value = "1013.25";
  document.getElementById("cell" + j + "_temperature").value = "296";
  for(var h=1; h < 7; h++) {
   cv_str = "cell" + j + "_vmr" + h;
   document.getElementById(cv_str).value = "0.1";
  } //end for loop
 }
 document.getElementById("calc_gas_db").selectedIndex = 0;
 populate_cell_gases();
} //end function reset_all
