// cross browser form controls, this is a pretty rudimentary system, but it works
// well enough for the few things that we do

// general purpose function for wrapping text in a given textrea.
//   formname, formfield - identifies the textarea to use
//   text1 - text to insert before the user's current selection
//   text2 - text to insert after the user's current selection
//   replace - true/false, if true, remove the user's selected text (overwrite it),
//             else use text1+selection+text2, preserving it
// return value is undefined/inconsequential.
function wrapText(formname, formfield, text1, text2, replace) {
	// get the textarea and focus it
    var textarea = document.forms[formname].elements[formfield];
//    var textarea = document.getElementById(formID).elements[formfield];
	textarea.focus();

	// if this is true, it's IE/Opera style
	if (document.selection && document.selection.createRange) {
		var sel = document.selection.createRange();
		if (replace) {
		    sel.text = text1 + text2;
		} else {
		    sel.text = text1 + sel.text + text2;
		}

	// okay, see if we can use Firefox style
	} else if (textarea.selectionStart != undefined) {
		var selStart = textarea.selectionStart;
		var selEnd = textarea.selectionEnd;

		var begin = textarea.value.substr(0, selStart);
		var selection = textarea.value.substr(selStart, selEnd - selStart);
		var end = textarea.value.substr(selEnd);

		var newCursorPos = selStart;
		var scrollPos = textarea.scrollTop;

		if (replace) selection = '';
		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange) {
			if (selection.length == 0) {
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			} else {
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			}
		    textarea.focus();
		}
		textarea.scrollTop = scrollPos;

	// nothing, let's just append and hope for the best
	} else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

// wrapper that handles prompting the user for something if they don't have a selection
// and applying the right data.
function wrapTextWrapper(formname, formfield, tag, theprompt) {
    var strSelection = "";
    if (! hasSelection(formname, formfield)) {
    	strSelection = window.prompt(theprompt,"");
    }
    wrapText(formname, formfield, '[' + tag + ']' + strSelection, '[/' + tag + ']', 0);
}

// returns true/false based on whether the user has selected text in the
// given textarea.
function hasSelection(formname, formfield) {
    // get the textarea and focus it
    var textarea = document.forms[formname].elements[formfield];
    textarea.focus();

    // if this is true, it's IE/Opera style
    if (document.selection && document.selection.createRange) {
        var sel = document.selection.createRange();
        if (sel.text) return true;

        // okay, see if we can use Firefox style
    } else if (textarea.selectionStart != undefined) {
        var selStart = textarea.selectionStart;
        var selEnd = textarea.selectionEnd;
        if (selStart < selEnd) return true;
    }

    // if we fall through, nothing was selected
    return false;
}

function italicThis(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'i', 'Skriv inn teksten du ønsker i kursiv:');
}

function underlineThis(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'u', 'Skriv inn teksten du ønsker å understreke:');
}

function boldThis(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'b', 'Skriv inn teksten du ønsker fet:');
}

function ahrefThis(formname, formfield) {
    // we do this manually instead of using wrapTextWrapper since this requires two prompts
    // and otherwise special handling
	var strURL = window.prompt("Skriv inn adressen du ønsker å linke til:", "http://");
    var strSelection = "";
    if (! hasSelection(formname, formfield)) {
    	strSelection = window.prompt("Skriv inn teksten du ønsker skal linkes:", "link");
    }
    wrapText(formname, formfield, '[url=' + strURL + ']' + strSelection, '[/url]', 0)
}
	
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

/* Not in use
function quotey(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'quote', 'Enter the text to quote:');
}

function alignjustify(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'justify', 'Enter the text to justify:');
}

function aligncenter(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'center', 'Enter the text to align center:');
}

function alignright(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'right', 'Enter the text to align right:');
}

function alignleft(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'left', 'Enter the text to align left:');
}

function imgThis(formname, formfield) {
    wrapTextWrapper(formname, formfield, 'img', 'Enter the URL to your image:');
}

function list(formname, formfield) {
    wrapText(formname, formfield, '[list][*]', '[/*][/list]');
}

function blist(formname, formfield) {
	frmSelection = prompt("Select what list you would like to use: a, i, or 1", "");
    if (frmSelection != 'a' && frmSelection != 'i' && frmSelection != '1') {
	    alert('List must be one of: a, i, or 1.');
	    return;
	}
	wrapText(formname, formfield, "[list=" + frmSelection + "][*]", "[/*][/list=" + frmSelection + "]");
}

// generic wrapper for inserting a smilie, this overwrites the user's text.
function insertsmilie(smilieface,formname,formfield) {
    wrapText(formname, formfield, '', smilieface, 1);
}

*/