/* 
 * 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)
//function handleSuccessRate(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 = "";
			}
		}
		//alert(root.contents[1].type);
		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);
						//alert('2--'+curRateSymbol+'=='+rates.contents[i].attributes.bid );
						if (rateTag != undefined)
						{
							var prevRateBidTag = document.getElementById(curRateSymbol + "_bid");
							var prevRateBidTag2 = document.getElementById(curRateSymbol + "2_bid");
							if (prevRateBidTag != undefined)
							{
								if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
								{
									var prevRateBid = take_out_first(prevRateBidTag.innerHTML);	
								}else
								{
									var prevRateBid = take_out(prevRateBidTag.innerHTML);
								}
								var prevRateBid3 = "#"+curRateSymbol + "_bid";
								var prevRateBid4 = "#"+curRateSymbol + "2_bid";
								var curRateBid = rates.contents[i].attributes.bid;
								if (!isNaN(parseFloat(prevRateBid)) && curRateBid > prevRateBid)
								{
									//prevRateBidTag.className = "bid, up1";
									//changecolorup(prevRateBidTag);
									if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
									{
										changecolorup(prevRateBid3);
										changecolorup("#"+curRateSymbol + "_ask");
									}else
									{
										changecolorup(prevRateBid3);
									}
									//prevRateBidTag2.innerHTML ="<img src=\"images/up.gif\" />";
									//$('#pricefeed_container').find(prevRateBid4).html('<img src=\"images/up.gif\" />');
									//$('.feed_pair_container_content').find(prevRateBid4).html('<img src=\"images/pic_arrow_up.gif\" />');
									
									$('.feed_pair_container_content').find(prevRateBid4).html('<img src=\"images/BlueArrow.png\" />');

								}
								else if (!isNaN(parseFloat(prevRateBid)) && curRateBid < prevRateBid)
								{
									//changecolordown(prevRateBidTag);
									if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
									{
										changecolordown(prevRateBid3);
										changecolordown("#"+curRateSymbol + "_ask");
									}else
									{
										changecolordown(prevRateBid3);
									}
									//prevRateBidTag2.innerHTML ="<img src=\"images/down.gif\" />";
									//$('#pricefeed_container').find(prevRateBid4).html('<img src=\"images/down.gif\" />');
									//$('.feed_pair_container_content').find(prevRateBid4).html('<img src=\"images/pic_arrow_down.gif\" />');
									
									$('.feed_pair_container_content').find(prevRateBid4).html('<img src=\"images/RedArrow.png\" />');	
									
									
								}
								//prevRateBidTag.innerHTML = curRateBid;
								if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
								{
									curRateBid=last_two_first(curRateBid);
								}else
								{
									curRateBid=last_two_first(curRateBid);
								}
								$('.feed_pair_container_content').find(prevRateBid3).html(curRateBid);								
							}
										
							var prevRateAskTag = document.getElementById(curRateSymbol + "_ask");
							if (prevRateAskTag != undefined)
							{
								if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
								{
									var prevRateAsk = take_out_first(prevRateAskTag.innerHTML);
								}else
								{
									var prevRateAsk = take_out(prevRateAskTag.innerHTML);
								}
								var prevRateAsk3 = "#"+curRateSymbol + "_ask";
								var prevRateAsk4 = "#"+curRateSymbol + "2_ask";
								var curRateAsk = rates.contents[i].attributes.ask;
								if (!isNaN(parseFloat(prevRateAsk)) && curRateAsk > prevRateAsk)
								{
									//revRateAskTag.className = "ask, up";
									if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
									{
										changecolorup(prevRateAsk3);
										changecolorup("#"+curRateSymbol + "_bid");
									}else
									{
										changecolorup(prevRateAsk3);	
									}
									
								}
								else if (!isNaN(parseFloat(prevRateAsk)) && curRateAsk < prevRateAsk)
								{
									//prevRateAskTag.className = "ask, down";
									if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
									{
										changecolordown(prevRateAsk3);
										changecolordown("#"+curRateSymbol + "_bid");
									}else
									{
										changecolordown(prevRateAsk3);	
									}	
								}
								//prevRateAskTag.innerHTML = curRateAsk;
								if(curRateSymbol=='GCUSD' || curRateSymbol=='SIUSD')
								{
									curRateAsk=last_two_first(curRateAsk);
								}else
								{
									curRateAsk=last_two_first(curRateAsk);
								}
								$('.feed_pair_container_content').find(prevRateAsk3).html(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:[]
};

function refresh_currency()
{
	var request = YAHOO.util.Connect.asyncRequest('GET', ratesURL, callbackRate);
	window.setTimeout('refresh_currency()', ratesRefresh * 1000);
	
};
/*
function refresh_currency()
{
	//var request = YAHOO.util.Connect.asyncRequest('GET', ratesURL, callbackRate);
	var temp = $.ajax({
		method: "GET",
		url: "../php/rates.php",
		datatype: "xml",
		//data: "page="+content_show,
		//beforeSend: function(){$("#loading").show();}, //show loading just when link is clicked
		//error: function(){$("#error").fadeIn(500);},
		//complete: function(){ $("#loading").fadeOut(500);}, //stop showing loading when the process is complete
		success: function(html){ //so, if data is retrieved, store it in html
		//$(".content").show(); //animation
		//$(".content").text(html); //show the html inside .content div
		handleSuccessRate(html);
		 }
	}); //close $.ajax(
	
	//Window.setTimeout('refresh_currency()', ratesRefresh * 1000);	
};
*/
var bflag=true;
var bt=navigator.appName;
var bv=navigator.appVersion;
if(bv.indexOf("MSIE 9.0")==-1 && bt.indexOf("Microsoft")!=-1)
{
	bflag=false;
}


function last_two(x)
{
	if(bflag==false)
	{
		return x;	
	}
	var last_two=x.substr(-2,2);
	var org=x.slice(0,x.length-2);
	return org+"<span class=\"last_two\">"+last_two+"</span>";
}
function take_out(x)
{
	var temp=x.replace("<span class=\"last_two\">","");
	temp=temp.replace("</span>","");
	//alert(temp);
	return temp;
}
function last_two_first(x)
{
	if(bflag==false)
	{
		return x;	
	}
	var last_two=x.substr(-2,2);
	var org=x.slice(0,x.length-2);
	return org+"<span class=\"last_two_first\">"+last_two+"</span>";
}

function take_out_first(x)
{
	var temp=x.replace("<span class=\"last_two_first\">","");
	temp=temp.replace("</span>","");
	//alert(temp);
	return temp;
}

function changecolorup(v)
{
	 //$(v).animate( { color:"#ff0000"}, 300 ).animate( { color:"#ffffff"}, 100 );
	 //$('#pricefeed_container').find(v).animate( { color:"#ff0000"}, 300 ).animate( { color:"#000000"}, 100 );
	 $('.feed_pair_container_content').find(v).css("color","#264dbc");
}
function changecolordown(v)
{
	 //$(v).animate( { color:"#264dbc"}, 300 ).animate( { color:"#ffffff"}, 100 );
	  //$('#pricefeed_container').find(v).animate( { color:"#264dbc"}, 300 ).animate( { color:"#000000"}, 100 );
	  $('.feed_pair_container_content').find(v).css("color","#ae0e0e");
}
