/*

IPv6 WWW Test

(c) Braintrust, 2008
Written by Nathan Ward <nward@braintrust.co.nz>
http://www.braintrust.co.nz/

Modifications by Jari Korva <jari.korva@iki.fi>

*/

var __ipv6wwwtest_site = "CNL";					// Textual description of your site, must be OK in a URL
var __ipv6wwwtest_timeoutMsec = 60000;					// Timeout for 'final' result message in milliseconds
var __ipv6wwwtest_hostSuffix = ".willab.fi";		// Suffix to go on the IMG hostnames
var __ipv6wwwtest_stopAtTimeout = true;					// Whether to stop when the timeout is reached or not

var __ipv6wwwtest_pageLoadTime;
var __ipv6wwwtest_timeout = false;
var __ipv6wwwtest_ipv4LoadTime = false;
var __ipv6wwwtest_ipv6LoadTime = false;
var __ipv6wwwtest_ipv6and4LoadTime = false;
var __ipv6wwwtest_id = Math.floor(Math.random()*Math.pow(2,31));

function __ipv6wwwtest_startTest() {
	__ipv6wwwtest_pageLoadTime = new Date();

    if(!document.getElementsByTagName || !document.getElementById)
        return;

	var body = document.getElementsByTagName("body");

	if(body == null || body.length != 1 || !body.item(0).appendChild)
		return;

    var div = body.item(0).appendChild(document.createElement("div"));

    div.style.display = "none";

    div.innerHTML =
        "<img height=\"1\" width=\"1\" id=\"__ipv6wwwtest_ipv4Img\" alt=\"\" />" +
        "<img height=\"1\" width=\"1\" id=\"__ipv6wwwtest_ipv6and4Img\" alt=\"\" />" +
        "<img height=\"1\" width=\"1\" id=\"__ipv6wwwtest_ipv6and4Img2\" alt=\"\" />" +
        "<img height=\"1\" width=\"1\" id=\"__ipv6wwwtest_ipv6Img\" alt=\"\" />" +
        "<img height=\"1\" width=\"1\" id=\"__ipv6wwwtest_resultsImg\" alt=\"\" />";

	document.getElementById("__ipv6wwwtest_ipv4Img").src = "http://princess" + __ipv6wwwtest_hostSuffix +"/test/ipv4.gif?site=" + __ipv6wwwtest_site + "&id=" + __ipv6wwwtest_id;
	document.getElementById("__ipv6wwwtest_ipv6Img").src = "http://ipv6only.ipv6" + __ipv6wwwtest_hostSuffix +"/test/ipv6.gif?site=" + __ipv6wwwtest_site + "&id=" + __ipv6wwwtest_id;
	document.getElementById("__ipv6wwwtest_ipv6and4Img").src = "http://ipv6" + __ipv6wwwtest_hostSuffix +"/test/ipv6and4.gif?site=" + __ipv6wwwtest_site + "&id=" + __ipv6wwwtest_id;

	document.getElementById("__ipv6wwwtest_ipv4Img").onload = function() { __ipv6wwwtest_ipv4LoadTime = new Date(); __ipv6wwwtest_checkFinished(); };
	document.getElementById("__ipv6wwwtest_ipv6Img").onload = function() { __ipv6wwwtest_ipv6LoadTime = new Date(); __ipv6wwwtest_checkFinished(); };
	document.getElementById("__ipv6wwwtest_ipv6and4Img").onload = function() { __ipv6wwwtest_ipv6and4LoadTime = new Date(); __ipv6wwwtest_checkFinished(); };
}

function __ipv6wwwtest_sendResults(stage) {
	document.getElementById("__ipv6wwwtest_resultsImg").src = "http://princess" + __ipv6wwwtest_hostSuffix +"/test/results.gif?site=" + __ipv6wwwtest_site + "&id=" + __ipv6wwwtest_id + "&stage=" + stage + "&timeout=" + __ipv6wwwtest_timeoutMsec + "&stop_at_timeout=" + __ipv6wwwtest_stopAtTimeout + "&ipv4=" + __ipv6wwwtest_getLoadTime(__ipv6wwwtest_ipv4LoadTime) + "&ipv6=" + __ipv6wwwtest_getLoadTime(__ipv6wwwtest_ipv6LoadTime) + "&ipv6and4=" + __ipv6wwwtest_getLoadTime(__ipv6wwwtest_ipv6and4LoadTime);
};

function __ipv6wwwtest_getLoadTime(item) {
	if (item == false) {
		return "NaN";
	} else {
		return (item.getTime() - __ipv6wwwtest_pageLoadTime.getTime());
	}
}

function __ipv6wwwtest_checkFinished() {
	if ( (! __ipv6wwwtest_ipv6LoadTime) || (! __ipv6wwwtest_ipv4LoadTime) || (! __ipv6wwwtest_ipv6and4LoadTime) ) {
		if (!__ipv6wwwtest_timeout) {
			__ipv6wwwtest_timeout = window.setTimeout('__ipv6wwwtest_sendFinalResults()',__ipv6wwwtest_timeoutMsec);
		}
		__ipv6wwwtest_sendResults('partial');
	} else {
		__ipv6wwwtest_sendFinalResults();
	}
}

function __ipv6wwwtest_sendFinalResults() {
	if (__ipv6wwwtest_timeout) {
		window.clearTimeout(__ipv6wwwtest_timeout);
	}
	__ipv6wwwtest_sendResults('final');

	if (__ipv6wwwtest_stopAtTimeout) {
		document.getElementById("__ipv6wwwtest_ipv4Img").src = "";
		document.getElementById("__ipv6wwwtest_ipv6Img").src = "";
		document.getElementById("__ipv6wwwtest_ipv6and4Img").src = "";
	}
}

window.onload = __ipv6wwwtest_startTest;
