
$(document).ready(function() {

    function doXSLT(xml, xslt) {
     if(typeof XSLTProcessor !== "undefined") { // FF, Safari, Chrome etc
         var xmlDoc = xml;//$.parseXML(xml);
         var xsltDoc = xslt;//$.parseXML(xslt);
         var xsltProcessor = new XSLTProcessor();
         xsltProcessor.importStylesheet(xsltDoc);
         var xmlFragment = xsltProcessor.transformToFragment(xmlDoc, document);
         return XMLToString(xmlFragment);
     }
    
     var xmlDoc = StringToXML(xml);
     var xsltDoc = StringToXML(xslt);
     //console.log("xmlDoc=",xmlDoc.xml);

     if (typeof (xmlDoc.transformNode) != "undefined") { // IE6, IE7, IE8
         return xmlDoc.transformNode(xsltDoc);
     } else {
         try { // IE9 and grater
             if (window.ActiveXObject != "undefined") {
                 var xslt_w = new ActiveXObject("Msxml2.XSLTemplate");
                 var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
                 //console.log("xslt="+xsltDoc.xml);
                 xslDoc.loadXML(xslt);
                 xslt_w.stylesheet = xslDoc;
                 var xslProc = xslt_w.createProcessor();
                 xslProc.input = xmlDoc;
                 xslProc.transform();
                 var x = xslProc.output;
                 return xslProc.output;
             }
         }
         catch (e) {
             alert(e);
             return null;
         }
     }
  }


function XMLToString(oXML) {
   if ("ActiveXObject" in window) { //code for IE
     var oString = oXML.xml; return oString;
   } else {
     return (new XMLSerializer()).serializeToString(oXML);
   }
}
/*
function StringToXML(oString) {
  if ("ActiveXObject" in window) { //code for IE
   var oXML = new ActiveXObject("Microsoft.XMLDOM");
   oXML.loadXML(oString);
   if (oXML.parseError.errorCode != 0)
     {
     alert("Error in line " + oXML.parseError.line +
     " position " + oXML.parseError.linePos +
     "\nError Code: " + oXML.parseError.errorCode +
     "\nError Reason: " + oXML.parseError.reason +
     "Error Line: " + oXML.parseError.srcText);
     //return(null);
     }
   return oXML;
  } else { // code for Chrome, Safari, Firefox, Opera, etc.
   return (new DOMParser()).parseFromString(oString, "text/xml");
  }
}
*/



  $('#invButton').click(function(){
    console.log('pressed');
    
    var path = location.origin + location.pathname.replace("galactic.html", "");
    
    $.get(path + 'vlkb-search.xsl?v=' + (new Date()).getTime(), function(result) {
//    $.get('http://localhost:8080/libjnifitsdb-0.22.0/vlkb-search.xsl', function(result) {
      var xslt = result;
      
      var lon = $('#lon').val();
      var lat = $('#lat').val();
      var radius = $('#radius').val();
      var vl = $('#vl').val();
      var vu = $('#vu').val();
      
      var jsonpar = {
       l: lon,
       b: lat,
       r: radius
      };
      if(vl !== '') {
         jsonpar['vl'] = vl;
      }
      if(vu !== '') {
         jsonpar['vu'] = vu;
      }
      
      console.log(jsonpar);
      
//      $.get(path + 'vlkb_search?l=' + lon + '&b=' + lat + '&r=' + radius + '&vl=' + vl  + '&vu=' + vu,
      
      $.get(path + 'vlkb_search', jsonpar, function(result) {
         var xml = result;
         //console.log(xml);
         var htmlRes = $('<div>' + (new Date()).toLocaleString() + doXSLT(xml, xslt) + '</div>');
         $('#gendHtml').html(htmlRes);

/* format 2 decimals         
         var ths = $('table:nth-child(2) th');
         
         for(var i=0; i<ths.length; i++) {
         
            var th = ths[i];
            if($(th).text() === 'vel-from')  {
              var tds = $('table:nth-child(2) tr td:nth-child(' + (i+1) + ')');
              for(var j=0; j< tds.length; j++) {
                 var td = tds[j];                                
                 $(td).text(parseFloat($(td).text()).toFixed(2));

              }
              break;
            }
         }
*/
      });
    });
  });

});

