/*
	XMLHTTP code adapted from
		http://xkr.us/code/javascript/XHConn/  (version 2005-04-08)
	
	IFRAME code adapted from
		http://developer.apple.com/internet/webcontent/iframe.html
		(note: iframes require the use of SimpleAjax, below, because we
		 don't get access to an XML object)
	
	usage:
		function CALLBACK( xmlObj) { alert(xmlObj.responseText); }

		var myConn = new Ajaxer();
		if ( myConn)
			myConn.connect( url, vars, CALLBACK);
*/
var tempAjaxObj;

function Ajaxer()
{
	var xmlhttp, bComplete = false, privateFunc, iframeObj;
	
	this.useIframe = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { this.useIframe = true; }}}
	this.ok = true;
	
	this.setPrivateFunc = function(f) { privateFunc = f; }

	// iframe method tested & works in:
	//		IE5.5/PC, IE6/PC, Safari, IE5.2/Mac
	// not working in Firefox
	// this is fine -- it's primarily for IE5.2 and also IE/PC (when ActiveX turned off)
	//
	if ( this.useIframe) {
		var iframeObj, iframeDoc;
		this.connect = function(sURL,sVars,fnDoneUNUSED)	// you MUST set privateFunc when using IFrames!
		{
			if (!iframeObj) {
				// create the IFrame and assign a reference to the object to our global variable iframeObj.
				// this will only happen the first time callToServer() is called
				try {
					var ifr=document.createElement('iframe');
					ifr.setAttribute('id','AjaxIFrame');
					ifr.setAttribute('name','AjaxIFrame');
					ifr.style.visibility = 'hidden';
					ifr.style.position = 'absolute';
					ifr.style.width = ifr.style.height = ifr.borderWidth = '0px';
					iframeObj = document.body.appendChild(ifr);
					if (document.frames) {
						// this is for IE5 Mac, because it will only allow access to the document object
						// of the IFrame if we access it through the document.frames array
						iframeObj = document.frames['AjaxIFrame'];
					}
				} catch(e) {
					// This is for IE5 PC, which does not allow dynamic creation and manipulation of an
					// iframe object. Instead, we'll fake it up by creating our own objects.
					var iframeHTML='<iframe id="AjaxIFrame" name="AjaxIFrame" style="display:none"></iframe>';
					document.body.innerHTML+=iframeHTML;
					iframeObj = new Object();
					iframeObj.document = new Object();
					iframeObj.document.location = new Object();
					iframeObj.document.location.iframe = document.getElementById('AjaxIFrame');
					iframeObj.document.location.replace = function(location) { this.iframe.src = location; }
				}
			}
			
			if (navigator.userAgent.indexOf('Gecko') !=-1 && !iframeObj.contentDocument) {
				// we have to give NS6 a fraction of a second to recognize the new IFrame
				tempAjaxObj = this;
				setTimeout('tempAjaxObj.connect(sURL,sVars,0)',10);
				return false;
			}
			
			iframeDoc = ( iframeObj.contentDocument ? iframeObj.contentDocument :			// NS6
						( iframeObj.contentWindow ? iframeObj.contentWindow.document :		// IE5.5, IE6
						( iframeObj.document ? iframeObj.document : 0 )));					// IE5
		
			if ( iframeDoc) {
				iframeDoc.location.replace(sURL + '?' + sVars);
				setTimeout('IFrameCheck()', (window.opera ? 250 : 100));
			}

			IFrameCheck = function() {
				var doc = iframeObj.contentDocument || iframeObj.document;
				if (doc.readyState == 'complete') {
					var txt = doc.getElementsByTagName('body')[0].innerHTML;
					if ( privateFunc) privateFunc(txt);
				}
				else
					setTimeout('IFrameCheck()', 50);
			}
			return false;
		};	// connect
	}
	//	---	END IFRAME, BEGIN XMLHTTP ---
	else {
		this.connect = function(sURL,sVars,fnDone)
		{
			if (!xmlhttp) return false;
			bComplete = false;	
			try {
				// eliminated GET method
				xmlhttp.open("POST", sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && !bComplete) {
						bComplete = true;
						privateFunc ? privateFunc(xmlhttp.responseText) : fnDone(xmlhttp);
					}};
				xmlhttp.send(sVars);
			}
			catch(z) { return false; }
			return true;
		};
	}
	return this;
}


/* optional: use this first */
function UserHasAjax() {
	var x = new Ajaxer(),
		hasAJ = (x.ok);
	delete x;
	return hasAJ;
}


/*----- even more simplified access to a simple class.

	usage:
		function CALLBACK(responseText) { alert(responseText); }
		var a = new SimpleAjax( url, vars, CALLBACK);

	which you can then reuse or reset:
		a.reset( url, vars, CALLBACK);

	use the reset if a new request comes in before the old
	one finishes.
*/

// both functions return TRUE if ok, FALSE if not
function SimpleAjax(url,vars,fn) {
	this.myConn = null;
	return this.reset(url,vars,fn);
}
SimpleAjax.prototype.reset = function(url,vars,fn) {
	this.myConn = new Ajaxer();
	if ( this.myConn) {
		this.myConn.setPrivateFunc( fn);
		this.myConn.connect(url, vars);
		return true;
	}
	else
		return false;
}

