/*======== crea oggetto XMLHttpRequest in base al tipo di browser ============*/
function getXmlHttp(){
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
  		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		try
      	{
      		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      	}
    		catch (e)
      	{
      		alert("Your browser does not support AJAX!");
      		xmlHttp=false;
      	}
    	}
	}
	return xmlHttp;
}
/*======== aggiunge riga in tabella per inserimento elementi in select =======*/
function insert (id){
	var select = document.getElementById(id);
	select.disabled = true;
	numero_riga = ((select.parentNode).parentNode).rowIndex;
	((select.parentNode).parentNode).cells[2].innerHTML = "";
	//var x=document.getElementById('tabella_form').insertRow(numero_riga+1);
	var x=(((select.parentNode).parentNode).parentNode).insertRow(numero_riga+1);
	var uno=x.insertCell(0);
	var due=x.insertCell(1);
	var tre=x.insertCell(2);
	due.innerHTML='<input type="text" name="a_'+id+'" id="a_'+id+'">';
	tre.innerHTML='<span class="link" onClick="ajaxInsert(\'a_'+id+'\');">Salva</span>';
	input = document.getElementById('a_'+id);
	input.focus();
}

/*======== post al server per aggiungere una option in database ==============*/
function ajaxInsert(id){
	var params = id+'='+document.getElementById(id).value
	xmlHttp = getXmlHttp();
	
  
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 && xmlHttp.status == 200)
		{
  			// Get the data from the server's response
			var riga = ((document.getElementById(id)).parentNode).parentNode
			if(isNaN(xmlHttp.responseText))
			{
				if(!document.getElementById('errorline')){
				riga = riga.parentNode.insertRow(riga.rowIndex)
				riga.insertCell(0);
				riga.id = "errorline";
				cella = riga.insertCell(1);
				riga.insertCell(2);
				cella.innerHTML = xmlHttp.responseText;
				}
				else{
					riga = document.getElementById('errorline');
					riga.cells[1].innerHTML = xmlHttp.responseText;
				}
				
			}
			else
			{
				if(errorline = document.getElementById('errorline'))
				{
					errorline.parentNode.deleteRow(errorline.rowIndex);
				}
				newelement = document.getElementById(id).value;
				(riga.parentNode).deleteRow(riga.rowIndex);

				id = id.replace(/a_/,"");
				var select = document.getElementById(id);
				select.disabled = false;
				
				var option=document.createElement('option');
				option.text=newelement;
				option.value= parseInt(xmlHttp.responseText);
				select.add(option,null);
				option.selected = true;
				select.parentNode.parentNode.cells[2].innerHTML = '<span class="link" onClick="insert(\''+id+'\');">Aggiungi</span>';
			}
  		}
  	}
	post('ajax_select.php',params,xmlHttp);
}

/*======== send with post ==================================================*/
function post(address,content,xmlHttp)
{
	
	xmlHttp.open("POST",address,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", content.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(content);
}
