var http_request;

function cambiaProvincia(e)
{
 if(!e) var e=window.event;
 var n;
 if(e.target) n=e.target;
 else if(e.srcElement) n=e.srcElement;
 try
 {
  if(n.nodeType!=1) n=n.parentNode;
  if(!n || n.nodeType!=1) return false;
 } catch(e) { return false; }
 if(n.nodeName.toLowerCase()=="option" || n.nodeName=="html:option") n=n.parentNode;
 if(n.nodeName.toLowerCase()!="select" && n.nodeName!="html:select") return false;
 var est=n.id.substr(9);
 if(window.XMLHttpRequest)
 {
  http_request=new XMLHttpRequest();
  if(http_request.overrideMimeType) http_request.overrideMimeType("text/xml");
 }
 else if(window.ActiveXObject)
 {
  try { http_request=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e)
  {
   try { http_request=new ActiveXObject("Microsoft.XMLHTTP"); }
   catch(e) {}
  }
 }
 if(!http_request)
 {
  while(n=n.parentNode)
   if(n.nodeName.toLowerCase()=="body" || n.nodeName=="html:body") break;
   else if(n.nodeName.toLowerCase()=="form" || n.nodeName=="html:form") { n.submit(); break; }
  return false;
 }
 var prov=document.getElementById("provincia"+est);
 if(!prov) return false;
 try { document.getElementById("vecchiaprov"+est).value=prov.value; } catch(e) {}
 var comune=document.getElementById("comune"+est);
 var td;
 if(comune) { td=comune.parentNode; td.removeChild(comune); }
 else try { td=prov.parentNode.nextSibling; while(td.nodeType==3) td=td.nextSibling; } catch(e) {}
 if(!td) return false;
 comune=document.createElement("select");
 comune.setAttribute("id","comune"+est);
 comune.setAttribute("name","comune"+est);
 if(!prov.value)
 {
  var voce=document.createElement("option");
  voce.setAttribute("value","");
  voce.appendChild(document.createTextNode("Selezionare prima la provincia"));
  comune.appendChild(voce);
  td.appendChild(comune);
  return false;
 }
 var msg=document.getElementById("msgajax");
 if(msg) msg.parentNode.removeChild(msg);
 msg=document.createElement("div");
 msg.setAttribute("id","msgajax");
 msg.appendChild(document.createTextNode("Caricamento in corso..."));
 td.appendChild(msg);
 var stileTD,displayTD,tdprov=prov.parentNode;
 try { stileTD=((typeof tdprov.currentStyle!="undefined")?tdprov.currentStyle:document.defaultView.getComputedStyle(tdprov,null)); displayTD=stileTD["display"] } catch(e) { displayTD="block"; }
 if(td.style.display && td.style.display=="none") td.style.display=displayTD;
 msg=document.getElementById("msgajax");
 http_request.onreadystatechange=function()
 {
  if(http_request.readyState==4)
  {
   if(http_request.status==200)
   {
    var xmldoc=http_request.responseXML;
    var root=xmldoc.getElementsByTagName("root").item(0);
    var ris=root.firstChild;
    var voce=document.createElement("option");
    voce.setAttribute("value","");
    voce.appendChild(document.createTextNode("Selezionare"));
    comune.appendChild(voce);
    var numVoci=0;
    while(ris)
    {
     var opt,val;
     if(ris.nodeType==1 && ris.tagName.toLowerCase()=="c")
     {
      opt=ris.getAttribute("id");
      if(ris.firstChild && ris.firstChild.nodeType==3) val=ris.firstChild.nodeValue;
      else val="";
      if(opt && val)
      {
       voce=document.createElement("option");
       voce.setAttribute("value",opt);
       voce.appendChild(document.createTextNode(val));
       comune.appendChild(voce);
       numVoci++;
      }
     }
     ris=ris.nextSibling;
    }
    if(numVoci>0)
     td.removeChild(msg);
    else
    {
     if(!root.hasChildNodes() || !root.firstChild.nodeValue)
      msg.firstChild.nodeValue="Errore: impossibile caricare i comuni della provincia "+document.getElementById("provincia"+est).value;
     else
      msg.firstChild.nodeValue="Errore: "+root.firstChild.nodeValue;
     comune.setAttribute("class","nascosto");
     comune.className="nascosto";
    }
   }
   else
    msg.firstChild.nodeValue="Errore: "+http_request.status;
   td.appendChild(comune);
  }
  else
   return;
 };
 http_request.open("GET","/comuni.php/"+document.getElementById("provincia"+est).value,true);
 http_request.send(null);
}

function cambiaNazione(e)
{
 if(!e) var e=window.event;
 var n;
 if(e.target) n=e.target;
 else if(e.srcElement) n=e.srcElement;
 try
 {
  if(n.nodeType!=1) n=n.parentNode;
  if(!n || n.nodeType!=1) return false;
 } catch(e) { return false; }
 if(n.nodeName.toLowerCase()=="option" || n.nodeName=="html:option") n=n.parentNode;
 if(n.nodeName.toLowerCase()!="select" && n.nodeName!="html:select") return false;
 var est=n.id.substr(7);

 var nazione=document.getElementById("nazione"+est);
 if(!nazione) return false;
 var comune=document.getElementById("comune"+est);
 if(!comune) return false;
 var td=comune.parentNode;
 var stileTD,displayTD;
 try { stileTD=((typeof td.currentStyle!="undefined")?td.currentStyle:document.defaultView.getComputedStyle(td,null)); displayTD=stileTD["display"] } catch(e) { displayTD="block"; }
 var label=td.getElementsByTagName("label").item(0).firstChild;
 var provincia=document.getElementById("provincia"+est);
 var frazione=document.getElementById("frazione"+est);
 try { document.getElementById("vecchianaz"+est).value=nazione.value; } catch(e) {}
 if(nazione.value=="IT")
 {
  if(provincia) { provincia.parentNode.style.display=displayTD; provincia.selectedIndex=0; }
  if(frazione) frazione.parentNode.style.display=displayTD;
  td.setAttribute("colspan","1");
  td.removeChild(comune);
  comune=document.createElement("select");
  comune.setAttribute("id","comune"+est);
  comune.setAttribute("name","comune"+est);
  var voce=document.createElement("option");
  voce.setAttribute("value","");
  voce.appendChild(document.createTextNode("Selezionare prima la provincia"));
  comune.appendChild(voce);
  td.appendChild(comune);
  label.nodeValue=label.nodeValue.replace(/Città/,"Comune");
  if(est=="_fisc")
   try
   {
//     if(defCampi.campi.partitaiva) defCampi.campi.partitaiva.obbligatorio=true;
    if(defCampi.campi.codicefiscale) defCampi.campi.codicefiscale.obbligatorio=true;
//     if(document.getElementById("partitaiva")) document.getElementById("partitaiva").parentNode.style.display=displayTD;
    if(document.getElementById("codicefiscale")) document.getElementById("codicefiscale").parentNode.style.display=displayTD;
   } catch(e) {}
 }
 else
 {
  if(provincia) provincia.parentNode.style.display="none";
  if(frazione) frazione.parentNode.style.display="none";
  td.setAttribute("colspan","3");
  td.removeChild(comune);
  comune=document.createElement("input");
  comune.setAttribute("type","text");
  comune.setAttribute("id","comune"+est);
  comune.setAttribute("name","comune"+est);
  comune.setAttribute("value","");
  td.appendChild(comune);
  label.nodeValue=label.nodeValue.replace(/Comune/,"Città");
  if(est=="_fisc")
  {
//    try { if(defCampi.campi.partitaiva) defCampi.campi.partitaiva.obbligatorio=false; } catch(e) { }
   try { if(defCampi.campi.codicefiscale) defCampi.campi.codicefiscale.obbligatorio=false; } catch(e) { }
//    if(document.getElementById("partitaiva")) document.getElementById("partitaiva").parentNode.style.display="none";
   if(document.getElementById("codicefiscale")) document.getElementById("codicefiscale").parentNode.style.display="none";
  }
 }
}

