var xDoc= null;
var brprototype=document.createElement("br");
function initSearch()
{

	ajaxRead2("content.xml");
	
}
	function ajaxRead2(xfile)
	{
		var xmlObj = null;
  		if(window.XMLHttpRequest)
		{
			xmlObj = new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
      		xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  		} 
		else 
		{
      		return;
  		}
  		xmlObj.onreadystatechange = function()
		{
  			if(xmlObj.readyState == 4)
			{
       			xDoc=xmlObj.responseXML;
	   			drawSearch();
     		}
    	}
    	xmlObj.open ('GET', xfile, true);
    	xmlObj.send ('');
  	}
	function drawSearch() 
	{
		var query = new Array();
		var q=window.location.search.substring(1);
		if (q !="")
		{
			iq= q.split("&");
			for (i=0; i<iq.length; i++)
			{
				eq=iq[i].split("=");
				query[eq[0]]=decodeURIComponent(eq[1].replace(/\+/g," "));
			}
			document.getElementById("iq").value=query["q"]
			document.getElementById("itype").options[parseInt(query["type"])].selected=true;
			document.getElementById("icontent").options[parseInt(query["content"])].selected=true;
		}
		else
		{
			document.getElementById("iq").focus();
		}
		var maxItems=10;
		var nameProto=document.getElementById("linkProto").firstChild.cloneNode(true);
		var itemPro=document.getElementById("itemProto").firstChild.cloneNode(true);
		var totalName=xDoc.getElementsByTagName("content")[0].getElementsByTagName("item").length;
		var streamNo=0;
		var streamReturn=0;
		var downloadNo=0;
		var slideNo=0;
		var slideReturn=0;
		var scrollNo=0;
		var scrollReturn=0;
		var featurehead="";
		var feature=false;
		var linkName="";
		var including=0;
		var keywords="";
		for (var i = 0; i < totalName; i++)
		{
			var data = xDoc.getElementsByTagName("content")[0].getElementsByTagName("item")[i];
			including=0;
			keywords="";
			if (q!="")
			{
				if (data.getElementsByTagName("linktitle")[0].firstChild.nodeValue.toLowerCase().indexOf(query["q"].toLowerCase())!=-1)
				{
					if (query["type"]=="0" || query["type"]=="1")
					{
						including=1;
					}
				}
				if (data.getElementsByTagName("description")[0].firstChild.nodeValue.toLowerCase().indexOf(query["q"].toLowerCase())!=-1)	
				{
					if (query["type"]=="0" || query["type"]=="2")
					{
						including=1;
					}
				}
				for (var j = 0; j < data.getElementsByTagName("keyword").length; j++)
				{
					if (data.getElementsByTagName("keyword")[j].firstChild.nodeValue.toLowerCase().indexOf(query["q"].toLowerCase())!=-1)
					{
						if (query["type"]=="0" || query["type"]=="3")
						{
							including=1;
						}
					}
					if (keywords=="")
					{
						keywords=keywords+data.getElementsByTagName("keyword")[j].firstChild.nodeValue;
					}
					else
					{
						keywords=keywords+", "+data.getElementsByTagName("keyword")[j].firstChild.nodeValue;
					}
				}
			}
			itemPro.childNodes[0].setAttribute("title",data.getElementsByTagName("linktitle")[0].firstChild.nodeValue);
			itemPro.childNodes[0].setAttribute("href",data.getElementsByTagName("url")[0].firstChild.nodeValue);
			linkName=data.getElementsByTagName("linktitle")[0].firstChild.nodeValue;
			if (data.getElementsByTagName("length")[0])
			{
				linkName=linkName+" ("+data.getElementsByTagName("length")[0].firstChild.nodeValue+")";
			}
			if (data.getElementsByTagName("size")[0])
			{
				linkName=linkName+" ("+data.getElementsByTagName("size")[0].firstChild.nodeValue+")";
			}
			if (data.getElementsByTagName("date")[0])
			{
				linkName=linkName+" - "+data.getElementsByTagName("date")[0].firstChild.nodeValue;
			}
			if (data.getElementsByTagName("description")[0])
			{
				descrip=data.getElementsByTagName("description")[0].firstChild.nodeValue;
			}
			itemPro.childNodes[0].firstChild.nodeValue=linkName;
			itemPro.childNodes[1].firstChild.nodeValue=descrip;
			itemPro.childNodes[2].firstChild.nodeValue=keywords;
			nameProto.setAttribute("title",linkName);
			nameProto.setAttribute("href",data.getElementsByTagName("url")[0].firstChild.nodeValue);
			nameProto.firstChild.nodeValue=linkName;
						
			if (data.getAttribute("type")=="streaming")
			{
				if(streamNo < maxItems)
				{
					document.getElementById("streaming").appendChild(nameProto.cloneNode(true));
					document.getElementById("streaming").appendChild(brprototype.cloneNode(true));
				}
				if (including==1 && (query["content"]=="0" || query["content"]=="1"))
				{
					document.getElementById("streamings").appendChild(itemPro.cloneNode(true));
					document.getElementById("streamings").appendChild(brprototype.cloneNode(true));
					streamReturn++;
				}
				streamNo++;
			}

			if (data.getAttribute("type")=="scroll")
			{
				if(scrollNo < maxItems)
				{
					document.getElementById("scroll").appendChild(nameProto.cloneNode(true));
					document.getElementById("scroll").appendChild(brprototype.cloneNode(true));
				}
				if(including ==1 && (query["content"]=="0" || query["content"]=="2"))
				{
					document.getElementById("scrolls").appendChild(itemPro.cloneNode(true));
					document.getElementById("scrolls").appendChild(brprototype.cloneNode(true));
					scrollReturn++;
				}
				scrollNo++;
			}
			if (data.getAttribute("type")=="slide")
			{
				if(slideNo < maxItems)
				{
					document.getElementById("slide").appendChild(nameProto.cloneNode(true));
					document.getElementById("slide").appendChild(brprototype.cloneNode(true));
				}
				if (including==1 && (query["content"]=="0" || query["content"]=="3"))
				{
					document.getElementById("slides").appendChild(itemPro.cloneNode(true));
					document.getElementById("slides").appendChild(brprototype.cloneNode(true));
					slideReturn++;
				}
				slideNo++;
			}
			var descrip="";
			
		}
		if (streamNo > maxItems)
		{
			nameProto.firstChild.nodeValue="Archive";
			nameProto.setAttribute("href","streaming.html");
			nameProto.setAttribute("title","Video Streaming Archive");
			document.getElementById("streaming").appendChild(nameProto.cloneNode(true));
		}

		if (downloadNo > maxItems)
		{
			nameProto.firstChild.nodeValue="Archive";
			nameProto.setAttribute("href","download.html");
			nameProto.setAttribute("title","Video Download Archive");
			document.getElementById("download").appendChild(nameProto.cloneNode(true));
		}
		if (slideNo > maxItems)
		{
			nameProto.firstChild.nodeValue="Archive";
			nameProto.setAttribute("href","slideshow.html");
			nameProto.setAttribute("title","Slide Show Archive");
			document.getElementById("slide").appendChild(nameProto.cloneNode(true));
		}
		
		if (scrollNo > maxItems)
		{
			nameProto.firstChild.nodeValue="Archive";
			nameProto.setAttribute("href","scrollImage.html");
			nameProto.setAttribute("title","Scroll Image Archive");
			document.getElementById("scroll").appendChild(nameProto.cloneNode(true));
		}
		

		if (streamReturn == 0)
		{
			document.getElementById("streamings").style.display="none";
		}
		else
		{
			if (streamReturn==1)
			{
				document.getElementById("videoh").firstChild.nodeValue="Video Streaming "+"("+streamReturn+" item)";
			}
			else
			{
				document.getElementById("videoh").firstChild.nodeValue="Video Streaming "+"("+streamReturn+" items)";
			}
		}
		if (slideReturn == 0)
		{
			document.getElementById("slides").style.display="none";
		}
		else
		{
			if (slideReturn==1)
			{
				document.getElementById("slideh").firstChild.nodeValue="Slide Shows "+"("+slideReturn+" item)";
			}
			else
			{
				document.getElementById("slideh").firstChild.nodeValue="Slide Shows "+"("+slideReturn+" items)";
			}
		}
		if (scrollReturn == 0)
		{
			document.getElementById("scrolls").style.display="none";
		}
		else
		{
			if (scrollReturn==1)
			{
				document.getElementById("scrollh").firstChild.nodeValue="Scroll Images "+"("+scrollReturn+" item)";
			}
			else
			{
				document.getElementById("scrollh").firstChild.nodeValue="Scroll Images "+"("+scrollReturn+" items)";
			}
		}
	}
