function add_item() {
    var url = '/ajax/tag/normal_tag';
    var pars = Form.serialize("add");
    
    if (inherit_add != '') {
        var is_confirmed = confirm(inherit_add);
        if (is_confirmed) {
            pars = pars + '&inherit=1';
        }
    }

	$('message').innerHTML = "<p>"+ZORPIA_JS_LANG.LOADING+"</p>";
	Form.disable("add");

	var myAjax = new Ajax.Request( url, {
	    method: 'get',
	    parameters: pars,
	    onSuccess: showAddResponse,
	    onFailure: reportAddError
	} );
}

function showAddResponse(request) {
	response  = request.responseText;

    var xotree = new XML.ObjTree();
    xotree.force_array = [ "tag" ];
    var tree = xotree.parseXML( response ); 
    Form.enable("add");
    Form.reset("add");
    
    var bottom_msg = '';
    var message = '';
    
    for (i = 0; i < tree.xml.tag.length; i++) {
        if (tree.xml.tag[i].message != 'Add OK') {
            message = message + tree.xml.tag[i].message + '<br />';
        } else {
    		bottom_msg = bottom_msg + '<span id="tag' + tree.xml.tag[i].tag_id + '">, <a href="http://search.zorpia.com/search/' + tree.xml.tag[i].object_type + 's/' + tree.xml.tag[i].tagname + '/">' + tree.xml.tag[i].tagname + '</a>';
            bottom_msg = bottom_msg + " (<a href=\"javascript:del_item('" + tree.xml.tag[i].object_type + "', '" + tree.xml.tag[i].object_id + "', 'tag', '" + tree.xml.tag[i].tag_id + "')\">X</a>) </span>";
        }
    }

    new Insertion.Bottom('tags', bottom_msg);
    $(message_div).innerHTML = message;
    $('no_tags').style.display = 'none';
}
function reportAddError(request) {
    Form.enable("add");
    $(message_div).innerHTML = '<p>'+ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR+'</p>';
}

function del_item(object_type, object_id, tag_type, tag_id) {
    var url = '/ajax/tag/del_tag';
    var pars = 'tag_type=' + tag_type + '&tag_id=' + tag_id + '&object_type=' + object_type + '&object_id=' + object_id + '&group_code=' + group_code;
    
    if (inherit_del != '') {
        var is_confirmed = confirm(inherit_del);
        if (is_confirmed) {
            pars = pars + '&inherit=1';
        }
    }

	$(message_div).innerHTML = "<p>"+ZORPIA_JS_LANG.LOADING+"</p>";

	var myAjax = new Ajax.Request( url, {
	    method: 'get',
	    parameters: pars,
	    onSuccess: showDelResponse,
	    onFailure: reportDelError
	} );
}
function showDelResponse(request) {
    response  = request.responseText;

    var xotree = new XML.ObjTree();
    var tree = xotree.parseXML( response ); 

    if (tree.xml.message != 'OK') {
        $(message_div).innerHTML = tree.xml.message;
        return;
    }
    $(message_div).innerHTML = '';

    if (tree.xml.tag_type == 'user') {
        $('people'+tree.xml.tag_id).style.display = 'none';
    } else {
        $('tag'+tree.xml.tag_id).style.display = 'none';
    }
}
function reportDelError(request) {
    $(message_div).innerHTML = '<p>'+ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR+'</p>';
}
