/**
 * This file contains 3 functions:
 * 1) addTag()
 * 2) removeTag()
 * 3) updateTags()
**/

/**
 * Add Tag
 * 
 * This method adds a tag, which is given from the text field.
 * 
 * The following HTML id components are expected to exist:
 * 1) tagField
 * 2) myTags
**/
function addTag()
{
	hideSuggest();
	var tagToAdd = $('tagField').value;
	
	if(tagToAdd.length > 0)
	{
		// Set up parameters for AJAX request
		var params = "listingID=" + myListingID;
		params += "&userID=" + client.userID;
		params += "&cfToken=" + client.cfToken;
		params += "&tag=" + encodeURIComponent(tagToAdd);

		// Fire off AJAX request
		ajax("POST", "/cfc/tags.cfc?method=addTagRPC", updateTags, null, params);

		// Clear the text field
		$('tagField').value = "";
	}
}

/**
 * Remove Tag
 * 
 * This method removes a tag, which is given from the text field.
 * 
 * The following HTML id components are expected to exist:
 * 1) tagField
 * 2) myTags
**/
function removeTag()
{
	hideSuggest();
	var tagToRemove = $('tagField').value;
	
	if(tagToRemove.length > 0)
	{
		// Set up parameters for AJAX request
		var params = "listingID=" + myListingID;
		params += "&userID=" + client.userID;
		params += "&cfToken=" + client.cfToken;
		params += "&tag=" + encodeURIComponent(tagToRemove);

		// Fire off AJAX request
		ajax("POST", "/cfc/tags.cfc?method=removeTagRPC", updateTags, null, params);

		// Clear the text field
		$('tagField').value = "";
	}
}

/**
 * For internal use only.
**/
function updateTags(tags)
{
	// tags - 2-Dimensional Array of all tags for the user
	// tags[row][0] = tag name
	// tags[row][1] = number of listings user has used this tag for

	// We want to update the 'myTags' div to include all the users tags
	var newContent = "<h5>My Tags</h5>";

	for(i=0; i<tags.length; i++)
	{
		newContent += "<a href='/myTags.cfm?tag="+ tags[i][0] +"' class='greenBold'>"+ tags[i][0] +"</a> ("+ tags[i][1] +")";
		if(i != (tags.length - 1))
			newContent += ", &nbsp";
	}

	$('myTags').innerHTML = newContent;
}

