/* copyright Paul Bristow 2008 */

/* forum */

function showHideAddMsg(vis) {
	var el = document.getElementById('forumadd');
	var linkel = document.getElementById('hideshowaddmess');
	if (el) {
		if (vis == 0) {
			el.style.display = 'none';
			linkel.innerHTML = '<a href="abc" onclick="return showHideAddMsg(1);">Add Message</a>';
		}
		else {
                        showHideSmiley(0);
			el.style.display = 'block';
			linkel.innerHTML = '<a href="abc" onclick="return showHideAddMsg(0);">Hide Form</a>';
		}
	}
	return false;
}

function showHideSmiley(vis) {
	var el = document.getElementById('forumsmiley');
	var linkel = document.getElementById('hideshowsmiley');
	if (el) {
		if (vis == 0) {
			el.style.display = 'none';
			linkel.innerHTML = '<input type="button" name="smileys" value="Show Smileys" onclick="return showHideSmiley(1);" >';
		}
		else {
			el.style.display = 'block';
			linkel.innerHTML = '<input type="button" name="smileys" value="Hide Smileys" onclick="return showHideSmiley(0);" >';
		}
	}
	return false;
}

function AddSmileyIcon(iconCode) {
      document.getElementById('forumaddform').content.value += iconCode + " ";
}

function boldtxt() {
	var mytextarea = document.getElementById('txmcontent')
	var old = '';
	var selectedtxt = ''
	var boldened = '';
	if (document.selection) {
		//alert('ie');
		old  = document.selection.createRange().text;
		if (old.length > 0) {
			boldened = '[b]' + old + '[/b]';
			document.selection.createRange().text = boldened;
		}
		else {
			alert('select text and then click Bold');
			return false;
		}
	}
	else {
		//alert('ff');
		selectedtxt = mytextarea.value.substring(mytextarea.selectionStart, mytextarea.selectionEnd);
		if (selectedtxt.length > 0) {
			boldened = '[b]' + selectedtxt + '[/b]';
			old = mytextarea.value;
			mytextarea.value = old.substring(0,mytextarea.selectionStart) + boldened + old.substring(mytextarea.selectionEnd, mytextarea.value.length);
		}
		else {
			alert('select text and then click Bold');
			return 0;
		}
	}
	return false;
}

function italictxt() {
	var mytextarea = document.getElementById('txmcontent')
	var old = '';
	var selectedtxt = ''
	var boldened = '';
	if (document.selection) {
		//alert('ie');
		old  = document.selection.createRange().text;
		if (old.length > 0) {
			boldened = '[i]' + old + '[/i]';
			document.selection.createRange().text = boldened;
		}
		else {
			alert('select text and then click Italic');
			return false;
		}
	}
	else {
		//alert('ff');
		selectedtxt = mytextarea.value.substring(mytextarea.selectionStart, mytextarea.selectionEnd);
		if (selectedtxt.length > 0) {
			boldened = '[i]' + selectedtxt + '[/i]';
			old = mytextarea.value;
			mytextarea.value = old.substring(0,mytextarea.selectionStart) + boldened + old.substring(mytextarea.selectionEnd, mytextarea.value.length);
		}
		else {
			alert('select text and then click Italic');
			return 0;
		}
	}
	return false;
}

function fshowonly(category) {

}

function fshowall() {

}

function fprev(idx) {

}

function fnext(idx) {

}

function fCheckForm() {
	return true;
}

function fApprove() {

}

function fSearch() {

}

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

	//Check for a name
	if (document.getElementById('forumaddform').by.value == ""){
		errorMsg += "\n\tName \t\t- Enter your Name";
	}

	if (document.getElementById('forumaddform').by.value.length > 80){
		errorMsg += "\n\tName \t\t- Your Name is too long";
	}

	if (document.getElementById('forumaddform').email.value.length > 80){
		errorMsg += "\n\tEmail \t\t- Your Email is too long";
	}

	if (document.getElementById('forumaddform').email.value.length != 0){
		if (document.getElementById('forumaddform').email.value.indexOf("@", 1) <= 0) {
			errorMsg += "\n\tEmail \t\t- Your Email is invalid";
		}
	}
	
	//Check for msgcontent
	if (document.getElementById('forumaddform').content.value == ""){
		errorMsg += "\n\tMessage \t- Enter a message to add to the Forum";
	}
	
	//Check the msgcontent length before submiting the form	
	if (document.getElementById('forumaddform').content.value.length > 2048){
		errorMsgLong += "\n- Your message is too long";
	}
	
	//Check for HTML tags before submitting the form	
	for (var count = 0; count < document.getElementById('forumaddform').elements.length; ++count){
		if (document.getElementById('forumaddform').elements[count].value.indexOf("<", 0) >= 0) {
			errorMsgLong += "\n- HTML tags are not permitted, remove all HTML tags.";
		}			
	}
	
	//If there is aproblem with the form then display an error
	if ((errorMsg != "") || (errorMsgLong != "")){
		msg = "Your Post has not been added because there are problem(s).\n";
		msg += "Please correct them and try again.\n";
		msg += "___________________________________________________________________\n\n";
		msg += "The following field(s) need to be corrected: -\n";
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}
	
	return true;
}

