function detectBrowser()
{	
	browser = navigator.appName;

	//detecting older netscapes
	if (!document.all)
		browser = "old netscape"
	else if(navigator.userAgent.indexOf("Opera") > -1)
		browser = "opera";
	else if (navigator.userAgent.indexOf("MSIE") > -1)
		browser = "ie";
	else if (navigator.appName == "Netscape" && document.all)
		browser = "netscape";

	gBrowser = browser;
	return browser;
}

function createRequestObject()
{
	var browser = detectBrowser();
	if(browser == "ie")
		return new ActiveXObject("Microsoft.XMLHTTP");
	return new XMLHttpRequest();
}


var http = createRequestObject();
var http2 = createRequestObject();
var http3 = createRequestObject();
var lastRequest = "";
var lastRequestTime = "";
var lastRequestResponse = "";



function sndReq(file, param1, param2, param3, param4, httpIndex, waitTime)
{
	// Spam security
	newRequest = file + param1 + param2 + param3;
	now = new Date();
	now = now.getTime();
	if(newRequest == lastRequest && now - lastRequestTime < waitTime)
	{
		// If makes the same request more than once in a short time
		handleResponse(lastRequestResponse);
		return;
	}

	lastRequest = newRequest;
	lastRequestTime = now;
	
	// Create request object
	if(!http)
		http = createRequestObject();
	if(!http2)
		http2 = createRequestObject();

	//http.setRequestHeader("Content-Type","application/x-javascript; charset:ISO-8859-1");
	//http2.setRequestHeader("Content-Type","application/x-javascript; charset:ISO-8859-1");

	if(isLocal)
		file = "/dit/" + file;
	else
		file = "/" + file;

	var sendString = file;
	if(file.indexOf(".php") == -1)
		sendString += ".php";

	if(param1)	sendString += '?param1='+param1;
	if(param2)	sendString += '&param2='+param2;
	if(param3)	sendString += '&param3='+param3;
	if(param4)	sendString += '&param4='+param4;

	//alert(sendString);
	
	if(httpIndex && httpIndex == 2)
	{
		http2.open('get', sendString);
		http2.onreadystatechange = handleResponse;
		http2.send(null);
	}
	else
	{
		http.open('get', sendString);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
}

function sndReq2(file, param1, param2, param3, param4, param5, param6, param7, httpIndex)
{
	if(!http)
		http = createRequestObject();
	if(!http2)
		http2 = createRequestObject();

	if(isLocal)
		file = "/dit/" + file;
	else
		file = "/" + file;
	//alert(file);

	sendString = file	
	if(param1)	sendString += '?param1='+param1;
	if(param2)	sendString += '&param2='+param2;
	if(param3)	sendString += '&param3='+param3;
	if(param4)	sendString += '&param4='+param4;
	if(param5)	sendString += '&param5='+param5;
	if(param6)	sendString += '&param6='+param6;
	if(param7)	sendString += '&param7='+param7;

	
	if(httpIndex && httpIndex == 2)
	{
		http2.open('get', sendString);
		http2.onreadystatechange = handleResponse;
		http2.send(null);
	}
	else
	{
		http.open('get', sendString);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
}

/*
Statusar:
switch(http2.readyState)
	{
		//case 0:	// UNINITIALIZED
		case 1:		// LOADING
			break;
		case 2:		// LOADED
			break
		case 3:		// INTERACTIVE
			break;
		case 4:		// COMPLETE
			break;
	}*/
function handleResponse(cachedResponse)
{
	if(!cachedResponse)
	{
		if(http.readyState != 4 && http2.readyState != 4)
			return;
		
		kind = (http.readyState == 4) ? '' : 2;
		response = (http.readyState == 4) ? http.responseText : http2.responseText;
		lastRequestResponse = response;
		eval("http" + kind + " = 0;");	// Clear http object
	}
   
	var update = new Array();
	if(response.length == 0)
		return;

	splitIndex = response.indexOf('|');
	if(splitIndex  == -1)
		return;

	

	//update = response.split('|');
	update = Array();
	update[0] = response.substring(0, splitIndex);
	update[1] = response.substring(splitIndex+1);
	
	if(update[0] == "")
	{
		alert("Inget mottagarelement var angivet\n\n" + response);
		return;
	}
	else if(update[0] == "js") // Set JS-value
	{
		splitIndex = update[1].indexOf('|');
		update[2] = update[1].substring(splitIndex+1);
		update[1] = update[1].substring(0,splitIndex);
		element = getElement(update[1]);
		
		if(!element)
			alert("Error: Element '" + update[1] + "' was not found! (js)");

		element.value = update[2];
		responseCB(update[1], update[2]);
	}
	else
	{
		element = getElement(update[0]);
		if(!element)
			alert("Error: Element '" + update[0] + "' was not found!");
		element.innerHTML = update[1];
		responseCB(update[0], update[1]);
	}
}

function responseCB(receiver, value, value2)
{
	//alert(value2);
	if(receiver == "toolTip")
	{
		if(value.indexOf('<div id="threadPreviewBase">') >= 0)
		{
			threadsPreset();
		}
	}
	else if(receiver == "blogCategories")
	{
		
	}
	else if(receiver == "profileContent")
	{
		if(value.indexOf('id="newPost"') >= 0)
		{
			profilePreset();
		}
		
	}
	else if(receiver == "tabContentReal") // login welcome
	{
		threadsPreset();
		chatTabShift();
	}
	else if(receiver == "visitCard")
	{
		prepareVCScripts();
	}
	else if(receiver.substring(0, 5) == "chat_")
	{
		chatScrollDown(receiver.substring(5));
	}
}
