// $Id: chartEntry.js,v 1.3 2009/12/14 14:13:29 hbr Exp $

// Global variables
var favoriteChartURL = "/services/mymarketpulse/watchlist_add.json";
var chartGetDataURL = "/services/mymarketpulse/chart_get_data.json";

// Add chart from the portfolio
function initAutocompleteChart(selectionData) {
    var targetURL = chartGetDataURL + "?selectionData=" + selectionData;
    
    // Request the data needed to make an "Add Watch" pop-up
    dojo.xhrGet( {
        url: targetURL,
        handleAs: "json-comment-filtered",
        timeout: 20000,
        
        // If HTTP 200 (successful or "Problem")
        load: function(response, ioArgs) {
        
          // HTTP OK - but problem with data
          if (response.Message.substring(0,5) == "Probl") {
            reportProblem("chartAdd", response.Message);
          }
          else {
            initChartAdd(response.securityId, 
                         response.valorSymbol, 
                         response.portalSegment, 
                         response.shortName, 
                         response.nMax);
          }
          return response;
        },
        // HTTP not OK - report error message and wait for user
        error: function(response, ioArgs) {
          reportProblem("chartAdd", response.message);
          return response;
        }
    });
}

// Add a watch entry from Product Explorer
function initChartAdd(securityId, valorSymbol, portalSegment, shortName, nMax) {
        
    var nSet = getFavoriteChartsFromDom().length;

    var nRemain = nMax - nSet;
    
    dojo.byId("chartNumberSet").innerHTML = nSet;
    dojo.byId("chartNumberTotal").innerHTML = nMax;
    
    if (nSet == 1) {
            dojo.style("chartStatusSingular", "display", "");
            dojo.style("chartStatusPlural", "display", "none");
    } else {
            dojo.style("chartStatusSingular", "display", "none");
            dojo.style("chartStatusPlural", "display", "");            
    }    
    
    if (nRemain > 0) {
        // Show the right table
        dojo.style("chartAddTable", "display", "");
        dojo.style("chartNoAddTable", "display", "none");

        dijit.byId("chartTitle").setValue(valorSymbol);
        dojo.byId("chartSecurityId").value = securityId;
        dojo.byId("chartValorSymbol").value = valorSymbol;
        dojo.byId("chartPortalSegment").value = portalSegment;
    }
    else {
        // Show the right table
        dojo.style("chartAddTable", "display", "none");
        dojo.style("chartNoAddTable", "display", "");
    }
    dijit.byId("chartAddDialog").show();
}

function chartAdd(securityId, domain, valorSymbol, portalSegment, chartTitle) {

    var position = getFavoriteChartsFromDom().length + 1;
    
    if (portalSegment != null) {
       portalSegment.replace(/^\s+/, "").replace(/^\s+/, ""); // trim
       if (portalSegment == "null") {
         portalSegment = "";
       }
    }
    
    var queryString = "add="
                    + securityId+"|"
                    + domain+"|"
                    + position+"|"
                    + valorSymbol+"|"
                    + chartTitle+"|"
                    + portalSegment;
                    
    // now tell the server about this new chart
    var _url = "/ajax/favorite-charts?" + queryString;
    
    dojo.xhrGet( {
              url          : _url,
              handleAs     : "json-comment-filtered",
              timeout      : 20000,      // in miliseconds
              preventCache : true,
      
              load: function(response, ioArgs) {
                // if everything is ok:
                // 1. Hide the dialog
                fadeAway("chartAddDialog");
                // 2. Stop the old movies
                stopFavoriteChartMovies();
                // 3. Refresh the charts pane
                dijit.byId("favorite_charts_pane").refresh()
              },      // end load
      
              error: function(response, ioArgs) { // On error, just log
                if (ioArgs && ioArgs.xhr) {
                  console.log("AJAX Error, HTTP status code: " + ioArgs.xhr.status +"\n"+response);
                }
                fadeAway("chartAddDialog");
                return response;
              }       // end error
    } );        // end dojo.xhrGet
}
