if (document.images){
  upImage = new Image();
  upImage.src = "/hsretail/images/icons/arrow-up.gif";
  downImage = new Image();
  downImage.src = "/hsretail/images/icons/arrow-down.gif";
  nochangeImage = new Image();
  nochangeImage.src = "/hsretail/images/icons/arrow-same.gif";
}

var doneCalled = false;

// this matches symbols to the row number
var rowArray = new Array(0);
function mapKey(key){
  rowArray[0] = key;
}

var changeValue = "0.00";

bGColorUp = '#1BA71B';
bGColorDown = '#CC0000';
bGColorNoChange = '#4883C0';
textColorUp = '#FFFFFF';
textColorDown = '#FFFFFF';
textColorNoChange = '#FFFFFF';

function Comma(SS){
  var separated = '', FullNumber = String(SS), numLength = FullNumber.length-1, j;
  for (j=0; j<=numLength; j++){
    separated += FullNumber.charAt(j);
    if ((j<numLength) && ((numLength-j) %3 ==0)) separated += ',';
  }
  return separated;
}

function roundOff(value, precision){
  strValue = "" + value; //convert value to string
  precision = parseInt(precision);
  var whole = Math.round(strValue * Math.pow(10, precision));
  var negative = false;
  if (whole < 0){
    whole = Math.abs(whole);
    negative = true;
  }
  whole = "" + whole;
  var decPoint = whole.length - precision;
  result = whole.substring(0, decPoint);
  if (result >= 1){
    result += ".";
  } else {
    result += "0.";
  }
  result += whole.substring(decPoint, whole.length);
  if (negative) result = "-" + result;
  return result;
}

//converts to two decimal places
function twoDP(theValue){
  if (isNaN(theValue)){
    return '-';
  }else{
    theValue = Math.round(theValue * 100)/100;
    theValue = theValue.toString();
    if (theValue.indexOf('.') == -1) theValue = theValue + '.00';
    return theValue
  }
}

function abbNum(num){
  scaleStr = '';
  if (isNaN(num)){
    return '-';
  }else{
    if (Math.abs(num) >= 1000){
      num = (num / 1000);
      scaleStr = 'k';
      if (Math.abs(num) >= 1000){
        num = (num / 1000);
        scaleStr = 'm';
        if (Math.abs(num) >= 1000){
          num = (num / 1000);
          scaleStr = 'bn';
        }
      }
    }
  }
  abbreviatedNum = roundOff(num, 2);
  abbreviatedNum = abbreviatedNum + "" + scaleStr;
  return abbreviatedNum;
}

function getParameter(msg, param){
  ret = "";
  indx = msg.indexOf(";"+param);
  if (indx > -1){
    nextPara = msg.indexOf(";", indx+1);
    ret = msg.substring(indx+param.length+2, nextPara);
  }
  return ret;
}

function newRecord(theMsg){
  symbol = getParameter(theMsg, 'symbol');
  cid = getParameter(theMsg, 'cid');
  row = getRow(symbol);

  if (row == -1) row = getRow(cid);
  if (row == -1){
    // If row == -1 then nothing will work so terminate it here
  }else{
    cid = "";
    var buyvolume   = -1;
    var sellvolume  = -1;
    var unkwnvolume = -1;
    var theshares = 0;
    var theprice = 0;
    var thechange = 0;
    var theeps = 0;
    var thedps = 0;
    
    cid = symbol = getParameter(theMsg, 'companyid');

    //alert(theMsg);

    var companyType = getParameter(theMsg, 'companyType');
    var doShowPe = (companyType == 'NRM' || companyType == 'INS' || companyType == 'BNK');
    
    doShowPe = true;        
    if (!doShowPe){
      setCell('pe', '_pe', 0, 'N/A', false);
      //setCell('pe', '_pe', 0, companyType, false);
    }else{
      setCell('pe', '_pe', 0, ' ', false);
    }

    theprice = getParameter(theMsg, 'price');
    theshares = getParameter(theMsg, 'shares');
    thechange = getParameter(theMsg, 'change');
    pricechange = getParameter(theMsg, 'pricechg');
    buyvolume = getParameter(theMsg, 'buyvol');
    sellvolume = getParameter(theMsg, 'sellvol');
    unkwnvolume = getParameter(theMsg, 'unkwnvol');
    thedps = getParameter(theMsg, 'dps');
    theeps = getParameter(theMsg, 'eps');

    setCell('price', '_p', row, theprice, false);
    cellColorChange('price', 'p', row, pricechange);
    setBid(row, getParameter(theMsg, 'bid'));
    setAsk(row, getParameter(theMsg, 'ask'));
    setCell('shares', '_sii', row, theshares, true);
    setChange(row, thechange);
    setCell('dayHigh', '_h', row, getParameter(theMsg, 'dayhigh'), false);
    setCell('daylow', '_l', row, getParameter(theMsg, 'daylow'), false);

    //alert(getParameter(theMsg, 'trdtime'));

    tradeDate = new Date(Date.parse(getParameter(theMsg, 'trdtime')));
    //alert(tradeDate);

    //alert((nowDate.getMonth() == tradeDate.getMonth()) + '\n' + nowDate.getMonth() +'\n'+ tradeDate.getMonth());

    if(nowDate.getMonth() == tradeDate.getMonth()){
      if(nowDate.getDate() == tradeDate.getDate()){
        setDayvolume(row, getParameter(theMsg, 'dayvol'));
        //alert(getParameter(theMsg, 'trdprice'));
        setCell('lastTradePrice', '_tp', row, twoDP(getParameter(theMsg, 'trdprice')), false)
        setCell('lastTradevolume', '_tv', row, Comma(getParameter(theMsg, 'trdvol')), false)
        
        setTradeChg(row, getParameter(theMsg, 'trdchg'));
        setLastTradTyp(row, getParameter(theMsg, 'lsttrdtyp'));
        setLasttradetime(row, getParameter(theMsg, 'trdtime'));
      }
    }
    cellColorChange('change', 'c', row, pricechange);
    cellColorChange('changepercent', 'cp', row, pricechange);
    if (theprice != 0 && thechange != 0){
      thechangeperc = ((thechange / getParameter(theMsg, 'lastclose')) * 100);
      thechangeperc = roundOff(thechangeperc, 2);
      setCell('changePerC', '_cp', 0, thechangeperc, false)
    }else if (theprice != 0  && thechange == 0){
      setCell('changePerC', '_cp', 0, 0.0, false)
    }
    if (theprice != 0  && theshares != 0){
      themarketcap = ((theshares * theprice) / 100);
      themarketcap = roundOff(themarketcap, 2);
      setCell('marketCap', '_mc', 0, themarketcap, true)
      cellColorChange('marketCap', 'mc', 0, pricechange)
    }
    if (thedps != 0  && theprice != 0){
      thedy = (((thedps * 100) / theprice) * 100);
      thedy = roundOff(thedy, 2);
      setCell('dy', '_dy', 0, thedy, false)
      cellColorChange('dy', 'dy', 0, pricechange)
    }
    if (theeps != 0  && theprice != 0 && doShowPe){
      thepe = ((theprice / theeps)/100) ;
      thepe = roundOff(thepe, 2);
      setCell('pe', '_pe', 0, thepe, false);
      cellColorChange('pe', 'pe', 0, pricechange);
    }
    if (!doneCalled){
      updateColour(rowNumber);
      updateColour(rowNumber, "trade");
    }
    if (buyvolume != -1  && sellvolume != -1  && unkwnvolume != -1) {
      fnBarChart(buyvolume, sellvolume, unkwnvolume, 160, 14, 'cell_0_ind');
    }
  }
}

function done() {
  doneCalled = true;
  document.getElementById("divLoadingMsgFrame").style.display = "none";
  document.getElementById("divPrices").style.display = "block";
}

function getRow(codename){
  var sCode = new String();
  var sCodeIn = new String();
  sCodeIn = codename;
  rowNumber = -1;
  for (i=0; i<rowArray.length; i++){
    sCode = rowArray[i];
    if (String(sCode) == String(sCodeIn)) rowNumber = i;
  }
  return rowNumber;
}

function setCell(itemName, itemId, rowNumber, data, doAbbNum){
  cellName = "cell_" + rowNumber + itemId;
  if (doAbbNum) data = abbNum(data);
  //alert(data + '\n' + cellName);
  if (data.length != 0 ) document.getElementById(cellName).innerHTML = data;
  // else alert(data + '\n' + cellName + '\n' + data.length);
}

function setTradeChg(rowNumber, data) {
  cellName = "cell_" + rowNumber + "_tc";
  if (data == 'b'){
    data = 'BUY';
    tradeChgUp(rowNumber);
  }else if (data == 's'){
    data = 'SELL';
    tradeChgDown(rowNumber);
  }else if (data == '?'){
    data = 'UNKNOWN';
    tradeChgNoChange(rowNumber);
  }
  document.getElementById(cellName).innerHTML = data;
}

function setLastTradTyp(rowNumber, data){
  cellName = "cell_" + rowNumber + "_tc";
  if (data == 'b'){
    data = 'BUY';
  }else if (data == 's'){
    data = 'SELL';
  }else if (data == '?'){
    data = 'UNKNOWN';
  }
  document.getElementById(cellName).innerHTML = data;
}

function setBid(rowNumber, data) {
  cellName = "cell_" + rowNumber + "_b";
  current = document.getElementById(cellName).innerHTML;
  if (doneCalled){
    if (data > current){
      valueUp(rowNumber, data, 'b');
    }else if (data < current){
      valueDown(rowNumber, data, 'b');
    }else{
      document.getElementById(cellName).innerHTML = data;
    }
  }else{
    document.getElementById(cellName).innerHTML = data;
  }
}

function valueUp(rowNumber, data, cell){
  cellName = "cell_" + rowNumber + "_" + cell;
  document.getElementById(cellName).innerHTML = data;
  document.getElementById(cellName).style.color = '#FFFFFF';
  document.getElementById(cellName).style.backgroundColor = '#1BA71B';
  setTimeout("bidAskNoChange(" + rowNumber + ")", 2000);
}

function valueDown(rowNumber, data, cell){
  cellName = "cell_" + rowNumber + "_" + cell;
  document.getElementById(cellName).innerHTML = data;
  document.getElementById(cellName).style.color = '#FFFFFF';
  document.getElementById(cellName).style.backgroundColor = '#CC0000';
  setTimeout("bidAskNoChange(" + rowNumber + ")", 2000);
}

function bidAskNoChange(rowNumber){
  rowName = "row_" + rowNumber;
  cellName1 = "cell_" + rowNumber + "_b";
  cellName2 = "cell_" + rowNumber + "_a";
  document.getElementById(cellName1).style.backgroundColor = '#FDFDFD';
  document.getElementById(cellName2).style.backgroundColor = '#FDFDFD';
  colourString = '#4883C0';
  if (changeValue > 0.00){
    colourString = '#1BA71B';
  }else if (changeValue < 0.00){
    colourString = '#CC0000';
  }
  setColour(rowNumber, "b", colourString);
  setColour(rowNumber, "a", colourString);
}

function setAsk(rowNumber, data){
  cellName = "cell_" + rowNumber + "_a";
  current = document.getElementById(cellName).innerHTML;
  if (doneCalled){
    if (data > current){
      valueUp(rowNumber, data, 'a');
    }else if (data < current){
      valueDown(rowNumber, data, 'a');
    }else{
      document.getElementById(cellName).innerHTML = data;
    }
  }else{
    document.getElementById(cellName).innerHTML = data;
  }
}

function setChange(rowNumber, data){
  changeValue = data;
  if (data > 0.00) {
    up(rowNumber);
  }else if (data < 0.00){
    down(rowNumber);
  }else{
    nochange(rowNumber);
  }
  if (data > 0.00) data = "+" + data;
  cellName = "cell_" + rowNumber + "_c";
  document.getElementById(cellName).innerHTML = data;
}

function setDayvolume(rowNumber, data) {
  // Note: Calling this method makes the page hang (no errors), even
  // though the method returns correctly.
  //    formattedData = formatLong(data);
  //    document.write(formattedData);
  data = Comma(data);
  cellName = "cell_0_dv";
  document.getElementById(cellName).innerHTML = data;
}

function setLasttradetime(rowNumber, data){
  tradeTime = new Date(Date.parse(data));
  formattedTime = formatDate(tradeTime);
  cellName = "cell_" + rowNumber + "_tt";
  document.getElementById(cellName).innerHTML = formattedTime;
}

function formatDate(dateIn){
  hour = new String(dateIn.getHours());
  minute = new String(dateIn.getMinutes());
  second = new String(dateIn.getSeconds());
  if (hour.length == 1) hour = "0" + hour;
  if (minute.length == 1) minute = "0" + minute;
  if (second.length == 1) second = "0" + second;
  dateOut = hour + ":" + minute + ":" + second;
  return dateOut;
}

function up(imageNumber){
  try{
    ImageName = "jimage_" + imageNumber;
    document.images[ImageName].src = upImage.src;
  }catch (exception){
    alert(ImageName + " doesn't exist. " + exception.description);
  }
}

function down(imageNumber){
  try{
    ImageName = "jimage_" + imageNumber;
    document.images[ImageName].src = downImage.src;
  }catch (exception){
    alert(ImageName + " doesn't exist. " + exception.description);
  }
}

function nochange(imageNumber){
  try{
    ImageName = "jimage_" + imageNumber;
    document.images[ImageName].src = nochangeImage.src;
  }catch (exception){
    alert(ImageName + " doesn't exist. " + exception.message);
  }
}

function noimage(imageNumber){
  ImageName = "jimage_" + imageNumber;
  document.images[ImageName].style.visibility = "hidden";
}

function cellColorChange(itemName, itemId, rowNumber, move){
  //itemName for reference
  if (move == '+'){
    bGColorThis = bGColorUp;
    textColorThis = textColorUp;
  }else if (move == '-'){
    bGColorThis = bGColorDown;
    textColorThis = textColorDown;
  }else{
    bGColorThis = bGColorNoChange;
    textColorThis = textColorNoChange;
  }
  rowName = "row_" + rowNumber;
  cellName1 = "cell_" + rowNumber + "_" + itemId;
  if (doneCalled || move == '.') document.getElementById(cellName1).style.backgroundColor = bGColorThis;
  if (doneCalled && move != '.') document.getElementById(cellName1).style.color = textColorThis;
  if (move != '.') setTimeout("cellColorChange('"+itemName+"','"+itemId+"','"+rowNumber+"','.')", 2000);
  else updateColour(rowNumber);
}

function tradeChgUp(rowNumber){
  if (doneCalled){
    rowName = "row_" + rowNumber;
    cellName1 = "cell_" + rowNumber + "_tc";
    cellName2 = "cell_" + rowNumber + "_tp";
    cellName3 = "cell_" + rowNumber + "_tv";
    cellName4 = "cell_" + rowNumber + "_tt";
    cellName5 = "cell_" + rowNumber + "_dv";
    document.getElementById(cellName1).style.backgroundColor = bGColorUp;
    document.getElementById(cellName1).style.color = textColorUp;
    document.getElementById(cellName2).style.backgroundColor = bGColorUp;
    document.getElementById(cellName2).style.color = textColorUp;
    document.getElementById(cellName3).style.backgroundColor = bGColorUp;
    document.getElementById(cellName3).style.color = textColorUp;
    document.getElementById(cellName4).style.backgroundColor = bGColorUp;
    document.getElementById(cellName4).style.color = textColorUp;
    //document.getElementById(cellName5).style.backgroundColor = '#FFCC00';
    //document.getElementById(cellName5).style.color = '#000000';
    setTimeout("tradeSetLast(" + rowNumber + ", 'up')", 2000);
  }
}

function tradeChgDown(rowNumber){
  if (doneCalled){
    rowName = "row_" + rowNumber;
    cellName1 = "cell_" + rowNumber + "_tc";
    cellName2 = "cell_" + rowNumber + "_tp";
    cellName3 = "cell_" + rowNumber + "_tv";
    cellName4 = "cell_" + rowNumber + "_tt";
    cellName5 = "cell_" + rowNumber + "_dv";
    document.getElementById(cellName1).style.backgroundColor = bGColorDown;
    document.getElementById(cellName1).style.color = textColorDown;
    document.getElementById(cellName2).style.backgroundColor = bGColorDown;
    document.getElementById(cellName2).style.color = textColorDown;
    document.getElementById(cellName3).style.backgroundColor = bGColorDown;
    document.getElementById(cellName3).style.color = textColorDown;
    document.getElementById(cellName4).style.backgroundColor = bGColorDown;
    document.getElementById(cellName4).style.color = textColorDown;
    //document.getElementById(cellName5).style.backgroundColor = '#FFCC00';
    //document.getElementById(cellName5).style.color = '#000000';
    setTimeout("tradeSetLast(" + rowNumber + ", 'down')", 2000);
  }
}

function tradeSetLast(rowNumber, type){
  if (type == "up") colourString = '#1BA71B';
  else colourString = '#CC0000';
  rowName = "row_" + rowNumber;
  cellName1 = "cell_" + rowNumber + "_tc";
  cellName2 = "cell_" + rowNumber + "_tp";
  cellName3 = "cell_" + rowNumber + "_tv";
  cellName4 = "cell_" + rowNumber + "_tt";
  cellName5 = "cell_" + rowNumber + "_dv";
  document.getElementById(cellName1).style.backgroundColor = '#FDFDFD';
  document.getElementById(cellName1).style.color = colourString;
  document.getElementById(cellName2).style.backgroundColor = '#FDFDFD';
  document.getElementById(cellName2).style.color = colourString;
  document.getElementById(cellName3).style.backgroundColor = '#FDFDFD';
  document.getElementById(cellName3).style.color = colourString;
  document.getElementById(cellName4).style.backgroundColor = '#FDFDFD';
  document.getElementById(cellName4).style.color = colourString;
  //document.getElementById(cellName5).style.backgroundColor = '#FDFDFD';
  //document.getElementById(cellName5).style.color = '#000000';
}

function tradeChgNoChange(rowNumber){
  rowName = "row_" + rowNumber;
  cellName1 = "cell_" + rowNumber + "_tc";
  cellName2 = "cell_" + rowNumber + "_tp";
  cellName3 = "cell_" + rowNumber + "_tv";
  cellName4 = "cell_" + rowNumber + "_tt";
  cellName5 = "cell_" + rowNumber + "_dv";
  document.getElementById(cellName1).style.backgroundColor = bGColorNoChange;
  document.getElementById(cellName1).style.color = textColorNoChange;
  document.getElementById(cellName2).style.backgroundColor = bGColorNoChange;
  document.getElementById(cellName2).style.color = textColorNoChange;
  document.getElementById(cellName3).style.backgroundColor = bGColorNoChange;
  document.getElementById(cellName3).style.color = textColorNoChange;
  document.getElementById(cellName4).style.backgroundColor = bGColorNoChange;
  document.getElementById(cellName4).style.color = textColorNoChange;
  //document.getElementById(cellName5).style.backgroundColor = '#FDFDFD';
  //document.getElementById(cellName5).style.color = '#000000';
  updateColour(rowNumber, "trade");
}

function updateColour(rowNumber, type){
  colourString = '#4883C0';
  if (changeValue > 0.00){
    colourString = '#1BA71B';
  }else if (changeValue < 0.00){
    colourString = '#CC0000';
  }
  if (type == "trade"){
    setColour(rowNumber, "tc", colourString);
    setColour(rowNumber, "tp", colourString);
    setColour(rowNumber, "tv", colourString);
    setColour(rowNumber, "tt", colourString);
    //setColour(rowNumber, "dv", colourString);
  } else {
    setColour(rowNumber, "c", colourString);
    //setColour(rowNumber, "h", colourString);
    //setColour(rowNumber, "l", colourString);
    setColour(rowNumber, "p", colourString);
    setColour(rowNumber, "cp", colourString);
    //setColour(rowNumber, "sii", colourString);
    setColour(rowNumber, "mc", colourString);
    setColour(rowNumber, "pe", colourString);
    setColour(rowNumber, "dy", colourString);
  }
  if (!doneCalled){
    setColour(rowNumber, "b", colourString);
    setColour(rowNumber, "a", colourString);
  }
}

function setColour(row, column, colour){
  cellName = "cell_" + row + "_" + column;
  document.getElementById(cellName).style.color = colour;
  document.getElementById(cellName).style.backgroundColor = '#FDFDFD';
}

function setError(message){
  document.getElementById("status").innerHTML = "";
  document.getElementById("status").innerHTML = message;
  document.getElementById("status").style.color = '#CC0000';
}

