/**
 * Ricava il radiobutton selezionato di un gruppo specificato
 * @param {Object} radioObj
 * @param {Object} value
 */
function getRadioObject(radioObj, value)
{
  if( radioObj == null)
    return null;

  var radioLength = radioObj.length;
  if(radioLength == undefined)
      return radioObj;
  else
  {
    for(var i = 0; i < radioLength; i++)
    {
      if(radioObj[i].value == value)
      {
        return radioObj[i];
      }
    }
  }

  return null;
}

/**
 * Ricava il valore del radiobutton selezionato di un gruppo specificato
 * @param {Object} radioObj
 */
function getCheckedValue(radioObj) {
  if( radioObj == null)
    return null;

  var radioLength = radioObj.length;
  if(radioLength == undefined)
    if(radioObj.checked)
      return radioObj.value;
    else
      return null;
  for(var i = 0; i < radioLength; i++) {
    if(radioObj[i].checked) {
      return radioObj[i].value;
    }
  }
  return null;
}

/**
 * Interpreta un XML per riempire un Combo
 * @param {Object} transport
 * @param {Object} combobox
 * @deprecated
 */
function updateCombo(transport, combobox)
{
  var rispostaXML = transport.responseText || "no response text";

	try
	{

	  if (document.implementation && document.implementation.createDocument)
	  {
		  parser = new DOMParser();
		  xmlDoc = parser.parseFromString(rispostaXML, "text/xml");
	  }
	  else if (window.ActiveXObject)
	  {
		  xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
		  xmlDoc.loadXML(rispostaXML);
	  }

	  var doc = xmlDoc.documentElement;
	  var comuni = doc.getElementsByTagName('item');

	  // Svuota il dropbox dei comuni

    clear(combobox);

	  // Popola i comuni
	  for(var i=0; i<comuni.length; i++)
	  {
		var comune = comuni[i];

		var child = comune.childNodes[0];

		var valore = comune.childNodes[0].childNodes[0].nodeValue;
		var caption = comune.childNodes[1].childNodes[0].nodeValue;

		var elOptNew = document.createElement('option');
		elOptNew.text = caption;
		elOptNew.value = valore;
		var elOptOld = combobox.options[combobox.options.lenght-1];
		try
		{
			combobox.add(elOptNew, elOptOld); // standards compliant; non funziona con IE
		}
		catch(ex)
		{
			combobox.add(elOptNew, comboComuni.selectedIndex); // per compatibilit� con IE
		}
	  }
	}
	catch(ex)
	{
	  alert('errore:\n\n' + ex.message);
	}

}

function enable(el, state)
{
  el.style.display = (state?"block":"none");
  el.style.top = '-100px';
  return;

  try
  {
    el.disabled = !state;
  }
  catch(E)
  {
  }
  if (el.childNodes && el.childNodes.length > 0)
  {
    for (var x = 0; x < el.childNodes.length; x++)
    {
      enable(el.childNodes[x], state);
    }
  }
}

/**
 *
 * @param {Object} combo
 * @param {Object} fromcombo
 * @param {Object} ajaxFile
 * @param {Object} conditions
 * @deprecated
 */
function aggiornaComboAjax(combo, fromcombo, ajaxFile, conditions)
{
	aggiornaComboAjax(combo, fromcombo, ajaxFile, conditions, false)
}

function aggiornaComboAjax_2(combo, fromcombo, ajaxFile, conditions)
{
	aggiornaComboAjax(combo, fromcombo, ajaxFile, conditions, false)
}

/**
 * Imposta il cursore sulla clessidra durante le operazioni Ajax
 */
function displayLoading()
{
	document.body.style.cursor='wait';
}

/**
 * Imposta il cursore come normale dopo le operazioni Ajax
 */
function displayIdle()
{
	document.body.style.cursor='default';
}



/**
 * Svuota un combobox
 * @param {Object} combobox
 */
function clear(combobox)
{
	for (var i = (combobox.options.length-1); i >= 0; i--)
		combobox.options[i]=null;

}


/**
 * Popola un combo con i valori restituiti da un file PHP chiamato in Ajax. I valori restituiti devono essere un array
 * con formato array(array('value', 'caption'))
 * @param {Object} combo Combo da popolare
 * @param {string} ajaxFile
 * @param {array} conditions

 */
function aggiornaComboAjax(combo, ajaxFile, conditions)
{
	combo.disabled = true;
  displayLoading();
 	new Ajax.Request(
		ajaxFile,
		{
			method:'post',
			parameters: conditions,
      asynchronous: false,
			onSuccess: function(transport)
			{
				items = transport.responseText.evalJSON();
				// Riempi il combo

				clear(combo);
                var len = items.length;

				for(var i=0; i<len; i++)
				{
					var elOptNew = document.createElement('option');
					elOptNew.text = items[i]['caption'];
					elOptNew.value = items[i]['value'];
					var elOptOld = combo.options[combo.options.lenght-1];
					try
					{
						combo.add(elOptNew, elOptOld); // standards compliant; non funziona con IE
					}
					catch(ex)
					{
						combo.add(elOptNew, elOptOld.selectedIndex); // per compatibilit� con IE
					}
				}

				combo.disabled = false;
				displayIdle();
			}
			,
			onFailure: function(){
				alert('Errore ajax')
			}
		}
	);
}




function aggiornaComboAjax_2(combo, fromcombo, ajaxFile, conditions, zeroIsValid)
{
  combo.disabled = true;
  clear(combo);
  if( fromcombo == null || !( fromcombo.value == 0 && zeroIsValid == false ))
  {

	displayLoading();
	new Ajax.Request(
		  ajaxFile + '?' + conditions,
		  {
			method:'get',
			onSuccess: function(transport)
			{
				updateCombo(transport, combo);
				combo.disabled = false;
				displayIdle();
			}
			,
			onFailure: function(transport){
				alert("Errore ajax:\r\n\r\n" + transport.status);
			}
		  }
		);
	}

}



function aggiornaComuni_2(comuni, province)
{
  aggiornaComboAjax_2(comuni, province, 'inc/ajax_aggiorna_comuni_cimp.php', 'provincia=' + province.value);
}

function aggiornaComuni(comuni, province)
{
  aggiornaComboAjax(comuni, province, 'inc/ajax_aggiorna_comuni.php', 'provincia=' + province.value);
}

function toggleAjax(divid, ajax, id) {
	$(divid).toggle();
	new Ajax.Updater(divid, ajax, { asynchronous: false, parameters: {id: id}, evalScripts: true});
}

function hideAll(elements)
{
	if(elements)
    {
		elements.each(function(son)
        {
						//alert(son.tagName);
						//Effect.toggle(son, 'blind', { duration: .1 });
						//son.addClassName('hide');
						son.hide();
		}
		)
	}
	else
    {
		return null;
	}
}




