//////////////////////////////////////////////
// Login User Handler VERSION_0.8
//////////////////////////////////////////////
//
// Joe Lambert 10.2006
//
// INSTRUCTIONS
// ------------
//
// The Login script will check for an object in the UI with its id set to 'mSpaceUser', this is the
// value it will update on a succesful login/registration.
//
// The values needed for the login/registration are pulled from the form via JS so the neccesary values are:
//
//	- <input id="LoginUsername" />
//	- <input id="LoginPassword" />
//
//	- <input id="RegisterUsername" />
//	- <input id="RegisterPassword1" />
//	- <input id="RegisterPassword2" />
//
//////////////////////////////////////////////

var Login = Class.create();
Login.prototype = {
//
// Member Variables

//local settings
m_loggedin: false,
m_xmlRequest: new XMLHttpRequest(),
m_previousURL: null,
m_forever: 100000,
m_currentUsername: null,
m_TagForm: null,
m_currentAddTagUri: null,
m_recentTags: new Array(),
m_defaultExternalLoginUrl: "http://edina.ac.uk/cgi/nfoautho/",

//
// Constructor
initialize: function(optionsObject)
	{
		
		
		
		//Override the defatul object parameters, with those from the objectOptions.
		ParseOptions(optionsObject, this);
		
		
		new TagControl();
		
		mSpaceApplication.Subscribe("LoginControl.MyAccount", this);
		mSpaceApplication.Subscribe("LoginControl.Logout", this);
		mSpaceApplication.Subscribe("LoginControl.Register", this);
		mSpaceApplication.Subscribe("LoginControl.LoginOk", this);
		
		
		mSpaceApplication.Subscribe("LoginControl.AddTag", this);
		mSpaceApplication.Subscribe("LoginControl.DeleteTag", this);
		
		mSpaceApplication.Subscribe("LoginControl.ShareClip", this);
		mSpaceApplication.Subscribe("LoginControl.DiscussClip", this);
		mSpaceApplication.Subscribe("LoginControl.AddComment", this);
		
		mSpaceApplication.Subscribe("LoginControl.ExternalLogin", this);
		
	},
	
Notify: function(eventName, eventParams)
	{
			
		switch(eventName)
		{			
			case 'LoginControl.MyAccount' : this.ShowMyAccount(eventParams); break;
			case 'LoginControl.Logout' : this.Logout(eventParams); break;
			case 'LoginControl.Register' : this.Register(eventParams); break;
			case 'LoginControl.LoginOk' : this.LoginOk(eventParams); break;
			case 'LoginControl.AddTag' : this.AddTag(eventParams); break;
			case 'LoginControl.DeleteTag' : this.DeleteTag(eventParams); break;
			case 'LoginControl.ShareClip' : this.ShareClip(eventParams); break;
			case 'LoginControl.DiscussClip' : this.DiscussClip(eventParams); break;
			case 'LoginControl.AddComment' : this.AddComment(eventParams); break;
			case 'LoginControl.ExternalLogin' : this.ExternalLogin(eventParams); break;
			default: break;
		}		
	},
	
ShowMyAccount: function(params)
	{

			var loginUrl = "./inc/mspace.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=userinfo&page=login&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel"));
			mSpaceApplication.FireEvent('InformationControl.SetUrl', loginUrl);
			
	},

Logout: function()
	{
		
		this.m_currentUsername = null;

		// TODO: Return user to previous page
		mSpaceApplication.FireEvent('InformationControl.SetUrl', 'inc/logout.php');
		
	},
	
Register: function()
	{			
			if (this.m_currentUsername==null)
			{
				var loginUrl = "./inc/mspace.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=register&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel"));
				mSpaceApplication.FireEvent('InformationControl.SetUrl', loginUrl);
			}		
	},
	
LoginOk: function(eventParams)
	{
		this.m_currentUsername = eventParams;
	},
	
AddTag: function(eventParams)
	{
		
		this.m_currentAddTagUri = eventParams.normal;
		
		var label  = "Add tags to '" + eventParams.label + "'";
		
			
		if (this.m_TagForm==null)
		{
			this.m_TagForm = createDiv("AddTagForm");		
			
			var tempLabel = createDiv("label", true);
			tempLabel.innerHTML = "Space separated list of tags:";
			
			this.m_TagForm.appendChild(tempLabel);
			
			var editBoxContainer = createDiv("editContainer", true);
			
			this.m_TagForm.appendChild(editBoxContainer);
			
			this.m_tagEditBox = document.createElement("input");
			this.m_tagEditBox.type = "text";
			
			editBoxContainer.appendChild(this.m_tagEditBox);
			
			

			var publicCheckContainer = createDiv("publicContainer", true);
			
			this.m_TagForm.appendChild(publicCheckContainer);
			
			this.m_tagPublicBox = document.createElement("input");
			this.m_tagPublicBox.type = "checkbox";
			this.m_tagPublicBox.checked = true;
			
			publicCheckContainer.appendChild(this.m_tagPublicBox);
			
			
			
			publicCheckContainer.appendChild(document.createTextNode("Tags are Public"));
			
			
			
			var btnContainer = createDiv("buttonContainer", true);
			
			this.m_TagForm.appendChild(btnContainer);
			
			this.m_tagCancelBtn = document.createElement("input");
			this.m_tagCancelBtn.type = "button";
			this.m_tagCancelBtn.value = "Cancel";
			
			RegisterEvent(this.m_tagCancelBtn, "click", this, "ClearTagForm");
			
			btnContainer.appendChild(this.m_tagCancelBtn);	
			
			
			this.m_tagBtn = document.createElement("input");
			this.m_tagBtn.type = "submit";
			this.m_tagBtn.value = "Add Tags";
			
			RegisterEvent(this.m_tagBtn, "click", this, "SendTags");
			
			btnContainer.appendChild(this.m_tagBtn);			
			
			
		}
		else
		{
			this.m_tagPublicBox.checked = true;
			this.m_tagEditBox.value = "";
			
		}
		
		var label  = "Add tags to '" + eventParams.label + "'";
		this.m_tagEditBox.value = "";
		this.m_tagPublicBox.checked = true;
		mSpaceApplication.FireEvent('LightBox.Show', { 'childNode' : this.m_TagForm, 'title': label });
	},
	
ClearTagForm: function()
	{
		mSpaceApplication.FireEvent('LightBox.Hide', 0);
	},
	
SendTags: function()
	{
		
		if (this.m_currentAddTagUri==null)
		{
			mSpaceApplication.FireEvent('LightBox.Hide', 0);
			return;
		}
		
		

		
			var tagUrl = "./inc/mspace.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=addtag&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel")) + "&itemuri=" + URLEncode(this.m_currentAddTagUri);
			

			
			if ((this.m_tagPublicBox!=null) && (this.m_tagPublicBox.checked))
			{
				tagUrl += "&public=1";
			}
			
			if (this.m_GroupEditBox!=null)
			{
				tagUrl += "&groups=" + URLEncode(this.m_GroupEditBox.value);
				//Reset group box, in so its not reused
				this.m_GroupEditBox.value = "";				
			}	
			
			if ((this.m_tagEditBox!=null) && (this.m_tagEditBox.value!=""))
			{
				tagUrl += "&tags=" + URLEncode(this.m_tagEditBox.value);
				//Reset group box, in so its not reused
				this.m_tagEditBox.value = "";				
			}
			else if ((this.m_GroupTagEditBox!=null) && (this.m_GroupTagEditBox.value!=""))
			{
				tagUrl += "&tags=" + URLEncode(this.m_GroupTagEditBox.value);
				//Reset group box, in so its not reused
				this.m_GroupTagEditBox.value = "";				
			}			
			
			
		
			mSpaceApplication.FireEvent('LightBox.Hide', 0);
		
		
			var thisObj = this;

			// BUG FIX IN FIREFOX FOR 'NS_ERROR_NOT_INITIALIZED' ERROR
			if((browserDetails[0]=="msie") && (browserDetails[1]=="7.0") || XMLRequestBusy_LoginControl && thisObj.m_xmlRequest.readyState != 4 && thisObj.m_xmlRequest.readyState != 0)
			{
				this.m_xmlRequest.onreadystatechange = function () {}; // Must clear the handler otherwise a status 4 is called which makes the script think it has made a successful call
				this.m_xmlRequest.abort();
				this.m_xmlRequest = new XMLHttpRequest();
			}
			
			
			// Use the onload function if the browser is anything other than ie <7.0.  Fixes problems on some firefox and mozilla
			// versions
			if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
			{
				this.m_xmlRequest.onreadystatechange = function() {
					try {
						if(thisObj.m_xmlRequest.readyState == 4 && (thisObj.m_xmlRequest.status == 200 || thisObj.m_xmlRequest.status == 304)) {
							//#################
							//RESULTS
							//#################
							XMLRequestBusy_LoginControl = false;	
							mSpaceApplication.FireEvent('InformationControl.Refresh');					
						}
					}catch (e){
					}				  
				};
			}else{
				this.m_xmlRequest.onload = function() {
					try {
						//#################
						//RESULTS
						//#################
						XMLRequestBusy_LoginControl = false;
						mSpaceApplication.FireEvent('InformationControl.Refresh');
					}
					catch (e)
					{
					}				  
				};			
			}
		
			try
			{
				if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
				{
					window.setTimeout( function()
												{
													thisObj.m_xmlRequest.open('GET', tagUrl);
													XMLRequestBusy_LoginControl = true;
													thisObj.m_xmlRequest.send(null);
												}
										,500);
				}
				else
				{
					this.m_xmlRequest.open('GET', tagUrl);
					XMLRequestBusy_LoginControl = true;
					this.m_xmlRequest.send(null);			
				}	
			}
			catch(e){}		
		
		
	},
	
DeleteTag: function(eventParams)
	{
		
		var uri1 = eventParams.normal;
		var uri2 = eventParams.adv;
		var tag = eventParams.tag
		
		
			var tagUrl = "./inc/xml.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=deletetag&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel")) + "&itemuri=" + URLEncode(uri1) + "&itemuri2=" + URLEncode(uri2) + "&tag=" + URLEncode(tag);
			
		
			var thisObj = this;

			// BUG FIX IN FIREFOX FOR 'NS_ERROR_NOT_INITIALIZED' ERROR
			if((browserDetails[0]=="msie") && (browserDetails[1]=="7.0") || XMLRequestBusy_LoginControl && thisObj.m_xmlRequest.readyState != 4 && thisObj.m_xmlRequest.readyState != 0)
			{
				this.m_xmlRequest.onreadystatechange = function () {}; // Must clear the handler otherwise a status 4 is called which makes the script think it has made a successful call
				this.m_xmlRequest.abort();
				this.m_xmlRequest = new XMLHttpRequest();
			}
			
			
			// Use the onload function if the browser is anything other than ie <7.0.  Fixes problems on some firefox and mozilla
			// versions
			if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
			{
				this.m_xmlRequest.onreadystatechange = function() {
					try {
						if(thisObj.m_xmlRequest.readyState == 4 && (thisObj.m_xmlRequest.status == 200 || thisObj.m_xmlRequest.status == 304)) {
							//#################
							//RESULTS
							//#################
							XMLRequestBusy_LoginControl = false;
							if (thisObj.m_xmlRequest.responseText.indexOf("ok")!=-1)
							{
								var tagElement = document.getElementById("tag_" + tag);
								if ((tagElement) && (tagElement.parentNode))
								{
									tagElement.parentNode.removeChild(tagElement);
								}
							}
						}
					}catch (e){					
					}				  
				};
			}else{
				this.m_xmlRequest.onload = function() {
					try {
						//#################
						//RESULTS
						//#################
						XMLRequestBusy_LoginControl = false;
							if (thisObj.m_xmlRequest.responseText.indexOf("ok")!=-1)
							{
								var tagElement = document.getElementById("tag_" + tag);
								if ((tagElement) && (tagElement.parentNode))
								{
									tagElement.parentNode.removeChild(tagElement);
								}
							}						
					}
					catch (e)
					{
					}				  
				};			
			}
		
			try
			{
				if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
				{
					window.setTimeout( function()
												{
													thisObj.m_xmlRequest.open('GET', tagUrl);
													XMLRequestBusy_LoginControl = true;
													thisObj.m_xmlRequest.send(null);
												}
										,500);
				}
				else
				{
					this.m_xmlRequest.open('GET', tagUrl);
					XMLRequestBusy_LoginControl = true;
					this.m_xmlRequest.send(null);			
				}	
			}
			catch(e){}		
		
		
	},
		
		
ShareClip: function(eventParams)
	{
		
		this.m_currentAddTagUri = eventParams.normal;
		
		var label  = "Share '" + eventParams.label + "' with your groups using tags";
		
			
		if (this.m_GroupTagForm==null)
		{
			this.m_GroupTagForm = createDiv("AddTagForm");		
			
			
			/*GROUP FORM PART*/
			
			var tempLabel1 = createDiv("label", true);
			tempLabel1.innerHTML = "Space separated list of groups:";
			
			this.m_GroupTagForm.appendChild(tempLabel1);
			
			var editBoxContainer1 = createDiv("editContainer", true);
			
			this.m_GroupTagForm.appendChild(editBoxContainer1);
			
			this.m_GroupEditBox = document.createElement("input");
			this.m_GroupEditBox.type = "text";
			
			editBoxContainer1.appendChild(this.m_GroupEditBox);			
			
			
			
			/* TAG FORM PART */
			var tempLabel = createDiv("label", true);
			tempLabel.innerHTML = "Space separated list of tags:";
			
			this.m_GroupTagForm.appendChild(tempLabel);
			
			var editBoxContainer = createDiv("editContainer", true);
			
			this.m_GroupTagForm.appendChild(editBoxContainer);
			
			this.m_GroupTagEditBox = document.createElement("input");
			this.m_GroupTagEditBox.type = "text";
			
			editBoxContainer.appendChild(this.m_GroupTagEditBox);

			
			
			
			var btnContainer = createDiv("buttonContainer", true);
			
			this.m_GroupTagForm.appendChild(btnContainer);
			
			this.m_GroupTagCancelBtn = document.createElement("input");
			this.m_GroupTagCancelBtn.type = "button";
			this.m_GroupTagCancelBtn.value = "Cancel";
			
			RegisterEvent(this.m_GroupTagCancelBtn, "click", this, "ClearTagForm");
			
			btnContainer.appendChild(this.m_GroupTagCancelBtn);	
			
			
			this.m_GroupTagBtn = document.createElement("input");
			this.m_GroupTagBtn.type = "submit";
			this.m_GroupTagBtn.value = "Add Tags";
			
			RegisterEvent(this.m_GroupTagBtn, "click", this, "SendTags");
			
			btnContainer.appendChild(this.m_GroupTagBtn);			
			
			
		}
		else
		{
			this.m_GroupTagEditBox.value = "";
			this.m_GroupEditBox.value = "";
			
		}
		
		mSpaceApplication.FireEvent('LightBox.Show', { 'childNode' : this.m_GroupTagForm, 'title': label });
	},
	
	
ExternalLogin: function(url)
	{
		var actual_url = url || this.m_defaultExternalLoginUrl;
		
		
		actual_url += "?url=" + URLEncode(window.mSpaceApplication.m_baseUrl);
		
		var histStr = window.mSpaceHistory.GetHistoryHash();
		
		if (histStr!="")
		{
			actual_url += "&history=" + URLEncode(histStr)
		}
		
		document.location.href = actual_url;
		
	}
		
	
};


var TagControl = Class.create();
TagControl.prototype = {
//
// Member Variables

//local settings
m_recentTags: new Array(),
m_myGroups: new Array(),
m_currentItemUri: "",
	
	//
	// Constructor
initialize: function()
	{
		
		
			this.m_TagForm = createDiv("AddTagForm");		
			
			//## Add the groups area box.
			
			var tempLabel2 = createDiv("label", true);
			tempLabel2.innerHTML = "Your Groups:";
			
			this.m_TagForm.appendChild(tempLabel2);
			
			this.m_groupBoxContainer = createDiv("groupContainer", true);
			this.m_groupBoxContainer.style.display = "none";
			this.m_groupBoxContainer.appendChild(document.createTextNode("Loading..."));
			
			this.m_TagForm.appendChild(this.m_groupBoxContainer);
			
			
			
			//## Add the tags box to the 
			
			var tempLabel = createDiv("label", true);
			tempLabel.innerHTML = "Space separated list of tags:";
			
			this.m_TagForm.appendChild(tempLabel);
			
			var editBoxContainer = createDiv("editContainer", true);
			
			this.m_TagForm.appendChild(editBoxContainer);
			
			this.m_tagEditBox = document.createElement("input");
			this.m_tagEditBox.type = "text";
			
			editBoxContainer.appendChild(this.m_tagEditBox);
			
			

						
			
			
			
			
			var recentTagsHeader = createDiv("recentTagsHeader", true);
			recentTagsHeader.innerHTML = "Recent Tags:";
			this.m_TagForm.appendChild(recentTagsHeader);
			
			this.m_recentTagsContainer = createDiv("recentTagsContainer", true);
			this.m_TagForm.appendChild(this.m_recentTagsContainer);
			

			var publicCheckContainer = createDiv("publicContainer", true);
			
			this.m_TagForm.appendChild(publicCheckContainer);
			
			this.m_tagPublicBox = document.createElement("input");
			this.m_tagPublicBox.type = "checkbox";
			this.m_tagPublicBox.checked = true;
			
			publicCheckContainer.appendChild(this.m_tagPublicBox);
			
			
			
			publicCheckContainer.appendChild(document.createTextNode("Tags are Public"));
			
			
			
			var btnContainer = createDiv("buttonContainer", true);
			
			this.m_TagForm.appendChild(btnContainer);
			
			this.m_tagCancelBtn = document.createElement("input");
			this.m_tagCancelBtn.type = "button";
			this.m_tagCancelBtn.value = "Cancel";
			
			RegisterEvent(this.m_tagCancelBtn, "click", this, "HideTagForm");
			
			btnContainer.appendChild(this.m_tagCancelBtn);	
			
			
			this.m_tagBtn = document.createElement("input");
			this.m_tagBtn.type = "submit";
			this.m_tagBtn.value = "Add Tags";
			
			RegisterEvent(this.m_tagBtn, "click", this, "SendTags");
			
			btnContainer.appendChild(this.m_tagBtn);		
			
			
			
		
		
		mSpaceApplication.Subscribe("TagControl.AddTag", this);
		mSpaceApplication.Subscribe("TagControl.ExternalAddTags", this);
		mSpaceApplication.Subscribe("TagControl.AddGroupTag", this);
		mSpaceApplication.Subscribe("TagControl.DeleteTag", this);
		mSpaceApplication.Subscribe("TagControl.GroupToggle", this);
	},
	
Notify: function(eventName, eventParams)
	{
			
		switch(eventName)
		{			
			case 'TagControl.AddTag' : this.ShowTagForm(eventParams, "tag"); break;
			case 'TagControl.ExternalAddTags' : this.SendTagsFromExternal(eventParams); break;
			case 'TagControl.AddGroupTag' : this.ShowTagForm(eventParams, "group"); break;
			case 'TagControl.DeleteTag' : this.DeleteTag(eventParams); break;
			case 'TagControl.GroupToggle' : this.GroupToggle(eventParams); break;
			default: break;
		}		
	},
	
TagInputBoxChange: function(e, element)
	{
	},

ShowTagForm: function(eventParams, mode)
	{
		this.m_currentMode = mode;
		this.m_currentItemUri = eventParams.uri;
		this.m_appendTagTextId = eventParams.tagsectionid;
		this.m_replaceTagSection = eventParams.replacetags || false;
		var label  = "Add tags to '" + eventParams.label + "'";
		if (mode=="group")
		{
			label  = "Share '" + eventParams.label + "' with your groups";
			this.m_groupBoxContainer.style.display = "";
			
			this.m_groupBoxContainer.innerHTML = "";
			this.m_groupBoxContainer.appendChild(document.createTextNode("Loading..."));
			
			var getGroupsUrl = "./inc/mspace.php?stylesheetname=grouptag&count=-1&action=userinfo&page=groups&" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel"));			
			mSpaceApplication.FireEvent("DataController.Request", { 'sender' : this, 'id' : 'getgroups', 'type' : "GET", 'url' : getGroupsUrl });			
		}
		else
		{
			this.m_groupBoxContainer.style.display = "none";
		}
		this.m_tagEditBox.value = "";		
		this.m_tagPublicBox.checked = true;
		
		this.m_recentTagsContainer.innerHTML = "";

		if (this.m_recentTags.length>0)
		{
			var end = 0;
			if (this.m_recentTags.length>10)
			{
				end =  this.m_recentTags.length - 10;
			}			
			for (var i=(this.m_recentTags.length-1);i>=end;i--)
			{
					var newTagLink = document.createElement("A");
					newTagLink.innerHTML = this.m_recentTags[i];
					
					this.m_recentTagsContainer.appendChild(newTagLink);
					this.m_recentTagsContainer.appendChild(document.createTextNode(" "));
					
					RegisterEvent(newTagLink, "click", this, "AddTagText", false, false, this.m_recentTags[i]);
			}
		}
		
		mSpaceApplication.FireEvent('LightBox.Show', { 'childNode' : this.m_TagForm, 'title': label });		
	},
	
AddTagText: function(e, element, name, params)
	{
		this.m_tagEditBox.value = this.m_tagEditBox.value + " " + params;
	},
	
HideTagForm: function(params)
	{
		mSpaceApplication.FireEvent('LightBox.Hide', 0);
	},
	
SendTags: function(params)
	{
		if ((this.m_currentItemUri=="") || (this.m_tagEditBox.value==""))
		{
			mSpaceApplication.FireEvent('LightBox.Hide', 0);
			return;
		}
		
		var tagString = this.m_tagEditBox.value;
		var tagList = tagString.split(' ');
		
		for (var i=0;i<tagList.length;i++)
		{
			this.m_recentTags.push(tagList[i]);
		}
		
	
		var tagUrl = "./inc/mspace.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=addtag&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel"));
		
		tagUrl  += "&itemuri[]=" + URLEncode(this.m_currentItemUri);
		tagUrl += "&tags=" + URLEncode(this.m_tagEditBox.value);
		
		if ((this.m_tagPublicBox!=null) && (this.m_tagPublicBox.checked))
		{
			tagUrl += "&public=1";
		}	
		
		if (this.m_currentMode=="group")
		{
			tagUrl += "&groups=" + URLEncode(this.m_myGroups.join(" "));
		}
					
		
		this.m_currentItemUri = "";
		
		mSpaceApplication.FireEvent("DataController.Request", { 'sender' : this, 'id' : this.m_currentMode, 'type' : "GET", 'url' : tagUrl });
	},
	
SendTagsFromExternal: function(eventParams)
	{
		
		if (!eventParams.tags || !eventParams.items)
		{
			return;
		}
		
		var tagString = eventParams.tags;
		var tagList = tagString.split(' ');
		
		for (var i=0;i<tagList.length;i++)
		{
			this.m_recentTags.push(tagList[i]);
		}
		
	
		var tagUrl = "./inc/mspace.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=addtag&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel"));
		
		for (var i=0;i<eventParams.items.length;i++)
		{
			var itemUri = eventParams.items[i];
			tagUrl  += "&itemuri[]=" + URLEncode(itemUri);
		}		
		tagUrl += "&tags=" + URLEncode(eventParams.tags);
		
		if (eventParams.public)
		{
			tagUrl += "&public=1";
		}
		
		mSpaceApplication.FireEvent("DataController.Request", { 'sender' : this, 'id' : "external", 'type' : "GET", 'url' : tagUrl, 'returnParameter' : eventParams});
	},
	
DeleteTag: function(eventParams)
	{
		var uri = eventParams.uri;
		this.m_deleteTag = eventParams.tag
		
		
		var tagUrl = "./inc/xml.php?" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=deletetag&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel")) + "&itemuri=" + URLEncode(uri) + "&tag=" + URLEncode(this.m_deleteTag);
			
		mSpaceApplication.FireEvent("DataController.Request", { 'sender' : this, 'id' : 'delete', 'type' : "GET", 'url' : tagUrl });
				
	},
	
HandleResponse: function(id, data, returnParameter)
	{
		if ((id=="tag") || (id=="external"))
		{
			this.js_Response = null;
			//alert(data);
			try
			{
				eval(data);
			}
			catch (e)
			{
			}
					
			
			mSpaceApplication.FireEvent('LightBox.Hide', 0);
			
			if ((this.js_Response!=null) && ((this.js_Response.response=="FAIL") || (this.js_Response.response=="500")))
			{
				alert("Error adding tag: " + this.js_Response.message);
				if (returnParameter.notifyFunction)
				{
					returnParameter.notifyFunction("error");
				}				
				return;
			}
			else if (id=="tag")
			{
				var tagDiv = document.getElementById(this.m_appendTagTextId);
				if (this.m_replaceTagSection)
				{
					tagDiv.innerHTML = data;
				}
				else
				{
					tagDiv.innerHTML += data;
				}
			}
			
			if (returnParameter.notifyFunction)
			{
				returnParameter.notifyFunction("ok");
			}			
		}
		else if (id=="delete")
		{
				var tagElement = document.getElementById("tag_" + this.m_deleteTag);
				if ((tagElement) && (tagElement.parentNode))
				{
					tagElement.parentNode.removeChild(tagElement);
				}			
		}
		else if (id=="group")
		{
			this.js_Response = null;
			//alert(data);
			try
			{
				eval(data);
			}
			catch (e)
			{
			}
					
			
			mSpaceApplication.FireEvent('LightBox.Hide', 0);
			
			if ((this.js_Response!=null) && (this.js_Response.response=="FAIL"))
			{
				alert("Error adding tag: " + this.js_Response.message);
			}			
		}
		else if (id=="getgroups")
		{
			this.m_groupBoxContainer.innerHTML = data;
		}
	},
	
GroupToggle: function(groupname)
	{
		var chk = document.getElementById("GroupTagCheck_" + groupname);
		
		var newGroupArray = new Array();
		for (var i=0;i<this.m_myGroups.length;i++)
		{
			if (this.m_myGroups[i]!=groupname)
			{
				newGroupArray.push(this.m_myGroups[i]);
			}
		}
		
		this.m_myGroups = newGroupArray;
		
		if (chk.checked)
		{
			this.m_myGroups.push(groupname);
		}
		
	}

}



