var last_store_id = null;
var map = null;
var marker = new Array();
var ew = null;

function makeObject(){
var x;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer"){
x = new ActiveXObject("Microsoft.XMLHTTP");
}
else{
x = new XMLHttpRequest();
}
return x;
}

var request = makeObject();

function open1(myform) {
if (document.forms[myform].url.value == "" || document.forms[myform].method.value=="") {

}
else {
request.open(''+document.forms[myform].method.value+'', ''+document.forms[myform].url.value+'');
request.onreadystatechange = parseInfo1;
request.send('');
}
}

function open2(myform) {
if (document.forms[myform].url2.value == "" || document.forms[myform].method2.value=="") {

}
else {
request.open(''+document.forms[myform].method2.value+'', ''+document.forms[myform].url2.value+'');
request.onreadystatechange = parseInfo2;
request.send('');
}
}

function open3(url,vars) {
  request.open('GET',url+"?"+vars);
  request.onreadystatechange = parseInfo2;
  request.send('');
}

function openPP(myform) {
if (document.forms[myform].url.value == "" || document.forms[myform].method.value=="") {

}
else {
request.open(''+document.forms[myform].method.value+'', ''+document.forms[myform].url.value+'');
request.onreadystatechange = parseInfoPP;
request.send('');
}
}

function openLocator() {
	if (document.getElementById('url').value != "" || document.getElementById('method').value != "") {
		request.open(''+document.getElementById('method').value+'', ''+document.getElementById('url').value+'');
		request.onreadystatechange = parseInfoLocator;
		request.send('');
	}
}

//////////////////////////////////////////////////

function parseInfo2(){
if(request.readyState == 1){
document.getElementById('my_div2').innerHTML = "<table width='100%' border='0' cellspacing='0'><tr><td valign='top' align='left' height='20' class='fntHeader'>LOADING...</td></tr></table>";
}
if(request.readyState == 4){
var answer = request.responseText;
document.getElementById('my_div2').innerHTML = answer;
}
}

function parseInfo1(){
if(request.readyState == 1){
document.getElementById('my_div').innerHTML = "<table width='100%' border='0' cellspacing='0'><tr><td valign='top' align='right' height='20' class='fntHeader'>LOADING...<img src='../img/spacer.gif' width='5' height='5' hspace='3' /></td></tr></table>";
}
if(request.readyState == 4){
var answer = request.responseText;
document.getElementById('my_div').innerHTML = answer;
}
}

function parseInfoPP(){
if(request.readyState == 1){
document.getElementById('my_divPP').innerHTML = "<br><br><br><br>Sending...";
}
if(request.readyState == 4){
var answer = request.responseText;
document.getElementById('my_divPP').innerHTML = answer;
}
}

/*
function createMarker(point, icon, store_id) {
  marker[store_id] = new GMarker(point, icon);
  GEvent.addListener(marker[store_id], "click", function() {
	if(map){
		map.closeInfoWindow();
	}
	window.location.hash = "#" + store_id;
	setColor(store_id);
	//alert(store_id);
  });
  return marker[store_id];
}
*/

function createMarker(point, icon, store_id) {
  marker[store_id] = new GMarker(point, icon);
  GEvent.addListener(marker[store_id], "click", function() {
	if(ew){
		ew.hide();
	}
	window.location.hash = "#" + store_id;
	setColor(store_id);
	//alert(store_id);
  });
  return marker[store_id];
}

function setColor(store_id){
	
	if (last_store_id)
	document.getElementById(last_store_id + "tbl").style.backgroundColor="#232323";
	
	document.getElementById(store_id + "tbl").style.backgroundColor="#660000";
	last_store_id = store_id;	
}

/**************************************************************
* "parseInfoLocator()" Uses the XHTMLObject asynchronously
*  The response is retrieved from "/functions/search_locator.php"
**************************************************************/
function parseInfoLocator(){
	var counter = 0;
	var loading_image = supportsPNG24() ? "/misc/img/locator_loading.png" : "/misc/img/locator_loading.gif";
	
	/**************************************************************
	* While there is no response display the searching messages.
	**************************************************************/
	if(request.readyState == 1){
		var searching_content = "<table width=100%><tr><td align='center' class='fntReg'><br /><br /><br /><br /><br /><br /><br /><br />Loading Store Locations...</td></tr></table>";
		document.getElementById('contentDiv').innerHTML = searching_content;
		searching_content = "<table width=100%><tr><td align='center' class='fntReg'><br /><br /><br /><br /><br /><br /><br /><img src='" + loading_image + "' /></td></tr></table>";
		document.getElementById('flashcontent').innerHTML = searching_content;
	}// end if
	
	
	/**************************************************************
	* When the request is complete.
	**************************************************************/
	if(request.readyState == 4){
		/**************************************************************
		* Separate the store locator data, from the map data.
		* "answer[0]" holds the HTML fromated store data to display
		* "answer[1]" holds the zip code, longitude, and latitude information
		* for the users location
		* "answer[2]" holds the zip code, longitude, latitude, and company
		* name of the different stores.
		**************************************************************/
		var answer = request.responseText.split("<!-- STORE_LOCATION_DATA -->\n");
		
		
		var originating_zip_longitude_and_latitude = answer[1].split(",");
		var store_longitude_latitudes = answer[2].split("\n");
		
		// Set the HTML fromated store data to "contentDiv" for display.
		document.getElementById('contentDiv').innerHTML = answer[0];
		// DIV ID: flashcontent
		//var geocoder = new GClientGeocoder();
		
		/**************************************************************
		* Check "answer[0]" to see if the user input an invalid
		* zip code, or there are no stores in the users area.  Then
		* output the stores location in the media area, and the data
		* in answer[0], then exit the function.
		* If there are results display the map of the location, 
		* the stores plotted with the appropriate baloon, and the list
		* of stores.
		**************************************************************/
		if(answer[0].match(/No\ records\ found|Please\ enter\ a\ valid\ Zip\ Code\ above/)){
			document.getElementById("flashcontent").innerHTML = "<img src=\"../flash/location.jpg\" border=\"0\" />";
		} else {
			
			/**************************************************************
			* If the browser is compatible with Google maps display
			* the users location with stores plotted in the media area.
			* If not display the image of the BSN building.
			**************************************************************/
			if (GBrowserIsCompatible()) {
				
				var icon = null;
				var store_icon_index = new Array(null,null,null,null);
				var display_store_icon_index = '';
				var distance = null;

				/**************************************************************
				* Instantiate the "GMap2" class, from the "EWindow.js" include.
				**************************************************************/
				map = new GMap2(document.getElementById("flashcontent"));
				
				/**************************************************************
				* Set the distance, the default is aproximately 5 miles.
				**************************************************************/
				switch(originating_zip_longitude_and_latitude[3]){
					// 11 is good for 5 miles
					// 10 is good for 10 miles
					// 9 is good for 25 miles
					// 8 is good for 50 miles
					// 7 is good for 100 miles
					case '10':
						distance = 10;
						break;
					case '25':
						distance = 9;
						break;
					case '50':
						distance = 8;
						break;
					case '100':
						distance = 7;
						break;
					default:
						distance = 11;
				}// end switch
				
				
				/**************************************************************
				* Use the small map contols
				**************************************************************/
				map.addControl(new GSmallMapControl());
				
				
				
				/**************************************************************
				* Set the searching image to use.
				**************************************************************/
				var searchIcon = new GIcon();
				searchIcon.image = supportsPNG24() ? "/misc/img/locator_loading.png" : "/misc/img/locator_loading.gif";
				searchIcon.iconSize = new GSize(295, 141);
				searchIcon.iconAnchor = new GPoint(148, 89);

				
				
				/**************************************************************
				* Create the small store icon template
				**************************************************************/
				var baseIcon = new GIcon();
				baseIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
				baseIcon.iconSize = new GSize(12, 20);
				baseIcon.shadowSize = new GSize(22, 20);
				baseIcon.iconAnchor = new GPoint(6, 20);
				baseIcon.infoWindowAnchor = new GPoint(5, 1);

				/**************************************************************
				* Create the different store icons
				**************************************************************/
				// GNC
				var redIcon = new GIcon(baseIcon);
				redIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
				
				// Vitamin Shoppe
				var blueIcon = new GIcon(baseIcon);
				blueIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
				
				// Vitamin World
				var greenIcon = new GIcon(baseIcon);
				greenIcon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
				
				// All other BSN Retailers
				var yellowIcon = new GIcon(baseIcon);
				yellowIcon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
	
				
				/**************************************************************
				* Center the map to the users zip code.
				**************************************************************/
				map.setCenter(new GLatLng(originating_zip_longitude_and_latitude[1], originating_zip_longitude_and_latitude[2]), distance);

				/**************************************************************
				* Display the loading image.
				**************************************************************/
				var loading_marker = new GMarker(new GLatLng(originating_zip_longitude_and_latitude[1], originating_zip_longitude_and_latitude[2]), searchIcon);
				map.addOverlay(loading_marker);
				
				/**************************************************************
				* Create an EWindow, this is used for the popup over the store
				* markers for the user to input their address.
				**************************************************************/
		      	ew = new EWindow(map, E_STYLE_LOCATOR);      
      			map.addOverlay(ew);
	  			
				
				/**************************************************************
				* Loop through the returned stores, specify the correct
				* icon to use for each store, and display a marker for the store
				* on the map.
				* The "store_icon_index" array is filled with store types
				* that are available in the users search area.
				**************************************************************/
				for (var counter = 0; counter < store_longitude_latitudes.length; counter++){
					var store_information = store_longitude_latitudes[counter].split(",");
					if(store_information[0]){
						
						switch(store_information[3]){
							case 'GNC':
								icon = redIcon;
								store_icon_index[0] = "<img src=\"/misc/img/reddot.png\" width=\"10\" height=\"10\" align=\"bottom\" /> GNC&nbsp;&nbsp;&nbsp;";
								break;
							case 'The Vitamin Shoppe':
								icon = blueIcon;
								store_icon_index[1] = "<img src=\"/misc/img/bluedot.png\" width=\"10\" height=\"10\" /> The Vitamin Shoppe&nbsp;&nbsp;&nbsp;";
								break;
							case 'Vitamin World':
								icon = greenIcon;
								store_icon_index[2] = "<img src=\"/misc/img/greendot.png\" width=\"10\" height=\"10\" /> Vitamin World&nbsp;&nbsp;&nbsp;";
								break;
							default:
								icon = yellowIcon;
								store_icon_index[3] = "<img src=\"/misc/img/yellowdot.png\" width=\"10\" height=\"10\" /> BSN Retailer";
						}
	
						
						map.addOverlay(createMarker(new GLatLng(store_information[1], store_information[2]), icon, store_information[0]));
					}
				}// end for
				
				
				/**************************************************************
				* Loop through the "store_icon_index" array, and set the
				* "storeIconIndex" div to display an index of the different
				* stores in the area that carry BSN products.
				**************************************************************/
				for(counter = 0; counter < store_icon_index.length; counter++){
					if(store_icon_index[counter]){
						display_store_icon_index += store_icon_index[counter];
					}
				}
				document.getElementById("storeIconIndex").innerHTML = display_store_icon_index;
				
				/**************************************************************
				* Remove the loading image. 
				**************************************************************/
				map.removeOverlay(loading_marker);
				
			} else {
				document.getElementById("flashcontent").innerHTML = "<img src=\"../flash/location.jpg\" border=\"0\" />";	
			}// end if
		}// end if
	}// end if
}// end fucntion





































