// empty Topics select list content
function clearSelect(select) {
    while (select.length > 0) {
        select.remove(0);
    }
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
	select.options[select.options.length] = new Option(content, value, false, false);
}
// toggle visibility by setting style.visibility
// as in this case we want the element to continue
// taking up space in the design
function toggleVisibility(obj) {
	var el = document.getElementById(obj);
	if ( el.style.visibility == 'hidden' ) {
		el.style.visibility = '';
		el.style.display = '';
	}
	else {
		el.style.visibility = 'hidden';
	}
}

// toggle visibility by setting style.display
// in this case we want the element to release
// its space in the design
function toggleDisplay(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

// toggle whether an item is enabled or note
// generally only useful for input controls
function toggleEnable(obj) {
	var el = document.getElementById(obj);
	if ( el.enabled != false ) {
		el.enabled = false;
	}
	else {
		el.enabled = true;
	}
}

function loadCss (url) {
    if (document.layers)
        window.location.href = url;
    else if (document.getElementById) {
        var script = document.createElement('link');
        script.href = url;
        script.rel = 'stylesheet';
        script.type = 'text/css';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
}

function loadJavascript (url) {
    //alert('loadJavascript : ' + url);
    if (document.layers)
        window.location.href = url;
    else if (document.getElementById) {
        var script = document.createElement('script');
        script.src = url;
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
}

function random () {
    return 1 + Math.floor(Math.random() * 1000);
}

function displayRateTable(rateTableHtml) {
  //alert('Loading rateTableHtml');
  document.getElementById('rateTablePlaceHolder').innerHTML = rateTableHtml;
  toggleVisibility('imgRateTableLoading');
  toggleEnable('btnGetRateTable');
  //document.getElementById('btnGetRateTable').enabled = true;
}

function getRateTable(virtualPath) {
  var selectList = document.getElementById("ddCurrencies");
  //alert(selectList.value);
  if (selectList.value == "") {
    alert('Please select a currency to show the rates for.');
  } else {
    //document.getElementById('btnGetRateTable').enabled = false;
    toggleEnable('btnGetRateTable');
    toggleVisibility('imgRateTableLoading');
    var urlString = virtualPath + 'rateTable.ashx?pid='+partnerId+'&c='+selectList.value+'&'+random()+'='+random();
    // using JSON callback function		
    loadJavascript(urlString);
    // using LazyLoad and post load function
    //LazyLoad.load(urlString, displayRateTable);
  }
}
var partnerId = 1;
var virtualPath = 'http://preview.customhouse.com/ratecalc2/';
var rateTableHeaderHtml = "<p><label for='ddCurrencies'>Buy/Sell Rates for : </label><select id='ddCurrencies' onChange='getRateTable(virtualPath); return false;'><option value=''>Select a currency.</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/AUD_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='AUD'>Australia Dollar</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/CAD_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='CAD'>Canada Dollar</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/CHF_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='CHF'>Switzerland Franc</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/EUR_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='EUR'>Euro Member Countries Eur</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/GBP_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='GBP'>Great Britain Pound</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/HKD_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='HKD'>Hong Kong Dollar</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/JPY_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='JPY'>Japan Yen</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/MXN_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='MXN'>Mexico Peso</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/NZD_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='NZD'>New Zealand Dollar</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/SEK_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='SEK'>Sweden Kroner</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/SGD_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='SGD'>Singapore Dollar</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/USD_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='USD'>United States Dollar</option><option style='background: white url(http://preview.customhouse.com/ratecalc2/_images/flags/ZAR_small.gif) no-repeat; background-position:2px 0px; padding-left: 20px;' value='ZAR'>South Africa Rand</option></select><input id='btnGetRateTable' type='button' value='Show Rates' onClick='getRateTable(virtualPath); return false;' /></p><div id='rateTablePlaceHolder'></div><p>Rates provided by <a href='http://www.customhouse.com' target='_blank'>Custom House</a> &nbsp;<img style='visibility:hidden;' id='imgRateTableLoading' src='http://preview.customhouse.com/ratecalc2/_images/waiting.gif' /></p>";
var placeholder = document.createElement('div');
placeholder.setAttribute('id', 'chRateTableDiv');
placeholder.innerHTML = rateTableHeaderHtml;
document.getElementById('chRateTableLink').parentNode.appendChild(placeholder);
document.getElementById('chRateTableLink').style.display = 'none';
loadCss('http://preview.customhouse.com/ratecalc2/_scripts/ajaxRateTableScript.css');
