var urlVF = "/proxy_VF.aspx";
var Decimales = 2;
var separador = "&#160;&#160; &#160;&#160;&#160; | &#160;&#160;&#160;&#160;";
var msg = "";


function CommaFormatted(amount)
{
	var delimiter = "."; //Remplazar según corresponda
	var a = amount.split(',',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + ',' + d; }
	amount = minus + amount;
	return amount;
}
// end of function CommaFormatted()



$(document).ready(function(){
	/*UF 2*/
	$.ajax({
	  type: "GET",
	  url: urlVF,
	  dataType: "xml",
	  data: {met:'uf_hoy'},
	  async: false,
	  success: function(xml){
		$('UF',xml).each(function(i,item){
			var nombre = $('titulo',item).text();
			var valor = $('valor',item).text().split(',');
			if (nombre.toUpperCase() in {'DOLAR':'','EURO':'', 'UF':'', 'IPSA':'', 'S&P500':'', 'REAL BRASILE\u00d1O':''})
			{
				msg += nombre.toUpperCase() + '=&nbsp;' +valor[0]+','+valor[1].substr(0,Decimales)+ '&nbsp; &nbsp;&nbsp;'+ separador;
			}
		});
	  },
	  error: function(i,x,c){
		//alert(x);
	  }
	});
	/*          MONEDAS           */
	$.ajax({
	  type: "GET",
	  url: urlVF,
	  dataType: "xml",
	  data: {met:'mon'},
	  async: false,
	  success: function(xml){
		$('Moneda',xml).each(function(i,item){
			var nombre = $(this).attr('nombre');
			var valor = $('valor',this).text().split(',');
			var variacion = $(this).find('variacion_porcentaje').text().split(',');
			if (nombre.toUpperCase() in {'DOLAR':'','EURO':'', 'UF':'', 'IPSA':'', 'S&P500':'', 'REAL BRASILE\u00d1O':''})
			{
				msg += nombre.toUpperCase() + '=&nbsp;' +valor[0]+','+valor[1].substr(0,Decimales)+ '&nbsp; &nbsp;&nbsp;('+variacion[0]+','+variacion[1].substr(0,Decimales) +' %)'+ separador;
			}
		});
	  },
	  error: function(i,x,c){
		//alert(x);
	  }
	});
	/*          INDICES           */
	$.ajax({
	  type: "GET",
	  url: urlVF,
	  dataType: "xml",
	  data: {met:'ind'},
	  async: false,
	  success: function(xml){
		$(xml).find('Indices').children().each(function(){
			var nombre = this.tagName;
			var valor = $(this).find('valor').text().split(',');
			var variacion = $(this).find('variacion').text().split(',');
			if (nombre.toUpperCase() in {'DOLAR':'','EURO':'', 'UF':'', 'IPSA':'', 'S&P500':'', 'REAL BRASILE\u00d1O':''})
			{
				msg += nombre.toUpperCase() + '=&nbsp;' +valor[0]+','+valor[1].substr(0,Decimales)+ '&nbsp; &nbsp;&nbsp;('+variacion[0]+','+variacion[1].substr(0,Decimales) +' %)'+ separador;
			}
		});
	  },
	  error: function(i,x,c){
		//alert(x);
	  }
	});
	/*          INDICES  2         */
	$.ajax({
	  type: "GET",
	  url: urlVF,
	  dataType: "xml",
	  data: {met:'ind_varios'},
	  async: false,
	  success: function(xml){
		$(xml).find('Indicadores').children().each(function(){
			var symb = this.tagName;
			var nombre = $(this).find('nombre').text().replace(' -',' ');
			var valor = CommaFormatted($(this).find('valor').text().replace('.',','));
			// var variacion = $(this).find('variacion').text().split(',');
			var variacion = $(this).find('variacion').text();
			if (symb.toUpperCase() in {'FTSE':'','DJI':'', 'BVSP':''})
			{
				msg += nombre.toUpperCase() + '=&nbsp;' +valor+ '&nbsp; &nbsp;&nbsp;('+variacion+' %)'+ separador;
			}
		});
		pos = msg.lastIndexOf('|');
		msg = msg.substring(0,pos-1);
		$('<marquee direction="rigth" scrollamount="4" OnMouseOver="this.stop();" OnMouseOut="this.start();"></marquee>').html(msg).appendTo("#spanNoticias");
		$("#spanNoticias").height(12);
	  },
	  error: function(i,x,c){
		//alert(x);
	  }
	});
});
