function trimString (str) {
  str = typeof this.valueOf() == 'string' ? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function isEmpty(value) {
    if ( value == null || value == "" ) {  return false; }
    return true;
}

function isValidEmail(strEmail) {
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  if (strEmail == null || strEmail.search(validRegExp) == -1) { 
	return false; 
  }
  return true; 
}

function check(form) { 	
	var commentform = document.getElementById('commentform');
	if (commentform == null) return true;

	author = commentform.author.value.trim(); 
	comment = commentform.comment.value.trim();
	strEmail = commentform.email.value;
	antyspam = commentform.dgaspvalue.value;
	
	t_author = isEmpty(author);
	t_comment = isEmpty(comment);
	t_email = isValidEmail(strEmail);
	t_antyspam = isEmpty(antyspam)
	
	if(!t_author || !t_comment || !t_email) {
		var usermessage = document.getElementById('cf_usermessage');
		if (usermessage != null)  { 
			str_error = 'Proszę wypełnić wszystkie wymagane pola. <ol>';
			if(!t_author) str_error = str_error + '<li>Proszę podać imię oraz nazwisko.</li>';
			if(!t_email) str_error = str_error + '<li>Proszę podać prawidłowy adres e-mail.</li>';
			if(!t_comment) str_error = str_error + '<li>Proszę wpisać tekst komentarza.</li>';
			if(!t_antyspam) str_error = str_error + '<li>Proszę wpisać odpowiedź na pytanie kontrolne.</li>';
			str_error = str_error + '</ol>';
			usermessage.innerHTML = str_error;
			usermessage.style.display = 'block'; 
			return false;
		}
	}
	
	return true; 
}

