
var ajax_needledrop_r = make_new_ajax_request();


function handle_nd_cb(el_in)
{  id = el_in.id;

   if (id == "nd_check_public")
   {  el = document.getElementById("nd_public_section");
      el.style.display = (el_in.checked ? "block" : "none");
      
   }

   else if (id == "nd_check_program" || id == "nd_check_theatres")
   {  showAlert("We are currently not offering automated agreements " + 
         "for this option.  Please contact " + domain_info.display_name +
         " for a quote.");
      el_in.checked = false;
   }

   else if (id == "nd_check_commercial")
   {  el = document.getElementById("nd_commercial_section");
      el.style.display = (el_in.checked ? "block" : "none");
   }   

   else if (id == "nd_check_live")
   {  el = document.getElementById("nd_live_section");
      el.style.display = (el_in.checked ? "block" : "none");
   }
   
   else if (id == "nd_check_internet")
   {  el = document.getElementById("nd_internet_section");
      el.style.display = (el_in.checked ? "block" : "none");
   }
   
   else if (id == "nd_check_media")
   {  el = document.getElementById("nd_media_section");
      el.style.display = (el_in.checked ? "block" : "none");
   }
   
   else if (id == "nd_check_internal")
   {  el = document.getElementById("nd_internal_section");
      el.style.display = (el_in.checked ? "block" : "none");
   }   
   process_needledrop(false);
}

/*
 * TODO:
    capture Ad Agency
 */

function set_no_price()
{  el = document.getElementById("price_status");
   el.innerHTML = "Price for your options: (none)";
}

function process_needledrop(submit_mode)
{ 
   clear_nd_errors();
   form_ok = true;

   // these will be all the prices we're going to check
   prices = new Array();

   // cumulative price for options.  null means not set, -1 means call
   price_total = null;

   // is the 'our Cues will be shown to public via broadcast or Internet' and
   //  'Commercial - Radio, TV' picked?

   if (document.getElementById("nd_check_public").checked &&
       document.getElementById("nd_check_commercial").checked)
   {
   // get agency and cities
      cities = trim(document.getElementById("nd_city_list").value);
      agency = trim(document.getElementById("nd_agency").value);
      project = trim(document.getElementById("nd_project_box").value);

   // get territory choice
      els = document.getElementsByName("nd_territory_group");
      terr_picked = null;
      for (i = 0; i < els.length; i++)
      {
         cb = els[i];
         if (cb.checked)
         {  terr_picked = cb.value
            break;
         }
      }

   // get the broadcast formmat choice(s)
      els = document.getElementsByName("nd_bcast_group");
      bcast_checked = new Array();
      for (i = 0; i < els.length; i++)
      {
         cb = els[i];
         if (cb.checked)
         {  bcast_checked.push(cb.value);
         }
      }

   // get the term
      el = document.getElementById("nd_commercial_term");
      term = el.value;

      if (project == "" && submit_mode)
      {  el = document.getElementById("nd_project_error");
         el.innerHTML = "Please enter a project name";
         form_ok = false;
      }

   // check for errors
      if (bcast_checked.length < 1)
      {  if (!submit_mode)
         {  price_total = -1;
         }
         else
         {  showAlert("Please select at least one Broadcast Format");
         }
         return;
      }
      if (terr_picked == null)
      {  if (!submit_mode)
         {  price_total = -1;
         }
         else
         {  showAlert("Please select at least one Territory");
         }
         return;
      }  

   // kind of a hack, but not easy to get to the territory
   //  DEFINEs in PHP from here.
      else if ((terr_picked == 7 || terr_picked == 8)  &&
         !cities)
      {  if (!submit_mode)
         {  price_total = -1;
         }
         else
         {  showAlert("Please enter the cities where the clip will air");
         }
         return;
      }

   // make prices
      if (price_total != -1)
      {
         for (i = 0; i < bcast_checked.length; i++)
         {
            p_a = new Array();
            p_a.push(terr_picked); 
            p_a.push(bcast_checked[i]);
            p_a.push(term);
            p_a = p_a.sort(numOrdA);
            price_add = get_pricing(p_a);

            if (price_add == null || price_add == -1)
            {  price_total = -1;
            }
            else
            {  price_total += price_add;
            }
         }
      }            

/*         p = new Object();
         p.lic_type_id = 1; // Commercials / Promos
         p.enum_ids = new Array();
         p.enum_ids.push(bcast_checked[i]);
         p.enum_ids.push(terr_picked); 
         p.enum_ids.push(term);
         prices.push(p);*/
   }
   if (document.getElementById("nd_check_public").checked &&
       document.getElementById("nd_check_internet").checked)
   {
      els = document.getElementsByName("nd_internet_type");
      checked = new Array();
      for (i = 0; i < els.length; i++)
      {
         cb = els[i];
         if (cb.checked)
         {  checked.push(cb.value);
         }
      }

   // get the term
      el = document.getElementById("nd_internet_term");
      term = el.value;

   // check for errors
      if (checked.length < 1)
      {  if (!submit_mode)
         {  price_total = -1;
         }
         else
         {  showAlert("Please select at least one Internet usage type");
         }
      }

   // make prices
      if (price_total != -1)
      {
         for (i = 0; i < checked.length; i++)
         {
            p_a = new Array();
            p_a.push(checked[i]);
            p_a.push(term);
            p_a = p_a.sort(numOrdA);
            price_add = get_pricing(p_a);

            if (price_add == null || price_add == -1)
            {  price_total = -1;
            }
            else
            {  price_total += price_add;
            }
         }
      }
   }
   if (document.getElementById("nd_check_public").checked &&
       document.getElementById("nd_check_live").checked)
   {
      els = document.getElementsByName("nd_live_fmt");
      checked = new Array();
      for (i = 0; i < els.length; i++)
      {
         cb = els[i];
         if (cb.checked)
         {  checked.push(cb.value);
         }
      }

   // check for errors
      if (checked.length < 1)
      {  if (!submit_mode)
         {  price_total = -1;
         }
         else
         {  showAlert("Please select at least one Live event usage type");
         }
      }

   // make prices
      if (price_total != -1)
      {
         for (i = 0; i < checked.length; i++)
         {
            p_a = new Array();
            p_a.push(checked[i]);
            p_a = p_a.sort(numOrdA);
            price_add = get_pricing(p_a);

            if (price_add == null || price_add == -1)
            {  price_total = -1;
            }
            else
            {  price_total += price_add;
            }
         }
      }
   }

   if (price_total != null && price_total != -1)
   {  el = document.getElementById("price_status");
      el.innerHTML = "Price for your options: " + price_total.toFixed(2);
   }
   else
   {  set_no_price();
      return;
   }
   return;


   if (form_ok)
   {
      xml  = "<\?xml version=\"1.0\"\?><PriceRequest>";
      for (i = 0; i < prices.length; i++)
      {
         xml += '<Set>' +
            '<LicTypeID>' + p.lic_type_id + '</LicTypeID>';
         xml += '<EnumIDs>';
         for (j = 0; j < p.enum_ids.length; j++)
         {  
            xml += '<EnumID>' + p.enum_ids[j] + '</EnumID>';

         }
         xml += '</EnumIDs>';
         xml += '</Set>';

      }
      xml += "</PriceRequest>";
      xml = "xml=" + escape(xml);

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

function clear_nd_errors()
{
   document.getElementById("nd_errors").innerHTML = "";
//   dump_registration_errors(null);
}

function show_needledrop(show)
{  
   if (standalone)
   {  return;
   }  
   el = document.getElementById("price_status");
   if (el == null)
   {  return;
   }
   el.innerHTML = "Price for your options: (none)";
   el = document.getElementById("needledrop_section");
   if (el == null)
   {  return;
   }
   el.style.display = (show ? "block" : "none");

   if (show != true)
   {  // all done now 
      return;
   }
   update_breadcrumb("NEEDLEDROP", false);

   clear_preview();
   show_login(false);
   show_cart(false);
   show_my_bins(false); 
   show_bin(false, 0); 
   show_info_request(false, 0);
   show_order_summary(false, null);
   show_search(false);
   show_search_form(false);
   show_share(false, null);
   show_data_find(false);
   show_new_releases(false);
   show_cue_sheets(false);
   update_cart_status_line();
   //clear_errors();
}

/*
function dump_registration_errors(e)
{
   el  = document.getElementById("registration_errors_section");
   if (e == null)
   {  out = "";
   }
   else
   {  out = 'Errors:<UL class="errorText">';
      for (i = 0; i < e.length; i++)
      {  out += "<LI class=\"errorText\">" + e[i] + "</LI>";
      }
      out += "</UL>";
   }
   el.innerHTML = out;
}
*/


function price_quote_callback()
{
   if (ajax_needledrop_r.readyState == 4) {
      if (ajax_needledrop_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_needledrop_r.responseXML;

         node = d.getElementsByTagName('Result')[0];
         if (!node ||  node.childNodes[0].nodeValue != "Success")
         {  
            s = "Error talking to server";
            node = d.getElementsByTagName('Error')[0];
            if (node)
            {  s += "\n" + node.childNodes[0].nodeValue;
            }
            showAlert(s);
         }
         else
         {  
            el = document.getElementById("price_status");
            node = d.getElementsByTagName('PriceStatus')[0];
            if (node.childNodes[0].nodeValue == "Quoted")
            {  price = d.getElementsByTagName('Price')[0].childNodes[0].nodeValue;
               el.innerHTML = "Price for your options: " + price;
            }
            else
            {
               el.innerHTML = "Price for your options: CALL";
            }            
         } 
      }
   }
}

