var obj;
var navUrl;
var control;
var ctrlname;
var ctrlid;
function GetData(fcn,url,ctrl,name,id)
{
	try
	{
		//obj = new ActiveXObject("Msxml2.XMLHTTP");
		if (window.XMLHttpRequest) // MOZILLA
		{
			//alert('Hi');
			obj = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject) // IE
		{
			obj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	catch(e)
	{
		try
		{
			//obj = new ActiveXObject("Microsoft.XMLHTTP");
			if (window.XMLHttpRequest) // MOZILLA
			{
				obj = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) // IE
			{
				obj = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		catch(e1)
		{
			obj = null;
		}
	}
	//imgtbl.style.visibility = 'visible';
	control=ctrl;
	ctrlname=name;
	ctrlid=id;
	if(obj!=null)
	{
		obj.onreadystatechange = fcn;
		obj.open("GET",url,true);
		obj.send(null);
	}
	return false;
}
function FillState()
{	
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{
			var dsRoot=obj.responseXML.documentElement;			
			var ddlOrders = document.getElementById(control);
			//var ddlOrders = document.getElementById('<%=ddlstate.ClientID%>');
			for (var count = ddlOrders.options.length-1;count >-1; count--)
			{
				ddlOrders.options[count] = null;
			}
			//alert(dsRoot);
			if(dsRoot == null)
			{
				listItem = new Option("Select State", "Select State",  false, false);
				ddlOrders.options[ddlOrders.length] = listItem;
			}
			else
			{
				var orders = dsRoot.getElementsByTagName(ctrlname);
				var orders1 = dsRoot.getElementsByTagName(ctrlid);
				var text;
				var text1;
				var listItem;
				for (var count = 0; count < orders.length; count++)
				{
					text = (orders[count].textContent || orders[count].innerText || orders[count].text);
					text1 = (orders1[count].textContent || orders1[count].innerText || orders1[count].text);
					listItem = new Option(text, text,  false, false);
					ddlOrders.options[ddlOrders.length] = listItem;
				}
			}
		}
		else
		{
			alert("Error retrieving data!" );
		}
	}
}
function HandleResponse()
{
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{
			// Before filling new contents into the DataGrid, clear the cotents by calling this function
			//ClearTable();
			//Start
				var tbl = document.getElementById(control).getElementsByTagName("tbody")[0];
				var row = tbl.rows.length;
				alert(row);
				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;}
				}
			//End
			// Fill the cleared Datagrid with new XML Reponse
			FillTable(obj.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." );
		}
	}
}
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(control).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","black");
		
		// 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 = auth.context.childNodes(i).childNodes(j).text;
			// 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);
	}
}
function ClearTable()
{
	var tbl = document.getElementById(control).getElementsByTagName("tbody")[0];	
	var row = tbl.rows.length;
	alert(row);
	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;}
		alert('deleted');
	}
}
function ProcessResponse()//Function that alerts the return value
{
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{						
			var retval=obj.responseText;
			alert(retval);
		}
		else
		{
			alert("Error retrieving data!" );
		}
	}
}
function showTable()
{
	if(imgtbl.style.visibility = 'hidden')
	{
		imgtbl.style.visibility = 'visible';
		return true;
	}
	return false;
	/*tdTable.style.visibility = 'visible';
	tdGrid.style.visibility = 'hidden';
	btnAdd.style.visibility = 'hidden';
	btnSave.style.visibility = 'visible';
	btnUpdate.style.visibility = 'hidden';*/
}