//this is required to callback functions. read http://www.snook.ca/archives/javascript/javascript_pass/ for more info.
if ( ! Function.prototype.bind ) {
    Function.prototype.bind = function( object ) {
        var __method = this;
        return function() {
            __method.apply( object, arguments );
        };
    };
}

function Ajax(containerID) {
	this.isSupported = !(navigator.userAgent.indexOf("Opera")>=0);
	this.isIE = false;
	this.ID = containerID;
	this.SenderClass = "";
	this.ExecScript = true;
	if (this.isSupported)
	{
		if (window.XMLHttpRequest) {
			this.isSupported = true;
		} else if (window.ActiveXObject) {
			this.isSupported = true;
			this.isIE = true;
		} else
			this.isSupported = false;
	}
	this.container = document.getElementById(containerID);
	this.xmlHttp = null;

	if (false && CustomCursor && this.isSupported)
	{
		var img = document.createElement("IMG");
		img.style.display = "none";
		img.src = "/images/waiting.gif";
		document.body.appendChild(img);
		this.cursor = new CustomCursor(img);
		//this.cursor.offsetX = 9;
		//this.cursor.offsetY = 9;
		this.cursor.element.style.right = "10px";
		this.cursor.element.style.bottom = "30px";
		this.cursor.followMouse = false;
	}

	if (!document.body.ajax)
	{
		document.body.ajax = new Object();
	}
	document.body.ajax[containerID] = this;
	if (this.container==null)
	{
	//	this.isSupported = false;
	}

}

Ajax.prototype.findContainer = function () {
	this.container = document.getElementById(this.ID);
}

Ajax.prototype.init = function () {
    if (window.XMLHttpRequest) {
        this.xmlHttp = new XMLHttpRequest();
		return true;
    } else if (window.ActiveXObject) {
        this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		return true;
    }
	return false;
}

Ajax.prototype.getData = function (url, onCompleteCallBack) {
	if (!this.isSupported)
	{
		return false;
	}

	document.body.style.cursor = "wait";
	if (this.cursor)
		this.cursor.enable();
	if (this.xmlHttp==null)
	{
		this.init();
	}
	this._customCallBack = onCompleteCallBack;
	/*if (onCompleteCallBack==null)
	{
		onCompleteCallBack = this.callBack;
		onCompleteCallBack.ajax = this;
	}
	*/
    if (window.XMLHttpRequest) {
        this.xmlHttp.onreadystatechange = this.callBack.bind(this);//onCompleteCallBack;
        this.xmlHttp.open("GET", url, true);
		this.xmlHttp.setRequestHeader("AjaxCall", "true");
		this.xmlHttp.setRequestHeader("AjaxID", this.ID);
		this.xmlHttp.setRequestHeader("AjaxSenderClass", this.SenderClass);
        this.xmlHttp.send(null);
		return true;
    } else if (window.ActiveXObject) {
		this.init();
        if (this.xmlHttp) {
            this.xmlHttp.onreadystatechange = this.callBack.bind(this);//onCompleteCallBack;
            this.xmlHttp.open("GET", url, true);
			this.xmlHttp.setRequestHeader("AjaxCall", "true");
			this.xmlHttp.setRequestHeader("AjaxID", this.ID);
			this.xmlHttp.setRequestHeader("AjaxSenderClass", this.SenderClass);
            this.xmlHttp.send();
			return true;
        }
    }
	return false;
}

Ajax.prototype.postData = function (url, data, onCompleteCallBack) {
	if (!this.isSupported)
	{
		return false;
	}

	document.body.style.cursor = "wait";
	if (this.cursor)
		this.cursor.enable();
	if (this.xmlHttp==null)
	{
		this.init();
	}
	this._customCallBack = onCompleteCallBack;
	/*
	if (onCompleteCallBack==null)
	{
		onCompleteCallBack = this.callBack;
		onCompleteCallBack.ajax = this;
		//onCompleteCallBack = document.body.ajax[this.ID].callBack;
	}
	*/
	//onCompleteCallBack.ajax = this;
	//alert(onCompleteCallBack);
	//alert(onCompleteCallBack.ajax);
	//alert(onCompleteCallBack);

    if (window.XMLHttpRequest) {
        this.xmlHttp.onreadystatechange = this.callBack.bind(this);//onCompleteCallBack;
		this.xmlHttp.open("POST", url, true);
		this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.xmlHttp.setRequestHeader("AjaxCall", "true");
		this.xmlHttp.setRequestHeader("AjaxID", this.ID);
		this.xmlHttp.setRequestHeader("AjaxSenderClass", this.SenderClass);
		this.xmlHttp.send(data);//"ajax="+this.ID);
		return true;
    } else if (window.ActiveXObject) {
		this.init();
        if (this.xmlHttp) {
            this.xmlHttp.onreadystatechange = this.callBack.bind(this);//onCompleteCallBack;
			this.xmlHttp.open("POST", url, true);
			this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.xmlHttp.setRequestHeader("AjaxCall", "true");
			this.xmlHttp.setRequestHeader("AjaxID", this.ID);
			this.xmlHttp.setRequestHeader("AjaxSenderClass", this.SenderClass);
			this.xmlHttp.send(data);//"ajax="+this.ID);
			return true;
        }
    }
	return false;
}

Ajax.prototype.callBack = function () {
	if (this.xmlHttp.readyState == 4) {
		document.body.style.cursor = "default";
		if (this.cursor)
			this.cursor.disable();
	}
	if (this._customCallBack && this._customCallBack(this))
		return;

	try
	{
		if (this.xmlHttp.readyState == 4) {
			if (this.xmlHttp.status == 200) {
				if (this.container!=null)
				{
					var txt = this.xmlHttp.responseText;
					this.container.innerHTML=txt;

					if (this.ExecScript)
					{
						var re = new RegExp("<script.*>([^\0]*)</script>", "mi")
						var matches = txt.match(re)
						if (matches && matches.length>1)
						{
							eval(matches[1]);
						}
					}
				}
			} else {
				window.status="AJAX error: " + this.xmlHttp.statusText;
			}
		}
	}
	catch (e)
	{
		window.status="AJAX error: " + e.message;
	}
}

Ajax.prototype.complete = function () {
	try
	{
		if (this.xmlHttp.readyState == 4) {
			
			if (this.xmlHttp.status == 200) {
				return true;
			}
		}	
	}
	catch (e)
	{
	}
	return false;
}

Ajax.prototype.data = function () {
	return this.xmlHttp.responseText;
}


var isIE = false;
var req;
var spanid = '';

function ajax_GetData(url, onCompleteCallBack) {
    if (navigator.userAgent.indexOf("Opera")>=0)
    {
        alert("Opera not supported...")
        return;
    }

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = onCompleteCallBack;
        req.open("GET", url, true);
        req.send(null);
    }
 else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = onCompleteCallBack;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            buildTopicList();
         }
 else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
         }
    }
}


function loadsubs(subpage,spanner) {
	document.getElementById(spanner).innerHTML = "Attempting Login, Please Wait...";
	spanid = spanner;
	loadXMLDoc(subpage);
}
function buildTopicList() {
    var str = req.responseText;
	var subtype_array = str.split("|");
	var x = 0;
	var tempstr = "";

	if (subtype_array[0] == "2") {
			tempstr = subtype_array[1];
			Effect2.Appear('edwarning',{duration:2.0, transition:Effect2.Transitions.wobble});
		    document.getElementById(spanid).innerHTML = tempstr;
			Effect2.SlideUp('edsubpanel')
	} else {
		if (subtype_array[0] == "0") {
			tempstr = subtype_array[1];
			Effect2.Shake('edmainpanel');
			Effect2.Appear('edwarning',{duration:2.0, transition:Effect2.Transitions.wobble});
		    document.getElementById(spanid).innerHTML = tempstr;
		} else {
			 window.location = subtype_array[1];
		}
	}
}

function loadXMLDocPass(url) {
	url = url + "?login=" + document.passform.login.value;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    }
 else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function loadsubsPass(subpage,spanner) {
	spanid = spanner;
	loadXMLDocPass(subpage);
}
