
/*START CODE TO GET THE ARTICLES FROM THE XML FILE ON THE SERVER TROUGH AJAX*/	
	var http_request 	= false;
	var _ID = -1;
	var lastArticleShown = 0;
	var numArticles = 0;

    function makeRequest(url,id) 
	{

		if(!url)
		    url = '/news/articles.xml';
		    
		 if(id != null)
		    _ID = id;
		  else 
		    _ID = -1;
	
        http_request = false;
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { 
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
		

        http_request.onreadystatechange = updateArticles;
        http_request.open('GET', url, true);
        http_request.send(null);
        
        return false;
    }
	
	
	
	function getNumArticles(xmldoc)
	{
		var aryIDNodes = xmldoc.getElementsByTagName('id');
	    numArticles = aryIDNodes.length;
	}
	
	function getNodeNumberFromXMLFile(xmldoc)
	{
	    var aryIDNodes = xmldoc.getElementsByTagName('id');
	    numArticles = aryIDNodes.length;
		for(var counter=0;counter<numArticles;counter++)
		{
			//alert(xmldoc.getElementsByTagName('id')[counter].firstChild.data);
			var url = aryIDNodes[counter].firstChild.data;
			aurl = _ID.toString();
			if(aurl.match(url))
			{
				return counter;
			}
		}
		
		//return false _ID value does not match any in the articles.xml file
		return 666;
	}

    function updateArticles() {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			
                //alert(http_request.responseText);
				var xmldoc = http_request.responseXML;
				getNumArticles(xmldoc);
				
				    
				    if( _ID == -1 )
		            {
			            if(lastArticleShown >= (numArticles - 1))
				            _ID = 0;
			            else
			                _ID = lastArticleShown + 1;
		            }
		            lastArticleShown = _ID;
		                
				
				//get nodenr from xmldoc
				var nodenr = getNodeNumberFromXMLFile(xmldoc);
				
				//if getNodeNumberFromXMLFile returns false(666) then inform user and exit function
				if(nodenr == 666) 
				{
				    alert("The article for this : " + _ID + " id was not found, please contact site administrator.");
				    return;
				}
								
				//retrieving data from xmldoc
				var image = xmldoc.getElementsByTagName('img')[nodenr].firstChild.data;
				var title = xmldoc.getElementsByTagName('title')[nodenr].firstChild.data;
				var txt = xmldoc.getElementsByTagName('text')[nodenr].firstChild.data;
				var link = xmldoc.getElementsByTagName('link')[nodenr].firstChild.data;
				
				//printing data in the tables live without refresh
				document.getElementById('atitle').innerHTML = title;
				document.getElementById('featureprofile').rows[1].cells[0].innerHTML = txt;
				document.getElementById('featureprofile').rows[1].cells[0].innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;" + "<a id=\"rtalink\" href=\"" + link + "\">Read the article</a>";

                //print article nrs
                var parentOfNrs = document.getElementById("slideshowcontrol");
                var nrs = "";
                var previousHTML = "";
                var nextHTML = "";
                
                
                var aryIDNodes = xmldoc.getElementsByTagName('id');
		        for(var cnt=0; cnt<numArticles; cnt++)
		        {
		            if(parseInt(_ID) == cnt)
		            {
		                nrs += "<a class=\"anr\" href=\"#\" onclick=\"return makeRequest('/news/articles.xml','"+cnt+"');\" ><b>"+(cnt + 1)+"</b></a>&nbsp;";
		            }
		            else
		            {
		                nrs += "<a class=\"anr\" href=\"#\" onclick=\"return makeRequest('/news/articles.xml','"+cnt+"');\">"+(cnt + 1)+"</a>&nbsp;";
		            }
			    }
				
				

			    
			    
			    //if the previous article doest exist, cycle trough the articles by providing the last article id again
                if((parseInt(_ID) - 1) < 0)
                {
                    previousHTML = "<a href=\"#\"onclick=\"return makeRequest('/news/articles.xml','"+(numArticles - 1)+"');\"><img src=\"/images/back.jpg\" alt=\"previous article\" /></a>&nbsp;";
                }
                else
                {
                    previousHTML = "<a href=\"#\"onclick=\"return makeRequest('/news/articles.xml','"+(_ID - 1)+"');\"><img src=\"/images/back.jpg\" alt=\"previous article\" /></a>&nbsp;";
                }
			    
			    
			    //if the next article doest exist, cycle trough the articles by providing the first article id again
			    if((parseInt(_ID) + 1) >= numArticles)
                {
                    nextHTML = "<a href=\"#\"onclick=\"return makeRequest('/news/articles.xml','0');\"><img src=\"/images/forward.jpg\" alt=\"next article\" /></a>&nbsp;";
                }
                else
                {
                    nextHTML = "<a href=\"#\"onclick=\"return makeRequest('/news/articles.xml','"+(parseInt(_ID) + 1)+"');\"><img src=\"/images/forward.jpg\" alt=\"next article\" /></a>&nbsp;";
                }

			    parentOfNrs.innerHTML = previousHTML + nrs + nextHTML;

				//reset nodenr variable
				nodenr = 666;
            } else {
               // alert('There was a problem with the request.');
            }
        }

    }
    

/*function cycleTroughArticles()
{
	//alert("before");
	intervalID = window.setInterval("cycleArticle()",10000)
	//alert("after");
}


function cycleArticle()
{
	if(id > maxArticle) {id = 0; alert(id);}
	//alert(id);
	//alert(maxArticle);
	makeRequest('/articles/articles.xml',id);
}*/


 
/*END CODETO GET THE ARTICLES FROM THE XML FILE ON THE SERVER TROUGH AJAX*/	

/*START CODE TO ROTATE THE ARTICLE BEING DISPLAYED*/	



/*function checkCookie()
{
alert(document.cookie);
}

function createCookie(id)
{
    var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	
	document.cookie = "FA="+id
    
}*/