﻿//Class - CCrossPricePanel
function CCrossPricePanel(sUserControlID /*String*/) {

    /* Private variables */
    var tdBid; //TD Array
    var tdOffer; //TD Array
    var labBid; //Span Array
    var labOffer //Span Array
    var _this = this; //this instance

    /* Constructor() */
    {
        //Initialize private variables
        tdBid = new Array();
        tdOffer = new Array();
        labBid = new Array();
        labOffer = new Array();

        for (var i = 1; i <= 8; i++) {
            tdBid[i - 1] = document.getElementById(sUserControlID + 'tdC' + i + 'Bid'); //For example, 1.23
            tdOffer[i - 1] = document.getElementById(sUserControlID + 'tdC' + i + 'Offer'); //For example, 1.24

            labBid[i - 1] = document.getElementById(sUserControlID + 'labC' + i + 'Bid'); //For example, 98
            labOffer[i - 1] = document.getElementById(sUserControlID + 'labC' + i + 'Offer'); //For example, 99
        }

    }
    //end of contructor

    this.Start = function() {
        setTimeout(_this.RefreshCrossPrice_Core, 5000);
    }
    
    /* public function RefreshCrossPrice_Core() */ //- Refresh cross price by AJAX
    this.RefreshCrossPrice_Core = function () {

        //1. Call AJAX for get US Price
        var xmlHttp = GetXmlHttpObject();

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                if (xmlHttp.status == 200) {
                    var sReturnContent = xmlHttp.responseText;

                    _this.UpdatePrice(sReturnContent);
                }

                xmlHttp = null;
                setTimeout(_this.RefreshCrossPrice_Core, 5000);

            }
        }
        xmlHttp.open("GET", "ajax/ajaxUSPrice.aspx?type=2", true);
        xmlHttp.setRequestHeader("If-Modified-Since", "0");
        xmlHttp.send(null);
    }

    /* public UpdatePrice(String) : void */
    this.UpdatePrice = function(sPriceString) {

        var sPriceArr = sPriceString.split("#");
        for (var i = 0; i <= 7; i++)
            FullOneUSPrice(sPriceArr[i], i);
    }

    /* private FullOneUSPrice(String, int) */
    function FullOneUSPrice(sPriceStr, iArrIndex) {

        //sPriceStr = "1.4079 | 1.4081 | 1"
        var sArr = sPriceStr.split("|"); // {"1.4079", "1.4081", "1"}

        if (sArr[2] == "1") { //Up
            tdBid[iArrIndex].className = "up";
            tdOffer[iArrIndex].className = "up";
            
        } else { //Down
            tdBid[iArrIndex].className = "down";
            tdOffer[iArrIndex].className = "down";
            
        }

        //Update GUI by array
        labBid[iArrIndex].innerHTML = sArr[0];
        labOffer[iArrIndex].innerHTML = sArr[1];
    }
}

