Ajax = {};

Ajax.setCookie = function(c_name,value)
{
	var exdate=new Date();
	var expiredays = 365;
	exdate.setDate(exdate.getDate()+expiredays);
	top.document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

Ajax.getCookie = function(c_name)
{
	if (top.document.cookie.length>0)
	{
		c_start=top.document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start = c_start + c_name.length+1;
			c_end = top.document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = top.document.cookie.length;
			var ret = unescape(top.document.cookie.substring(c_start,c_end));
			if (!(ret > 0)) { ret = 0; }
			return ret;
		}
	}
	return 0;
}

Ajax.makeRequest = function(method, url, callbackMethod)
{
	this.request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP");
	this.request.onreadystatechange = callbackMethod;
	this.request.open(method, url, true);
	this.request.send(url);
}

Ajax.checkReadyState = function(_id)
{
	switch (this.request.readyState)
	{
		case 4:
			AjaxUpdater.isUpdating = false;
			return this.request.status;
			break;
	}
}

Ajax.getResponse = function()
{
	if (this.request.getResponseHeader('Content-Type').indexOf('xml') != -1)
	{
		return this.request.responseXML.documentElement;
	} else {
		return this.request.responseText;
	}
}

AjaxUpdater = {};
AjaxUpdater.place = "myContext";
AjaxUpdater.ajaxdiv = null;
AjaxUpdater.Creator = false;

AjaxUpdater.initalize = function()
{
	AjaxUpdater.isUpdating = false;
}
AjaxUpdater.initalize();

AjaxUpdater.Update = function(method, service, callback, placeTo, toCreate)
{
	AjaxUpdater.Creator = toCreate;
	if (callback == undefined || callback == "")
	{
		AjaxUpdater.place = placeTo;
		callback = AjaxUpdater.onResponse;
	}
	Ajax.makeRequest(method, service, callback);
	AjaxUpdater.isUpdating = true;
}

AjaxUpdater.onResponse = function()
{
	if (Ajax.checkReadyState('loading') == 200)
	{
		if ((!document.getElementById(AjaxUpdater.place)) && (AjaxUpdater.Creator)) {
			AjaxUpdater.createDiv();
		}

		var exists = (document.getElementById(AjaxUpdater.place)) ? true : false;

		if (exists) {
			document.getElementById(AjaxUpdater.place).innerHTML = Ajax.getResponse();
		}

		AjaxUpdater.isUpdating = false;
	}
}

AjaxUpdater.createDiv = function()
{
	var ajaxdiv = document.createElement('div');
	ajaxdiv.name = AjaxUpdater.place;
	ajaxdiv.id = AjaxUpdater.place;
	ajaxdiv.style.zIndex = 1;
	ajaxdiv.style.position = "absolute";
	ajaxdiv.style.width = "auto";
	ajaxdiv.style.top = Ajax.getCookie(AjaxUpdater.place+"_top");
	ajaxdiv.style.left = Ajax.getCookie(AjaxUpdater.place+"_left");
	AjaxUpdater.ajaxdiv = ajaxdiv;

	document.body.appendChild(ajaxdiv);

}

AjaxUpdater.closeDiv = function()
{
	if (AjaxUpdater.ajaxdiv) {
		document.body.removeChild(AjaxUpdater.ajaxdiv);
	}
}