/*  Created Date:  6/29/2005 8:24 PM
 * 	Author:  Irvin Owens Jr
 *  For: Irvin Owens Jr
 *  Project: 
 *  Description:  This is the xmlhttprequest object that I use to get data
 *  Libraries Used: 
 */

// The XMLHttpRequest class object

debug = false;

function Request (url,oFunction,type,passThroughObject /* string */,forcePost) {
	this.funct = "";
         if(forcePost){
           this.forcePost = forcePost;
        }else{
         this.forcePost = false;
         }
	// this.req = "";
	this.url = url;
	this.oFunction = oFunction;
	this.type = type;
	this.doXmlhttp = doXmlhttp;
	this.loadXMLDoc = loadXMLDoc;
	this.checkRequestStatus = checkRequestStatus;
	this.passThroughObject = passThroughObject;
}

function doXmlhttp() {
	//var funct = "";
        if(document.getElementById("loading")){
            document.getElementById("loading").style.display = "block";
            setTimeout('document.getElementById("loading").style.display = "none";',30000);
        }
	if (this.type == 'text') {
		this.funct = this.oFunction + '(req.responseText,"' + this.passThroughObject + '")';
	} else {
		this.funct = this.oFunction + '(req.responseXML,"' + this.passThroughObject + '")';
	}
	this.loadXMLDoc();
	return false;
}

function loadXMLDoc() {
	
	var functionA = this.funct;
	var req;
         var pos = this.forcePost;
	req = false;
	function processReqChange() {
		// alert('reqChange is being called');
	    // only if req shows "loaded"
	    if (req.readyState == 4) {
	        // only if "OK"
	        if (req.status == 200) {
	            // ...processing statements go here...
	            eval(functionA);
                    if(document.getElementById("loading")){
                        setTimeout('document.getElementById("loading").style.display = "none";',300);
                    }
	            if(debug){
					var debugWin = window.open('','aWindow','width=600,height=600,scrollbars=yes');
					debugWin.document.body.innerHTML = req.responseText;
				}
	        } else {
	            alert("There was a problem retrieving the data:\n" +
	                req.statusText + '\nstatus: ' + req.status);
				if(debug){
					var debugWin = window.open('','aWindow','width=600,height=600,scrollbars=yes');
					debugWin.document.body.innerHTML = req.responseText;
				}
	        }
	    	}
	}
	
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
	        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
	        	try {
	          		req = new ActiveXObject("Microsoft.XMLHTTP");
	        	} catch(e) {
	          		req = false;
	        	}
	}
    }
    // branch for no xmlhttprequest whatsoever
    else{
	if(browserSniffer() == 1){
		_iframe1 = document.createElement("<iframe id='commIframe' name='commIframe' src='" + this.url + "' style='display:none;'>");
	}else{
		_iframe1 = document.createElement("iframe");
		     _iframe1.id = 'commIframe';
		     _iframe1.name = 'commIframe';
		     _iframe1.src = this.url;
		     _iframe1.style.display = 'none';
	}
	document.getElementsByTagName("body")[0].appendChild(_iframe1);
	_checkLoadInterval = setInterval('this.checkRequestStatus("' + this.oFunction + '")',100);
     }
	
	
	
	if(req) {
		req.onreadystatechange = processReqChange;
		if(this.forcePost == true || this.url.length > 2000){
			var urlSpl = this.url.split('?');
			req.open("POST",urlSpl[0],true);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.send(urlSpl[1]);
		} else {
			req.open("GET", this.url, true);
			req.send("");
		}
	}
}

// iframe eventuality check status function
function checkRequestStatus(fname){
	if(typeof(_iframe1.contentDocument) != "undefined" || typeof(_iframe1.contentWindow) != "undefined"){
		if(typeof(_iframe1.contentDocument) != "undefined"){
			var content = _iframe1.contentDocument.getElementsByTagName("body")[0].innerHTML;
		}else{
			var content = _iframe1.contentWindow.document.getElementsByTagName("body")[0].innerHTML;
		}
		var funct = fname + '(content)';
		//alert(funct);
		var pp = _iframe1.parentNode;
		pp.removeChild(_iframe1);
		delete _iframe1;
		clearInterval(_checkLoadInterval);
		eval(funct);
	}else{
		// do nothing
	}
}

// browser sniffer function

function browserSniffer(){
	if(navigator.userAgent.toLowerCase().indexOf("msie") != -1){
		return 1;
	}
	if(navigator.userAgent.toLowerCase().indexOf("firefox") != -1){
		return 2;
	}
	if(navigator.userAgent.toLowerCase().indexOf("opera") != -1){
		return 3;
	}
	if(navigator.userAgent.toLowerCase().indexOf("safari") != -1){
		return 4;
	}
	return 5;
}
