var showoutput = 0;
var justsummary = 0;

var theRequest = false;

function goajax(page, recurring)
{
	theRequest = false;

	if(window.XMLHttpRequest)
	{
		theRequest = new XMLHttpRequest();
		if(theRequest.overrideMimeType)
		{
			theRequest.overrideMimeType('text/xml');
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			theRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
				theRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if(!theRequest)
	{
		// client's browser doesn't support AJAX.
		return false;
	}

	// IE will cache this (making it useless) unless you 1) use POST instead of GET,
	// and 2) send a variable instead of null in the send().
	theRequest.onreadystatechange = recurring ? updateVlog : nothing;
	theRequest.open('POST', page, true);
	theRequest.send('ie=junk');
}

function nothing() { }

function updateVlog()
{
	if(theRequest)
	{
		if(theRequest.readyState == 4)
		{
			if(theRequest.status == 200)
			{
				if(document.getElementById("vlogoutput"))
				{
					document.getElementById("vlogoutput").innerHTML = theRequest.responseText;
				}
			}
			scheduleVlogUpdate();
		}
	}
}

function scheduleVlogUpdate()
{
	window.setTimeout("goajax('" + get_target() + "', 1)", 170000);
}

function scheduleSingleUpdate()
{
	window.setTimeout("goajax('" + get_target() + "', 0)", 3000);
}

function get_vid()
{
	var vid = "";
	if(document.getElementById("vlogidnum"))
	{
		vid = document.getElementById("vlogidnum").innerHTML;
	}
	return vid;
}

function get_target()
{
	var target = "/cgi-bin/visitors.cgi?res=" + screen.width + "x" + screen.height + "&amp;showoutput=" + showoutput + "&amp;siteid=" + get_vid() + "&amp;format=xml";
	if(justsummary)
	{
		target = target + "&amp;justsummary";
	}
	return target;
}

function setResCookie()
{
	var userres = screen.width + "x" + screen.height;
	if(!get_cookie("screenres"))
	{
		set_cookie("screenres", userres, 168, "/");
	}
}

function isNum(testval,decimalsOK)
{
	if(typeof(testval) == 'undefined') return false;
	testval = testval.toString();
	if (!testval.length) return false;
	var numbers = decimalsOK ? '.0123456789' : '0123456789';
	for (i=0; i<testval.length; i++)
	{
		if (numbers.indexOf(testval.charAt(i),0) == -1) return false;
	}
	return true;
}

function set_cookie(name, value, hours_to_live, path, domain, secure)
{
	var expireDate = "";
	if(hours_to_live)
	{
		expireDate = (new Date((new Date()).getTime() + hours_to_live*3600000)).toGMTString();
	}

	var curCookie = name + "=" + escape(value) +
	((hours_to_live) ? "; expires=" + expireDate : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");

	document.cookie = curCookie;
}

function get_cookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function schedule_onload_action(newfunc)
{
	var already_scheduled = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = newfunc;
	}
	else
	{
		window.onload = function()
		{
			already_scheduled();
			newfunc();
		}
	}
}


schedule_onload_action(setResCookie);
schedule_onload_action(scheduleSingleUpdate);
schedule_onload_action(scheduleVlogUpdate);
