// Quelle: http://www.devblog.de
// "Shoutbox als AJAX-Anwendung"
// Code darf frei verwendet werden

// globale Instanz von XMLHttpRequest
var xmlHttp = false;

// XMLHttpRequest-Instanz erstellen
// ... für Internet Explorer
try {
    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
    try {
        xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        xmlHttp  = false;
    }
}
// ... für Mozilla, Opera und Safari
if (!xmlHttp  && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}


function loadData(idx)
{
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/wtg.firmendetails.google.php?id='+ idx, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("asb_content").innerHTML = unescape(xmlHttp.responseText);

         }
         
         
         
     };
     xmlHttp.send(null);
 }
}

function loadWetterData(revierkrzl)
{
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/wtg.karte.wetterwelt.php?r='+ revierkrzl, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("asb_content").innerHTML = unescape(xmlHttp.responseText);

         }
         
         
         
     };
     xmlHttp.send(null);
 }
}


function loadPOI(idx)
{
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/wtg.karte.poidetais.php?id='+ idx, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("asb_content").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }

}

function loadTour(idx)
{
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/wtg.karte.tourdetails.php?id='+ idx, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("asb_content").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }

}


function suche(suchwort)
{
 if (xmlHttp && suchwort.length>=3) {
     xmlHttp.open('GET', BASEURL+'data/wtg.kartensuche.php?suchwort='+ suchwort, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("suchergebnisse").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }

}


function kommentarspeichern() {

	if (document.getElementById("kommentar").value != "") {

		 if (xmlHttp) {
			 xmlHttp.open('GET', BASEURL+'data/wtg.karte.poidetais.kommentar.php?aktion=speichern&poiid='+ document.getElementById("poiid").value + "&kommentar=" + nl2br(document.getElementById("kommentar").value), true);
	      	 xmlHttp.onreadystatechange = function () {
         	 if (xmlHttp.readyState == 4) {
				 
				 document.getElementById("asb_content").innerHTML = unescape(xmlHttp.responseText);
				 
			 	}
			 };
			 xmlHttp.send(null);
			 
		}
	}
}

function formatZahl(zahl, k, fix)
{
    if(!k) k = 0;
    var neu = '';
    // Runden
    var f = Math.pow(10, k);
    zahl = '' + parseInt( zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;
    // Komma ermittlen
    var idx = zahl.indexOf('.');
    // fehlende Nullen einf&uuml;gen
    if(fix)
    {
         zahl += (idx == -1 ? '.' : '' )
         + f.toString().substring(1);
    }
    // Nachkommastellen ermittlen
    idx = zahl.indexOf('.');
    if( idx == -1) idx = zahl.length;
    else neu = ',' + zahl.substr(idx + 1, k);

    // Tausendertrennzeichen
    while(idx > 0)
    {
        if(idx - 3 > 0)
        neu = '.' + zahl.substring( idx - 3, idx) + neu;
        else
        neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }
    return neu;
}

 function nl2br(str) {
    if(typeof(str)=="string") return str.replace(/(\r\n)|(\n\r)|\r|\n/g,"<BR>");
    else return str;
  }

function firmenteasern(x,y)
{
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/wtg.firmenteasern.php?ajax=1&x='+ x + "&y=" + y, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("firmenteasern").innerHTML = unescape(xmlHttp.responseText);
     		//amazon(x,y);
	 	}
     };
     xmlHttp.send(null);
	 
	 
 }
	
}
// FUNKTIONEN ZUR TÖRNPLANUNG 
function toernplanung()
{
	var startpunkt = document.getElementById("field_dstartpos").value;
	var endpunkt = document.getElementById("field_dzielpos").value;

			  
			
	document.getElementById("erg_info").innerHTML = "<center>T&ouml;rn wird berechnet!<br/><img src='"+BASEURL+"img/layout/ajax-loader.gif'></center>";
			
	if (xmlHttp) {
		 xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=addNotiz+startpunkt=' + startpunkt + '+endpunkt=' + endpunkt , true);
		 xmlHttp.onreadystatechange = function () {
			 if (xmlHttp.readyState == 4) {
				 document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
				 document.getElementById("erg_info").innerHTML = "";
				 document.getElementById("erg_d").innerHTML = "<b>N&auml;chsten Zielpunkt in die Karte setzten!</b>";
			 }
		 };
		 xmlHttp.send(null);
	 }
	document.getElementById("field_dstartpos").value = document.getElementById("field_dzielpos").value;
	document.getElementById("field_dzielpos").value = "";
}

// Promoten und Demoten von Stopps
function pro_dem_stop(art, notiznr)
{
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion='+art+'+notiznr='+notiznr, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }
}

// Einen T&ouml;rn hinzuf&uuml;gen
function addToern()
{

 document.getElementById("toernplanertabelle").innerHTML = "<center>Liste wird aktualisiert!<br/><img src='"+BASEURL+"img/layout/ajax-loader.gif'></center>";

 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=addToern', true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }
}

// Einen Stop oder Toern L&ouml;schen
function del_stop_toern(art, notiznr)
{

 document.getElementById("toernplanertabelle").innerHTML = "<center>T&ouml;rn wird aktualisiert!<br/><img src='"+BASEURL+"img/layout/ajax-loader.gif'>";     
 if (xmlHttp) {

     if(art == "delNotiz")
	     xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=delNotiz+notiznr='+notiznr, true);
	 else if(art == "delToern")
	 	xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=delToern', true);
	 
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }
}


// Alle Daten eines T&ouml;rns anzeigen
function showFullToern()
{

 if (xmlHttp) {
     
     xmlHttp.open('GET', BASEURL+'data/aquasirius.php', true);
	 
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }
}

// Aktiven T&ouml;rn ausw&auml;hlen
function setActiveToern(toernnr)
{
 document.getElementById("toernplanertabelle").innerHTML = "<center>T&ouml;rn wird aktualisiert!<br/><img src='"+BASEURL+"img/layout/ajax-loader.gif'></center>";
 if (xmlHttp) {
     
     xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=setActiveToern+toernnr='+toernnr, true);
	 
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }
}

// Revierinformationen als T&ouml;rnstopp hinzuf&uuml;gen (aktueller SESSION T&ouml;rn)
function addRevier(id)
{
 document.getElementById("poidetails").innerHTML = "<center>T&ouml;rn wird aktualisiert!<br/><img src='"+BASEURL+"img/layout/ajax-loader.gif'></center>";
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=addRevier+poi='+id, true);

     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("poidetails").innerHTML = "";
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
             toernplaner=1;expandcontent('toernplanung');
         }
     };
     xmlHttp.send(null);
 }
}

// Firma (die ID) als T&ouml;rnstopp hinzuf&uuml;gen (aktueller SESSION T&ouml;rn)
function addFirma(id)
{
document.getElementById("firmendetails").innerHTML = "<center>T&ouml;rn wird aktualisiert!<br/><img src='"+BASEURL+"img/layout/ajax-loader.gif'></center>";
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=addFirma+idx='+id, true);

     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("firmendetails").innerHTML = "";
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
             toernplaner=1;expandcontent('toernplanung');
         }
     };
     xmlHttp.send(null);
 }
}


// Firma (die ID einer Marina) als HafenStopp hinzuf&uuml;gen (aktueller SESSION T&ouml;rn)
function addHafen(id)
{
document.getElementById("firmendetails").innerHTML = "<center>T&ouml;rn wird aktualisiert!<br/><img src='"+BASEURL+"img/layout/ajax-loader.gif'></center>";
 if (xmlHttp) {
     xmlHttp.open('GET', 'data/aquasirius.php?aktion=addHafen+idx='+id, true);

     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("firmendetails").innerHTML = "";
             document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
             toernplaner=1;expandcontent('toernplanung');
         }
     };
     xmlHttp.send(null);
 }
}


//Kommentar aendern
function kommentaraendern(id,text)
{
	 //Kommentar speichern
	var kommentartext = prompt("Kommentar bearbeiten (max. 254 Zeichen):", text);
	
	if(kommentartext.length < 255){
		
		if (xmlHttp) {
		    xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=updateKommentar+id='+id+'+text='+escape(kommentartext), true);
		    xmlHttp.onreadystatechange = function ()
		    {
	        	if (xmlHttp.readyState == 4)
	        	{
		       		document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
		   		}
			};
			xmlHttp.send(null);
		}
	}else
		alert(unescape('Text zu lang (max. 254), %E4nderung verworfen.'));
}


//T&ouml;rnName &auml;ndern
function updateToernname(id, text)
{
	var toernnametext = prompt(unescape("Neuer Name f%FCr den T%F6rn %28max. 30 Zeichen%29%3A"), text);
	
	if(toernnametext.length < 31){
	
		if (xmlHttp) {
		    xmlHttp.open('GET', BASEURL+'data/aquasirius.php?aktion=updateToernName+id='+id+'+name='+escape(toernnametext), true);
	
		    xmlHttp.onreadystatechange = function () {
		        if (xmlHttp.readyState == 4) {
	            		document.getElementById("toernplanertabelle").innerHTML = unescape(xmlHttp.responseText);
	        		}
			};
			xmlHttp.send(null);
		}
	}else
		alert(unescape('Name zu lang, %E4nderung verworfen.'));
}

//Funktion für Amazon Buchenpfehlungen
function amazon(x,y)
{
 if (xmlHttp) {
     xmlHttp.open('GET', BASEURL+'data/wtg.amazon.php?x='+ x + "&y=" + y, true);
     xmlHttp.onreadystatechange = function () {
         if (xmlHttp.readyState == 4) {
             document.getElementById("amazon").innerHTML = unescape(xmlHttp.responseText);
         }
     };
     xmlHttp.send(null);
 }
}
