// global xmlhttprequest object
var xmlHttp = false;
/** AJAX functions **/

// 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
 */
var msProgIDs = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];

function getXMLRequester (){
	var req = null;
	try
	{
		if (window.XMLHttpRequest)
			req = new XMLHttpRequest();
		else if (window.ActiveXObject)
		{
			while (!req && msProgIDs.length)
			{
				try { req = new ActiveXObject(msProgIDs[0]); } catch (e) { req = null; }
				if (!req)
					msProgIDs.splice(0, 1);
			}
		}
	}
	catch (e) { req = null;	}

	if (!req)
		alert("Der Inhalt konnte nicht geladen werden!" );

	return req;
};

/**
 * 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 strData, 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 , FctID){
   
    if( !strData )
        strData = '';

    // default type (0 = GET, 1 = xml, 2 = POST )
    if( isNaN( intType ) )
        intType = 0; // GET

    //default id
    if (isNaN(intID))
      intID = 0;
      
    // 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( 'Content-Type', 'application/x-www-form-urlencoded' );
            xmlHttp.setRequestHeader( 'Content-length', strData.length );
            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 );
            strData = null;
    }
    // set onload data event-handler        
    xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ", " + FctID + ")" ); ;

    // 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, FctID){
    // 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:
        // loading
        case 1:
        // loaded
        case 2:
        // interactive
        case 3:
            break;
        // complete
        case 4:    
            // check http status
            // success
            if( xmlHttp.status == 200 ){
              processData( xmlHttp, intID, FctID);
            }
            // 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 ) ;
            }
    }
}

/** End AJAX functions **/

function handleAJAXError( xmlHttp, intID ){
  try {
  }
  catch(e){}
}

/** real application functions **/

// process data from server
function processData( xmlHttp, intID, FctID ){  
  
// process text data
  if (intID==1)
  {
    htmldata = xmlHttp.responseText;
    htmldata = htmldata.substring(htmldata.indexOf('##BeginContent##') + 16, htmldata.indexOf('##EndContent##'));
    ShowLightbox(htmldata);
  }
  
}

// process data from server

function GetAJAXLightboxContent(URL) 
{
  tmp = self.location.href;
  if (tmp.substring(tmp.length - 1) != '/' && tmp.substring(tmp.length - 1) != 'x')
    URL = '/' + URL;  

  sendRequest(URL, '', REQUEST_POST,1,-1);
}
