/*

	$Id: safe-httprequest.js,v 1.1 2006/05/02 18:51:37 scottd Exp $
	
*/

function xmlHttp () {

	var xmlhttp = false;
	
	this.obj = false;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
 	try {
  	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 	 	try {
   		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		} catch (E) {
  		xmlhttp = false;
  		}
 	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
        	xmlhttp = new XMLHttpRequest();
    	} catch (e) {
        	xmlhttp=false;
    	}
	}
	if (!xmlhttp && window.createRequest) {
    	try {
       		xmlhttp = window.createRequest();
    	} catch (e) {
        	xmlhttp=false;
    	}
	}
	//xmlhttp._callback = false;
	this.obj = xmlhttp;
	
}

function getXmlHttp ( wrapped ) {
	var tmp = new xmlHttp();
	var newObj = false;
	if ( tmp && tmp.obj ) {
		if ( wrapped ) {
			newObj = new Object();
			newObj.req = tmp.obj;
			newObj.callback = null;
			return newObj;
		}
		else 
			return tmp.obj;
	}
	return false;
}

/* xmlHttp wrapper function */
function slXmlHttp( _method, url, async, data, callback, useResponseText ) {
    var _xmlHttpObj = false;
    try {
        _xmlHttpObj = xmlHttpObj ( _method, url, async, data, callback, useResponseText );
    }
    catch ( err ) {
        alert ( err );
        return false;
    }
    return false;
}




function xmlHttpObj( _method, url, async, data, callback, useResponseText ) {
    var reqObject, req;


    if ( !_method ) {
        throw ( "can't instantiate a xmlHttp request object with a `method' parameter (1)");
        return false;
    }

    if ( !url ) {
        throw ( "can't instantiate a xmlHttp request object with a `url' parameter (2)");
        return false;
    }

    if ( !async ) {
        async = false;
    }
    else {
        if ( callback && ( typeof ( callback ) != "function" ) ) {
            throw ( "callback parameter (5) " + callback + " cannot be found." );
            return false;
        }
        //if ( !data ) {
        //    throw ( "no data parameter (3) specified to xmlHttp request object." );
        //    return false;
        //}
        async = true;
    }

    reqObject = getXmlHttp(true);
	req = reqObject.req;

	try {
    	reqObject.callback = callback;
	}
	catch ( e ) {
		
	}
    req.open ( _method, url + ( _method.toLowerCase() == "post" ? "" : ( data && data.match ( new RegExp ( /^\?/ ) ) ? "" : "?" ) + data ), async );

    if ( _method.toLowerCase() == "post" )
        req.setRequestHeader ( "Content-Type", "application/x-www-form-urlencoded" );

    if ( async ) {
        req.onreadystatechange = function () {
            switch ( req.readyState ) {

                case 4:
					document.body.style.cursor = "default";
                    if ( req.status == "200" ) {
                        if ( reqObject.callback ) {
                            reqObject.callback ( useResponseText ? req.responseText : req.responseXML );
                        }
                    }
                    else {
                        throw ( "xmlHttp request encountered HTTP-ERROR " + req.status );
                        return false;
                    }
                    break;
                case 3:
                case 2:
                case 1:
            }
        }
    }
	if ( document.logger )
		document.logger.log ( "about to send data" );

		document.body.style.cursor = "wait";

    req.send ( _method.toLowerCase() == "post" ? data : null );
	//document.body.style.cursor = "busy";

	if ( document.logger )
        document.logger.log ( "send method data" );

    if ( !async ) {
		document.body.style.cursor = "default";
		if ( reqObject.callback ) {
			if ( document.logger )
				document.logger.log ( "returning data back via callback function" );

        	reqObject.callback ( useResponseText ? req.responseText : req.responseXML );
		}
		else {
			if ( document.logger )
				document.logger.log ( "returing data (sync-mode)" );
			if ( useResponseText )
				this.responseText = req.responseText;
			else
				this.responseXML = req.responseXML;
			return this;
		}
    }

}

