/****************************************
 * File: AjasxObject.js
 * Purpose: Request for a webpage while keeping the current page active  
 * 
 * How to use:
 * ===========
 * Include this in your page:
      <script type="text/JavaScript"    Language="JavaScript" src="../js/AjaxObject.js"></script>
      <script type="text/JavaScript"    Language="JavaScript">
         // maak een nieuw AjaxObject aan
         //
         MyAjax = new AjaxObject('MyAjax', '../php/getCityState.php', 'MyAjaxHandler');
         function MyAjaxHandler(responsetext, responsearray) {
            alert(responsetext);
         }
      </script>
    
 *
 *  If you want to load the page, use:
     MyAjax.Load('?param1=value1&param2=value2'); // parameters are optional
 *   
 * 
 ****************************************/  

function getHTTPObject() {
   var xmlhttp;
   /*@cc_on
     @if (@_jscript_version >= 5)
     try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
       try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (E) {
          xmlhttp = false;
       }
     }
     @else
        xmlhttp = false;
     @end @*/

   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
      try {
         xmlhttp = new XMLHttpRequest();
      } catch (e) {
         xmlhttp = false;
      }
   }

   return xmlhttp;
}

function AjaxObject(name, url, handler) {
   // Properties
   //
   this.name            = name;
   this.http            = getHTTPObject(); // We create the HTTP Object
   this.url             = null; // The server-side script
   this.responsehandler = null; // The function that handles the response
   this.isProcessing    = false;

   // methods
   //
   this.Load               = aj_Load;
   this.setHandler         = aj_setHandler;
   this.setUrl             = aj_setUrl;
   this.handleHttpResponse = aj_handleHttpResponse;
 
   // initialise
   //
   this.setHandler(handler);
   this.setUrl(url);
}
function aj_Load(paramstring, method) {
   if(typeof method=='undefined') {
      method = 'GET';
   }
   if(method == 'POST') {
     postdata    = paramstring;
     paramstring = '';
   }else {
     postdata    = null;
   }
   if (!this.isProcessing && this.http) {
      if(typeof paramstring == 'undefined') {
         paramstring = '';
      }
      if(paramstring != '') {
         paramstring = '?'+paramstring;
      }
      //alert(this.url + paramstring);      
      this.http.open(method, this.url + paramstring, true);
      this.http.onreadystatechange = new Function(this.name+'.handleHttpResponse()');
      this.isProcessing = true;
      if(method == 'POST') {
         this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      }
      this.http.send(postdata);
   }
}
function aj_setHandler(responsehandler) {
   this.responsehandler = responsehandler;
}
function aj_setUrl(url) {
   this.url = url;
}
function aj_handleHttpResponse() {
  if (this.http.readyState == 4) {
     if (this.http.responseText.indexOf('invalid') == -1) {
        // Split the comma delimited response into an array
        responsearray = this.http.responseText.split(",");
        this.isProcessing = false;
        eval(this.responsehandler+'(this.http.responseText, responsearray);');
     }
  }
}

// Every page gets a hit object, so we can register the hits
//
function setLinkStats() {
   // Call this function in the onload of the body.
   // By specifying a linkname in the target property of the anchor tags
   // a hit with the specified name will be logged in the database for every click
   // the real target can be placed behind the ; 
   // e.g. <a href="home.html" target="homepage;"> 
   // or <a href="home.html" target="homepage;_blank">
   // When you only want a target and no hitcount, make sure no ; is used in the target property
   // If the href contains JavaScript, don't use the target to register a hit but do it like this:
   // <a href="JavaScript:registerHit('LoginButton', 'submit()');document.theForm.submit();">
   //
   anchors = document.body.getElementsByTagName('a');
   for(i=0;i<anchors.length;i++) {
     if(anchors[i].target.indexOf(';') != -1) {
        arr = anchors[i].target.split(';');
        linkname          = arr[0];
        anchors[i].target = arr[1];
        destination       = anchors[i].href;
        eval("anchors[i].onclick = function() {registerHit('"+linkname+"', '"+destination+"') }");
     }
   }
   registerHit('__pageopened', ''); // Het huidige pagina bezoek registreren.
}

// Het blijkt dat met ajax geen data van een ander domein geladen kan worden.
// beveiligingsrisico... Dat is jammer, de hitcounter moet dus in hetzelfde domein...
//
//onderstraande regel aan bij offline testen 
//var hit = new AjaxObject('hit', '/lync/website/www/php/main.php', 'hitRegistered');
//var hit = new AjaxObject('hit', '../php/main.php', 'hitRegistered');
//onderstaande regel aanzetten bij online

//var hit = new AjaxObject('hit', 'http://www.lync.nl/lyncstats/php/main.php', 'hitRegistered');
//var hit = new AjaxObject('hit', 'http://www.testserver.nl/lync/website/www-corporate-lcms/lyncstats/php/main.php', 'hitRegistered');
var hit = new AjaxObject('hit', 'http://www.lync.nl/php/main.php', 'hitRegistered');

function hitRegistered(txt, arr) {
   //alert(txt);
}
function registerHit(linkname, destination) {
   var browser = navigator.appCodeName;
   var version = navigator.appVersion;
   var source  = document.title.split('#')[0]; // somehow the title contains the hash on a refresh...
   var referrer= document.referrer.split('#')[0]; // remove hash;
   var referrer= referrer.split('?')[0]; // remove vars;
   hit.Load('c-Object=hit&c-Action=RegisterHit&link='+linkname+'&source='+source+'&dest='+destination+'&referrer='+referrer);
}
