// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var xmlHttp1 = createXmlHttpRequestObject();
var tekstAkcijaTMP='';
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
	else
	return xmlHttp;
}

// read a file from the server
function process()
{
	// only continue if xmlHttp isn't void
	if (xmlHttp)
	{
		// try to connect to the server
		try
		{
			// initiate reading a file from the server
			xmlHttp.open("POST", "ajaksServer.php", true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			//------xmlHttp.send("param1=x&param2=y");
			xmlHttp.send(null);			
		}
		// display the error in case of failure
		catch (e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
	
}
function AjaxAkcijePogledane(id)
{
	// only continue if xmlHttp isn't void
	if (xmlHttp1)
	{
		// try to connect to the server
		try
		{
			// initiate reading a file from the server
			xmlHttp1.open("POST", "akcijePogledane.php", true);
			xmlHttp1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//xmlHttp1.onreadystatechange = handleRequestStateChange1;			
			xmlHttp1.send("IDakcije="+id);
			//xmlHttp1.send(null);			
		}
		// display the error in case of failure
		catch (e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
	
}
// function called when the state of the HTTP request changes
function handleRequestStateChange()
{
// when readyState is 4, we are ready to read the server response
if (xmlHttp.readyState == 4)
{
	// continue only if HTTP status is "OK"
	if (xmlHttp.status == 200)
	{
		try
		{
			// do something with the response from the server
			handleServerResponse();
		}
		catch(e)
		{
			// display error message
			//alert("Error reading the response: " + e.toString());
		}
	}
	else
	{
		// display status message
		alert("There was a problem retrieving the data:\n" +xmlHttp.statusText);
	}
}
}

// function called when the state of the HTTP request changes
function handleRequestStateChange1()
{
// when readyState is 4, we are ready to read the server response
if (xmlHttp1.readyState == 4)
{
	// continue only if HTTP status is "OK"
	if (xmlHttp1.status == 200)
	{
		try
		{
			// do something with the response from the server
			handleServerResponse1();
		}
		catch(e)
		{
			// display error message
			alert("Error reading the response1111: " + e.toString());
		}
	}
	else
	{
		// display status message
		alert("There was a problem retrieving the data:\n" +
		xmlHttp1.statusText);
	}
}
}


// handles the response received from the server
function handleServerResponse()
{
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
	throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Invalid XML structure");
	// obtain the XML's document element
	
	xmlRoot = xmlResponse.documentElement;
	// obtain arrays with book titles and ISBNs
	sadrzajArray = xmlRoot.getElementsByTagName("sadrzaj");
	vrstaArray = xmlRoot.getElementsByTagName("vrsta");
	// generate HTML output
	var html = "";
	// iterate through the arrays and create an HTML structure
	
	
	tekstAkcija='';	
	for (var i=0; i<vrstaArray.length; i++)
	{
		if(vrstaArray.item(i).firstChild.data=='1') 
		{
			alert(sadrzajArray[i].firstChild.data);
			if (document.location.href.indexOf('porukeFanovaJednog.php')>0)
				document.location.reload(true);
		}
		if(vrstaArray.item(i).firstChild.data=='2')
			if (document.getElementById("brojPosetilaca")) document.getElementById("brojPosetilaca").innerHTML=sadrzajArray[i].firstChild.data;
		if(vrstaArray.item(i).firstChild.data=='3')
		{
			if (document.getElementById("brojPoruka"))
			{
				brPor=sadrzajArray[i].firstChild.data;
				if (brPor==0) brPor='';else brPor='('+brPor+')';
				document.getElementById("brojPoruka").childNodes[0].childNodes[2].innerHTML=brPor;
			}			
		}
		if(vrstaArray.item(i).firstChild.data=='4')
		{
			tekstAkcija=tekstAkcija+sadrzajArray[i].firstChild.data;														
		}	
	}		
	if (tekstAkcija!='' && tekstAkcijaTMP!=tekstAkcija )	
	{				
		if (document.getElementById("tab3"))
		{	
					
			
			document.getElementById("tab3").innerHTML='<div id="kontejnerSK" class="kontejnerSK" ><!-- kontejner --->'+
			'<div id="kontejnerSadrzajSK" class="kontejnerSadrzajSK" ><!-- kontejner sadrzaj--->'+
			'<div id="proba">'+tekstAkcija+'</div>'+
			'<!-- ovde stoji sadrzaja --->'+
			'</div><!-- kontejner sadrzaj kraj--->'+
			'<div id=scrollBarNosilacUspSK class="scrollBarNosilacUspSK" ><!-- skrolbar nosilac uspravni--->'+
			'<div id=scrollBarTrakaUspSK class="scrollBarTrakaUspSK">'+
			'<div id=scrollBarThumbUspSK class="scrollBarThumbUspSK" onmousedown="dragOBJ(this,event); return false;"></div>'+
			'</div>'+
			'</div>';

			//document.getElementById("tab33").innerHTML=tekstAkcija;
			if (tekstAkcijaTMP!='') Pripremi();
			
			document.getElementById("tab_obav").style.display='block';				
			tekstAkcijaTMP=tekstAkcija;			
		}
	}
	else if (tekstAkcija=='' && tekstAkcijaTMP!=tekstAkcija )  
	{
		pokaziTab(1,document.getElementById("tabtab1"));
		document.getElementById("tab_obav").style.display='none';
		tekstAkcijaTMP='';
	}
	
	
	
	//html += vrstaArray.item(i).firstChild.data +", " + sadrzajArray.item(i).firstChild.data + "<br/>";
	// obtain a reference to the <div> element on the page
	//myDiv = document.getElementById("myDivElement");
	// display the HTML output
	//myDiv.innerHTML = html;
	//process();	
	mytime=setTimeout('process()',2000);	
}



// handles the response received from the server
function handleServerResponse1()
{
	alert(222)
	// read the message from the server
	var xmlResponse = xmlHttp1.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
	throw("Invalid XML structure:\n" + xmlHttp1.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Invalid XML structure");
	// obtain the XML's document element
	
	xmlRoot = xmlResponse.documentElement;
	// obtain arrays with book titles and ISBNs
	sadrzajArray = xmlRoot.getElementsByTagName("sadrzaj");
	vrstaArray = xmlRoot.getElementsByTagName("vrsta");
	// generate HTML output
	var html = "";
	// iterate through the arrays and create an HTML structure
	
	
	tekstAkcija='';	
	for (var i=0; i<vrstaArray.length; i++)
	{
		if(vrstaArray.item(i).firstChild.data=='1') 
		{
			alert(sadrzajArray[i].firstChild.data);
			if (document.location.href.indexOf('porukeFanovaJednog.php')>0)
				document.location.reload(true);
		}
		if(vrstaArray.item(i).firstChild.data=='2')
			if (document.getElementById("brojPosetilaca")) document.getElementById("brojPosetilaca").innerHTML=sadrzajArray[i].firstChild.data;
		if(vrstaArray.item(i).firstChild.data=='3')
		{
			if (document.getElementById("brojPoruka"))
			{
				brPor=sadrzajArray[i].firstChild.data;
				if (brPor==0) brPor='';else brPor='('+brPor+')';
				document.getElementById("brojPoruka").childNodes[0].childNodes[2].innerHTML=brPor;
			}			
		}
		if(vrstaArray.item(i).firstChild.data=='4')
		{
			tekstAkcija=tekstAkcija+sadrzajArray[i].firstChild.data;														
		}	
	}		
	
	//mytime=setTimeout('process()',2000);	
}