var objhttpCalculator;
var type,typeRange;
var calcLoadingGifTimeout = "";

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	objhttpCalculator = new XMLHttpRequest();
    if (objhttpCalculator.overrideMimeType) 
    	objhttpCalculator.overrideMimeType('text/xml');
} 
else if (window.ActiveXObject) 
{ // IE
	try {
    	objhttpCalculator = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (e) 
	{
	    try {
    	    objhttpCalculator = new ActiveXObject("Microsoft.XMLHTTP");
        } 
		catch (e) {}
    }
 }	
	
//update claculator results
function updateCalculatorResult()
{	
	try{	
		//show reload gif
		changeLoadingGifStatusCalc(true);
		if ((objhttpCalculator.readyState==4) && (objhttpCalculator.status == 200))
		{			
			calcLoadingGifTimeout = window.setTimeout("changeLoadingGifStatusCalc(false)",500);
			var respText = objhttpCalculator.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
				updateCalculatorTableCells(ar.linkCalc);
				
			}				
		}
	}catch(e){
	}					
}

function updateRangeCalculatorResult()
{	
	try{	
		//show reload gif
		changeLoadingGifStatusCalc(true);
		if ((objhttpCalculator.readyState==4) && (objhttpCalculator.status == 200))
		{			
			calcLoadingGifTimeout = window.setTimeout("changeLoadingGifStatusCalc(false)",500);
			var respText = objhttpCalculator.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
				updateRangeCalculatorTableCells(ar.linkCalc);
				
			}				
		}
	}catch(e){
	}					
}

function updateCalculatorTableCells(tableData)
{
	if (type=="dollar")
	{
		document.getElementById("calcLinkdollarTD").style.display = "inline";
		document.getElementById("calcLinkindiceTD").style.display = "none";
		document.getElementById("calcRangedollarTD").style.display = "none";
		document.getElementById("calcRangeindiceTD").style.display = "none";
		document.getElementById("calcLink" + type + "Date").innerHTML = tableData[0].date;
	}
	else
	{
		document.getElementById("calcLinkdollarTD").style.display = "none";
		document.getElementById("calcLinkindiceTD").style.display = "inline";
		document.getElementById("calcRangedollarTD").style.display = "none";
		document.getElementById("calcRangeindiceTD").style.display = "none";
		val15 = tableData[0].date ;
		val15 = val15.substr(val15.indexOf("/")+1);
		document.getElementById("calcLink" + type + "Date").innerHTML = val15 ;
	} 

	
	document.getElementById("calcLink" +type +"Res").innerHTML = tableData[0].res;
}

function updateRangeCalculatorTableCells(tableData)
{
	var fd,td ;

	if (typeRange=="dollar")
	{
		document.getElementById("calcLinkdollarTD").style.display = "none";
		document.getElementById("calcLinkindiceTD").style.display = "none";
		document.getElementById("calcRangedollarTD").style.display = "inline";
		document.getElementById("calcRangeindiceTD").style.display = "none";
		fd = tableData[0].fromDate;
		td = tableData[0].toDate;
	}
	else
	{
		document.getElementById("calcLinkdollarTD").style.display = "none";
		document.getElementById("calcLinkindiceTD").style.display = "none";
		document.getElementById("calcRangedollarTD").style.display = "none";
		document.getElementById("calcRangeindiceTD").style.display = "inline";
		fd = tableData[0].fromDate;
		td = tableData[0].toDate;
		fd = fd.substr(fd.indexOf("/")+1);
		td = td.substr(td.indexOf("/")+1);
	} 

	document.getElementById("calcRange" + typeRange + "Date").innerHTML = fd + " - " + td;
	document.getElementById("calcRange" +typeRange +"Res").innerHTML = tableData[0].diff;
}


/*
gets update from db
*/
function CalculatorUpdateFromDB(dolRadButObj)
{
	var parameters = "";
	var parDate = document.getElementById("date1").value ;
	
	if (dolRadButObj.checked)
		type = "dollar" ;
	else
		type = "indice" ;
	
	try{	
		objhttpCalculator.open ("GET", "/f/ajax/calculator/getUpdate.jhtml?date1=" + parDate + "&rateType=" + type, true);
		objhttpCalculator.onreadystatechange=updateCalculatorResult;				
		objhttpCalculator.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;");				
		objhttpCalculator.send(parameters);			
	}catch(e){}					
}


function rangeCalculatorUpdateFromDB(dolRadButObj1)
{
	var parameters = "";
	var startDate = document.getElementById("date2").value ;
	var endDate = document.getElementById("date3").value ;
	
	if (dolRadButObj1.checked)
		typeRange = "dollar" ;
	else
		typeRange = "indice" ;
	
	try{	
		objhttpCalculator.open ("GET", "/f/ajax/calculator/getUpdateRange.jhtml?fDate=" + startDate + "&tDate=" + endDate + "&difType=" + typeRange, true);
		objhttpCalculator.onreadystatechange=updateRangeCalculatorResult;				
		objhttpCalculator.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;");				
		objhttpCalculator.send(parameters);			
	}catch(e){}					
}

function changeLoadingGifStatusCalc(status)
{
	if(status)
	{
		document.getElementById("loadingGifCalc").style.display = "inline";
		document.getElementById("replaceLoadingGifCalc").style.display = "none";
	}
	else
	{
		document.getElementById("loadingGifCalc").style.display = "none";
		document.getElementById("replaceLoadingGifCalc").style.display = "inline";
	}
}
