// Ani-link AJAX extension
// Zolka

// global variables
var xmlHttpReq = null;
var waitingForAnswer = false;
var sessionCode = 0;

var uri = new Object();		// will contain the actual url
GetURL(uri);							// Gets the actual url


function AJAX_GetXMLHttpReqObj()
{
	var result = null;
	
	if (window.XMLHttpRequest)
	{
		// If IE7, Mozilla, Safari, etc: Use native object
		result = new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		// ...otherwise, use the ActiveX control for IE5.x and IE6
		result = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return result;
}

function AJAX_CreateUrl() { return uri.dir + "/index.php?ajax=1&sessioncode=" + sessionCode; }

function AJAX_SendRequest(postparams)
{
//	var postparams = "lorem=ipsum&name=binny";

	if (waitingForAnswer && (xmlHttpReq != null))
		xmlHttpReq.abort();
	
	xmlHttpReq = AJAX_GetXMLHttpReqObj();
	if (xmlHttpReq != null)
	{
		xmlHttpReq.open('POST', AJAX_CreateUrl(), true);
		xmlHttpReq.onreadystatechange = AJAX_ReceiveData;
		xmlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttpReq.setRequestHeader("Content-length", postparams.length);
		xmlHttpReq.setRequestHeader("Connection", "close");
		xmlHttpReq.send(postparams);
		waitingForAnswer = true;
	}
}

function AJAX_ReceiveData()
{
	try
	{
		if (xmlHttpReq.readyState == 4)
		{
			if(xmlHttpReq.status == 200)
				AJAX_ParseResponse(xmlHttpReq.responseText);

			waitingForAnswer = false;
//			alert("Response: \""+xmlHttpReq.responseText+"\"");
		}
	}
	catch(e)
	{
		if (e.name == "NS_ERROR_NOT_AVAILABLE")			// avoid NS_ERROR_NOT_AVAILABLE
			return;

//		alert("Error at reading the received data: \n" + e);
	}

	return;
}

function AJAX_ParseResponse(responseText)
{
	var xmlDoc = null;

	if (typeof(DOMParser) == 'undefined')
	{
		if(typeof(ActiveXObject) != 'undefined') 
		{
			xmlDoc = new ActiveXObject('MSXML.DomDocument');
			xmlDoc.async = false;
			xmlDoc.loadXML(responseText);
		} 
	  else if (typeof(XMLHttpRequest) != 'undefined') 
	  {
			var xmldata = new XMLHttpRequest;

			xmldata.open('GET', 'data:application/xml;charset=utf-8,' + encodeURIComponent(responseText), false);
			if(xmldata.overrideMimeType) 
				xmldata.overrideMimeType('application/xml');
			xmldata.send(null);
			xmlDoc = xmldata.responseXML;	
		}
	}
	else
	{
		xmlDoc = (new DOMParser()).parseFromString(responseText, "text/xml");
	}

	if (!xmlDoc)
		return;

	// convert the string to an XML object
	waitingForAnswer = false;

	var functionName = GetTextContent(xmlDoc.documentElement.childNodes[0]);
	var listVariable = null;
	var paramObj = xmlDoc.documentElement.childNodes[1];
	var params = ''; var comma = '';
	for(var i = 0; i < paramObj.childNodes.length; i++)
	{
		var node = paramObj.childNodes[i];
		if (node.getAttribute('listparam'))		// .hasAttribute
			params += (comma + "listVariable");
		else
			params += (comma + '"' + GetTextContent(node) + '"');
		comma = ', ';
	}
	
	if (xmlDoc.documentElement.childNodes.length > 2)
		listVariable = xmlDoc.documentElement.childNodes[2];
	
	if (eval('typeof window.'+functionName) == 'function')
		eval(functionName+'('+params+')');
	return;
}

// Current Page Reference
// Copyright Stephen Chapman, 1st Jan 2005
// You may copy this function but please keep the copyright notice with it.
function GetURL(uri) 
{
	uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
	uri.dom = uri.dir; 
	if (uri.dom.substr(0, 7) == 'http:\/\/') 
		uri.dom = uri.dom.substr(7);
		
	uri.path = ''; 
	var pos = uri.dom.indexOf('\/'); 
	if (pos > -1) 
	{
		uri.path = uri.dom.substr(pos + 1); 
		uri.dom = uri.dom.substr(0, pos);
	}
	
	uri.page = location.href.substring(uri.dir.length + 1, location.href.length + 1);
	pos = uri.page.indexOf('?');
	if (pos > -1) 
		uri.page = uri.page.substring(0, pos);
	
	pos = uri.page.indexOf('#');
	if (pos > -1) 
		uri.page = uri.page.substring(0, pos);
	
	uri.ext = ''; 
	pos = uri.page.indexOf('.');
	if (pos > -1) 
	{
		uri.ext = uri.page.substring(pos + 1); 
		uri.page = uri.page.substr(0, pos);
	}
	
	uri.file = uri.page;
	if (uri.ext != '') 
		uri.file += '.' + uri.ext;
	if (uri.file == '') 
		uri.page = 'index';
		
	uri.args = location.search.substr(1).split("?");
	return uri;
}                  

function GetTextContent(node)
{
	if (!node) return '';
	
	if (typeof(node.text) == 'string')
		return node.text;
	if (typeof(node.textContent) == 'string')
		return node.textContent;
		
	alert('TextContent cannot be retrieved!');
}

// ----------------------------------------------------
// Session code changing:

function SetSessionCode(newcode)
{
	sessionCode = newcode;
}
function RequestNewCode()
{
	if (sessionCode != 0)
		AJAX_SendRequest("action=requestnewcode");
}
setInterval("RequestNewCode()", 570000);	// requesting new code in every 9.5th minutes

// ----------------------------------------------------