

  function NewXmlDoc(rootTagName, namespaceURL) {
    if (!rootTagName) rootTagName = "";
    if (!namespaceURL) namespaceURL = "";

    if (document.implementation && document.implementation.createDocument) {
        // This is the W3C standard way to do it
        return document.implementation.createDocument(namespaceURL,
                       rootTagName, null);
    }
    else { // This is the IE way to do it
        // Create an empty document as an ActiveX object
        // If there is no root element, this is all we have to do
        var doc = new ActiveXObject("MSXML2.DOMDocument");

        // If there is a root tag, initialize the document
        if (rootTagName) {
            // Look for a namespace prefix
            var prefix = "";
            var tagname = rootTagName;
            var p = rootTagName.indexOf(':');
            if (p != -1) {
                prefix = rootTagName.substring(0, p);
                tagname = rootTagName.substring(p+1);
            }

            // If we have a namespace, we must have a namespace prefix
            // If we don't have a namespace, we discard any prefix
            if (namespaceURL) {
                if (!prefix) prefix = "a0"; // What Firefox uses
            }
            else prefix = "";

            // Create the root element (with optional namespace) as a
            // string of text
            var text = "<" + (prefix?(prefix+":"):"") + tagname +
                (namespaceURL
                 ?(" xmlns:" + prefix + '="' + namespaceURL +'"')
                 :"") +
                "/>";
            // And parse that text into the empty document
            doc.loadXML(text);
        }
        return doc;
    }
};




function CambioReg(strTipoOrig,strSourceName,strTargetName,strTargetVal){
	var strValSrc, ctrl,i,opt,strValTrg,strFiltro,strUrl,strXml,xmlDoc,OptionList,e,parser,strTmp;
	

 strValSrc=document.getElementById(strSourceName).value;	
 
	ctrl=document.getElementById(strTargetName)
	
 strValTrg=ctrl.value
 //if (strValTrg=='') {strValTrg=0}
 
	
	//se ho sezionato il 'nessuna regione' azzero anche il comune
	if (strFiltro=='')
	 {//tolgo tutte le option tranne quelle 'nessun valore'
	 	for (i=ctrl.length-1;i>=0;i--)
	 	{if (ctrl.options[i].value !='') { ctrl.options[i]=null}}
	 }
	else
		{
		 strUrl='http://' +document.domain  +'/GetDatiGeo2011.asp?Tinp=' + strTipoOrig+ '&ValFil=' + strValSrc + '&CurVal=' + strTargetVal;
		 //alert(strUrl);
   GetUrl2(strUrl,ctrl);
  
  
	}
}


function AggiornaTendina(strXml,ctrlTarget){

			//l'URL mi doverebbe tornare ul elenco di tag <options>

			
		 //Rimpiazzo le options del target con quelle ricevuto,preservo comunque la prima option che è quella di  "nessun valore"
		 if (ctrlTarget.length>0){strHtml='<option value="0">' + ctrlTarget.options[0].text + '</option>' }

		 //aggiungo un tag di root per trasformare la stringa in un documento xml (che ammete un solo tag di root)
		 	strXml='<options>' + strHtml + strXml + '</options>';

    
    //'cancello tutte le opzioni della tendina e poi aggiungo quelle nuove (a cui è preposta l'opzione '0')
    for (i=ctrlTarget.length-1;i>=0;i--)
	 	 { ctrlTarget.options[i]=null}
	   
	 
    
   //carico l'xml in un oggetto xml
		 try //Internet Explorer
	  {
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  xmlDoc.async="false";
		  xmlDoc.loadXML(strXml);

		  OptionList=xmlDoc.selectNodes("options/option");
    
    
   
		  for (x=0;x<OptionList.length;x++)
		  {
		   ctrlTarget.options[ctrlTarget.length]= new Option(OptionList(x).text,OptionList(x).getAttribute('value'),false,false);
		   //if (OptionList(x).getAttribute('value')=='Milano'){ctrlTarget.options[ctrlTarget.length].selected=true;}
		  }

	  }
			catch(e)
	  {

		  try //Firefox, Mozilla, Opera, etc.
		    {
							parser=new DOMParser();
							xmlDoc=parser.parseFromString(strXml,"text/xml");
							OptionList = xmlDoc.evaluate("options/option", xmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

		     for (x=0;x<OptionList.snapshotLength ;x++)
					  {
					  	
					  	strTmp=OptionList.snapshotItem(x).textContent;
					  	strValTmp=OptionList.snapshotItem(x).getAttribute('value');
					  	
					   ctrlTarget.options[ctrlTarget.length]= new Option(strTmp,strValTmp,false,false);
					    //if (strValTmp=='Milano'){ctrlTarget.options[ctrlTarget.length].selected=true;}
					  }
		    }
		  catch(e) {alert(e.message)}
	  }	
	  
	 }

function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}

	
	
function GetUrl2(strURL,ctrlTarget)
{

	 var xmlhttp=false;
	 	
	 
	/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/

			if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
				try {
					xmlhttp = new XMLHttpRequest();
				} catch (e) {
					xmlhttp=false;
				}
			}
			if (!xmlhttp && window.createRequest) {
				try {
					xmlhttp = window.createRequest();
				} catch (e) {
					xmlhttp=false;
				}
			}

		xmlhttp.open("GET", strURL,true);
  xmlhttp.onreadystatechange=function() {
  
  if (xmlhttp.readyState==4) {
    AggiornaTendina(xmlhttp.responseText,ctrlTarget);
  }
  
 }
  
 xmlhttp.send(null);
 return ;
}

function GestClear(strTipoInp){
	
	switch (strTipoInp){
		case 'R':
		 ClearCombo('idpr',1);
		 ClearCombo('luogo',1);
		break;
		case 'P':
		  	 ClearCombo('luogo',1);
		break;
		case 'M':
		   ClearCombo('field',1); 	 
		break;
	}
}

function ClearCombo(strNome, strSalvaZero){
	var ctrl;
	ctrl=document.getElementById(strNome);
	for (i=ctrl.length-1;i>=0;i--){
	   //se strSalvaZero=1 non elimina le option con valore 0 o ''
	 		if ( (ctrl.options[i].value !='0' && ctrl.options[i].value !='') || strSalvaZero !=1 ) { ctrl.options[i]=null}
	}
	
}

/*
function ClearCombo(strNome, strSalvaZero){
	var ctrl;
	ctrl=document.getElementById(strNome);
	
	for (i=ctrl.length-1;i>=0;i--){
	 		if ( ctrl.options[i].value !='0' || strSalvaZero !=1 ) { ctrl.options[i]=null}
	}
	
}
*/

function Selezionavalore(ctrSelect,strVal)
{
	var x;

	for(x=0;x<ctrSelect.length;x++)
	{
		if (ctrSelect.options[x]==strVal)
		{
			ctrSelect.selectedIndex=x;
			return;
		}
	}
}


