var GetTagurl = "tagGetData.php";
var SendTagurl = "tagSendData.php";
var lastID = -1; //initial value will be replaced by the latest known id
window.onload = initJavaScript;
function initJavaScript() {
	document.forms['tagform'].elements['tagtext'].setAttribute('autocomplete','off');	//no firefox autofill function.. clashes with the script
	document.getElementById('taglist').innerHTML="";
	checkStatus('');
	checkName();
	textCounter();
	receiveTagText();
}
//initiates the first data query
function receiveTagText(){
	if (httpReceiveTag.readyState == 4 || httpReceiveTag.readyState == 0) {
		httpReceiveTag.open("GET",GetTagurl + '?lastID=' + lastID + '&rand='+Math.floor(Math.random() * 1000000), true);
		httpReceiveTag.onreadystatechange = handlehhttpReceiveTag; 
		httpReceiveTag.send(null);
	}
}
//deals with servers' reply to requesting new content
function handlehhttpReceiveTag(){
	if (httpReceiveTag.readyState==4){
		var line=httpReceiveTag.responseText.split("\n");
		for(var i=(line.length-1);i>=0;i--) if(line[i].length>1) insertNewContent(line[i].split("\t"));
		setTimeout('receiveTagText();',5000);//next query in 5s
	}
}
//inserts the new content into the page
function insertNewContent(li){
	lastID=li[0];
	insertO=document.getElementById("taglist");
	oLi=document.createElement('li');
	oSpan=document.createElement('b');
	var d=new Date(1000*li[0]);
	oSpan.setAttribute('title','Gepost op '+d.toLocaleString());
	oSpan.setAttribute('className','name');//for IE's sake
	oSpan.setAttribute('class','name');
	oSpan.style.backgroundImage='url(img/avatar/'+li[1]+'.gif)';
	oSpan.innerHTML=li[2]+':';
	oLi.appendChild(oSpan);
	oLi.innerHTML+=li[3];
	insertO.insertBefore(oLi,insertO.firstChild);
}
//stores a new comment on the server
function sendComment(){
	oText = document.forms['tagform'].elements['tagtext'];
	tagText = oText.value;
	if (tagText != '' & (httpSendTag.readyState == 4 || httpSendTag.readyState == 0)) {
		tagName = document.forms['tagform'].elements['name'].value;
		tagName = tagName.replace(/&/g,"%26");
		tagText = tagText.replace(/&/g,"%26");
		param = 'n=' + tagName + '&c=' + tagText + '&url=';
		httpSendTag.open("POST", SendTagurl, true);
		httpSendTag.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		httpSendTag.onreadystatechange = handlehhttpSendTag;
		httpSendTag.send(param);
		oText.value = '';
		oText.focus();
		textCounter();
	}
	else setTimeout('sendComment();',1000);
}
//deals with servers' reply to sending a comment
function handlehhttpSendTag() {
	//refreshes tagboard after new comment added (makes it more responsive)
	if (httpSendTag.readyState == 4) receiveTagText();
}
function checkStatus(focusState){
	tagText = document.forms['tagform'].elements['tagtext'].value;
	oSubmit = document.forms['tagform'].elements['submit'];
	if (tagText != '' || focusState == 'active') oSubmit.disabled = false;
	else oSubmit.disabled = true;
}
function checkName(){
	tagName = document.forms['tagform'].elements['name'];
	if (tagName.value == '') tagName.value = 'Naam';
}
function smiley(id){
	var el=document.getElementById(id).style;
	if(el.display=="none") el.display="";
	else el.display = "none";
}
function textCounter(){
	maxlimit = 200;
	oSubmit = document.forms['tagform'].elements['submit'];
	oName = document.forms['tagform'].elements['name'];
	oText = document.forms['tagform'].elements['tagtext'];
	if(oText.value.length > maxlimit) oText.value = oText.value.substring(0, maxlimit);
	else document.getElementById('rem').innerHTML = maxlimit - oText.value.length;
	if ((oText.value.length==0)||(oName.value=="Naam")||(oName.value.length==0)) oSubmit.disabled = true;
	else oSubmit.disabled = false;
}
function insert(id,str){
	document.getElementById(id).value += ' '+str;
	textCounter();
	document.getElementById(id).focus();
}
function clearName(){
	oName = document.forms['tagform'].elements['name'];
	if(oName.value=="Naam")oName.value="";
	else if (oName.value.length==0)oName.value="Naam";
	if(oName.value=="Naam"){
		oName.style.textDecoration="blink";
		oName.style.color="red";
	}
	else{
		oName.style.textDecoration="none";
		oName.style.color="black";
	}
}
//initiate XMLHttpRequest object
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

// initiates the two objects for sending and receiving data
var httpReceiveTag = getHTTPObject();
var httpSendTag = getHTTPObject();