﻿// JScript File

function RefreshButton_onclick(strSymbol) 
{
    ret = WSGetQuote.WM_GetQuote(strSymbol, OnComplete, OnError, strSymbol);
    gSetInnerHTML("PC_" + strSymbol, "<img alt='loading...' border=0 src='img/loading.gif'/>");
    gSetInnerHTML("LV_" + strSymbol, "");
    gSetInnerHTML("PV_" + strSymbol, "");
    
    return true;   
}
function OnComplete(ret, strContext, strMethodName)
{
    strSymbol = strContext;
    if(( gIsNull(strSymbol) || strSymbol.length <= 0) || (gIsNull(ret) || ret.length <= 0))
    {
        gSetInnerHTML("PC_" + strSymbol, "<small><font color=red>Error</font><small>");
        return;
    }
    
    var vLatestValueTime, vLatestValue, vPredictChange;
    var strRets = ret.split('|');
	for(var i=0;i < strRets.length;i++) 
	{
		var c = strRets[i];
		while (c.charAt(0)==' ') 
		    c = c.substring(1,c.length);
		    
		switch(i)
		{
		    case 0:
		        vLatestValueTime = c;
		        break;
		    case 1:
		        vLatestValue = c;
		        break;
		    case 2:
		        vPredictChange = c;
		        break;
		}
	}
	var vpv = "";
	if(vPredictChange != "")
	{
	    vpv = parseFloat(vLatestValue) * (100 + parseFloat(vPredictChange))/100;
	    vpv = Math.round(100 * vpv)/100;
	}
	if(vpv == "")
	{
	    gSetInnerHTML("PV_" + strSymbol, "");
	    gSetInnerHTML("PC_" + strSymbol, "<small><font color=red>Error</font><small>");
	}
    else if(vPredictChange.length > 1 && vPredictChange.charAt(0) == '-')
    {
        vPredictChange = "<span color=red><b><img border=0 src='img/down.gif'>&nbsp;" + vPredictChange + "%</b></span>";
        gSetInnerHTML("PV_" + strSymbol, "<big><b><span color=red>" + vpv + "</span></b><big>");
        gSetInnerHTML("PC_" + strSymbol, vPredictChange);
    }
    else if(ret.length > 1)
    {
        vPredictChange = "<span color=green><b><img border=0 src='img/up.gif'>&nbsp;" + vPredictChange + "%</b></span>";
        gSetInnerHTML("PV_" + strSymbol, "<big><b><span color=green>" + vpv + "</span></b></big>");
        gSetInnerHTML("PC_" + strSymbol, vPredictChange);
    }
    
    gSetInnerHTML("LV_" + strSymbol, "<big><b>" + vLatestValue + "</b></big> &nbsp;<small>" + vLatestValueTime + "</small>");   
}

function OnError(args)
{
}
function RefreshButton_onmouseover(obj)
{
    obj.style.textDecoration = "underline";
    obj.style.cursor = "hand";
}
function RefreshButton_onmouseout(obj)
{
    obj.style.textDecoration = "none";
    obj.style.cursor = "";
}
function ButtonDel_onclick(nIndex, strSymbol)
{
    //1. Delete it from table
    tableObj = gGetObject("GridViewSymbols");
    tableObj.obj.deleteRow(nIndex);
    
    //Change all others color
    for(i = nIndex ; i < tableObj.obj.rows.length; i++)
    {
        cur_row = tableObj.obj.rows[i];
        if(i % 2 == 1)
            cur_row.style.backgroundColor = "#EFF3FB";
        else
            cur_row.style.backgroundColor = "";
            
        if(i != tableObj.obj.rows.length - 1)
        {
            cur_row.cells[0].innerHTML = i;
            var v = cur_row.cells[1].innerText;
            
            cur_row.cells[6].innerHTML = "<img  alt='Delete' src='img/delete.gif' border='0' title='Click to delete this symbol' onclick='ButtonDel_onclick(" + i +", \"" + v +"\");' onmouseover='RefreshButton_onmouseover(this);' onmouseout='RefreshButton_onmouseout(this);' />";
        }
    }
    
    //alert(tableObj.obj.innerHTML);
    //2. Delete it from cookie
    var strList = readCookie("SYMBOLS");
  
   if(gIsNull(strList) || strList.length == 0)
        return;
   strSymbol = strSymbol.toUpperCase();
   strList = strList.replace(strSymbol, "");
   strList = strList.replace("++", "+");
   if(strList.charAt(strList.length - 1) == "+")
      strList = strList.substr(0, strList.length - 1);
   createCookie("SYMBOLS", strList, 3000); 
}

function ButtonAdd_onclick()
{
  newSymbol = gGetObject("TextNewSymbol");
  if(gIsNull(newSymbol) || gIsNull(newSymbol.obj))
  {
        alert("Please enter a valid symbol.   ");
        return;
  }
  if(newSymbol.obj.value == null || newSymbol.obj.value.length <= 0)  
  {
        alert("Please enter a valid symbol.  ");
        return;
  }
  
  var strNewSymbol = newSymbol.obj.value.toUpperCase();
  
  //Read setting back from cookie and make sure no duplicated
  var strList = readCookie("SYMBOLS");
  if(!gIsNull(strList) && strList.indexOf(strNewSymbol) >= 0)
  {
      alert(strNewSymbol + " is already in the list.  " );
      return;
  }
  
  //Add this new item into cookie
  if(gIsNull(strList) || strList.length == 0)
    strList = strNewSymbol;
  else
    strList += "+" + strNewSymbol;
    
  createCookie("SYMBOLS", strList, 3000);  
  
  AddNewRow(strNewSymbol);
  
  textNewSymbol = gGetObject("TextNewSymbol");
  if(gIsNull(textNewSymbol))
    return;
    
  textNewSymbol.obj.value = "";
  
  RefreshButton_onclick(strNewSymbol);
}

function AddNewRow(strNewSymbol)
{
  //Get the table object
  tableObj = gGetObject("GridViewSymbols");
  nRowNumber = tableObj.obj.rows.length - 1;
  
  lastRow = gGetObject("NewSymbolRow");
  
  //Insert a new row
  trObj = tableObj.obj.insertRow(nRowNumber);
  if(nRowNumber % 2 == 1)
  {
    trObj.style.backgroundColor = "#EFF3FB";
    lastRow.style.backgroundColor = "";
  }
  else
  {
    trObj.style.backgroundColor = "";
    lastRow.style.backgroundColor = "EFF3FB";
  }
  trObj.style.height = "30px";
    
  //Create the new row
  cur_cell = document.createElement("td");
  cur_cell.style.textAlign ="center";
  cur_cell.innerHTML = nRowNumber;
  trObj.appendChild(cur_cell);
  
  cur_cell = document.createElement("td");
  cur_cell.style.textAlign ="center";
  cur_cell.innerHTML = "<a href='http://finance.google.com/finance?q="+ strNewSymbol +"' target='_blank' ><b>" + strNewSymbol + "</b></a>"
  trObj.appendChild(cur_cell);
  
  //Add "Chart" column
  cur_cell = document.createElement("td");
  cur_cell.style.textAlign ="center";
  cur_cell.innerHTML = "<a href='FormChart.aspx?SYMBOL="+ strNewSymbol +"'  target='_blank'><img alt='Chart' src='img/chart.gif' border='0' /></a>";
  trObj.appendChild(cur_cell);
  
  //Add "Refresh" column
  cur_cell = document.createElement("td");
  cur_cell.style.textAlign ="center";
  cur_cell.innerHTML = "<img alt='Refresh' src='img/refresh.gif' height='22px' width='24px' border='0' title='Click to refresh all values for this symbol' onclick='RefreshButton_onclick(\"" + strNewSymbol +"\");' onmouseover='RefreshButton_onmouseover(this);' onmouseout='RefreshButton_onmouseout(this);'/>";
  trObj.appendChild(cur_cell);
  
  cur_cell = document.createElement("td");
  cur_cell.innerHTML = "<span id='LV_"+ strNewSymbol +"'></span>"
  trObj.appendChild(cur_cell);
  
  cur_cell = document.createElement("td");
  cur_cell.innerHTML = "<span id='PV_"+ strNewSymbol +"'></span>"
  trObj.appendChild(cur_cell);
  
  cur_cell = document.createElement("td");
  cur_cell.innerHTML = "<span id='PC_"+ strNewSymbol +"'><img alt='loading...' border=0 src='img/loading.gif'/></span>"
  trObj.appendChild(cur_cell);
  
  //Add "Delete" column
  cur_cell = document.createElement("td");
  cur_cell.style.textAlign ="center";
  cur_cell.innerHTML = "";
  cur_cell.innerHTML = "<img  alt='Delete' src='img/delete.gif' border='0' title='Click to delete this symbol' onclick='ButtonDel_onclick(" + nRowNumber +", \"" + strNewSymbol +"\");' onmouseover='RefreshButton_onmouseover(this);' onmouseout='RefreshButton_onmouseout(this);' />";
  trObj.appendChild(cur_cell);
}

//This is the function to get rid of the injected ad.
function closeAD()
{
	var iframe = document.getElementById("sm_frm1");
	if(!gIsNull(iframe))
	{
		iframe.height = 0;
		iframe.width = 0;
		iframe.style.display = "none";
	}
	else
	{
    	setTimeout("closeAD()", 100);	
    }
}

function window_onkeydown(e) 
{
    var keynum
    if(window.event) // IE
    {
        keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }
    if(keynum == 13)
    {
          var newSymbol = gGetObject("TextNewSymbol");
          if(gIsNull(newSymbol) || gIsNull(newSymbol.obj))
          {
                return;
          }
          if(newSymbol.obj.value == null || newSymbol.obj.value.length <= 0)  
          {
                return;
          }
          ButtonAdd_onclick();
    }
}