//Function to check form is filled in correctly before submitting
function CheckGuestBookForm () {
	//Intialise variables
	var errorMsg = "";
	var errorMsgLong = "";

	//Check for a name
	if (document.frmSignGuestBook.name.value == ""){
		errorMsg += "\n\tName \t\t- Enter your Name";
	}
	
	//Check for a country
	if (document.frmSignGuestBook.country.value == "0"){
		errorMsg += "\n\tCountry \t\t- Select the country you are in";
	}
	//Check for comments
	if (document.frmSignGuestBook.comments.value == ""){
		errorMsg += "\n\tComments \t- Enter a comment to add to the Guestbook";
	}
	//Check the description length before submiting the form	
	if (document.frmSignGuestBook.comments.value.length > 500){
		errorMsgLong += "\n- Your comments are " + document.frmSignGuestBook.comments.value.length + " chracters long, they need to be shortned to below 200 chracters.";
	}
	//Check the word length before submitting
	words = document.frmSignGuestBook.comments.value.split(' ');
	for (var loop = 0; loop <= words.length - 1; ++loop){
		if (words[loop].length >= 50){
			errorMsgLong += "\n- A word in your comments contains " + words[loop].length + " characters, this needs to be shortened to below 50 characters.";
		}	
	}
	//Check for HTML tags before submitting the form	
	for (var count = 0; count <= 7; ++count){
		if ((document.frmSignGuestBook.elements[count].value.indexOf("<", 0) >= 0) && (document.frmSignGuestBook.elements[count].value.indexOf(">", 0) >= 0)){
			errorMsgLong += "\n- HTML tags are not permitted, remove all HTML tags.";
		}			
	}
	if ((errorMsg != "") || (errorMsgLong != "")){
		msg = "___________________________________________________________________\n\n";
		msg += "Your Comments have not been added because there are problem(s) with the form.\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "___________________________________________________________________\n\n";
		msg += "The following field(s) need to be corrected: -\n";
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}
	return true;
}

function AddMessageCode(code,promptText, InsertText) {

	if (code != "") {
		insertCode = prompt(promptText + "\n[" + code + "]xxx[/" + code + "]", InsertText);
			if ((insertCode != null) && (insertCode != "")){
				document.frmSignGuestBook.comments.value += "[" + code + "]" + insertCode + "[/"+ code + "] ";
			}
	}		
	document.frmSignGuestBook.comments.focus();
}

//Function to count the number of characters in the description text box
function DescriptionCharCount() {
	document.frmSignGuestBook.countcharacters.value = document.frmSignGuestBook.comments.value.length;	
}

//Function to add the code to the message for the smileys
function AddSmileyIcon(iconCode) {	
		document.frmSignGuestBook.comments.value += iconCode + " ";
		document.frmSignGuestBook.comments.focus();
}

//Function to open pop up window
function openWin(theURL,winName,features) {
  	window.open(theURL,winName,features);
}