
function sendRequest(url,postData) {
  var req = createXMLHTTPObject();
  if (!req) return;
  var method = (postData) ? "POST" : "GET";
  req.open(method,url,false);
  req.setRequestHeader('User-Agent','XMLHTTP/1.0');
  if (postData)
    req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  if (req.readyState == 4) return;
  req.send(postData);
  return req.responseText;
}

var XMLHttpFactories = [
  function () {return new XMLHttpRequest()},
  function () {return new ActiveXObject("Msxml2.XMLHTTP")},
  function () {return new ActiveXObject("Msxml3.XMLHTTP")},
  function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
  var xmlhttp = false;
  for (var i=0;i<XMLHttpFactories.length;i++) {
    try {
      xmlhttp = XMLHttpFactories[i]();
    }
    catch (e) {
      continue;
    }
    break;
  }
  return xmlhttp;
}


var keys = new Array();
var values = new Array();

function log(msg) {
  // top.document.getElementById("log").innerHTML += '\n'+msg;
}

// uit Moodle
function ScormAddTime (first, second) {
  var sFirst = first.split(":");
  var sSecond = second.split(":");
  var cFirst = sFirst[2].split(".");
  var cSecond = sSecond[2].split(".");
  var change = 0;

  FirstCents = 0;  //Cents
  if (cFirst.length > 1) {
      FirstCents = parseInt(cFirst[1],10);
  }
  SecondCents = 0;
  if (cSecond.length > 1) {
      SecondCents = parseInt(cSecond[1],10);
  }
  var cents = FirstCents + SecondCents;
  change = Math.floor(cents / 100);
  cents = cents - (change * 100);
  if (Math.floor(cents) < 10) {
      cents = "0" + cents.toString();
  }

  var secs = parseInt(cFirst[0],10)+parseInt(cSecond[0],10)+change;  //Seconds
  change = Math.floor(secs / 60);
  secs = secs - (change * 60);
  if (Math.floor(secs) < 10) {
      secs = "0" + secs.toString();
  }

  mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change;   //Minutes
  change = Math.floor(mins / 60);
  mins = mins - (change * 60);
  if (mins < 10) {
      mins = "0" + mins.toString();
  }

  hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change;  //Hours
  if (hours < 10) {
      hours = "0" + hours.toString();
  }

  if (cents != '0') {
      return hours + ":" + mins + ":" + secs + '.' + cents;
  } else {
      return hours + ":" + mins + ":" + secs;
  }
}

var API = {
  _LMSSetValuesCache : new Array(),
  _LMSGetValuesCache : new Array(),

  send : function(req) {
    return sendRequest(api_url,req);
  },
  LMSInitialize : function(x) {
    log('LMSInitialize '+x);
    return 'true';
    return this.send("action=LMSInitialize&param1="+encodeURIComponent(x));
  },
  LMSFinish : function(x) {
    log('LMSFinish '+x);
    this.LMSSetValue('cmi.core.total_time', ScormAddTime(this.LMSGetValue('cmi.core.total_time'), this.LMSGetValue('cmi.core.session_time')));
    this._LMSSaveSetValues();
    var finish = this.send("action=LMSFinish&param1="+encodeURIComponent(x));
    var status = this.LMSGetValue('cmi.core.lesson_status');
    // setTimeout("document.location.href = index_url;",500);
    return finish;
  },
  LMSGetValue : function(name) {
    if (this._LMSSetValuesCache[name]) {
      return this._LMSSetValuesCache[name];
    } else if (this._LMSGetValuesCache[name]) {
      return this._LMSGetValuesCache[name];
    } else {
      var value = this._LMSGetValueReal(name);
      this._LMSGetValuesCache[name] = value;
      return value;
    }
    return value;
  },
  _LMSGetValueReal : function(name) {
    var value = this.send("action=LMSGetValue&param1="+encodeURIComponent(name));
    log('LMSGetValue '+name+' -> '+value);
    return value;
  },
  LMSSetValue : function(name,value) {
    this._LMSSetValuesCache[name] = value;
    return true;
  },
  _LMSSetValueReal : function(name,value) {
    log('LMSSetValue '+name+'='+value);
    return this.send("action=LMSSetValue&param1="+encodeURIComponent(name)+"&param2="+encodeURIComponent(value));
  },
  _LMSSaveSetValues : function() {
    for (var name in this._LMSSetValuesCache) {
      this._LMSSetValueReal(name,this._LMSSetValuesCache[name]);
    }
  },
  LMSCommit : function(x) {
    log('LMSCommit '+x);
    return '';
    return this.send("action=LMSCommit&param1="+encodeURIComponent(x));
  },
  LMSGetLastError : function() {
    log('LMSGetLastError');
    return '0';
    return this.send("action=LMSGetLastError");
  },
  LMSGetErrorString : function(errorCode) {
    log('LMSGetErrorString '+errorCode);
    return '';
    return this.send("action=LMSGetErrorString&param1="+encodeURIComponent(errorCode));
  },
  LMSGetDiagnostic : function(errorCode) {
    log('LMSGetDiagnostic '+errorCode);
    return '';
    return this.send("action=LMSGetDiagnostic&param1="+encodeURIComponent(errorCode));
  }
}


