// script heavily inspired by Sander Stefann's http://ipv6test.max.nl/
// kudos to Sander!

// Ajax code contributed by the fine folks at APNIC (Geoff Huston)

// This script will make a webbrowser do a limited number of queries to a test platform
// to assess IPv4 vs IPv6 preparedness of the webbrowser and the 
// caching resolver that the webbrowser is using

// comments/questions to: eaben@ripe.net

var __wdm_obj;

function WDM (site_id) {
  this.site_id = site_id;
  this.version='8';
  this.timeoutMsec=5000; // 5 sec
  this.no_check_interval=86400000; // 1 day
  this.timeout=false;
  this.domainSuffix='.wdm.sg.ripe.net';
  // r=rr's returned,t=transport, 4=ipv6, 6=ipv6, d=dual stack
  this.tests = ['rd.td','r4.t6','r6.t4','r4.t4'];
  // randomise
  fisher_yates_shuffle( this.tests );
  this.now = new Date();
  this.test_time=this.now.getTime()+(this.now.getTimezoneOffset()*60000);
  this.test_id=Math.floor(Math.random()*Math.pow(2,31));

  // cookie parsing
  this.cookie_last_run=undefined;

  var a_all_cookies=document.cookie.split(';');
  for(i=0;i<a_all_cookies.length;i++) {
    var a_temp_cookie=a_all_cookies[i].split('=');
    var cookie_name=a_temp_cookie[0].replace(/^\s+|\s+$/g,'');
    if(cookie_name=='wdm_last_run'){ 
      this.cookie_last_run = parseInt(unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g,'')));
    }
  }
  __wdm_obj=this;
}

function fisher_yates_shuffle ( list ) {
  var i = list.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tmp_i = list[i];
     var tmp_j = list[j];
     list[i] = tmp_j;
     list[j] = tmp_i;
   }
}

WDM.prototype.setCookies=function(){
  // set last_run cookie
  var expire=new Date(this.now.getTime()+ this.no_check_interval );
  document.cookie='wdm_last_run='+this.now.getTime()+';expires='+expire.toGMTString()+';path=/';
}

WDM.prototype.run=function(forced){
  if(!forced && this.cookie_last_run ) { return };
  var oldonload=window.onload;
  var me=this;
  window.onload=function(){
    if(typeof oldonload=='function') oldonload();
    me.startTest();
  }
  this.setCookies(); // set regardless of fail or not
}

WDM.prototype.gih_ajax=function(URL){
    var req = null;

    try { req = new XMLHttpRequest(); } catch (e) { req = null; }
    if (!req) try { req = new ActiveXObject('Msxml2.XMLHTTP'); }
        catch (e) { req = null; }
    if (!req) try { req = new ActiveXObject('Microsoft.XMLHTTP'); }
        catch (e) { req = null; }

    try {
        req.open('GET', URL, true);
        req.send(null);
    } catch (e) {
        req = document.createElement('img');
        req.src = URL;
    }
    return req;
}


WDM.prototype.startTest=function(){
  var test_id = '__wdm_'+this.test_id;
  var prefix = 'u' +this.test_time + '.' + 
               's' + this.test_id + '.' + 
               'i' + this.site_id + '.' + 
               'v' + this.version;
  var infix = '/1x1.gif?'+prefix;
  // http://c1234.s1234.i1.v1.r4.t4.wdm.ripe.net?1x1.gif?c1234.s1234.i1.v1.r4.t4
  for(i=0;i<(this.tests.length);i++) {
    this.gih_ajax( "http://"+prefix+'.'+this.tests[i]+this.domainSuffix + infix + '.' + this.tests[i] );
  }
}

var wdm_test = new WDM(170);
wdm_test.run();


