if (typeof KOHA == "undefined" || !KOHA) {
    var KOHA = {};
}

/**
* A namespace for Tags related functions.
* readCookie is expected to already be declared.  That's why the assignment below is unscoped.
* readCookie should be from basket.js or undefined.

$.ajaxSetup({
	url: "/cgi-bin/koha/opac-tags.pl",
	type: "POST",
	dataType: "script"
});
*/
if (typeof(readCookie) == "undefined") {
	readCookie = function (name) { // from http://www.quirksmode.org/js/cookies.html
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' '){ c = c.substring(1,c.length); }
			if (c.indexOf(nameEQ) == 0){ return c.substring(nameEQ.length,c.length); }
		}
		return null;
	}
}
//COREMAIN S.L.U.
//ID 0001524
// Ignacio Javier
// Errores en JSON formato de bloques falla en IE si va con shorcuts de listas
KOHA.Tags = {
	add_tag_button: function(){
		var mybibnum = $(this).attr("title");
		var mynewtag = "newtag" + mybibnum;
		var mytagid = "#" + mynewtag;
		var mydata = {CGISESSID: readCookie('CGISESSID')};	// Someday this should be OPACSESSID
		mydata[mynewtag] = $(mytagid).val();	// need [bracket] for variable property id
		var response;	// AJAX from server will assign value to response.
		$.post(
			"/cgi-bin/koha/opac-tags.pl",
			mydata,
			function(data){
				// alert("AJAX Response: " + data);
				eval(data);
				// alert("counts: " + response["added"] + response["deleted"] + response["errors"]);
				KOHA.Tags.set_tag_status(
					mytagid + "_status",
					KOHA.Tags.common_status(response["added"], response["deleted"], response["errors"])
				);
				if (response.alerts) {
					alert(response.alerts.join("\n\n"));
				}
			},
			'script'
		);
		return false;
	},
	common_status : function(addcount, delcount, errcount) {
	    var cstat = "";
	    if (addcount && addcount > 0) {cstat += "Engadida etiqueta "   + addcount + (addcount==1 ? "" : " tags") + ".  " ;}
	    if (delcount && delcount > 0) {cstat += "Eliminada etiqueta " + delcount + (delcount==1 ? "" : " tags") + ".  " ;}
	    if (errcount && errcount > 0) {cstat += (errcount==1 ? "ERROR" : errcount + " ERRORS") + " durante a operacion.";}
	    return cstat;
	},
	set_tag_status : function(tagid, newstatus) {
		$(tagid).html(newstatus);
		$(tagid).css({display:"inline"});
	},
//COREMAIN S.L.U.
//GIT - 14/07/2008
	tag_message: {
	tagsdisabled : function(arg) {return (_("Sintoo, as etiquetas non están habilitadas no sistema."));},
       scrubbed_all_bad : function(arg) {return (_("Error! A túa etiqueta ten código de marcado. Non foi engadida. Por favor, inténtao con texto plano."));},
       badparam : function(arg) {return (_("Error! Parámetro ilegal '" +arg+ "'."));},
       scrubbed : function(arg) {return (_("Nota: a etiqueta que contiña código de marcado foi eliminado. A etiqueta foi engadida como '" +arg+ "'."));},
    failed_add_tag : function(arg) {return (_("Error! A operación add_tag fallou en '" +arg+ "'.  Nota: Non se poden engadir etiquetas co mesmo nome. Revisa 'As miñas etiquetas' para ver as que ten engadidas."));},
    failed_delete  : function(arg) {return (_("Error! Non se pode eliminar a etiqueta '" +arg+ "'.  Nota: só podes eliminar as túas etiquetas."));}
       }
//Cierre 14/07/2008       
};
// Cierre

//COREMAIN S.L.U.
//ID 0001382 - 30/05/2008
//GIT	 
//failed_add_tag : function(arg) {return ("Error! The add_tag operation failed on '" +arg+ "'.");},	
//Cierre    
