﻿function FillTag(tagContainer, tagBoxName)
{   
     var tagBox = document.getElementById(tagBoxName);
     var tag = tagContainer.innerHTML;
     
     // if it finds the tag
     if (tagBox.value.indexOf(tag) != -1)
     {
        tagBox.value = tagBox.value.replace(tag, '');
        tagContainer.className = "";
     }
     else 
     {
        if (tagBox.value.length > 0)
        {
            tagBox.value += ' ' + tag;
        }
        else
        {
            tagBox.value = tag;
        }
        
        tagContainer.className = "selected";
    }
    
    Tidy(tagBox)
}

function Tidy(textBox)
{
    // replaces double spaces
    textBox.value = textBox.value.replace("  ", ' ');
    
    // trim leading spaces
    textBox.value = trim(textBox.value);
}