var AJ_REQUEST_GET = 0;
var AJ_REQUEST_POST = 2;
var AJ_REQUEST_XML = 3;

var ajXmlHttp;

function ajSendRequest(strSource, strData, intType, intID, callback) {
		
    if( !strData ) {
    	strData = '&cs='+new Date().getTime();
    } else {
    	strData = strData + '&cs='+new Date().getTime();
    }
    
    if( isNaN( intType ) ) intType = 0;
    
    if( ajXmlHttp && ajXmlHttp.readyState ) {
        ajXmlHttp.abort( );
        ajXmlHttp = false;
    }
    
    if( !ajXmlHttp ) {
        ajXmlHttp = ajGetXmlRequester( );
        if( !ajXmlHttp ) return;
    }
        
    if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
        strData = strData.substring( 1, strData.length );
    
    var dataReturn = strData ? strData : strSource;
    
    switch( intType ) {
        case 1:    // xml
            strData = "xml=" + strData;
        case 2: // POST            
            ajXmlHttp.open( "POST", strSource, true );
            ajXmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=utf-8' );
            ajXmlHttp.setRequestHeader( 'Content-length', strData.length );
            break;        
        default: // GET            
            var strDataFile = strSource + (strData ? '?' + strData : '' );          
            ajXmlHttp.open( "GET", strDataFile, true );
            strData = null;
    }

    ajXmlHttp.onreadystatechange = new Function( "", "ajProcessResponse(" + intID + ",'" + callback + "')" ); 
    ajXmlHttp.send( strData ); 

    return dataReturn;
}

function ajProcessResponse(intID,callback) {   
	
    switch( ajXmlHttp.readyState ) {        
        case 0: // uninitialized        
        case 1:// loading        
        case 2: // loaded        
        case 3: // interactive
            break;        
        case 4: // complete               
            if( ajXmlHttp.status == 200 ) {
                ajProcessData(ajXmlHttp, intID, callback);
            } else {
                if( window.ajHandleError )
                    ajHandleError( ajXmlHttp, intID );
                else
                    alert( "ERROR\n HTTP status = " + ajXmlHttp.status + "\n" + ajXmlHttp.statusText ) ;
            }
    }
}

// process data from server
function ajProcessData( ajXmlHttp, intID, callback ){ 	
	
    if(ajXmlHttp.getResponseHeader("Content-Type").indexOf("html") != -1) {
    	eval(callback+"('"+escape(ajXmlHttp.responseText.replace(/^\s*|\s*$/g,""))+"')");    	 
    } else {   	    
    	eval(callback+"('"+ajXmlHttp.responseText.replace(/^\s*|\s*$/g,"")+"')");	
    }       
}

// handle response errors
function ajHandleError( ajXmlHttp, intID ) {}

function ajGetXmlRequester() {	   
    
    var ajXmlHttp = false;
	// Mozilla, Opera, Safari and Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined') {
	    ajXmlHttp = new XMLHttpRequest();
	}
	if (!ajXmlHttp) {
	    // Internet Explorer 6 and older
	    try {
	        ajXmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(e1) {
	        try {
	            ajXmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch(e2) {
	            ajXmlHttp  = false;
	        }
	    }
	}
       
    return ajXmlHttp ;
}
