// -------------------------------------------------------
// -------------------------------------------------------
function addEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  }
  else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  }
  else {
    elm['on' + evType] = fn;
  }
}

// -------------------------------------------------------
// -------------------------------------------------------
function removeEvent(elm, evType, fn) {
  if (elm.removeEventListener) {
    elm.removeEventListener(evType, fn, false);
    return true;
  }
  else if (elm.detachEvent) {
    var r = elm.detachEvent('on' + evType, fn);
    return r;
  }
  else {
    elm['on' + evType] = null;
  }
}


function getCommandIframe() {
  return document.getElementById("ifmCommand");
}

// -------------------------------------------------------
// -------------------------------------------------------
function hentData(url, callback) {
  var ifm = getCommandIframe();
  if(ifm) {
    addEvent(ifm, "load", callback);
    ifm.src = url + (url.indexOf("?") > -1 ? "&" : "?") + "ts=" + (new Date()).getTime();
  }
  ifm = null;
}

// -------------------------------------------------------
// -------------------------------------------------------
function hentDataEx(url, params, callback) {
  var ifm = getCommandIframe();
  if(ifm) {
    addEvent(ifm, "load", ServerComm_callbackWrapper);
    ifm.fnCallback = callback;
    ifm.src = url + (url.indexOf("?") > -1 ? "&" : "?") + params + (params && params.length != 0 ? "&" : "") + "ts=" + (new Date()).getTime();
  }
  ifm = null;
}

// -------------------------------------------------------
// -------------------------------------------------------
function ServerComm(iframeName) {
  this.ifmComm = iframeName;
}

ServerComm.prototype.hentDataEx = function(url, params, callback) {
  var ifm = this.getCommandIframe();
  if(ifm) {
    addEvent(ifm, "load", ServerComm_callbackWrapper);
    ifm.fnCallback = callback;
    ifm.src = url + (url.indexOf("?") > -1 ? "&" : "?") + params + (params && params.length != 0 ? "&" : "") + "ts=" + (new Date()).getTime();
  }
  else
    alert("hentDataEx: Iframe kunne ikke findes...");
  ifm = null;
}

ServerComm.prototype.postData = function(url, frm, callback) {
  var ifm = this.getCommandIframe();
  if(ifm) {
    addEvent(ifm, "load", ServerComm_callbackWrapper);
    ifm.fnCallback = callback;
    frm.action = url + (url.indexOf("?") > -1 ? "&" : "?") + "ts=" + (new Date()).getTime();
    frm.method = "post";
    frm.target = this.ifmComm;
    frm.submit();
  }
  else
    alert("postData: Iframe kunne ikke findes...");
  ifm = null;
}

ServerComm.prototype.getCommandIframe = function() {
  return document.getElementById(this.ifmComm);
}

// -------------------------------------------------------
// -------------------------------------------------------
function ServerComm_callbackWrapper(e) {
  var ifm = (e && e.target ? e.target : event.srcElement);
  if(ifm) {
    removeEvent(ifm, "load", ServerComm_callbackWrapper)
    var win = (ifm.contentDocument ? ifm.contentDocument.defaultView : ifm.contentWindow);
    if(win) {
      if(win.data) {
        ifm.fnCallback(win.data);
      }
      else
        alert('Ingen data returneret fra server-kald.\n\nDette kan skyldes en fejl på serveren.\nKontakt evt. DK Golf, så vi kan finde en løsning.');
    }
    else
      alert('Der opstod en fejl under server-kald.\n\nKontakt evt. DK Golf så der kan findes en løsning.');
    win = null;
  }
  ifm = null;
}

