﻿// JavaScript File
//based on code provided on DZone Snippets by Wieland 9/26/06
    window.onload=loadIndex;
 function loadIndex() { // load indexfile
    // most current browsers support document.implementation
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		//xmlDoc.load("xml_proxy.php?url=http://98.131.16.234/KeywordXML.aspx");
		//xmlDoc.load("KeywordXML.aspx");
		xmlDoc.load("keywordproxy.php");
	}
    // MSIE uses ActiveX
	else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		//xmlDoc.load("xml_proxy.php?url=http://98.131.16.234/KeywordXML.aspx");
		//xmlDoc.load("KeywordXML.aspx");
		xmlDoc.load("keywordproxy.php");

	}
}

//multi-dimensional array numeric sort
//using third dimension
function SortDescendThird(a, b)
{
    return b[2]-a[2];
}


function searchIndex() { // search the index (duh!)
	if (!xmlDoc) {
		loadIndex();
	}
	// get the search term from a form field with id 'searchme'
	var searchterm = document.getElementById("searchme").value;
    if (searchterm.length < 3) {
	    alert("Enter at least three characters");
    } 
    else 
    {
		//alert("DEBUG: xmlDoc nodes "+xmlDoc.documentElement.nodeValue);
        //cycle through each company
        //find the number of matches
	    var Companies = xmlDoc.getElementsByTagName("company");
        var CompanyMatches=new Array();
        
        
	    for (var CompanyIndex=0;CompanyIndex<Companies.length;CompanyIndex++)
	    {
	        //alert("Company "+CompanyIndex+" text: "+Companies[CompanyIndex].text);
	        var CompanyNode=Companies[CompanyIndex];
	        var allitems = CompanyNode.getElementsByTagName("item");
	        results = new Array;
	        
	        for (var i=0;i<allitems.length;i++) {
            // see if the XML entry matches the search term,
            // and (if so) store it in an array
		        var name = allitems[i].lastChild.nodeValue;
		        var exp = new RegExp(searchterm,"i");
		        if ( name.match(exp) != null) {
			        results.push(allitems[i]);
		        }
	        }

            if (results.length>0)
            {
                //create a company match
                //get the title
                var Title=CompanyNode.getElementsByTagName("business_name")[0].firstChild.data;//.text;
                //get the link
                var Link=CompanyNode.getElementsByTagName("url")[0].firstChild.data;//.text;
                //get the description
                var Desc=CompanyNode.getElementsByTagName("description")[0].firstChild.data;//.text;
                
                var MatchedCompany=new Array(Title,Link,results.length,Desc);
                CompanyMatches.push(MatchedCompany);
            }
     
	    }

	    DisplayResults(CompanyMatches,searchterm);
	}
}

function DisplayResults(CompanyMatches, searchterm) {

	if (CompanyMatches.length > 0) 
	{
        // if there are any results, put them in a list inside the "resultshere" div
		var resultshere = document.getElementById("resultshere");
		var header = document.createElement("h5");
		var list = document.createElement("div");
		var searchcount=CompanyMatches.length;
		var searchedfor = document.createTextNode("Your search for '"+searchterm+"' resulted in "+searchcount+" result(s).");
		//remove previous search results
		while (resultshere.childNodes.length>0)
		{
		    resultshere.removeChild(resultshere.childNodes[0]);
		}

		//sort
		var SortedMatches=CompanyMatches.sort(SortDescendThird);
		//var SortedMatches=CompanyMatches;
		for (var i=0;i<SortedMatches.length;i++) {
			//var listitem = document.createElement("p");
			//add breaks
			var SplitDesc=new Array();
			var strDescription=SortedMatches[i][3];
			SplitDesc=SortedMatches[i][3].split("\n");
			var Para=new Array();
			for (j=0;j<SplitDesc.length;j++)
			{
			    Para[j]=document.createElement("p");
			    var DescText=document.createTextNode(SplitDesc[j]);
			    Para[j].appendChild(DescText);
			    list.appendChild(Para[j]);
			}

			var link1=document.createElement("a");
			//replace the spaces with underscores in the business name
			var PageName=SortedMatches[i][0].replace(/ /g,"_");
			//replace the apostrophes, periods and commas
			PageName=PageName.replace(/,/g,"");
			PageName=PageName.replace(/\./g,"");
			PageName=PageName.replace(/\'/g,"");
			
			PageName+=".html";
			link1.setAttribute("href",PageName);
			//link1.appendChild(document.createTextNode(SortedMatches[i][0]));
			link1.appendChild(document.createTextNode("Click here for "+SortedMatches[i][0]+" Business Profile"));

            //url
			var link2=document.createElement("a");
			link2.setAttribute("href",SortedMatches[i][1]);
			//link2.appendChild(document.createTextNode(SortedMatches[i][1]));
			link2.appendChild(document.createTextNode("Click here for website"));

			//listitem.appendChild(desc);
			var br=document.createElement("br");
			Para[SplitDesc.length-1].appendChild(br);
			Para[SplitDesc.length-1].appendChild(link1);
			Para[SplitDesc.length-1].appendChild(document.createTextNode(" | "));
			Para[SplitDesc.length-1].appendChild(link2);
		}	
			
		resultshere.appendChild(header);
		resultshere.appendChild(list);
		header.appendChild(searchedfor);


	} 
	else 
	{
        // else tell the user no matches were found
		var resultshere = document.getElementById("resultshere");
		//remove previous search results
		while (resultshere.childNodes.length>0)
		{
		    resultshere.removeChild(resultshere.childNodes[0]);
		}

		var para = document.createElement("p");
		var notfound = document.createTextNode("Sorry, I couldn't find anything like '"+searchterm +"'!");
		resultshere.appendChild(para);
		para.appendChild(notfound);
	}
}
