/*
+--------------------------------------------------------------------------
|   PunBB code buttons 1.0.1 
|   ========================================
|   by Maciej Ziolkowski
|   http://pubbmod.wz.cz/forum
|   ========================================
|   Time: Fri, 28 May 2004 13:08:30 GMT+1
|   Email: 
+---------------------------------------------------------------------------
*/

//--------------------------------------------
// Determine browser type and stuff.
//--------------------------------------------
var comp = navigator.userAgent.toLowerCase();  
var vers = parseInt(navigator.appVersion);  
var msie = ((comp.indexOf("msie") != -1) && (comp.indexOf("opera") == -1));  
var nets = ((comp.indexOf('spoofer')==-1) && (comp.indexOf('compatible') == -1) && (comp.indexOf('opera')==-1) && (comp.indexOf('webtv')==-1) && (comp.indexOf('hotjava')==-1));  
var moz = ((comp.indexOf("msie") == -1) && (comp.indexOf('mozilla')!=-1) && (comp.indexOf("opera") == -1)); 
var win  = ((comp.indexOf("win")!=-1) || (comp.indexOf("16bit") != -1));  

//--------------------------------------------
// Set up  values
//--------------------------------------------
var b_prompt = "Please enter the text to be formatted in bold.";
var i_prompt = "Please enter the text to be formatted in italics";
var u_prompt = "Please enter the text to be underlined.";
var font_prompt = "Please enter the text to be formatted with the font tag.";
var size_prompt = "Please enter the text to be formatted with the size tag.";
var color_prompt = "Please enter the text to be formatted with the color tag.";
var align_prompt = "Please enter the text to be aligned.";
var quote_prompt = "Please enter the text to be quoted.";
var code_prompt = "Please enter the code to be treated as code and unformatted text.";

var myTags = new Array();
var buttonOver = "";
var normalmode = false;

//--------------------------------------------
// Set radio button status 
//--------------------------------------------
prep_mode();

function cookieVal(cookieName) {
	thisCookie = document.cookie.split("; ");
	for (i=0; i<thisCookie.length; i++) {
		if (cookieName == thisCookie[i].split("=")[0]) {
			return thisCookie[i].split("=")[1];
		}
	}
	return "";
}

function prep_mode() {
	expireDate = new Date;
	expireDate.setMonth(expireDate.getMonth()+6);

	theMode = cookieVal("cb_mode");
	if ( theMode == "normal" ) {
		document.getElementsByName("mode")[0].checked = true;
		document.getElementsByName("mode")[1].checked = false;
		normalmode = true;
	} else {
		document.getElementsByName("mode")[0].checked = false;
		document.getElementsByName("mode")[1].checked = true;
		normalmode = false;
		theMode = "advanced";
	}
	document.cookie = "cb_mode="+theMode+"; path=/; expires=" + expireDate.toGMTString();
}

function changeMode(newMode) {
	expireDate = new Date;
	expireDate.setMonth(expireDate.getMonth()+6);
	document.cookie = "cb_mode="+newMode+"; path=/; expires=" + expireDate.toGMTString();
	if(newMode == "normal") {
		normalmode = true;
	} else {
		normalmode = false;
	}
}
//--------------------------------------------
// function  Size
//--------------------------------------------
function Size(arrayName) {
	for (i=0;i<arrayName.length;i++) {
		if((arrayName[i] == "undefined") || (arrayName[i] == "") || (arrayName[i] == null)) {
			return i;
		}
	}
	return arrayName.length;
}
//--------------------------------------------
// function  Push
//--------------------------------------------
function Push(arrayName, arrayValue) {
	arraySize = Size(arrayName);
	arrayName[arraySize] = arrayValue;
}
//--------------------------------------------
// function  Pop
//--------------------------------------------
function Pop(arrayName) {
	arrayMo = Size(arrayName);
	arrayValue = arrayName[arrayMo - 1];
	delete arrayName[arrayMo - 1];
	return arrayValue;
}
//--------------------------------------------
// function inArray

//--------------------------------------------
function inArray(item, arrayName) {
	for(i=0;i<arrayName.length;i++) {
		if(arrayName[i] == item) {
			return true;
		}
	}
	return false;
}
//--------------------------------------------
// function pos
//--------------------------------------------
function Pos(item, arrayName) {
	for(i=0;i<arrayName.length;i++) {
		if(arrayName[i] == item) {
			return i;
		}
	}
	return -1;
}
//--------------------------------------------
// function list
//--------------------------------------------
function List(type) {
	if(type) {
		theList = "[list="+type+"]\n";
	} else {
		theList = "[list]\n";
	}
	promptRes = "1";
	while((promptRes != "") && (promptRes != null)) {
		promptRes = prompt("Enter a list item. Click cancel or leave blank to end the list.", "");
		if((promptRes != "") && (promptRes != null)) {
			theList = theList+"[*]"+promptRes+"\n";
		}
	}
	theList = theList+"[/list]\n";
	Pastecode(theList);
}
//--------------------------------------------
// function url
//--------------------------------------------
function Hyperlink() {
	var url = prompt("Please enter the URL of the website.", "http://");
	if(url) {
		var urltitle = prompt("If you wish to, you may also insert a title to be shown instead of the URL.", "");
		if(urltitle) {
			Pastecode("[url="+url+"]"+urltitle+"[/url]");
		} else {
			Pastecode("[url]"+url+"[/url]");
		}
	} else {
// should we display errror or not? if you want remove this :)
		alert("You must enter a URL and a title.");
// ------------------------------------------------------------
	}
}
//--------------------------------------------
// function image
//--------------------------------------------
function Img() {
	var image = prompt("Please enter the remote URL of the image.", "http://");
	if(image) {
		Pastecode("[img]"+image+"[/img]");
	} else {
// should we display errror or not? if you want remove this :)
		alert("You must enter a URL for the image.");
// ------------------------------------------------------------
	}
}
//--------------------------------------------
// function email
//--------------------------------------------
function Email() {
	var email = prompt("Please enter the email address.", "");
	if(email) {
		var emailtitle = prompt("If you wish to, you may also insert a title to be shown instead of the email address.", "My Email Address");
		if(emailtitle) {
			Pastecode("[email="+email+"]"+emailtitle+"[/email]");
		} else {
			Pastecode("[email]"+email+"[/email]");
		}
	} /* added by me */
	else {
// should we display errror or not? if you want remove this :)
		alert("You must enter a email address.");
// ------------------------------------------------------------
	}
}
//--------------------------------------------
// function Paste bbcode
//--------------------------------------------
function Pastebbcode(myCode,selItem) {
	if(myCode == "list") {
		List(selItem);
	} else if(myCode == "url") {
		Hyperlink();
	} else if(myCode == "img") {
		Img();
	} else if(myCode == "email") {
		Email();
	} else {
		if(normalmode) {
			var prompttext = eval(myCode + "_prompt");
			if(selItem) {
				if(selItem != "-") {
					promptres = prompt(prompttext + "\n["+myCode+"="+selItem+"]xxx[/"+myCode+"]", "");
				}
			} else {
				promptres = prompt(prompttext + "\n["+myCode+"]xxx[/"+myCode+"]", "");
			}
			if((promptres != null) && (promptres != "")) {
				if(selItem) {
					if(selItem != "-") {
						Pastecode("["+myCode+"="+selItem+"]"+promptres+"[/"+myCode+"]");
					}
				} else {
					Pastecode("["+myCode+"]"+promptres+"[/"+myCode+"]");
				}
			} 
		} else {
			var thingA = myCode+"_"+selItem;
			args = thingA.split("_");
			lastIndex = 0;
			alreadyopen = false;
			for(i=0;i<myTags.length;i++) {
				theTag = myTags[i];
				if(theTag) {
					args2 = theTag.split("_");
					if(args2[0] == args[0]) {
						alreadyopen = true;
						lastIndex = i;
					}
				}
			}
			if(alreadyopen) {
				while(myTags[lastIndex]) {
					tagRemove = Pop(myTags);
					thingB = tagRemove.split("_");
					Pastecode("[/"+thingB[0]+"]", "", false);
					// unclick it
					element = eval("document.images['"+tagRemove+"']");
					if(element && !element.disabled && element.nodeName == "IMG") {
						if(Over == tagRemove) {							
						}
					}
					if(thingA == tagRemove) { 
						noInsert = true;
					}
				}
			}

			if(selItem && selItem != "-" && !noInsert) {
				isClosed = Pastecode("["+myCode+"="+selItem+"]", "[/"+myCode+"]", true);
				if(!isClosed) {
					Push(myTags, myCode+"_"+selItem);
				} else {					
					element = eval("document.images['"+myCode+"']");
					if(element && !element.disabled && element.nodeName == "IMG") {
						element.className = "toolbar";
					}
				}
			}
			else if(!selItem && !alreadyopen) {
				isClosed = Pastecode("["+myCode+"]", "[/"+myCode+"]", true);
				if(!isClosed) {
					Push(myTags, myCode);
				} else {
					element = eval("document.images['"+myCode+"']");
					if(element && !element.disabled && element.nodeName == "IMG") {
						element.className = "toolbar";
					}
				}
			}
			var alreadyopen = false;
			var noInsert = false;
		}
	}
	setFocus(document.all.body);
}
//--------------------------------------------
// function close all Tags
//--------------------------------------------
function closeAllTags() {
	if(myTags[0]) {
		while(myTags[0]) {
			tagRemove = Pop(myTags);
			args = tagRemove.split("_");
			isClosed = Pastecode("[/"+args[0]+"]", "", false, true);
			element = eval("document.images['"+tagRemove+"']");
			if(element && !element.disabled && element.nodeName == "IMG") {
				element.className = "toolbar_normal";
			}
		}
	}
	myTags = new Array();
	setFocus(document.all.body);
}
//--------------------------------------------
// function Pastecode
//--------------------------------------------
function Pastecode(myCode, myClose, singleTag, ignoreSel) {
	isClosed = true;
	if(ignoreSel != true) {
		ignoreSel = false;
	}
	var messageBox = document.getElementById("body"); ;
	if(msie && win && (vers >= 4)) {
		setFocus(messageBox);
		var seltext = document.selection;
		var range = seltext.createRange();
		if(ignoreSel != false) {
			range.collapse;
		}
		if(((seltext.type == "Text" || seltext.type == "None") && range != null) && ignoreSel != true) {
			if(myClose && range.text.length > 0) {
				myCode = myCode+range.text+myClose;
			} else {
				if(singleTag) {
					isClosed = false;
				}
			}
			range.text = myCode;
		} else {
			messageBox.value += myCode;
		}
	}
	else if(moz) {
		var select_start = messageBox.selectionStart;
		var select_end = messageBox.selectionEnd;
		if(select_end <= 2) {
			select_end = messageBox.textLength;
		}
		var start = (messageBox.value).substring(0, select_start);
		var middle = (messageBox.value).substring(select_start, select_end);
		var end = (messageBox.value).substring(select_end, messageBox.textLength);
		if((messageBox.selectionEnd - messageBox.selectionStart > 0) && ignoreSel != true) {
			middle = myCode+middle+myClose;
		} else {
			if(singleTag) {
				isClosed = false;
			}
			middle = myCode+middle;
		}
		messageBox.value = start+middle+end;
	}
	else {
		messageBox.value += myCode;
		if(singleTag) {
			isClosed = false;
		}
	}
	setFocus(messageBox);
				
	return isClosed;
}
//--------------------------------------------
// function setFocus
//--------------------------------------------
function setFocus(formElement) {
	formElement.focus();
}
//--------------------------------------------
// function addsmilies 
//--------------------------------------------
function smilie(addemot)
{
	Pastecode(" " + addemot + " ", "", false);
}
//--------------------------------------------
// Script End
//--------------------------------------------