﻿
var updating=false;var exchangeRates={exchangeRate:0.000,invExchangeRate:0.000,lastUpdated:'',lastUpdatedLink:'',invExchangeRateDelta:'',exchangeRateDelta:'',C1Currency:'',C2Currency:'',C1Symbol:'',C2Symbol:'',Amount:1.00};var calculator={defaultNotesText:"Click to add notes.",initialize:function(){$("#start").click(function(){this.select();});$('#start').bind('keyup',function(){calculator.validateInput(this);});$("#buttonConvertC1toC2").click(calculator.convertC1toC2);$("#buttonConvertC2toC1").click(calculator.convertC2toC1);$(".addNotesHereBox").live('click',calculator.clearIfDefault);$(".addNotesHereBox").live('blur',calculator.restoreDefaultIfEmpty);$("#start").keyup(calculator.startInputKeyUp);},startInputKeyUp:function(e){var isValid=calculator.validateInput(this);if(e.keyCode==13&&isValid){if(!isFlipSite){$("#buttonConvertC1toC2").click();$('#buttonConvertC1toC2').addClass('ui-selected');}else{$("#buttonConvertC2toC1").click();$('#buttonConvertC2toC1').addClass('ui-selected');}}},convertC1toC2:function(){try{var startValue=$("#start").val();calculator.invConvert(startValue);var amount=calculator.convertFrom(startValue);calculator.setupPermaLink(exchangeRates.C1Currency,exchangeRates.C2Currency,exchangeRates.C1Symbol,startValue,exchangeRates.C2Symbol,amount);calculator.addConverstionToTable(exchangeRates.C1Currency,exchangeRates.C2Currency,exchangeRates.C1Symbol,startValue,exchangeRates.C2Symbol,amount);return false;}
catch(ex){alert(ex.description);return false;}},convertC2toC1:function(){try{var startValue=$("#start").val();calculator.convert(startValue);var amount=calculator.convertTo(startValue);calculator.setupPermaLink(exchangeRates.C2Currency,exchangeRates.C1Currency,exchangeRates.C2Symbol,startValue,exchangeRates.C1Symbol,amount);calculator.addConverstionToTable(exchangeRates.C2Currency,exchangeRates.C1Currency,exchangeRates.C2Symbol,startValue,exchangeRates.C1Symbol,amount);return false;}
catch(ex){alert(ex.description);return false;}},convert:function(startValue){if(!isNaN(startValue)){$('#finish').val(calculator.convertTo(startValue));$('#FromCurrencySymbol').html('&nbsp;'+exchangeRates.C2Symbol);$('#ToCurrencySymbol').html('&nbsp;'+exchangeRates.C1Symbol);}else{calculator.showNaNAlert();}},invConvert:function(startValue){if(!isNaN(startValue)){$('#finish').val(calculator.convertFrom(startValue));$('#FromCurrencySymbol').html('&nbsp;'+exchangeRates.C1Symbol);$('#ToCurrencySymbol').html('&nbsp;'+exchangeRates.C2Symbol);}else{calculator.showNaNAlert();}},convertTo:function(amount){return Math.round((exchangeRates.exchangeRate*amount)*100)/100;},convertFrom:function(amount){return Math.round((exchangeRates.invExchangeRate*amount)*100)/100;},showNaNAlert:function(){alert('The value entered is not a number, please enter only numeric values. e.g. 34.53');$("#start").focus();$("#start").select();},validateInput:function(){var isValid=false;if(isNaN($("#start").val())){$("#start").addClass('validationError');$("#start").removeClass('validationOK');$("#ErrorMessage1").text("The value entered is not a number, please enter only numeric values. e.g. 1234.56");$("#ErrorMessage1").show('fast');}else{$("#start").addClass('validationOK');$("#start").removeClass('validationError');$("#ErrorMessage1").hide();$("#ErrorMessage1").text();isValid=true;}
$("finish").text("");return isValid;},setupPermaLink:function(fromCurrency,toCurrency,fromSymbol,from){var permaLink=document.getElementById("permalink");permaLink.title="Permalink for the conversion of "+fromCurrency+" "+from+" to "+toCurrency;permaLink.href=calculator.getPermaLink(fromCurrency,toCurrency,from);var permalinkSpan=document.getElementById("permalinkSpan");permalinkSpan.style.display="";},addConverstionToTable:function(currency,toCurrency,fromSymbol,from,toSymbol,to){if(isNaN(to)||isNaN(from)){return;}
var url=calculator.getPermaLink(currency,toCurrency,from);var fromCell=document.createElement("TD");fromCell.innerHTML+='<a href="'+encodeURI(url)+'" class="PopupLink" target="_blank">'+fromSymbol+from+'</a>';var middleCell=document.createElement("TD");middleCell.innerHTML='=';var toCell=document.createElement("TD");toCell.innerHTML=toSymbol+to;var notes=document.createElement("TD");notes.innerHTML='<input type="text" value="'+calculator.defaultNotesText+'" class="addNotesHereBox addNotesHereDefault"/>';var row=document.createElement("TR");row.appendChild(fromCell);row.appendChild(middleCell);row.appendChild(toCell);row.appendChild(notes);var tbody=document.getElementById("historyTable").getElementsByTagName("tbody")[0];tbody.appendChild(row);},clearIfDefault:function(e){if($(this).hasClass("addNotesHereDefault")){$(this).val("");$(this).removeClass("addNotesHereDefault");}
return false;},restoreDefaultIfEmpty:function(e){if($(this).val()==""){$(this).val(calculator.defaultNotesText);$(this).addClass("addNotesHereDefault");}
return false;},getPopupLink:function(converterUrl,fromCurrency,amount){return converterUrl+'?Base='+fromCurrency+'&amp;Amount='+amount+'&amp;Source='+siteName;},getPermaLink:function(fromCurrency,toCurrency,amount){return'/Convert/'+fromCurrency+'/'+toCurrency+'/'+amount;},getShortLink:function(fromCurrency,toCurrency){return'http://d2p.cc/'+fromCurrency+'/'+toCurrency;},getShortLinkForAmount:function(fromCurrency,toCurrency,amount){return'http://d2p.cc/'+fromCurrency+'/'+toCurrency+'/'+amount;},selectHistoryRow:function(){var sourceElement=event.srcElement;sourceElement.ClassName="Selected";}};var autoUpdate={getLatestExchangeRate:function(){$.ajax({type:"GET",url:"/API/ExchangeRate?Key=E3ADCED4-0CCF-4531-AC07-BEB51218BF6C&C1="+exchangeRates.C1Currency+"&C2="+exchangeRates.C2Currency,contentType:"application/json; charset=utf-8",processData:false,dataType:"json",timeout:5000,async:true,success:function(result){autoUpdate.showUpdatedRates(result);},error:function(xhr,textStatus,errorThrown){ajaxFailed(xhr,textStatus,errorThrown);}});},showUpdatedRates:function(result){autoUpdate.updateRateValues(result);autoUpdate.chartRefreshCounter++;autoUpdate.refreshRates();},updateRateValues:function(result){exchangeRates.invExchangeRate=result.C1Value;exchangeRates.exchangeRate=result.C2Value;exchangeRates.lastUpdated=result.LastUpdated;exchangeRates.lastUpdatedLink=result.LastUpdatedLink;exchangeRates.invExchangeRateDelta=exchangeRates.invExchangeRate-result.C1Value;exchangeRates.exchangeRateDelta=exchangeRates.exchangeRate-result.C2Value;},refreshRates:function(){var fadeCompleted=false;$(".fadable").fadeOut(400,function(){$('#ratesC1Value').html(exchangeRates.invExchangeRate);$('#ratesC2Value').html(exchangeRates.exchangeRate);$('#ratesUpdatedLink').html(exchangeRates.lastUpdated);$('#ratesUpdatedLink').attr('href',exchangeRates.lastUpdatedLink);$(".fadable").fadeIn(400,function(){});});}};var chart={chartRefreshCounter:0,selectedDaysHistory:365,refreshChart:function(){if(chart.chartRefreshCounter>=5){chart.chartRefreshCounter=0;$("#historyChart").fadeOut('slow',function(){chart.selectChart(chart.selectedDaysHistory);$("#historyChart").fadeIn('slow',function(){});});}},getChartCurrencyFragment:function(){if(chart.flipped){return exchangeRates.C1Currency+"-"+exchangeRates.C2Currency;}else{return exchangeRates.C2Currency+"-"+exchangeRates.C1Currency;}},getLinkUrl:function(){var currencyFragment;if(chart.flipped){currencyFragment=exchangeRates.C1Currency+"/"+exchangeRates.C2Currency;}else{currencyFragment=exchangeRates.C2Currency+"/"+exchangeRates.C1Currency;}
return"/Charts/"+currencyFragment+"/"+chart.selectedDaysHistory;},selectChart:function(daysHistory){chart.selectedDaysHistory=daysHistory;var imageSource="http://forexcharts.s3.amazonaws.com";var chartCurrencies=chart.getChartCurrencyFragment();var imageUrlTimeStampParameter="?timestamp="+new Date().getTime();var imageUrl=imageSource+"/HistoryChart-"+chartCurrencies+"-"+daysHistory+"-days.png"+imageUrlTimeStampParameter;var imageLink=chart.getLinkUrl();$('#historyChart').attr('src',imageUrl).attr('alt','exchange rate history chart for the last '+chart.selectedDaysHistory).attr('width',600).attr('height',444);$('#chartLink').attr('href',imageLink);},flipChart:function(){chart.flipped=!chart.flipped;chart.selectChart(chart.selectedDaysHistory);}};var newChart={flipped:true,selectedHistoryOption:0,buildChart:function(){var url='/API/ChartData?Key=E3ADCED4-0CCF-4531-AC07-BEB51218BF6C';if(newChart.flipped){url+='&C1='+exchangeRates.C2Currency;url+='&C2='+exchangeRates.C1Currency;}else{url+='&C1='+exchangeRates.C1Currency;url+='&C2='+exchangeRates.C2Currency;}
url+='&Amount='+exchangeRates.Amount;$.getJSON(url,newChart.populateChart);},populateChart:function(data){window.chart=new Highcharts.StockChart({chart:{renderTo:'highchartContainer'},rangeSelector:{buttons:[{type:'day',count:1,text:'1d'},{type:'week',count:1,text:'1w'},{type:'month',count:1,text:'1m'},{type:'month',count:3,text:'3m'},{type:'month',count:6,text:'6m'},{type:'year',count:1,text:'1y'},{type:'year',count:2,text:'2y'},{type:'year',count:5,text:'5y'},{type:'all',text:'All'}],selected:newChart.selectedHistoryOption,inputEnabled:false},title:{text:'Exchange Rate History'},subtitle:{text:data.SeriesCaption},credits:{enabled:false},series:[{name:data.SeriesCaption,data:data.SeriesData,tooltip:{yDecimals:4}}],exporting:{filename:data.SeriesCaption+'-ExchangeRateHistory'}});}};function ajaxFailed(xhr,textStatus,errorThrown){return;}
