// This creates a XMLRequest (ActiveXObject) to be sent to the server
var XmlReq;
// This page returns the XML Response for the selected choice
var AjaxServerPageName = "AjaxServer.aspx";

function CreateXmlReq()
{
	try
	{
		XmlReq = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlReq = null;
		}
	}
	if(!XmlReq && typeof XMLHttpRequest != "undefined") 
	{
		XmlReq = new XMLHttpRequest();
	}
}

//This fucntion is to send the choice into the AJAX Server page for processing
function FetchDGContents(amin)
{
	var selecteditem = amin ; //document.Form1.ddlcity.value;
	//Starts displaying the Process Image table
	imgtbl.style.visibility = 'visible';
	var requestUrl = AjaxServerPageName + "?Choice=" + encodeURIComponent(selecteditem);
	
	CreateXmlReq();
	
	if(XmlReq)
	{
		XmlReq.onreadystatechange = HandleResponse;
		XmlReq.open("GET", requestUrl,  true);
		XmlReq.send();		
	}
}

function HandleResponse()
{
	if(XmlReq.readyState == 4)
	{
		if(XmlReq.status == 200)
		{			
			// Before filling new contents into the DataGrid, clear the cotents by calling this function
			ClearTable();
			// Fill the cleared Datagrid with new XML Reponse
			FillTable(XmlReq.responseXML.documentElement);
			// Hides the Process Image Table after displaying the contents
			imgtbl.style.visibility = 'hidden';
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Fills the datagrid contents with the newly recieved Response content
function FillTable(scity)
{
	// Gets the response XML
	var auth = scity.getElementsByTagName('Authors'); 
	// Gets the table type content present in the Response XML and gets the data into a variable tbl
	var tbl = document.getElementById('dgauthors').getElementsByTagName("tbody")[0];
			
	// Iterate through the table and add HTML Rows & contents into it.
	for(var i=0;i<auth.context.childNodes(0).parentNode.childNodes.length;i++)
	{
		// Create a HTML Row
		var row = document.createElement("TR"); 
		
		// Set the style
	//	row.setAttribute("className","text");
	//	row.setAttribute("bgColor","#ECECEC");
		
		// Iterate thorugh the columns of each row
		//for(var j=0;j<auth.context.childNodes(0).childNodes.length;j++)
	//	{
			// Create a HTML DataColumn
			var cell = document.createElement("TD"); 
			// Store the cotents we got from Response XML into the column
			cell.innerHTML = "<table border='0' width='100%' id='table1' cellpadding='0' cellspacing ='0' >	<tr>		<td><a href='index.aspx?Request=GNews&GNewsID=" +  auth.context.childNodes(0).childNodes(0).text + "'> <img  width='160' height = '110' src='" + auth.context.childNodes(0).childNodes(7).text + "' > </td>	</tr>	<tr>		<td dir='rtl' align='right'>"  + auth.context.childNodes(0).childNodes(1).text + " </a></td>	</tr></table>" ;
			// Append the new column into the current row
			row.appendChild(cell); 
		//}
		// Append the current row into the HTML Table(i.e DataGrid)
		tbl.appendChild(row)
	}
}

// Clearing the existing contents of the Datagrid
function ClearTable()
{
	var tbl = document.getElementById('dgauthors').getElementsByTagName("tbody")[0];
	var row = tbl.rows.length
	for (var i=1,j=1;j<row;i++,j++)
	{
		if (tbl.rows.length == 2){i = 1;}
		tbl.deleteRow(i);
		if (tbl.rows.length == i) {i = 0;}
	}
}
