var objhttpCalculatorExchange;
var calcExchangeLoadingGifTimeout = "";
var amount;
var fromSecCode,toSecCode;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	objhttpCalculatorExchange = new XMLHttpRequest();
    if (objhttpCalculatorExchange.overrideMimeType) 
    	objhttpCalculatorExchange.overrideMimeType('text/xml');
} 
else if (window.ActiveXObject) 
{ // IE
	try {
    	objhttpCalculatorExchange = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (e) 
	{
	    try {
    	    objhttpCalculatorExchange = new ActiveXObject("Microsoft.XMLHTTP");
        } 
		catch (e) {}
    }
 }	
	
//update claculator results
function updateCalculatorExchangeResult()
{	
	try{	
		//show reload gif
		changeLoadingGifStatusCalcExchange(true);
		if ((objhttpCalculatorExchange.readyState==4) && (objhttpCalculatorExchange.status == 200))
		{			
			calcExchangeLoadingGifTimeout = window.setTimeout("changeLoadingGifStatusCalcExchange(false)",500);
			var respText = objhttpCalculatorExchange.responseText;//get response text	
			if(respText !="")//if there is response text
			{		
				// respText is written in json
				var ar = eval('(' + respText + ')');
				
				//ar.securities is array of data
				updateCalculatorExchangeTableCells(ar.exchangeCalc);
			}				
		}
	}catch(e){
	}					
}


function updateCalculatorExchangeTableCells(tableData)
{
	var fRate = tableData[0].from;
	var tRate = tableData[0].to;
	var res = fRate/tRate ;
	var fn = tableData[0].fromName;
	var tn = tableData[0].toName;
	
	while(fn.indexOf("~")!=-1)
	{
		fn=fn.replace("~","&quot;");
	}
	while(fn.indexOf("#")!=-1)
	{
		fn=fn.replace("#","&rsquo;");
	}
	
	while(tn.indexOf("~")!=-1)
	{
		tn=tn.replace("~","&quot;");
	}
	while(tn.indexOf("#")!=-1)
	{
		tn=tn.replace("#","&rsquo;");
	}

	if (fromSecCode=='8')  // YEN
		res = res / 100 ;
	if (toSecCode=='8')    //YEN
		res = res * 100 ; 
	if (fromSecCode=='36')  //LEBANON
		res = res / 10 ; 
	if (toSecCode=='36')    //LEBANON
		res = res * 10 ; 
	
	if (amount!=0)
	{
		res = FixFormat(res * amount,4) ;
		amount = FixFormat(amount,4) ;
	}
	else
		res = 0 ;
		
	
	
	document.getElementById("calcExchangeTD").style.display = "inline";
	document.getElementById("calcExchangeFrom").innerHTML = amount + " " + fn;
	document.getElementById("calcExchangeTo").innerHTML = res + " " + tn;
}

/*
gets update from db
*/
function CalculatorExchangeUpdateFromDB(f,t,am)
{
	var parameters = "";
	amount = am ;
	fromSecCode = f ;
	toSecCode = t ;
	try{	
		objhttpCalculatorExchange.open ("GET", "/f/ajax/calculator/getUpdateExchange.jhtml?curFrom=" + f + "&curTo=" + t, true);
		objhttpCalculatorExchange.onreadystatechange=updateCalculatorExchangeResult;				
		objhttpCalculatorExchange.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;");				
		objhttpCalculatorExchange.send(parameters);			
	}catch(e){}					
}

function changeLoadingGifStatusCalcExchange(status)
{
	if(status)
	{
		document.getElementById("loadingGifCalcExchange").style.display = "inline";
		document.getElementById("replaceLoadingGifCalcExchange").style.display = "none";
	}
	else
	{
		document.getElementById("loadingGifCalcExchange").style.display = "none";
		document.getElementById("replaceLoadingGifCalcExchange").style.display = "inline";
	}
}
