// Bildwechsler (in 2 Versionen: Adminbereich und Public
function change(img,ref) { document.images[img].src = "bilder/"+ref+".jpg"; }
function changeadmin(img,ref) { document.images[img].src = "../bilder/"+ref+".jpg"; }

// Popup für Bilder (in 2 Versionen: Adminbereich und Public
function popup(imgid,breite,hoehe) {
   window.open("popup.php?img="+imgid,imgid,"width="+breite+",height="+hoehe+",top=100,left=100,location=no,menubar=no,resizeable=no,scrollbars=no,status=no,toolbar=no"); }
function popupadmin(imgid,breite,hoehe) {
   window.open("popup.php?img="+imgid,imgid,"width="+breite+",height="+hoehe+",top=100,left=100,location=no,menubar=no,resizeable=no,scrollbars=no,status=no,toolbar=no"); }

// Popup für Karten (in 2 Versionen: Adminbereich und Public
function popupkarte(imgid,breite,hoehe) {
   window.open("admin/popup.php?karte="+imgid,imgid,"width="+breite+",height="+hoehe+",top=100,left=100,location=no,menubar=no,resizeable=no,scrollbars=no,status=no,toolbar=no"); }
function popupkarteadmin(imgid,breite,hoehe) {
   window.open("popup.php?karte="+imgid,imgid,"width="+breite+",height="+hoehe+",top=100,left=100,location=no,menubar=no,resizeable=no,scrollbars=no,status=no,toolbar=no"); }

function rootchangespop(site) {
   window.open("root_changes_pop.php?site=" + site,"changes","width=420,height=480,location=no,menubar=no,resizeable=yes,scrollbars=yes,status=no,toolbar=no");
}

function popupurl(URL,NAME,WIDTH,HEIGHT) {
   window.open(URL,NAME,"width="+WIDTH+",height="+HEIGHT+",location=no,menubar=no,resizeable=yes,scrollbars=yes,status=no,toolbar=no");
}

// Funktionen für Hauptbuttons oben - wird nicht genutzt, da zu starker effekt
function bon (BUTTON) { document.getElementById(BUTTON).style.background = '#fff'; }
function bof (BUTTON) { document.getElementById(BUTTON).style.background = '#99cc2b'; }
function aof (BUTTON) { document.getElementById(BUTTON).style.background = '#ebebeb'; }

function Go  (select) {
  var wert = select.options[select.options.selectedIndex].value;
  if (wert != "leer") {
    location.href = wert;
    return;
  }
}
function Go2 (select) {
  var wert = select.options[select.options.selectedIndex].value;
  if(wert == '0') { ZIEL = sitelink; }
  else { ZIEL = sitelink + '&gpxgeraet=' + wert; }
  location.href = ZIEL; return;
}
function jump (ZIEL) {
   location.href = ZIEL; return;
}

// Funktion zum Bildwechsel bei Icon-Auswahl von Positionen
function imgOnSelect(select) {
  var wert = select.options[select.options.selectedIndex].value;
  if (wert != "0") {
     img = "iconbild";
     document.images[img].src = "ft_icons/png/"+wert+".png";
  }
}
function imgOnSelectAdmin(select) {
  var wert = select.options[select.options.selectedIndex].value;
  if (wert != "0") {
     img = "iconbild";
     document.images[img].src = "../ft_icons/png/"+wert+".png";
  }
}


// Übergibt einem element (id) einen wert (wert)
// und ändert css-wert von objekt mit id "zusatzerklaerung"
function put(id,wert,show,text,anzeigenhoehewert,feuer,shown,showb,osm,osm2,osmauto,osmuplink,osmnameshow) {
   document.getElementById(id).value = wert;

   // showobject = 'zusatzerklaerung';
   // if(show == 'y') { document.getElementById(showobject).style.display = 'block'; }
   // else { document.getElementById(showobject).style.display = 'none'; }
   // checkbox = 'zusatzerklaerungbox';
   // document.getElementById(checkbox).checked = false;
   
   if(feuer) { document.getElementById('feuer').style.display = 'block'; }
   else {      document.getElementById('feuer').style.display = 'none'; }
   
   showobject = 'nuruser';
   if(show == 'y') {
      if(reggeduser) {
         document.getElementById(showobject).style.display = 'none';
      } else {
         document.getElementById(showobject).style.display = 'block';
      }
   } else {
      document.getElementById(showobject).style.display = 'none';
   }

   document.images['iconbig'].src = "../ft_icons/png/"+wert+".png";
   
   // $check = document.getElementById('name').value;
   // if($check == '') { document.getElementById('name').value = text; }
   
   // Im offline OSM Modus existieren folgende fünf nicht
   if(document.getElementById('showname')) {
      if(shown == 'y') { document.getElementById('showname').checked = true; }
      else { document.getElementById('showname').checked = false; }

      if(showb == 'y') { document.getElementById('showbeschreibung').checked = true; }
      else { document.getElementById('showbeschreibung').checked = false; }
      
      // OSM Feld ändern (IMMER bei Symbol-Klick)
      document.getElementById('osm').value = osm.replace(/##rn##/g,"\r\n");
      
      // OSM2 (Weitere mögliche TAGs anzeigen im DIV-Feld
      if(osm2 == '') {
         document.getElementById('osm2').innerHTML = 'keine Vorschläge vorhanden';
      } else {
         document.getElementById('osm2').innerHTML = osm2;
      }
      // OSM-AUTO Hinweis ergänzen
      document.getElementById('osmauto').innerHTML = osmauto;

      // OSM Uplink einstellen
      if(osmuplink == 'y') {
         document.getElementById('osm_uplink1').checked = true;
         document.getElementById('osm_uplink2').checked = false;
      } else {
         document.getElementById('osm_uplink1').checked = false;
         document.getElementById('osm_uplink2').checked = true;
      }
      // OSM Bezeichnung für Name
      if(osmnameshow == '') {
         document.getElementById('osmnameshow').innerHTML = '<strong>Name:</strong>';
      } else {
         document.getElementById('osmnameshow').innerHTML = "<strong>" + osmnameshow + ":</strong>";
      }

      
   }
   
   document.getElementById('anzeigenhoehe').value = anzeigenhoehewert;
   document.getElementById('anzeigenhoehe').focus();
}


function gmaps() {
   fenster = 'width=900,height=550,scrollbars,resizable=yes';
   url     = 'http://maps.google.de/?ie=UTF8&hl=de&q='+$coordb+','+$coordl+'&ll='+$coordb+','+$coordl+'&spn=0.03866,0.013561';
   titel   = 'EinzelPosition';
   window.open(url,titel,fenster);
}
function gearth() {
   icon = document.getElementById('iconid').value;
   url = 'http://www.freietonne.de/modul_gps_single_kml.php?bdez='+$coordb+'&ldez='+$coordl+'&icon='+icon;
   location.href = url;
   return;
}
function openstreetmap() {
   fenster = 'width=350,height=350,scrollbars,resizable=yes';
   url     = 'http://www.freietonne.de/osm/index.php?lat='+$coordb+'&lon='+$coordl+'&zoom=15';
   titel   = 'EinzelPosition';
   window.open(url,titel,fenster);
}
function osmapi(ID) {
   fenster = 'width=640,height=400,scrollbars,resizable=yes';
   url     = 'http://api.openstreetmap.org/api/0.6/node/' + ID;
   titel   = 'OSM Node ' + ID;
   window.open(url,titel,fenster);
}
function osmapi2(ID) {
   fenster = 'width=640,height=400,scrollbars,resizable=yes';
   url     = 'http://api.openstreetmap.org/api/0.6/node/' + ID + '/history';
   titel   = 'OSM Node-History ' + ID;
   window.open(url,titel,fenster);
}
function setCoordb() {
   $coordb = document.getElementById('coordb').value;
   $coordb = $coordb.replace(/,/,".");
   $coordb = parseFloat($coordb);
   document.getElementById('coordb').value = $coordb;
   makeGrad('b');
}
function setCoordl() {
   $coordl = document.getElementById('coordl').value;
   $coordl = $coordl.replace(/,/,".");
   $coordl = parseFloat($coordl);
   document.getElementById('coordl').value = $coordl;
   makeGrad('l');
}

// Umrechnung von Grad in Dezimal
function makeDez($typ) {
   if($typ == "b") {
      var $grad = document.getElementById('bgra').value;
      var $min  = document.getElementById('bmin').value;
      var $sek  = document.getElementById('bsek').value;
      $sek = $sek.replace(/,/,".");

      if($grad == '' || $grad.match(/[^\d]/)) { $grad = 0; document.getElementById('bgra').value = 0; }
      if($min  == '' || $min.match(/[^\d]/)) { $min  = 0; document.getElementById('bmin').value = 0; }
      if($sek  == '' || $sek.match(/[^\d.]/)) { $sek  = 0; document.getElementById('bsek').value = 0; }

      $grad = parseInt($grad);
      $min  = parseInt($min);
      $sek  = parseFloat($sek);

      $dez = ($grad + $min/60 + $sek/3600) * 1000000;
      $dez  = Math.round($dez) / 1000000;
      document.getElementById('coordb').value = $dez;
      setCoordb();
   }

   if($typ == "l") {
      var $grad = document.getElementById('lgra').value;
      var $min  = document.getElementById('lmin').value;
      var $sek  = document.getElementById('lsek').value;
      $sek = $sek.replace(/,/,".");

      if($grad == '' || $grad.match(/[^\d]/)) { $grad = 0; document.getElementById('lgra').value = 0; }
      if($min  == '' || $min.match(/[^\d]/)) { $min  = 0; document.getElementById('lmin').value = 0; }
      if($sek  == '' || $sek.match(/[^\d.]/)) { $sek  = 0; document.getElementById('lsek').value = 0; }

      $grad = parseInt($grad);
      $min  = parseInt($min);
      $sek  = parseFloat($sek);

      $dez = ($grad + $min/60 + $sek/3600) * 1000000;
      $dez  = Math.round($dez) / 1000000;
      document.getElementById('coordl').value = $dez;
      setCoordl();
   }
}
function vorzeichen($typ,$wert) {
   if($typ == "b") {
      $zahl = Math.abs($coordb);
      if($wert == "+") { document.getElementById('coordb').value = $zahl; setCoordb(); }
      else { document.getElementById('coordb').value = 0 - $zahl; setCoordb(); }
   }

   if($typ == "l") {
      $zahl = Math.abs($coordl);
      if($wert == "+") { document.getElementById('coordl').value = $zahl; setCoordl(); }
      else { document.getElementById('coordl').value = 0 - $zahl; setCoordl(); }
   }
}
function makeGrad($typ) {
   if($typ == "b") { $wert = $coordb; }
   if($typ == "l") { $wert = $coordl; }

   if($wert < 0) {
      $positiv = false;
      $wert = Math.abs($wert);
   } else { $positiv = true; }
   $grad = parseInt($wert);
   $wert = ($wert - $grad) * 60;
   $min  = parseInt($wert);
   $sek  = ($wert - $min) * 6000;
   $sek  = Math.round($sek) / 100;
   if($sek == 60) { $min = $min + 1; $sek = 0; }
   if($min == 60) { $grad = $grad + 1; $min = 0; }

   if($typ == "b") {
      document.getElementById('bgra').value = $grad;
      document.getElementById('bmin').value = $min;
      document.getElementById('bsek').value = $sek;
      if($positiv) {
          document.getElementById('bvorn').checked = true;
          document.getElementById('bvors').checked = false;
      } else {
          document.getElementById('bvorn').checked = false;
          document.getElementById('bvors').checked = true;
      }
   }
   if($typ == "l") {
      document.getElementById('lgra').value = $grad;
      document.getElementById('lmin').value = $min;
      document.getElementById('lsek').value = $sek;
      if($positiv) {
          document.getElementById('lvoro').checked = true;
          document.getElementById('lvorw').checked = false;
      } else {
          document.getElementById('lvoro').checked = false;
          document.getElementById('lvorw').checked = true;
      }
   }
}

function getgpxexport(select,$admin) {
   var wert = select.options[select.options.selectedIndex].value;
   url = "modul_gpx_iq_export_file.php?geraet=" + wert;
   if($admin == 1) { url = "../" + url; }
   if (wert != "leer") {
    location.href = url;
    return;
   }
}

// Funktionen für Breitenbeschränkung ------------------------------------------
function neuAufbau() {
   location.href = location.href;
}
function holeWeite() {
   var xInnen = 0;
	// Prüfung ob normaler Browser (kein IE)
	if(window.innerWidth && window.outerWidth) {
		xInnen   = window.innerWidth;
	// Prüfung für IE6+ im Standard-Konformen Modus
	} else if (document.documentElement.clientWidth) {
		xInnen = document.documentElement.clientWidth;
	// Prüfung für IE6+ im NICHT Standard-Konformen Modus und anderen IE Versionen
	} else if (document.body.clientWidth) {
		xInnen = document.body.clientWidth;
	// Wenn Browser garnichts unterstützt
	} else {
      xInnen = false;
	}
	return xInnen;
}
function fixWeite(Weite) {
   if(Weite > 1000) {
      document.getElementById('maintable').style.width = "960px";
      if(document.getElementById('admintable')) {
         document.getElementById('admintable').style.width = "960px"; }
   }
}
function weiteOnload() {
   Weite = holeWeite();
   fixWeite(Weite);

   // Nur für Positionsformular
   // unhandlich JJ 090913
   //$firstfield = document.getElementById('coordb');
   //if($firstfield) { $firstfield.focus(); }
   
}

function memberlogin(typ) {
   $benutzername = document.getElementById('pubuser').value;
   $benutzername = $benutzername.replace(/ /g,'');

   if(typ == 1) {
      if($benutzername == 'Benutzername') {
         document.getElementById('pubuser').value = '';
      }
   }
   if(typ == 2) {
      if($benutzername == '') {
         document.getElementById('pubuser').value = 'Benutzername';
      } else {
         document.getElementById('pubuser').value = $benutzername;
      }
   }
}

function switchosmextras () {
   if(document.getElementById) {
      if($osmextras == 'none') {
         document.getElementById("osmextras").style.display = 'block';
         $osmextras = 'block';
      } else {
         document.getElementById("osmextras").style.display = 'none';
         $osmextras = 'none';
      }
   } else if(document.all) {
      if($osmextras == 'none') {
         document.all("osmextras").style.display = 'block';
         $osmextras = 'block';
      } else {
         document.all("osmextras").style.display = 'none';
         $osmextras = 'none';
      }
   }
}


// Diese Funktion schaut nach einem INPUT-Feld 'icon'
// und macht mit dessen Inhalt enie anfrage an ajaxosm.php?icon=XXX
// per AJAX. Der Rückgabewert wird in die Textarea 'osm' überschrieben.
function setOsmField() {
   if (xmlhttp.readyState==4) {
      if (xmlhttp.status==200) {
         zeile = xmlhttp.responseText;
         document.getElementById("osm").value = zeile;
      }
   }
}

function ajaxosm() {

   $icon = document.getElementById("iconid").value;
   if($icon) {

      pURL  = 'http://www.freietonne.de/ajaxosm.php?icon=' + $icon;

      // AJAX
      if (window.XMLHttpRequest) {
         xmlhttp = new XMLHttpRequest();
         xmlhttp.onreadystatechange = setOsmField;
         xmlhttp.open("GET", pURL, true); // true für Gecko lassen
         xmlhttp.send(null);
      // Böse Browser (IE)
      } else if (window.ActiveXObject) {
         xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
         if (xmlhttp) {
            xmlhttp.onreadystatechange = setOsmField;
            xmlhttp.open('GET', pURL, false);
            xmlhttp.send();
         }
      }

   } else { return false; }
}
