/* 
 * Copyright (c) 2003-2006 Finatek LLC.
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Finatek LLC
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with Finatek LLC. No permission to use is granted
 * to anybody without license agreement with Finatek LLC.
 */

var handleSuccessRate = function(o)
{
	if(o.responseText != undefined)
	{
		var root = Xparse(o.responseText);
		
		var errorTag = document.getElementById("rates_error");
		if (errorTag != undefined)
		{
			if (o.responseText.indexOf("<?xml") != 0)
			{
				errorTag.innerHTML = o.responseText;
			}
			else
			{
				errorTag.innerHTML = "";
			}
		}

		if (root.contents != undefined)
		{
			for (var i = 0; i < root.contents.length - 1; i++)
			{
				if (root.contents[i].type == "element" && root.contents[i].name == "rates")
				{
					var rates = root.contents[i];
				}
			}

			if (rates != undefined)
			{
				for (var i = 0; i < rates.contents.length - 1; i++)
				{
					if (rates.contents[i].type == "element" && rates.contents[i].name == "rate")
					{
						var curRateSymbol = rates.contents[i].attributes.symbol;
		
						var rateTag = document.getElementById(curRateSymbol);
						if (rateTag != undefined)
						{
							var prevRateBidTag = document.getElementById(curRateSymbol + "_bid");
							if (prevRateBidTag != undefined)
							{
								var prevRateBid = prevRateBidTag.innerHTML;
								var curRateBid = rates.contents[i].attributes.bid;
								if (!isNaN(parseFloat(prevRateBid)) && curRateBid > prevRateBid)
								{
									prevRateBidTag.className = "bid, up";
								}
								else if (!isNaN(parseFloat(prevRateBid)) && curRateBid < prevRateBid)
								{
									prevRateBidTag.className = "bid, down";
								}
								prevRateBidTag.innerHTML = curRateBid;
							}
							
							
			
							var prevRateAskTag = document.getElementById(curRateSymbol + "_ask");
							if (prevRateAskTag != undefined)
							{
								var prevRateAsk = prevRateAskTag.innerHTML;
								var curRateAsk = rates.contents[i].attributes.ask;
								if (!isNaN(parseFloat(prevRateAsk)) && curRateAsk > prevRateAsk)
								{
									var simbolTag = document.getElementById(curRateSymbol);
									try
									{simbolTag.className = "icono_up";
									}
									catch(e){}
									prevRateAskTag.className = "ask, up";
								}
								else if (!isNaN(parseFloat(prevRateAsk)) && curRateAsk < prevRateAsk)
								{
									var simbolTag = document.getElementById(curRateSymbol);
									try
									{simbolTag.className = "icono_down";
									}
									catch(e){}
									prevRateAskTag.className = "ask, down";
								}
								prevRateAskTag.innerHTML = curRateAsk;
							}
		
		
							
							var prevRateHighTag = document.getElementById(curRateSymbol + "_high");
							if (prevRateHighTag != undefined)
							{
								prevRateHighTag.innerHTML = rates.contents[i].attributes.high;
							}
		
							
							
							var prevRateLowTag = document.getElementById(curRateSymbol + "_low");
							if (prevRateLowTag != undefined)
							{
								prevRateLowTag.innerHTML = rates.contents[i].attributes.low;
							}
						}
					}
				}
			}
		}
	}
};


var handleFailureRate = function(o)
{
	var errorTag = document.getElementById("rates_error");
	if (errorTag != undefined)
	{
		errorTag.innerHTML = "AJAX error. " + o.statusText;
	}
}

var callbackRate =
{
	success:handleSuccessRate,
	failure:handleFailureRate,
//	timeout: 5000,
	argument:[]
};

var unique = Math.random();

function refresh_currency()
{
	protocolo = "http";
	//var randomnumber=Math.floor(Math.random()*1000);
	if((location.href.indexOf('https')!=-1))
	{
		protocolo = "https";
	}
	//ratesURL = protocolo + '://www.forexchile.cl/precios/rates.php?k';
	ratesURL = protocolo + '://www.forexchile.cl/precios/fx/resumen.php';

	if((location.href.indexOf('forexchile')!=-1) || (location.href.indexOf('200.29.22.2')!=-1)) {
		//+ '&rnd='+randomnumber+'&r='+unique
		var request = YAHOO.util.Connect.asyncRequest('GET', ratesURL, callbackRate);
		window.setTimeout('refresh_currency()', ratesRefresh * 1000);	
	}
};



