// popcornA.js
//  Ajax JavaScript code for the popcornA.html document

/********************************************************/
// function getPlace
//  parameter: zip code
//  action: create the XMLHttpRequest object, register the
//          handler for onreadystatechange, prepare to send
//          the request (with open), and send the request,
//          along with the zip code, to the server
//  includes: the anonymous handler for onreadystatechange, 
//            which is the receiver function, which gets the 
//            response text, splits it into city and state,
//            and puts them in the document

function loadrecord(searchstring) { 
  document.myform2.value2.value = searchstring; 
  removeSuggest(this.value);
} 

function removeSuggest(city) {
  document.getElementById('hiddenDIV2').style.visibility='hidden';
  document.getElementById('hiddenDIV2').style.height='0px';
}

function getStuff(searchstring) {
type = document.myform2.type.value; 
  if (searchstring != "") {
    var xmlreq = false;
    
    // Create XMLHttpRequest object in non-Microsoft browsers
    if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
      
    } else if (window.ActiveXObject) {
      
      try {
        // Try to create XMLHttpRequest in later versions
        // of Internet Explorer
        
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
        
      } catch (e1) {
        
        // Failed to create required ActiveXObject
        
        try {
          // Try version supported by older versions
          // of Internet Explorer
          
          xhr = new ActiveXObject("Microsoft.XMLHTTP");
          
        } catch (e2) {
          
          // Unable to create an XMLHttpRequest by any means
          xhr = false;
        }
      }
    }
    
    
// Register the embedded handler function
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        var result = xhr.responseText;
        var place = result.split("|||");
        var i, current;
        i=0;
        current = 1;
        type = "stuff";
        response = "<table width=550><tr style=\"border: 1px solid black\"><td align=\"center\"><font size=\"3\"><font size=\"2\">Top 5 Stuff Matches:</td></tr>";
        for (i in place) {
          place[i] = place[i].replace(/\s*[(]/g,' ('); //Remove extra spaces from database

          if (i == 0){
		exactmatchindex = place[i].indexOf("||ES||"); 
		if (exactmatchindex != -1) {
         		current = 0;
			place[i] = place[i].substring(9, 999);
		}
	  }

if (place[i].indexOf("||M||") != "-1") {
if (current == 1) {
response = response + "<tr><td><font size=1 color='black' STYLE=\"text-decoration:none;font-style:normal;font-weight:bold\"><center>No Stuff Matches Found</font></td></tr>";
}
current = 0;
type = "manufacturer";
}

if (place[i].indexOf("||C||") != "-1") {
if (current == 2) {
response = response + "<tr><td><font size=1 color='black' STYLE=\"text-decoration:none;font-style:normal;font-weight:bold\"><center>No Manufacturer Matches Found</font></td></tr>";
}
current = 0;
type = "category";
}
        citydisplay = place[i].split(" (");
        //statedisplay = citydisplay[1] + "";
        //statedisplay = statedisplay.replace(/[)]/g, "");
//manudisplay = citydisplay[0] + citydisplay[1].split(" - ")[0];
	

         if (place[i].length > 3 && type == "stuff"){
		if (i > 0){
			place[i] = place[i].substring(2,250);
		}
tag = citydisplay[1].split(" - ")[0] + "-" + citydisplay[0]; 
tag = tag.replace(/ /g, "");
tag = tag.replace(/&#32;/g, "");
        	response = response + "<tr><td style=\"border: 0px solid black; padding: 0px 5px 0px 5px\">" + "<font size=1>" + current + " <a STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\" href=\"details.asp?Type=Name&value=" + citydisplay[0] + "\"><font size=2 color='blue' STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\">";
 
		if (current == 0){
			response = response + "<b>"
		}
		response = response + place[i] + "</font></a></td></tr>";
          }

	
 else if (place[i].length > 3 && type == "manufacturer"){

if (current == 0) {
        response = response + "<tr style=\"border: 1px solid black\"><td align=\"center\"><font size=\"2\">Top 5 Manufacturer Matches:</td></tr>";	
} else {

exactmatchindex = place[i].indexOf("||EM||"); 
	if (exactmatchindex != -1) {
place[i] = place[i].substring(9, 999);
tag = citydisplay[0].substring(9, 999).replace(/ /g, "");
tag = tag.replace(/&#32;/g, "");
response = response + "<tr><td style=\"border: 0px solid black; padding: 0px 5px 0px 5px\">" + "<font size=1>" + current + " <a STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\" href=\"details.asp?Type=tag&value=" + tag + "\"><font size=2 color='blue' STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\"><b>" + place[i] + "</b></font></a></td></tr>";
} else {
tag = citydisplay[0].replace(/ /g, "");
tag = tag.replace(/&#32;/g, "");
response = response + "<tr><td style=\"border: 0px solid black; padding: 0px 5px 0px 5px\">" + "<font size=1>" + current + " <a STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\" href=\"details.asp?Type=tag&value=" + tag + "\"><font size=2 color='blue' STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\">" + place[i] + "</font></a></td></tr>";
}
 }
}
      
else if (place[i].length > 3 && type == "category"){

if (current == 0) {
        response = response + "<tr style=\"border: 1px solid black\"><td align=\"center\"><font size=\"2\">Top 5 Category Matches:</td></tr>";
} else {
exactmatchindex = place[i].indexOf("||EC||"); 
	if (exactmatchindex != -1) {
place[i] = place[i].substring(9, 999);
tag = citydisplay[0].substring(9, 999).replace(/ /g, "");
tag = tag.replace(/&#32;/g, "");
response = response + "<tr><td style=\"border: 0px solid black; padding: 0px 5px 0px 5px\">" + "<font size=1>" + current + " <a STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\" href=\"details.asp?Type=tag&value=" + tag + "\"><font size=2 color='blue' STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\"><b>" + place[i] + "</b></font></a></td></tr>";
} else {
tag = citydisplay[0].replace(/ /g, "");
tag = tag.replace(/&#32;/g, "");
response = response + "<tr><td style=\"border: 0px solid black; padding: 0px 5px 0px 5px\">" + "<font size=1>" + current + " <a STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\" href=\"details.asp?Type=tag&value=" + tag + "\"><font size=2 color='blue' STYLE=\"text-decoration:none;font-style:normal;font-weight:normal\">" + place[i] + "</font></a></td></tr>";;
 }
}
}
 current = current + 1;
}   

if (current == 2) {
response = response + "<tr><td><font size=1 color='black' STYLE=\"text-decoration:none;font-style:normal;font-weight:bold\"><center>No Category Matches Found</font></td></tr>";
}

	response = response + "<tr><td></td></tr></table> <!-- <font size = 1><br /></font> -->";
	document.getElementById("hiddenDIV2").style.visibility="visible"; 
	document.getElementById('hiddenDIV2').style.height='';
	if (i > 0){
  		document.getElementById("hiddenDIV2").innerHTML=response;
	} else {
  		document.getElementById("hiddenDIV2").innerHTML="<center>No Match Found</center>"; 
	}
      }
    }
  random_num = (Math.round((Math.random()*20)+1));
  xhr.open("GET", "gettags.asp?type=" + type + "&value=" + searchstring + "&random=" + random_num);
  xhr.send(null);
  } else {
    document.getElementById("hiddenDIV2").style.visibility="hidden"; 
    document.getElementById('hiddenDIV2').style.height='0px';
  }
  }


