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;
}

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;
}

 function clear(combobox)
 {
  for (var i = (combobox.options.length-1); i >= 0; i--)
  {
    combobox.options[i]=null;
  }
 }


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);
    }
  }
}

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

function displayLoading()
{
	document.body.style.cursor='wait';
	/*
	var newdiv = document.createElement('div');
	var id = 'ajaxLoading';
	newdiv.setAttribute('id',id);
	newdiv.setAttribute("class","ajaxloading");
	//newdiv.innerHTML = '<div style="width:75;height:75px;background-color:#fff;border:1px solid #aaa;"><img src="img/loading.gif" /></div>';
	newdiv.innerHTML = '<img src="img/loading.gif" />';
	document.body.appendChild(newdiv);
	return id;
	*/
}

function aggiornaComboAjax2(combo, ajaxFile, conditions)
{
 combo.disabled = true;


 displayLoading();
 new Ajax.Request(
  ajaxFile,
  {
   method:'post',
   parameters: conditions,
      asynchronous: false,
   onSuccess: function(transport)
   {


    var 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 displayIdle(id)
{
	document.body.style.cursor='default';
	/*
	var element = document.getElementById(id);
	document.body.removeChild(element);
	*/
}

function aggiornaComboAjax(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(){ alert('Errore ajax') }
		  }
		);
	}

}

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

function aggiornaProfiloFormativo(profiliFormativi, settori)
{
	aggiornaComboAjax(profiliFormativi, settori, 'inc/ajax_aggiorna_profili_formativi.php', 'settore=' + settori.value);
}

