// 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 = "http://www.mysoretrendz.com/UserControls/AjaxServer.aspx";
//var AjaxServerPageName = "/MT/UserControls/AjaxServer.aspx";



function CreateXmlReq()
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    XmlReq=new XMLHttpRequest();    
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      XmlReq=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        XmlReq=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
  }






//This fucntion is to send the choice into the AJAX Server page for processing
function FetchDGContents(value)
{
   
	var SearchKey = value;
	
	var requestUrl = AjaxServerPageName + "?Key=" + encodeURIComponent(SearchKey);
	
	CreateXmlReq();
	
	if(XmlReq)
	{
	   	XmlReq.onreadystatechange = HandleResponse;
		XmlReq.open("GET", requestUrl,  true );
		XmlReq.send(null);		
	}
}

function HandleResponse()
{
  
	if(XmlReq.readyState == 4 || XmlReq.readyState=="complete")
	{
	
		if(XmlReq.status == 200)
		{			
		
		str=XmlReq.responseText;
		
		// 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);
		
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Fills the datagrid contents with the newly recieved Response content
function FillTable(scity)
{   
   var Hasrow=0;
   
	// Gets the response XML
	              
	var auth = scity.getElementsByTagName('divSubdomain');
	
	
	// Gets the table type content present in the Response XML and gets the data into a variable tbl
	var tbl = document.getElementById('TabStripastro1_subdomain_lstSubDomains').getElementsByTagName("tbody")[0];
	
	
	if(auth.context !=null)
	{	
		
	if (auth.context.childNodes(0) != null )
	{	
	
	// 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"); 
		
		
		// Iterate thorugh the columns of each row
		for(var j=0;j<auth.context.childNodes(0).childNodes.length;j++)
		{
		  Hasrow=1;  
			// Create a HTML DataColumn
			var cell = document.createElement("TD"); 
			// Store the cotents we got from Response XML into the column
		
			cell.innerHTML ="<a href='" + auth.context.childNodes(i).childNodes(j).text + "'>" +auth.context.childNodes(i).childNodes(j).text+ "</a>"  ;
		
			// 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);		
	}
	//else
	//{
	//alert("no records found");	
	//}
	} 
	}
	if (Hasrow==0)
		{
		    var row = document.createElement("TR"); 
		    var cell = document.createElement("TD"); 
		    cell.innerHTML ="No Records Found" ;
		    row.appendChild(cell); 
		    tbl.appendChild(row);
		}
	
		
}

// Clearing the existing contents of the Datagrid
function ClearTable()
{
   
	var tbl = document.getElementById('TabStripastro1_subdomain_lstSubDomains').getElementsByTagName("tbody")[0];
	var row = tbl.rows.length;
	

	for (var i=0,j=0;j<row;i++,j++)
	{
	  
		
		if (tbl.rows.length == i||tbl.rows.length == 1) {
		i =0;}
		tbl.deleteRow(i);
	}
}

