var isIE = false;

//global request and XML document objects
var req;

var obj_target;

//retrieve XML document (reusable generic function);
//parameter is URL string (relative or complete) to
//an .xml file whose Content-Type is a valid XML
//type, such as text/xml; XML source must be from
//same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
 if (window.XMLHttpRequest) {
     req = new XMLHttpRequest();
     if(req){
	req.onreadystatechange = processReqChange;
     	req.open("GET", url, true);
     	req.send(null);
     	//alert(url);
     	//alert(req.responseText);
     	// branch for IE/Windows ActiveX version
	}
 } else if (window.ActiveXObject) {
     isIE = true;
     req = new ActiveXObject("Microsoft.XMLHTTP");
     if (req) {
         req.onreadystatechange = processReqChange;
         req.open("GET", url, true);
         req.send();
         //alert(url);
         //alert(req.responseText);
     }
 }
}

//handle onreadystatechange event of req object
function processReqChange() {
 // only if req shows "loaded"
 if (req.readyState == 4) {
     // only if "OK"
     if (req.status == 200) {
     	if(obj_target!="")
	        buildTopicList(obj_target);
      } else {
         //alert("There was a problem retrieving the XML data:\n" + req.statusText);
      }
 }
}

//invoked by "Category" select element change;
//loads chosen XML document, clears Topics select
//element, loads new items into Topics select element
function loadDoc(evt, obj, url) {
 
    // equalize W3C/IE event models to get event object

 	obj_target = obj;
    evt = (evt) ? evt : ((window.event) ? window.event : null);
 if (evt) {
     // equalize W3C/IE models to get event target reference
     var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
     if (elem) {
         try {
             
		loadXMLDoc(url);
		 }
         catch(e) {
             var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
             //alert("Klaida. Negalima užkrauti duomenų iš serverio.\n"/* + msg + " - " + elem.selectedIndex*/);
             return;
         }
     }
 }
 
}

//retrieve text of an XML document element, including
//elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
 var result = "";
 if (prefix && isIE) {
     // IE/Windows way of handling namespaces
     result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
 } else {
     // the namespace versions of this method 
     // (getElementsByTagNameNS()) operate
     // differently in Safari and Mozilla, but both
     // return value with just local name, provided 
     // there aren't conflicts with non-namespace element
     // names
     result = parentElem.getElementsByTagName(local)[index];
 }
 if (result) {
     // get text, accounting for possible
     // whitespace (carriage return) text nodes 
     if (result.childNodes.length > 1) {
         return result.childNodes[1].nodeValue;
     } else {
         if(result.firstChild)
	         return result.firstChild.nodeValue;    		
         else
         	return "";
     }
 } else {
     return "";
 }
}

//fill Topics select list with items from
//the current XML document
function buildTopicList(obj_target) {

	var items = req.responseXML.getElementsByTagName("item");

		 	tb_node = document.createElement("table");
		 	tb_node.setAttribute("width", "100%");
		 	tb_node.setAttribute("cellPadding", "0");
		 	tb_node.setAttribute("cellSpacing", "0");
		 	tb_node.setAttribute("className", "tb_list");
		 	bd_node = document.createElement("tbody");
		 	//alert(arr_title.length);
		 	if(items.length==0){
			 	 tr_node = document.createElement("tr");
			 	 td_node = document.createElement("td");	
			 	 td_node.innerHTML = "Komentarų nėra.";	 
			 	 tr_node.appendChild(td_node);		
			 	 bd_node.appendChild(tr_node);
				 tb_node.appendChild(bd_node);
				 obj_target.appendChild(tb_node);				 	 
		 	}
		 	for (var i = 0; i < items.length; i++) {

			     arr_id[i] 		= getElementTextNS("", "id"	, 			items[i], 0), document.createTextNode(getElementTextNS("", "id", items[i], 0));
			     arr_title[i] 	= getElementTextNS("", "title"	, 		items[i], 0), document.createTextNode(getElementTextNS("", "title", items[i], 0));
			     //arr_short_title[i]= getElementTextNS("","short_title", items[i], 0), document.createTextNode(getElementTextNS("", "short_title", items[i], 0));
			     arr_email[i] 	= getElementTextNS("", "email"	, 		items[i], 0), document.createTextNode(getElementTextNS("", "email", items[i], 0));
			     arr_name[i] 	= getElementTextNS("", "name"	, 		items[i], 0), document.createTextNode(getElementTextNS("", "name", items[i], 0));
			     arr_date[i] 	= getElementTextNS("", "public_date", 	items[i], 0), document.createTextNode(getElementTextNS("", "public_date", items[i], 0));

			 	 tr_node = document.createElement("tr");
			 	 //tr_node.setAttribute("className", "");
			 	 td_node = document.createElement("td");
			 	 if(i%2==0)
				 	td_node.setAttribute("className", "td_list_1");
				 else
					td_node.setAttribute("className", "td_list_2");
				td_node.setAttribute("verticalAlign", "top");
			 	 td_node.innerHTML = "<table border=0 cellpadding=2 cellspacing=0><tr><td nowrap valign=top><b style='font-size:10px;'>"+arr_date[i]+"</b></td><td valign=top><a href='mailto:"+arr_name[i]+"'>"+arr_email[i]+"</a></td><td valign=top>"+arr_title[i]+"</td></tr></table>";
			 	 tr_node.appendChild(td_node);
		
			 	 bd_node.appendChild(tr_node);
				 tb_node.appendChild(bd_node);
				 obj_target.appendChild(tb_node);	
			 }
}
