var REQUEST_GET        = 0;
var REQEST_POST        = 2;
var REQUEST_HEAD    = 1;
var REQUEST_XML        = 3;
    
function c_ajax() 
{
    //Variablen
    this.xmlHttp=false;
    this.intResultNum=0;
    this.sendOnChange=false;var REQUEST_GET        = 0;
    var REQEST_POST        = 2;
    var REQUEST_HEAD    = 1;
    var REQUEST_XML        = 3;
    
function c_ajax() 
{
    //Variablen
    this.xmlHttp=false;
    this.intResultNum=0;
    this.sendOnChange=false;
    this.arrOpts=new Array();
         
    //Funktionen
    this.setURI = function(strURI) {this.strURI=strURI;};
    
    this.setOpts = function(strKey, strName) 
    {    
        for(i=this.arrOpts.length-1; i>=0; i--)  
        {
            if(this.arrOpts[i].indexOf(strKey)==0 && this.arrOpts[i].indexOf("%5B%5D")==-1) 
            {
                this.arrOpts[i]=strKey+"="+strName;
                if(this.sendOnChange) {
                    this.sendRequest();
                }
                return; 
            }
        }
        this.arrOpts.push(strKey+"="+strName);
        if(this.sendOnChange) {
            this.sendRequest();
        }
    };
           
    this.sendRequest=sendRequest;
    this.processResponse=processResponse;
    this.processData=processData;
           
    this.gotoPage=gotoPage;
    
    this.clearOpts=clearOpts;
           
    //set Options
    this.clearOpts();
} 
    
function gotoPage(intPage) {
    this.intPage=intPage;
    this.setOpts(new Array('sb='+document.getElementById('sb').value), true);    
}
String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}[]0-9.-+Eaeflnr-u nrt]/.test(
                this.replace(/"(.|[^"])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return this;
    }
};
        
/**
 * xmlhttprequest object anlegen
 *
 * @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 )
        {
			var progIDs = [ 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0' ];

			for (var i = 0; i < progIDs.length; i++) {
				try 
				{
            		xmlHttp = new ActiveXObject( progIDs[i] );
					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 ;
}
/**
 * sendet einen http request zum server
 * @param intType, Integer,request type: 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( intType, intID )
{
    //document.getElementById('status').style.display = "block";
    this.strData=this.arrOpts.join("&");
    // default type (0 = GET, 1 = xml, 2 = POST )
    if(isNaN(intType ))    {intType = 2;} // GET
    // previous request not finished yet, abort it before sending a new request
    if( this.xmlHttp && this.xmlHttp.readyState )
    {
        this.xmlHttp.abort( );
        this.xmlHttp = false;
    }
        
    // create a new instance of xmlhttprequest object
    // if it fails, return
    if( !this.xmlHttp )
    {
        this.xmlHttp = getXMLRequester( );
        if( !this.xmlHttp ) 
            return;
    }
    
    // parse query string
    if( intType != 1 && ( this.strData && this.strData.substr( 0, 1 ) == '&' || this.strData.substr( 0, 1 ) == '?' ) )
        this.strData = this.strData.substring( 1, this.strData.length );
    // data to send using POST
    var dataReturn = this.strData ? this.strData : this.strURI;
    
    switch( intType )
    {
        case 1:    // xml
            this.strData = "xml=" + this.strData;
        case 2: // POST
            // open the connection 
            this.xmlHttp.open( "POST", this.strURI, true );
            this.xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
            this.xmlHttp.setRequestHeader( 'Content-length', this.strData.length );
            break;
        case 3: // HEAD
            // open the connection 
            this.xmlHttp.open( "HEAD", this.strURI, true );
            this.strData = null;
            break;
        default: // GET
            // open the connection 
            var strDataFile = this.strURI + (this.strData ? '?' + this.strData : '' );
            this.xmlHttp.open( "GET", strDataFile, true );
            this.strData = null;
    }
            
    //this.xmlHttp.onreadystatechange = new Function( "", "this.processResponse(" + intID + ")" ); ;
    var reqObj = this;
    this.xmlHttp.onreadystatechange = function(){reqObj.processResponse();};
    // send request to server
    this.xmlHttp.send( this.strData );    // param = POST data
    
    return dataReturn;
}
    
/**
 * process the response data from server
 *
 * @param intID, Integer, ID of this response
 */
function processResponse()
{
    // 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( this.xmlHttp.readyState )
    {
        // uninitialized
        case 0:
        // loading
        case 1:
        // loaded
        case 2:
        // interactive
        case 3:
            break;
        // complete
        case 4:     
            // check http status
            if( this.xmlHttp.status == 200 )    // success
            {
                this.processData();
            }
            // loading not successfull, e.g. page not available
            else
            {
                if( window.handleAJAXError )
                    handleAJAXError( this.xmlHttp );
                else
                    alert( "ERRORn HTTP status = " + this.xmlHttp.status + "n" + this.xmlHttp.statusText ) ;
            }
    }
}

/**
 * Parameter setzten
 * Kann per onChange (Benutzereingaben) oder direkt (bei festen Werten) aufgerufen werden
 * Format: ac.setOpts(string name, string value);
 *
 * @param arrOpts, Array, Array mit Parametern im Format "name=var"
 * @param bSend, Bool, Paramter nach Auswertung direkt versenden 
 **/ 
function clearOpts() 
{
    this.arrOpts=new Array();    
    this.setOpts("qs", new Date().getTime());
}

function processData() 
{
    this.arrResults = this.xmlHttp.responseText.parseJSON();
    this.showResult();
}
function getValues( xmlDocument, strTagName )
{
    var xmlTags;
    if( strTagName )
        xmlTags =  xmlDocument.getElementsByTagName( strTagName );
    else
        xmlTags =  xmlDocument.childNodes;
        
    var intLen = xmlTags.length;
    if( !intLen )
        return null;
    else if( intLen == 1 )
    {
        return xmlTags[ 0 ].firstChild.nodeValue;
    }
    else
    {
        var arrValues = new Array( );
        for( var i = 0; i < intLen; i+=1 )
        {
            arrValues[ i ] = xmlTags[ i ].nodeValue;
        }            
        return arrValues;
    }
}      
    this.arrOpts=new Array();
         
    //Funktionen
    this.setURI = function(strURI) {this.strURI=strURI;};
    
    this.setOpts = function(strKey, strName) 
    {    
        for(i=this.arrOpts.length-1; i>=0; i--)  
        {
            if(this.arrOpts[i].indexOf(strKey)==0 && this.arrOpts[i].indexOf("%5B%5D")==-1) 
            {
                this.arrOpts[i]=strKey+"="+strName;
                if(this.sendOnChange) {
                    this.sendRequest();
                }
                return; 
            }
        }
        this.arrOpts.push(strKey+"="+strName);
        if(this.sendOnChange) {
            this.sendRequest();
        }
    };
           
    this.sendRequest=sendRequest;
    this.processResponse=processResponse;
    this.processData=processData;
           
    this.gotoPage=gotoPage;
    
    this.clearOpts=clearOpts;
           
    //set Options
    this.clearOpts();
} 
    
function gotoPage(intPage) {
    this.intPage=intPage;
    this.setOpts(new Array('sb='+document.getElementById('sb').value), true);    
}

String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}[]0-9.-+Eaeflnr-u nrt]/.test(
                this.replace(/"(.|[^"])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return this;
    }
};
        
/**
 * xmlhttprequest object anlegen
 *
 * @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-=1)
            {
                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 ;
}

/**
 * sendet einen http request zum server
 * @param intType, Integer,request type: 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( intType, intID )
{
    //document.getElementById('status').style.display = "block";
    this.strData=this.arrOpts.join("&");

    // default type (0 = GET, 1 = xml, 2 = POST )
    if(isNaN(intType ))    {intType = 2;} // GET

    // previous request not finished yet, abort it before sending a new request
    if( this.xmlHttp && this.xmlHttp.readyState )
    {
        this.xmlHttp.abort( );
        this.xmlHttp = false;
    }
        
    // create a new instance of xmlhttprequest object
    // if it fails, return
    if( !this.xmlHttp )
    {
        this.xmlHttp = getXMLRequester( );
        if( !this.xmlHttp ) 
            return;
    }
    
    // parse query string
    if( intType != 1 && ( this.strData && this.strData.substr( 0, 1 ) == '&' || this.strData.substr( 0, 1 ) == '?' ) )
        this.strData = this.strData.substring( 1, this.strData.length );

    // data to send using POST
    var dataReturn = this.strData ? this.strData : this.strURI;
    
    switch( intType )
    {
        case 1:    // xml
            this.strData = "xml=" + this.strData;
        case 2: // POST
            // open the connection 
            this.xmlHttp.open( "POST", this.strURI, true );
            this.xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
            this.xmlHttp.setRequestHeader( 'Content-length', this.strData.length );
            break;
        case 3: // HEAD
            // open the connection 
            this.xmlHttp.open( "HEAD", this.strURI, true );
            this.strData = null;
            break;
        default: // GET
            // open the connection 
            var strDataFile = this.strURI + (this.strData ? '?' + this.strData : '' );
            this.xmlHttp.open( "GET", strDataFile, true );
            this.strData = null;
    }
            
    //this.xmlHttp.onreadystatechange = new Function( "", "this.processResponse(" + intID + ")" ); ;
    var reqObj = this;
    this.xmlHttp.onreadystatechange = function(){reqObj.processResponse();};
    // send request to server
    this.xmlHttp.send( this.strData );    // param = POST data
    
    return dataReturn;
}
    

/**
 * process the response data from server
 *
 * @param intID, Integer, ID of this response
 */
function processResponse()
{
    // 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( this.xmlHttp.readyState )
    {
        // uninitialized
        case 0:
        // loading
        case 1:
        // loaded
        case 2:
        // interactive
        case 3:
            break;
        // complete
        case 4:     
            // check http status
            if( this.xmlHttp.status == 200 )    // success
            {
                this.processData();
            }
            // loading not successfull, e.g. page not available
            else
            {
                if( window.handleAJAXError )
                    handleAJAXError( this.xmlHttp );
                else
                    alert( "ERRORn HTTP status = " + this.xmlHttp.status + "n" + this.xmlHttp.statusText ) ;
            }
    }
}


/**
 * Parameter setzten
 * Kann per onChange (Benutzereingaben) oder direkt (bei festen Werten) aufgerufen werden
 * Format: ac.setOpts(string name, string value);
 *
 * @param arrOpts, Array, Array mit Parametern im Format "name=var"
 * @param bSend, Bool, Paramter nach Auswertung direkt versenden 
 **/ 
function clearOpts() 
{
    this.arrOpts=new Array();    
    this.setOpts("qs", new Date().getTime());
}


function processData() 
{
    this.arrResults = this.xmlHttp.responseText.parseJSON();
    this.showResult();
}

function getValues( xmlDocument, strTagName )
{
    var xmlTags;
    if( strTagName )
        xmlTags =  xmlDocument.getElementsByTagName( strTagName );
    else
        xmlTags =  xmlDocument.childNodes;
        
    var intLen = xmlTags.length;
    if( !intLen )
        return null;
    else if( intLen == 1 )
    {
        return xmlTags[ 0 ].firstChild.nodeValue;
    }
    else
    {
        var arrValues = new Array( );
        for( var i = 0; i < intLen; i+=1 )
        {
            arrValues[ i ] = xmlTags[ i ].nodeValue;
        }            
        return arrValues;
    }
}          
