function open_popup(page) {
  window.open(page,"popup","width=600,height=400,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes");
  return false;
}


function open_popup2(page) {
  window.open(page,"popup","width=800,height=600,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes");
  return false;
}


function ShowPacchetti(id){
	if (document.getElementById) {
		if (document.getElementById(id).style.display=='none'){
			document.getElementById(id).style.display='block';/* cambiare block con inline a seconda dell'elemento da mostrare */
		} else {
			document.getElementById(id).style.display='none';
		}
	} else if (document.all) {
		if (document.all[id].style.display=='none'){
			document.all[id].style.display='block';
		}else{
			document.all[id].style.display='none';
		}
	}else if (document.layers){
		if (document.layers[id].display=='none'){
			document.layers[id].display='block';
		}else{
			document.layers[id].display='none';
		}
	} else {
		alert("Browser incompatibile!");
	}
	return false;
}



/*CREAZIONE DINAMICA TENDINE*/

var http = createRequestObject();
var http2 = createRequestObject();
var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;

function createRequestObject() {
   var ro;
   var browser = navigator.appName;
   if(browser == "Microsoft Internet Explorer"){
     ro = new ActiveXObject("Microsoft.XMLHTTP");
   }else{
     ro = new XMLHttpRequest();
   }
   return ro;
}

function addElement(ogg,val,text,selected){
  var newOption;
  newOption = document.createElement("option");
  newOption.value = val;
  newOption.text = text;
  if (selected == 1){
  	newOption.setAttribute('selected','selected');
  }	
  ogg.add(newOption, where);    
}

function removeElement(ogg){
  if(ogg.options.length > 0){
    while (ogg.options.length) {
      ogg.remove(0);
    }    
  }
}

function disabledElement(ogg,val){
  document.getElementById(ogg).disabled = val;
}

function loadingState(oggId){
  var id = oggId;
  // inizializzazione Cittą  
  var City = document.getElementById('Localita');
  removeElement(City);
  addElement(City,'sel','-');
  disabledElement('Localita',true);
  http.open('GET','getState.php?id='+id, true);
  http.onreadystatechange = getState;
  http.send(null);
}

function loadingCity(oggId){
  var id = oggId;
  http2.open('GET','getCity.php?id='+id, true);
  http2.onreadystatechange = getCity;
  http2.send(null);
}


function getState(){
	qsParm['idProvincia'] = null;
	qs();
  var State = document.getElementById('idProvincia');
  removeElement(State);
  id = document.getElementById('idNazione').value;
  if(id == 'sel'){
    disabledElement('idProvincia',true);
    addElement(State,'sel','-');
  }else{
    if(http.readyState == 4) {
      if (http.status == 200) {
        var response = http.responseText;
        if(response == ''){
          disabledElement('idProvincia',true);
          addElement(State,'sel','-');
        }else{
          removeElement(State);
          coppia = response.split(',');
          max = coppia.length;
          addElement(State,'sel','-');
          for(x=0;x<max;x++){
            val = coppia[x].split('-');
            if (val[0] == qsParm['idProvincia']){
            	addElement(State,val[0],val[1],1);
            } else {
            	addElement(State,val[0],val[1],0);
            }  
          }
          disabledElement('idProvincia',false);
        }
      }
    }else{
      addElement(State,'sel','Loading...');
    }
  }
}


function getCity(){
	qsParm['Localita'] = null;
	qs();
  var City = document.getElementById('Localita');
  removeElement(City);
  id = document.getElementById('idProvincia').value;
  if(id == 'sel'){
    disabledElement('Localita',true);
    addElement(City,'sel','-');
  }else{
    if(http2.readyState == 4){
      if (http2.status == 200){
        var response = http2.responseText;
        if(response == ''){
          disabledElement('Localita',true);
          addElement(City,'sel','-');
        }else{
          removeElement(City);
          coppia = response.split(',');
          max = coppia.length;
          addElement(City,'sel','-');
          for(x=0;x<max;x++){
            val = coppia[x].split('-');
            if (val[0] == qsParm['Localita']){
            	addElement(City,val[0],val[1],1);
            } else {
            	addElement(City,val[0],val[1],0);
            }
          }
          disabledElement('Localita',false);
        }
      }
    }else{
      addElement(City,'sel','Loading...');
    }
  }
}




/*****************************************
 LEGGERE I PAREMETRI TRAMITE QUERY STRING
******************************************/
var qsParm = new Array();
function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
}