/*
 * Function to add a com.connexience.server.social.tag to an object and to the page
 */
function addTag(tags, soid)
{
  //if the tags are undefined then get them from the add_tags element.
  if (tags === undefined || tags === null || tags === "")
  {
    tags = document.getElementById("add_tags").value;
  }

  //if the socialobject id is not defined, get them from the tags_so element
  if (soid === undefined || soid === null || soid === "")
  {
    soid = document.getElementById("tag_so").value;
  }

  //if the default text is still there, do nothing
  if (tags !== "Add Tags")
  {

    //remove the commas
    var commasRemovedTags = tags.replace(",", " ");

    //split the tags on quotes and spaces and then remove the quotes 
    var tagNames = commasRemovedTags.match(/("[^"]+"|[^"\s]+)/g);

    for (var i = 0; i < tagNames.length; i++)
    {
      tagNames[i] = tagNames[i].replace(/^"([^"]+)"$/, "$1");
    }

    //send the id of the file and the new name
    var tagURL = "../servlets/fileapi?method=addTag";
    var callData = {id: soid, tagNames: tagNames};
    var callString = YAHOO.lang.JSON.stringify(callData);

    //prepare our callback object
    var callback = {

      success: function(oResponse)
      {
        //get the results into a JS assoicative array
        var oResults = eval("(" + oResponse.responseText + ")");

        //deal with the response
        if ((oResults.Result == "success"))
        {
          //the response is an array of links so iterate through them and add each com.connexience.server.social.tag to the page
          var tagLinks = oResults.tagLinks;
          for (var i = 0; i < tagLinks.length; i ++)
          {
            document.getElementById("tags").innerHTML += oResults.tagLinks[i];
          }
          document.getElementById("add_tags").value = "Add Tags";
        }
        else //fail
        {
          alert("Adding Tag failed");
        }
      },

      //log failure
      failure: function(oResponse)
      {
        alert("XHR call failed");
      },

      timeout: 7000
    };

    //call the server to store the link
    YAHOO.util.Connect.asyncRequest('POST', tagURL, callback, callString);
  }
}

/*
 * Function to remove a com.connexience.server.social.tag from an object and from the page
 * */
function removeTag(serverObjectId, tagId)
{

  //send the id of the file and the new name
  var tagURL = "../servlets/fileapi?method=removeTag";
  var callData = {tagId: tagId, soid: serverObjectId};
  var callString = YAHOO.lang.JSON.stringify(callData);

  //prepare our callback object
  var callback = {

    success: function(oResponse)
    {
      //get the results into a JS assoicative array
      var oResults = eval("(" + oResponse.responseText + ")");

      //deal with the response
      if ((oResults.Result == "success"))
      {
        var tagEl = document.getElementById(this.argument.tagId);
        tagEl.parentNode.removeChild(tagEl);
      }
      else //fail
      {
        alert("Removing Tag failed");
      }
    },

    //log failure
    failure: function(oResponse)
    {
      alert("XHR call failed");
    },
    argument:{tagId: tagId},

    timeout: 7000
  };

  //call the server to store the link
  YAHOO.util.Connect.asyncRequest('POST', tagURL, callback, callString);
}

