/*
 * @name 	captcha
 * @description checks for humans vs computers
 * 			Completely Automated Program to Test Computers and Humans Apart
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
var $ = function (s) { 
	if (document.getElementById) 
		return document.getElementById(s);
	else
		return false;
}

var code; // the captcha code

/*
 * @name 	randomString
 * @description return a string of random uppercase letters
 * @param	len	integer, length of the string returned (>0)
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function randomString(len)
{
	s = '';
	while (len-- > 0)
		s = s+String.fromCharCode(parseInt(Math.random()*26)+65);
	return s;
}

/*
 * @name 	initCaptcha
 * @description This should be called from the body onload or similar
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function initCaptcha()
{
	newcode = randomString(6);
	field = $("code");
	if (field)
		field.innerHTML = newcode;
}

/*
 * @name 	checkCode
 * @description check the captcha code
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function checkCode()
{
	ret = false;
	if (!$('captcha'))
		return;
	if (!$('code'))
		return;
	// get elements
	fieldValue = $("captcha").value;
	codeValue = $("code").innerHTML;
	// check code
	if (fieldValue==codeValue)
		ret = true;
	else
		alert('Type over the code exactly as shown. You may use copy/paste');
	return ret;
}

/*
 * @name 	showForm
 * @description show the form if needed, after preview, e.g. to correct edits
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function showForm(el)
{
	elCommentForm = $("commentForm");
	if (elCommentForm)
		elCommentForm.style.display = "block";
}

/*
 * @name 	showError
 * @description show an error and set the focus to the field
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function showError(field, error)
{
	alert(error);
	field.focus();
	return false;
}

/*
 * @name 	checkFields
 * @description check the fields 
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function checkFields(frm)
{
	ret = true;
	
	if (frm.Comments.value=="")
		return showError(frm.Comments, 'Please enter your comments');
	if (frm.Name.value=="")
		return showError(frm.Name, 'Please enter your name');
/* email address is optional!
	if (frm.Email.value=="")
		return showError(frm.Email, 'Please enter your email address');
*/
	if (frm.Email.value!='' && !frm.Email.value.match(/.*@.*\..*/))
		return showError(frm.Email, 'Please enter a correct email address');

	return ret;
}

/*
 * @name 	preview
 * @description show a preview of the comment before posting
 * 		This hides the form using CSS/JS and puts the
 * 		preview in it's place.
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function preview(frm)
{
	// check mandatory fields
	if (!checkFields(frm))
		return
	// check captcha code
	if (!checkCode())
		return;
	
	// get elements
	elCommentForm = $("commentForm");
	elCommentPreview = $("commentPreview");
	elCommentForm.style.display = "none";
	elButtonPreview = $("btnPreview")
	elButtonClear = $("btnClear")
	// generate preview
	out = "<p>@comment@</p><p>@email@</p>";
	out = out.replace("@comment@", frm.Comments.value);
	out = out.replace("@email@", frm.Email.value);
	// show preview
	elCommentPreview.innerHTML = out;
	// generate submit button
	html = '<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit">';
	btn = $("btnSubmitHTML");
	btn.innerHTML = html;
	// hide preview button
	elButtonPreview.style.display="none";
	elButtonClear.style.display="none";
}

/*
 * @name 	confirmForm
 * @description confirmation of the form, and change confirm to 'submit' button
 * @author 	Michiel van der Blonk
 * @date	Jan 17, 2006
*/
function confirmForm(frm)
{
	// check mandatory fields
	if (!checkFields(frm))
		return
	// check captcha code
	if (!checkCode())
		return;
	
	// get elements
	elButtonConfirm = $("btnConfirm")
	elButtonClear = $("btnClear")
	// generate submit button
	html = '<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit">';
	btn = $("btnSubmitHTML");
	btn.innerHTML = html;
	// hide confirm button
	elButtonConfirm.style.display="none";
	elButtonClear.style.display="none";
}
