
window.onfocus = setFocus;
window.onclose = closeWindow;
function setFocus()
{
if (window.opener)
{
if (window.opener.setRegainWindow)
{
window.opener.setRegainWindow();
}
if (window.opener.getAmount && window.opener.setAmount)
{
amount = window.opener.getAmount();
if (amount != "0")
{
window.opener.setAmount("0");
fromCurrencyCode = getFromCurrencyObject().value;
doConversion(fromCurrencyCode, amount);
}
}
}
}
function closeWindow()
{
if (window.opener.closingCurrencyConverter)
{
window.opener.closingCurrencyConverter();
}
}
function selectDropdownItem(selectedItem, dropdown)
{
if(dropdown.selectIndex)
{
for(var i=0; i<dropdown.length; i++)
{
var selectText = dropdown.options[i].value;
if(selectText == selectedItem)
{
dropdown.selectedIndex = i;
}
}
}
}
function setCurrencies(srcCurrency, dstCurrency)
{
if(showFromCurrencyDropdown == true)
{
selectDropdownItem(srcCurrency, getFromCurrencyObject());
}
else
{
getFromCurrencyObject().value = srcCurrency;
}
if(showToCurrencyDropDown == true)
{
selectDropdownItem(srcCurrency, getToCurrencyObject());
}
else
{
getToCurrencyObject().value = dstCurrency;
}
}
function doConversion(fromCurrencyCode, amount)
{
if(busy == false)
{
var isValid = validateAmount(amount);
if(isValid)
{
if(fromCurrencyCode == getFromCurrencyObject().value)
{
getThisElement('ResultsSection').style.display = "block";
var numberOfDecimalPlacesFactor = Math.pow(10, decimalPlaces);
var result = new String( (amount * numberOfDecimalPlacesFactor) * exchangeRate);
var roundedResult = Math.round(result) / numberOfDecimalPlacesFactor;
result = roundedResult;
var srcCurrencyDescription = getSelectedFromCurrencyCode();
var dstCurrencyDescription = getSelectedToCurrencyCode();
document.getElementById("Result").innerHTML = amount + ' ' + srcCurrencyDescription + ' = ' + result + ' ' + dstCurrencyDescription + " " + approxText;
document.getElementById("ExchangeRate").innerHTML = "1 " + srcCurrencyDescription + " = " + exchangeRate + " " + dstCurrencyDescription + " " + basedText;
document.getElementById("ProcessingMessage").style.display = "none";
}
else
{
setCurrencies(fromCurrencyCode, getToCurrencyObject().value);
submit();
}
}
}
else
{
submit();
}
}
function validateCurrency(amount)
{
result = amount.match(/^\s*(\d*\.?\d*)\s*$/);
return result!=null&&result[1].length>0?result[1]:null;
}
function validateAmount(amount)
{
var isValid = false;
result = validateCurrency(amount)
if(result==null)
{
getThisElement('AmountError').style.display = "block";
}
else
{
getAmountToConvertObject().value = result;
getThisElement('AmountError').style.display = "none";
isValid = true;
}
return isValid;
}
function submit()
{
var isValid = validateAmount(getAmountToConvertObject().value);
if(isValid)
{
document.CurrencyConverter.submit();
}
}
function toCurrencyChanged()
{
busy = true;
document.getElementById("ResultsSection").style.display = "none";
document.getElementById("ProcessingMessage").style.display = "block";
submit();
}
function fromCurrencyChanged()
{
busy = true;
document.getElementById("ResultsSection").style.display = "none";
document.getElementById("ProcessingMessage").style.display = "block";
submit();
}
function populateCurrencies(selectedcurrencyCode)
{
for (var currencyCode in currencyList)
{
document.writeln('<option value="' + currencyCode + '"  ');
if(selectedcurrencyCode == currencyCode)
{
document.writeln(' selected ');
}
document.writeln('>' + currencyList[currencyCode] + ' (' + currencyCode + ')' + '</option>');
}
}
function getCurrencyDescription(currencyCode)
{
return(currencyList[currencyCode] + " (" + currencyCode + ")");
}
function getSelectedFromCurrencyCode()
{
return getFromCurrencyObject().value;
}
function getSelectedToCurrencyCode()
{
return getToCurrencyObject().value;
}
function getFromCurrencyObject()
{
return document.CurrencyConverter.fromCurrency;
}
function getToCurrencyObject()
{
return document.CurrencyConverter.toCurrency;
}
function getAmountToConvertObject()
{
return document.CurrencyConverter.amountToConvert;
}
function setDecimalPlaces(numberOfDecimalPlaces)
{
decimalPlaces = numberOfDecimalPlaces;
}
function setExchangeRate(rate)
{
exchangeRate = rate;
}
function getThisElement(myID)
{
if(document.getElementById)
{
return document.getElementById(myID);
}
else if(document.all)
{
return document.all[myID];
}
else if(document.layers)
{
return document.layers[myId];
}
return null;
}
function convertButton(text,linkurl)
{
divChoice = (document.all && document.getElementById && !document.body.uniqueID) ? "vsubmit3" : "vsubmit";
document.write('<div class='+divChoice+'>');
document.write('<div class="vsubmit2">');
document.write('<a style="color:#FFFFFF; text-decoration:none;" title="'+text+'" href="'+linkurl+'">&nbsp;' + text + '&nbsp;&nbsp;&nbsp;');
document.write('</a></div></div>');
}

