var m_aMMAXMLHolder = new Array();
var m_MMA_XMLID = 0;

// constants
var MMA_REQUEST_GET  = 0;
var MMA_REQUEST_POST  = 2;
var MMA_REQUEST_HEAD = 1;
var MMA_REQUEST_XML  = 3;


/**
 * instantiates a new mma_xmlHttprequest object
 *
 * @return mma_xmlHttprequest object or false
 */
function getXMLRequester( )
{
	var mma_xmlHttp = false;

    // try to create a new instance of the mma_xmlHttprequest object        
    try
    {
        // Internet Explorer
        if( window.ActiveXObject )
        {
            for( var i = 6; i; i-- )
            {
				if (i == 5) continue; // Version 5 wg. falscher Sicherheitsabfrage daektiviert.
				
                try
                {
                    // loading of a newer version of msxml dll (msxml3 - msxml5) failed
                    // use fallback solution
                    // old style msxml version independent, deprecated
                    if( i == 2 )
                    {
                        mma_xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
                    }
                    // try to use the latest msxml dll
                    else
                    {
                        
                        mma_xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                    }
                    break;
                }
                catch( excNotLoadable )
                {                        
                    mma_xmlHttp = false;
                }
            }
        }
        // Mozilla, Opera und Safari
        else if(  window.XMLHttpRequest )
        {
            mma_xmlHttp = new XMLHttpRequest();
        }
    }
    // loading of mma_xmlHttp object failed
    catch( excNotLoadable )
    {
        mma_xmlHttp = false;
    }
    return mma_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: MMA_REQUEST_GET, REQUEST_POST, MMA_REQUEST_XML, MMA_REQUEST_HEAD default MMA_REQUEST_GET
 * @param strData,     Integer, ID of this request, will be given to registered event handler onreadystatechange', optionally
 + @param String,       String, sCallbackFunction Funktion der aufgerufen wird wenn Ergebnis da ist
 * @return String,     request data or data source
  */
function sendRequest( strSource, strData, intType, CallID , sCallbackFunction)
{
	var oDatum = new Date();
	var XMLID;
	var XMLOBJ;
	var oOBJ;

	// Counter erhoehen
	m_MMA_XMLID += 1;
	
	//
	if(strSource.indexOf("?") > -1){
		strSource += "&T=" + oDatum.getTime() + "_" + m_MMA_XMLID;
	}else{
		strSource += "?T=" + oDatum.getTime() + "_" + m_MMA_XMLID;
	}
	
	
    if( !strData )
        strData = '';

    // default type (0 = GET, 1 = xml, 2 = POST )
    if( isNaN( intType ) )
        intType = 0; // GET


	oOBJ = new Object();
	oOBJ.ID = m_MMA_XMLID;
	oOBJ.XMLHTTP = getXMLRequester();
	AddXMLObj(oOBJ)
	
    // 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 
            oOBJ.XMLHTTP.open( "POST", strSource, true );
            oOBJ.XMLHTTP.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
            oOBJ.XMLHTTP.setRequestHeader( 'Content-length', strData.length );
            break;
        
		case 3: // HEAD
            // open the connection 
            oOBJ.XMLHTTP.open( "HEAD", strSource, true );
            strData = null;
            break;
        
		default: // GET
            // open the connection 
            var strDataFile = strSource + (strData ? '?' + strData : '' );
            oOBJ.XMLHTTP.open( "GET", strDataFile, true );
            strData = null;
    }
    
    // set onload data event-handler
	if (sCallbackFunction.length > 0){
		oOBJ.XMLHTTP.onreadystatechange = new Function( "", "processResponse(" + CallID + "," + oOBJ.ID + ",'" + sCallbackFunction +"')" );
	}	
    oOBJ.XMLHTTP.send( strData );    // param = POST data
    
    return dataReturn;
}

/**
 * process the response data from server
 *
 * @param CallID, Integer, ID of this response, Function for callback
 */
function processResponse( CallID, XMLID ,sCallbackFunction )
{

    // 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.

	var xmlHttpOBJ = GetXMLObj(XMLID);
	if (xmlHttpOBJ == null) return;
	
	xmlHttp = xmlHttpOBJ.XMLHTTP

	
    switch( xmlHttp.readyState )
    {
        case 0:
			// uninitialized

		case 1:
			// loading
			
        case 2:
			// loaded
		
        case 3:
			// interactive
            break;
        
        case 4:
			// complete --> check http status
            if( xmlHttp.status == 200 ){
				// no error
				processData( xmlHttpOBJ, XMLID, CallID, sCallbackFunction );
            }else{
				// loading not successfull, e.g. page not available
				m_aMMAXMLHolder = RemoveXMLObj(xmlHttpOBJ.ID)
                if( window.handleAJAXError )
                    handleAJAXError( xmlHttp, CallID );
                else
                    alert( "ERROR\n HTTP status = " + xmlHttp.status + "\n" + xmlHttp.statusText ) ;
            }
    }
}



// Rueckgabe der Daten vom Server
function processData( xmlHttpOBJ, XMLID, CallID ,sCallbackFunction ){

	if( window[sCallbackFunction]){
	
		window[sCallbackFunction](xmlHttpOBJ.XMLHTTP.responseText,XMLID);
		m_aMMAXMLHolder = RemoveXMLObj(xmlHttpOBJ.ID)
		return void(0);	
	}else{
		if (sCallbackFunction.length > 0) alert("Callbackfunction '" + sCallbackFunction + "' not found")
	}
}

// handle response errors
function handleAJAXError( xmlHttp, CallID )
{
    alert( "ERROR\n HTTP status = " + xmlHttp.status + "\n" + xmlHttp.statusText ) ;
}


// ++++++++++++++++++++

function GetXMLObj(nXMLID){
	for ( var i = 0; i < m_aMMAXMLHolder.length; i++ ){
		if (m_aMMAXMLHolder[i].ID == nXMLID){
			return m_aMMAXMLHolder[i];
		}
	}
	return null;
}

function AddXMLObj(oXMLObj){
  // haengt an Array ein Element an
  m_aMMAXMLHolder[m_aMMAXMLHolder.length] = oXMLObj;
}

function RemoveXMLObj(nXMLID){
  var arr = new Array();
  
  for ( var i = 0; i < m_aMMAXMLHolder.length; i++ ){
  	if ( m_aMMAXMLHolder[i].ID == nXMLID ) continue;
	arr[arr.length] =  m_aMMAXMLHolder[i];
  }
 
  return arr;
}


// ++++++++++++++++++++


