var xmlBrowserType = null;

// MSIE workarounds (MSIE SUCKS!)
function xmlGetTextContent(node)
{
	if(xmlBrowserType == 'IE') { return node.text; }
	return node.textContent;
}

function newXMLRequest()
{
	var req = null;

	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
			xmlBrowserType = 'W3C';
		} catch(e) {
			req = false;
		}

	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
	   	try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
			xmlBrowserType = 'IE';
	  	} catch(e) {
			try {
		  		req = new ActiveXObject("Microsoft.XMLHTTP");
		  		xmlBrowserType = 'IE';
			} catch(e) {
		  		req = false;
			}
		}
	}
	return req;
}

// Retrieve the value of the cookie with the specified name.
// Got this from MSDN
function GetCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0])
		{
			return unescape(aCrumb[1]);
		}
	}

	// a cookie with the requested name does not exist
	return null;
}

function serialize(arr, amp)
{
	if(amp == null) { amp = "&"; }

	var str = "";
	var first = 1;
	for(var key in arr)
	{
		if(!first) { str += amp; } else { first = 0; }
		str += key + "=" + escape(arr[key]);
	}
	return str;
}

function loadXMLDoc(url, inCallback)
{
	var req = null;
	var callback = inCallback;

	function ajax_closure() { callback(req); };

	if(req = newXMLRequest())
	{
		req.onreadystatechange = ajax_closure;
		req.open("GET", url, true);
		req.send("");
	}
}

function postXMLDoc(url, inCallback, post)
{
	var req = null;
	var callback = inCallback;

	function ajax_closure() { callback(req); };

	if(req = newXMLRequest())
	{
		req.onreadystatechange = ajax_closure;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(post);
	} else {
		alert("Could not connect to the network.\n");
	}
}






var rating_running = 0;
function ajax_rating_callback(req)
{
//	document.writeln("readyState = " + req.readyState + ", status=" + req.status +  "[" + req.statusText + "]");

	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...

//			alert( req.responseText );
			comments = req.responseXML.getElementsByTagName('comment');
//			alert(comments.length);
			var detailtext;
			for (var cid = 0; cid < comments.length; cid++)
			{
				var c = comments.item(cid);
				//alert(c);
				var commentid = parseInt( c.getAttribute('commentid').toString() );
				var myrating = parseInt( c.getAttribute('myrating').toString() );
				for(var nid = 0; nid < c.childNodes.length; nid++)
				{
					var node = c.childNodes.item(nid);

					if(node.tagName == 'detail')
					{
						detailtext = xmlGetTextContent(node);
					}
				}
			}

			// process details nodes
			var detnode = document.getElementById("comdet_" + commentid);
			detnode.innerHTML = detailtext;

			// process select tag
			var sel = document.getElementById("comsel_" + commentid);
			for(var oid = 0; oid < sel.options.length; oid++)
			{
				var node = sel.options.item(oid);

				var s = node.value.split(' ');
				node.value = s[0] + ' ' + myrating;
			}

			// set OK tickmark
			var ok = document.getElementById("comvis_" + commentid);
			if(myrating == 1)
			{
				ok.innerHTML = "<img src='/images/white.png' />";
			} else {
				ok.innerHTML = "<img src='/images/white.png' />";
			}

			// mission accomplished
			rating_running--;
		} else {
			alert("There was a problem retrieving the XML data:\n" +
				req.statusText);
		}
	}
}

function ajax_rate(sel)
{
	sel.blur();
	var commentid = sel.id.substring(7);

	// set "running" clock and increment "running" variable
	var ok = document.getElementById("comvis_" + commentid);
	ok.innerHTML = "<img src='/images/Throbber-small.gif' />";
	rating_running++;

	var arr = Array();
	arr["commentid"] = commentid
	arr["rating"] = sel.options[sel.selectedIndex].value;
	postXMLDoc("http://portal.connect.znanost.org/?option=intrinsic&module=ratings",
		ajax_rating_callback, serialize(arr));
}

function ajax_rate_onunload()
{
	if(running == 0) { return; }

	var recordVotes = confirm("Paznja: odlazite sa stranice bez da ste potvrdili vase glasove. Zelite li potvrditi glasove sada (ukoliko odgovorite 'ne', promjene koje ste napravili biti ce zaboravljene)");
	if(recordVotes)
	{
	}
}
