cues_to_download = null;
cue_download_audio_mode = null;
var ajax_download_r = make_new_ajax_request();

function instant_download(cue_id)
{  
   if (standalone)
   {
      cue_ids= new Array();
      cue_ids.push(cue_id);
      start_export(cue_ids);
      return;
   }

   str = "Cue " + all_cue_data[cue_id].cue_code + 
      " will begin downloading shortly";
   if (getInternetExplorerVersion() != -1)
   {  str += "<BR><BR>IE users: this may fail if popus are blocked";
   }
   showTempAlert(str, 4000);
   xml  = "<\?xml version=\"1.0\"\?><DownloadRequest>";
   xml += "<LicenseMode>CART</LicenseMode>";
   xml += "<LicensingUser>" + user.user_id + "</LicensingUser>";
   xml += "<InstantMode>1</InstantMode>";

   xml += "<CueID>" + cue_id + "</CueID>";
   xml += "</DownloadRequest>";

   xml = "xml=" + escape(xml);
   ajax_download_r.open("POST", "/download_backend.php", true);
   ajax_download_r.onreadystatechange = download_request_callback;
   ajax_download_r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajax_download_r.send(xml);
}

function instant_nr_download_push(album_id, is_aiff)
{  
   // IE has issues with download from the IFrame - it prompts you the first
   //  time, then reloads the page
   str = "The album will begin downloading shortly";
   showTempAlert(str, 4000);

   if (getInternetExplorerVersion() != -1)
   {
      window.open("/downloader.php?nr=" + album_id + 
         "&aiff=" + (is_aiff ? "1" : "0"),
         'download', 'width=200,height=200,toolbar=no, location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes');
   }
   else
   {
      var iframe = document.createElement("iframe");
      iframe.src = 'downloader.php?nr=' + album_id +
         "&aiff=" + (is_aiff ? "1" : "0");
      iframe.style.display = "none";
      document.body.appendChild(iframe);
   }
}

function instant_download_push(filename)
{  
   // IE has issues with download from the IFrame - it prompts you the first
   //  time, then reloads the page
   if (getInternetExplorerVersion() != -1)
   {
      window.open("/downloader.php?f=" + filename, 'download', 'width=200,height=200,toolbar=no, location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes');
   }
   else
   {  
      var iframe = document.createElement("iframe");
      iframe.src = 'downloader.php?f=' + filename;
      iframe.style.display = "none";
      document.body.appendChild(iframe);
   }
}


function download_request_callback()
{
   if (ajax_download_r.readyState == 4) 
   {
      if (ajax_download_r.status == 200) 
      {  
         // Get the response from the server - conveniently already
         //  as an XML tree (requires some subtle header settings in 
         //  the server script
         var d = ajax_download_r.responseXML;

         node = d.getElementsByTagName('Result')[0];
         if (node)
         {  if (node.childNodes[0].nodeValue == 'Success')
            {  
               url = XML_val(d, 'URL');
               if (XML_val(d, 'InstantMode') == "1")
               {  instant_download_push(url);
                  return;
               }
               else
               {
                  node = d.getElementsByTagName('FileSize')[0];

            // to get results like 3.5 MB
                  f_size = 
                     Math.round(node.childNodes[0].nodeValue/102400)/10;

                  f_name = XML_val(d, 'FileName');
                  
                  el = document.getElementById("progress_indicator");
                  el.style.display = "none";

                  el = document.getElementById("download_message");
                  el.innerHTML = '<A HREF="/' + url + 
                     '">Click here to download your order<BR>' +
                     '(filename "' + f_name +'", size is ' + f_size + ' MB)</A>';
                  node = d.getElementsByTagName('LicenseMode')[0];
                  if (node)
                  {  if (node.childNodes[0].nodeValue == 'CART')
                     {  handle_clear_cart(true);  
                     }
                  }
               }
            }
            else
            {  err_msg = "???";
               node = d.getElementsByTagName('Error')[0];
               if (node)
               {  error_msg = node.childNodes[0].nodeValue;
               }
               if (user.is_share_guest)
               {
                  alert('Sorry, something went wrong with the download:' +
                     error_msg);
               }
               else
               {
                  showAlert('Sorry, something went wrong with the download:' +
                     error_msg);
               }
            }
         }
      }
   }
}

function handle_disagree()
{
   el = document.getElementById("accept_order");
   el.style.display = "none";

   el = document.getElementById("reject_order");
   el.style.display = "block";
}

function send_disagree_reason()
{ 
   el = document.getElementById("cancel_reason");
   comment = el.value;
   if (comment != "")
   {
      xml  = "<\?xml version=\"1.0\"\?><CancelOrder>";
      xml += "<Comment>" + comment + "</Comment>";
   
      for (i = 0; i < cues_to_download.length; i++)
      {  xml += "<CueCode>" + all_cue_data[cues_to_download[i]].cue_code + 
            "</CueCode>";
      }

      xml += "</CancelOrder>";
      xml = "xml=" + escape(xml);
      ajax_download_r.open("POST", "/cancel_order_backend.php", true);
      ajax_download_r.onreadystatechange = cancel_order_request_callback;
      ajax_download_r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      ajax_download_r.send(xml);
   }
}

function cancel_order_request_callback()
{
   if (ajax_download_r.readyState == 4) 
   {
      if (ajax_download_r.status == 200) 
      {  showAlert("Thank you for your comment");
         el = document.getElementById("cancel_reason");
         el.value = "";
         jump_to_last_breadcrumb();
      }
   }
}

 
function show_order_summary(show, cues)
{  
   el = document.getElementById("order_summary_section");
   if (el != null)
   {  el.style.display = (show ? "block" : "none"); 
   }
   if (show != true) 
   {  // all done now 
      return;
   }

   show_cart(false);
   show_my_bins(false); 
   show_bin(false, 0); 
   show_info_request(false, 0);
   show_login(false);
   show_search(false);
   show_share(false);
   show_register(false);
   if (standalone)
   {  show_prefs(false);
      show_updater(false);
   }
   show_needledrop(false);
   show_data_find(false);
   show_new_releases(false);
   show_cue_sheets(false);

   clear_preview();
   cues_to_download = cues;
   update_breadcrumb("ORDER_SUMMARY", false);

   if (!standalone)
   {  el = document.getElementById("order_cost");
      el.style.display = (user.is_annual ? "none" : "block");
   }
   el = document.getElementById("accept_order");
   el.style.display = "block";

   el = document.getElementById("reject_order");
   el.style.display = "none";

   el = document.getElementById("download_section");
   el.style.display = "none";

   cue_str = '';
   ftt = true;
   for (i = 0; i < cues.length; i++)
   {  if (ftt)
      {  ftt = false;
      }
      else
      {  cue_str +=  '<BR>';
      }
      cue_str +=  all_cue_data[cues[i]].cue_code;
   }
   el = document.getElementById("cues_to_lic");
   el.innerHTML = cue_str; 

   if (!standalone)
   {
      el = document.getElementById("lic_company");
      el.innerHTML = user.company;

      el = document.getElementById("lic_attn");
      el.innerHTML = user.pretty_name;

      el = document.getElementById("lic_addr");
      el.innerHTML = user.address + "<BR>" + user.city + ", " + user.state + 
         " " + user.zip + "(" + user.country + ")"; 

      el = document.getElementById("lic_email");
      el.innerHTML = user.email;

      el = document.getElementById("lic_phone");
      el.innerHTML = user.phone;

      el = document.getElementById("lic_fax");
      el.innerHTML = user.fax;
   }
}
function standalone_download_callback(filename)
{  
   el = document.getElementById("download_section");
   el.style.display = "block";

   el = document.getElementById("progress_indicator");
   el.style.display = "block";

   el = document.getElementById("download_message");
   el.innerHTML = "Please wait for your export to complete. <BR><BR>Please do not navigate away from this page.<BR>";  

   xml  = "<\?xml version=\"1.0\"\?><DownloadRequest>";
   xml += "<LicenseMode>EXPORT</LicenseMode>";

   for (i = 0; i < cues_to_download.length; i++)
   // all_cue_data[cues_to_download[i]].cue_code WAS
   {  xml += "<CueID>" + cues_to_download[i] + "</CueID>";
   }
   // I have globals, but hard give the whole callback busines
   xml += "<AudioMode>" + cue_download_audio_mode + "</AudioMode>";
   xml += "<DestinationDir>" + filename + "</DestinationDir>";

   xml += "</DownloadRequest>";
   xml = "xml=" + escape(xml);

   ajax_download_r.open("POST", "/download_backend.php", true);
   ajax_download_r.onreadystatechange = download_request_callback;
   ajax_download_r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajax_download_r.send(xml);
}

// cues_to_download is a global set before calling this,
//  with the exception of the shared bin business
function start_downloads(audio_mode)
{  if (standalone)
   {  // should never get here    
   }
   else
   { 
      if (!user.is_share_guest)
      {
         el = document.getElementById("accept_order");
         el.style.display = "none";
         licensee = user.user_id;
      }
      else
      {  
      // global found in search.php
         licensee = shared_bin_licensee;
         el = document.getElementById("share_download_button");
         el.style.display = "none";
      }
   }

   el = document.getElementById("download_section");
   el.style.display = "block";

   el = document.getElementById("progress_indicator");
   el.style.display = "block";

   el = document.getElementById("download_message");
   el.innerHTML = "Do not close your browser or leave the o1engine until the download link appears.<BR><BR>(AIF versions take longer to process and download)";

   xml  = "<\?xml version=\"1.0\"\?><DownloadRequest>";
   if (user.is_share_guest)
   {  xml += "<LicenseMode>SHARE</LicenseMode>" +
            "<BinID>" + shared_bin_id + "</BinID>";
   }
   else
   {  xml += "<LicenseMode>CART</LicenseMode>";
   }
   xml += "<LicensingUser>" + licensee + "</LicensingUser>";
   xml += "<AudioMode>" + audio_mode + "</AudioMode>";

   for (i = 0; i < cues_to_download.length; i++)
   // all_cue_data[cues_to_download[i]].cue_code WAS
   {  xml += "<CueID>" + cues_to_download[i] + "</CueID>";
   }
   xml += "</DownloadRequest>";
   xml = "xml=" + escape(xml);

   ajax_download_r.open("POST", "/download_backend.php", true);
   ajax_download_r.onreadystatechange = download_request_callback;
   ajax_download_r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajax_download_r.send(xml);
}


