window.onload = function() {
  writeDiv('currency', 'loading...');
  writeDiv('sell', '<!-- -->');
  writeDiv('buy', '<!-- -->');
  getQuotes();
}

// -----------------------------------------------------------------------------
function writeDiv(str, data) {
  if (document.getElementById(str)) {
    document.getElementById(str).innerHTML = data;
  }
  else if (document.layers) {
    eval('document.layers.'+ str +'.document.write(data)');
    eval('document.layers.'+ str +'.document.close()');
  }
  else {
    if (document.all) {
      eval(str +'.innerHTML = data');
    }
  }
}

// -----------------------------------------------------------------------------
function getQuotes() {
  httpObj = false;
  if (window.XMLHttpRequest) {
    try {
      httpObj = new XMLHttpRequest();
    } catch (e) {
      httpObj = false;
    }
  } else if (window.ActiveXObject) {
    try {
      httpObj = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
      try {
        httpObj = new ActiveXObject('Microsoft.XMLHTTP');
      } catch (e) {
        httpObj = false;
      }
    }
  }

  if (httpObj) {
    var now = new Date();
    var url = '/ajax/quotes.php?'+ now;
    httpObj.open('GET', url, true);
    httpObj.onreadystatechange = printQuotes;
    httpObj.send('');
  }
}

oldsell = 0;
// -----------------------------------------------------------------------------
function printQuotes() {
  if (httpObj.readyState == 4 && httpObj.status == 200) {
    var instrument = eval('('+ httpObj.responseText +')');
    writeDiv('currency', instrument.name);
    var color;
    if (oldsell == 0) {
      color = 'blue';
    }
    else if (oldsell < instrument.sell) {
      color = 'green';
    }
    else if (oldsell > instrument.sell) {
      color = 'red';
    }
    else if (oldsell == instrument.sell) {
      color = 'blue';
    }
    oldsell = instrument.sell;

    previewsell_str = '<div class="frame '+ color +'"><!-- -->'+
                        '<div class="line"><!-- --></div>'+
                        '<div class="price"><!-- -->'+
                          '<span class="s10 cwhite"><!-- -->'+ instrument.sell.substring(0,4) +'</span>'+
                          '<span class="s12 cwhite">'+ instrument.sell.substring(4,6) +'</span>'+
                        '</div>'+
                      '</div>';
    previewbuy_str  = '<div class="frame '+ color +'"><!-- -->'+
                        '<div class="line"><!-- --></div>'+
                        '<div class="price"><!-- -->'+
                          '<span class="s10 cwhite"><!-- -->'+ instrument.buy.substring(0,4) +'</span>'+
                          '<span class="s12 cwhite">'+ instrument.buy.substring(4,6) +'</span>'+
                        '</div>'+
                      '</div>';
    writeDiv('sell', previewsell_str);
    writeDiv('buy', previewbuy_str);
    timer = 5;
    countdown();
  }
}

// -----------------------------------------------------------------------------
function countdown() {
  if (timer > 0) {
    timer--;
    setTimeout("countdown()",1000);
  }
  else {
    getQuotes();
  }
}