function salvaDatiRichiestaInfo() {
	var form = $("formRichiestaInfo");
	var div = $("RichiestaInfo");
	var msgErroreContainer = div.down(".divErrore");

	$A($$(".tcontent")).each(function(item) {
		item.hide();
	});

	$A($$(".tinner")).each(function(item) {
		item.style.backgroundImage = "";
	});

	// Ripulisce eventuali errori precedenti:
	msgErroreContainer.hide().update();

	new Ajax.Request("/moduli/annuncio/richiestaInfo.php", {
		method: 'post',
		postBody: form.serialize(),
		evalJSON: true,
		onComplete: function() {
			$A($$(".tcontent")).each(function(item) {
				item.show();
			});
		},
		onSuccess: function(r) {
			var data = r.responseJSON;

			if (data.errore) {
				msgErroreContainer.show();
				data.msgErrore.each(function(msg) {
					var li = new Element("li").update(msg);
					msgErroreContainer.insert(li);
				});
			} else {
				form.update(data.msgOk);
			}
		}
	});

	return false;
}

var __def_nazione_value = 0;
var __def_provincia_value = 0;

function getSelectProvincia() {
	var inNazione = parseInt($F("inNazione"));

	if (inNazione != __def_nazione_value) {
		__def_nazione_value = inNazione;

		$("inLocalitaContainer").update();
		$("inProvinciaContainer").update();

		new Ajax.Updater("inProvinciaContainer", "/moduli/annuncio/richiestaInfo.php", {
			postBody: "select=1&nazione_id=" + inNazione
		});
	}
}

function getSelectLocalita(provincia_id) {
	var inProvincia = parseInt($F("inProvincia"));

	if (inProvincia != __def_provincia_value) {
		__def_provincia_value = inProvincia;

		$("inLocalitaContainer").update();

		new Ajax.Updater("inLocalitaContainer", "/moduli/annuncio/richiestaInfo.php", {
			postBody: "select=2&provincia_id=" + inProvincia
		});
	}
}

document.observe('emulated:submit', _submitEvent);
document.observe('submit', _submitEvent);

document.observe('emulated:change', _changeEvent);
document.observe('change', _changeEvent);

function _submitEvent(e) {
	if (e.element().match('#formRichiestaInfo')) {
		salvaDatiRichiestaInfo();
	}
}

function _changeEvent(e) {
	if (e.element().match('#inNazione')) {
		getSelectProvincia();
	} else if (e.element().match('#inProvincia')) {
		getSelectLocalita();
	}
}


(function() {
  // Technique from Juriy Zaytsev
  // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
  function isEventSupported(eventName) {
    var el = document.createElement('div');
    eventName = 'on' + eventName;
    var isSupported = (eventName in el);
    if (!isSupported) {
      el.setAttribute(eventName, 'return;');
      isSupported = typeof el[eventName] == 'function';
    }
    el = null;
    return isSupported;
  }

  function isForm(element) {
    return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
  }

  function isInput(element) {
    if (Object.isElement(element)) {
      var name = element.nodeName.toUpperCase()
      return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
    }
    else return false
  }

  var submitBubbles = isEventSupported('submit'),
      changeBubbles = isEventSupported('change');

  if (!submitBubbles || !changeBubbles) {
    // augment the Event.Handler class to observe custom events when needed
    Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
      function(init, element, eventName, selector, callback) {
        init(element, eventName, selector, callback)
        // is the handler being attached to an element that doesn't support this event?
        if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
             (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
          // "submit" => "emulated:submit"
          this.eventName = 'emulated:' + this.eventName
        }
      }
    )
  }

  if (!submitBubbles) {
    // discover forms on the page by observing focus events which always bubble
    document.on('focusin', 'form', function(focusEvent, form) {
      // special handler for the real "submit" event (one-time operation)
      if (!form.retrieve('emulated:submit')) {
        form.on('submit', function(submitEvent) {
          var emulated = form.fire('emulated:submit', submitEvent, true)
          // if custom event received preventDefault, cancel the real one too
          if (emulated.returnValue === false) submitEvent.preventDefault()
        })
        form.store('emulated:submit', true)
      }
    })
  }

  if (!changeBubbles) {
    // discover form inputs on the page
    document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
      // special handler for real "change" events
      if (!input.retrieve('emulated:change')) {
        input.on('change', function(changeEvent) {
          input.fire('emulated:change', changeEvent, true)
        })
        input.store('emulated:change', true)
      }
    })
  }
})();


