
/* ---------------------------------------------------------- */
/* ---------------------------------------------------------- */
/* -------------- A J A X  - basic functions ---------------- */
/* ---------------------------------------------------------- */
/* ---------------------------------------------------------- */

// global xmlhttprequest object
var xmlHttp = false;

// constants
var REQUEST_GET        = 0;
var REQUEST_POST        = 2;
var REQUEST_HEAD    = 1;
var REQUEST_XML        = 3;


/**
 * instantiates a new xmlhttprequest object
 *
 * @return xmlhttprequest object or false
 */
function getXMLRequester( )
{
    var xmlHttp = false;
            
    // try to create a new instance of the xmlhttprequest object        
    try
    {
        // Internet Explorer
        if( window.ActiveXObject )
        {
            for( var i = 5; i; i-- )
            {
                try
                {
                    // loading of a newer version of msxml dll (msxml3 - msxml5) failed
                    // use fallback solution
                    // old style msxml version independent, deprecated
                    if( i == 2 )
                    {
                        xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
                    }
                    // try to use the latest msxml dll
                    else
                    {
                        
                        xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                    }
                    break;
                }
                catch( excNotLoadable )
                {                        
                    xmlHttp = false;
                }
            }
        }
        // Mozilla, Opera und Safari
        else if( window.XMLHttpRequest )
        {
            xmlHttp = new XMLHttpRequest();
        }
    }
    // loading of xmlhttp object failed
    catch( excNotLoadable )
    {
        xmlHttp = false;
    }
    return xmlHttp ;
}


/**
 * sends a http request to server
 *
 * @param strSource, String, datasource on server, e.g. data.php
 *
 * @param strData, String, data to send to server, optionally
 *
 * @param intType, Integer,request type, possible values: REQUEST_GET, REQUEST_POST, REQUEST_XML, REQUEST_HEAD default REQUEST_GET
 *
 * @param intID, Integer, ID of this request, will be given to registered event handler onreadystatechange', optionally
 *
 * @return String, request data or data source
 */
function sendRequest( strSource, strData, intType, intID )
{	ajaxstarting();// change to loading-img
    if( !strData )
        strData = '';

    // default type (0 = GET, 1 = xml, 2 = POST )
    if( isNaN( intType ) )
        intType = 0; // GET

    // previous request not finished yet, abort it before sending a new request
    if( xmlHttp && xmlHttp.readyState )
    {
        xmlHttp.abort( );
        xmlHttp = false;
    }
        
    // create a new instance of xmlhttprequest object
    // if it fails, return
    if( !xmlHttp )
    {
        xmlHttp = getXMLRequester( );
        if( !xmlHttp )
            return;
    }
    
    // parse query string
    if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
        strData = strData.substring( 1, strData.length );

    // data to send using POST
    var dataReturn = strData ? strData : strSource;
    
    switch( intType )
    {
        case 1:    // xml
            strData = "xml=" + strData;
        case 2: // POST
            // open the connection 
            xmlHttp.open( "POST", strSource, true );
			xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
           	xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1' ); // charset added rma
            xmlHttp.setRequestHeader( 'Content-length', strData.length );// added rma
			if (xmlHttp.overrideMimeType) xmlHttp.overrideMimeType('text/xml; charset=ISO-8859-1'); // added rma
            break;
        case 3: // HEAD
            // open the connection 
            xmlHttp.open( "HEAD", strSource, true );
            strData = null;
            break;
        default: // GET
            // open the connection 
            var strDataFile = strSource + (strData ? '?' + strData : '' );
            xmlHttp.open( "GET", strDataFile, true );
			xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");// added rma
           	xmlHttp.setRequestHeader( 'Content-Type', 'charset=iso-8859-1' ); // added rma
			if (xmlHttp.overrideMimeType) xmlHttp.overrideMimeType('text/xml; charset=ISO-8859-1'); // added rma
            strData = null;
    }
    
    // set onload data event-handler
    xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ")" ); ;

    // send request to server
    xmlHttp.send( strData );    // param = POST data
    
    return dataReturn;
}

/**
 * process the response data from server
 *
 * @param intID, Integer, ID of this response
 */
function processResponse( intID )
{
    // status 0 UNINITIALIZED open() has not been called yet.
    // status 1 LOADING send() has not been called yet.
    // status 2 LOADED send() has been called, headers and status are available.
    // status 3 INTERACTIVE Downloading, responseText holds the partial data.
    // status 4 COMPLETED Finished with all operations.
    switch( xmlHttp.readyState )
    {
        // uninitialized
        case 0: break;
        // loading
        case 1: break;
        // loaded
        case 2: break;
        // interactive
        case 3:
            break;
        // complete
        case 4:   
            // check http status
            if( xmlHttp.status == 200 )    // success
            {
                processData( xmlHttp, intID );
            }
            // loading not successfull, e.g. page not available
            else
            {
                if( window.handleAJAXError )
                    handleAJAXError( xmlHttp, intID );
                else
                    alert( "ERROR\n HTTP status = " + xmlHttp.status + "\n" + xmlHttp.statusText ) ;
            }
    }
}


/* --------------    A J A X  - end         ---------------- */



/* ---------------------------------------------------------- */
/* ---------------- modified specific functions ------------- */
/* ---------------------------------------------------------- */


var globaldestlayer = "dynContentlayer";// destination-layer for ajaxoutput


// handle response errors
function handleAJAXError( xmlHttp, intID )
{
    /* */
	ajaxstopping();
	alert( "ERROR\n HTTP status = " + xmlHttp.status + "\n" + xmlHttp.statusText ) ;
}


// process data from server
function processData( xmlHttp, intID )
{
    // process text data
	
	if (intID == 888) updateLayer ("overview_map", xmlHttp.responseText )
    else updateLayer (globaldestlayer, xmlHttp.responseText ); 
	
	if (intID == 666) displaymap();
	
	if (intID == 555) displaymapLrt();
	
	
	//if (intID == 777) displaymap ();
	ajaxstopping(); // change to normal loading-img
}


// call search-php-file with searchstring
var globalsearchstring = ""; // to store searchstring and compare if new request is necessary -> changes


function getBiotopdataFromName(thesearchstring)
{
    var strURL = "/includes/db_search_ffh.inc.php";
	var strSEARCH = "?abfragenach=site_name&query_string=" + escape(thesearchstring);
	if (thesearchstring != globalsearchstring && thesearchstring.length > 3){
		globalsearchstring = thesearchstring;
		globaldestlayer = "dynContentlayer";
	    sendRequest( strURL, strSEARCH);
	}
}

function getBiotopdataFromNr(thesearchstring)
{
    var strURL = "/includes/db_search_ffh.inc.php";
	var strSEARCH = "?abfragenach=sitecode&query_string=" + escape(thesearchstring);
	if (thesearchstring != globalsearchstring && thesearchstring.length > 3){
		globalsearchstring = thesearchstring;
		globaldestlayer = "dynContentlayer";
	    sendRequest( strURL, strSEARCH);
	}
}


var globaldieart="";
var globalderlrt="";
var globalthetaxon="";

function getBiotopdataFromSpecNr(specnum,thetaxon){	
	globalthetaxon=thetaxon;
	var dieart=document.getElementById('artselect').value;
	globaldieart=dieart;
	document.getElementById("overview_map").style.display = "block";
	
	
    var strURL = "/includes/db_search_ffh_of_spec.inc.php";
	var strSEARCH = "?taxon="+escape(thetaxon)+"&dieart="+escape(dieart);
		
	if (specnum!="") { 
		globaldestlayer = "dynContentlayer";
		sendRequest(strURL, strSEARCH,0,666);
	}
	
}


function getBiotopdataFromLrt(derlrt){
	var derlrt=document.getElementById('lrtselect').value;
	globalderlrt=derlrt;
	document.getElementById("overview_map").style.display = "block";
	
	
    var strURL = "/includes/db_search_ffh_of_lrt.inc.php";
	var strSEARCH = "?derlrt="+escape(derlrt);
	if (derlrt!="") { 
		globaldestlayer = "dynContentlayer";
		sendRequest(strURL, strSEARCH,0,555);
	}
	
}




function getSteckbriefdataFromLrt(derlrt){
	var derlrt=document.getElementById('lrtselect').value;
	globalderlrt=derlrt;
	//document.getElementById("overview_map").style.display = "block";
	
	
    var strURL = "/includes/db_search_steckbr_of_lrt.inc.php";
	var strSEARCH = "?derlrt="+escape(derlrt);
		
	if (derlrt!="") { 
		globaldestlayer = "dynContentlayer";
		sendRequest(strURL, strSEARCH,0,444);
	}
	
}


function getSteckbriefFromSpecNr(dieart){
	var dieart=document.getElementById('artselect').value;
	//globalderlrt=derlrt;
	//document.getElementById("overview_map").style.display = "block";
	
	
    var strURL = "/includes/db_search_steckbr_of_art.inc.php";
	var strSEARCH = "?dieart="+escape(dieart);
		
	if (dieart!="") { 
		globaldestlayer = "dynContentlayer";
		sendRequest(strURL, strSEARCH,0,333);
	}
	
}


function displaybiotops (){	//only if getBiotopdataFromSpecNr is ready
	
	// show a list in dynlayer
	
	var strURL1 = "/includes/db_get_ffh_list_from_temp.inc.php";
	globaldestlayer = "dynContentlayer";
	sendRequest(strURL1,"",0,777);
	
	
	
}
function displaymap (){	//only if list is ready
	
	// show mappointer
	strURL2 = "/includes/db_get_ffh_pointer_of_spec.inc.php";
	var strSEARCH = "?taxon="+escape(globalthetaxon)+"&dieart="+escape(globaldieart);
	sendRequest(strURL2,strSEARCH,0,888);
	
}
function displaymapLrt (){	//only if list is ready
	
	// show mappointer
	strURL2 = "/includes/db_get_ffh_pointer_of_lrt.inc.php";
	var strSEARCH = "?derlrt="+escape(globalderlrt);
	sendRequest(strURL2,strSEARCH,0,888);
	
}


function getBiotopdetails(sitecode,lon1,lon2,lon3,lat1,lat2,lat3) {
		showsatell();
		// show infos
	    var strURL = "/includes/db_details_ffh_from_id.inc.php";
		var strSEARCH = "?sitecode="+escape(sitecode);
		globaldestlayer = "colright";
		sendRequest(strURL, strSEARCH);
		// show map
		showFFHarea(lon1,lon2,lon3,lat1,lat2,lat3)
}
		
// rewrite contentlayer
function updateLayer (layername, layerstring) {
	
	if (document.getElementById) {
		document.getElementById(layername).innerHTML = layerstring;
	}
	else if (document.all) {
		document.all[layername].innerHTML = layerstring;
	}
}

// events if ajax starts and stops

function ajaxstarting() {
	if (document.images) document.images["ajaxload"].src = "/img/ajax-loader.gif";
}

function ajaxstopping() {
	if (document.images) document.images["ajaxload"].src = "/img/ajax-loader_def.gif";
}



/* ---------------------------------------------------------- */
/* ---------------- googlemaps ------------- */
/* ---------------------------------------------------------- */

	
    //var map = new GMap2(document.getElementById("map"));
	//var icon = new GIcon();
	
    function initmapold() {
	/*
      if (GBrowserIsCompatible()) {
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
		*/
		// Create FFH marker icon
		/*icon.image = "marker_ffh.png";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(52, 46);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(25, 46);
		icon.infoWindowAnchor = new GPoint(5, 1);		
      }*/
    }
	
	function showFFHarea(lon1,lon2,lon3,lat1,lat2,lat3) {
		// create map
		if(ismapinit == 0) initmap();
		
		var gesamtbreite = lat1 + (lat2/60) + (lat3/3600);
		var gesamtlaenge = lon1 + (lon2/60) + (lon3/3600);
		
        map.setCenter(new GLatLng(gesamtbreite,gesamtlaenge), 14);
		map.setMapType(G_HYBRID_MAP);
		var point = new GLatLng(gesamtbreite,gesamtlaenge);
		
		map.addOverlay(new GMarker(point, icon));
	
	}
	
	function showGermany () {
		// create map
		if(ismapgermanyinit == 0) initmapGermany();
		
		lat1=50;
		lat2=5;
		lat3=58;
		lon1=8;
		lon2=46;
		lon3=4;
		var gesamtbreite = lat1 + (lat2/60) + (lat3/3600);
		var gesamtlaenge = lon1 + (lon2/60) + (lon3/3600);
		
        //map2.setCenter(new GLatLng(gesamtbreite,gesamtlaenge), 6);
        map2.setCenter(new GLatLng(51.557223, 9.94503), 6);
		
		map2.setMapType(G_SATELLITE_MAP);
	
	}
	
	function makeGermanyPoint (id,lon1,lon2,lon3,lat1,lat2,lat3) {
		var gesamtbreite = lat1 + (lat2/60) + (lat3/3600);
		var gesamtlaenge = lon1 + (lon2/60) + (lon3/3600);
		
		var point = new GLatLng(gesamtbreite,gesamtlaenge);
		
		
		
		dermarker = new GMarker(point, germanicon);
		GEvent.addListener(dermarker, "click", function() {
	    	getBiotopdetails(id,lon1,lon2,lon3,lat1,lat2,lat3);
     	}); 
		map2.addOverlay(dermarker);
	
	}
	
	var ismapinit = 0; // 1 if first mapdisplay
    
	function initmap() {
	ismapinit = 1;
      if (GBrowserIsCompatible()) {
     	map = new GMap2(document.getElementById("google_map"));
		map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		
		// Create FFH marker icon
		icon = new GIcon();
		icon.image = "/img/area_circle.png";
		icon.iconSize = new GSize(22, 22);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(25, 46);
		icon.infoWindowAnchor = new GPoint(5, 1);		
		
      }
    }
	
	var ismapgermanyinit = 0; // 1 if first mapdisplay
	var germanicon="";
	function initmapGermany() {
	ismapgermanyinit = 1;
      if (GBrowserIsCompatible()) {
     	map2 = new GMap2(document.getElementById("germany_map"));
		map2.addControl(new GSmallMapControl());
		map2.addControl(new GMapTypeControl());
		
		// Create FFH marker icon
		germanicon = new GIcon();
		germanicon.image = "/img/area_overview.png";
		germanicon.iconSize = new GSize(15, 15);
		germanicon.shadowSize = new GSize(15, 15);
		germanicon.iconAnchor = new GPoint(15, 30);
		germanicon.infoWindowAnchor = new GPoint(5, 1);		
		
      }
    }
	
	