
	var liveFavatarReq = false;
	var t = null;
	var url;

	var name_input;
	var url_input;
	var text_input;
	var country_input;

	if (window.XMLHttpRequest) {
		liveFavatarReq = new XMLHttpRequest();
	}

	function favatarInit() {

		name_input = document.getElementById(name_input_id);
		url_input = document.getElementById(url_input_id);
		text_input = document.getElementById(text_input_id);
		country_input = document.getElementById(country_input_id);
	
		if (url_input) {
			if (url_input.addEventListener) {
				url_input.addEventListener("keypress", getFavatarStart, false);
				name_input.addEventListener("keypress", updateNameStart, false);
				text_input.addEventListener("keypress", updateTextStart, false);
				country_input.addEventListener("keypress", updateCountryStart, false);
			} else if (url_input.attachEvent) {
				url_input.attachEvent("onkeypress", getFavatarStart, false);
				name_input.attachEvent("onkeypress", updateNameStart, false);
				text_input.attachEvent("onkeypress", updateTextStart, false);
				country_input.attachEvent("onkeypress", updateCountryStart, false);
			}
	
			getFavatarStart();
			updateName();

		}

	}

	function getFavatarStart() {
		if (t) {
			window.clearTimeout(t);
		}
		t = window.setTimeout("getFavatar()", 200);
	}

	function updateNameStart() {
		if (t) {
			window.clearTimeout(t);
		}
		t = window.setTimeout("updateName()", 200);
	}

	function updateTextStart() {
		if (t) {
			window.clearTimeout(t);
		}
		t = window.setTimeout("updateText()", 200);
	}

	function updateCountryStart() {
		if (t) {
			window.clearTimeout(t);
		}
		t = window.setTimeout("updateCountry()", 200);
	}

	function setFavatarStart() {
		if (t) {
			window.clearTimeout(t);
		}
		t = window.setTimeout("setFavatar()", 200);
	}

	function getFavatar() {

		url = url_input.value;

		document.getElementById('livepreviewnamelink').setAttribute('href', url);

		if (!isURL(url)) {
			return false;
		}

		if (liveFavatarReq && liveFavatarReq.readyState < 4) {
			liveFavatarReq.abort();
		}

		if (window.XMLHttpRequest) {
			// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			liveFavatarReq = new ActiveXObject("Microsoft.XMLHTTP");
		}

		liveFavatarReq.onreadystatechange = setFavatarStart;
		liveFavatarReq.open("GET", 'http://www.addedbytes.com/livefavatars.php?url=' + escape(url));
		liveFavatarReq.send(null);

	}

	function setFavatar() {
		if (liveFavatarReq.readyState == 4) {
			if (liveFavatarReq.responseText) {
				/* Ok, we've got a favatar. Replace the default. */
				document.getElementById('favatar_wrapper').innerHTML = liveFavatarReq.responseText;
			}
		}
	}

	function isURL(argvalue) {

		if (argvalue.indexOf(" ") != -1)
			return false;
		else if (argvalue.indexOf("http://") == -1)
			return false;
		else if (argvalue == "http://")
			return false;
		else if (argvalue.indexOf("http://") > 0)
			return false;

		argvalue = argvalue.substring(7, argvalue.length);
		if (argvalue.indexOf(".") == -1)
			return false;
		else if (argvalue.indexOf(".") == 0)
			return false;
		else if (argvalue.charAt(argvalue.length - 1) == ".")
			return false;

		if (argvalue.indexOf("/") != -1) {
			argvalue = argvalue.substring(0, argvalue.indexOf("/"));
			if (argvalue.charAt(argvalue.length - 1) == ".")
				return false;
		}

		if (argvalue.indexOf(":") != -1) {
			if (argvalue.indexOf(":") == (argvalue.length - 1))
				return false;
			else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
				return false;
			argvalue = argvalue.substring(0, argvalue.indexOf(":"));
			if (argvalue.charAt(argvalue.length - 1) == ".")
				return false;
		}

		return true;

	}

	function updateName() {
		if (name_input.value != '') {
			document.getElementById('livepreviewnamelink').innerHTML = name_input.value;
		} else {
			document.getElementById('livepreviewnamelink').innerHTML = 'Anonymous';
		}
	}

	function updateText() {
		var commenttext = text_input.value.replace(/^\s*|\s*$/g,"");
		commenttext = commenttext.replace(/&/g,"&amp;");
		commenttext = commenttext.replace(/</g,"&lt;");
		commenttext = commenttext.replace(/>/g,"&gt;");
		commenttext = commenttext.replace(/\n/g,"<br>");

		document.getElementById('commenttext_preview').innerHTML = commenttext;
	}

	function updateCountry() {
		var commentcountry = country_input.value.replace(/^\s*|\s*$/g,"");
		commentcountry = commentcountry.replace(/&/g,"&amp;");
		commentcountry = commentcountry.replace(/</g,"&lt;");
		commentcountry = commentcountry.replace(/>/g,"&gt;");
		commentcountry = commentcountry.replace(/\n/g,"<br>");

		document.getElementById('countrytext_preview').innerHTML = commentcountry;
	}

	if (window.addEventListener) {
		window.addEventListener("load", favatarInit, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", favatarInit)
	} else if (document.getElementById) {
		window.onload = favatarInit;
	}
