/**
 * Used to manage tags
*/

function TaggingTool(handlerUrl) {
	this.url = handlerUrl;
}

TaggingTool.prototype.post = function(url, parameterString, callback) {
    if (window.XMLHttpRequest) {
	   	xmlReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	//alert(url+"?"+parameterString);
	xmlReq.persistenceTool = this;
	xmlReq.open("POST",url,true); 
	xmlReq.onreadystatechange = callback;
	xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
	xmlReq.send(parameterString);
}

TaggingTool.prototype.completeTag = function() {
    if (xmlReq.readyState == 4) {
        if (xmlReq.status == 200) {
		    var response = xmlReq.responseText;
		    var status = (response.split("\n")[0]);
		    if (status == "success" || status == "success")
				;//alert("Successfully added tags: "+response.split('\n')[1]);
			else
				alert("Adding tags failed: \n"+response);
		} else {
			alert("The website is having some problems ... the code returned is "+xmlReq.status);
		}
    } else {
		//alert("received file with status "+xmlReq.readyState);
    }
}

TaggingTool.prototype.tag = function(artifactid, type, username, text) {
	var str = 'id='+artifactid+'&type='+type+'&user='+username+'&text='+text;
	this.post(this.url, str, TaggingTool.prototype.completeTag);
}

TaggingTool.prototype.completeDeleteTag = function() {
    if (xmlReq.readyState == 4) {
        if (xmlReq.status == 200) {
		    var response = xmlReq.responseText;
		    var status = (response.split("\n")[0]);
		    if (status == "success" || status == "success")
				;//alert("Successfully deleted tags: "+response.split('\n')[1]);
			else
				alert("Deleting tags failed: \n"+response);
		} else {
			alert("The website is having some problems ... the code returned is "+xmlReq.status);
		}
    } else {
		//alert("received file with status "+xmlReq.readyState);
    }
}

TaggingTool.prototype.deleteTag = function(artifactid, type, username, text) {
	var str = 'command=Delete&id='+artifactid+'&type='+type+'&user='+username+'&text='+text;
	this.post(this.url, str, TaggingTool.prototype.completeDeleteTag);
}

