var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
var is_safari = ((clientPC.indexOf('applewebkit')!=-1) && (clientPC.indexOf('spoofer')==-1));

// Forum edit buttons
var forumEditButtons = [];
function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
	forumEditButtons[forumEditButtons.length] =
		{"imageId": imageId,
		 "imageFile": imageFile,
		 "speedTip": speedTip,
		 "tagOpen": tagOpen,
		 "tagClose": tagClose,
		 "sampleText": sampleText};
}
function InsertEditButton(parent, item) {
	var image = document.createElement("img");
	image.width = 17;
	image.height = 16;
	image.className = "toolbar-editbutton";
	if (item.imageId) image.id = item.imageId;
	image.src = item.imageFile;
	image.border = 0;
	image.alt = item.speedTip;
	image.title = item.speedTip;
	image.style.cursor = "pointer";
	image.onclick = function() {
		insertTags(item.tagOpen, item.tagClose, item.sampleText);
		return false;
	};

	parent.appendChild(image);
	return true;
}
function SetupEditToolbar() {
	var toolbar = document.getElementById('toolbar');
	if (!toolbar) { return false; }
	var textbox = document.getElementById('formattarget');
	if (!textbox) { return false; }
	if (!document.selection && textbox.selectionStart === null) { return false;}
	for (var i = 0; i < forumEditButtons.length; i++) { InsertEditButton(toolbar, forumEditButtons[i]); }
	return true;
}
function escapeQuotes(text) {
	var re = new RegExp("'","g");
	text = text.replace(re,"\\'");
	re = new RegExp("\\n","g");
	text = text.replace(re,"\\n");
	return escapeQuotesHTML(text);
}
function escapeQuotesHTML(text) {
	var re = new RegExp('&',"g");
	text = text.replace(re,"&amp;");
	re = new RegExp('"',"g");
	text = text.replace(re,"&quot;");
	re = new RegExp('<',"g");
	text = text.replace(re,"&lt;");
	re = new RegExp('>',"g");
	text = text.replace(re,"&gt;");
	return text;
}
function insertTags(tagOpen, tagClose, sampleText) {
	var txtarea;
	if (document.editform) { txtarea = document.editform.wpTextbox1;} 
	else {
		var areas = document.getElementsByTagName('textarea');
		txtarea = areas[0];
	}
	if (document.selection  && !is_gecko) {
		var theSelection = document.selection.createRange().text;
		if (!theSelection) { theSelection=sampleText; }
		txtarea.focus();
		if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
		} else {
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
		}
	} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
		var replaced = false;
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		if (endPos-startPos) { replaced = true; }
		var scrollTop = txtarea.scrollTop;
		var myText = (txtarea.value).substring(startPos, endPos);
		if (!myText) { myText=sampleText; }
		var subst;
		if (myText.charAt(myText.length - 1) == " ") { 	subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";}
		else {						subst = tagOpen + myText + tagClose;}
		txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
		txtarea.focus();
		if (replaced) {
			var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
			txtarea.selectionStart = cPos;
			txtarea.selectionEnd = cPos;
		} else {
			txtarea.selectionStart = startPos+tagOpen.length;
			txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
		}
		txtarea.scrollTop = scrollTop;
	}
	if (txtarea.createTextRange) { txtarea.caretPos = document.selection.createRange().duplicate();}
}


function updateOpenCloseForum(tablename, val){
	var oc = readCookie('dix_fopenclose');
	if(oc==null) {	
		createCookie('dix_fopenclose', tablename+'='+val+'-', 365);
		return;
	}
	var current = new Array();
	var i = oc.indexOf(tablename);
	if(i!=-1){
		var e = oc.indexOf('-', i);
		if(e!=-1) {
			createCookie('dix_fopenclose', oc.substring(0,i)+tablename+'='+val + oc.substring(e), 365);
			return;
		}
	}
	createCookie('dix_fopenclose', oc+tablename+'='+val+'-', 365);
}
function collapseForum(tablename){
	var l = tablename + '_icon';
	t = document.getElementById(l);
	t.innerHTML = '<img src="../pics/p.gif" alt="" style="cursor:pointer;" onClick="expand(\''+tablename+'\');" onmouseover="this.src=\'../pics/p'+cscheme+'.gif\'" onmouseout="this.src=\'../pics/p.gif\'"/>';
	var l = tablename + '_title';
	t = document.getElementById(l);
	t.onclick = function(){ expandForum(tablename); }
	t = document.getElementById(tablename);
	for(i=1; i<t.rows.length; i++){
		t.rows[i].style.display="none";
	}
	updateOpenCloseForum(tablename, 'c');
}
function expandForum(tablename){
	t = document.getElementById(tablename);
	for(i=1; i<t.rows.length; i++){
		t.rows[i].style.display="";
	}
	var l = tablename + '_icon';
	t = document.getElementById(l);
	t.innerHTML = '<img src="../pics/m.gif" alt="" style="cursor:pointer;" onClick="collapse(\''+tablename+'\');" onmouseover="this.src=\'../pics/m'+cscheme+'.gif\'" onmouseout="this.src=\'../pics/m.gif\'"/>';
	var l = tablename + '_title';
	t = document.getElementById(l);
	t.onclick = function(){ collapseForum(tablename); }
	t = document.getElementById(tablename);
	for(i=1; i<t.rows.length; i++){
		t.rows[i].style.display="visible";
	}
	updateOpenCloseForum(tablename, 'e');
}
