// JavaScript Document
// Caricamento moduli
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
    try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (err2) {
        try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (err3) {
            req = false;
        }
    }
}
return req;
} 

function getRequestBody(oForm)// Funzione per la creazione della stringa di nomevariabile-valore.
{
	var aParams = new Array(); // creo un array vuoto
	for (var i=0 ; i < oForm.elements.length; i++) // eseguo la codifica come URL degli elementi del form
	{
		var sParam = encodeURIComponent(oForm.elements[i].name);// creo una variabile che contiene il nome dell'elemento del form
		sParam += "=";// aggiungo alla variabile sParam "="
		if(oForm.elements[i].type=="checkbox")
			sParam += (oForm.elements[i].checked) ? oForm.elements[i].value : false;
		else
			sParam += encodeURIComponent(oForm.elements[i].value);// aggiungo alla variabile sParam il valore dell'elemento del form
		aParams.push(sParam);// aggiungo all'array aParams la stringa sParam che conterrą l'accoppiata nome=valore push aggiunge un elemento all'array
	}
	return aParams.join("&");// il join mi permette di ottenere un'unica stringa contenete la coppia nome-valore concatenate da &
}

function compilazione_pagina(form, iddiv){
	var objForm=document.getElementById(form);

	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.open('post', objForm.action, false);
	xmlHttp.setRequestHeader("Content-Type", objForm.encoding);
	xmlHttp.send(getRequestBody(objForm));
	if(xmlHttp.status==200 || xmlHttp.status==0){
		var risultatoTxt = xmlHttp.responseText;
		var divinner=document.getElementById(iddiv);
		divinner.innerHTML=risultatoTxt;
	} else {
		alert("comunicazione fallita (codice: "+xmlHttp.status+"! Motivo: "+xmlHttp.statusText+")");
	}
}

function populate(selectName, id, idsel)
{
   
   var xmlHttp = getXMLHTTPRequest();
	xmlHttp.open('get', 'formmod.php?id='+id+'&div='+selectName, false);
	xmlHttp.send(null);
	if(xmlHttp.status==200 || xmlHttp.status==0){
		var risultatoTxt = xmlHttp.responseText;
		str_nome_val=risultatoTxt.split('*----*');
		testo=str_nome_val[0].split('.;.');
		valore=str_nome_val[1].split('.;.');
		//alert(risultatoTxt);
	} else {
	alert('errore');
	}
	var sel = document.getElementsByName(selectName)[0];
   var tutti = Element.descendants(sel);
   
   for(i = 0; i < tutti.length; i++)
   {      
      sel.remove(tutti[i]);
   }
   for(i = 0; i < testo.length; i++)
   {
      var opt = document.createElement('option');
	  opt.value = valore[i];
      opt.appendChild(document.createTextNode(testo[i]));
      sel.appendChild(opt);
	}
	if(idsel!=''){
		sel.value=idsel;
	}
	
}

