﻿var previousTrades; 
var newTrades;

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var d = currentTime.getDay();        
var Updates = "";

function UpdateRates() 
{
   var numOfTradesToDisplay = 10;
   var tradesMonitorRefresh = 60000;
   var TradesMonitorRefreshWeekend = 600000;
   
   TradeWS.getTrades(numOfTradesToDisplay, OnSucceeded, OnFailed); 

   updateTime();       

   // Satruday and Sunday
   if (d == 0 || d == 6)
   {
        setTimeout('UpdateRates();', TradesMonitorRefreshWeekend);
   }
   else
   {
        setTimeout('UpdateRates();', tradesMonitorRefresh);
   }
}         

 function OnSucceeded(result, userContext, methodName) 
 {               
        
   if (result != null) // If resula is not null
   {
       $get("errorMessageLabel").style.display = 'none';
   
       var length = result.length;
       //alert(length);
       
       if (length > 0)
       {              
           newTrades = result;           
           
           for(var i = 0; i < length; i++) 
           {
                if (result[i]["Buy"] != "0" || result[i]["Sell"] != "0")
                {
                   $get('lblNumber' + (i+1)).innerHTML = i+1; 
                   $get('lblPair' + (i+1)).innerHTML = result[i]["Pair"]; 
                   
                   if (result[i]["Buy"] == "0")
                   {
                        $get("tdBuyWidth" + (i+1)).className = "red";
                        $get("tdSellWidth" + (i+1)).className = "red";

                        $get('lblBuy' + (i+1)).innerHTML = ""; 
                        $get('lblSell' + (i+1)).innerHTML = result[i]["Sell"] + "%";  
                         
                        $get('imgLeftEnd' + (i+1)).src = 'images/right_end2.gif';
                        $get('imgRightEnd' + (i+1)).src = 'images/right_end.gif';
                        
                   }
                   else if (result[i]["Sell"] == "0")
                   {
                        $get("tdBuyWidth" + (i+1)).className = "green";
                        $get("tdSellWidth" + (i+1)).className = "green";

                        $get('lblBuy' + (i+1)).innerHTML = result[i]["Buy"] + "%"; 
                        $get('lblSell' + (i+1)).innerHTML = "";   
                        
                        $get('imgLeftEnd' + (i+1)).src = 'images/left_end.gif';
                        $get('imgRightEnd' + (i+1)).src = 'images/left_end2.gif';
                   }
                   else
                   {
                       $get("tdBuyWidth" + (i+1)).className = "green";
                       $get("tdSellWidth" + (i+1)).className = "red";

                       $get('lblBuy' + (i+1)).innerHTML = result[i]["Buy"] + "%"; 
                       $get('lblSell' + (i+1)).innerHTML = result[i]["Sell"] + "%";                    

                       $get('imgLeftEnd' + (i+1)).src = 'images/left_end.gif';
                       $get('imgRightEnd' + (i+1)).src = 'images/right_end.gif';
                   }
                   
                   $get("tdBuyWidth" + (i+1)).style.width = result[i]["Buy"] + "%";
                   $get("tdSellWidth" + (i+1)).style.width = result[i]["Sell"] + "%";

                   $get("Pair" + (i+1)).style.display = 'block';
                   $get("AllPairs").style.display = 'block';                           
                }
                else
                {
                    $get("Pair" + (i+1)).style.display = 'none';
                }
           }
           
            if (previousTrades != null)
            {
                // Update the status line
                UpdateStatus(previousTrades, newTrades);
            }                   

            previousTrades = newTrades;          
       }        
   }
 }             
    
 function UpdateStatus(pTrades, nTrades)
 {                               
    updateTime();

    var DateTime = day + "/" + month + " " + hours + ":" + minutes
 
    fcontent = new Array();
    var fcontentCounter = 0;
    
    if (pTrades != null && nTrades != null
        && pTrades.length > 0 && nTrades.length > 0)
    {
        // a new currency pair is ranked first.
        if (nTrades[0]["Pair"] != pTrades[0]["Pair"])
        {
            if (nTrades[0]["Buy"] != 0 || nTrades[0]["Sell"] != 0)
            {
                Updates += DateTime + " - Number 1 currency traded right now is the " + nTrades[0]["Pair"] + " which surpassed the " + pTrades[0]["Pair"] + ".<br>";
                fcontent[fcontentCounter++] = DateTime + " - Number 1 currency traded right now is the " + nTrades[0]["Pair"] + " which surpassed the " + pTrades[0]["Pair"] + ".<br>";
            }
        }

        // a new currency pair is ranked second.
        if (nTrades[1]["Pair"] != pTrades[1]["Pair"])
        {
            if (nTrades[1]["Buy"] != 0 || nTrades[1]["Sell"] != 0)
            {
                Updates += DateTime + " - Number 2 currency traded right now is the " + nTrades[1]["Pair"] + " which surpassed the " + pTrades[1]["Pair"] + ".<br>";
                fcontent[fcontentCounter++] = DateTime + " - Number 2 currency traded right now is the " + nTrades[1]["Pair"] + " which surpassed the " + pTrades[1]["Pair"] + ".<br>";
            }
        }

        // a new currency pair is ranked third.
        if (nTrades[2]["Pair"] != pTrades[2]["Pair"])
        {
            if (nTrades[2]["Buy"] != 0 || nTrades[2]["Sell"] != 0)
            {
                Updates += DateTime + " - Number 3 currency traded right now is the " + nTrades[2]["Pair"] + " which surpassed the " + pTrades[2]["Pair"] + ".<br>";
                fcontent[fcontentCounter++] = DateTime + " - Number 3 currency traded right now is the " + nTrades[2]["Pair"] + " which surpassed the " + pTrades[2]["Pair"] + ".<br>";
            }
        }


        // the trend changes for a currency pair from buy to sell or vice versa.
        for (var nt in nTrades)
        {
            for (var pt in pTrades)
            {
                if (nTrades[nt]["Buy"] != 0 || nTrades[nt]["Sell"] != 0)
                {
                    if (!isCommodity(nTrades[nt]["Pair"]))
                    {

                        if (nTrades[nt]["Pair"] == pTrades[pt]["Pair"] && nTrades[nt]["Buy"] > 50 && pTrades[pt]["Buy"] < 50)
                        {
                            Updates += DateTime + " - " + nTrades[nt]["Pair"] + " just changed its direction from sell to buy, which means our top traders currently believe the " + nTrades[nt]["Pair"].substr(0, nTrades[nt]["Pair"].indexOf("/")) + " is going to rise.<br>";
                            fcontent[fcontentCounter++] = DateTime + " - " + nTrades[nt]["Pair"] + " just changed its direction from sell to buy, which means our top traders currently believe the " + nTrades[nt]["Pair"].substr(0, nTrades[nt]["Pair"].indexOf("/")) + " is going to rise.<br>";
                        }
                        else if (nTrades[nt]["Pair"] == pTrades[pt]["Pair"] && nTrades[nt]["Buy"] < 50 && pTrades[pt]["Buy"] > 50)
                        {
                            Updates += DateTime + " - " + nTrades[nt]["Pair"] + " just changed its direction from buy to sell, which means our top traders currently believe the " + nTrades[nt]["Pair"].substr(nTrades[nt]["Pair"].indexOf("/")+1 ,3) + " is going to rise.<br>";
                            fcontent[fcontentCounter++] = DateTime + " - " + nTrades[nt]["Pair"] + " just changed its direction from buy to sell, which means our top traders currently believe the " + nTrades[nt]["Pair"].substr(nTrades[nt]["Pair"].indexOf("/")+1 ,3) + " is going to rise.<br>";
                        }
                    }
                }
            }
        }

        // buy/sell percentage ratio changes to above 90% on any direction.
        for (var nt in nTrades)
        {
            for (var pt in pTrades)
            {
                if (nTrades[nt]["Buy"] != 0 || nTrades[nt]["Sell"] != 0)
                {
                    if (!isCommodity(nTrades[nt]["Pair"]))
                    {
                        if (nTrades[nt]["Pair"] == pTrades[pt]["Pair"] && nTrades[nt]["Buy"] >= 90)
                        {
                            Updates += DateTime + " - " + nTrades[nt]["Buy"] + "% of all " + nTrades[nt]["Pair"] + " trades are buy side which means there is a strong trend towards the " + nTrades[nt]["Pair"].substr(0, nTrades[nt]["Pair"].indexOf("/")) + " .<br>";
                            fcontent[fcontentCounter++] = DateTime + " - " + nTrades[nt]["Buy"] + "% of all " + nTrades[nt]["Pair"] + " trades are buy side which means there is a strong trend towards the " + nTrades[nt]["Pair"].substr(0, nTrades[nt]["Pair"].indexOf("/")) + " .<br>";
                        }
                        else if (nTrades[nt]["Pair"] == pTrades[pt]["Pair"] && nTrades[nt]["Buy"] < 10)
                        {
                            Updates += DateTime + " - " + nTrades[nt]["Sell"] + "% of all " + nTrades[nt]["Pair"] + " trades are sell side which means there is a strong trend towards the " + nTrades[nt]["Pair"].substr(nTrades[nt]["Pair"].indexOf("/")+1, 3) + " .<br>";
                            fcontent[fcontentCounter++] = DateTime + " - " + nTrades[nt]["Sell"] + "% of all " + nTrades[nt]["Pair"] + " trades are sell side which means there is a strong trend towards the " + nTrades[nt]["Pair"].substr(nTrades[nt]["Pair"].indexOf("/")+1, 3) + " .<br>";
                        }
                    }
                }
            }
        }


        // a currency jumps in rank at least 4 places.
        var i = 1;
        var j = 1;

        for (var nt in nTrades)
        {
            for (var pt in pTrades)
            {
                if (nTrades[nt]["Buy"] != 0 || nTrades[nt]["Sell"] != 0)
                {
                    if (nTrades[nt]["Pair"] == pTrades[pt]["Pair"] && (j - i) > 2)  //3
                    {
                        Updates += DateTime + " - " + nTrades[nt]["Pair"] + "  jumps ahead as favorable and is now ranked " + i + " among our traders.<br>";
                        fcontent[fcontentCounter++] = DateTime + " - " + nTrades[nt]["Pair"] + "  jumps ahead as favorable and is now ranked " + i + " among our traders.<br>";
                    }
                }

                j++;
            }

            i++;
            j = 1;
        }


        // a relatively rarely traded pair has entered the list (for instance least traded five currencies).
        var k = 1;

       for (var nt in nTrades)
        {

            if (nTrades[nt]["Buy"] != 0 || nTrades[nt]["Sell"] != 0)
            {
                if (rarePair(nTrades[nt]["Pair"]))
                {
                    Updates += DateTime + " - The " + nTrades[nt]["Pair"] + " is proving popular and is now ranked " + k + ". <br>";
                    fcontent[fcontentCounter++] = DateTime + " - The " + nTrades[nt]["Pair"] + " is proving popular and is now ranked " + k + ". <br>";
                }
            }

            k++;
        }

    }                
    
    if(Updates == "")
    {
        //$get('lblUpdates').innerHTML = "No updates";
    }
    else
    {
        //$get('lblUpdates').innerHTML = Updates;
    }
    
    updateTime();       

    // Satruday and Sunday
    if (d == 0 || d == 6)
    {
       fcontent = new Array();
       fcontent[0] = "";
    }
    else if (fcontent.length <= 0)
    {
        fcontent[0] = "";
    }
 }

 function rarePair(pair)
 {
       var pairsArray = ["EUR/CHF", "EUR/AUD", "EUR/CAD", "AUD/JPY", "CAD/JPY", "CHF/JPY"];

       for (var p in pairsArray)
       {
            if (pairsArray[p] == pair)
                return true;
       }
         
       return false;
 }

 function OnFailed(error, userContext, methodName) 
 { 
   $get("errorMessageLabel").style.display = 'block';
   $get('errorMessageLabel').innerHTML = "Top Trades' Insight is not available at the moment...";
 }             

function updateTime() 
{        
    currentTime = new Date()
    month = currentTime.getMonth() + 1
    day = currentTime.getDate()
    year = currentTime.getFullYear()
    hours = currentTime.getHours()
    minutes = currentTime.getMinutes()
    d = currentTime.getDay();        

    if (minutes <= 9) {
          minutes = "0" + minutes;
    }
    if (day <= 9) {
          day = "0" + day;
    }
    if (month <= 9) {
          month = "0" + month;
    }
    if (hours <= 9) {
          hours = "0" + hours;
    }
}     

function readCookie(name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}             

function isCommodity(comm)
{
	var commodities = new RegExp("GOLD|SILVER|OIL");
	return commodities.test(comm);
}
