<!--
// CONSTANTS
var separator = ","; // use comma as 000's separator
var decpoint = "."; // use period as decimal point
var percent = "%";
var currency = "$"; // use dollar sign for currency

function formatNumber(number, format, print) 
{ // use: formatNumber(number, "format")

  if (print) 
    document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

  if (number - 0 != number) 
    return null; // if number is NaN return null
  var useSeparator = format.indexOf(separator) != -1; // use separators in number
  var usePercent = format.indexOf(percent) != -1; // convert output to percentage
  if (usePercent) 
    number *= 100;
  var useCurrency = format.indexOf(currency) != -1; // use currency format
  format = strip(format, separator + percent + currency); // remove key characters
  number = "" + number; // convert number input to string

  // split number and format into LHS and RHS using decpoint as divider
  var dec = number.indexOf(decpoint) != -1;
  var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
  var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";
  dec = format.indexOf(decpoint) != -1;
  var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
  var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";
  if (nrightEnd.length > 3)
    var srightEnd = (dec) ? format.substring(format.indexOf(".") + 2) : "";

   // adjust decimal places by cropping or adding zeros to LHS of number
  if (srightEnd.length < nrightEnd.length) 
  {
    var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
    nrightEnd = nrightEnd.substring(0, srightEnd.length);
    if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1); // round up
    if (srightEnd.length < nrightEnd.length) 
    {
      nrightEnd = nrightEnd.substring(1);
      nleftEnd = (nleftEnd - 0) + 1;
    }
  } 
  else 
  {
    for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) 
    {
      if (srightEnd.charAt(i) == "0") 
        nrightEnd += "0"; // append zero to RHS of number
      else break;
    }
  }
   // adjust leading zeros
  sleftEnd = strip(sleftEnd, "#"); // remove hashes from LHS of format
  while (sleftEnd.length > nleftEnd.length)
    nleftEnd = "0" + nleftEnd; // prepend zero to LHS of number
   if (useSeparator) 
     nleftEnd = separate(nleftEnd, separator); // add separator
  var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : ""); // combine parts
  return ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
}
function strip(input, chars) 
{ // strip all characters in 'chars' from input
  var output = ""; // initialise output string
  for (var i=0; i < input.length; i++)
    if (chars.indexOf(input.charAt(i)) == -1)
      output += input.charAt(i);
  return output;
}
function separate(input, separator) 
{ // format input using 'separator' to mark 000's
  var output = ""; // initialise output string
  for (var i=0; i < input.length; i++) 
  {
    if (i != 0 && (input.length - i) % 3 == 0) output += separator;
    output += input.charAt(i);
  }
  return output;
}

function LTrim(str) 
{
  var s = new String(str);
  while (s.charAt(0) == ' ')
    s = s.substring(1);
  return s;
}

function LTrimZero(str) 
{
  var s = new String(str);
  while (s.charAt(0) == '0')
    s = s.substring(1);
  return s;
}

function RTrim(str) 
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" ");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) 
  {
    // We have a string with trailing blank(s)...
    var i = s.length - 1;       // Get length of string
    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;

    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }
  return s;
}
/*
function formatPrice(number) 
{
   return formatQty(Math.floor(number-0) + '') + formatcent(number - 0);
}

function formatQty(number) 
{
  if (number.length <= 3)
      return (number == '' ? '0' : number);
  else 
  {
      var mod = number.length%3;
      var output = (mod == 0 ? '' : (number.substring(0,mod)));
      for (i=0 ; i < Math.floor(number.length/3) ; i++) 
      {
          if ((mod ==0) && (i ==0))
              output+= number.substring(mod+3*i,mod+3*i+3);
          else
              output+= ',' + number.substring(mod+3*i,mod+3*i+3);
      }
      return (output);
  }
}

function formatcent(amount) 
{
  var output;
  amount = Math.round( ( (amount) - Math.floor(amount) ) *1000);
  if (amount < 10)
    output = '.00'+amount;
  else if (amount < 100)
    output = '.0'+amount;
  else
    output = '.'+amount;

  return(output);
  //return (amount < 10 ? '.0' + amount : '.' + amount);
}
*/
function checkNaN(str)
{
  var bVal = false;
  if (isNaN(str) == false)
  {
    if (str.indexOf(".") == -1)
      bVal = true;
  }
  return bVal;
}

function isEmpty(str)	
{
  var b = false;
		
  strTemp = new String(str);	
  if (strTemp == null)
		b = true;
  else 
		if (strTemp == 'undefined' || strTemp=='null'|| strTemp.length == 0) 
			b = true;
	
  return b;
}

function ValidateCSWPrice(Price)
{
	if (Price == "-")
		Price = 0;
		
	return Price;
}

function mOvr(src, color)
{ 
  if (!src.contains(event.fromElement))
  { 
    src.style.cursor = 'hand'; 
    //src.bgColor = '#DEDEEF'
    src.bgColor = color
  } 
} 

function mOut(src, color)
{ 
  if (!src.contains(event.toElement))
  { 
    src.style.cursor = 'default'; 
    src.bgColor = color;
  } 
}

function OnMSBanner()
{
	window.open(URL_MSBANNER);
}

function FormatPrice(number)
{
	number = new String(number);
	if (number != "-")
	{
		var Pos = number.indexOf("-");
		if (Pos != -1)
		{
			var newnum = number.substr(1, String(number).length-1);
			newnum = formatNumber(newnum,",###.000");		
			number = "-" + newnum;
		}
		else
			number = formatNumber(number,",###.000");		
	}
		
	return number;
}

function FormatQty(number)
{
	if (number != "-")
	{
		var Pos = number.indexOf("-");
		if (Pos != -1)
		{
			var newnum = number.substr(1, String(number).length-1);
			newnum = formatNumber(newnum,"###,###");		
			number = "-" + newnum;
		}
		else
			number = formatNumber(number,"###,###");		
	}
		
	return number;
}

function Round(number,X) 
{
// rounds number to X decimal places, defaults to 2
  X = (!X ? 2 : X);
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function PriceChange(LastDone, RefPrice)
{
	var strChange = "";
	if (LastDone == "-" || RefPrice == "-")
		strChange = "-";
	else
	{
		var change = Round((LastDone - RefPrice), 3);
		change = formatNumber(change,",###.000");
		//var change = ValidatePFPrice((LastDone - RefPrice));
		if (change > 0)
		{
			strChange = change+"<img SRC=\"images/arrow_up.gif\" border=\"0\" align=\"absmiddle\">";
		}
		else if (change < 0)
		{
			strChange = "<font color=\"red\">"+change+"<img SRC=\"images/arrow_down.gif\" border=\"0\" align=\"absmiddle\"><\/font>";
		}
		else
			strChange = formatNumber(0,",###.000")+"<img SRC=\"images/arrow_neutral.gif\" border=\"0\" align=\"absmiddle\">";
	}
	return strChange;
}

function CheckPriceBid(Price)
{
	var nPrice,PriceBid,NearestPrice;
  nPrice = Math.floor(Price*1000);
  if (nPrice <= 0 || nPrice >= 1000000)
  {
		alert("This is an invalid price. Please try again.");  
    return 0;
  }
  else
  {
		if (nPrice >= 100000) 
		  PriceBid = 500; //0.5
		else if (nPrice >= 25000)
		  PriceBid = 250; //0.25
		else if (nPrice >= 10000)
		  PriceBid = 100; //0.1
		else if (nPrice >= 5000)
		  PriceBid = 50; //0.05
		else if (nPrice >= 3000)
		  PriceBid = 20; //0.02
		else if (nPrice >= 1000)
		  PriceBid = 10; //0.01
		else 
		  PriceBid = 5; //0.005

		NearestPrice = parseInt(nPrice / PriceBid);
		NearestPrice = parseInt(NearestPrice * PriceBid);
		if (nPrice != NearestPrice)
		{
		  nPrice = NearestPrice;
		  var sTemp = (parseFloat(nPrice)) / 1000.0;
		  alert("Invalid price bid, nearest price used.");    		  
		  return Round(sTemp,3);
		}
		return Round((parseFloat(nPrice) / 1000.0),3);  
  }
}

function CalculateTransactionCost(StockName, Quantity, Price, StampDutyMin, StampDutyMax, StampDutyVal, ClearingFee, MaxClearingFee, MinBrokNormal, MinBrokOther, BrokerageRate)
{
	var TransactionCost = 0;

	if (isEmpty(StampDutyMin) || isEmpty(BrokerageRate))
	{
		StampDutyMin = TCOST_STAMPDUTY;
		StampDutyMax = TCOST_STAMPDUTYMAX;
		StampDutyVal = TCOST_STAMPDUTYVAL;
		ClearingFee = TCOST_CLEARINGFEE;
		MaxClearingFee = TCOST_CLEARINGFEEMAX;
		MinBrokNormal = TCOST_MINBROKNORMAL;
		MinBrokOther = TCOST_MINBROKOTHER;
		BrokerageRate = TCOST_BROKERAGERATE;
	}

	var StockValue = CalculateStockValue(Quantity, Price);
	TransactionCost += CalculateBrokerage(StockName, BrokerageRate, StockValue, MinBrokNormal, MinBrokOther) * 1;
	TransactionCost += CalculateStampDuty(StockValue, StampDutyMin, StampDutyMax, StampDutyVal) * 1;
	TransactionCost += CalculateClearingFee(StockValue, ClearingFee, MaxClearingFee) * 1;
	return TransactionCost;
}

function CalculateStockValue(Quantity, Price)
{
	return Quantity * Price;
}

function CalculateBrokerage(StockName, BrokerageRate, StockValue, MinBrokNormal, MinBrokOther)
{
	var Brokerage = (BrokerageRate * StockValue) / 100;
	var dBrokerage = Brokerage * 100;
	var iBrokerage = parseInt(dBrokerage);
	if ((dBrokerage - iBrokerage) >= 0.5)
		iBrokerage = iBrokerage + 1;

	Brokerage = iBrokerage / 100;
	var MinBrokerage = 0;
	if (StockName.indexOf(STOCKNAMEPATTERNBOND) != -1)
		MinBrokerage = 	MinBrokOther;
	else
		MinBrokerage = MinBrokNormal;
	
	if (Brokerage < MinBrokerage)
		Brokerage = MinBrokerage;

	return Brokerage;
}

function CalculateStampDuty(StockValue, StampDutyMin, StampDutyMax, StampDutyVal)
{
	var StampDuty = 0;
	if (StampDutyMin > 0 && StampDutyMax > 0)
	{
		var dMultiplier = StockValue / StampDutyMax;
		var iMultiplier = parseInt(dMultiplier);
		if (dMultiplier - iMultiplier > 0)
			iMultiplier = iMultiplier + 1;
			
		StampDuty = StampDutyMin * iMultiplier;
	}
	if (StampDuty > StampDutyVal)
		StampDuty = StampDutyVal;

	return StampDuty;
}

function CalculateClearingFee(StockValue, ClearingFee, MaxClearingFee)
{
	var ClearingFeeInDollar = (StockValue * ClearingFee) / 100;
	//round up
	ClearingFeeInDollar = ClearingFeeInDollar * 100;

  if (Round(ClearingFeeInDollar,2) != Round(parseInt(ClearingFeeInDollar),2))
    ClearingFeeInDollar = parseInt(ClearingFeeInDollar) + 1;
    
  ClearingFeeInDollar = ClearingFeeInDollar / 100;
  	
  if (ClearingFeeInDollar > MaxClearingFee)
    ClearingFeeInDollar = MaxClearingFee;

  return ClearingFeeInDollar;
}

function GenerateMarketInfo(StockArray, InfoType)
{
	var Title = "";
	if (InfoType == MARKETINFO_ACTIVE)
		Title = MARKETINFOMSG_ACTIVE;
	else if (InfoType == MARKETINFO_GAINER)
		Title = MARKETINFOMSG_GAINER;
	else
		Title = MARKETINFOMSG_LOSER;
		
	var StrTable = "<table width=\"225\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"marketinfo\">";
	StrTable += "<tr align=\"center\"><td colspan=\"4\"><b>"+Title+"<\/b><\/td><\/tr>";
	for (var x=0; x<StockArray.length; x++)
	{
		if (x % 2 == 0)
			StrTable += "<tr bgcolor=\"#FFFFFF\">";
		else
			StrTable += "<tr>";
		StrTable += "<td width=\"75\">"+RTrim(StockArray[x][MKTINFOARR_STK])+"<\/td>";
		StrTable += "<td width=\"50\" align=\"right\">"+StockArray[x][MKTINFOARR_LD]+"<\/td>";
		StrTable += "<td width=\"50\" align=\"right\">"+StockArray[x][MKTINFOARR_CHG]+"<\/td>";
		StrTable += "<td width=\"50\" align=\"right\">"+StockArray[x][MKTINFOARR_VOL]+"<\/td>";
		StrTable += "<\/tr>";
	}
	StrTable += "<\/table>";
	return StrTable;
}

function OnReload()
{
	window.location.reload()
}

function SetTimer()
{
	self.setTimeout("OnReload()", 60000);
}

function OnDownload(type, fname, action)
{
  var w=650, h=400, t=(screen.height - (h+150))/2, l=(screen.width - w)/2;
  var url = "downloadsession.asp?type="+type+"&fn="+escape(fname)+"&action="+action;
  var ExtDoc = "doc";
  var ExtFile = fname.substr((fname.length - 3), 3);
  if (ExtFile == ExtDoc)
    action = 'D';
    
  if (action == DOWNLOADFLAG_DOWNLOAD)
    window.location = "downloadfrm.asp?type="+type+"&fn="+escape(fname)+"&action="+action;
  else
  {
    var openwin = window.open(url, "openwin", "top="+t+",left="+l+", width="+w+", height="+h+",toolbar=1,location=1,menubar=1,status=1,scrollbars=1,resizable=1");
    openwin.focus();
  }
}

function ValidateStr(DataStr, Type)
{
  var bAllowed = true;
  var CheckStr = "";
  if (Type == CHECKFLAG_EMAIL)
  {
		CheckStr = CHECKESTR_EMAIL;
		if (DataStr.indexOf("@") == -1)
			return false;
	}
	else
		CheckStr = CHECKSTR_USER;
		
  for (i=0; i<DataStr.length && bAllowed; i++)
  {
    var bChecked = false;
    ch = DataStr.charAt(i);
    for (j = 0; j < CheckStr.length; j++)
    {
      if (ch == CheckStr.charAt(j))
      {
        bChecked = true;
        break;
      }
    }
    if (j == CheckStr.length)
    {
      if (!bChecked)
        bAllowed = false;
    }
  }
	return bAllowed;
}

function WinPopup(url, name, width, height, top, left)
{
	var w = width, h = height, l = (screen.Width-w)/2, t = (screen.Height-h)/2;
	var setting = "top="+t+",left="+l+",width="+w+",height="+h+",toolbar=0,location=0,menubar=0,status=1,location=0,scrollbars=1,resizable=1";
	var NewWin = window.open(url,name,setting);
	NewWin.focus();
}
//-->
