// ap Ajax.

// default functions
function apAjax_loadedFunction(obj){
	apGui_satus(obj.url +" is loaded ");
}

function apAjax_errorFunction(obj){
	alert("There was a problem retrieving the XML data from:\n" + obj.url +" (status: "+ obj.XMLobject.status +")" );
}



// end default functions

function apAjax(){
	this.settings = new Object();
	this.isIE = false;
	this.isLoaded = apAjax_loadedFunction;
	this.isError = apAjax_errorFunction;
	this.asyncFlag = true;
	this.userName = false;
	this.password = false;
	this.readyState_1 = function(){return;};
	this.readyState_2 = function(){return;};
	this.readyState_3 = function(){return;};
	return this;
}


function apAjax_loadXMLDoc(obj) {
	obj.XMLobject = false;
	try {
		if (window.XMLHttpRequest) {
        	obj.XMLobject = new XMLHttpRequest();
    	} else if (window.ActiveXObject) {
        	obj.isIE = true;
        	obj.XMLobject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	if (obj.XMLobject) {
    		obj.XMLobject.onreadystatechange = function(){
    			XMLHttpRequest_readyState(obj);
    		}
			obj.XMLobject.open("GET", obj.url, true);
			if (obj.isIE) {
				obj.XMLobject.send();
			} else {
				obj.XMLobject.send(null);
			}
        }
	}
	catch(e){
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		alert("Unable to get XML data:\n" + msg);
		return;
	}
}

function apAjax_postData(obj) {
	obj.XMLobject = false;
	try {
		if (window.XMLHttpRequest) {
        	obj.XMLobject = new XMLHttpRequest();
    	} else if (window.ActiveXObject) {
        	obj.isIE = true;
        	obj.XMLobject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	if (obj.XMLobject) {
    		obj.XMLobject.onreadystatechange = function(){
    			XMLHttpRequest_readyState(obj);
    		}
			if(!obj.userName) {
				obj.XMLobject.open("POST", obj.url, obj.asyncFlag);
			} else {
				obj.XMLobject.open("POST", obj.url, obj.asyncFlag, obj.userName, obj.password);
			}
			obj.XMLobject.setRequestHeader("Method", "POST " + obj.url + " HTTP/1.1");
			obj.XMLobject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			obj.XMLobject.send(obj.data);
        }
	}
	catch(e){
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		alert("Unable to get XML data:\n" + msg);
		return;
	}
}


function XMLHttpRequest_readyState(obj) {
	/*
	Object status integer:
	0 = uninitialized
	1 = loading
	2 = loaded
	3 = interactive
	4 = complete
	*/
	switch (obj.XMLobject.readyState) {
		case 1:
			//apGui_satus(obj.XMLobject.readyState + " - " + obj.XMLobject.status);
			obj.readyState_1(obj);
		break;
		case 2:
			//apGui_satus(obj.XMLobject.readyState + " - " + obj.XMLobject.status);
			obj.readyState_2(obj);
		break;
		case 3:
			//apGui_satus(obj.XMLobject.readyState + " - " + obj.XMLobject.status);
			obj.readyState_3(obj);
		break;
		case 4:
			//apGui_satus(obj.XMLobject.readyState + " - " + obj.XMLobject.status);
			if (obj.XMLobject.status == 200) {
				obj.isLoaded(obj);
			} else {
				obj.isError(obj);
			}
	}
}

// end ap Ajax.