
function addListingSubmit() {
  var listingRec = {
	  'category_type': 'Material',
    'type': x$('add_listing_type').value(),
    'count': x$('add_listing_count').value(),
    'months': x$('add_listing_months').value(),
    'business_logo': x$('add_listing_business_logo').value(),
    'website_link': x$('add_listing_website_link').value(),
    'hotprice': x$('add_listing_hotprice').value()
  };
  
  addListingRecord(listingRec);
}

function addBusinessAdSubmit() {
  var listingRec = {
    'type': x$('business_add_listing_type').value(),
    'count': x$('business_add_listing_count').value(),
    'months': x$('business_add_listing_months').value()
  };
  
  addListingRecord(listingRec);
}

function addListingRecord(listingRec) {
  beginAddingAjaxMessagesToQueue();
  sendAjaxMessage('Pricing', 'addListing', {'listing':listingRec});
  sendAjaxMessage('Pricing', 'fetchListingsTable', null, fetchListingsTableCallback);
  sendAjaxMessage('Pricing', 'fetchTotalsTable', null, fetchTotalsTableCallback);
  sendQueuedAjaxMessages();
}

function fetchTotalsTableCallback(html) {
  document.getElementById('token-calculator-totals').innerHTML = html;
}

function removeListing(index) {
  beginAddingAjaxMessagesToQueue();
  sendAjaxMessage('Pricing', 'removeListing', {'index':index});
  sendAjaxMessage('Pricing', 'fetchListingsTable', null, fetchListingsTableCallback);
  sendAjaxMessage('Pricing', 'fetchTotalsTable', null, fetchTotalsTableCallback);
  sendQueuedAjaxMessages();
}

function fetchListingsTableCallback(html) {
  document.getElementById('listings_container').innerHTML = html;
}

function updateListing(index) {
  var listingRec = {
    'index': index,
    'type': x$('listing_type['+index+']').value(),
    'count': x$('listing_count['+index+']').value(),
    'months': x$('listing_months['+index+']').value(),
    'business_logo': x$('listing_business_logo['+index+']').value(),
    'website_link': x$('listing_website_link['+index+']').value(),
    'hotprice': x$('listing_hotprice['+index+']').value()
  };
  
  beginAddingAjaxMessagesToQueue();
  sendAjaxMessage('Pricing', 'updateListing', {'listing':listingRec}, updateListingCallback);
  sendAjaxMessage('Pricing', 'fetchTotalsTable', null, fetchTotalsTableCallback);
  sendQueuedAjaxMessages();
}

function updateListingCallback(response) {
  document.getElementById('listing_subtotal['+response.index+']').innerHTML = response.listing_subtotal;
}

function showBusinessAdForm() {
  x$('show_business_options_btn').hide();
  x$('business_options_form').show();
}

