// Script 13.6 - stores.js

/*	This page does all the magic for applying
 *	Ajax principles to a store retrieval form.
 *	The users's zip code is sent to a PHP 
 *	script which will return data in JSON format.
 */

// Function that starts the Ajax process:
var theUserEnteredZip = 0;
var theUserEnteredMiles = 0;
var theUserEnteredState = '';
var myAjax = returnAjaxConn();
var yahooMapAddresses = new Array();

function get_stores(zip,milesAway) 
	{

	if (myAjax) {
		map.removeAllMarkers();
		
		theUserEnteredZip = zip;
		theUserEnteredMiles = milesAway;
		// Call the PHP script.
		// Use the GET method.
		// Pass the zip code in the URL.
		myAjax.open('get', 'find_stores.php?zip=' + encodeURIComponent(zip) + '&' + 'milesAway=' + encodeURIComponent(milesAway));
		yahooMapAddresses.length = 0;
		// Function that handles the response:
		myAjax.onreadystatechange = function ()
			{
			handle_stores();	
			}
		//myAjax.onreadystatechange = handle_stores;
		
		// Send the request:
		myAjax.send(null);

		return false;
		
	} else { // Can't use Ajax!
		return true;
	}
	
} // End of get_stores() function.

function get_storesbyState(state) 
	{
	
	
	if (myAjax) { 
		map.removeAllMarkers();
		
		theUserEnteredState = state;
		//theUserEnteredMiles = milesAway;
		myAjax.open('get', 'find_stores.php?state=' + encodeURIComponent(state));
		yahooMapAddresses.length = 0;
		// Function that handles the response:
		myAjax.onreadystatechange = function ()
			{
			handle_stores(state);	
			}
		
		// Send the request:
		myAjax.send(null);

		//return false;
		
	} else { // Can't use Ajax!
		return true;
	}
	
} // End of get_stores() function.


// Function that handles the response from the PHP script:
function handle_stores(isItAState) {
	
	if (isItAState)
		{
			
		if ( (myAjax.readyState == 4) && (myAjax.status == 200) ) 
			{
		
			// Check the length of the response:
			if (myAjax.responseText.length > 10) 
				{			
				// Send the response, in object form, 
				// to the show_stores() function:
				show_stores((eval('(' + myAjax.responseText + ')')), isItAState);	
				
				} 
			else 
				{
				document.getElementById('list').innerHTML = '<p>No stores matched your search.</p>';
				}			
			}	
		}
	else
		{
		// If everything's OK:
		//alert ('AJAX.');
		if ( (myAjax.readyState == 4) && (myAjax.status == 200) ) 
			{
		
			// Check the length of the response:
			if (myAjax.responseText.length > 10) 
				{			
				// Send the response, in object form, 
				// to the show_stores() function:
				show_stores(eval('(' + myAjax.responseText + ')'));	
				
				} 
			else 
				{
				document.getElementById('list').innerHTML = '<p>No stores matched your search.</p>';
				}			
			}
	   } // else
	
} // End of handle_stores() function.

// Function that shows the list of stores:
function show_stores(stores, typeOfSearch) {

	// Initialize a string:
	var message = '';
	var resultsTag = '';
	var distanceMilesInfo = '';
	var closestStoreAddress = '';
	var theZoomLevel = 0;
	// Get each store:
	for (var i = 0 ; i < stores.length ; i++) {

		// Add to the string:
		if (typeOfSearch)
			{
			resultsTag = 'Results for Locations within ' +  stores[0].state + '<br />';	
			}
		else
			{
			resultsTag = 'Results for Locations within ' +  theUserEnteredMiles + ' miles of ' + theUserEnteredZip + '<br />';
			distanceMilesInfo = '(approximately ' + stores[i].distance + ' miles)</p>' + '</li>';
			}
		
		message += '<li>' + '<h2>' + stores[i].name + ' #' + stores[i].storeNumber + ' - ' + stores[i].state + '</h2>';
		if ((stores[i].contactName != '') || (stores[i].contactName != 'NULL') || (stores[i].contactName != null))
			{
			message += 'Contact: ' + stores[i].contactName + '<br />';
			}		
		
		message += stores[i].address + '<br />'
		+ stores[i].city + ', ' + stores[i].state + ' ' + stores[i].zip + '<br /><br />'
		+ '<b>' + stores[i].phone;
		
		if ((stores[i].phone2 != '') || (stores[i].phone2 != 'NULL') || (stores[i].phone2 != null) || (stores[i].phone2 != NULL))
			{
			message += ', ' + stores[i].phone2 + '</b>' + '<br />';
			}
		else
			{
			message += '</b>'+ '<br />';	
			}
		
		message += 'Email: ' + stores[i].email + '<br />'
		+ 'Website: ' + '<a href="' + stores[i].website + '">' + stores[i].website + '</a>' + '<br />' + distanceMilesInfo;
		
		
		
		yahooMapAddresses[i] = stores[i].address + ' ' + stores[i].city + ', ' + stores[i].state + ' ' + stores[i].zip;
		

	}
	
	closestStoreAddress = stores[0].address + ' ' + stores[0].city + ', ' + stores[0].state + ' ' + stores[0].zip;
	
	if (stores.length < 2)
		{
		theZoomLevel = 5;
		}
	else
		{
		theZoomLevel = 10; 
		}
	 
	

	// Place the string in the page:
	document.getElementById('list').innerHTML = resultsTag + message;
	
	
	 function addNewMarker() 
		{		
		for (var i=0; i<yahooMapAddresses.length; i++) 
			{
			var marker = new CustomPOIMarker(stores[i].name, ' #' + stores[i].storeNumber + '  <br />' + stores[i].phone, yahooMapAddresses[i], '0xFF0000', '0xFFFFFF');
			map.addMarkerByAddress( marker, yahooMapAddresses[i] );
			}
		map.setCenterByAddressAndZoom(closestStoreAddress, theZoomLevel, 300);
		}
	
	addNewMarker();




} // End of show_stores() function.
