var xmlHttp = getXmlHttpObject();

function loadList(tb, id){
xmlHttp.open('GET', 'gestioeventi.php?tipo='+tb+'&idtipo='+id, true);
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.send(null);
}

function loadList2(tb, id){
var sector  = getSelected(document.getElementById('settore'));
xmlHttp.open('GET', 'gestioeventi.php?tipo='+tb+'&idtipo='+id+'&settore='+sector, true);
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.send(null);
}

function addOption(select, value, text) {
	//Aggiunge un elemento <option> ad una lista <select>
	var option = document.createElement("option");
	option.value = value,
	option.text = text;
	try {
		select.add(option, null);
	} catch(e) {
		//Per Internet Explorer
		select.add(option);
	}
}
function getSelected(select) {
	//Ritorna il valore dell'elemento <option> selezionato in una lista
	return select.options[select.selectedIndex].value;
}
function stateChanged() {
	if(xmlHttp.readyState == 4) {
		//Stato OK
		if (xmlHttp.status == 200) {
			var resp = xmlHttp.responseText;
			
			if(resp) {
				
				
				//Le coppie di valori nella striga di risposta sono separate da ;
				var values = resp.split(';');
				//Il primo elemento � l'ID della lista.
				var listId = values.shift();
				var select = document.getElementById(listId);
				//Elimina i valori precedenti
				while (select.options.length) {
					select.remove(0);
				} 
				
				
					addOption (select, 0, '-- Selezionare --');
			
				var limit = values.length;
				
				for(i=0; i < limit; i++) {
					var pair = values[i].split('|');
					//aggiunge un elemento <option>
					addOption(select, pair[0], pair[1]);
				}
			}
		} else {
			alert(xmlHttp.responseText);
		}
	}
}

function getXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}


function addArgument( tipo , id ){

xmlHttp.open('GET', 'gestioeventi.php?s='+tipo+'&idargomento='+id, true);
xmlHttp.onreadystatechange = addRequest;
xmlHttp.send(null);

}

function addRequest( ) {

if(xmlHttp.readyState == 4) {
		//Stato OK
		if (xmlHttp.status == 200) {
			var resp = xmlHttp.responseText;
			
			if(resp) {
			
				var valore = resp.split(";");
				var limit = valore.length;
				
				for(i=0; i < limit; i++) {
					var pair = valore[i].split('|');
					
					switch ( pair[2] )
					{
						case "newscorrelate" :
						var id = '#listaArgomentiNews';
						break;
						case "schedecorrelate" :
						var id = '#listaArgomentiScheda';
						break;
						case "prodotticorrelati" :
						var id = '#listaArgomentiCatalogo';
						break;
					}
					var html = "<span id='"+pair[0]+"'><b style='cursor:pointer;' onclick='deleteArg("+pair[0]+")'>X</b> |"+pair[1]+"<input type='checkbox' name='"+pair[2]+"[]' value='"+pair[0]+"' checked='checked' style='display: none;' /></span><br />";
						
					$(id).append(html);
								
			}
		}
		else {
			alert(xmlHttp.responseText);
		}
}

}

}


function deleteArg ( arg ) {

	$("#"+arg).empty();

}




function getMessageUpload (){
	if(xmlHttp.readyState == 4) {
		//Stato OK
		if (xmlHttp.status == 200) {
			var resp = xmlHttp.responseText;
			if(resp) {
				window.frames[0].value = resp;
			}
		}
	}
}