function CreateNewCaptcha()
{
	url = 'captcha_create.php';
	new Ajax.Request(url,{
			method: 'get',
			onSuccess: CreateNewCaptchaOK,
			onFailure: AjaxError
			});
}

function CreateNewCaptchaOK(request)
{
	var captchaID = request.responseText;
	$('theCaptcha').src = 'captcha.php?id=' + captchaID;
}

function CreateGuestbookEntry()
{
	var thename = encodeURIComponent($('name').value);
	var themail = encodeURIComponent($('email').value);
	var theinet = encodeURIComponent($('www').value);
	var thesubj = encodeURIComponent($('subject').value);
	var themsg  = encodeURIComponent($('msg').value);

	if (thename.length == 0) { alert('Please insert a name'); return; }
	if (themsg.length == 0) { alert('Please insert a message'); return; }

	var pars = '' +
		'username=' + thename +
		'&mail=' + themail +
		'&inet=' + theinet +
		'&subj=' + thesubj +
		'&msg=' + themsg +
		'';

	if ($('captcha'))
	{
		if ($('captcha').value.length = 0)
		{
			alert('Please enter the Captcha');
			$('captcha').focus
			return;
		}
		else
		{
			pars += '&captcha=' + $('captcha').value;
		}
	}

	// Set Ajax Request
	var url = './guestbook_addentry.php';

	new Ajax.Request(url,{
				method: 'post',
				parameters: pars,
				onSuccess: CreateGuestbookEntryOK,
				onFailure: AjaxError
				});
}

function CreateGuestbookEntryOK(request)
{
	var return_code = request.responseText;
	if (return_code == '0') // Wrong Capture
	{
		// Somehow tell the user!
		alert('Wrong capture code!\nPlease try again');
		$('captcha').focus
		//Create new Code
		CreateNewCaptcha();
	}
	else
	{
		//document.location.reload();
		alert('Guestbook entry made!');
		var thename = $('name').value;
		var themail = $('email').value;
		var theinet = $('www').value;
		var thesubj = $('subject').value;
		var themsg  = $('msg').value;

		var now = new Date();

		$('gbform').style.display = 'none';
		$('gb_writtentitle').style.display = '';
		$('gb_writtenhr').style.display = '';

		$('gb_writtendate').innerHTML = now.getDay() + '.' + now.getMonth() + '.' + now.getYear();
		$('gb_writtentime').innerHTML = now.getHours() + ':' + now.getMinutes();
		$('gb_writtenfromname').innerHTML = thename;
		$('gb_writtensubject').innerHTML = thesubj;
		$('gb_writtendescr').innerHTML = themsg;
	}
}
