﻿/*-------------------------------------------*\
            VARIABLES GLOBALES
\*-------------------------------------------*/
var watchlist_html        = '';
var watchlist_cotes_html  = '';
var watchlist_fonds_html  = '';

/*-------------------------------------------*\
    AJOUTER UNE COTE OU UN FOND À MA LISTE
\*-------------------------------------------*/
function add(code, auto_printWatchList)	{

	var WATCHLIST_NB_ITEM_MAX = 10;	// on ne veut pas plus de 10 éléments dans la watchlist
	
	if (typeof(auto_printWatchList) == "undefined")	{
		auto_printWatchList = true;
	}

	var watchlist_cookie = readCookie('watchlist');
	if (watchlist_cookie)	{
		watchlist = watchlist_cookie.split(',');

		exists = false;
		for (i = 0; i < watchlist.length; ++ i)	{
			if	(watchlist[i] == code)	{
				exists = true;
			}
		}

		if (!exists)	{
			// on ajoute au début de la liste, à l'indice 0 (zéro)
			watchlist.unshift(code);
			
			// si on dépasse le nombre maximum d'éléments prévue (WATCHLIST_NB_ITEM_MAX),
			// on tronque les plus anciens
			if (watchlist.length > WATCHLIST_NB_ITEM_MAX)	{
				watchlist = watchlist.slice(0, WATCHLIST_NB_ITEM_MAX - 1);
			}

			// 10 ans (3650 jours)
//			alert(watchlist);
			createCookie('watchlist', watchlist.join(','), 3650);
			if (auto_printWatchList)	{
				printWatchList();
			}
		}
	}
	else	{
		// 10 ans (3650 jours)
		createCookie('watchlist', code, 3650);
		if (auto_printWatchList)	{
			printWatchList();
		}
	}
}

/*-------------------------------------------*\
          AJOUTER UNE COTE À MA LISTE
\*-------------------------------------------*/
function addCote(marketsymbol, auto_printWatchList)	{

	add('c:'+marketsymbol, auto_printWatchList);
}

/*-------------------------------------------*\
          AJOUTER UN FOND À MA LISTE
\*-------------------------------------------*/
function addFond(codefond, auto_printWatchList)	{

	add('f:'+codefond, auto_printWatchList);
}

/*-------------------------------------------*\
       VIDER LE BLOC MA LISTE D'ACTIONS
\*-------------------------------------------*/
function removeWatchList()	{
	
	eraseCookie('watchlist');

	// 10 ans (3650 jours)
	createCookie('watchlist_dejavidee', 'true', 3650);

	printWatchList();
}

/*-------------------------------------------*\
            BLOC MA LISTE D'ACTIONS
\*-------------------------------------------*/
function printWatchList()	{

	// ---------------------------------
	// lire les cookies pour savoir quelles cotes/fonds on affiche
	// ---------------------------------
	var watchlist_cookie       = readCookie('watchlist');
	var watchlist_cotes_cookie = new Array();
	var watchlist_fonds_cookie = new Array();
	
	watchlist_html = '';

	// ---------------------------------
	// si la liste est vide, on vérifie si on l'a déjà vu
	// ---------------------------------
	if (!watchlist_cookie)	{

		var watchlist_dejavidee_cookie = readCookie('watchlist_dejavidee');

		// si on a déjà vidé notre liste on affiche un message d'explication
		// sinon on ajoute une cote par défaut soit, 'ca;IQW' (Quebecor World Inc.)
		if (watchlist_dejavidee_cookie != 'true')	{
			addCote('ca%3BIQW', false);	// false, car sinon la watchlist va s'imprimer avant qu'on aie fini de la générer
			watchlist_cookie = readCookie('watchlist');
		}		
	}

	// ---------------------------------
	// si la liste n'est pas vide 
	// ---------------------------------
	if (watchlist_cookie)	{

		watchlist_html += '<table>';
		watchlist_html += '<tr valign="top">';
		watchlist_html += '<th class="hsymb">Symbol</th><th class="hcours">Last trade ($)</th><th>Variation</th><th>% Variation</th>';
		watchlist_html += '</tr>';

		// comme le cookie watchlist contient les cotes ET les fonds
		// on le split en 2 array différent pour pouvoir appeler les 2 web services
		var watchlist = watchlist_cookie.split(',');
		
		for (i = 0; i < watchlist.length; ++ i)	{

			var watchlist_item = watchlist[i].split(':');
			var type = watchlist_item[0];
			var code = watchlist_item[1];

			if	(type == 'c')	{
				watchlist_cotes_cookie.push(code);
			}
			else if (type == 'f')	{
				watchlist_fonds_cookie.push(code);
			}
		}
		
//		alert('cotes = ' + watchlist_cotes_cookie);
//		alert('fonds = ' + watchlist_fonds_cookie);
	}

	// ---------------------------------
	// si on a des cotes dans le cookie, on affiche la liste des cotes
	// ---------------------------------
	if (watchlist_cotes_cookie.length)	{

		// ---------------------------------
		// appel du web service pour voir le détail des cotes
		// ---------------------------------
		$.ajax({
			type: "GET",
			url: '/cgi-bin/ws/getCotesList.cgi?q=' + watchlist_cotes_cookie,
			dataType: "json",
			async: false,
			success: function(output)	{

				watchlist_cotes_html = '';
				
				if (!output || !output.Result)
				{
				  return 0;
				}
				
				for (var i in output.Result)	{

					watchlist_cotes_html += '<tr valign="top">';

					// Symbole
					var Symbol = output.Result[i].Symbol.split(';');
					var marche  = Symbol[0];
					var symbole = Symbol[1];
					watchlist_cotes_html += '<td class="tsymb"><a href="/quotes/index.html?marketsymbol='+encodeURIComponent(output.Result[i].Symbol)+'" title="'+output.Result[i].CompanyName + ' ' + output.Result[i].ExchangeName+'">';
					watchlist_cotes_html += symbole;
					watchlist_cotes_html += '</a></td>';

					// Cours ($)
					if (output.Result[i].Last == "")	{
						watchlist_cotes_html += '<td>';
						watchlist_cotes_html += 'N/D'
					}
					else	{
						var Last = new Number(output.Result[i].Last);
						watchlist_cotes_html += '<td class="tcours">';
						watchlist_cotes_html += Last.toFixed(3);
					}
					watchlist_cotes_html += '</td>';

					// Variation ($)
					if (output.Result[i].DollarChange == "")	{
						watchlist_cotes_html += '<td>';
						watchlist_cotes_html += 'N/D'
					}
					else	{
						var DollarChange = new Number(output.Result[i].DollarChange);
						watchlist_cotes_html += '<td class="tvar'+(DollarChange < 0 ? ' down' : '')+'">';
						watchlist_cotes_html += (DollarChange >= 0 ? '+' : '');
						watchlist_cotes_html += DollarChange.toFixed(3);
					}
					watchlist_cotes_html += '</td>';

					// Variation (%)
					if (output.Result[i].PercentChange == "")	{
						watchlist_cotes_html += '<td>';
						watchlist_cotes_html += 'N/D'
					}
					else	{
						var PercentChange = new Number(output.Result[i].PercentChange);
						watchlist_cotes_html += '<td class="tpvar'+(PercentChange < 0 ? ' down' : '')+'">';
						watchlist_cotes_html += (PercentChange >= 0 ? '+' : '');
						watchlist_cotes_html += PercentChange.toFixed(3) + '%';
					}
					watchlist_cotes_html += '</td>';
					watchlist_cotes_html += '</tr>';
				}

			}
		});
	}

	// ---------------------------------
	// si on a des fonds dans le cookie, on affiche la liste des fonds
	// ---------------------------------
	if (watchlist_fonds_cookie.length)	{
		
		watchlist_fonds_html = '';
		watchlist_fonds = watchlist_fonds_cookie;
		
//		alert ('ws = ' + watchlist_fonds);
//		alert (watchlist_fonds);
		
		for (var i in watchlist_fonds)	{

			// ---------------------------------
			// appel du web service pour avoir le détail du fond
			// ---------------------------------
//			alert(watchlist_fonds[i]);
			$.ajax({
				type: "GET",
				url: '/cgi-bin/ws/getFondsList.cgi?q=' + watchlist_fonds[i],
				dataType: "json",
				async: false,
				success: function(output)	{

					
					if (output  && output.data)
					{
					
						watchlist_fonds_html += '<tr valign="top">';

						// Symbole
						var symbole = watchlist_fonds[i];

						family_name='';
						fund_name='';

						if (output.data.FAMILY_NAME_FR) {
						 family_name=output.data.FAMILY_NAME_FR;
						} else if (!output.data.FAMILY_NAME_FR && output.data.FAMILY_NAME_EN )
						{
						 family_name=output.data.FAMILY_NAME_EN;
						}

						if (output.data.FUND_NAME_FRENCH) {
						 fund_name=output.data.FUND_NAME_FRENCH;
						} else if (!output.data.FUND_NAME_FRENCH && output.data.FUND_NAME_ENGLISH )
						{
						 fund_name=output.data.FUND_NAME_ENGLISH;
						}


						watchlist_fonds_html += '<td class="tsymb"><a href="/funds/details.html?codefonds='+encodeURIComponent(symbole)+'" title="' + family_name + ' - ' + fund_name + '">';
						watchlist_fonds_html += symbole;
						watchlist_fonds_html += '</a></td>';

						// Cours ($)
						if (output.data.NET_ASSET_VALUE_PER_SHARE == "")	{
							watchlist_fonds_html += '<td>';
							watchlist_fonds_html += 'N/D'
						}
						else	{
							var NET_ASSET_VALUE_PER_SHARE = new Number(output.data.NET_ASSET_VALUE_PER_SHARE);
							watchlist_fonds_html += '<td class="tcours">';
							watchlist_fonds_html += NET_ASSET_VALUE_PER_SHARE.toFixed(3);
						}
						watchlist_fonds_html += '</td>';

						// Variation ($)
						if (output.data.CHG_NAVPS_OR_CURRENT_YIELD == "")	{
							watchlist_fonds_html += '<td>';
							watchlist_fonds_html += 'N/D'
						}
						else	{
							var CHG_NAVPS_OR_CURRENT_YIELD = new Number(output.data.CHG_NAVPS_OR_CURRENT_YIELD);
							watchlist_fonds_html += '<td class="tvar'+(CHG_NAVPS_OR_CURRENT_YIELD < 0 ? ' down' : '')+'">';
							watchlist_fonds_html += (CHG_NAVPS_OR_CURRENT_YIELD >= 0 ? '+' : '');
							watchlist_fonds_html += CHG_NAVPS_OR_CURRENT_YIELD.toFixed(3);
						}
						watchlist_fonds_html += '</td>';

						// Variation (%)
						if (output.data.VALUATION_PC == "")	{
							watchlist_fonds_html += '<td>';
							watchlist_fonds_html += 'N/D'
						}
						else	{
							var VALUATION_PC = new Number(output.data.VALUATION_PC);
							watchlist_fonds_html += '<td class="tpvar'+(VALUATION_PC < 0 ? ' down' : '')+'">';
							watchlist_fonds_html += (VALUATION_PC >= 0 ? '+' : '');
							watchlist_fonds_html += VALUATION_PC.toFixed(3) + '%';
						}
						watchlist_fonds_html += '</td>';
						watchlist_fonds_html += '</tr>';

					}
				}
			});

		}
	}

	// ---------------------------------
	// si on a au moins une cote ou un fond
	// ---------------------------------
	if (watchlist_cookie)	{

		watchlist_html += watchlist_cotes_html;
		watchlist_html += watchlist_fonds_html;

		watchlist_html += '</table>';
	    watchlist_html += '<div id="btebasActions">';
		watchlist_html += '<a href="javascript:removeWatchList();" id="modsuppr">Delete my quotes list</a>';
		watchlist_html += '</div>'
	}
	else	{
	    watchlist_html += '<div id="msgWatchList">';
		watchlist_html += "There are currently no quotes to be displayed in your list of stock quotes. You may add quotes in your list by clicking on Add to my list of stocks on a stock market quote page.";
		watchlist_html += '</div>'
	}


	$("#coteajour").html(watchlist_html);
	
}

