function decoderFunc2(thisstring) {
	var returnstring = unescape(thisstring);
	var returnstring2 = returnstring.replace(/\+/g, " ");
	return returnstring2;
}

// holds an instance of XMLHttpRequest
var xmlHttpSearch = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttpSearch;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttpSearch = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array('MSXML2.xmlHttpSearch.6.0',
                                    'MSXML2.xmlHttpSearch.5.0',
                                    'MSXML2.xmlHttpSearch.4.0',
                                    'MSXML2.xmlHttpSearch.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP');
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttpSearch; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttpSearch = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttpSearch)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttpSearch;
}

// read a file from the server
function doSearch() {
	var searchbox = document.getElementById("search");
	var searchword = searchbox.value;
  // only continue if xmlHttp isn't void
  if (xmlHttpSearch)
  {
    // try to connect to the server
    try
    {
	var theUrl = "processing/search.php?keyword=" + searchword;
		//alert(theUrl);
    	// initiate reading a file from the server
    	xmlHttpSearch.open("GET", theUrl, true);
    	xmlHttpSearch.onreadystatechange = handleRequestStateChangeSearch;
    	xmlHttpSearch.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChangeSearch() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttpSearch.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttpSearch.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponseSearch();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttpSearch.statusText);
    }
  }
}

 
// handles the response received from the server
function handleServerResponseSearch()
{
	if (document.getElementById("frontpg")) {
		featuredDiv = document.getElementById("frontpg");
		featuredDiv.innerHTML = "";
	}
  // read the message from the server
  var xmlResponseSearch = xmlHttpSearch.responseXML;
  // obtain the XML's document element
  xmlRootSearch = xmlResponseSearch.documentElement;  
  // obtain arrays with manufacturer names and ids 
  searchNamesArray = xmlRootSearch.getElementsByTagName("name");
  searchIdsArray = xmlRootSearch.getElementsByTagName("id");
  searchThumbsArray = xmlRootSearch.getElementsByTagName("thumb");
  searchDialsArray = xmlRootSearch.getElementsByTagName("dial");
  numresults = xmlRootSearch.getAttribute("numresults");
  searchkeyword = xmlRootSearch.getAttribute("thekeyword");
  // generate HTML output
  var html = "";  
  var mystring = "";
  myDiv = document.getElementById("showwatches");
  mainDiv = document.getElementById("pagetext");
  mainDiv.innerHTML = "";
  // iterate throughthe arrays and create an HTML structure
  if (numresults == 0) {
  	html += "Your search for &quot;" + decoderFunc2(searchkeyword) + "&quot; returned no results.";
  } else if (numresults == 1) {
  	html += "Your search for &quot;" + decoderFunc2(searchkeyword) + "&quot; returned " + numresults + " result:";
	  for (var i=0; i < searchNamesArray.length; i++) {
		html += "<div class=\"watchbox\">";
		html += "<a href=\"#\" onclick=\"showWatch('" + decoderFunc2(searchIdsArray.item(i).firstChild.data) + "');\">";
		html += "<img src=\"images/thumbs/" + searchThumbsArray.item(i).firstChild.data + "\" width=\"138\" height=\"131\" alt=\"" + decoderFunc2(searchNamesArray.item(i).firstChild.data) + "\" border=\"0\" />";
		html += "</a>";
		html += "<span class=\"watchname\"><a href=\"#\" onclick=\"showWatch('" + searchIdsArray.item(i).firstChild.data + "')\">" + decoderFunc2(searchNamesArray.item(i).firstChild.data) + "</a></span><br />";
		html += "<span class=\"watchdial\">" + decoderFunc2(searchDialsArray.item(i).firstChild.data) + "</span><br />";
		html += "</div>";		
		}
  } else {
	  html += "Your search for &quot;" + decoderFunc2(searchkeyword) + "&quot; returned " + numresults + " results:";
	  for (var i=0; i < searchNamesArray.length; i++) {
		html += "<div class=\"watchbox\">";
		html += "<a href=\"#\" onclick=\"showWatch('" + searchIdsArray.item(i).firstChild.data + "');\">";
		html += "<img src=\"images/thumbs/" + searchThumbsArray.item(i).firstChild.data + "\" width=\"138\" height=\"131\" alt=\"" + decoderFunc2(searchNamesArray.item(i).firstChild.data) + "\" border=\"0\" />";
		html += "</a>";
		html += "<span class=\"watchname\"><a href=\"#\" onclick=\"showWatch('" + searchIdsArray.item(i).firstChild.data + "')\">" + decoderFunc2(searchNamesArray.item(i).firstChild.data) + "</a></span><br />";
		html += "<span class=\"watchdial\">" + decoderFunc2(searchDialsArray.item(i).firstChild.data) + "</span><br />";
		html += "</div>";		
		}
	    //html += "<a href=\"#\" onclick=\"showManufacturers('" + searchIdsArray.item(i).firstChild.data + "'); return false;\">" + searchNamesArray.item(i).firstChild.data +  "</a><br/>";
	  // obtain a reference to the <div> element on the page
	  }
	  // display the HTML output
	 	 myDiv.innerHTML = html;
		breadDiv = document.getElementById("breadcrumbs");
		//breadDiv.innerHTML = "Brands | " + thebrandname;
	
}

