
function getTag(element) {

    var elem;

    if( document.getElementById ) // this is the way the standards work
        elem = document.getElementById(element);
    else if( document.all ) // this is the way old msie versions work
        elem = document.all[element];
    else if( document.layers ) // this is the way nn4 works
        elem = document.layers[element];

    return(elem);
}

function navRollOver(tabId, hover, tab) {

    var element = getTag(tabId);
    if (element) {
        if (hover)
            element.className = 'buttonShape buttonHover';
        else {
            if (tabId == tab)
                element.className = 'buttonShape buttonHover';
            else
                element.className = 'buttonShape buttonNormal';
        }
    }
}

function menuClick(obj) {

    var anchors = obj.getElementsByTagName("a");
    window.location = anchors[0];
}

function validateTag(tagId, description) {
    var inputTag = getTag(tagId);
    if(inputTag.value == "")
    {
        alert("Enter " + description + " first.");
        inputTag.focus();
        return false;
    }
    return true;
}

function hasTag(tagName, maxTags) {

    var i, element;

    for (i = 0; i < maxTags; i++) {
        element = getTag('userTagA' + i);
        if (element.innerHTML == tagName) {
            return true;
        }
    }
    return false;
}

function removeTag(tagIndex) {

    var element;

    element = getTag('userTagA' + tagIndex);
    if (element) {
        element.innerHTML = '';
        element.style.display = "none";
    }

    element = getTag('userTag' + tagIndex + 'Value');
    if (element) {
        element.value = '';
    }
}

function removeSearchTags() {

    for (var i = 0; i < 4; i++)
        removeTag(i);
}

function attachTag(tagName, maxTags) {

    var i, element;

    if (!maxTags)
        maxTags = 10

    for (i = 0; i < maxTags; i++) {

        element = getTag('userTagA' + i);

        if (element.innerHTML == '') {

            if (hasTag(tagName, maxTags)) {
                alert("You already have the tag '" + tagName + "'.");
                return;
            }

            element.innerHTML = tagName;
            element.style.display = "block";

            element = getTag('userTag' + i + 'Value');
            element.value = tagName;
            return
        }
    }
    if (maxTags == 1)
        alert("You are only allowed one tag.");
    else
        alert("You are only allowed a maximum of " + maxTags + " tags.");
}

var text = "";

function limitText(tag, limit) {

    if (tag) {
        if (tag.value.length > limit)
            tag.value = text;
        else
            text = tag.value;
    }
}

function insertAtCursor(myField, myValue) {

    // IE support
    if (document.selection) {
        myField.focus();
        var sel = document.selection.createRange();
        sel.text = myValue;
    }
      // MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
                      + myValue
                      + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

function setFocus(tag) {
    var elem = getTag(tag);
    elem.focus();
}

function showSmilies() {
    window.open('/comment/smiliePopup','smilies','width=660,height=530,scrollbars=1');
}

function addImage() {
    window.open('/comment/addImages','smilies','width=504,height=230,scrollbars=1');
}

function iconCallBack(icon) {
    var textArea = getTag("message");
    if (!textArea) {
        textArea = getTag("introMsg");
    }
    insertAtCursor(textArea, ":" + icon + ": ");
    textArea.focus();
}

function showTags(url) {
    window.open(url + 'tag/selectTags','Tags','width=510,height=445,scrollbars=1');
}

function imageCallBack(code) {

    var textArea = getTag("message");
    if (!textArea) {
        textArea = getTag("introMsg");
    }

    if (code == "Image:" || code == "YouTube:") {
        code += "Invalid"
    }

	code = code.replace("YouTube:embed/", "YouTube:v/");
	
	if (code.indexOf("YouTube:v/") == -1) {
		code = code.replace("YouTube:", "YouTube:v/");
	}
		
    textArea.focus();
    insertAtCursor(textArea, "#" + code + "# ");
//    textArea.focus();
}


