// The code here contains the functions contained in
// the publications page as well as XHR for
// loading repectve content

// declare global variables
var pubs_arrow = document.images[pubs_arrow];
var isSetArrow = 0 ; // stores if arrow was clicked
var XHRPubsObject = createXHRRequestObject();

// pre-cache images
if(document.images)
{
	var pubsImages = new Object();
	pubsImages["play"] = new Image();
	pubsImages["collapse"] = new Image();
	
	// assign URLs
	pubsImages["play"].src = "images/play.gif";
	pubsImages["collapse"].src = "images/collapse.gif";
	
}

function createXHRRequestObject() 
{ 
	// will store the reference to the XMLHttpRequest object 
	var XHRPubsObject; 
	
	// this should work for all browsers except IE6 and older 
	try 
	{ 
		// try to create XMLHttpRequest object 
		XHRPubsObject = new XMLHttpRequest(); 
	} 
	catch(e) 
	{ 
		// assume IE6 or older 
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "SXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"); 
		
		// try every prog id until one works 
		for (var i=0; i<XmlHttpVersions.length && !XHRPubsObject; i++) 
		{ 
			try
			{ 
				// try to create XMLHttpRequest object 
				XHRPubsObject = new ActiveXObject(XmlHttpVersions[i]); 
			} 
			catch (e) {} 
		} 
	} 
	// return the created object or display an error message 
	if (!XHRPubsObject) 
		alert("Error creating the XMLHttpRequest object."); 
	else 
		return XHRPubsObject; 
} 

function getPublications(pub_category)
{
	// this functiom describes the 
	// category of articles desired
 

	// close the help and reset arrow
	 displayPubsMenu();

	 // get the data for repective
	 // category of the publiation
	 if(XHRPubsObject)
 	{
		try
		{
			if(XHRPubsObject.readyState==4 || XHRPubsObject.readyState==0)
			{
				XHRPubsObject.open("GET","publications.php?cat="+pub_category,true);
				XHRPubsObject.onreadystatechange = handleReturnedPubs;
				XHRPubsObject.send(null);
			}
			else
			{
				// recheck availability after i sec
				setTimeout("getPublications("+pub_category+")",1000);
			}
		}
		catch(e)
		{
			alert("Error has Ocurred Trying to Access Database, Please retry");
		}
	}
}

function displayPubsMenu()
{
// this function changes the 
// green image
// & displays menu

	var publications_cats = document.getElementById('publications_cats');
	
	// check if clicked
	isSetArrow=(isSetArrow==0)?1:0;

	if(document.images)
	{
		// change image first
		if(isSetArrow==1)
		{
			//document.images["pubs_arrow"].src = pubsImages["collapse"].src;
		
			// display the menu
			publications_cats.style.top = "250px";
			publications_cats.style.left = "410px";
			//publications_cats.style.visibility = 'visible';
		}
		else
		{
			//document.images["pubs_arrow"].src = pubsImages["play"].src;
			publications_cats.style.visibility = 'hidden';
		}			
	}
}

function handleReturnedPubs()
{
	/* this function returns the  data returned
		as the details about the publications */
		
	if(XHRPubsObject.readyState != 4)
	{
		// specify that the div 
		// with default message should appear
		
		showLoadingMessage();
	}					
	else if (XHRPubsObject.readyState == 4 && XHRPubsObject.status == 200) 
	{ 
		// hide the loading message
		var messageDiv = document.getElementById('messageDiv');
		messageDiv.style.visibility = 'hidden';
		
		try 
		{ 
			// process the server's response 
			updatePublications(); 
		} 
		catch(e) 
		{
			// display the error message 
			alert(e.toString()); 
		} 
	} 
	else 
	{ 
		alert("There was a problem retrieving the data:\n" +XHRPubsObject.statusText); 
	} 
}

function updatePublications()
{
	/***********************************************************
	* this function updates the publications list from the XML *
	* files obtained from the pucblications table			   *
	***********************************************************/
	xmlResponse = XHRPubsObject.responseXML;
	xmlDocumentElement = xmlResponse.documentElement; 
	var pubs_page = document.getElementById('pubs_page');
	var div=""; // stores the results
	
	// get the text message, which is in the first child of 
	// the the document element 
	var _id = xmlDocumentElement.getElementsByTagName('id'); 
	var no_of_pubs = parseInt(_id.length);

	// start to create necessary html
	var div = "<div id=pubs_page STYLE=\" height: 600px; width: 526px; overflow: auto;\">"
			+"<table width=510 height=600 id=main_content_table border=0 cellpadding=5 cellspacing=0>"
			+"<tr><td colspan=2 height=20 background=images/bg.jpg align=right><span id=main_title>|&nbsp;Resources</span>&nbsp;&nbsp;</td></tr>"
			+"<tr><td id=lower_border height=40 colspan=2 align=left valign=top><img align=left height=30 src=images/list.ico><span id=lower_sub_title>&nbsp;Publications</span>"
			+"</td></tr>";
		
	// collect the new details
	for(var i=0;i<no_of_pubs;i++)
	{
		var _id = xmlDocumentElement.getElementsByTagName('id')[i].firstChild.data; 
		var _name = xmlDocumentElement.getElementsByTagName('name')[i].firstChild.data; 
		var _author = xmlDocumentElement.getElementsByTagName('author')[i].firstChild.data; 
		var _format = xmlDocumentElement.getElementsByTagName('format')[i].firstChild.data; 
		var _details = xmlDocumentElement.getElementsByTagName('details')[i].firstChild.data; 
		var _date_uploaded = xmlDocumentElement.getElementsByTagName('date_uploaded')[i].firstChild.data; 
		var _category = xmlDocumentElement.getElementsByTagName('category')[i].firstChild.data;
		
		// append more results
		// to full data set
		div += "<tr><td id=lower_border_dashed width=20 height=40 align=center valign=top><img src=images/marker.gif></td>"
				+"<td width=496 id=lower_border_dashed valign=top align=left>"
				+"&nbsp;<a target=_blank href='publications/"+_name+"' id=large_green_link>"+_name.substr(0,_name.indexOf(".pdf")+1)+"</a>"
				+"<br/>&nbsp;<span id=author><b>"+_author+"</b></span>"
				+"<br/><span id=content>"+_details+"...</span>"
				+"<span id=uploaded>&nbsp;[ Uploaded On:  "+_date_uploaded+" ]</span>"
				+"<br/>"
				+"</td></tr>";

	}

	// finalise formatting
	div += "<tr><td colspan=2></td></tr></table></div>";

	// append to page
	pubs_page.innerHTML = div;
}

function showLoadingMessage()
{
	// this function shows that 
	// the content is loading
	var messageDiv = document.getElementById('messageDiv');
	
	// define gif file to note loading
	var content = '<img src=images/loading.gif>';
	messageDiv.innerHTML = content;

	messageDiv.style.visibility = 'visible';
} 
