//#################################################################################
//## Copyright (C) 2000-02 Michael Anderson, Pierre Gorissen,
//##                       Huw Reddick and Richard Kinser
//##
//## This program is free software; you can redistribute it and/or
//## modify it under the terms of the GNU General Public License
//## as published by the Free Software Foundation; either version 2
//## of the License, or any later version.
//##
//## All copyright notices regarding Snitz Forums 2000
//## must remain intact in the scripts and in the outputted HTML
//## The "powered by" text/logo with a link back to
//## http://forum.snitz.com in the footer of the pages MUST
//## remain visible when the pages are viewed on the internet or intranet.
//##
//## This program is distributed in the hope that it will be useful,
//## but WITHOUT ANY WARRANTY; without even the implied warranty of
//## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//## GNU General Public License for more details.
//##
//## You should have received a copy of the GNU General Public License
//## along with this program; if not, write to the Free Software
//## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//##
//## Support can be obtained from support forums at:
//## http://forum.snitz.com
//##
//## Correspondence and Marketing Questions can be sent to:
//## reinhold@bigfoot.com
//##
//## or
//##
//## Snitz Communications
//## C/O: Michael Anderson
//## PO Box 200
//## Harpswell, ME 04079
//#################################################################################

helpstat = false;
stprompt = false;
basic = true;

function thelp(swtch){
	if (swtch == 1){
		basic = false;
		stprompt = false;
		helpstat = true;
	} else if (swtch == 0) {
		helpstat = false;
		stprompt = false;
		basic = true;
	} else if (swtch == 2) {
		helpstat = false;
		basic = false;
		stprompt = true;
	}
}

//function AddText(NewCode) {
//	document.PostTopic.Message.value+=NewCode
//}

function AddText(NewCode) {
	if (document.PostTopic.Message.createTextRange && document.PostTopic.Message.caretPos) {
		var caretPos = document.PostTopic.Message.caretPos;
		caretPos.text = NewCode;
	} else {
		document.PostTopic.Message.value+=NewCode;
	}
	document.PostTopic.Message.focus();
}

function bold() {
	var text = getText();
	if (helpstat) {
		alert("Gör den markerade texten fet.\n\nExempel:\n[b]Det här är text i fetstil[/b]");
	} else if (basic) {
		AddTxt="[b]" + text + "[/b]";
		AddText(AddTxt);
	} else {
		if (text) {
			txt=prompt("Text att göra FET",text);
		} else {
			txt=prompt("Text att göra FET","Text");
		}
		if (txt!=null) {
			AddTxt="[b]"+txt+"[/b]";
			AddText(AddTxt);
		}
	}
}

function italicize() {
	var text = getText();
	if (helpstat) {
		alert("Gör den markerade texten kursiv.\n\nExempel:\n[i]Det här är kursiv text[/i]");
	} else if (basic) {
		AddTxt="[i]" + text + "[/i]";
		AddText(AddTxt);
	} else {
		if (text) {
			txt=prompt("Text att göra KURSIV",text);
		} else {
			txt=prompt("Text att göra KURSIV","Text");
		}
		if (txt!=null) {
			AddTxt="[i]"+txt+"[/i]";
			AddText(AddTxt);
		}
	}
}

function underline() {
	var text = getText();
  	if (helpstat) {
		alert("Gör den markerade texten understruken.\n\nExempel:\n[u]Den här texten är understruken[/u]");
	} else if (basic) {
		AddTxt="[u]" + text + "[/u]";
		AddText(AddTxt);
	} else {
		if (text) {
			txt=prompt("Text att stryka under",text);
		} else {
			txt=prompt("Text att stryka under","Text");
		}
		if (txt!=null) {
			AddTxt="[u]"+txt+"[/u]";
			AddText(AddTxt);
		}
	}
}

function strike() {
	var text = getText();
	if (helpstat) {
		alert("Gör så den markerade texten har ett streck över sig.\n\nExempel:\n[s]Det här är struken text[/s]");
	} else if (basic) {
		AddTxt="[s]" + text + "[/s]";
		AddText(AddTxt);
	} else {
		if (text) {
			txt=prompt("Text att stryka",text);
		} else {
			txt=prompt("Text att stryka","Text");
		}
		if (txt!=null) {
			AddTxt="[s]"+txt+"[/s]";
			AddText(AddTxt);
		}
	}
}

function hyperlink() {
	var text = getText();
	if (helpstat) {
		alert("Skapar en länk av innesluten text.\n\n\nExempel:\n\n [url]http://www.minadress.com[/url]\n\n [url=http://www.minadress.com]min länktext[/url]");
	} else if (basic) {
		AddTxt="[url]" + text + "[/url]";
		AddText(AddTxt);
	} else { 
		txt2=prompt("Text som ska synas som länk.\nLämna blankt om du vill att adressen ska synas som länk.","");
		if (txt2!=null) {
			txt=prompt("Länkens adress.","http://");
			if (txt!=null) {
				if (txt2=="") {
					AddTxt="[url]"+txt+"[/url]";
					AddText(AddTxt);
				} else {
					AddTxt="[url="+txt+"]"+txt2+"[/url]";
					AddText(AddTxt);
				}
			}
		}
	}
}

function image() {
	var text = getText();
	if (helpstat){
		alert("Infogar en bild i inlägget.\n\nExempel:\n[img]http://www.anywhere.com/image.gif[/img]");
	} else if (basic) {
		AddTxt="[img]" + text + "[/img]";
		AddText(AddTxt);
	} else {
		txt=prompt("Länk till bilden","http://");
		if(txt!=null) {
			AddTxt="[img]"+txt+"[/img]";
			AddText(AddTxt);
		}
	}
}

function spoiler() {
	var text = getText();
	if (helpstat) {
		alert("Gör texten omöjligt att läsa om man inte markerar den.\nBra för spoilers i filmer.\n\nExempel:\n[spoiler]Dold spoiler-text[/spoiler]");
	} else if (basic) {
		AddTxt="[spoiler]" + text + "[/spoiler]";
		AddText(AddTxt);
	} else {
		txt=prompt("Skriv spoiler-text","");
		if (txt!=null) {
			AddTxt="[spoiler]"+txt+"[/spoiler]";
			AddText(AddTxt);
		}
	}
}

function quote() {
	var text = getText();
	if (helpstat){
		alert("Skapar en citering av innesluten text för att hänvisa till text skriven i annat inlägg.\n\nExempel:\n[quote]Detta är en citering.[/quote]");
	} else if (basic) {
		AddTxt=" [quote]" + text + "[/quote]";
		AddText(AddTxt);
	} else {
		if (text) {
			txt=prompt("Text to be quoted",text);
		} else {
			txt=prompt("Text to be quoted","Text");
		}
		if(txt!=null) {
			AddTxt=" [quote] "+txt+" [/quote]";
			AddText(AddTxt);
		}
	}
}

function list() {
	if (helpstat) {
		alert("Skapar en punktlista, numrerad lista eller alfabetisk lista.\n\nExempel:\n[list] [*]punkt 1[/*] [*]punkt2[/*] [*]punkt3[/*] [/list]");
	} else if (basic) {
		AddTxt="\n[list]\n[*][/*]\n[*][/*]\n[*][/*]\n[/list]";
		AddText(AddTxt);
	} else {
		type=prompt("Välj listtyp: \'a\' för alfabetisk, \'1\' för numrerad. Lämna blankt för punktlista.","");
		while ((type!="") && (type!="a") && (type!="a") && (type!="1") && (type!=null)) {
			type=prompt("FEL! De enda möjliga värdena för listtyper är: blankt, 'a' och '1'.","");
		}
		if (type!=null) {
			if (type=="") {
				AddTxt="\n[list]\n";
			} else {
				AddTxt="\n[list="+type+"]\n";
			}
			txt="1";
			while ((txt!="") && (txt!=null)) {
				txt=prompt("Skriv text för aktuell punkt. Lämna blankt för att avsluta.","");
				if (txt!="") {
					AddTxt+="[*]"+txt+"[/*]\n";
				}
			}
			if (type=="") {
				AddTxt+="[/list]";
			} else {
				AddTxt+="[/list="+type+"]";
			}
			AddText(AddTxt);
		}
	}
}

function small() {
	var text = getText();
	if (helpstat){
		alert("Gör den skrivna texten mindre. Bra för signaturer.\n\nExempel:\n[small]Detta är liten text.[/small]");
	} else if (basic) {
		AddTxt="[small]" + text + "[/small]";
		AddText(AddTxt);
	} else {
		if (text) {
			txt=prompt("Text som ska vara liten",text);
		} else {
			txt=prompt("Text som ska vara liten","Text");
		}
		if(txt!=null) {
			AddTxt="[small]"+txt+"[/small]";
			AddText(AddTxt);
		}
	}
}

function storeCaret(ftext) {
	if (ftext.createTextRange) {
		ftext.caretPos = document.selection.createRange().duplicate();
		
	}
	
}

function getText() {
	if (document.PostTopic.Message.createTextRange && document.PostTopic.Message.caretPos) {
		return document.PostTopic.Message.caretPos.text;
	} else {
		return '';
	}
}

//code below found here:  http://www.marzie.com/devtools/javascript/functions.asp

function ltrim(s) {
	return s.replace( /^\s*/, "" );
}
function rtrim(s) {
	return s.replace( /\s*$/, "" );
}
function trim ( s ) {
	return rtrim(ltrim(s));
}

