////////////////////////////////////////////////////////////////////
// CICEntity class
CICEntity.CLASS_NAME = 'Entity';

// Static vars/objs
CICEntity.htObj = new Hashtable();

// XML constructor
function CICEntity(xml) {
  this.id = $attr(xml, 'id');
  this.country = $attr(xml, 'cn');
  this.desc = $attr(xml, 'd');
  this.mainExchange = $attr(xml, 'me');
  this.mainSymbol = $attr(xml, 'ms');
  this.numBonds = $attr(xml, 'bond');
  this.numCFDs = $attr(xml, 'cfd');
  this.numForex = $attr(xml, 'fx');
  this.numFutures = $attr(xml, 'fut');
  this.numFuturesOptions = $attr(xml, 'fop');
  this.numIndexes = $attr(xml, 'ind');
  this.numMutualFunds = $attr(xml, 'fund');
  this.numOptions = $attr(xml, 'opt');
  this.numRelatedEntities = $attr(xml, 'nr');
  this.numStocks = $attr(xml, 'stk');
  this.numWarrants = $attr(xml, 'war');

  CICEntity.htObj.put(this.id, this);
}

// Static method to make a entity product link
CICEntity.makeEntityProductLink = function(entityId, linkText, linkUrl) {
  var text = '<a href="javascript:CICFunc.openPageForEntity(\'' + entityId + '\',\'' + linkUrl + '\')">' + linkText + "</a>,&nbsp; ";
  return text;
}

// Instance method to display a row of text on this entity
CICEntity.prototype.getRowText = function() {
  var wlId = $('wlId').value;
  var lang = $('lang').value;

  var prodCountsText = this.getAvailableProductsText();

  var imgStr = "&nbsp;";

  var html = '<tr class="underlying">' +
             '<td><img src="images/flags/' + this.country + '.gif" border=0></td>' +
             '<td><b><a href="javascript:CICFunc.openPageForEntity(\'' + this.id + '\',\'' + CICFunc.BASE_URL + '?action=Advanced Search&entityId=' + this.id + '\')" class="company">' + 
             this.desc + ' (' + this.mainSymbol + '@' + this.mainExchange + ')</a></b></td>' +
             '<td>' + prodCountsText + '</td></tr>';
//             '<td>' + prodCountsText + '</td><td>' + imgStr + '</td></tr>';
  return html;
}

// Get the available products text
CICEntity.prototype.getAvailableProductsText = function() {
  var text = "";

  var linkUrl = null;
  var linkText = null

  if (this.numBonds > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Bond Group&entityId=' + this.id;
    linkText = 'Bond(' + this.numBonds + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numCFDs > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=CFD Search&entityId=' + this.id;
    linkText = 'Contract for Difference (CFD)(' + this.numCFDs + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numForex > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Forex Search&entityId=' + this.id;
    linkText = 'Forex(' + this.numForex + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numFutures > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Futures Search&entityId=' + this.id;
    linkText = 'Futures(' + this.numFutures + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numFuturesOptions > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Futures Option Chain&entityId=' + this.id;
    linkText = 'Futures Options(' + this.numFutures + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  } 
  if (this.numIndexes > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Index Search&entityId=' + this.id;
    linkText = 'Index(' + this.numIndexes + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  } 
  if (this.numMutualFunds > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Mutual Fund Search&entityId=' + this.id;
    linkText = 'Mutual Fund(' + this.numMutualFunds + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numOptions > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Option Chain&entityId=' + this.id;
    linkText = 'Option(' + this.numOptions + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numStocks > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Stock Search&entityId=' + this.id;
    linkText = 'Stock(' + this.numStocks + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numWarrants > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Warrant Chain&entityId=' + this.id;
    linkText = 'Warrants(' + this.numWarrants + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  if (this.numRelatedEntities > 0) {
    linkUrl = CICFunc.BASE_URL + '?action=Related Products Search&entityId=' + this.id;
    linkText = 'Related Products(' + this.numRelatedEntities + ')';
    text += CICEntity.makeEntityProductLink(this.id, linkText, linkUrl);
  }
  text = text.substring(0, text.length-8);
  return text;
}

