//
// IASP 5.0 Social Media
//
// Copyright (C) IASP 2007.
// Unauthorised reproduction or use
// on external websites will 
// be subject to rigorous prosecution
//

// Tagging functions
CMS.Tag = function() { }

// Remove a tag from a node
CMS.Tag.removeTag = function(objLink) {
	var oParent = objLink.parentNode || objLink.parent;
	var idString = oParent.id.substring(8);
	// Create AJAX
	var _xh = GetXMLHTTP();
	
	if (_xh) {
		var parts = idString.split('_');
		
		var requestUrl = "/default.aspx?f=_callback&func=del_data_tag&d=" + parseInt(parts[0]) + "&tag_id=" + parseInt(parts[1]);
		
		AJAX_loadStart();
		_xh.onreadystatechange = function() {
			if (_xh.readyState == 4) {
				AJAX_loadEnd();
				if (_xh.status == 200) {
					if (_xh.responseText == 'OK') {
						// Remove ID
						oParent = objLink.parentNode || objLink.parent;
						var oParentParent = oParent.parentNode || oParent.parent;
						oParentParent.removeChild(oParent);
					} else {
						alert('Sorry, there was a problem while attempting to store tag information.\n\nOur team has been notified of this issue. Please try again later.');
					}					
				} else {
					alert('Sorry, there was a problem while attempting to store tag information.\n\nOur team has been notified of this issue. Please try again later.');
				}
			}
		}
		_xh.open('GET',requestUrl,true);
		_xh.send(null);
	} else {
			cms_NoAJAXAlert();
	}	
	return false;
	return false;
}

// Handle keypress in new tag textbox
CMS.Tag.handleNewTagKeypress = function(e,objInput) {
	var key = (window.event ? window.event.keyCode : e.which);

	if (key == 13) {
		//
		// Create new node
		//
		var liObj = objInput.parentNode || objInput.parent;

		var _xh = GetXMLHTTP();
		
		if (_xh) {
			var dataId = parseInt(objInput.id.substring(9));
			var requestUrl = "/default.aspx?f=_callback&func=set_data_tag&d=" + dataId + "&tag=" + encodeURIComponent(objInput.value);

			AJAX_loadStart();
			objInput.value = '';
			_xh.onreadystatechange = function() {
				if (_xh.readyState == 4) {
					AJAX_loadEnd();
					if (_xh.status == 200) {
						if (_xh.responseText == 'AlreadySet') {
							alert('The requested tag is already set for this item.');
						} else {
							var parts = _xh.responseText.split(':');

							// Create new list item
							var newLi = document.createElement('LI');

							newLi.innerHTML = parts[1] + ' <a href="#" onclick="return CMS.Tag.removeTag(this);" title="Remove this tag">[x]</a>';
							newLi.id = 'cms_tag_' + dataId + '_' + parseInt(parts[0]);

							var oParent = liObj.parentNode || liObj.parent;
							oParent.insertBefore(newLi,liObj);
						}
					}
				}
			}
			_xh.open('GET',requestUrl,true);
			_xh.send(null);
		} else {
			cms_NoAJAXAlert();
		}

		//
		// Cancel keypress
		//
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

		return false;
	}
	else
		return true;
}
