// Lightstreamer Basic Portfolio Demo
// Order Entry Management

//////////////// Drop-down List Composition

// The list of stocks available on the market would be normally provided
// by an application server, based on some search criteria and on the user
// profile. In this sample, we will simply hard-wire on the Client the list
// of stocks managed by the QUOTE_ADAPTER Data Adapter.

var availStocks = ["item1","item2","item3","item4","item5",
                   "item6","item7","item8","item9","item10",
                   "item11","item12","item13","item14","item15",
                   "item16", "item17", "item18", "item19", "item20",
                   "item21", "item22", "item23", "item24", "item25",
                   "item26", "item27", "item28", "item29", "item30"];

// populate the HTML select element
var select = document.getElementById("stockN");
for (var i = 0; i < availStocks.length; i++) {
  var option = document.createElement("option");
  if (i == 0) {
    option.selected = true;
  }
  option.value = availStocks[i];
  option.innerHTML = availStocks[i];
  select.appendChild(option);
}

//////////////// Order Submission Management

// In order to simplify the deployment of this sample, the "sendMessage"
// facility is used for order submission. In this way, orders are handled
// by Lightstreamer Server directly, without the need for an additional
// Web/application server.

// Please, consider that it is not recommended to use "sendMessage" as a
// message delivery channel from the Client to the Server in mission critical
// scenarios. In particular, no acknowledgement of the reception and
// acceptance of the message by the Server is received by the Web Client.
// The recommended architecture is based on traditional patterns: the Client
// sends messages to a Web/application server, which publishes them on a 
// message bus (e.g. JMS); a Lightstreamer Data Adapter subscribes to these
// messages through the bus. In this way, clustering is fully supported.

function submitForm(op) {
  var name = document.getElementById("stockN");
  var qtyN = document.getElementById("qtyN");

  if (!window.engineRef || !window.portfolioId || !op || !name || !qtyN) {
    //this shouldn't happen
    return;
  }

  name = name.value;
  qtyN = qtyN.value;

  if (!qtyN || !name) {
    alert("No empty fields admitted. Please fill the 'quantity' field and choose a stock.");
  } else {
    var wrong = false;

    if (isNaN(qtyN)) {
      alert("Only digits are admitted for the 'quantity' field. Please re-type the value.");
      return;
    }

    var mex = op + "|" + portfolioId + "|" + name + "|" + qtyN;
    engineRef.sendMessage(mex);
  }
}

function enableForm() {
  document.getElementById("buy").disabled = false;
  document.getElementById("sell").disabled = false;
}

function disableForm() {
  document.getElementById("buy").disabled = true;
  document.getElementById("sell").disabled = true;
}