// include our JavaScript stuff for Ajax
// the ajaxml object we need to make our calls

// need way to keep track things already added to a cart!

var ajax_sharing_r = make_new_ajax_request();
cues_to_share = null;

var share_email_rows = new Array();


// again, because JS assoc arrays aren't what they appear to be...
function get_num_sharing_email_rows()
{  count = 0;
   for (r in share_email_rows)
   {  if (share_email_rows[r])
      {  count++;
      }
   }
   return count;
}

function add_sharing_recipient()
{
   table = document.getElementById("sharing_table");
  
   len_now = get_num_sharing_email_rows();
   row = table.insertRow(5 + len_now);

   // first new row should be row '1' - row '0' is the permanent, starter one
   row.id = "share_email_row" + len_now;

   cell_left = row.insertCell(0);
   el = document.createElement('input');
   el.className = "mainText textBoxBackground";
   el.value = "";
   cell_left.appendChild(el);
   row.name_el = el;

   cell_right = row.insertCell(1);
   el = document.createElement('input');
   el.className = "mainText textBoxBackground";
   el.value = "";
   el.size = "40";
   cell_right.appendChild(el);
   row.email_el = el;

   share_email_rows[len_now] = row;
}

function clear_sharing_recipients()
{
   table = document.getElementById("sharing_table");
   document.getElementById("share_email_name0").value = "";
   document.getElementById("share_email_email0").value = "";

   for (r in share_email_rows)
   {  table.deleteRow(share_email_rows[r].rowIndex);
      delete share_email_rows[r];
   }
}


function share_callback()
{
   if (ajax_sharing_r.readyState == 4) {
      if (ajax_sharing_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_sharing_r.responseXML;
         node = d.getElementsByTagName('Result')[0];
         if (node)
         {  if (node.childNodes[0].nodeValue == 'Success')
            {  cues_to_share = null;
               showAlert("An invitation has been sent out");
               jump_to_last_breadcrumb();
            }
            else
            {
               node = d.getElementsByTagName('Error')[0];
               showAlert(node.childNodes[0].nodeValue);
            }
         }
      }       
   }
}

function show_share(show, cues)
{  
   el = document.getElementById("share_section");
   if (!el)
   {  return;
   }
   el.style.display = (show ? "block" : "none"); 

   if (show != true) 
   {  // all done now 
      return;
   }

   clear_preview();
   cues_to_share = cues;

   show_cart(false);
   show_my_bins(false); 
   show_bin(false, 0); 
   show_login(false);
   show_order_summary(false, null);
   show_search(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);

   el = document.getElementById("share_cost");
   el.style.display = (user.is_annual ? "none" : "block");

   update_breadcrumb("SHARE", false);

   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_share");
   el.innerHTML = cue_str;

   update_cart_status_line();
}

// kind of odd helper function to avoid code tangles in handle_share_submit()
function __check_share_email(el_name, el_email)
{  
   this_name  = el_name.value;
   this_email = el_email.value;

   if (!this_name && !this_email)
   {  return true;
   }

   if (this_name && !this_email)
   {  showAlert("Please enter in an email for '" + this_name + "'");
      el_email.select();
      return false;
   }

   if (!this_name && this_email)
   {  showAlert("Please enter in a name for email '" + this_email + "'");
      el_email.select();
      return false;
   }

   if (!isValidEmail(this_email))
   {  showAlert("'" + this_email + "' is not a valid email address");
      el_email.select();
      return false;
   }
   return true;
}

function handle_share_submit()
{
   radios = document.getElementsByName("share_use_group");
   
   dl_and_preview = null;

   for (i = 0; i < radios.length; i++)
   {  if (radios[i].checked == true)
      {  if (radios[i].value == "preview")
         {  dl_and_preview = false;
         }
         else
         {  dl_and_preview = true;
         }
         break;
      }
   }

   if (dl_and_preview == null)
   {  showAlert("Please choose whether you'd like to let the user download " + 
         "the tracks or not");
      return;
   }

   el_name  = document.getElementById("share_email_name0");
   el_email = document.getElementById("share_email_email0");

   if (!__check_share_email(el_name, el_email))
   {  return;
   }
   
   names = new Array();

   this_one = new Object();
   this_one.name  = trim(el_name.value);
   this_one.email = trim(el_email.value);

   if (this_one.name && this_one.email)
   {  names.push(this_one);
   }
   
   for (r in share_email_rows)
   {  
      if (share_email_rows[r])
      {  row = share_email_rows[r]; 
         this_one = new Object();

         if (!__check_share_email(row.name_el, row.email_el))
         {  return;
         }

         this_one.name  = trim(row.name_el.value);
         this_one.email = trim(row.email_el.value);

         if (this_one.name && this_one.email)
         {  names.push(this_one);
         }
      }
   }


   if (names.length < 1)
   {  showAlert("Please enter in at least one email recipient");
   }


   el = document.getElementById("share_subject");
   subject_in = trim(el.value);

   if (!subject_in)
   {  showAlert("Please provide a subject for this");
      return;
   }

   el = document.getElementById("share_comments");
   comment_in = trim(el.value);

/* reset fields
 */
/*
   el = document.getElementById("share_name");
   el.value = "";

   el = document.getElementById("share_email");
   el.value = "";

   el = document.getElementById("share_subject");
   el.value = "";

   el = document.getElementById("share_comments");
   el.value = "";
*/
   request_share(names, dl_and_preview, subject_in, comment_in);
}

function request_share(names, dl_and_preview, subject, comment)
{ 

   xml = "<\?xml version=\"1.0\"\?><ShareRequest>";
   xml += "<AllowDownload>" + (dl_and_preview ? "1" : "0") + 
      "</AllowDownload>";

   xml += "<RecipientList>";

   if (!names || names.length < 1)
   {  showAlert("somehow the names got corrupted while initiating a share");
      return;
   }

   for (i = 0; i < names.length; i++)
   {
      xml += "<Recipient>";
      xml += "<Name>" +  xml_escape(names[i].name)  + "</Name>";
      xml += "<Email>" + xml_escape(names[i].email) + "</Email>";
      xml += "</Recipient>";
   }
   xml += "</RecipientList>";

   xml += "<Subject>" + xml_escape(subject) + "</Subject>";
   xml += "<Comment>" + xml_escape(comment) + "</Comment>";
   xml += "<Cues>";

   for (i = 0; i < cues_to_share.length; i++)
   {
      xml += "<CueID>" + cues_to_share[i] + "</CueID>";
   }

   xml += "</Cues>";
   xml += "</ShareRequest>";

   xml = "xml=" + escape(xml);

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



















