ajaxobject = function()
{
	this.url	 	= "";
	this.funktion	= "";
	this.params		= "";
	this.elm		= {};
}

/*-------------------------------------------------------
	Generic script functions
------------------------------------------------------*/

ajaxobject.prototype.keepAlive = function()
{
	this.funktion	= "keepAlive";
	this.get("/pages/adm_odds/editor/functions/keepalive.php");
}

ajaxobject.prototype.addRemoveLink = function(userId,LinkId,obj)
{
	this.funktion	= 'addRemoveLinkResponse';
		
	this.params		= '&userId='+userId+'&linkId='+LinkId;
	
	this.elm		= obj;
	
	this.get('pages/ajax/addlink.ajax.php');
}

/*-------------------------------------------------------
	Generic ajax functions
------------------------------------------------------*/

ajaxobject.prototype.initConnection	= function()
{
	xmlhttp = false;

	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest(); 
	}
	else if (window.ActiveXObject)
	{ 
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	
	return xmlhttp;
}


/*----------------------------------------
	which url
----------------------------------------*/

ajaxobject.prototype.prepareuri	= function(url)
{
	this.url = (url) ? url : (this.url) ? this.url : false;
}

ajaxobject.prototype.prepareparams = function(params)
{
	cnt = 0;
	this.params	= "";	
	
	for(u in params)
	{
		sep 		= (cnt==0) ? "" : "&";
		this.params += "" + sep + "" + u + "=" + encodeURI(params[u]);

		cnt++;
	}
}

/*----------------------------------------
	get and add data
----------------------------------------*/

ajaxobject.prototype.get	= function(url)
{
	var obj = this;
	this.prepareuri(url);
	
	this.xmlhttpGet	= this.initConnection();
	
	if(this.url && this.xmlhttpGet)
	{
		this.xmlhttpGet.onreadystatechange =	function(){obj.writedata();};
		this.xmlhttpGet.open('GET', this.url + "?" + this.params , true);
		
		this.xmlhttpGet.send(null);
	}
	
	//remove ie click
	return false;
}

ajaxobject.prototype.post	= function(url, params)
{
	var obj = this;
	this.prepareuri(url);
	this.prepareparams(params);
	
	this.xmlhttpGet	= this.initConnection();
	
	if(this.url && this.xmlhttpGet)
	{
		this.xmlhttpGet.onreadystatechange = function(){obj.writedata();};
		this.xmlhttpGet.open('POST', this.url, true);
		this.xmlhttpGet.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.xmlhttpGet.setRequestHeader("Content-length", this.params.length);
		this.xmlhttpGet.setRequestHeader("Connection", "close");
		this.xmlhttpGet.send(this.params);

	}
	
	//remove ie click
	return false;
}



/*----------------------------------------
	write data
----------------------------------------*/

ajaxobject.prototype.writedata	= function(d)
{
	if (this.xmlhttpGet.readyState == 4)
	{
		if(this.xmlhttpGet.status == 200)
		{
			eval("json = " + this.xmlhttpGet.responseText);
			
			switch (this.funktion)
			{
				case "addRemoveLinkResponse":
					this.addRemoveLinkResponse(json, this.elm);
					break;
				default : 
					break;
			}
		}
	}
}

ajaxobject.prototype.addRemoveLinkResponse 	= function(data, elm)
{
	for(i=0 ; i<data.length ; i++)
	{
		for(x in data[i])
		{
			if(data[i][x] == 'Add')
			{
				elm.innerHTML = 'Fjern fra favoritlinks';
			}
			else
			{
				elm.innerHTML = 'Tilføj til favoritlinks';
			}
		}
	}
	
}

/*----------------------------------------
	Søren hansen funktion START
----------------------------------------*/

ajaxobject = new ajaxobject;