/*
 * Function for replacing text inside a text input field.
 * Mainly used for 'blanking' out the field onClick
 *
 */

function inputReplaceValue(element, checkfor, replacewith) {
    if (element.value == checkfor) {
        element.value = replacewith;
    }
}

function toggleVisible(element) {
    var target = document.getElementById(element);

    if (target.style.display == 'none') {
        target.style.display = 'block';
    } else {
        target.style.display = 'none';
    }
}

function toggleImage(element, image1, image2) {
    image1 = "http://" + window.location.hostname + image1;
    image2 = "http://" + window.location.hostname + image2;

    if (element.src == image1) {
        element.src = image2;
    } else {
        element.src = image1;
    }
}

// creates a confirmation Dialog for a checkbox
function msgBox(text, obj)
 {
    var box = window.confirm(text);
    if (box == true) {
        } else if (box == false) {
        obj.checked = false;
    }
}

function insert(aTag, eTag, form, textarea) {
    var input = document.forms[form].elements[textarea];
    input.focus();

    /* for Internet Explorer */
    if (typeof document.selection != 'undefined') {
        /* put in the format code */
        var range = document.selection.createRange();
        var insText = range.text;
        range.text = aTag + insText + eTag;
        /* setting up the cursor position */
        range = document.selection.createRange();
        if (insText.length == 0) {
            range.move('character', -eTag.length);
        } else {
            range.moveStart('character', aTag.length + insText.length + eTag.length);
        }
        range.select();
    }
    /* for Gecko based browser */
    else if (typeof input.selectionStart != 'undefined')
    {
        /* put in the format code */
        var start = input.selectionStart;
        var end = input.selectionEnd;
        var insText = input.value.substring(start, end);
        input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
        /* setting up the cursor position */
        var pos;
        if (insText.length == 0) {
            pos = start + aTag.length;
        } else {
            pos = start + aTag.length + insText.length + eTag.length;
        }
        input.selectionStart = pos;
        input.selectionEnd = pos;
    }
    /* for the rest of the browserworld */
    else
    {
        var pos;
        var re = new RegExp('^[0-9]{0,3}$');
        while (!re.test(pos)) {
            pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
        }
        if (pos > input.value.length) {
            pos = input.value.length;
        }
        var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
        input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
    }
}

function setUpSmilies(form, textarea) {

    var smilies = document.getElementById('smileBox');
    var nodes = smilies.childNodes;
    for (var ii in nodes) {
        el = nodes[ii];
        el.onclick = function() {
            insert(this.title, '', form, textarea);
        };
    }
}

(function ($J) {

  $J(function () {

    $J('.notification a').bind('click', function (event) {
      pageTracker._trackEvent('Notifications', $J(this).text());
    });

  });

})(jQuery);
