// JavaScript Document
this.name = "MainWin";
var ie = false;
var campo_http;
function change_status (num) {
  var obj1 = document.form1.s_genere;
  var obj2 = document.form1.s_specie;
  var obj3 = document.form1.s_autore;
  obj1.disabled = !(obj1.disabled);
  obj2.disabled = !(obj2.disabled);
  obj3.disabled = !(obj3.disabled);
}
function ControllaCampiFunghi(num) {
	document.getElementById('confermato_'+num).value = 0;
	document.getElementById('confermato_img_'+num).src="images/button_cancel.png";
	if (document.getElementById('altitudine'+num).value == "") return false;
	if (document.getElementById('habitat'+num).value == "") return false;
	if (document.getElementById('rfo'+num).value == "") return false;
	document.getElementById('confermato_img_'+num).src="images/button_accept.png";
	document.getElementById('confermato_'+num).value = 1;
}
function ControllaConfermato(num) {
	if (document.form1_step2.data_ritrovo.value == "")  {
			alert("Inserire la data del ritrovamento!");
			return false; 
	}
	for (x = 0;x < num;x++) {
		if (document.getElementById('confermato_'+x).value == 0) {
			 alert("Il fungo numero " + (x+1) + " non e' completato!");
			 return false;
		}
	}
	return true;
	
}
function CreaTimeout(azione,tempo){
//window.setTimeout(azione,tempo);
} 

function stopload() {
  document.getElementById('preload').style.display = "none";
}
function Operazione() {
  //objHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  //objHttp.send("search_what=All");
  if (objHttp.readyState == 4) {

      if (objHttp.responseText == "Esiste") {
      
      } 
        
          document.getElementById(campo_http).innerHTML = objHttp.responseText;
      
  }
}
function InviaRichiesta(type,url) {
  objHttp.open(type,url,true);
  objHttp.onreadystatechange = Operazione;
  objHttp.send(null);

}
function CreaOggettoHTTP() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        var objHttp = new ActiveXObject("Microsoft.XMLHTTP");  
        ie = true;
    } else {
        var objHttp = new XMLHttpRequest();
    }
    return objHttp;
}
var objHttp = CreaOggettoHTTP();

function apri_div (nome) {
    campo_http = "coord";
    InviaRichiesta("GET","moduli.php?page=aggiungi_coo.php&num=" + id_coordinata);
    
    document.getElementById(nome).style.display = "block";
    
}

function apri_div2 (nome) {
    campo_http = "coord";
    InviaRichiesta("GET","moduli.php?page=aggiungi_ritrovamenti.php&num=" + id_coordinata);
    document.getElementById(nome).style.display = "block";
}

function apri_div_extra (nome,url) {
    campo_http = nome;
     document.getElementById(nome).innerHTML = '<font color="black">Caricamento in corso...</font>';
    InviaRichiesta("GET",url);
    document.getElementById(nome).style.display = "block";
}

function export_zona () {
   coorddst =document.form1.coordinatedst;
   lungsrcl = coorddst.options.length;
   document.form1.zone.value = "";
    for (x = 0;x < lungsrcl;x++) { 
        document.form1.zone.value = document.form1.zone.value + coorddst.options[x].value + ";";
    }
    
}
function popupopen (str) {
  window.open(str,'Mappa','width=200,height=450,resizable=no');
}
function popup_apri (str,titolo) {
  window.open(str,titolo,'width=200,height=400,resizable=no');
}
function popupopen2 (str) {
  window.open(str,'Mappa','width=400,height=200,resizable=no');
}

function add_zona (coor) {
    if (document.getElementById('coord').style.display != "block") {
        alert("Clicca prima la voce CREA o RIMUOVI coordinate");
        return;
    }
    if (coor == "null") {
      coorddst =document.form1.coordinatedst;
      coordscr =document.form1.coordinatesrc;
      lungsrcl = coorddst.options.length;
      lungsrcl2 = coordscr.options.length;
      for (x = lungsrcl2-1;x >= 0;x--) {
          if (coordscr.options[x].selected == true) {
             
              coorddst.options[lungsrcl] = new Option(coordscr.options[x].text, coordscr.options[x].value );
             coordscr.options[x] = null;
          }
      }
   } else {
      coorddst =document.form1.coordinatedst;
      coordscr =document.form1.coordinatesrc;
      lungsrcl = coorddst.options.length;
      lungsrcl2 = coordscr.options.length;
      for (x = lungsrcl-1;x >= 0;x--) {
          if (coorddst.options[x].text == coor) {
            return;
           }
      }
      ce = 0;
      for (x = lungsrcl2-1;x >= 0;x--) {
          if (coordscr.options[x].text == coor) {
              ce++;
          }
      }
      if (ce == 0) { return; }
      for (x = lungsrcl2-1;x >= 0;x--) {
          if (coordscr.options[x].text == coor) {
             coorddst.options[lungsrcl] = new Option(coordscr.options[x].text, coordscr.options[x].value );
             coordscr.options[x] = null;
          }
      }
   }
}


function CompriAdmin() {
     var newElm = document.createElement("div");
     newElm.setAttribute("id","admin_div");
     document.getElementById('header').appendChild(newElm);
     document.getElementById('admin_div').innerHTML = "<form name=\"form_admin\" action=\"\" method=\"post\">User:<input type=\"text\" name=\"user\"> Password:<input type=\"password\" name=\"pwd\"> <input type=\"submit\" id=\"submit\" name=\"submit\" value=\"Login\"></form> ";
      document.getElementById('admin_div').style.className = "admin";
}

function visual_div(nome) {
      try {
          if (document.getElementById(nome).style.display == "block") {
                document.getElementById(nome).style.display = "none";
          }
          else {
              document.getElementById(nome).style.display="block";
          } 
       }
	   catch (e) { }
}

function delete_item(nome) {
    var obj = nome;
    obj[obj.selectedIndex] = null;
}
function delete_items(nome) {
    var obj = nome;
    for (x = obj.length-1;x >= 0;x--) {
        obj[x] = null;
    }
}
function add_item(nome,nome2) {
    var obj1 = nome;
    var obj2 = nome2;
    for (x = 0;x < obj1.length;x++) {
        if (obj1[x].value == obj2[obj2.selectedIndex].value) {
            return;
        }
    }
    obj1[obj1.length] = new Option(obj2[obj2.selectedIndex].text,obj2[obj2.selectedIndex].value);
    
}
function add_position_item(nome,nome2,pos) {
    var obj1 = nome;
    var obj2 = nome2;
    for (x = 0;x < obj1.length;x++) {
        if (obj1[x].value == obj2[obj2.selectedIndex].value) {
            return;
        }
    }
    obj1[pos] = new Option(obj2[obj2.selectedIndex].text,obj2[obj2.selectedIndex].value);
    Select_item(obj1,pos);
    
}
function Select_item(nome,pos) {
    var obj1 = nome;
    obj1[pos].selected = true;
}
function export_data(nome,nome2) {
    var obj1 = nome;
    var obj2 = nome2;
    obj2.value = "";
    for (x = 0;x < obj1.length;x++) {
        obj2.value = obj2.value + obj1[x].value + ";";
    }
}
function StampaStriscia() {
    var pos_striscia = findPos(document.getElementById('footer_li'));
    var pos_fine = findPos(document.getElementById('footer'));
    document.getElementById('footer_li').style.height = (pos_fine[1]-pos_striscia[1])+"px";
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function CaricaSpecie(dest,num) {
		
		 delete_items(dest);
		 objHttp.open("get","moduli.php?page=cerca_specie.php&num="+num,true);
 		 objHttp.onreadystatechange = function Operazione() {
		 if (objHttp.readyState == 4) {
			 if (objHttp.responseXML != null) {
			 	
				 var xmlDoc = objHttp.responseXML;
				 var TagDati = xmlDoc.getElementsByTagName('Dati')[0];
				 var TagMessaggi = TagDati.getElementsByTagName('Speci')[0];
				 var TagMessaggiItems = TagMessaggi.getElementsByTagName("specie");
				 if (TagMessaggi.length != 0) {
					 for (i = dest.options.length;i >= 0;i--) {
					 	dest.options[i] = null;
					 }
					 dest.options[dest.options.length] = new Option("Seleziona specie","null");
					 for (i = 0;i < TagMessaggiItems.length;i++) {
					 	dest.options[dest.options.length] = new Option(TagMessaggiItems[i].firstChild.nodeValue,TagMessaggiItems[i].getAttribute("num"));
					 }
				 }
			 }
		 }
 }
   objHttp.send(null);
}

