
session.active_requests = 0;

function search_test(args, name)	{

	if(session.active_requests > 1)	{
		
		return(false);
	}

	args.name = name;

	//clear_status_text();
	show_status_text("Active Requests: " + session.active_requests);

	if(! document.getElementById('search_results_section'))	{


		// Return false if no elements exist to show search results
		
		var url = "/buy.html?fn=search";
		for(var i in args)	{
			url += "&" + i + "=" + args[i];
		}
		
		document.location = url;
		
		return(false);
	}
	
	// Determine whether to remove or create search results
	var table;
	if(table = document.getElementById("search_results"))	{
		// Table exists, remove, change toggle image, & return
		
		table.parentNode.removeChild(table);
	}

	// Table does not exist, get categories & create elements

	// xmlhttp.js retrieve method
	var url = "/xml/search_results_test.xml?";

	for(var i in args)	{
		url += "&"+i+"="+args[i];
	}

	setCookie('search_type', 'category');
	setCookie('search_string', url);

	var xmlhttp = new Object();

	if(window.ActiveXObject)	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else	{
		xmlhttp = new XMLHttpRequest();
	}

	xmlhttp.onreadystatechange = function()	{

		show_status_text("readyState " + xmlhttp.readyState);

		if(xmlhttp.readyState == 1)	{		
			show_status_bar();
		}
		else if(xmlhttp.readyState == 4)	{

			show_status_text("in ready 4");			
			
			var doc = xmlhttp.responseXML;
			show_status_text("generate_search_results starting");
			generate_search_results(doc, args);
			show_status_text("generate_search_results complete");

			var count = doc.getElementsByTagName("items")[0].getAttribute("count");
			if(count > 0)	{
				// Results found
				//update_search_history('category', category_id, name);
			}

			show_status_text("hiding status bar..");
			hide_status_bar();
			show_status_text("hiding status bar done");
			show_status_text("");

			session.active_requests--;
		}
	};
	session.active_requests++;

	xmlhttp.open("GET", url, true);
	show_status_text('e4.staging.iasfilm.com' + url);

	xmlhttp.send(null);

	return(true);
}


function generate_search_results(doc, args)	{

   show_status_text("in generate_search_results");
  
	var tbody = document.createElement("TBODY");

	var color = "#f3d286";

	if(args && args.name)	{
		// New row
		
	        show_status_text("new_row");		
		
		var tr = document.createElement("TR");
		var td = document.createElement("TD");
		td.setAttribute("colSpan", 2);
		td.style.color = "#00a766";
		
		if(args["within_category_id"])	 {
			var hierarchy = doc.getElementsByTagName("searched_category_hierarchy")[0];
			remove_text_nodes(hierarchy);
			
			for(var i=0; i<hierarchy.childNodes.length; i++)   {
				if(hierarchy.childNodes[i].nodeType != 3) {
					
					remove_text_nodes(hierarchy.childNodes[i]);
					var name = hierarchy.childNodes[i].childNodes[1].firstChild.nodeValue;
					var category_id = hierarchy.childNodes[i].childNodes[2].firstChild.nodeValue;
					if(args.name == name)  {
						var container = document.createElement("SPAN");
						container.style.fontWeight = "bold";
						container.style.fontSize = "15px";
					}
					else {
						var container = document.createElement("A");
						setOnClick(container,
							"update_search_history("
							+ "'category', '" 
							+ category_id
							+ "', '"
							+ name
							+ "'); "
							+ "search_test("
							+ " { active: 1, within_category_id: '"
							+ category_id
							+ "' }, '"
							+ name
							+ "');"
						);
					}
					
					container.href = "#";
					container.appendChild(document.createTextNode(name));
					td.appendChild(container);
					
					if(args.name != name)  {
						td.appendChild(document.createTextNode(" > "));
					}
				}
			}
		}
		
		// td.appendChild(document.createTextNode(args.name));

		// if(args.within_category_id)	{
		// 	td.appendChild(document.createTextNode(args.within_category_id));
		// }
		
		tr.appendChild(td);
		tbody.appendChild(tr);
		// End row	
	}

	// New row
	var tr = document.createElement("TR");
	var td = document.createElement("TD");
	td.setAttribute("colSpan", 4);
	td.style.fontSize = "10px";
	td.style.backgroundColor = "#f3b338";
	td.style.fontWeight = "bold";
	td.style.border = "solid black 1px";

	//var count = doc.getElementsByTagName("items")[0].getElementsByTagName("count")[0].firstChild.nodeValue;
	var count = doc.getElementsByTagName("items")[0].getAttribute("count");
	td.appendChild(document.createTextNode(count));

	if(count == 1)	{
		td.appendChild(document.createTextNode(" Auction found"));
	}
	else	{
		td.appendChild(document.createTextNode(" Auctions found"));
	}

	tr.appendChild(td);
	tbody.appendChild(tr);
	// End row	
	
	// alert("LENGTH:" + doc.getElementsByTagName("item").length);

	// Loop through items
	for(var i=0; i<doc.getElementsByTagName("item").length; i++)	{

		// Item block
		var item = doc.getElementsByTagName("item")[i];
		remove_text_nodes(item);

		// Alternate background color on each row
		if(color == "#f3d286")	{
			color = "#f3e1b6";
		}
		else	{
			color = "#f3d286";
		}
		
		// New row
		tr = document.createElement("TR");
		td = document.createElement("TD");
		td.setAttribute("colSpan", 3);
		td.appendChild(document.createTextNode(" "));
		tr.appendChild(td);
		tbody.appendChild(tr);
		// End row

		// New row
		tr = document.createElement("TR");
		td = document.createElement("TD");
		td.setAttribute("colSpan", 3);
		td.setAttribute("nowrap", 1);
		td.style.fontSize = "10px";
		
		var categories = item.getElementsByTagName("category_hierarchy")[0].getElementsByTagName("category");

		for(var j=0; j<categories.length; j++)	{
			
			var category = categories[j];
			remove_text_nodes(category);

			var a = document.createElement("A");
			a.style.textDecoration = "underline";
			a.setAttribute("title", category.getElementsByTagName("id")[0].firstChild.nodeValue);
			category.getElementsByTagName("name")[0].firstChild.nodeValue
			a.href = "#";
			setOnClick(a, 
			      "update_search_history(" 
			      + "'category'"
			      + ", '"
			      + category.getElementsByTagName("id")[0].firstChild.nodeValue
			      + "', '"
			      + category.getElementsByTagName("name")[0].firstChild.nodeValue
			      + "'); "
				+ "search_test("
				+ " { active: 1, within_category_id: '"
				+ category.getElementsByTagName("id")[0].firstChild.nodeValue
				+ "' }, '"
				+ category.getElementsByTagName("name")[0].firstChild.nodeValue
				+ "');"
			);
			a.appendChild(document.createTextNode(
				category.getElementsByTagName("name")[0].firstChild.nodeValue
			));
			
			td.appendChild(a);

			if(j < (categories.length-1))	{
				td.appendChild(document.createTextNode(" > "));
			}
		}

		//td.appendChild(document.createTextNode(
		//	item.getElementsByTagName("category")[0].firstChild.nodeValue
		//));
		tr.appendChild(td);

		td = document.createElement("TD");
		td.setAttribute("colSpan", 2);
		td.style.textAlign = "right";
		td.style.fontSize = "10px";
		td.appendChild(document.createTextNode(
			item.getElementsByTagName("nickname")[0].firstChild.nodeValue
		));
		tr.appendChild(td);
		tbody.appendChild(tr);
		// End row
		
		// New row
		tr = document.createElement("TR");
		tr.style.backgroundColor = color;
		
		td = document.createElement("TD");
		td.setAttribute("rowSpan", 2);

		if(item.getElementsByTagName("thumbnail").length && item.getElementsByTagName("thumbnail")[0].hasChildNodes())	{
			// Thumbnail exists
		alert(item.getElementsByTagName("thumbnail").length);	
			var img = document.createElement("IMG");
			img.setAttribute("src", item.getElementsByTagName("thumbnail")[0].firstChild.nodeValue);
			td.appendChild(img);
		}
		tr.appendChild(td);

		td = document.createElement("TD");
		var a = document.createElement("A");
		a.style.fontWeight = "bold";
		a.setAttribute("href", "/item.html?id=" + item.getElementsByTagName("id")[0].firstChild.nodeValue);
		a.appendChild(document.createTextNode(
			item.getElementsByTagName("name")[0].firstChild.nodeValue
		));
		td.appendChild(a);
		tr.appendChild(td);

		td = document.createElement("TD");
		td.setAttribute("valign", "top");
		td.setAttribute("align", "right");
		td.setAttribute("rowSpan", 2);
		img = document.createElement("IMG");
		img.setAttribute("src", "/image/small_" + item.getElementsByTagName("type")[0].firstChild.nodeValue + "_icon.jpg");
		img.style.border = "solid black 1px";
		td.appendChild(img);
		tr.appendChild(td);
		
		td = document.createElement("TD");
		td.setAttribute("rowSpan", 2);
		td.style.verticalAlign = "top";

		var div = document.createElement("DIV");
		div.style.fontSize = "11px";
		var span = document.createElement("SPAN");
		span.style.textTransform = "capitalize";
		span.appendChild(document.createTextNode(item.getElementsByTagName("type")[0].firstChild.nodeValue + " auction"));
		div.appendChild(span);
		td.appendChild(div);

		if(item.getElementsByTagName("type")[0].firstChild.nodeValue == "inverse")	{
			
			var msg;
			
			if(item.getElementsByTagName("buyer")[0].hasChildNodes())   {
			   div.appendChild(document.createElement("BR"));
			   div.appendChild(document.createTextNode("Sold for " + item.getElementsByTagName("f_current_amount")[0].firstChild.nodeValue));
			}
			else  {
			   //alert(item.getElementsByTagName("user")[0].firstChild.nodeValue);
			   div.appendChild(document.createElement("BR"));
			   var a = document.createElement("A");
			   a.setAttribute("href", "/buy_inverse.html?id=" + item.getElementsByTagName("id")[0].firstChild.nodeValue);
			   a.appendChild(document.createTextNode("Buy for " + item.getElementsByTagName("f_current_amount")[0].firstChild.nodeValue));
			   div.appendChild(a);
			}
		}

		tr.appendChild(td);
		tbody.appendChild(tr);
		// End row

	       if(item.getElementsByTagName("short_description")[0].firstChild)	  {
		  // Description exists, continue with New row
		  
		  tr = document.createElement("TR");
		  td = document.createElement("TD");
		  td.appendChild(document.createTextNode(
		  	item.getElementsByTagName("short_description")[0].firstChild.nodeValue
		  ));
		  tr.appendChild(td);
		  tbody.appendChild(tr);
		  // End row
	       }
		
	}

	var table = document.createElement("TABLE");
	table.id = "search_results";
		
	table.setAttribute("cellSpacing", 0);
	table.setAttribute("cellPadding", 4);
	table.setAttribute("border", 0);
	
	table.appendChild(tbody);
	table.setAttribute("width", "100%");

	var search_results_section = document.getElementById('search_results_section');
	remove_all_nodes(search_results_section);
	
	search_results_section.appendChild(table);
}



