clientPC = navigator.userAgent.toLowerCase()
is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1))
is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1))

/**
 *
 */
function helpline(id) {
    elem = getElement("helpbox")
    elem.innerHTML = helpTips[id]
}

/**
 *
 */
function bbstyle(id) {
    var textArea = getElement("modifyForm").lyrics

    if (id == 0) { // bold
        insertTags(textArea, "[b]", "[/b]", "text")
    } else if (id == 1) { // italic
        insertTags(textArea, "[i]", "[/i]", "text")
    } else if (id == 2) { // underline
        insertTags(textArea, "[u]", "[/u]", "text")
    } else if (id == 3) { // code
        insertTags(textArea, "[code]", "[/code]", "code")
    } else if (id == 4) { // image_url
        insertTags(textArea, "[img]", "[/img]", "image_url")
    } else if (id == 5) { // url_text
        insertTags(textArea, "[url=]", "[/url]", "url_text")
    }
}

/**
 * apply tagOpen/tagClose to selection in textarea,
 * use sampleText instead of selection if there is none
 * copied and adapted from wikipedia
 */
function insertTags(textArea, tagOpen, tagClose, sampleText, win) {
    if (!win) {
        win = window
    }

    // IE
    if (win.document.selection && !is_gecko) {
        theSelection = win.document.selection.createRange().text
        if (!theSelection) {
            theSelection = sampleText
        }
        textArea.focus();
        if (theSelection.charAt(theSelection.length - 1) == " ") {// exclude ending space char, if any
            theSelection = theSelection.substring(0, theSelection.length - 1)
            win.document.selection.createRange().text = tagOpen + theSelection + tagClose + " "
        } else {
            win.document.selection.createRange().text = tagOpen + theSelection + tagClose
        }

    // Mozilla
    } else if (textArea.selectionStart || textArea.selectionStart == '0') {
        startPos = textArea.selectionStart
        endPos = textArea.selectionEnd
        scrollTop = textArea.scrollTop
        myText = (textArea.value).substring(startPos, endPos)
        if (!myText) {
            myText = sampleText
        }
        lastChar = myText.charAt(myText.length - 1)
        if (lastChar == " " || lastChar == "\n") { // exclude ending space char, if any
            subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + lastChar
        } else {
            subst = tagOpen + myText + tagClose
        }
        textArea.value = textArea.value.substring(0, startPos) + subst + textArea.value.substring(endPos, textArea.value.length)
        textArea.focus()

        cPos = startPos + (tagOpen.length + myText.length + tagClose.length)
        textArea.selectionStart = cPos;
        textArea.selectionEnd = cPos;
        textArea.scrollTop = scrollTop;

    // All others
    } else {
        copy_alertText = alertText
        re1 = new RegExp("\\$1", "g")
        re2 = new RegExp("\\$2", "g")
        copy_alertText = copy_alertText.replace(re1, sampleText)
        copy_alertText = copy_alertText.replace(re2, tagOpen + sampleText + tagClose)
        text = ""
        if (sampleText) {
            text = prompt(copy_alertText)
        }
        if (!text) {
            text = sampleText
        }
        text = tagOpen + text + tagClose
        win.document.infoform.infobox.value = text
        // in Safari this causes scrolling
        if (!is_safari) {
            textArea.focus();
        }
    }
    // reposition cursor if possible
    if (textArea.createTextRange) textArea.caretPos = win.document.selection.createRange().duplicate();
}
