var is_hidden = false;

insertHTML = function(html, val) {
	var hilited = null;
	var insert_val = true;
		
	switch (html) {
		case 'b':
		case 'i':
		case 'u':
			if (el('visualeditor').selectionStart) {
				if (el('visualeditor').selectionStart != el('visualeditor').selectionEnd) {
			 		el('visualeditor').value = el('visualeditor').value.substring(0,el('visualeditor').selectionStart) + '['+html+']' + el('visualeditor').value.substring(el('visualeditor').selectionStart,el('visualeditor').selectionEnd) + '[/'+html+'] ' + el('visualeditor').value.substring(el('visualeditor').selectionEnd,el('visualeditor').value.length);
					insert_val = false;
				}
				else 
					return;
			}
			else if (document.selection.createRange) {
				hilited = document.selection.createRange().text;
	    		if (!hilited) return;
			    document.selection.createRange().text = '[' + html + ']' + hilited + '[/' + html + '] ';
				insert_val = false;
			}
			else {
				var val = prompt("Enter text to format:", "");
				if (val == "" || val == null )
					return;
				else
					val = "[" + html + "]" + val + "[/" + html + "]";
			}

			break;

		case 'hr':
			val = "[" + html + "]";
			break;

		case 'link':
		case 'email':
			var start = (html == 'link') ? '[link' : '[link:mailto:';
			insert_val = false;
			if (_isMozilla_) {
				if (el('visualeditor').selectionStart != el('visualeditor').selectionEnd) {
					hilited = el('visualeditor').value.substring(el('visualeditor').selectionStart,el('visualeditor').selectionEnd);
					var hilited_url = hilited;
					if (html == 'link') {
						if (hilited.substring(0,7) != 'http://') hilited_url = 'http://' + hilited;
					}
					
			 		el('visualeditor').value = el('visualeditor').value.substring(0,el('visualeditor').selectionStart) + start + hilited_url + ']' + hilited + '[/link] ' + el('visualeditor').value.substring(el('visualeditor').selectionEnd,el('visualeditor').value.length)
				}
				else {
					(html == 'link') ? doDialog('/editor/dialog_link.html', 'dlnk', 450, 150) : doDialog('/editor/dialog_email.html', 'demail', 450, 110);
				}
			}
			else if (_isIE_) {
				hilited = document.selection.createRange().text;
		
		   		if (!hilited) 
					(html == 'link') ? doDialog('/editor/dialog_link.html', 'dlnk', 450, 150) : doDialog('/editor/dialog_email.html', 'demail', 450, 110);
				else {
					var hilited_url = hilited;
					if (html == 'link') {
						if (hilited.substring(0,7) != 'http://') hilited_url = 'http://' + hilited;
					}
					document.selection.createRange().text = start + hilited_url + ']' + hilited + '[/link] ';
				}
			}
			else
				(html = 'link') ? doDialog('/editor/dialog_link.html', 'dlnk', 450, 150) : doDialog('/editor/dialog_email.html', 'demail', 450, 110);
			break;
			
	}
	
	//only insert if a selection was not acted upon
	if (insert_val && val != "") {
		if (el('visualeditor').value != "") 
			el('visualeditor').value += " " + val;
		else
			el('visualeditor').value = val;

		setTimeout("el('visualeditor').focus()", 200);
	}
};

doDialog = function(url, nme, w, h) {
	var wref = null;
	t = l = 150;
	if ( (wref = openWin(url, nme, 'no', 'no', 'no', 'no', 'no', 'no', 'no', w, h, t, l, 0,0,1)) )
		wref.focus();
};

doPreview = function() {
	if (el('visualeditor').value != "") {
		el('editor_form').action = '/editor/previewer.php';
		el('editor_form').target = '_prevWin';
		openWin('/editor/previewer.html', '_prevWin', 'no', 'no', 'no', 'no', 'yes', 'no', 'no', 750, 420, 50, 50, 0,0,0);
		setTimeout("doPreviewExec()", 100);
	}
};

doPreviewExec = function() {
	el('editor_form').submit();
	setTimeout("el('editor_form').target = '_self'", 100);
}

doSpellCheck = function() {
	if (el('visualeditor').value != "") {
		el('editor_form').action = '/editor/spell_checker.php';
		el('editor_form').target = '_spellCheck';
		openWin('/editor/previewer.html', '_spellCheck', 'no', 'no', 'no', 'no', 'yes', 'yes', 'yes', 500, 280, 50, 350, 0,0,0);
		setTimeout("el('editor_form').submit()", 100);
	}
};

function doSmilie(val) {
	insertHTML("smilie", val);
};

toggleSmilies = function() {
	if (el('smilies_box')) {
		if (el('smilies_box').style.visibility == 'hidden') {
			el('smilies_box').style.visibility = 'visible';
			el('smilies_toggler').src = '/images/collapse.jpg';
			setCookie('bpwSmilies', 1, expiration, '/');
			if (el('smilies_box').innerHTML == '') {
				el('smilies_box').innerHTML = '<img src="/images/spinner.gif" alt="" border="0" /> Fetching...';
				smiley = new Ajax();
				if(smiley && !smiley.in_request) {
					smiley.send('/members/get_smilies.php', 'post', 'handleSmilieResponse', '');
				}
			}
		}
		else {
			el('smilies_box').style.visibility = 'hidden';
			el('smilies_toggler').src = '/images/expand.jpg';
			delCookie('bpwSmilies', '/');
		}
	}
};

handleSmilieResponse = function() {
 	if (smiley.request.readyState == 4) {
        if (smiley.request.status == 200) {	        
	        var response = smiley.request.responseText;
	        if (response) {
	        	smiley.in_request = false;
	        	el('smilies_box').innerHTML = response;
				smiley.removeCallBack();
				smiley = null;
	        }
		}
		else {
			alert("Error occurred. Try again later.");
			smiley.in_request = false;
		}
    }    
};

verifyURL = function(val) {
	if (el('linkurl').value == "") {
		el('linkurl').focus();
		alert("You did not enter a URL to verify.");
		return;
	}

	var pwin = openWin("http://"+el('linkurl').value,"","yes","yes","yes","yes","yes","yes","yes",600,400,10,10,0,0,1); 
	if (pwin)
		pwin.focus();
};

checkProtocol = function(do_pop) {
	var obj = el('linkurl');
	if (obj.value != "") {
		if (obj.value.substring(0,7).toLowerCase() == "http://") {
			obj.value = obj.value.substring(7, obj.value.length);
		}
	}
	if (do_pop != undefined)
		popLinkName();
};

hideEditor = function() {
	if (el('editor_box')) {
		el('editor_box').style.visibility = 'hidden';
		el('editor_box').style.display = 'none';	
		is_hidden = true;
	}
};

toggleEditorButtons = function(state) {
	if (el('editor_spellChkBtn')) el('editor_spellChkBtn').disabled = state;
	if (el('editor_postBtn')) el('editor_postBtn').disabled = state;
	if (el('editor_previewBtn')) el('editor_previewBtn').disabled = state;
	if (el('editor_cancelBtn')) el('editor_cancelBtn').disabled = state;
};
