/*
The contents of this file including all code, commments, etc. within is
Copyright Jim Auldridge 2005.

DISCLAIMER: THIS CODE SUPPLIED 'AS IS', WITH NO WARRANTY EXPRESSED OR IMPLIED.
YOU USE AT YOUR OWN RISK.  JIM AULDRIDGE DOES NOT ACCEPT ANY LIABILITY FOR
ANY LOSS OR DAMAGE RESULTING FROM USE, WHETHER CAUSED BY CODING MISTAKES (MINE
OR YOURS), 'HACKERS' FINDING HOLES, OR ANY OTHER MEANS.
*/
var AJAX = {
	xhr: false,
	isLive: true,
	statusString: 'uninitiated',
	kill: function(errorString){
        AJAX.xhr = false;
		AJAX.isLive = false;
		AJAX.statusString = errorString;
		throw("AJAX object has died:\n\nxhr="+AJAX.xhr+"\nisLive="+AJAX.isLive+"\nstatusString="+AJAX.statusString);
	},
	statusUpdate: function(statusString){
		AJAX.isLive = true;
		AJAX.statusString = statusString;
	},
	xhrInit: function(){
		if (AJAX.isLive){
		    if(window.XMLHttpRequest) {
				try {
					AJAX.xhr = new XMLHttpRequest();
					AJAX.statusUpdate('initiated');
				}
				catch(e) {
					AJAX.kill('Unable to initiate XMLHttpRequest object in Native prong of xhrInit().');
				}
		    }
			else if(window.ActiveXObject) {
		       	try {
					AJAX.xhr = new ActiveXObject("Msxml2.XMLHTTP");
					AJAX.statusUpdate('initiated');
				}
				catch(e) {
					try {
						AJAX.xhr = new ActiveXObject("Microsoft.XMLHTTP");
						AJAX.xhrStatus = 'initiated';
					}
					catch(e) {
						AJAX.kill('Unable to initiate XMLHttpRequest object in ActiveX prong of xhrInit().');
					}
				}
			}
		}
	},
	getDoc: function(urlToDoc,postData,nameOfSuccessMethod,argumentsToSuccessMethod,nameOfFailureMethod) {
		var method;
		if (AJAX.xhr && AJAX.isLive){
			AJAX.xhr.onreadystatechange = function(){
				if (AJAX.xhr.readyState == 4) {
					if (AJAX.xhr.status==200){
						AJAX.statusUpdate('Document retrieved');
						eval(nameOfSuccessMethod+'('+argumentsToSuccessMethod+');');
					}
					else {
						AJAX.statusUpdate('Document retrieval was not OK');
						eval(nameOfFailureMethod+'();');
					}
					AJAX.xhrInit();
				}
			};
			(!postData) ? method = "GET" : method = "POST";
			AJAX.xhr.open(method, urlToDoc, true);
			if (method == "POST"){AJAX.xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');}
			AJAX.xhr.send(postData);
		}
		else if (AJAX.isLive){
			AJAX.xhrInit();
			AJAX.getDoc(urlToDoc,postData,nameOfSuccessMethod,argumentsToSuccessMethod,nameOfFailureMethod);
		}
		else {
			throw("AJAX object is dead, cannot execute method 'getDoc()':\n\nxhr="+AJAX.xhr+"\nisLive="+AJAX.isLive+"\nstatusString="+AJAX.statusString);
		}
	}
};


function getRand(){
	AJAX.getDoc('http://www.volksfolks.org/vwhistory?random',null,'successMethod',"1,2", 'errMethod');
	document.getElementById("vwhistoryheader").innerHTML = "Some Day in VW History";
	return false;
}

function getToday(){
	AJAX.getDoc('http://www.volksfolks.org/vwhistory',null,'successMethod',"1,2", 'errMethod');
	document.getElementById("vwhistoryheader").innerHTML = "Today in VW History";
	return false;
}

function successMethod(){
	document.getElementById("vwhist").innerHTML = (AJAX.xhr.responseText);
}

function errMethod(){
//	alert("Server returned:\n"+AJAX.xhr.status+" "+AJAX.xhr.statusText);
}

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}
function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
