// This script is used for the Parts Price & Availability page to generate the dropdown and as the AJAX front end for the search
function main() {
  createDropDown("modelnum");
	}

// This function creates the master dropdown list to select search by part # or model #, etc.
function createDropDown(option) {
	switch( option ) {
    case "partnum":
			txt = "<select id=\"list\" onchange=\"createDropDown(document.getElementById('list').value)\">\n";
      txt = txt + "<option selected=\"selected\" value=\"partnum\">Search by Part Number</option>\n";
      txt = txt + "<option value=\"modelnum\">Search by Model</option>";
	    txt = txt + "</select>\n";
	    txt = txt + "<p style=\"margin-top: 15px; text-align: center\">Enter a part number to check pricing and availability.</p>\n";
			txt = txt + "<div class=\"container\">\n";
			txt = txt + "<div class=\"floatform\" style=\"text-align: right\">\n";
			txt = txt + "<b>Part Number:</b>\n";
			txt = txt + "</div>\n";
			txt = txt + "<div class=\"floatform\">\n";
			txt = txt + "<input type=\"text\" maxlength=\"20\" onkeyup=\"autoComplete(event,'box','partnum','part','partnum')\" id=\"partnum\" size=\"20\"/>\n"; 
			txt = txt + "<div id=\"box\"></div>";
			txt = txt + "</div>\n";
	    txt = txt + "<div class=\"floatform\" style=\"text-align: left\">\n";
			txt = txt + "<input type=\"button\" value=\"Search\" onclick=\"partSearch(document.getElementById('partnum'),'part','scripts/partsquery.php')\" />\n";
			txt = txt + "</div>\n";
			txt = txt + "</div>\n";
      break;
    case "modelnum":
		  txt = "<select id=\"list\" onchange=\"createDropDown(document.getElementById('list').value)\">\n";
      txt = txt + "<option value=\"partnum\">Search by Part Number</option>\n";
      txt = txt + "<option selected=\"selected\" value=\"modelnum\">Search by Model</option>";
	    txt = txt + "</select>\n";
	    txt = txt + "<p style=\"margin-top: 15px; text-align: center\">Enter a model number to check pricing and availability.</p>\n";
	    txt = txt + "<div class=\"container\">\n";
			txt = txt + "<div class=\"floatform\" style=\"text-align: right\">\n";
			txt = txt + "<b>Model Number:</b>\n";
			txt = txt + "</div>\n";
			txt = txt + "<div class=\"floatform\">\n";
			txt = txt + "<input type=\"text\" maxlength=\"20\" onkeyup=\"autoComplete(event,'box','partnum','model','partnum')\" id=\"partnum\" size=\"20\"/>\n";
			txt = txt + "<div id=\"box\"></div>";
			txt = txt + "</div>\n";
	    txt = txt + "<div class=\"floatform\" style=\"text-align: left\">\n";
			txt = txt + "<input type=\"button\" value=\"Search\" onclick=\"partSearch(document.getElementById('partnum'),'model','scripts/partsquery.php')\" />\n";
			txt = txt + "</div>\n";
			txt = txt + "</div>\n";
			break;
  }
	updateHTML(txt, 'searchmethod')
}

// This is the main search function call to load the PHP search script
function partSearch(str1, type, script) {
 	xmlHttp=GetXmlHttpObject();
 	if (xmlHttp==null) {
 		alert ("Browser does not support HTTP Request");
 		return;
  } 

  // Hide the autocomplete box
  box('0');

  // If any fields are blank, does not continue.
  reason = isEmpty(str1, "<b>A model or part number must be entered!</b><br />");

  if (reason != "") {
    updateHTML( reason, 'output' );
    return false;
  }

  script=script+"?id="+str1.value;
  script=script+"&type="+type;
  script=script+"&sid="+Math.random();
  xmlHttp.onreadystatechange = function() {stateChanged("output", xmlHttp);}
  xmlHttp.open("GET",script,true);
  xmlHttp.send(null);
}

