// 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.lushturkey.com/pages/AjaxSearchServer.aspx";
//var AjaxServerPageName = "http://localhost:51680/pages/AjaxSearchServer.aspx";
var anything;

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();
	}
}

function clearContent()
{
document.getElementById('Bulunanlar_DataGrid').style.display = "none";
}
//This fucntion is to send the choice into the AJAX Server page for processing
function FetchDGContents(given_word)
{

	var enteredWord = given_word; //document.getElementById('given_word').value;

	//Starts displaying the Process Image table
	//imgtbl.style.visibility = 'visible';
	var requestUrl = AjaxServerPageName + "?Word=%20" + encodeURIComponent(enteredWord);
	document.getElementById('Bulunanlar_DataGrid').style.display = "block";
	CreateXmlReq();
	
	if(XmlReq)
	{
		XmlReq.onreadystatechange = HandleResponse;
		XmlReq.open("GET", requestUrl,  true);
		XmlReq.send(anything);		
	}
}

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(sword)
{
		
	// Gets the response XML
	var auth = sword.getElementsByTagName('NewDataSet');
	// Gets the table type content present in the Response XML and gets the data into a variable tbl
	var tbl = document.getElementById('Bulunanlar_DataGrid').getElementsByTagName("tbody")[0];
	//alert(document.getElementById('Bulunanlar_DataGrid').innerHTML);
	
if ( tbl.hasChildNodes() )
{
    while ( tbl.childNodes.length >= 1 )
    {
        tbl.removeChild(tbl.lastChild);
        //cell.removeChild( cell.firstChild );       
    } 
}

	
	//alert(sword.childNodes.length);
	
	
	ua=navigator.userAgent;
    brw=ua.indexOf("MSIE");
    if (brw<0) //FF
    {
    for(var i=0;i<sword.childNodes.length;i++)
	{
		// Create a HTML Row
		var row = document.createElement("TR"); 
		// Set the style
		row.setAttribute("class","kelimeItem");
		if(i % 2 == 0 )
		{row.setAttribute("bgColor","#F1F1F1");}
		else{row.setAttribute("bgColor","#FFFFFF");}
			
		
		// Iterate thorugh the columns of each row
		
		//alert(sword.getElementsByTagName('kelime')[0].childNodes[0].nodeValue);
		for(var j=0;j<sword.getElementsByTagName('Resim')[i].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 = '<img height="23" width="23" src="http://www.lushturkey.com/images/products/45/' + sword.getElementsByTagName('Resim')[i].childNodes[j].nodeValue + '">'; //<div style="cursor:hand;" onmouseover="javascript:this.className=\'kelimeItemHover\'" onmouseout="javascript:this.className=\'kelimeItem\'">&nbsp;&nbsp;' + sword.getElementsByTagName('Resim')[i].childNodes[j].nodeValue + '</div>'; //sword.getElementsByTagName('kelime')[i].childNodes[j].nodeValue;
			//cell.innerHTML = '<img height="23" width="23" src="http://localhost:51680/images/products/45/' + sword.getElementsByTagName('Resim')[i].childNodes[j].nodeValue + '">'; //<div style="cursor:hand;" onmouseover="javascript:this.className=\'kelimeItemHover\'" onmouseout="javascript:this.className=\'kelimeItem\'">&nbsp;&nbsp;' + sword.getElementsByTagName('Resim')[i].childNodes[j].nodeValue + '</div>'; //sword.getElementsByTagName('kelime')[i].childNodes[j].nodeValue;
			// Append the new column into the current row
			row.appendChild(cell); 
		}
		for(var j=0;j<sword.getElementsByTagName('UrunAdi')[i].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 = '<div style="cursor:hand;" onmouseover="javascript:this.className=\'kelimeItemHover\'" onmouseout="javascript:this.className=\'kelimeItem\'">&nbsp;&nbsp;' + sword.getElementsByTagName('UrunAdi')[i].childNodes[j].nodeValue + '</div>'; //sword.getElementsByTagName('kelime')[i].childNodes[j].nodeValue;
			cell.innerHTML = '<div style="cursor:hand;" onmouseover="javascript:this.className=\'kelimeItemHover\'" onmouseout="javascript:this.className=\'kelimeItem\'">&nbsp;&nbsp;<a href="http://www.lushturkey.com/' + sword.getElementsByTagName('Link')[i].childNodes[j].nodeValue + '" class="searchItem">' + sword.getElementsByTagName('UrunAdi')[i].childNodes[j].nodeValue + '</a></div>'; //sword.getElementsByTagName('kelime')[i].childNodes[j].nodeValue;
			//cell.innerHTML = '<div style="cursor:hand;" onmouseover="javascript:this.className=\'kelimeItemHover\'" onmouseout="javascript:this.className=\'kelimeItem\'">&nbsp;&nbsp;<a href="http://localhost:51680/' + sword.getElementsByTagName('Link')[i].childNodes[j].nodeValue + '" class="searchItem">' + sword.getElementsByTagName('UrunAdi')[i].childNodes[j].nodeValue + '</a></div>'; //sword.getElementsByTagName('kelime')[i].childNodes[j].nodeValue;
			// 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 //IE
    {
    // Iterate through the table and add HTML Rows & contents into it.
  
  
    if ( auth.context.hasChildNodes() )
    {
        
    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","kelimeItem");
		if(i % 2 == 0 )
		{row.setAttribute("bgColor","#F1F1F1");}
		else{row.setAttribute("bgColor","#FFFFFF");}
		
		// 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");
			//cell.className = "rcMenuItem";
			// Store the cotents we got from Response XML into the column
			if (j==0) // Resim icin
			{
			cell.innerHTML = '<img height="23" width="23" src="http://www.lushturkey.com/images/products/45/' + auth.context.childNodes(i).childNodes(j).text + '">';
			//cell.innerHTML = '<img height="23" width="23" src="http://localhost:51680/images/products/45/' + auth.context.childNodes(i).childNodes(j).text + '">';
						}
			if (j==1) // UrunAdi icin
			{
			cell.innerHTML = '<div style="cursor:hand;" onmouseover="javascript:this.className=\'kelimeItemHover\'" onmouseout="javascript:this.className=\'kelimeItem\'">&nbsp;&nbsp;<a href="http://www.lushturkey.com/' + auth.context.childNodes(i).childNodes(2).text + '" class="searchItem">' + auth.context.childNodes(i).childNodes(j).text + '</a></div>';
			//cell.innerHTML = '<div style="cursor:hand;" onmouseover="javascript:this.className=\'kelimeItemHover\'" onmouseout="javascript:this.className=\'kelimeItem\'">&nbsp;&nbsp;<a href="http://localhost:51680/' + auth.context.childNodes(i).childNodes(2).text + '" class="searchItem">' + auth.context.childNodes(i).childNodes(j).text + '</a></div>';
			}
			// 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('Bulunanlar_DataGrid').getElementsByTagName("tbody")[0];
	//var row = tbl.rows.length;
	
	
	/*for (var i=1;i<tbl.rows.length;i++)
	{
	//alert("row: " + row);
	//alert("i: "+i);
		tbl.deleteRow(i);
	}*/
	
	for (var i=1,j=1;j<tbl.rows.length;i++,j++)
	{
		if (tbl.rows.length == 2){i = 1;}
		tbl.deleteRow(i);
		if (tbl.rows.length == i) {i = 0;}
	}
}
