var xmlHttp;
var ACTION;
var COMMENTID;
var DATA;
var UHASH;
var tag_id;
var SERVER;
function createXMLHttpRequest() 
 {
    if (window.ActiveXObject) 
    {
	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) 
    {
	xmlHttp = new XMLHttpRequest();
    }
}

function srv_call(action,ID,uhash)
{
	ACTION=action;
	COMMENTID=ID;
	xmlHttp	= false;

	if( action=='post' ) {
		document.getElementById('commentarea').disabled=true;
		document.getElementById('postbutton').value='Wait...';
		document.getElementById('postbutton').disabled=true;
		document.getElementById('discardbutton').disabled=true;
	}

	var submitURL   = SERVER;
//	alert(submitURL);
	var parameters  = 'action=' + action + '&parent_comment_id=' + ID + '&uhash=' + uhash + '&data=' + DATA;
//	alert(parameters);

	createXMLHttpRequest();

	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open('POST', submitURL, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
	return false;
}

function VoteComment(action,ID,uhash,direction)
{
	DATA=direction;
	setTimeout("srv_call('vote','"+ID+"','"+uhash+"')",0);
	return;
}

function handleStateChange() {
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			var serverResponse = xmlHttp.responseText;
//			alert(ACTION+'|'+serverResponse);
			if( ACTION=='post' ) {
				if( serverResponse.indexOf('1')==0 )
					document.getElementById('postbutton').value='Posted';
				else if( serverResponse.indexOf('0')==0 ) {
					if( serverResponse.indexOf('too many')>0 )
						document.getElementById('postbutton').value='Too many posts';
					else if( serverResponse.indexOf('Invalid uhash')>0 ) {
						document.getElementById('postbutton').value='Please Log In';
						alert('You have to log in first in order to post a comment.');
					} else
						document.getElementById('postbutton').value='Error posting';
				}

//				if( serverResponse.indexOf('1')!=0 ) {
//					alert(serverResponse);
//				}
				setTimeout("srv_call('load','"+COMMENTID+"','"+UHASH+"')",0);
			}
			if( ACTION=='vote' ) {
				score=parseFloat(document.getElementById('score_'+COMMENTID).innerHTML)+parseFloat(serverResponse);
				document.getElementById('score_'+COMMENTID).innerHTML=score;
				if( score==0 )
					document.getElementById('score_'+COMMENTID).className='scoreNeutral';
				if( score>0 )
					document.getElementById('score_'+COMMENTID).className='scorePositive';
				if( score<0 )
					document.getElementById('score_'+COMMENTID).className='scoreNegative';
				if( serverResponse.indexOf('0')==0 ) {
					ShowShoutbox('shoutbox_'+COMMENTID,'Cant\' vote')
				} else {
					ShowShoutbox('shoutbox_'+COMMENTID,'Thank You')
				}
			}
			if( ACTION=='load' ) {
				document.getElementById('comments').innerHTML = serverResponse;
			}
		} else {
			alert("Error in AJAX "+xmlHttp.status);
		}
	}
}

function ShowComments() {
	var commentsbox	= document.getElementById('commentsbox');
	var postibox	= document.getElementById('postibox');
	if( postibox.style.display == 'block' ) {
		postibox.style.display = "";
	}
	commentsbox.style.display = commentsbox.style.display? "":"block";
}

var Submit;
function PostComment(thisform) {
	var formElements = document.forms['postiboxform'].elements;
	if( Submit.value == 'Discard' ) {
		document.getElementById('postibox').style.display='';
		document.getElementById('commentLen').value=500;
		return false;
	}
	if( formElements['comment'].value.length < 2 || formElements['comment'].value.length > 500 ) {
		alert("Comment allowed size is from 2 to 500 characters.");
		return false;
	}
	
	COMMENTID=formElements['parent_id'].value;
	DATA=formElements['comment'].value;
	UHASH=formElements['uhash'].value;
	setTimeout("srv_call('post','"+formElements['parent_id'].value+"','"+formElements['uhash'].value+"')",0);
	return false;
}

function openPostbox(ID) {
	DATA='';
	document.getElementById('parent_id').value=ID;

	document.getElementById('commentarea').value='';
	document.getElementById('commentLen').value=500;
	document.getElementById('commentarea').disabled=false;
	document.getElementById('postbutton').value='Post Comment';
	document.getElementById('postbutton').disabled=false;
	document.getElementById('discardbutton').disabled=false;

	document.getElementById('postibox').style.display='block';
}

function ShowShoutbox(shoutboxID,message) {
	var shoutbox = document.getElementById(shoutboxID);
	shoutbox.innerHTML = message;
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
		cntfield.value = maxlimit - field.value.length;
}
