/*
// $RCSfile: ajax.js,v $
// $Source: C:/cvsrepo/cvs/sportsweb-live/includes/ajax.js,v $, $Revision: 1.1 $, $Date: 2005/10/28 14:02:31 $, $State: Exp $ 
*/

function doSearch(base) {

  // Obtain an XMLHttpRequest instance
  /*var req = newXMLHttpRequest();
  
  if(req) { // Can do AJAX: carry on
	  // Set the handler function to receive callback notifications
	  // from the request object
	  var handlerFunction = getReadyStateHandler(req, updateSearch);
	  req.onreadystatechange = handlerFunction;
	  
	  // Open an HTTP POST connection to the shopping cart.
	  // Third parameter specifies request is asynchronous.
	  req.open("POST", base+"ajax.php", true);
	
	  // Specify that the body of the request contains form data
	  req.setRequestHeader("Content-Type", 
						   "application/x-www-form-urlencoded");
	
	  // Send form encoded data stating that I want to add the 
	  // specified item to the cart.
	  var brand = document.forms['fSearch'].brand.value;
	  var sport = document.forms['fSearch'].sport.value;
	  var size = document.forms['fSearch'].size.value;
	  var rx = document.forms['fSearch'].rx.value;
	  
	  var query = "brand="+brand+"&sport="+sport+"&size="+size+"&rx="+rx;
	  req.send(query);
  } else {	 // can't do AJAX; just submit the form*/
	  	document.forms['fSearch'].submit();
  /*}*/
}

function updateSearch(searchXML) {

 // Get the root "cart" element from the document
 var cart = searchXML.getElementsByTagName("search")[0];

	updateOptions(document.forms['fSearch'].brand, cart.getElementsByTagName("brand")[0]);
	updateOptions(document.forms['fSearch'].sport, cart.getElementsByTagName("sport")[0]);
	updateOptions(document.forms['fSearch'].size, cart.getElementsByTagName("size")[0]);
	updateOptions(document.forms['fSearch'].rx, cart.getElementsByTagName("rx")[0]);
}

function updateOptions(elt, list) {
		var opt, node, elts;
		elt.options.length = 0;
		elts = list.getElementsByTagName("option");
		
		for(var i=0; i < elts.length; i++) {
			node = elts[i];
			//alert(node);
			opt = new Option(node.firstChild.nodeValue,
							 node.getAttribute("value"),
							 false,
							 node.getAttribute("selected")=="selected");
			/*opt = document.createElement("option");
			opt.text = node.nodeValue;
			opt.value = node.getAttribute("value");
			if(node.getAttribute("selected")=="selected") {
				opt.selected = true;
			}*/
			elt.options[elt.options.length] = opt;
			//elt.appendChild(opt);
		}
}
