/*
	This script controls the comments added via AJAX-technology
	+ some other stuff :)
	(C) 2006 Helmet Software
*/

var myOpacity;
var myloader;
var myWindow = null;

function mylogIn(hash)
{
	user = document.forms['nojs'].user.value;
	pass = hex_md5(document.forms['nojs'].pass.value);
	pass = hex_md5(pass + hash);
	document.forms['js'].user.value = user;
	document.forms['js'].pass.value = pass;
	document.forms['js'].submit();
}

function myFunction(request){
	action = '';
	myOpacity = new fx.Combo('newComment', {duration: 700, opacity: true,height: true, width:false, onComplete: addCommentcomplete});
	myOpacity.hide();
	eval(request.responseText); // The magic :-)
	if (text != 'DO_NOTHING')
	{
		if (action == 'addcomment')
		{
			postcount += text[0];
			obj3 = document.getElementById('commcount');
			obj4 = document.getElementById('commentText');
			commentcount = postcount;
			obj3.innerHTML = commentcount;
			obj4.innerHTML = commentcount == 1 ? 'comment' : 'comments';
			lastid = text[2];
			el = document.getElementById('newComment');
			el.innerHTML = text[1];
			myOpacity.toggle();
		}

		if (action == 'error')
		{
			el = document.getElementById('postError');
			el.innerHTML = text;
			el.style.display = 'block';

		}
	}
}

function resetForm()
{
	if (arguments[0] != 'keepContent')
	{
		document.forms['comment'].comment.value = '';
	}
	else
	{
		setTimeout('myloader.hide();',600);
	}
	document.forms['comment'].submitbutton.disabled = false;
	document.forms['comment'].name.disabled = false;
	document.forms['comment'].comment.disabled = false;
	myloader.hide();
}

function addCommentcomplete() {
	obj = document.getElementById('newComment');
	obj2 = document.getElementById('usercomments');
	obj2.innerHTML += obj.innerHTML;
	obj.innerHTML = '';
	myOpacity.hide();
	if (document.getElementById('firstpost')) {	removeElement('firstpost');	}
	resetForm();
}


function addComment(name, comment, postid, lastid, counter,from){
	myloader.toggle();
	el = document.getElementById('postError');
	el.style.display = 'none';
	document.forms['comment'].submitbutton.disabled =  true;;
	new ajax(base + '/ajaxComment/', {postBody: 'name=' + escape(name) + '&comment=' + escape(comment) + '&lastid=' + lastid + '&postid=' + postid + '&postcount=' + counter,  onComplete: myFunction});
	document.forms['comment'].name.disabled = true;
	document.forms['comment'].comment.disabled = true;
}

function removeElement(element) {
  var d = document.getElementById('right');
  var olddiv = document.getElementById(element);
  d.removeChild(olddiv);
}

window.onload = function()
{
	if (document.getElementById('loader'))
	{
		myloader = new fx.Opacity('loader', {duration: 700});
		myloader.hide();
	}
}

function openWindow(url)
{
	if (myWindow == null || myWindow.closed) 
	{
		myWindow = window.open(url,'myWindow','width=620,height=438,toolbar=0,resizable=0,statusbar=0');
	}
	else
	{
		myWindow.focus();
	}
}
