// Add the code for bold, italic and underline, to the comment section
function AddMessageCode(code,promptText, InsertText) {

	if (code != "") {
		insertCode = prompt(promptText + "\n[" + code + "] text [/" + code + "]", InsertText);
			if ((insertCode != null) && (insertCode != "")){
				document.Guestbook.Comments.value += "[" + code + "]" + insertCode + "[/"+ code + "] ";
			}
	}		
	document.Guestbook.Comments.focus();
}

// Add the code to the comment section for the emoticons
function AddSmileyIcon(iconCode) {	
		document.Guestbook.Comments.value += iconCode + " ";
		document.Guestbook.Comments.focus();
}

// Add the code to comment section for the emoticons
function AddSmileyIcon(iconCode) {	
	window.opener.document.Guestbook.Comments.value += iconCode + " ";
}

// Count the number of characters in the Comments textarea box
function DescriptionCharCount() {
	document.Guestbook.countcharacters.value = document.Guestbook.Comments.value.length;	
}

// Hide all errors for revalidation
function hideAllErrors()
{
	document.getElementById("mainError").style.display = "none"
	document.getElementById("nameError").style.display = "none"
	document.getElementById("findSiteError").style.display = "none"
	document.getElementById("otherFindSiteError1").style.display = "none";
	document.getElementById("otherFindSiteError2").style.display = "none";
	document.getElementById("commentsError").style.display = "none";
	document.getElementById("captchaError").style.display = "none";
}

// Define whitespace characters
var whitespace = " \t\n\r";
function isEmpty(s)
{
   var i;
   if((s == null) || (s.length == 0))
     return true;
   // Search string looking for characters that are not whitespace
    for (i = 0; i < s.length; i++)
    {   
     var c = s.charAt(i);
     if (whitespace.indexOf(c) == -1) 
              return false;
    }

    // All characters are whitespace.
    return true;
}

	// Validates Name, Email, Reason, FindSite, Response and Comments

function validate()
{

  hideAllErrors();
  
  var flag1 = 0;
	
  if (isEmpty(document.getElementById('Name').value))
    {
      flag1 = 1;
	  document.getElementById("nameError").style.display = "inline";
	}
	
	if (document.getElementById('FindSite').selectedIndex == 0)
    {
      flag1 = 1;
	  document.getElementById("findSiteError").style.display = "inline";
    }
	
	if (document.getElementById('FindSite').value == "Other:" && document.getElementById('OtherFindSite').value == "")
    {
      flag1 = 1;
	  document.getElementById("otherFindSiteError1").style.display = "inline";
    }
	
	if (document.getElementById('FindSite').value != "Other:" && document.getElementById('OtherFindSite').value != "")
    {
      flag1 = 1;
	  document.getElementById("otherFindSiteError2").style.display = "inline";
    }
	
	if (isEmpty(document.getElementById('Comments').value))
    {
      flag1 = 1;
	  document.getElementById("commentsError").style.display = "inline";
    }
	
	if (isEmpty(document.getElementById('strCAPTCHA').value))
    {
      flag1 = 1;
	  document.getElementById("captchaError").style.display = "inline";
    }
		
	//Check the word length before submitting
	words = document.getElementById('Comments').value.split(' ');
	for (var loop = 0; loop <= words.length - 1; ++loop){
		if (words[loop].length >= 50)
	{
	  flag1 = 1;
	  BadInfo = BadInfo + "    A word in your comments contains " + words[loop].length + " characters\nThis needs to be shortened to below 50 characters. \n"
		}
	}	
	
	//Check for HTML tags before submitting the form	
	for (var count = 0; count <= 7; ++count){
		if ((document.Guestbook.elements[count].value.indexOf("<", 0) >= 0) && (document.Guestbook.elements[count].value.indexOf(">", 0) >= 0))
	{
	  flag2 = 1;
	  BadInfo = BadInfo + "    HTML tags are not permitted, please remove all HTML tags. \n"
		}
	}
	 
	if (flag1 == 1)
	{
 	  document.getElementById("mainError").style.display = "inline";
	  return false;
	}
  
	if (flag2 == 1)
	{
	  alert(BadInfo);
	  return false;
	}
 
	else
	{
	  // alert("Page was submitted");
 	  return true;
	}

}

