var tmtButtons = new Array();
var tmtLinks = new Array();
var tmtOpenTags = new Array();
var tmtFieldTemp = new Array();
var isIE = false;

function tmtButton(id, display, tagStart, tagEnd, access, open) {
	this.id = id;				// used to name the toolbar button
	this.display = display;		// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.access = access;			// set to -1 if tag does not need to be closed
	this.open = open;			// set to -1 if tag does not need to be closed
}

// button definitions

tmtButtons.push(
	new tmtButton(
		'tmt_bold'
		,'Bold'
		,'<b>'
		,'</b>'
		,'b'
	)
);

tmtButtons.push(
	new tmtButton(
		'tmt_italic'
		,'Italic'
		,'<i>'
		,'</i>'
		,'i'
	)
);

tmtButtons.push(
	new tmtButton(
		'tmt_link'
		,'Link'
		,''
		,'</a>'
		,'a'
	)
); // special case

function tmtLink(display, URL, newWin) {
	this.display = display;
	this.URL = URL;
	if (!newWin) {
		newWin = 0;
	}
	this.newWin = newWin;
}

function tmtShowButton(which, button, i) {
	if (button.access) {
		var accesskey = ' accesskey = "' + button.access + '"'
	}
	else {
		var accesskey = '';
	}
	switch (button.id) {
		case 'tmt_link':
			document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="tmt_button" onclick="tmtInsertLink(\'' + which + '\', ' + i + ');" value="' + button.display + '" />');
			break;
		default:
			document.write('<input type="button" id="' + button.id + '_' + which + '" ' + accesskey + ' class="tmt_button" onclick="tmtInsertTag(\'' + which + '\', ' + i + ');" value="' + button.display + '"  />');
			break;
	}
}

function tmtShowLinks() {
	var tempStr = '<select onchange="tmtQuickLink(this.options[this.selectedIndex].value, this);"><option value="-1" selected>(Quick Links)</option>';
	for (i = 0; i < tmtLinks.length; i++) {
		tempStr += '<option value="' + i + '">' + tmtLinks[i].display + '</option>';
	}
	tempStr += '</select>';
	document.write(tempStr);
}

function tmtAddTag(which, button) {
	if (tmtButtons[button].tagEnd != '') {
		tmtOpenTags[which][tmtOpenTags[which].length] = button;
		document.getElementById(tmtButtons[button].id + '_' + which).value = '/' + document.getElementById(tmtButtons[button].id + '_' + which).value;
	}
}

function tmtRemoveTag(which, button) {
	for (i = 0; i < tmtOpenTags[which].length; i++) {
		if (tmtOpenTags[which][i] == button) {
			tmtOpenTags[which].splice(i, 1);
			document.getElementById(tmtButtons[button].id + '_' + which).value = document.getElementById(tmtButtons[button].id + '_' + which).value.replace('/', '');
		}
	}
}

function tmtCheckOpenTags(which, button) {
	var tag = 0;
	for (i = 0; i < tmtOpenTags[which].length; i++) {
		if (tmtOpenTags[which][i] == button) {
			tag++;
		}
	}
	if (tag > 0) {
		return true; // tag found
	}
	else {
		return false; // tag not found
	}
}	

function tmtCloseAllTags(which) {
	var count = tmtOpenTags[which].length;
	for (o = 0; o < count; o++) {
		tmtInsertTag(which, tmtOpenTags[which][tmtOpenTags[which].length - 1]);
	}
}

function tmtQuickLink(i, thisSelect) {
	if (i > -1) {
		var newWin = '';
		if (tmtLinks[i].newWin == 1) {
			newWin = ' target="_blank"';
		}
		var tempStr = '<a href="' + tmtLinks[i].URL + '"' + newWin + '>' 
		            + tmtLinks[i].display
		            + '</a>';
		thisSelect.selectedIndex = 0;
		tmtInsertContent(tmtCanvas, tempStr);
	}
	else {
		thisSelect.selectedIndex = 0;
	}
}

// code to display toolbar

function tmtShowTools(which) {
	var re = new RegExp('tmt_toolbar_','g');
	var el = document.getElementsByTagName('div');
	for (var i=0; i<el.length; i++) {
		if (el[i].id.match(re)) {
			if (el[i].style.visibility == "visible") {
				el[i].style.visibility = "hidden";
				el[i].style.display = "none";
				break;
			}
		}
	}
	which = "tmt_toolbar_" + which;
	document.getElementById(which).style.visibility = "visible";
	document.getElementById(which).style.display = "block";
}

function tmtToolbar(which) {
	document.write('<div id="tmt_toolbar_' + which + '" style="visibility: hidden; display: none"><span>');
	for (i = 0; i < tmtButtons.length; i++) {
		tmtShowButton(which, tmtButtons[i], i);
	}
	document.write('</span>');
	document.write('</div>');
    tmtOpenTags[which] = new Array();
}

// insertion code

function tmtInsertTag(which, i) {
    if (tmtIsSelection(which)) {
		myField = document.getElementById(which);
		//IE support
		if (document.selection) {
			myField.focus();
			sel = document.selection.createRange();
			if (sel.text.length > 0) {
				sel.text = tmtButtons[i].tagStart + sel.text + tmtButtons[i].tagEnd;
			}
			/*
			else {
				if (!tmtCheckOpenTags(which, i) || tmtButtons[i].tagEnd == '') {
					sel.text = tmtButtons[i].tagStart;
					tmtAddTag(which, i);
				}
				else {
					sel.text = tmtButtons[i].tagEnd;
					tmtRemoveTag(which, i);
				}
			}
			*/
			myField.focus();
		}
		//MOZILLA/NETSCAPE support
		else if (myField.selectionStart || myField.selectionStart == '0') {
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var cursorPos = endPos;
			var scrollTop = myField.scrollTop;
			if (startPos != endPos) {
				myField.value = myField.value.substring(0, startPos)
							  + tmtButtons[i].tagStart
							  + myField.value.substring(startPos, endPos) 
							  + tmtButtons[i].tagEnd
							  + myField.value.substring(endPos, myField.value.length);
				cursorPos += tmtButtons[i].tagStart.length + tmtButtons[i].tagEnd.length;
			}
			/*
			else {
				if (!tmtCheckOpenTags(which, i) || tmtButtons[i].tagEnd == '') {
					myField.value = myField.value.substring(0, startPos) 
								  + tmtButtons[i].tagStart
								  + myField.value.substring(endPos, myField.value.length);
					tmtAddTag(which, i);
					cursorPos = startPos + tmtButtons[i].tagStart.length;
				}
				else {
					myField.value = myField.value.substring(0, startPos) 
								  + tmtButtons[i].tagEnd
								  + myField.value.substring(endPos, myField.value.length);
					tmtRemoveTag(which, i);
					cursorPos = startPos + tmtButtons[i].tagEnd.length;
				}
			}
			*/
			myField.focus();
			myField.selectionStart = cursorPos;
			myField.selectionEnd = cursorPos;
			myField.scrollTop = scrollTop;
		}
		/*
		else {
			if (!tmtCheckOpenTags(which, i) || tmtButtons[i].tagEnd == '') {
				myField.value += tmtButtons[i].tagStart;
				tmtAddTag(which, i);
			}
			else {
				myField.value += tmtButtons[i].tagEnd;
				tmtRemoveTag(which, i);
			}
			myField.focus();
		}
		*/
	} else {
		alert("First highlight the text that you wish to style");
	}
}

function tmtInsertContent(which, myValue) {
    myField = document.getElementById(which);
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}

function tmtInsertLink(which, i, defaultValue) {
    if (tmtIsSelection(which)) {
		//IE support
		if (document.selection) {
   			isIE = true;
   			myField = document.getElementById(which);
			//myField.focus();
			tmtFieldTemp.seltext = document.selection.createRange().text;
			var startSelIndex = myField.value.indexOf(tmtFieldTemp.seltext);
			var endSelIndex = startSelIndex + tmtFieldTemp.seltext.length;
			tmtFieldTemp.pretext = myField.value.substring(0, startSelIndex);
			tmtFieldTemp.posttext = myField.value.substring(endSelIndex);
			//alert(startSelIndex + ', ' + endSelIndex + ', seltext: ' + tmtFieldTemp.seltext + ', pretext: ' + tmtFieldTemp.pretext + ', posttext: ' + tmtFieldTemp.posttext);
			//myField.focus();
		}
		myField = document.getElementById(which);
		if (!defaultValue) {
			defaultValue = 'http://';
		}
		//if (!tmtCheckOpenTags(which, i)) {
			//var URL = prompt('Enter the URL' ,defaultValue);
			show_floating_box(which,250);
			var fb_content = fb_close_link();
			fb_content += '<div align="left">';
			fb_content += '<span class="brownbodynoindent">Enter Link URL:<br /></span>';
			fb_content += '<input type="text" class="brownbodynoindent" name="fb_url" id="fb_url" size="28" value="' + defaultValue + '" />';
			fb_content += ' <input type="button" class="brownbodynoindent" value="insert" onclick="tmtInsertLink2(\'' + which + '\',' + i + ')" />';
			fb_content += '</div>';
			document.getElementById('floating_box').innerHTML = fb_content;
			document.getElementById('fb_url').focus();
			//if (URL) {
			//	tmtButtons[i].tagStart = '<a href="' + URL + '" target="_blank">';
			//	tmtInsertTag(which, i);
			//}
		//} else {
		//	tmtInsertTag(which, i);
		//}
	} else {
		alert("First highlight the text to which the link will be attached");
	}
}

function tmtInsertLink2(which, i) {
	var URL = document.getElementById('fb_url').value;
	if (URL) {
		if (URL.substring(0,7) != "http://") {
			URL = "http://" + URL;
		}
		var linkStart = '<a href="' + URL + '" target="_blank">';
		if ((tmtFieldTemp.seltext != "") && (isIE)) {
			var newFieldText = tmtFieldTemp.pretext + linkStart + tmtFieldTemp.seltext + '</a>' + tmtFieldTemp.posttext;
			document.getElementById(which).value = newFieldText;
			tmtFieldTemp.pretext = tmtFieldTemp.seltext = tmtFieldTemp.posttext = "";
			isIE = false;
		} else {
			tmtButtons[i].tagStart = linkStart;
			tmtInsertTag(which, i);
		}
	}
	close_floating_box();
}

function tmtIsSelection(which) {
	myField = document.getElementById(which);
	
	//IE support
	if (document.selection) {
		if (document.selection.createRange().text.length > 0) {
			return true;
		} else {
			return false;
		}
	//MOZILLA/NETSCAPE support
	} else if (myField.selectionStart || myField.selectionStart == '0') {
		if (myField.selectionStart != myField.selectionEnd) {
			return true;
		} else {
			return false;
		}
	}
}
