/*
* This JavaScript object creates a connection from the client to
* the database of the server and sends the reply back to the provided
* target.
* Both POST and GET requests can be handled.
*/

function DBconnection(){
  this.detect = navigator.userAgent.toLowerCase();
  var xmlhttp;
  this.url = "process.php";
  
  this.init = function(){
    if (this.detect.indexOf("msie") + 1) {
      try {
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
          xmlhttp=false
        }
      }
    }
    else{
      if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
          xmlhttp = new XMLHttpRequest();
        } catch (e) {
          xmlhttp=false;
        }
      }
    }
  }
  
  /*
  * After the dbconnection object is initialized, parameters can be send
  * to the server script.
  */
  this.send = function(sourceid, post_get, parameters){
    switch(post_get){
      case 'POST':
        xmlhttp.open(post_get,this.url,true);
        xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4) {
            if (xmlhttp.status == 200) {
              /*
              * A reply of the database has been received
              * now return it to the proper page element
              * using the source id.
              */
              //alert(xmlhttp.responseText);
              bericht_ontvangen(xmlhttp.responseText);
            } else {
              alert('Er is een fatale fout opgetreden..');
            }
          }
        }
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", parameters.length);
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(parameters);
      break;
      case 'GET':
        xmlhttp.open(post_get,this.url + parameters,true);
        xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4) {
            if (xmlhttp.status == 200) {
              /*
              * A reply of the database has been received
              * now return it to the proper page element
              * using the source id.
              */
              //alert(xmlhttp.responseText);
              bericht_ontvangen(xmlhttp.responseText);
            } else {
              alert('Er is een fatale fout opgetreden..');
            }
          }
        }
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(null);
      break;
    }
  }
}
