var http_request = false;

function make_request(posturi, posthandle, postsend)
{
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('ERROR; No connection to server. Try again later.');
			return false;
	}
	http_request.onreadystatechange = posthandle;
	http_request.open('POST', posturi, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(postsend);
}

function vVote(id, event){
	make_request('v', vote_handle, 'v='+event+id+'&a=y');
	return false;
}

function vComment(id){
	make_request('c', comment_handle, 'c='+id+'&a=y');
	return false;
}
function vSubmitComment(vId){
	var tName = document.getElementById('textname'+vId).value;
	var tComment = document.getElementById('textcomment'+vId).value;
	if (tName.length < 1)
	{
		alert(unescape('Du%20m%E5ste%20fylla%20i%20ett%20namn.'));
	}
	else if (tComment.length < 1)
	{
		alert(unescape('Du%20m%E5ste%20fylla%20i%20en%20kommentar.'));
	}
	else
	{
		document.getElementById('vComments'+vId).innerHTML = parseFloat(document.getElementById('vComments'+vId).innerHTML) + 1;
		make_request('c', comment_handle, 'c='+vId+'&a=y&textname='+tName+'&textcomment='+tComment+'&save='+vId);
	}
	return false;
}

function vSubmitComplain(id){
	var tComplaint = document.getElementById('complaintext').value;
	var tValidation = document.getElementById('validation').value;
	if (tComplaint.length < 10)
	{
		alert(unescape('Du%20m%E5ste%20klaga!.'));
		document.getElementById('sendbutton').disabled = false;
		document.getElementById('sendbutton').value = 'Skicka';
	}
	else
	{
		make_request('complain', complaint_handle, 'save=ajax&complaintext='+tComplaint+'&validation='+tValidation);
	}
	return false;
}

function complaint_handle() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var HRRT = http_request.responseText;
			var HRstatus = HRRT.split("|");
			if (HRstatus[0] == 'OK') {
				var vId = HRstatus[1];
				var vCom = HRstatus[2];
				document.getElementById('cHeader').innerHTML = 'Ditt klagom&aring;l &auml;r skapat!';
				document.getElementById('cForm').innerHTML = '<div id="c'+vId+'c0c0c0" class="complaint">'+vCom+'</div>';
				goVote();
			}
			else if (HRstatus[0] == "ERROR") {
				alert(unescape(HRstatus[1]));
			}
		} else {
			alert('Error;  no connection to server. Try again.');
		}
	}
}

function comment_handle() {
	var comment = "";
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var HRRT = http_request.responseText;
			var HRstatus = HRRT.split("|");
			if (HRstatus[0] == 'OK') {
				var vId = HRstatus[1];
				for(i=2, x=HRstatus.length; i<x; i++){
					var DataComment = HRstatus[i].split("@");
					comment+=unescape(DataComment[0]) + ' sa: ' + unescape(DataComment[1]) + '<br />';
				}
				if (comment == "") { comment = "Inga kommentarer.<br />"; }
				comment+='<br />Skicka in en kommentar:<br />';
				comment+='<form onsubmit="return vSubmitComment(\'' + vId + '\');" method="post" action="c?c=' + vId + '" id="vSCom' + vId + '">';
				comment+='<input type="hidden" name="save" value="' + vId + '" />';
				comment+='Namn: <input type="text" id="textname' + vId + '" name="textname" class="textbox" style="width:50px" maxlength="20" /> ';
				comment+='Kommentar: <input type="text" id="textcomment' + vId + '" class="textbox" name="textcomment" style="width:250px" maxlength="255" /> ';
				comment+='<input class="button" type="submit" value="Kommentera." /></form>';
				document.getElementById('comments'+ vId).innerHTML = comment;
				document.getElementById('comments'+ vId).style.display = "block";
			}
			else if (HRstatus[0] == "ERROR") {
				alert(unescape(HRstatus[1]));
			}
		} else {
			alert('Error;  no connection to server. Try again.');
		}
	}
}

function vote_handle() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var HRRT = http_request.responseText;
			var HRstatus = HRRT.split("|");
			if (HRstatus[0] == 'OK') {
				var vId = HRstatus[1];
				var vT = HRstatus[2];
				if (vT == 'u') {
					document.getElementById('counter'+vId).innerHTML = parseFloat(document.getElementById('counter'+vId).innerHTML) + 1;
				}
				else {
					document.getElementById('counter'+vId).innerHTML = parseFloat(document.getElementById('counter'+vId).innerHTML) - 1;
				}
				document.getElementById('vUp'+vId).style.display = 'none';
				document.getElementById('vDn'+vId).style.display = 'none';
			}
			else if (HRstatus[0] == "ERROR") {
				alert(unescape(HRstatus[1]));
			}
		} else {
			alert('Error;  no connection to server. Try again.');
		}
	}
}	

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function goVote() {
	var voters = document.getElementsByTagName('DIV');
	for(i=0, x=voters.length; i<x; i++){
		if (voters[i].className == 'complaint'){
			var vInner = voters[i].innerHTML;
			var vDivId = voters[i].id;
			var vSplit = vDivId.split('c');
			var vId = vSplit[1];
			var vVote = vSplit[2];
			var vComment = vSplit[3];
			var vVoted = vSplit[4];
			var NewInner = '<span style="font-size:11px">';
				NewInner+= '<a href="/' + vId + '">#' + vId + '</a> ';
				if (vVoted == 0) {
					NewInner+= '<span id="vUp' + vId +'"><a onclick="return vVote(\'' + vId + '\', \'u\');" title="R&ouml;sta upp!" href="v?v=u' + vId + '"><img alt="R&ouml;sta upp!" src="tu.png" /></a></span> ';
				}
				NewInner+= '(<span id="counter' + vId + '">' + vVote + '</span>) ';
				if (vVoted == 0) {
					NewInner+= '<span id="vDn' + vId + '"><a onclick="return vVote(\'' + vId + '\', \'d\');" title="R&ouml;sta ner!" href="v?v=d' + vId + '"><img alt="R&ouml;sta ner!" src="td.png" /></a></span> ';
				}
				NewInner+= '&bull; <a onclick="return vComment(\'' + vId + '\');" href="c?c=' + vId + '" title="Kommentera."><img src="icmt.png" alt="Kommentera." /></a>';
				NewInner+= ' (<span id="vComments' + vId + '">'+vComment+'</span>)';
				NewInner+= '</span>';
			    NewInner+= '<br />' + vInner + '<br /><br />';
				NewInner+= '<span class="comment" id="comments' + vId + '"></span>';
			document.getElementById(vDivId).innerHTML = NewInner;
		}
	}
}

