// ----------------------------------------------------
// base per il file upload

var CUpload = Class.create({

	getImage: function (gateway, params, preview)
	{
		new Ajax.Request(
			gateway,
			{
				method: 'get'
				, parameters: params
				, onComplete: function(request) {
						if (request.responseText.indexOf('+KO') != -1) {
							Element.update(preview, '');
						} else {
							Element.update(preview, '<img src="'+request.responseText.substr(3)+'"/>');
						}
					}
			}
		);
	},

	post: function(targetFrame, file, gateway, params)
	{
		if (file.value) {
			// posto l'immagine, creando il file
			file.form.target = targetFrame;
			file.form.action = gateway + '?' + params;
			file.form.method = 'POST';
			file.form.submit();
		}
	},

	get: function(id)
	{
		window.open('file.php?AZIONE=download&ID='+id,null,null);
	}

});


// ----------------------------------------------------
// upload avatar (c'e' sia nel back che nel front)

var CAvatar = Class.create(CUpload, {
	initialize: function() {
		this.file = null;
		this.preview = this.target = 'avatar';
	},
	
	upload: function (user, file)
	{
		this.file = file;
		this.post(this.target, this.file, 'avatar.php', 'AZIONE=add&USER=' + user);
	},

	update: function(user)
	{
		if (this.file) this.file.value = '';
		this.getImage('avatar.php', 'AZIONE=get&USER=' + user, this.preview)
	},

	rollback: function(user)
	{
		if (this.file) this.file.value = '';
		this.getImage('avatar.php', 'AZIONE=rollback&USER=' + user, this.preview);
	},

	reset: function(msg)
	{
		Ubk.error(Locale.string(3002) + msg);
		Element.update(this.preview, '');
		if (this.file) this.file.value = '';
	}
});
