function AjaxObject(url) {                                           // This is the object constructor
	var that=this;                                                    // A workaround for some javascript idiosyncrocies
	var updating = false;                                             // Set to true if this object is already working on a request
	this.callback = function() {}                                     // A post-processing call -- a stub you overwrite.

	this.update = function(passData, method) {                        // Initiates the server call.
		if (!method) { method = "POST"; }
		if (updating==true) { return false; }                          // Abort if we're already processing a call.
//		updating=true;                                                 // Set the updating flag.
		var AJAX = null;                                               // Initialize the AJAX variable.
		try {
			AJAX = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				AJAX = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					AJAX = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					AJAX = null;
				}
			}
		}
		if (AJAX==null) {                                              // If we couldn't initialize Ajax...
			alert("Ihre Browser unterstützt kein AJAX.");               // Sorry msg.                                              
			return false                                                // Return false (WARNING - SAME AS ALREADY PROCESSING!)
		} else {
			AJAX.onreadystatechange = function() {                      // When the browser has the request info..
				if (AJAX.readyState==4 || AJAX.readyState=="complete") { //   see if the complete flag is set.
					if (AJAX.status == 200) {
						that.data = AJAX.responseText.replace(/^\s*([\S ]+)\s*$/,'$1'); //   It is, so put the new data in the object's layer
						that.responseText = AJAX.responseText;
						that.responseXML = AJAX.responseXML;
						delete AJAX;                                       //   delete the AJAX object since it's done.
						updating=false;                                    //   Set the updating flag to false so we can do a new request
						that.callback();                                   //   Call the post-processing function.
					} else {
						// AJAX response error
						alert("Fehler in AJAX Antwort.");
					}
				}                                                        // End Ajax readystate check.
			}                                                           // End create post-process fucntion block.
			var timestamp = new Date();                                 // Get a new date (this will make the url unique)
			if (method == 'POST') {
				AJAX.open(method, urlCall, true);                        // Open the url this object was set-up with.
				AJAX.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
				AJAX.setRequestHeader('Content-length', passData.length);
				AJAX.send(passData);
			} else {
				var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1); // Append date to url (so the browser doesn't cache the call)
				AJAX.open(method, uri, true);                             // Open the url this object was set-up with.
				AJAX.send(null);                                          // Send the request.
			}
			return true;                                                // Everything went a-ok.
		}                                                              // End Ajax setup aok if/else block                 
	}
	this.send = this.update;
	
	// This area set up on constructor calls.
	var urlCall = url;                                                // Remember the url associated with this object.
}         	                                                         // End AjaxObject
var ajaxObject = AjaxObject;

to_check_entry = null;
to_lock_entry = null;
function whcmsLockEntry (system_id, entry_id) {
	var ajax = new ajaxObject('ajax_lock_entry.cfm');
	ajax.callback = whcmsLockEntryResponse;
	ajax.system_id = system_id
	ajax.entry_id = entry_id
	ajax.update('resy=' + system_id + '&id=' + entry_id);
}
function whcmsLockEntryResponse() {
	var msg = this.responseText.split('|');
	if (msg[0] == 'OK') {
		window.status = msg[1];
	} else if (msg[0] == 'Error') {
		if (msg[2] == 1) {
			alert(msg[1] + '\n\n Der Datensatz wird jetzt im Lesemodus angezeigt.');
			var dt = new Date();
			location.href = 'edit.cfm?id=' + this.entry_id + '&ts=' + dt.getTime();
		} else {
			alert(msg[1]);
		}
	}
	if (to_lock_entry) {
		window.clearTimeout(to_lock_entry);
	}
	to_lock_entry = window.setTimeout('whcmsLockEntry(' + this.system_id + ', ' + this.entry_id + ')', 30000);
}
function whcmsCheckEntry (system_id, entry_id) {
//	if ($('whcms_entry_status')) {
		var ajax = new ajaxObject('ajax_check_entry.cfm');
		ajax.callback = whcmsCheckEntryResponse;
		ajax.system_id = system_id
		ajax.entry_id = entry_id
		ajax.update('resy=' + system_id + '&id=' + entry_id);
//	}
}
function whcmsCheckEntryResponse() {
	var msg = this.responseText.split('|');
	if (msg[0] == 'Unlocked' || msg[0] == 'Locked') {
		with ($('whcms_entry_status')) {
			innerHTML = msg[1];
			className = msg[2];
		}
	}
	to_check_entry = window.setTimeout('whcmsCheckEntry(' + this.system_id + ', ' + this.entry_id + ')', 30000);
}


















var ajax = false;
var w = null;

function get_ajax() {
	try {
		ajax = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				ajax = false;
			}
		}
	}
	return ajax;
}
/*	try {
		if (window.ActiveXObject) {
			for (var i = 5; i; i-- ) {
				try {
					if (i == 2) {
						ajax = new ActiveXObject("Microsoft.XMLHTTP");    
					} else {
						ajax = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0");
					}
				} catch(excNotLoadable) {
					ajax = false;
				}
			}
		} else if(window.XMLHttpRequest) {
			ajax = new XMLHttpRequest();
	          if (ajax.overrideMimeType) {
                ajax.overrideMimeType('text/xml');
            }
		}
	} catch(excNotLoadable) {
		ajax = false;
	}
} */

get_ajax();

function ajax_request(url, data, func, method) {
	if (!method) {
		method = 'GET';
	}
	// ggf. alten Request abbrechen
	if (ajax && ajax.readyState) {
		ajax.abort();
	} else if (!ajax) {
		return void(0);
	}
	// Request senden
	if (method == 'POST') {
		ajax.open(method, url, true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.setRequestHeader('Content-length', data.length);
	} else {
		urldata = '';
		if (data != null && typeof data == 'string' && data.length > 0) {
			if (url.split('?').length > 1) {
				urldata = '&' + data;
			} else {
				urldata = '?' + data;
			}
		}
		ajax.open(method, url + urldata, true);
	}
	if (typeof func == 'function') {
		ajax.onreadystatechange = func;
	} else if (func != null) {
		ajax.onreadystatechange = new Function('', func);
	} else {
		ajax.onreadystatechange = make_ajax_response;
	}
	ajax.send(data);
}
