var mol_http = "";
var plot_http = "";
var lc_http = "";
var saf_counter =0;
function getHTTPObject()
{
 var xmlhttp;
 /*@cc_on
 @if (@_jscript_version >= 5)
  try {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (E) {
       xmlhttp = false;
      }
    }
 @else
 xmlhttp = false;
 @end @*/
 if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  try {
   xmlhttp = new XMLHttpRequest();
  } catch (e) {
    xmlhttp = false;
    }
 }
return xmlhttp;
} //end getHTTPObject

function clear_plots()
{
 var plot_div = document.getElementById("plot_div");
 plot_div.innerHTML = "";
} //end clear_plot function

function get_molecules()
{
 var p_type = document.getElementById("ab_plot_type1").checked;
 if(p_type == true)
 {
 var mol_table = document.getElementById("atmosphere").value;
 var get_mol_params = "mol_table=" + mol_table;
 mol_http = getHTTPObject();
 mol_http.open("POST", "ab_get_mol.php", true);
 mol_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 mol_http.setRequestHeader("Content-length", get_mol_params.length);
 mol_http.setRequestHeader("Connection", "close");
 mol_http.onreadystatechange = handleMolGet;
 mol_http.send(get_mol_params);
 } //end p_type = true
} //end get_molecules function

function handleMolGet()
{
 if(mol_http.readyState == 4)
 {
  var response = mol_http.responseXML;
  var output_string = "";
  var mol_div = document.getElementById("molecule_div");
  var mol_menu = document.getElementById("ab_plot_val");
  //var num_mols = response.getElementsByTagName('num_mols').item(0).firstChild.data;
  nullOptions(mol_menu);
  with (mol_menu){
  options[0] = new Option("Temperature", "temperature");
  options[1] = new Option("Pressure", "pressure");
  var opt_z = 2;
  var mol_tag = response.getElementsByTagName('molecules').item(0);
  var num_mols = mol_tag.childNodes.length;
  var temp_mol = "";
  var temp_name = "";
  for (var z = 0; z <num_mols; z++) 
  {
    temp_mol = mol_tag.getElementsByTagName("molecule").item(z).getAttribute("id");
    temp_name = mol_tag.getElementsByTagName("molecule").item(z).getAttribute("name");
// console.log("mol: " + temp_mol + " name "+ temp_name);
//   var temp_mol_str = "mol" + z;
//    temp_mol = response.getElementsByTagName(temp_mol_str).item(0).firstChild.data;
//   var temp_name_str = "mol_name" + z;
//    temp_name = response.getElementsByTagName(temp_name_str).item(0).firstChild.data;
   options[opt_z] = new Option(temp_name,temp_name);
   opt_z = opt_z + 1;
   }
  } //end with
 } //end if ready state = 4
} //end handleMolGet

function get_plot()
{
 var atmosphere = document.getElementById("atmosphere").value;
 var plot_val = document.getElementById("ab_plot_val").value;
 var log_linear = document.getElementById("log_linear").value;
 var plot_radio_val = document.getElementById("ab_plot_type1").checked;
 var logo = "logo";
 var error = false;
 var plot_type = "species";
 var err_str = ""; 
 var remote_addr = document.getElementById("calc_remote_addr").value;
 var remote_host = document.getElementById("calc_remote_host").value;
 var user_agent = navigator.appName;
 if(plot_radio_val == false) {
  plot_type = "altitude";
  if((parseFloat(plot_val) > 120) || (parseFloat(plot_val) < 0) || isNaN(parseFloat(plot_val)))  {
   error = true;
   err_str = "Altitude must be greater than/equal to 0 km and less than/equal to 120 km.";
  }
 }
  if(error == false)  {
   document.getElementById("ab_err_div").innerHTML =  "";
   var out = "ab_xml=<ab_info><atmosphere>" + atmosphere + "</atmosphere><plot_val>" + plot_val + "</plot_val><log_lin>";
   out+= log_linear + "</log_lin><r_addr>" + remote_addr + "</r_addr><r_host>";
   out+= remote_host + "</r_host><plot_type>" + plot_type + "</plot_type><uagt>" + user_agent + "</uagt></ab_info>";
   plot_http = getHTTPObject();
   plot_http.open("POST", "ab_get_plot.php", true);
   plot_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   plot_http.setRequestHeader("Content-length", out.length);
   plot_http.setRequestHeader("Connection", "close");
   plot_http.onreadystatechange = handlePlot;
   plot_http.send(out);
  }
  else  { 
   document.getElementById("ab_err_div").innerHTML =  "<font color='red'><b>ERRORS</b><br>" + err_str + "</font><br>";
  }
} //end get_plot


function handlePlot(){
 if(plot_http.readyState == 4) {
  var xmlDocument = plot_http.responseXML;
  var remind = xmlDocument.getElementsByTagName('remind').item(0).firstChild.data;
  var rnum =  xmlDocument.getElementsByTagName('rnum').item(0).firstChild.data;
  var file_name = xmlDocument.getElementsByTagName('filename').item(0).firstChild.data;
  var plot_type = xmlDocument.getElementsByTagName('plot_type').item(0).firstChild.data;
  var max_calcs = false;
  var par_status = document.getElementById("user_status").value;
  if((rnum >= 50) && (par_status != "on"))  {
   max_calcs = true;
   document.getElementById("calculate").disabled = true;
   document.getElementById("clear_plot").disabled = true;
   popUp2("daily_max", "ab_calc");
  } //end rnum > max number allowed and status is not on.
  else   {
   if(remind == "yes")   { 
    popUp2("remind","ab_calc");
    document.getElementById("calculate").disabled = true;
    document.getElementById("clear_plot").disabled = true;
   }
   var plot_div = document.getElementById("plot_div");
   var png_file = "plots/" + file_name + ".png";
   var ascii_file = "";
   if(par_status != "on")   {
     ascii_file = "<font color='blue'><u><a onClick='ps_acsii_err(\"" + par_status + "\" , \"ASCII data\");'>Text File</a></u></font><br><div id='ps_ascii_div'></div><br>";
     ascii_file+= "<font color='#555555'><input type='button' value='Subscribe' onClick='window.open(\"../info/plans.php\");'> to get <strong>logo free graphs</strong>, in addition to full access to the spectral calculator!</font><br>";
   }
   else if(plot_type == "species")   {
    var txt_size = xmlDocument.getElementsByTagName('fsz').item(0).firstChild.data;
    ascii_file = "<a href='plots/profile_" + file_name + ".dat' target='_blank'>Text File (" + txt_size + ")</a><br><br>";
   }
   else if(plot_type == "altitude")   {
    var txt_size = xmlDocument.getElementsByTagName('fsz').item(0).firstChild.data;
    ascii_file = "<a href='plots/vmrs_" + file_name + ".txt' target='_blank'>Text File (" + txt_size + ")</a><br><br>";
   }
   var output_string = "<img src='" + png_file + "'><br>";
   output_string = output_string + ascii_file;
   plot_div.innerHTML = output_string + plot_div.innerHTML;
  } //end else
 } //end httpd ready state = 4
} //end handlePlot

function nullOptions(aMenu){
 var menu_length = aMenu.length;
 for (var i = menu_length; 0 <= i; i--) {
   aMenu.options[i] = null;
 }
} //end nullOptions function

function change_plot_type(pt){
 var ptd = document.getElementById("plot_type_div"); 
 var mol_div = document.getElementById("molecule_div");
 if(pt == "species") {
  ptd.innerHTML = "Species";
  mol_div.innerHTML = "<SELECT name='ab_plot_val' id='ab_plot_val' STYLE='width: 125px'><OPTION></OPTION></SELECT>";
  document.getElementById("plot_options").className="show_options";
  get_molecules();
 }
 else if (pt == "altitude") {
  ptd.innerHTML = "Altitude";
  mol_div.innerHTML = "<input type='text' name='ab_plot_val' id='ab_plot_val' value='60' size='10'>&nbsp;km";
  document.getElementById("plot_options").className="hide_options";
 }
} //end change_plot_type

function close_popup(){
   document.getElementById("calculate").disabled = false;
   document.getElementById("clear_plot").disabled = false;
   popUp2("remind", "ab_calc");
} //end close popup

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

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

//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=ab&r_addr=" + remote_addr;
 lc_http = getHTTPObject();
 lc_http.open("POST", "../last_calc.php", true);
 lc_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 lc_http.setRequestHeader("Content-length", out.length);
 lc_http.setRequestHeader("Connection", "close");
 lc_http.onreadystatechange = load_calc;
 lc_http.send(out);
}

function load_calc() {
 if(lc_http.readyState == 4) {
  var xmlDocument = lc_http.responseXML;
  var ext = xmlDocument.getElementsByTagName('calc').item(0).firstChild.data;
  if(ext != "None") {
   var atm = xmlDocument.getElementsByTagName('atmosphere').item(0).firstChild.data;
   if(atm == "US_Standard") {
    document.getElementById("atmosphere").selectedIndex = 0;
   }
   else if(atm == "Tropical") {
    document.getElementById("atmosphere").selectedIndex = 1;   
   }
   else if(atm == "Polar_Summer") {
    document.getElementById("atmosphere").selectedIndex = 2;   
   }
   else if(atm == "Polar_Winter") {
    document.getElementById("atmosphere").selectedIndex = 3;   
   }
   else if(atm == "Midlatitude_Summer") {
    document.getElementById("atmosphere").selectedIndex = 4;   
   }
   else if(atm == "Midlatitude_Winter") {
    document.getElementById("atmosphere").selectedIndex = 5;   
   }            
   var log_lin = xmlDocument.getElementsByTagName('log_lin').item(0).firstChild.data; 
   if(log_lin == "nolog") {
    document.getElementById("log_linear").selectedIndex = 0;    
   }
   else {
    document.getElementById("log_linear").selectedIndex = 1;    
   }   
   
   var ptype = xmlDocument.getElementsByTagName('plot_type').item(0).firstChild.data;
   if(ptype == "species") {
    document.getElementById("ab_plot_type1").checked = true;  
    document.getElementById("ab_plot_type2").checked = false;         
   }
   else {
    document.getElementById("ab_plot_type2").checked = true;  
    document.getElementById("ab_plot_type1").checked = false;  
   }
   var specVal = xmlDocument.getElementsByTagName('plot_val').item(0).firstChild.data;   
   var ptd = document.getElementById("plot_type_div"); 
   var mol_div = document.getElementById("molecule_div");
   if(ptype == "species") {
    var mnu = document.getElementById("ab_plot_val");
    var mnuV = "";
    for(var i=0; i < mnu.length; i++) {
     mnuV = mnu[i].value;
     if(mnuV == specVal) {
      mnu.selectedIndex = i;
     }
    }
   }
   else {
   ptd.innerHTML = "Altitude";
   mol_div.innerHTML = "<input type='text' name='ab_plot_val' id='ab_plot_val' value='" + specVal + "' size='10'>&nbsp;km";
   document.getElementById("plot_options").className="hide_options";
   }  
  } //end if ext != None
 }  //end lc_http.readyState = 4
} //end function load_calc
