function redirect(url)
{
	document.location.href = url;
}
// Refresh della pagina ( semplice alias )
function refresh()
{
	document.location.reload();
}

// Ho selezionato qualcosa
function ajaxSelect(e)
{
  var data = $A(arguments);
  data.shift();

  // nomeFile viene valorizzato nell'header e contiene il nome del file in corso
  var source = nomeFile+'?mode=ajax&id='+this.value+'&link='+data[0];
  // Aggiorno la select corrente
  new Ajax.Updater(data[1], source, {asynchronous:true});

  // Svuoto tutte le altre select
  if (data.length > 2)
    for (var index = 2, len = data.length; index < len; ++index)
      $(data[index]).update('<option>- Seleziona-</option>');
}

// Aggiungo l'autocompleter
function addAutocompleter(idTarget,nomeLink,idPadre)
{
  // nomeFile viene valorizzato nell'header e contiene il nome del file in corso
  var source = nomeFile+'?mode=ajax&link='+nomeLink;
  if (idPadre != '')
  {
    new Ajax.Autocompleter
    (
      idTarget+'Testo',
      idTarget+'Update',
      source,{minChars:2,callback: function(element,entry){return entry="&id="+$(idPadre).value}}
    );
  }
  else
  {
    new Ajax.Autocompleter(idTarget+'Testo',idTarget+'Update',source,{minChars:2});
  }
}

// Aggiungo l'ajax ai check
function addChecker(selettore,field)
{
  $$(selettore).each(function(img)
  {
    Event.observe(img, 'click', updateCheck.bindAsEventListener(img,field));
  });
}
function updateCheck(e)
{
	var data = $A(arguments);
	data.shift();

	// nomeFile viene valorizzato nell'header e contiene il nome del file in corso
	var source = nomeFile+'?mode=check&id='+this.id+'&field='+data[0];
  var img = this;
	// Aggiorno il link
	new Ajax.Request
  (
    source,
    {
      onSuccess: function(transport){img.setAttribute('src',transport.responseText);},
      asynchronous:true
    }
  );
}

// Hide / Show
function hideShow(idTarget,idLink,testoAperto,testoChiuso)
{
	var changeText = true;
	if ((testoAperto == '') && (testoChiuso == ''))
		changeText = false;

  if ($(idTarget).style.display == "none")
	{
		new Effect.BlindDown($(idTarget),{duration:0.7});
		if (changeText)
    {
      $(idLink).textContent = testoAperto;
			$(idLink).innerHTML = testoAperto;
    }
	}
	else
	{
  	new Effect.BlindUp($(idTarget),{duration:0.4});
		if (changeText)
    {
      // Per IE textContent ( con innerHTML non cambia il testo.. )
      $(idLink).textContent = testoChiuso;
      // Per FF, innerHTML ( in caso di codice html firefox altrimenti codificherebbe il testo passato )
			$(idLink).innerHTML = testoChiuso;
    }
	}
	return false;
}


function addCalendar()
{
  $$('.cal').each(function(img)
  {
    var idCampo = img.id;
    idCampo = idCampo.substr(idCampo.indexOf('-')+1);
    Event.observe(img, 'click', imgCalendar_Click.bindAsEventListener(this, idCampo));
  });
	if ($("calendar-container"))
		new Draggable($("calendar-container"));
}

function addCalendarChange()
{
  if ($('cal-inizio'))
    Event.observe($('cal-inizio'), 'click', imgCalendar_Click.bindAsEventListener(this,$('inizio'),changeDate));
}

function changeDate(data)
{
  $('inizio').value = data.getDate()+"-"+(data.getMonth()+1)+"-"+data.getFullYear();
  document.filtro.submit();
}

function onLoad()
{
  addCalendarChange();
	addCalendar();
	SortableTable.load();

	Object.extend(Element.Methods, {
		sendData: function(form, options)
		{
			form = $(form), options = Object.clone(options || { });

			var params = options.parameters, action = form.readAttribute('action') || '';
			if (action.blank()) action = window.location.href;
			options.parameters = form.serialize(true);

			if (params) {
				if (Object.isString(params)) params = params.toQueryParams();
				Object.extend(options.parameters, params);
			}

			if (form.hasAttribute('method') && !options.method)
				options.method = form.method;
			if (options.action != null)
				action = options.action;

			return new Ajax.Request(action, options);
		}
	});
	Element.addMethods();
}


var AjaxInit = {
	onload : function() {
		AjaxInit.actions.each(function(func) {
			func();
		})
	},
	actions : $A([]),
	addOnLoad : function() {
		for(var x = 0; x < arguments.length; x++) {
			var func = arguments[x];
			if(!func || typeof func != 'function') continue;
			AjaxInit.actions.push(func);
		}
	}
}

function getLoader()
{
	var load = new Element('img', { src: '/template/img/loader.gif'});
	return load;
}

//if(FastInit)
//  FastInit.addOnLoad(onLoad);
//else
  Event.observe(window, 'load', onLoad);

