/*

	Ajax-implementation for blikk ngrhldm24@schule.suedtirol.it 20060912
	based on http://ajax.get-the-code.de

*/


// global xmlhttprequest object
var xmlHttp = Array();

// handled-flags array[intID][status] = true/false
// to avoid multiple responses from cache
var xmlHttpHandled = Array();

/** 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
 */
function getXMLRequester( )
{
    var newxmlHttp = 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 )
                    {
                        newxmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
                    }
                    // try to use the latest msxml dll
                    else
                    {
                        
                        newxmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                    }
                    break;
                }
                catch( excNotLoadable )
                {                        
                    newxmlHttp = false;
                }
            }
        }
        // Mozilla, Opera und Safari
        else if( window.XMLHttpRequest )
        {
            newxmlHttp = new XMLHttpRequest();
        }
    }
    // loading of xmlhttp object failed
    catch( excNotLoadable )
    {
        newxmlHttp = false;
    }
    return newxmlHttp ;
}


/**
 * 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, intID, strData, intType )
{


    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[intID] && xmlHttp[intID].readyState )
    {
        xmlHttp[intID].abort( );
        xmlHttp[intID] = false;
    }
        
    // create a new instance of xmlHttp[intID]request object
    // if it fails, return
    if( !xmlHttp[intID] )
    {
        xmlHttp[intID] = getXMLRequester( );
        if( !xmlHttp[intID] )
            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;
	
	// reset Handled-Flags
	if (!xmlHttpHandled[intID]) {
		xmlHttpHandled[intID] = Array();
	}
	xmlHttpHandled[intID][0] = false;
	xmlHttpHandled[intID][1] = false;
	xmlHttpHandled[intID][2] = false;
	xmlHttpHandled[intID][3] = false;
	xmlHttpHandled[intID][4] = false;
    
    switch( intType )
    {
        case 1:    // xml
            strData = "xml=" + strData;
        case 2: // POST
            // open the connection 
            xmlHttp[intID].open( 'POST', strSource, true );
            xmlHttp[intID].setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
            //xmlHttp[intID].setRequestHeader( 'Content-type', 'text/plain' );
            xmlHttp[intID].setRequestHeader( 'Content-length', strData.length );
            xmlHttp[intID].setRequestHeader( 'Connection', 'close' );
            break;
        case 3: // HEAD
            // open the connection 
            xmlHttp[intID].open( "HEAD", strSource, true );
            strData = null;
            break;
        default: // GET
            // open the connection 
			var junction = (strSource.indexOf('?') > -1) ? '&' : '?'; 
            var strDataFile = strSource + (strData ? junction + strData : '' );
            xmlHttp[intID].open( "GET", strDataFile, true );
            strData = null;
    }

	// set onload data event-handler
    xmlHttp[intID].onreadystatechange = new Function( "", "processResponse(" + intID + ")" ); ;

    // send request to server
    xmlHttp[intID].send( strData );    // param = 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[intID].readyState )
    {
        // uninitialized
        case 0:
        // loading
			xmlHttpHandled[intID][0] = true;
			break;
        case 1:
        // loaded
 			xmlHttpHandled[intID][1] = true;
			break;
       case 2:
        // interactive
			xmlHttpHandled[intID][2] = true;
			break;
        case 3:
			xmlHttpHandled[intID][3] = true;
            break;
        // complete
        case 4:   
			// check http status
			if (!xmlHttpHandled[intID][4]) {
				if( xmlHttp[intID].status == 200 )    // success
				{
					processData( intID );
				}
				// loading not successfull, e.g. page not available
				else
				{
					if( window.handleAJAXError )
						handleAJAXError( xmlHttp[intID], intID );
					else
						alert( "ERROR\n HTTP status = " + xmlHttp[intID].status + "\n" + xmlHttp[intID].statusText ) ;
				}
				xmlHttpHandled[intID][4] = true;
			}
     }
}

/** End AJAX functions **/
