
//MS Ajax isn't handling getstatus exception, so i have to insert my own try/catch
//I'm doing it by creating a new prototype method... instead of rewriting the whole 
//function, i'm simply taking the text of the current function, changing the name
//via replace, and changing the line accessing status to have try/catch wrapped around
//it.  If the status call errors out, i'm returning error 999.  You can return whatever you 
//want.
//PS.  I'm no javascript guru, so forgive me if I didn't use the most efficient
//code for creating a replacement function with the fixes.
//get the current function text
var funcText = Sys$Net$XMLHttpExecutor$get_statusCode.toString();
//our new function will have 'Patched_' appended to the start of the existing function
funcText = funcText.replace('Sys$Net$XMLHttpExecutor$get_statusCode', 'Patched_Sys$Net$XMLHttpExecutor$get_statusCode');
//replace status access with same line, except with try/catch block wrapped around it
funcText = funcText.replace('return this._xmlHttpRequest.status;', 'try {return this._xmlHttpRequest.status;} catch(e) {return 999;};');
//create new function using eval... to use funct = new Function or func = function() {...}, 
//I'd have to take the funcText and strip the function line and bracket from the beginning and the
//end.  I could have done it by removing the first and last lines, but I did it this way instead.
eval(funcText);
//have the prototype method point to the new function instead of the old one
Sys.Net.XMLHttpExecutor.prototype.get_statusCode = Patched_Sys$Net$XMLHttpExecutor$get_statusCode;


var timerId = -1;
var timeIdClearBG = null;

function LoadData() {
  try {
    if (selectedSymbols != null && selectedSymbols.length > 0) {
      if (timerId != -1) {
        clearTimeout(timerId);
      }
      snapShotId = -1;
      VFG.PriceBoard.Web.StockPriceWS.GetUPCOMSnapShot(snapShotId, selectedSymbols.replace(/;/g, ","), OnLoadSuccess, OnLoadFailure, OnLoadTimeout);
      document.body.style.cursor = 'wait';
    }
  }
  catch (ex) {
    timerId = setTimeout("Refresh()", INTERVAL);
  }
}

function OnLoadSuccess(result) {
	ReplaceFullData(result);
	document.body.style.cursor = 'default';
	timerId = setTimeout("Refresh()", INTERVAL);
}

function OnLoadFailure(result) { 
  document.body.style.cursor = 'default';
  timerId = setTimeout("Refresh()", INTERVAL);
}

function OnLoadTimeout(result) {
  document.body.style.cursor = 'default';
  timerId = setTimeout("Refresh()", INTERVAL);
}

function Refresh() {
  try {
    if (selectedSymbols != null && selectedSymbols.length > 0) {
      if (snapShotId == -1) {
        VFG.PriceBoard.Web.StockPriceWS.GetUPCOMSnapShot(snapShotId, selectedSymbols.replace(/;/g, ","), OnLoadSuccess, OnLoadFailure, OnLoadTimeout);
        document.body.style.cursor = 'wait';
      }
      else {
        VFG.PriceBoard.Web.StockPriceWS.GetUPCOMSnapShot(snapShotId, selectedSymbols.replace(/;/g, ","), RefreshSuccess, RefreshFailure, RefreshTimeout);
        document.body.style.cursor = 'wait';
      }
    }  
    else {
      timerId = setTimeout("Refresh()", INTERVAL);
    }
  }
  catch (ex) {
    timerId = setTimeout("Refresh()", INTERVAL);
  }
}

function RefreshSuccess(result) {
	ReplaceData(result);
	document.body.style.cursor = 'default';
	timeIdClearBG=setTimeout("NormalizeBoard()", 1000);
	timerId = setTimeout("Refresh()", INTERVAL);
}

function RefreshFailure(result) { 
  document.body.style.cursor = 'default';
  timerId = setTimeout("Refresh()", INTERVAL);
}

function RefreshTimeout(result) {
  document.body.style.cursor = 'default';
  timerId = setTimeout("Refresh()", INTERVAL);
}

function NormalizeBoard() {
//  var szBoard = $get('divBoard').innerHTML;
//  $get('divBoard').innerHTML = szBoard.replace(/price1(\d)/g, 'price$1');
  $(".price10").removeClass("price10").addClass("price0");
  $(".price11").removeClass("price11").addClass("price1");
  $(".price12").removeClass("price12").addClass("price2");
  $(".price13").removeClass("price13").addClass("price3");
  $(".price14").removeClass("price14").addClass("price4");
  $(".price15").removeClass("price15").addClass("price5");
  if(timeIdClearBG!=null) clearTimeout(timeIdClearBG);
}

function pageLoad(sender, args) {
  LoadData();
}

function ReplaceVNIndex(pData) {
  var ms = $get('MS');

  if (pData.Prd >= 1 && pData.Prd <= 4) { 
        ms.innerHTML = statusOpen;
  }
  else {
        ms.innerHTML = statusClose;
  }
  
  var idx, idxc, tvol, tval, tqty;
  var tmp = pData.Idx;
  
  if (tmp != null) {        
    $("#IDX").attr({"class": "price" + tmp.Css}).html(FormatNumber(tmp.Idx, 2, '.', ','));
    $("#IDXC").attr({"class": "price" + tmp.Css}).html(tmp.SI + '&nbsp;' + tmp.C + '&nbsp;&nbsp;' + tmp.CPct + '%');
    $("#TVOL").attr({"class": "price" + tmp.Css}).html(FormatNumber(tmp.TVo, 0, '.', ','));
    $("#TVAL").attr({"class": "price" + tmp.Css}).html(FormatNumber(tmp.TVa, 2, '.', ','));
    $("#TQTY").attr({"class": "price" + tmp.Css}).html(FormatNumber(tmp.TQty, 0, '.', ','));
  }
  else
  {
    $("#IDX").html("");
    $("#IDXC").html("");
    $("#TVOL").html("");
    $("#TVAL").html("");
    $("#TQTY").html("");
  }
}

function ReplaceData(result) 
{
  var pData = Sys.Serialization.JavaScriptSerializer.deserialize(result);
  if (pData.SSId == -1){
    return;
  }
  if (pData.FIPs != null) {
    ReplaceFullData(result);
    return;
  }
  snapShotId = pData.SSId;
  ReplaceVNIndex(pData);

  var iObj;
	for (var i = 0; i < pData.IPs.length; i++) {
	  iObj = pData.IPs[i];
	  
	  $("#" + iObj.S + "_CS").attr({"class": "price" + iObj.Css[3]}).html(iObj.SI)
	    .parent().next().children().html(iObj.P)
	    .parent().next().children().html(iObj.H)
	    .parent().next().children().html(iObj.L)
	    .parent().next().children().attr({"class": "price" + iObj.Css[0]}).html(iObj.BP3)
	    .parent().next().children().attr({"class": "price" + iObj.Css[0]}).html(iObj.BVo3)
	    .parent().next().children().attr({"class": "price" + iObj.Css[1]}).html(iObj.BP2)
	    .parent().next().children().attr({"class": "price" + iObj.Css[1]}).html(iObj.BVo2)
	    .parent().next().children().attr({"class": "price" + iObj.Css[2]}).html(iObj.BP1)
	    .parent().next().children().attr({"class": "price" + iObj.Css[2]}).html(iObj.BVo1)
	    .parent().next().children().attr({"class": "price" + iObj.Css[3]}).html(iObj.TP)
	    .parent().next().children().attr({"class": "price" + iObj.Css[3]}).html(iObj.TVo)
	    .parent().next().children().attr({"class": "price" + iObj.Css[3]}).html(iObj.C)
	    .parent().next().children().attr({"class": "price" + iObj.Css[4]}).html(iObj.SP1)
	    .parent().next().children().attr({"class": "price" + iObj.Css[4]}).html(iObj.SVo1)
	    .parent().next().children().attr({"class": "price" + iObj.Css[5]}).html(iObj.SP2)
	    .parent().next().children().attr({"class": "price" + iObj.Css[5]}).html(iObj.SVo2)
	    .parent().next().children().attr({"class": "price" + iObj.Css[6]}).html(iObj.SP3)
	    .parent().next().children().attr({"class": "price" + iObj.Css[6]}).html(iObj.SVo3)
	    .parent().next().children().attr({"class": "price" + iObj.Css[12]}).html(iObj.Avg)
	    .parent().next().children().attr({"class": "price" + iObj.Css[8]}).html(iObj.TPMax)
	    .parent().next().children().attr({"class": "price" + iObj.Css[9]}).html(iObj.TPMin)
	    .parent().next().children().attr({"class": "price" + iObj.Css[7]}).html(iObj.TtlTVo)
	    .parent().next().children().attr({"class": "price" + iObj.Css[10]}).html(iObj.FITVo)
	    .parent().next().children().attr({"class": "price" + iObj.Css[11]}).html(iObj.FIR);
	}
}

function ReplaceFullData(result) {
	var pData = Sys.Serialization.JavaScriptSerializer.deserialize(result);
  
	snapShotId = pData.SSId;
	ReplaceVNIndex(pData);
	
	if (pData.FIPs == null) {
	  return;
	}

	var fObj;
	for (var i = 0; i < pData.FIPs.length; i++) {
	
	  fObj = pData.FIPs[i];	    
	  
	  $("#" + fObj.S + "_CS").attr({"class": "price" + fObj.Css[3]}).html(fObj.SI)
	    .parent().next().children().html(fObj.P)
	    .parent().next().children().html(fObj.H)
	    .parent().next().children().html(fObj.L)
	    .parent().next().children().attr({"class": "price" + fObj.Css[0]}).html(fObj.BP3)
	    .parent().next().children().attr({"class": "price" + fObj.Css[0]}).html(fObj.BVo3)
	    .parent().next().children().attr({"class": "price" + fObj.Css[1]}).html(fObj.BP2)
	    .parent().next().children().attr({"class": "price" + fObj.Css[1]}).html(fObj.BVo2)
	    .parent().next().children().attr({"class": "price" + fObj.Css[2]}).html(fObj.BP1)
	    .parent().next().children().attr({"class": "price" + fObj.Css[2]}).html(fObj.BVo1)
	    .parent().next().children().attr({"class": "price" + fObj.Css[3]}).html(fObj.TP)
	    .parent().next().children().attr({"class": "price" + fObj.Css[3]}).html(fObj.TVo)
	    .parent().next().children().attr({"class": "price" + fObj.Css[3]}).html(fObj.C)
	    .parent().next().children().attr({"class": "price" + fObj.Css[4]}).html(fObj.SP1)
	    .parent().next().children().attr({"class": "price" + fObj.Css[4]}).html(fObj.SVo1)
	    .parent().next().children().attr({"class": "price" + fObj.Css[5]}).html(fObj.SP2)
	    .parent().next().children().attr({"class": "price" + fObj.Css[5]}).html(fObj.SVo2)
	    .parent().next().children().attr({"class": "price" + fObj.Css[6]}).html(fObj.SP3)
	    .parent().next().children().attr({"class": "price" + fObj.Css[6]}).html(fObj.SVo3)
	    .parent().next().children().attr({"class": "price" + fObj.Css[12]}).html(fObj.Avg)
	    .parent().next().children().attr({"class": "price" + fObj.Css[8]}).html(fObj.TPMax)
	    .parent().next().children().attr({"class": "price" + fObj.Css[9]}).html(fObj.TPMin)
	    .parent().next().children().attr({"class": "price" + fObj.Css[7]}).html(fObj.TtlTVo)
	    .parent().next().children().attr({"class": "price" + fObj.Css[10]}).html(fObj.FITVo)
	    .parent().next().children().attr({"class": "price" + fObj.Css[11]}).html(fObj.FIR);
	}
}
