// javascript powering the Provider Search

function runSearch(country, state) {

	

    try {

      if (country != 'NONE') {	

	var options = {action: 'search', country: country};
	if (state)
	  options = Object.extend(options, {state: state});

	// Searching: run an AJAX query to get search text
	new Ajax.Request('db/providerDriver.php', 
			 { method: 'get', 
			     parameters: options, 
			     onSuccess: function(transport) {

			       var resultsBox = $('resultsBlock');
			       resultsBox.show();
			       resultsBox.innerHTML = transport.responseText || "Error ocurred.";
			     
			       // make the results pageable
			       var options = {
				 pagedElements: $$('.providerResult'), 
				 nextButton: $('nextButton'),
				 prevButton: $('prevButton'),
				 statusBar: $('viewingResults')
			       }
			     
			       resultsBox.pageable = new Pageable(resultsBox, options);
			     
			       var msgBox = $('foundResults');
			       msgBox.innerHTML = 'Found ' + resultsBox.pageable.numItems() + ' providers.';
			   }
			 });      
      } // end if (country)
  
    } catch (e) {
      alert('An exception has occurred in this script. ' +
	    'Please alert MedForward technical support immediately ' +
	    'at support@medforward.com with the following information:' +
	    "\n\tError name: " + e.name +
	    "\n\tError message: " + e.message + '.' );
      
    }
}

// Let's people know that we're searching
function updateStatusBox() {
  
  var msgBox = $('foundResults');
  msgBox.show();
  msgBox.innerHTML = 'Searching...';
}

window.onload = function() {

  // Register this function to start anytime any AJAX call occurs
  Ajax.Responders.register({onCreate: updateStatusBox});

  // Run the search and hide the menus if get data was detected
  var qsParm = new Array();
  
	function qs() {
		var query = window.location.search.substring(1);
		var parms = query.split('&');
		for (var i=0; i<parms.length; i++) {
			var pos = parms[i].indexOf('=');
			if (pos > 0) {
				var key = parms[i].substring(0,pos);
				var val = parms[i].substring(pos+1);
				qsParm[key] = val.replace('+', ' ');
			}
		}
	} 
	qs();  //gather the get data, if any
	if (qsParm['country'] != null) {
		document.getElementById(qsParm['country']).selected = true;
		if (qsParm['state'] != null) {
			document.getElementById(qsParm['state']).selected = true;
			$('selectState').style.display = 'block'; 
			runSearch(qsParm['country'],qsParm['state']);
		} else {
			
			runSearch(qsParm['country'],null);	
		}
	}

  // Run the search on the select state
  $('select_state').onchange = function(event) {

     

    var state = this.value;
    var country = $('select_country').value;
	if (this.selectedIndex != 0)
    	runSearch(country, state);    
  }

  // Run the search as soon as they choose a country
  $('select_country').onchange = function(event) {        

      // See if they selected a country
      var country = this.value;

      if (country == 'United States') {
	$('select_state').selectedIndex = 0;
	$('selectState').style.display = 'block'; 
	$('resultsBlock').hide();
	$('foundResults').hide();
	$('viewingResults').hide();
	$('nextButton').hide();
	$('prevButton').hide();
      }
      else {
	$('selectState').hide();
	runSearch(country, null);
      }

  }
  
}