function include(script_filename) {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
}

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

[].indexOf || (Array.prototype.indexOf = function(v){
  for(var i = this.length; i-- && this[i] !== v;);
  return i;
});

include("http://static.forexyard.com/class/modules/ajax/AjaxRequest.js");

//var base_url = "http://www.forexyard.com/quotes/";
//var base_url = "http://quotes.forexyard.com/";
//var base_url = "/quotes/";

//var pairs = new Array('EURUSD','GBPUSD','USDJPY','USDCHF','AUDUSD','EURGBP','EURJPY','USDCAD');
//var pairs = ['EURUSD','GBPUSD','USDJPY','USDCHF','AUDUSD','EURGBP','EURJPY','USDCAD'];
var pairs = ['EURUSD','GBPUSD','USDJPY','USDCHF','AUDUSD','EURGBP','EURJPY','USDCAD'];
var pairs_str = "";
var show_updated = true;
var timer = false;

var refresh = 5;
var tbody;
var updated = "0";

function refreshQuotes(id) {
  return refreshDiv(id, base_url+"xml/quotes-static.xml");
}

function stopQuotes(id) {
//  if (refresh > 0) {
//    refresh = -refresh;
//  }
  if (timer) {
    clearTimeout(timer);
    timer = false;
  }
}

function startQuotes(id) {
//  if (refresh < 0) {
//    refresh = -refresh;
//  }
//  if (refresh > 0) {
    refreshQuotes(id);
//  }
}

function refreshDiv(id, url) {
//  AjaxRequest.get(
//    {
//	'url':url+"?updated="+updated
//	,'onSuccess':function(req){ parseXmlQuotes(req.responseXML); if (refresh>0) {setTimeout("refreshDiv('"+id+"', '"+url+"')", refresh*1000);} }
//	,'onError':function(req){ /* alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText); */ if (refresh) {setTimeout("refreshDiv('"+id+"', '"+url+"')", refresh*5000);} }
//    }
//  );

  stopQuotes(id);
  AjaxRequest.get(
    {
	'url':url
	,'generateUniqueUrl':false 
	,'onSuccess':function(req){ parseXmlQuotes(req.responseXML); /*alert("OK");*/if (refresh>0) {timer = setTimeout("refreshDiv('"+id+"', '"+url+"')", refresh*1000);} }
	,'onError':function(req){ /* alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText); */ if (refresh>0) {timer = setTimeout("refreshDiv('"+id+"', '"+url+"')", refresh*5000);} }
    }
  );
}

function appendTr(id, classN, pair, ask, bid, dir) {
  var tr;
  var td;
  var text;
  tr = document.createElement("tr");
  tr.setAttribute("id", id);
  tr.className = classN;

  td = document.createElement("td");
  text = document.createTextNode(pair);
  td.appendChild(text);
  td.className = "pair";
  tr.appendChild(td);

  td = document.createElement("td");
  text = document.createTextNode(ask);
  td.appendChild(text);
  td.className = "change0";
  tr.appendChild(td);

  td = document.createElement("td");
  text = document.createTextNode(bid);
  td.appendChild(text);
  td.className = "change0";
  tr.appendChild(td);

  td = document.createElement("td");
//  var img = document.createElement("img");
//  img.setAttribute("alt", "");
//  img.setAttribute("src", dir_img[dir]);
//  td.appendChild(img);
  td.className = "arrow0";

  tr.appendChild(td);
  tbody.appendChild(tr);
}

function initQuotes(id, show_pairs, _show_updated) {
  var i, div, a, table, tr, td, text, new_table, new_tbody, new_updated;
  startQuotes();
  if (typeof show_pairs != 'undefined') {
    pairs = new Array();
//    pairs = false;
//    pairs = [];
    for (i = 0; i < show_pairs.length; i++) {
//      pairs[show_pairs[i]] = 1;
//      pairs[i] = show_pairs[i];
      pairs.push(show_pairs[i]);
    }
  }
  for (i = 0; i < pairs.length; i++) {
    pairs_str += (i!=0 ? "," : "") + pairs[i];
  }
  if (typeof _show_updated != 'undefined')
    show_updated = _show_updated;
  div = document.getElementById(id);
  if (!div)
    return false;
  a = div.getElementsByTagName("a");
  if (!a || a.length == 0 || a[0].href.match("http://www.forexyard.com/") == null || a[0].innerHTML != "© <i><b>FOREX</b>YARD</i>") {
    a = document.createElement("a");
    a.href =  aff_url; // "http://www.forexyard.com/index.php?zone_id=345";
    a.target = "_parent";
    a.innerHTML = "&copy; <i><b>FOREX</b>YARD</i><br />";
    div.appendChild(a);
//    alert("You have to add the following code to the div:\n"+'<a href="http://www.forexyard.com/">&copy; <i><b>FOREX</b>YARD</i></a>');
    return;
  }
  table = div.getElementsByTagName("table");
  if (table.length) {
    new_table = false;
    table = table[0];
    tbody = table.getElementsByTagName("tbody");
  }
  else {
    new_table = true;
    table = document.createElement("table");
    table.setAttribute("cellSpacing", "0");
    tbody = document.createElement("tbody");
  }
  if (tbody.length) {
    new_tbody = false;
    tbody = tbody[0];
    tr = tbody.getElementsByTagName("tr");
  }
  else {
    new_tbody = true;
  }
  if (tr.length) {
  }
  else {
    appendTr("quotes_header", "tr0", "pair", "bid", "ask", 0);
    for (i = 0; i < pairs.length; i++) {
      appendTr(pairs[i], "tr"+(i%2 ? "2" : "1"), pairs[i].substring(0,3)+"/"+pairs[i].substring(3,6), "", "", 0);
    }
  }

  if (show_updated) {
    new_updated = true;
/*
    if (!new_tbody) {
      for (i = 0; i < tr.length ; i++) {
        if (tr[i].className == "updated") {
          new_updated = false;
          break;
        }
      }
    }
*/
    if (!new_tbody) {
      tr = document.getElementById("updated");
      if (tr)
        new_updated = false;
    }
    if (new_updated) {
      tr = document.createElement("tr");
      tr.setAttribute("id", "updated");
      tr.className = "updated";
      td = document.createElement("td");
      td.setAttribute("colSpan", "4");
      text = document.createTextNode("(C) FOREXYARD");
      td.appendChild(text);
      tr.appendChild(td);
      tbody.appendChild(tr);
    }
  }
//alert("table:"+new_table+", tbody:"+new_tbody+", tr.length:"+tr.length+", updated:"+new_updated);

  if (new_tbody)
    table.appendChild(tbody);
  if (new_table)
    div.appendChild(table);
  if (refresh > 0)
    setTimeout('refreshQuotes("'+id+'");', refresh);
}

function changeClass(classes, dir) {
  classes = classes.replace(/change\d/,"change"+dir);
  return classes;
}

function parseXmlQuotes(xml) {
  var message0, updated0, quotes, i, quote, pair, tr, old_ask, ask, bid, dir, netchg, date, dateStr;
  message0 = xml.getElementsByTagName("message")[0];
  if (message0)
    alert(message0.firstChild.nodeValue);
  updated0 = xml.getElementsByTagName("updated")[0];
  if (updated0)
    updated = updated0.firstChild.nodeValue;
  quotes = xml.getElementsByTagName("quote");
  for (i = 0; i < quotes.length; i++) {
    quote = quotes[i];
/*
try {
alert(i+". "+quote.getElementsByTagName("pair")+", "+quote.getElementsByTagName("pair")[0]+", "+quote.getElementsByTagName("pair")[0].firstChild+", "+quote.getElementsByTagName("pair")[0].firstChild.nodeValue);
} catch (e) {
var asd;
for (j = 0; j< xml.length; j++) {
  asd += xml[j]+ ", ";
}
alert("Exception: "+i+". "+quote.getElementsByTagName("pair")+", "+quote.getElementsByTagName("pair")[0]+", "+quote.getElementsByTagName("pair")[0].firstChild +", xml:"+ asd);
}
*/
    pair = quote.getElementsByTagName("pair")[0].firstChild.nodeValue;
//    if (!pairs[pair]) {
//alert("pairs:"+pairs+", type:"+typeof pairs);

    if (pairs.indexOf(pair) == -1) {
      continue;
    }

//if (pair == "EURGBP" || pair == "EURJPY") {
//  alert(pair);
//}
    tr = document.getElementById(pair);
    if (!tr) {
      continue;
    }
    old_bid = tr.childNodes[1].firstChild.nodeValue;
    bid = quote.getElementsByTagName("bid")[0].firstChild.nodeValue;
    ask = quote.getElementsByTagName("ask")[0].firstChild.nodeValue;
    netchg = quote.getElementsByTagName("netchg")[0].firstChild.nodeValue;
//    dir = quote.getElementsByTagName("dir")[0].firstChild.nodeValue;
    dir = bid - old_bid;
    dir = (dir > 0 ? '2' : (dir < 0 ? '1' : '0') );
//alert("old_bid:"+old_bid+", bid:"+bid+", dir:"+dir);
    if (old_bid != bid) {
      tr.childNodes[1].firstChild.nodeValue = bid;
      tr.childNodes[2].firstChild.nodeValue = ask;
      tr.childNodes[3].firstChild.nodeValue = netchg;
      tr.childNodes[1].className = "change"+dir;
      tr.childNodes[2].className = "change"+dir;
      tr.childNodes[3].className = changeClass(tr.childNodes[3].className,dir);
//      tr.childNodes[3].className = "arrow"+dir;
    }
    else {
      tr.childNodes[1].className = "change0";
      tr.childNodes[2].className = "change0";
      tr.childNodes[3].className = changeClass(tr.childNodes[3].className,0);
    }
//    if (dir > 0)
//      tr.childNodes[3].firstChild.src = dir_img[dir];
//    row++;
  }
  if (show_updated) {
    tr = document.getElementById("updated");
    if (tr) {
      date = new Date();
      date.setTime(updated /* *1000 */);
      dateStr = "";
      if (date.getHours() < 10)
        dateStr += "0";
      dateStr += date.getHours() + ":";
      if (date.getMinutes() < 10)
        dateStr += "0";
      dateStr += date.getMinutes() + ":";
      if (date.getSeconds() < 10)
        dateStr += "0";
      dateStr += date.getSeconds();
//alert(date+" -------- " + date.getMonth());
//    tr.childNodes[0].firstChild.nodeValue = (date.getFullYear()) + "." + date.getMonth() + "." + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
      tr.childNodes[0].firstChild.nodeValue = (date.getFullYear()) + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + dateStr;
    }
  }
}

