var currentdestinations = [];
var destinfos = [];
var destbubbles = [];
var destbubbles2 = [];
var gatheraddresses = [];
var destboxes = [];
var cpdestinations = [];
var destarray=[];
var hubbubble;
var hubtour;
var cpsearchtype;
var gcouponimage = "/globalgraphics/general/coupon.gif"

var outersearchradius;
var outersearchpoint;

var passportsearchtype;
var passportsearchradius;
var passportsearchterm;
var userpassportsearch;

//THE FOLLOWING VARIABLE IS FOR USE WITH SPONSORS AND IS SET TO TRUE INSIDE SPONSORFUNCTION.JS
var allowcustombubbles = false;

var currentbusinesses = [];
var bizinfos = [];
var bizbubbles = [];
var bizboxes = [];
var sbusinesses = [];

var bizinfo;
var bizbubble;
var bizbox;

var resultcount=0;
var prevcount;
var querycount;

var proxsearchstring;
var searchradius = 10;
var totalresults = 500;
var activeresult;
var activeresulttype;
var destnumber;
var activetextinput;
var currentquery;
var current_results;
var current_business_name;
var loadinganimation='<div align="center" style="padding:10px;"><img src="/globalgraphics/general/LoadingProgressBar.gif" /> Loading...</div>';

var cpresultslocation;
var cpresultsheader;
var cpresultscontrols;
var bizresultslocation;
var bizresultsheader;
var biznavresultslocation;
var activebizheader;
var gcustomzoom = 17;


function setsearchresultglobals(){
	cpresultslocation = document.getElementById("cplanetresults");
	cpresultsheader = document.getElementById("cpresultsheader");
	cpresultscontrols = document.getElementById("cpresultscontrols");
	bizresultslocation = document.getElementById("resultsmaster");
	bizresultsheader = document.getElementById("resultsheader");
	biznavresultslocation = document.getElementById("resultsnav");
}


function localgetdestinationcall(idtocall, orderby){	
	getdestinations(idtocall, orderby, 'self');
	
}

function createhubtour(hubpoint, tourid, subtour){
	
	if((hubpoint!='')&&(tourid!='')){
		var hubsuccess = getdestinations(hubpoint, '', 'sethub');
		hubtour = tourid;
		//alert(subtour);
		if(subtour!=true){
			clearmap();
			linecolor = dmosublinecolor;
		}else{
			linecolor = dmosublinecolor;
		}
	}else{
		alert('No HubPoint');	
	}
	
}

function dmadestsearch(){
	
	var searchterm = document.getElementById("txtDMO_passportsearch").value;
	passportsearchtype = 'destsearch';
	userpassportsearch = userspage;
	passportsearchterm = searchterm;
	
	getdestinations("", "", 'passportsearch');
	
}

//THE FOLLOWING 5 FUNCTIONS PROCESSES THE RESULTS FROM A CPLANET DATABASE QUERY ***************************************
function getdestinations(tourid, orderby, wherefrom){
	
	//alert(tourid);
	
	var bounds = new GLatLngBounds();
	var request = GXmlHttp.create();

	 //SETS LOADING IMAGE
	 try{
	 	cpresultslocation.innerHTML = loadinganimation;
	 }catch(err){
		//No Action 
	 }
	 
	 var d = new Date();
	 var currentitme = d.getTime();
	 var publiconly;
	 var issearch='true';
	 
	 try{
		 var whatuserspage = document.getElementById("HiddenFieldUser").value;
		 
		 if(whatuserspage==currentuser){
			publiconly = 0;
		 }else{
			publiconly = 1;
		 }
	 }catch(err){
		//NO ACTION 
	 }
 
 //THE FOLLOWING SWITCH CREATES THE CORRECT DATAFILE VARIABLE DEPENDING ON THE LOCATION OF THE CALL AND THE TYPE
	
	 switch (wherefrom){
		
		case 'search':
			cpsearchtype = 'tour';
			clearmap();
			var datafile = 'controls/Destinationsearch.aspx?tourid='+tourid+'&orderby='+orderby+'&time='+currentitme+'&ispublic='+publiconly+'&issearch='+issearch;
		break;
		
		case 'iframe':
			cpsearchtype = 'tour';
			clearmap();
			var datafile = 'Destinationsearch.aspx?tourid='+tourid+'&orderby='+orderby+'&time='+currentitme+'&ispublic='+publiconly;
		break;
		
		case 'prox_storefronts':
			cpsearchtype = 'prox';
			var datafile = 'controls/areasearchxml.aspx?lat='+currentlocation[0].lat+'&lng='+currentlocation[0].lng+'&distance='+globalproxdistance+'&type=3';
			//THE FOLLOWING STATEMENT ADD THE CURRENT DESTINATION FLAG TO THE BOUNDS OF THE MAP
			var flagpoint = new GLatLng(currentlocation[0].lat,currentlocation[0].lng);
		 	bounds.extend(flagpoint);
		break;
		
		case 'sf3d':
			cpsearchtype = 'prox';
			var sf3dprox = 2;
			var datafile = '../controls/areasearchxml.aspx?lat='+yahcenter_lat+'&lng='+yahcenter_lng+'&distance='+sf3dprox+'&type=3';
		break;
		
		case 'passportsearch':
			cpsearchtype = 'tour';
			clearmap();
			if(passportsearchtype == 'destsearch'){
				var datafile = 'controls/PassportSearch.aspx?username='+userpassportsearch+'&param='+passportsearchterm+'&type=destsearch';
				activetourname = passportsearchterm;
			}else{
				var datafile = 'controls/PassportSearch.aspx?username='+userpassportsearch+'&lat='+currentlocation[0].lat+'&lng='+currentlocation[0].lng+'&distance='+passportsearchradius+'&type=areasearch&param='+passportsearchterm;
				activetourname = passportsearchterm;
			}
		break;
		
		case 'dmainvitesearch':
			cpsearchtype = 'tour';
			clearmap();
			if(passportsearchtype == 'destsearch'){
				var datafile = 'controls/PassportSearch.aspx?username='+userpassportsearch+'&param='+passportsearchterm+'&type=destsearch';
				activetourname = passportsearchterm;
			}else{
				var datafile = 'controls/PassportSearch.aspx?username='+userpassportsearch+'&lat='+currentlocation[0].lat+'&lng='+currentlocation[0].lng+'&distance='+passportsearchradius+'&type=areasearch&param='+passportsearchterm;
				activetourname = passportsearchterm;
			}
		break;
		
		case 'hubtour':
			cpsearchtype = 'tour';
			var datafile = 'controls/Destinationsearch.aspx?tourid='+tourid+'&orderby='+orderby+'&time='+currentitme+'&ispublic='+publiconly;
		break;
		
		case 'tripbuilder':
			cpsearchtype = 'tour';
			var datafile = 'controls/Destinationsearch.aspx?tourid='+tourid+'&orderby='+orderby+'&time='+currentitme+'&ispublic='+publiconly;
		break;
		
		case 'dma':
			cpsearchtype = 'tour';
			clearmap();
			var datafile = 'controls/Destinationsearch.aspx?tourid='+tourid+'&orderby='+orderby+'&time='+currentitme+'&ispublic='+publiconly;
		break;
		
		default:
			cpsearchtype = 'tour';
			clearmap();
			var datafile = 'controls/Destinationsearch.aspx?tourid='+tourid+'&orderby='+orderby+'&time='+currentitme+'&ispublic='+publiconly;
		break;
		
	 }
 
	//alert(datafile);
		 currentdestinations = [];
	 	 var destresults_html = '<div style="width:90%; height:auto;">';
		 i=0;

     	 request.open("GET", datafile, true);
     	  
     	 request.onreadystatechange = function() {
        	if (request.readyState == 4) {
			   if(request.status == 200){
        	
         		var xmlDoc = '';
         		 
         		xmlDoc = GXml.parse(request.responseText);
				
         		 // obtain the array of markers and loop through it
         		 var destinations = xmlDoc.documentElement.getElementsByTagName("destination");
				 destnumber = destinations.length;
				 
          		 if(destnumber>totalresults){
					 destnumber = totalresults;
				 }
				 //alert(destnumber);
				 if(destnumber>0){
          		
					for (var i = 0; i < destnumber; i++) {
						
						 // obtain the attribues of each marker
						var tourid = GXml.value(destinations[i].getElementsByTagName("TourID")[0]);
						var tourname = GXml.value(destinations[i].getElementsByTagName("TourName")[0]);
						var tourowner = GXml.value(destinations[i].getElementsByTagName("TourOwner")[0]);
						var tourdescription = GXml.value(destinations[i].getElementsByTagName("TourDescription")[0]);
						var destinationid = GXml.value(destinations[i].getElementsByTagName("DestinationID")[0]);
						var associated = GXml.value(destinations[i].getElementsByTagName("Associated")[0]);
						var associatedid = parseFloat(GXml.value(destinations[i].getElementsByTagName("AssociatedID")[0]));
						var dname = GXml.value(destinations[i].getElementsByTagName("DestinationName")[0]);
						var description = GXml.value(destinations[i].getElementsByTagName("DestinationDescription")[0]);
						var tags = GXml.value(destinations[i].getElementsByTagName("Tags")[0]);
						var public = GXml.value(destinations[i].getElementsByTagName("isPublic")[0]);
						var address1 = GXml.value(destinations[i].getElementsByTagName("Address1")[0]);
						var address2 = GXml.value(destinations[i].getElementsByTagName("Address2")[0]);
						var city = GXml.value(destinations[i].getElementsByTagName("City")[0]);
						var state = GXml.value(destinations[i].getElementsByTagName("State")[0]);
						var zip = GXml.value(destinations[i].getElementsByTagName("Zip")[0]);
						var country = GXml.value(destinations[i].getElementsByTagName("Country")[0]);
						var phone = GXml.value(destinations[i].getElementsByTagName("Phone")[0]);
						var fax = GXml.value(destinations[i].getElementsByTagName("Fax")[0]);
						var website = GXml.value(destinations[i].getElementsByTagName("Website")[0]);
						var email = GXml.value(destinations[i].getElementsByTagName("Email")[0]);
						var datecreated = GXml.value(destinations[i].getElementsByTagName("DateCreated")[0]);
						var destinationdate = GXml.value(destinations[i].getElementsByTagName("DestinationDate")[0]);
						var datemodified = GXml.value(destinations[i].getElementsByTagName("DateModified")[0]);
						var glat = parseFloat(GXml.value(destinations[i].getElementsByTagName("Latitude")[0]));
						var glng = parseFloat(GXml.value(destinations[i].getElementsByTagName("Longitude")[0]));
						var point = new GLatLng(glat,glng);
						var gzoom = parseFloat(GXml.value(destinations[i].getElementsByTagName("Zoom")[0]));
						var gmaptype = GXml.value(destinations[i].getElementsByTagName("MapType")[0]);
						var markericon = GXml.value(destinations[i].getElementsByTagName("Marker")[0]);
						var user = GXml.value(destinations[i].getElementsByTagName("User")[0]);
						var hasmedia = GXml.value(destinations[i].getElementsByTagName("HasMedia")[0]);
						var desttype = GXml.value(destinations[i].getElementsByTagName("Type")[0]);
						var ip = GXml.value(destinations[i].getElementsByTagName("Ip")[0]);
						var coupon = GXml.value(destinations[i].getElementsByTagName("Coupon")[0]);
						
						//alert(coupon);
						
						//alert(glat+', '+glng+', '+gzoom+', '+gmaptype+', '+markericon+', '+point);
						
						currentdestinations[i]= new popdestinations(tourid, tourname, tourowner, tourdescription, destinationid, associated, associatedid, dname, description, tags, public, address1, address2, city, state, zip, country, phone, fax, website, email, datecreated, destinationdate, datemodified, point, glat, glng, gzoom, gmaptype, markericon, user, hasmedia, desttype, ip, coupon);
						
						
						
						switch(wherefrom){
							case 'sethub':
								var destination = createdestination(i);
								destarray[0] = currentdestinations[0];
								hubbubble = destbubbles[0];
								getdestinations(hubtour, '', 'hubtour');
								return true;
							break;
							
							case 'sf3d':
								//destresults_html += '<a href="javascript:gotostorefront('+i+')">'+currentdestinations[i].name+'</a><br />';
								builddesthtmltab1(i, false, 'sf3d');
								destresults_html += destinfo;
								placebillboard(i);
							break;
							
							case 'tourwindow':
								destresults_html += buildtourlist(i);
							break;
							
							case 'ctheresample1':
								destresults_html += buildsamplelist(i);
							break;
							
							case 'tripbuilder':
								var destination = createdestination(i, wherefrom);
								destresults_html += destinfos[i];
							break;
							
							case 'dmainvitesearch':
								destresults_html += buildinvitelist(i);
							break;
							
							default:
								var destination = createdestination(i, wherefrom);
								map.addOverlay(destination);
								var usernameraw=currentdestinations[i].user;
								var username=usernameraw.toLowerCase();
								if(username==currentuser){
									destination.enableDragging();
								}
								
								if (currentdestinations[i].marker){
									//alert(markericons[i]);
									var dmarker = markerpath+currentdestinations[i].marker;
									destination.setImage(dmarker);
								}else{
									destination.setImage(markerimage);
								}
								
								destresults_html += destinfos[i];
								bounds.extend(point);
							break;
						}
						
						
					}
					
					destresults_html += '</div>';
					 
					
					switch(wherefrom){
						case 'prox_storefronts':
							displaycgmarquee(destnumber);
						break;
						
						case 'ctheresample1':
							displaysamplelist(destresults_html);
						break;
						
						case 'tripbuilder':
							//NO ACTION
						break;
						
						case 'sf3d':
							//NO ACTION
						break;
						
						case 'dmainvitesearch':
							setsearchresults(destresults_html);
						break;
						
						default:
							expandmappane();
							 // ===== determine the zoom level from the bounds =====
							 if(wherefrom=='hubtour'){
								 var hubpoint = new GLatLng(destarray[0].lat, destarray[0].lng);
								 bounds.extend(hubpoint);
							 }
							 
							 var newzoom = map.getBoundsZoomLevel(bounds)-1;
							 
							 if(newzoom > 17){
								 newzoom = 17;
							 }
							 
							 if(newzoom <= 3){
								newzoom = 3;
							 }
							 //alert(newzoom);
							 map.setZoom(newzoom);
			
							// ===== determine the centre from the bounds ======
							var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
							var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
							map.setCenter(new GLatLng(clat,clng));
						break;
					}
					
					//activetourid=tourid;
					//activetourname=tourname;
					
				  }else{
					
						destresults_html += 'No Cplanet destinations found.</div>';
						
						if(wherefrom == 'prox_storefronts'){
							displaycgmarquee('none');
						}
						
				  }
					
			   	}else{
					destresults_html += 'Unformated data file.</div>';
					
				}
				
				displayresults(i, wherefrom, destresults_html);
         	}
			
     	}
		
		
		
		request.send(null);
		
		
}


function popdestinations(tourid, tourname, tourowner, tourdescription, destinationid, associated, associatedid, name, description, desttags, public, address1, address2, city, state, zip, country, phone, fax, weblink, email, datecreated, datemanual, datemodified, point, lat, lng, zoom, maptype, marker, user, hasmedia, desttype, ip, coupon){
	
	this.tourid=tourid;
	this.tourname=tourname;
	this.tourowner=tourowner;
	this.tourdescription=tourdescription;
	this.destinationid=destinationid;
	this.associated=associated;
	this.associatedid=associatedid;
	this.name=name;
	this.description=description;
	this.desttags=desttags;
	this.public=public;
	this.address1=address1;
	this.address2=address2;
	this.city=city;
	this.state=state;
	this.zip=zip;
	this.country=country;
	this.phone=phone;
	this.fax=fax;
	this.weblink=weblink;
	this.email=email;
	this.datecreated=datecreated;
	this.datemanual=datemanual;
	this.datemodified=datemodified;
	this.point = point;
	this.lat = lat;
	this.lng = lng;
	this.zoom= zoom;
	this.maptype=maptype;
	this.marker=marker;
	this.user=user;
	this.hasmedia=hasmedia;
	this.desttype=desttype;
	this.ip=ip;
	this.coupon=coupon;
	
}

//THIS FUNCTION POPULATES THE MAP WITH MARKERS FROM AN INTERNET SEARCH DONE FROM THE PRESENTATION PAGE.
function createdestination(i, wherefrom) {
		
		switch(wherefrom){
			case 'tripbuilder':
				var markonmap = false;
			break;
			
			default:
				var markonmap = true;
			break;
		}
		
		var htmltab2="";
		var label1=' Destination Information';
		var label2='Details';
		var label3='Search area';
		
	    var point = new GLatLng(currentdestinations[i].lat,currentdestinations[i].lng);
        var cpdestination = new GMarker(point, {icon:dynamicmarker, draggable: true, zIndexProcess:importanceOrder});
		cpdestination.importance = 1;
		var gatheraddress=builddesthtmltab1(i, markonmap, wherefrom);
		var desttab2 = builddesthtmltab2(i);
		
		var cleanaddress=cleansearchstring(gatheraddress);
		
		var locsearchlayout='<div class="bubble" runat="server">'
		+'<span style="font-weight:bold;">Search for something close by:</span><br />'
		+'<span class="minifont">Business Name or Business Catagory</span><br />'
		+'<input type="text" id="txtBusinessSearchBubble" name="textfield" size="25" onfocus="setenterkey(\'txtBusinessSearchBubble\')" /><br />'
		+'<span class="minifont">City, State, Zip</span><br />'
		+'<input type="text" id="txtBusinessSearchLocationBubble" name="textfield" size="25" value="'+cleanaddress+'"/><br />'
		+'<input id="btnBusinessSearchBubble" type="button" value="Search" onclick="showLocation(\'business\', \'bubble\')" />'
		+'<div style="padding-top:3px; margin-top:3px; border-top:solid 1px #666666;"><a href="javascript:connecttoone(\''+i+'\')">Connect to this</a>  |  <a href="javascript:connectall()">Connect all</a></div></div>';
		
        // save the info we need to use later for the side_bar
        cpdestinations[i] = cpdestination;
        destinfos[i] = destinfo;
		destbubbles[i] = destbubble;
		destbubbles2[i] = desttab2;
		gatheraddresses[i] = cleanaddress;
		destboxes[i] = destbox;
		        
		GEvent.addListener(cpdestination, "click", function() {
			activeresult=i;
			activeresulttype = 'cplanet';
			activemarker=cpdestination;
			//cpdestination.openInfoWindowTabsHtml([new GInfoWindowTab(label1,destbubbles[i]), new GInfoWindowTab(label2,desttab2), new GInfoWindowTab(label3,locsearchlayout)]);
			showdestination(i, true);
        });

        return cpdestination;
		
}

function importanceOrder (marker,b) {
	return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}
	  
	  
function showdestination(i, frommarkclick) {
	
		expandmappane();
		map.closeInfoWindow();
		
		activemarker=cpdestinations[i];
		activeresult=i;
		activeresulttype = 'cplanet';
		
		if(allowcustombubbles == true){
			if(!frommarkclick){
				displaymapmarker(i, 8);
			}
			setTimeout("openmarkerinfowindow("+i+", 'custom')",400);
		}else{
			displaymapmarker(i, gcustomzoom);
			setTimeout("openmarkerinfowindow("+i+", 'standard')",400);
		}
		
}

function plottourdest(i){
	expandmappane();
	activemarker=currentdestinations[i];
	activeresult=i;
	activeresulttype = 'cplanet';
	var point = currentdestinations[i].point;
	var tourdesst = new GMarker(point, {icon:dynamicmarker, draggable: true, zIndexProcess:importanceOrder});
	map.addOverlay(tourdesst);
	map.setCenter(point);
	
	var label1='Info';
	var label2='Details';
	tourdesst.openInfoWindowTabsHtml([new GInfoWindowTab(label1,destbubbles[i]), new GInfoWindowTab(label2,destbubbles2[i])]);
	
}

function displaymapmarker(i, customzoom){
	
	var point = new GLatLng(currentdestinations[i].lat , currentdestinations[i].lng);

	map.setCenter(point);
	//setTimeout("offsetmarker()",200);
	
	if(customzoom){
		if(customzoom!='skip'){
			map.setZoom(customzoom);
		}
	}else{
//		if(currentdestinations[i].zoom!=''){
//			map.setZoom(currentdestinations[i].zoom);
//		}else{
//			map.setZoom(12);
//		}
	}
	
	if (currentdestinations[i].maptype!=''){
		
		switch (currentdestinations[i].maptype){
			case 'Map':
				map.setMapType(G_NORMAL_MAP);
			break;
		
			case 'Hybrid':
				//alert('setting map type');
				map.setMapType(G_HYBRID_MAP);
			break;
		
			case 'Satellite':
				map.setMapType(G_SATELLITE_MAP);
			break;
		}		
	}
	
}

function offsetmarker(){
	//var xshift = (windowwidth * -0.35);
	//var yshift = (windowheight * 0.2);
	
	var xshift = -150;
	var yshift = 150;
	
	var markershift = new GSize(xshift,yshift);
	map.panBy(markershift);
}

function openmarkerinfowindow(i, type){
	
	switch (type){
		case 'standard':
			var label1='Info';
			var label2='Details';
			var label3='Search area';
			
			var locsearchlayout='<div class="bubble" runat="server">'
			+'<span style="font-weight:bold;">Search for something close by:</span><br />'
			+'<span class="minifont">Business Name or Business Catagory</span><br />'
			+'<input type="text" id="txtBusinessSearchBubble" name="textfield" size="25" onfocus="setenterkey(\'txtBusinessSearchBubble\')" /><br />'
			+'<span class="minifont">City, State, Zip</span><br />'
			+'<input type="text" id="txtBusinessSearchLocationBubble" name="textfield" size="25" value="'+gatheraddresses[i]+'"/><br />'
			+'<input id="btnBusinessSearchBubble" type="button" value="Search" onclick="showLocation(\'business\', \'bubble\')" />'
			+'<div style="padding-top:3px; margin-top:3px; border-top:solid 1px #666666;"><a href="javascript:connecttoone(\''+i+'\')">Connect to this</a>  |  <a href="javascript:connectall()">Connect all</a></div></div>';
			
			//cpdestinations[i].openInfoWindowTabsHtml([new GInfoWindowTab(label1,destbubbles[i]), new GInfoWindowTab(label2,destbubbles2[i]), new GInfoWindowTab(label3,locsearchlayout)]);
			cpdestinations[i].openInfoWindowHtml(destbubbles[i]);
			
		break;
		
		case 'custom':
			var custombubblehtml = '';
		
			if(currentdestinations[i].description != ''){
				if(currentdestinations[i].email != ''){
					var conxstr = currentdestinations[i].email;
					//alert(conxstr);
					var hasflag = conxstr.indexOf('createhubtour');
					if(hasflag != -1){
						var strsegs = conxstr.split('~');
				//ONCE STEW RAISES THE LIMIT TO THE EMAIL FIELD CHECK FOR THE MORE INFO AND LINK.
						//alert('# of string segments'+strsegs.length+'\n#1: '+strsegs[1]+'\n#2: '+strsegs[2]+'\n#3: '+strsegs[3]+'\n#4: '+strsegs[4]+'\n#5: '+strsegs[5]);
						if(strsegs.length == 5){
							custombubblehtml += '<div style="width:525px; height:25px;"><table width="100%" cellspacing="0" cellpadding="0" border="0"><tr>'
												+'<td align="left"><a href="javascript:createhubtour(\''+currentdestinations[i].destinationid+'\', \''+strsegs[1]+'\')">'+strsegs[2]+'</a></td>'
												+'<td align="right"><a href="javascript:framewebsite(\''+strsegs[4]+'\')">'+strsegs[3]+'</a></td></tr></table></div>';
						}
					}
				}
				
				custombubblehtml += '<div style="width:525px; height:250px; overflow:auto;">'+currentdestinations[i].description+'</div>';
				cpdestinations[i].openInfoWindowHtml(custombubblehtml);
			}else{
				openmarkerinfowindow(i, 'standard');
			}
		break;
		
	}
		
		
}

function buildsamplelist(i){
	var samplelisthtml = '<a href="http://www.cplanet.com/destwindow.aspx?destid='+currentdestinations[i].destinationid+'" target="_blank">'
						+'<img src="../globalgraphics/markers/'+currentdestinations[i].marker+'" border="0" /><br />'
						+currentdestinations[i].name+'</a><hr>';
						
	return samplelisthtml;
}

function addtomarquee(i){
	//currentdestinations[i].name
	//http://www.cplanet.com/destwindow.aspx?destid='+currentdestinations[i].destinationid
	//cgmarquee += ' <a href="http://www.cplanet.com/destwindow.aspx?destid='+currentdestinations[i].destinationid+'" target="_blank"><img src="../globalgraphics/general/storefrontSample.jpg" border="0" /></a>';
	
	//cgmarquee += '<div class="marqueeitem" style="height:100px;"><a href="javascript:showdestination(' + i + ')">'
	//			+currentdestinations[i].name
	//			+'<img src="../globalgraphics/markers/'+currentdestinations[i].marker+'" border="0" /></a></div>';
				
	cgmarquee += '<a href="javascript:showdestination(' + i + ')" class="marqueeitem">'
				+currentdestinations[i].name
				+'<img src="../globalgraphics/markers/'+currentdestinations[i].marker+'" border="0" /></a>';
	
	
}

//THIS FUNCTIONS TAKES THE VARIABLES OF EACH SEARCH RESULTS AND FORMATS THE HTML THAT IS DISPLAYED IN THE BUBBLES AND RESULTS DIVS
function builddesthtmltab1(i, markonmap, wherefrom){
	var gatheraddress='';
	destinfo='';
	destbubble='<div class="bubble">';
	destbox='';
	
	switch(wherefrom){
		case 'tourwindow':
			var showdestfunction = 'showdestintour';
		break;
		
		case 'tripbuilder':
			var showdestfunction = 'plottourdest';
		break;
	
		case 'sf3d':
			var showdestfunction = 'gotostorefront';
		break;
		
		default:
			var showdestfunction = 'showdestination';
		break;
	}
	
	if (currentdestinations[i].name!=""){
		destinfo+='<b><a href="javascript:'+showdestfunction+'(' + i + ')">' + currentdestinations[i].name + '</a></b>';
		destbubble+='<strong>'+currentdestinations[i].name+'</strong>';
		destbox+=currentdestinations[i].name;
	}
	
	if (currentdestinations[i].address1!=""){
		destinfo+='<br>'+currentdestinations[i].address1;
		destbubble+='<br>'+currentdestinations[i].address1;
		destbox+='\n'+currentdestinations[i].address1;
		gatheraddress+=currentdestinations[i].address1;
	}
	if (currentdestinations[i].address2!=""){
		destinfo+='<br>'+currentdestinations[i].address2;
		destbubble+='<br>'+currentdestinations[i].address2;
		destbox+='\n'+currentdestinations[i].address2;
	};
	if (currentdestinations[i].city!=""){
		destinfo+='<br>'+currentdestinations[i].city;
		destbubble+='<br>'+currentdestinations[i].city;
		destbox+='\n'+currentdestinations[i].city;
		gatheraddress+=' '+currentdestinations[i].city;
	}
	if (currentdestinations[i].state!=""){
		destinfo+=', '+currentdestinations[i].state;
		destbubble+=', '+currentdestinations[i].state;
		destbox+=', '+currentdestinations[i].state;
		gatheraddress+=' '+currentdestinations[i].state;
	}
	if (currentdestinations[i].zip!=""){
		destinfo+=', '+currentdestinations[i].zip;
		destbubble+=', '+currentdestinations[i].zip;
		destbox+=', '+currentdestinations[i].zip;
	}
	if (currentdestinations[i].country!=""){
		destinfo+=', '+currentdestinations[i].country;
		destbubble+=', '+currentdestinations[i].country;
		destbox+=', '+currentdestinations[i].country;
	}
	if (currentdestinations[i].phone!=""){
		destinfo+='<br>'+currentdestinations[i].phone;
		destbubble+='<br>'+currentdestinations[i].phone;
		destbox+='\n'+currentdestinations[i].phone;
	}
	
//	if (currentdestinations[i].description!=""){
//		var trucdescript=currentdestinations[i].description.slice(0,26 )+'...';
//		destinfo+='<br>'+trucdescript;
//		destbox+='\n'+trucdescript;
//	}
	
	if (currentdestinations[i].weblink!=""){
		if (currentdestinations[i].weblink.search("http://") == -1) {
			destinfo+='<br><a href="http://'+currentdestinations[i].weblink+'" target="_blank"><span class="minifont">Website</span></a>';
			destbubble+='<br><a href="http://'+currentdestinations[i].weblink+'" target="_blank">Website</a>';
			//destbox+='\nhttp://'+currentdestinations[i].weblink;
		}else{
			destinfo+='<br><a href="'+currentdestinations[i].weblink+'" target="_blank"><span class="minifont">Website</span></a>';
			destbubble+='<br><a href="'+currentdestinations[i].weblink+'" target="_blank">Website</a>';
			//destbox+='\n'+currentdestinations[i].weblink;
		}
	}
	
	if (currentdestinations[i].coupon!=""){
		//var printtrigger = 'alt="<a href=\"javascript:printimage(\''+currentdestinations[i].coupon+'\')\">Click here to print</a>"';
		
		var sendtoprintpath = cleanstring(currentdestinations[i].coupon);
		
		destinfo+='<br><table border="0"><tr>'+
			'<td align="center"><a href="'+currentdestinations[i].coupon+'" class="highslide" onclick="return hs.expand(this)">'+
			'<img src="'+gcouponimage+'" title="Click here to open coupon" border="0" /></a>'+
			'<div class="highslide-caption"><a href="javascript:printimage(\''+sendtoprintpath+'\')"><span class="cpfontheading">Click here to print</span></a></div></td>'+
			'<td align="center"><div class="cpfontbold" align="center"><span style="font-size:9px; font-weight:normal;">sponsored by:</span><br/>'+currentdestinations[i].user+'</div></td></tr></table>';
			
		destbubble+='<br><table border="0"><tr>'+
			'<td align="center"><a href="'+currentdestinations[i].coupon+'" class="highslide" onclick="return hs.expand(this)">'+
			'<img src="'+gcouponimage+'" title="Click here to open coupon" border="0" /></a>'+
			'<div class="highslide-caption"><a href="javascript:printimage(\''+sendtoprintpath+'\')"><span class="cpfontheading">Click here to print</span></a></div></td>'+
			'<td align="center"><div class="cpfontbold" align="center"><span style="font-size:9px; font-weight:normal;">sponsored by:</span><br/>'+currentdestinations[i].user+'</div></td></tr></table>';
			
		//destinfo+='<br><table width="100%" ><tr><td align="right"><a href="javascript:opencoupon(\''+currentdestinations[i].coupon+'\')"><img src="/globalgraphics/general/coupon.gif" alt="Click to Open Coupon." onmouseover="this.style.cursor=\'pointer\'" border="0" /></a></td></tr></table>';
		//destbubble+='<br><table width="100%" ><tr><td align="left"><a href="javascript:opencoupon(\''+currentdestinations[i].coupon+'\')"><img src="/globalgraphics/general/coupon.gif" alt="Click to Open Coupon." onmouseover="this.style.cursor=\'pointer\'" border="0" /></a></td></tr></table>';
	}
	
	
	var cleanaddress=cleansearchstring(gatheraddress);
	
	
	//THIS SWITCH IS HERE FOR FUTURE USE OF CHANGING THE EDIT OPTIONS INSIDE RESULTS LISTING AND BUBBLE TEXT.
	switch(wherefrom){
		
		case 'sf3d':
			//NO ACTION
		break;
		
		default:
			builddefaultoptions(i, markonmap, cleanaddress);
		break;
	}
	
	destinfo+='<hr>';
	destbubble+='</div>';
	
	return gatheraddress;
}

function builddefaultoptions(i, markonmap, cleanaddress){
	
	destinfo+='<table width="100%" style="padding-top:5px;"><tr>';
	destbubble+='<table width="100%"><tr>';
	
	var cleandestname=cleanstring(currentdestinations[i].name);

	var usernameraw=currentdestinations[i].user;
	var username=usernameraw.toLowerCase();
	//alert('Raw='+usernameraw+', Lowercase='+username);
	if(username==currentuser){
		destinfo+='<td bgcolor="#CCCCCC" style="border:solid 1px #666666;" align="left"><a href="javascript:optionsmenu('+i+')"><span class="minifont">Options Menu</span></a></td>';
	}
	
	
	if (currentdestinations[i].hasmedia=='True'){
		
		destinfo+='<td align="right"><a href="http://www.cplanet.com/destwindow.aspx?destid='+currentdestinations[i].destinationid+'" target="_blank"><span class="minifont"><img src="/globalgraphics/general/ctherebutton_small.gif" alt="Cthere - open a window to this destination." onmouseover="this.style.cursor=\'pointer\'" border="0" /></span></a></td>';
		//destbubble+='<br><a href="'+currentdestinations[i].weblink+'" onclick="return hs.htmlExpand(this, { objectType: \'iframe\', contentId:\'iframecontent\', preserveContent: false } )">Website</a>';
		destbubble+='<td align="center" style="padding:3px;"><a href="http://www.cplanet.com/destwindow.aspx?destid='+currentdestinations[i].destinationid+'" target="_blank"><img src="/globalgraphics/general/ctherebutton.gif" alt="Cthere - open a window to this destination." onmouseover="this.style.cursor=\'pointer\'" border="0" /></a></td>';
		//destbox+='\n'+currentdestinations[i].weblink;
		
		addtomarquee(i);
	}else{
		if (currentdestinations[i].desttype=='5'){
			destinfo+='<td align="right"><a href="http://www.cplanet.com/storefront_lite.aspx?destid='+currentdestinations[i].destinationid+'&destname='+currentdestinations[i].name+'" target="_blank"><span class="minifont"><img src="/globalgraphics/general/cthere_lite.gif" alt="Cthere - open a window to this destination." onmouseover="this.style.cursor=\'pointer\'" border="0" /></span></a></td>';
			//destbubble+='<br><a href="'+currentdestinations[i].weblink+'" onclick="return hs.htmlExpand(this, { objectType: \'iframe\', contentId:\'iframecontent\', preserveContent: false } )">Website</a>';
			destbubble+='<td align="center" style="padding:3px;"><a href="http://www.cplanet.com/storefront_lite.aspx?destid='+currentdestinations[i].destinationid+'&destname='+currentdestinations[i].name+'" target="_blank"><img src="/globalgraphics/general/cthere_lite.gif" alt="Cthere - open a window to this destination." onmouseover="this.style.cursor=\'pointer\'" border="0" /></a></td>';
		}
			
	}
	
	destinfo+='</tr></table>';
	destbubble+='</tr></table>';
	
	//destinfo+='<br>';
//	if(markonmap==true){
		destbubble+='<span style="font-size:11px;"><table width="100%" border="0" cellpadding="3"><tr>';
		
//		destbubble+='<tr><td width="50%" align="left" style="border-right:solid 1px #666666;">';
//		destbubble+='<img src="globalgraphics/general/loc_marker_mini_icon.jpg" />';
//		destbubble+='<a href="javascript:setsearchcenterpoint('+currentdestinations[i].lat+','+currentdestinations[i].lng+')">Set Explore Center</a></td>';
		
//		if ((currentdestinations[i].city!="")&&(currentdestinations[i].state!="")){
//			destbubble+='<td align="left" width="50%" style="border-right:solid 1px #666666;"><a href="javascript:callgeocode(\''+cleanaddress+'\')">Search &amp; Explore</a></td>';
//		}else{
//			destbubble+='<td align="left" width="50%" style="border-right:solid 1px #666666;">Search &amp; Explore</td>';
//		}
		
//		if(username!=currentuser){
//			if(currentdestinations[i].associated==1){
//				destbubble+='<td align="right"><a href="javascript:unassociatecpdestination()">Remove</a></td></tr>';
//			}else{
//				destbubble+='<td align="right"><a href="javascript:savecplanetdestination()">Add to My Passport</a></td></tr>';
//			}
//		}else{
//			destbubble+='<td align="right"><a href="javascript:verifiedaddpassport(\'edit\')">Edit</a></td></tr>';
//		}
		
		
		destbubble+='<tr><td align="left" style="border-right:solid 1px #666666;"><a href="javascript:buildshare(\'dest\')">Share destination</a></td>';
		destbubble+='<td align="right"><a href="javascript:prepforsend(\''+i+'\', \'cpdest\')">Send to me</a></td>';
		destbubble+='</tr></table></span>';
		
		var directioinpoint=currentdestinations[i].lat+','+currentdestinations[i].lng;
		var name = cleanstring(currentdestinations[i].name);
		destbubble += '<hr><div align="center"><span class="cpfontreg">Directions:<a href="javascript:setdirectionbubble(\'to\', \''+directioinpoint+'\', \''+name+'\' )">To Here  </a>- or -<a href="javascript:setdirectionbubble(\'from\', \''+directioinpoint+'\', \''+name+'\')">  From Here</a></span></div>';
//	}	
	
}


function builddesthtmltab2(i){
	
	var desttab2='<div class="bubble">';
	
	var directioinpoint=currentdestinations[i].lat+','+currentdestinations[i].lng;
	var name = cleanstring(currentdestinations[i].name);
	var directionstab ='<div><span class="titlefont">Directions:<a href="javascript:setdirectionbubble(\'to\', \''+directioinpoint+'\', \''+name+'\' )">To Here  </a>- or -<a href="javascript:setdirectionbubble(\'from\', \''+directioinpoint+'\', \''+name+'\')">  From Here</a></span></div><hr>';
	
	desttab2+=directionstab;
	desttab2+='<div style="height:120px; overflow:auto;">'
	
	if (currentdestinations[i].name!=""){
		desttab2+='<br><b>Name: </b>'+currentdestinations[i].name;
	}
	
	if (currentdestinations[i].description!=""){
		desttab2+='<br><b>Description: </b>'+currentdestinations[i].description;
	}
	
	if (currentdestinations[i].datemanual!=""){
		desttab2+='<br><b>Date: </b>'+currentdestinations[i].datemanual;
	}
	
	if (currentdestinations[i].email!=""){
		desttab2+='<br><a href="mailto:'+currentdestinations[i].email+'">Contact Email</a>';
	}
	
	if (currentdestinations[i].datecreated!=""){
		desttab2+='<hr><span class="minifont">Date Created: '+currentdestinations[i].datecreated+'</span>';
	}
	
	if (currentdestinations[i].datemodified!=""){
		desttab2+='<br><span class="minifont">Last Modified: '+currentdestinations[i].datemodified+'</span>';
	}
	
	if (currentdestinations[i].user!=""){
		desttab2+='<br><span class="minifont">Created by: '+currentdestinations[i].user+'</span>';
	}
	
	if (currentdestinations[i].desttype!=""){
		desttab2+='<br><span class="minifont">Destination Type: '+currentdestinations[i].desttype+'</span>';
	}
	
	if (currentdestinations[i].destinationid!=""){
		desttab2+='<br><span style="font-size:8px;">destid='+currentdestinations[i].destinationid+'</span>';
	}
	
	desttab2+='</div></div>';
	
	return desttab2;
	
}


function searchyahoo(searchtype, nav, whatsearch, biz){
	
	switch (searchtype){
			case 'venue':
				if (nav == 'next'){
				   clearmap();
				   querycount=querycount+1;
				}
			
				if (nav == 'initial'){
					resultcount = 0;
					querycount=1;
					clearmap();
				}
				
				if (nav == 'prev'){
					if(resultcount>40){
						resultcount = Number(resultcount) - 40;
					}else{
						resultcount = 0;
					}
					querycount=querycount-1;
					clearmap();
				}
				
				var searchstring_yahoo = 'controls/localsearch.aspx?bus='+currentquery+'&add='+search_locstring+'&cat='+currentvenue+'&rad='+searchradius+'&start='+resultcount;
				getbizsearch(searchstring_yahoo, searchtype, true);
			break;
		
			case 'prox':
				var business='*';
				var radius='0.5';
				var venueid='*';
				markerimage = markerstandard;
				
				if (nav == 'next')
				{	
					resultcount = Number(resultcount) + 20;
					querycount=querycount+1;
					clearmap();
					var searchstring_yahoo = proxsearchstring+resultcount;
					//restorelocation();
				}
				
				if (nav == 'initial')
				{
					resultcount = 0;
					querycount=1;
					
					if (activeresulttype == 'yahoo'){
						bizlocation=currentbusinesses[biz].address1+' '+currentbusinesses[biz].city+' '+currentbusinesses[biz].state;
						if(nav=='initial'){
							proxlocation=bizlocation;
							proxlocation_name=currentbusinesses[biz].name;
							proxlocation_lat=currentbusinesses[biz].lat;
							proxlocation_lng=currentbusinesses[biz].lng;
						}
					}
					if (activeresulttype == 'cplanet'){
						bizlocation=currentdestinations[biz].address1+' '+currentdestinations[biz].city+' '+currentdestinations[biz].state;
						if(nav=='initial'){
							proxlocation=bizlocation;
							proxlocation_name=currentdestinations[biz].name;
							proxlocation_lat=currentdestinations[biz].lat;
							proxlocation_lng=currentdestinations[biz].lng;
						}
					}
					
					var searchstring_yahoo = 'controls/localsearch.aspx?bus='+business+'&add='+bizlocation+'&cat='+venueid+'&rad='+radius+'&start='+resultcount;
					proxsearchstring = 'controls/localsearch.aspx?bus='+business+'&add='+bizlocation+'&cat='+venueid+'&rad='+radius+'&start=';
					
				}
				
				if (nav == 'prev')
				{
					if(resultcount>40){
						resultcount = Number(resultcount) - 40;
					}else{
						resultcount = 0;
					}
					querycount=querycount-1;
					clearmap();
					var searchstring_yahoo = proxsearchstring+resultcount;
					//restorelocation();
				}

				getbizsearch(searchstring_yahoo, searchtype, true);

		break;
		
			case 'search':
			
				switch (whatsearch){
					case 'sponsor':	
						currentbizsearchterm = sponsorsearchterm;
						markerimage =  markerpath+sponsormarker;
					break;
					
					case 'comguide':	
						currentbizsearchterm = comguidesearchterm;
						//markerimage =  markerpath+dmomarker;
					break;
					
					default:	
						if(dmoflag == true){
							markerimage =  markerpath+dmomarker;
						}else{
							markerimage = markerstandard;
							//globallocation=mod_searchstring;
						}
					break;
					
				}
				
				if (nav == 'next')
				{
					resultcount = Number(resultcount) + 20;
					querycount=querycount+1;
					clearmap();
					//restorelocation();
				}
				
				if (nav == 'initial')
				{
					resultcount = 0;
					querycount=1;
					clearmap();
				}
				
				if (nav == 'prev')
				{
					if(resultcount>40){
						resultcount = Number(resultcount) - 40;
					}else{
						resultcount = 0;
					}
					querycount=querycount-1;
					clearmap();
					//restorelocation();
				}
				
				activebizheader = currentbizsearchterm;
				
				//alert('currentsearchterm: '+currentbizsearchterm+'\nGlobalLocation: '+globallocation);
				var searchstring_yahoo = 'controls/localsearch.aspx?bus='+currentbizsearchterm+'&add='+search_locstring+'&cat=*&rad=10&start='+resultcount;
				
				//alert(searchstring_yahoo);
			
				switch (whatsearch){
					case 'create':
						getbizsearch(searchstring_yahoo, searchtype, false);
					break;
					
					case 'storefront':
						getbizsearch(searchstring_yahoo, searchtype, false);
					break;
					
					case 'sponsor':
						getbizsearch(searchstring_yahoo, 'sponsor', true);
					break;
					
					default:
						getbizsearch(searchstring_yahoo, searchtype, true);
					break;	
				}
			break;
		}
		
}




function resultsnav(searchtype, markonmap, resultstatus){
	
	//alert(resultcount);
	if(((resultcount-prevcount)<20)&&(querycount>1)){
		resultstatus='done';	
	}
	
	if(searchtype=='venue'){
		var navfunction_next = 'javascript:searchyahoo(\'venue\', \'next\')';
		var navfunction_prev = 'javascript:searchyahoo(\'venue\', \'prev\')';
	}
	
	if(searchtype=='prox'){
	   var navfunction_next = 'javascript:searchyahoo(\'prox\', \'next\')';
	   var navfunction_prev = 'javascript:searchyahoo(\'prox\', \'prev\')';
	}
	
	if(searchtype=='search'){
	   var navfunction_next = 'javascript:searchyahoo(\'search\', \'next\')';
	   var navfunction_prev = 'javascript:searchyahoo(\'search\', \'prev\')';
	}
	
	if(searchtype=='sponsor'){
	   var navfunction_next = 'javascript:searchyahoo(\'search\', \'next\', \'sponsor\')';
	   var navfunction_prev = 'javascript:searchyahoo(\'search\', \'prev\', \'sponsor\')';
	}
	
	//alert(resultcount);
	if ((resultcount<20)&&(resultstatus!='done')){
		
		var navstring = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
					  '<tr><td width="30%"><div id="resultsnext" align="left"></div></td>'+
					  '<td width="30%" valign="middle"></td>'+
					  '<td width="30%"><div id="resultsnext" align="right"></div></td>'+
					  '</tr></table>';
		
		if(destnumber<5){
			var navstring = '';
			//alert('destnumber is less than 5');
		}
	}
	
	if ((resultcount==20)&&(resultstatus!='done')){
		var navstring = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
					  '<tr><td width="30%"><div id="resultsnext" align="left"></div></td>'+
					  '<td width="30%" valign="middle"></td>'+
					  '<td width="30%"><div id="resultsnext" align="right"><a href="'+navfunction_next+'"><img src="/globalgraphics/general/next.gif" alt="Next 20 destinations." width="50" height="20" border="0" /></a></div></td>'+
					  '</tr></table>';
					  
	}
	
	if ((resultcount>20)&&(resultstatus!='done')){
		var navstring = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
					  '<tr><td width="30%"><div id="resultsnext" align="left"><a href="'+navfunction_prev+'"><img src="/globalgraphics/general/previous.gif" alt="Previous 20 destinations." width="50" height="20" border="0" /></a></div></td>'+
					  '<td width="30%" valign="middle"></td>'+
					  '<td width="30%"><div id="resultsnext" align="right"><a href="'+navfunction_next+'"><img src="/globalgraphics/general/next.gif" alt="Next 20 destinations." width="50" height="20" border="0" /></a></div></td>'+
					  '</tr></table>';
					  
	}
	
	if (resultstatus=='done'){
		var navstring = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
					  '<tr><td width="30%"><div id="resultsnext" align="left"><a href="'+navfunction_prev+'"><img src="/globalgraphics/general/previous.gif" alt="Previous 20 destinations." width="50" height="20" border="0" /></a></div></td>'+
					  '<td width="30%" valign="middle"></td>'+
					  '<td width="30%"><div id="resultsnext" align="right"></div></td>'+
					  '</tr></table>';
					  
	}
	
	if (markonmap==true){
		biznavresultslocation.innerHTML = navstring;
	}
	
}

//THE FOLLOWING 4 FUNCTIONS PROCESSES THE RESULTS FROM A YAHOO LOCAL SEARCH QUERY ***************************************
function getbizsearch(datafile, searchtype, markonmap){
	
	//alert(datafile);
	var bizwherefrom = 'bizsearch'
	bizresults_html='<div style="width:90%; height:auto;">';
	
	bizresultslocation.innerHTML = loadinganimation;
	
	   var bounds = new GLatLngBounds();
	   outersearchradius = 0;
	   outersearchpoint;
     	 var request = GXmlHttp.create();
     	 request.open("GET", datafile, true);
     	 request.onreadystatechange = function() {
        	if (request.readyState == 4) {
				 if(request.status == 200){
					var xmlDoc = GXml.parse(request.responseText);
         		 // obtain the array of markers and loop through it
         			var bs_results = xmlDoc.documentElement.getElementsByTagName("destination");
					
					prevcount=resultcount;
					resultcount=(Number(resultcount)+(bs_results.length));
					resultsnav(searchtype, markonmap);
					
					if(bs_results.length>0){
						if(markonmap){
						   expandmappane();
						}
					}
					
					var sp = 0;
					for (var x = 0; x < bs_results.length; x++) {
						 // obtain the attribues of each marker
						 	
							var bs_tourid = GXml.value(bs_results[x].getElementsByTagName("TourID")[0]);
							var bs_destinationid = GXml.value(bs_results[x].getElementsByTagName("DestinationID")[0]);
							var bs_dname = GXml.value(bs_results[x].getElementsByTagName("DestinationName")[0]);
							var bs_description = GXml.value(bs_results[x].getElementsByTagName("DestinationDescription")[0]);
							var bs_glat = parseFloat(GXml.value(bs_results[x].getElementsByTagName("Latitude")[0]));
							var bs_glng = parseFloat(GXml.value(bs_results[x].getElementsByTagName("Longitude")[0]));
							var bs_point = new GLatLng(bs_glat,bs_glng);
							var bs_gzoom = parseFloat(GXml.value(bs_results[x].getElementsByTagName("zoom")[0]));
							var bs_venue = GXml.value(bs_results[x].getElementsByTagName("Venue")[0]);
							var bs_date = GXml.value(bs_results[x].getElementsByTagName("TheDate")[0]);
							var bs_website = GXml.value(bs_results[x].getElementsByTagName("Website")[0]);
							var bs_logo = GXml.value(bs_results[x].getElementsByTagName("Logo")[0]);
							var bs_address = GXml.value(bs_results[x].getElementsByTagName("Address1")[0]);
							var bs_address2 = GXml.value(bs_results[x].getElementsByTagName("Address2")[0]);
							var bs_city = GXml.value(bs_results[x].getElementsByTagName("City")[0]);
							var bs_state = GXml.value(bs_results[x].getElementsByTagName("State")[0]);
							var bs_zip = GXml.value(bs_results[x].getElementsByTagName("Zip")[0]);
							var bs_region = GXml.value(bs_results[x].getElementsByTagName("Region")[0]);
							var bs_country = GXml.value(bs_results[x].getElementsByTagName("Country")[0]);
							var bs_phone = GXml.value(bs_results[x].getElementsByTagName("Phone")[0]);
							
							if((sponsoractive == true)&&(searchtype == 'sponsor')){
								var validsponsor = sponsorfilter(bs_dname);
								if(validsponsor == true){
									currentbusinesses[sp]= new popbusinesses(bs_dname, bs_description, bs_address, bs_address2, bs_city, bs_state, bs_zip, bs_country, bs_phone, bs_website, bs_glat, bs_glng, bs_point, bs_gzoom);
									
									bizresults_html += handlebizresults(sp, markonmap);
									bounds.extend(currentbusinesses[sp].point);
									sp++;
									
								}
							
							}else{
								currentbusinesses[x]= new popbusinesses(bs_dname, bs_description, bs_address, bs_address2, bs_city, bs_state, bs_zip, bs_country, bs_phone, bs_website, bs_glat, bs_glng, bs_point, bs_gzoom);
								bizresults_html += handlebizresults(x, markonmap);
								bounds.extend(currentbusinesses[x].point);
							}
							
							
					}
					
					bizresults_html += '</div>';
					
				}else{
					if(cyosf_active == true){
						bizwherefrom = 'storefront';
						bizresults_html='<div align="center" class="cpfontheading">No destinations found.<hr>'
								+'Click below to locate your business by address.<br>'
								+'<input id="btnLocateSF_FindAddress2" type="button" value="Locate by address"'
								+'onclick="locatebizaddress()" /></div>';
					}else{
						bizresults_html='No destinations found.';
					}

					if(querycount>1){
						resultsnav(searchtype, markonmap, 'done');
					}
					
				}
				
				if(cyosf_active == true){
					bizwherefrom = 'storefront';
				}
				
				if (markonmap==true){
					if(x>0){
						if(x>2){
							var searchzoom = map.getBoundsZoomLevel(bounds) - 1;
							map.setZoom(searchzoom);
		
							// ===== determine the centre from the bounds ======
							var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
							var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
							map.setCenter(new GLatLng(clat,clng));
							setvizsearchrad(outersearchpoint);
						}
					}
					current_results=bizresults_html;
					displayresults(x, bizwherefrom, bizresults_html);
				}else{
					displayresults(x, bizwherefrom, bizresults_html);
				}
				locationbubble(true);
       	    }
			
     	}
      	request.send(null);

}

function handlebizresults(x, markonmap){
	
	var tempbizresults_html;
	
	if (markonmap==true){
		var sbusiness = markbizsearch(x);
		map.addOverlay(sbusiness);
		sbusiness.setImage(markerimage);
		tempbizresults_html=bizinfos[x];
		
		setresultradius(x);
		
	}else{
		tempbizresults_html = buildlistresult(x);
	}
	
	return tempbizresults_html;
	
	
}

function buildlistresult(x){
	
	if(cyosf_active == true){
		var funcname = 'markstorefront'
	}else{
		var funcname = 'markbizresult';
	}
	
	var bizresultlist = '<a href="javascript:'+funcname+'(' + x + ')"><b>' + currentbusinesses[x].name + '</b></a>';
	
	if(currentbusinesses[x].address1 != ''){
		bizresultlist += '<br>' + currentbusinesses[x].address1;
	}
	
	if(currentbusinesses[x].address2 != ''){
		bizresultlist += '<br>' + currentbusinesses[x].address2;
	}
	
	if(currentbusinesses[x].city != ''){
		bizresultlist += '<br>' + currentbusinesses[x].city;
	}
	
	if(currentbusinesses[x].state != ''){
		bizresultlist += ', ' + currentbusinesses[x].state;
	}
	
	if(currentbusinesses[x].country != ''){
		bizresultlist += ', ' + currentbusinesses[x].country;
	}
	
	if(currentbusinesses[x].phone != ''){
		bizresultlist += '<br>' + currentbusinesses[x].phone;
	}
	
	bizresultlist += '<hr>';
	
	return bizresultlist;
	
}

function setresultradius(x){
	
	var tempouterradius = currentlocation[0].point.distanceFrom(currentbusinesses[x].point);

	if(tempouterradius > outersearchradius){
		outersearchradius = tempouterradius;
		outersearchpoint = currentbusinesses[x].point;
	}
	
}


function popbusinesses(name, description, address1, address2, city, state, zip, country, phone, weblink, lat, lng, point, zoom){
	
	this.name=name;
	this.description=description;
	this.address1=address1;
	this.address2=address2;
	this.city=city;
	this.state=state;
	this.zip=zip;
	this.country=country;
	this.phone=phone;
	this.weblink=weblink;
	this.lat = lat;
	this.lng = lng;
	this.point = point;
	this.zoom= zoom;
	
}


//THIS FUNCTION POPULATES THE MAP WITH MARKERS FROM AN INTERNET SEARCH DONE FROM THE PRESENTATION PAGE.
function markbizsearch(biz) {
	
		var markonmap = true;
		var htmltab2="";
		var label1='Info';
		var label2='Directions';
		
	    var point = new GLatLng(currentbusinesses[biz].lat,currentbusinesses[biz].lng);
        var sbusiness = new GMarker(point, {icon:dynamicmarker});
		buildhtml(biz, markonmap);
		
		var directioinpoint=currentbusinesses[biz].lat+','+currentbusinesses[biz].lng;
		var name = currentbusinesses[biz].name;
		current_business_name = currentbusinesses[biz].name;
		var directionstab ='<div ><span class="titlefont">DIRECTIONS:<br><br><a href="javascript:setdirectionbubble(\'to\', \''+directioinpoint+'\', \''+name+'\' )">To Here  </a>   OR   <a href="javascript:setdirectionbubble(\'from\', \''+directioinpoint+'\', \''+name+'\')">  From Here</a></span></div>';
		
        // save the info we need to use later for the side_bar
        sbusinesses[biz] = sbusiness;
        bizinfos[biz] = bizinfo;
		bizbubbles[biz] = bizbubble;
		bizboxes[biz] = bizbox;
        
		 GEvent.addListener(sbusiness, "click", function() {
			activeresult=biz;
			activeresulttype = 'yahoo';
			activemarker=sbusiness;
			createtype='business';
			setmastercreatetype(createtype);
			//panoClient.getNearestPanorama(point, showPanoData);
			//genericinfobox(biz);
			sbusiness.openInfoWindowTabsHtml([new GInfoWindowTab(label1,bizbubbles[biz]), new GInfoWindowTab(label2,directionstab)]);
        });

        return sbusiness;
}


function showsearchbiz(biz) {
	
		var label1='C-Info';
		var label2='Directions';
		var point = new GLatLng(currentbusinesses[biz].lat,currentbusinesses[biz].lng);
        
		var directioinpoint=currentbusinesses[biz].lat+','+currentbusinesses[biz].lng;
		var name = currentbusinesses[biz].name;
		current_business_name = currentbusinesses[biz].name;
		
		var directionstab ='<div ><span class="titlefont">DIRECTIONS:<br><br><a href="javascript:setdirectionbubble(\'to\', \''+directioinpoint+'\', \''+name+'\' )">To Here  </a>   OR   <a href="javascript:setdirectionbubble(\'from\', \''+directioinpoint+'\', \''+name+'\')">  From Here</a></span></div>';
		
		//Resets the page.
		expandmappane();
		
		activemarker=sbusinesses[biz];
		activeresult=biz;
		activeresulttype = 'yahoo';
		//panoClient.getNearestPanorama(point, showPanoData);
		//genericinfobox(biz);
		
		map.setCenter(point, destinationzoom);
		//map.setCenter(point);
		sbusinesses[biz].openInfoWindowTabsHtml([new GInfoWindowTab(label1,bizbubbles[biz]), new GInfoWindowTab(label2,directionstab)]);
		
}


//THIS FUNCTIONS TAKES THE VARIABLES OF EACH SEARCH RESULTS AND FORMATS THE HTML THAT IS DISPLAYED IN THE BUBBLES AND RESULTS DIVS
function buildhtml(biz, markonmap){
	
	var gatheraddress='';
	bizinfo='';
	bizbubble='<div class="bubble" >';
	bizbox='';
	
	
	if (currentbusinesses[biz].name!=""){
		bizinfo+='<b><a href="javascript:showsearchbiz(' + biz + ')">' + currentbusinesses[biz].name + '</a></b>';
		bizbubble+='<strong>'+currentbusinesses[biz].name+'</strong>';
		bizbox+=currentbusinesses[biz].name;
	}
	
	if (currentbusinesses[biz].address1!=""){
		bizinfo+='<br>'+currentbusinesses[biz].address1;
		bizbubble+='<br>'+currentbusinesses[biz].address1;
		bizbox+='\n'+currentbusinesses[biz].address1;
		gatheraddress+=currentbusinesses[biz].address1+' ';
	}
	if (currentbusinesses[biz].address2!=""){
		bizinfo+='<br>'+currentbusinesses[biz].address2;
		bizbubble+='<br>'+currentbusinesses[biz].address2;
		bizbox+='\n'+currentbusinesses[biz].address2;
	};
	if (currentbusinesses[biz].city!=""){
		bizinfo+='<br>'+currentbusinesses[biz].city;
		bizbubble+='<br>'+currentbusinesses[biz].city;
		bizbox+='\n'+currentbusinesses[biz].city;
		gatheraddress+=currentbusinesses[biz].city+' ';
	}
	if (currentbusinesses[biz].state!=""){
		bizinfo+=', '+currentbusinesses[biz].state;
		bizbubble+=', '+currentbusinesses[biz].state;
		bizbox+=', '+currentbusinesses[biz].state;
		gatheraddress+=currentbusinesses[biz].state;
	}
	if (currentbusinesses[biz].zip!=""){
		bizinfo+=', '+currentbusinesses[biz].zip;
		bizbubble+=', '+currentbusinesses[biz].zip;
		bizbox+=', '+currentbusinesses[biz].zip;
	}
	if (currentbusinesses[biz].county!=""){
		bizinfo+=', '+currentbusinesses[biz].country;
		bizbubble+=', '+currentbusinesses[biz].country;
		bizbox+=', '+currentbusinesses[biz].country;
	}
	if (currentbusinesses[biz].phone!=""){
		bizinfo+='<br>'+currentbusinesses[biz].phone;
		bizbubble+='<br>'+currentbusinesses[biz].phone;
		bizbox+='\n'+currentbusinesses[biz].phone;
	}
	
	
	if (currentbusinesses[biz].weblink!=""){
		
		bizinfo+='<br><a href="javascript:framewebsite(\''+currentbusinesses[biz].weblink+'\')"><span class="minifont">Website</span></a>';
		bizbubble+='<br><a href="javascript:framewebsite(\''+currentbusinesses[biz].weblink+'\')">Website</a>';
		bizbox+='\n'+currentbusinesses[biz].weblink;
	}
	
	var cleanaddress=cleansearchstring(gatheraddress);
	
	if(markonmap==true){
		bizbubble+='<br><br><div style="font-size:12px;"><table width="100%" border="0" cellpadding="0">';
		bizbubble+='<tr><td width="50%" align="left" style="border-right:solid 1px #666666;">';
		bizbubble+='<img src="globalgraphics/general/loc_marker_mini_icon.jpg" />';
		bizbubble+='<a href="javascript:setsearchcenter(\''+cleanaddress+'\')">Set Explore Center</a></td>';
		bizbubble+='<td align="right"><a href="javascript:prepforsend(\''+biz+'\', \'yahoo\')">Send to me</a></td></tr></table></div>';
		bizbubble+='<div style="font-size:12px;"><table width="100%" border="0" cellpadding="0">'
		bizbubble+='<tr><td width="5%"></td><td width="45%" style="border-right:solid 1px #666666;"></td>';
		if(tb_active == false){
			bizbubble+='<td align="right" style="padding-left:5px;"><a href="javascript:verifiedaddpassport(\'business\')">Add to My Passport</a></td></tr>';
		}else{
			bizbubble+='<td style="background-color:#F9AF6C; border:solid 1px #666666; padding:4px;" align="right" width="50%" valign="middle" style="padding-left:5px;"><a href="javascript:verifiedaddpassport(\'business\')">Add to Trip</a></td></tr>';
		}
		bizbubble+='</table></div>';
	}
	
	bizinfo+='<br><hr>';
	bizbubble+='</div>';
}


//HANDLES RESULTS FROM SEARCHFUNCTIONALITY.JS
function displayresults(i, wherefrom, destresults_html){
	
	//alert('i: '+i+'\n wherefrom: '+ wherefrom+'\ndestresults_html: '+destresults_html);
	
	if(cpsearchtype == 'tour'){
		//PUTS THE TOUR SHARING FUNCTIONALITY IN THE RESULTS PANE.
		var tourcontrols='<div id="tourcontrols">'+
						 '<table width="100%" cellpadding="0"><tr>'+
						 //'<td align="left"><a href="javascript:buildshare(\'tour\')">'+
						 '<td align="left"><a href="http://www.cplanet.com/tourviewer.html?tourid='+activetourid+'" target="blank">'+
						 '<b>Share</b><span class="minifont"> this tour</span></a></td>'+
						 '<td align="right"><a href="javascript:associatetour(\''+activetourid+'\', \''+currentuser+'\')">'+
						 '<b>Add</b><span class="minifont"> this tour to my stuff</span></a></td>'+
						 '</tr></table></div>';
			//tourcontrols+='<td><a href="javascript:testconnections()">Connect</a></td>';
		
		//alert('Tour name: '+activetourname+'\nTour ID: '+activetourid);
		
		var resultsheader='<div align="center" class="cpfontheading" id="dynresultsheader">'+activetourname+'</div>';
	}
	
	switch (wherefrom){
		
		case 'invitation':
			var extragreeting = '<div id="welcomemessage" style="height:270px;">'
								+'<table width="100%" border="0" >'
								+'<tr><td align="center" class="cpfontbold">You have been invited to see </td></tr>';
				if(i!=1){
					extragreeting += '<tr><td align="center" class="cpfontbold">the Cplanet Tour titled:</td></tr>'
									+'<tr><td align="center" class="cpfontheading">'+currentdestinations[i].tourname+'<hr></td></tr>'
									+'<tr><td align="center"><a href="javascript:displayresults(\'invitetour\')"><b>Click here to view this tour</b></a><br></td></tr>'
								    +'<tr><td align="center">Click the "Cthere" button to view the destination info and media (i.e. picture galleries, video, 3D models, etc.)</td></tr>';
									
					invitetourhtml=destresults_html;
					
				}else{
					extragreeting += '<tr><td align="center" class="cpfontbold">the Cplanet Destination titled:</td></tr>'
									+'<tr><td align="center" class="cpfontheading">'+currentdestinations[i].name+'<hr></td></tr>'
									+'<tr><td align="left">'+destresults_html+'</td></tr>';
					//alert(currentdestinations[i].hasmedia);
					if(currentdestinations[i].hasmedia=='True'){
						extragreeting += '<tr><td align="center">Click the "Cthere" button to view the destination info and media (i.e. picture galleries, video, 3D models, etc.)</td></tr>';
					}
					
					showdestination('0');
					
				}
				
			extragreeting += '</table></div>';
			
			cpresultsheader.innerHTML = 'Welcome to Cplanet';
			cpresultslocation.innerHTML = extragreeting;
			highslidecontrol('cpresultshtml');
		break;
		
		case 'invitetour':
			//USE THE HTML FROM THE GLOBAL VARIABLE SET WHEN THE TOUR INVITATION GREETING WAS POSTED.
			cpresultslocation.innerHTML = invitetourhtml;
			cpresultsheader.innerHTML = cpresultsheader;
			cpresultscontrols.innerHTML = tourcontrols;
		break;
		
		case 'poximitysearch':
			bizresultslocation.innerHTML = destresults_html;
			highslidecontrol('resultshtml');
			bizresultsheader.innerHTML='Featured Storefronts';
		break;
		
		case 'sf3d':
			//alert(destresults_html);
			//displayMessage(destresults_html, 'Cthere Storefront within this scene');
			document.getElementById("cplanetresults").innerHTML = destresults_html;
		break;
		
		case 'prox_storefronts':
			if(i>0){
				bizresultslocation.innerHTML = destresults_html;
				highslidecontrol('resultshtml');
				bizresultsheader.innerHTML='Featured Destinations';
			}
		break;
		
		case 'bizsearch':
			bizresultslocation.innerHTML = destresults_html;
			highslidecontrol('resultshtml');
			bizresultsheader.innerHTML = activebizheader;
		break;
		
		case 'storefront':
			displaybizresults(destresults_html);
		break;
		
		case 'hubtour':
			//highslidecontrol('cpresultshtml');
//THE FOLLOWING FUNCTION IS IN THE STOREFRONT.JS
			cpresultslocation.innerHTML = destresults_html;
			cpresultsheader.innerHTML = resultsheader;
			cpresultscontrols.innerHTML = tourcontrols;
			highslidecontrol('cpresultshtml');
			connecttohub();
		break;
		
		case 'hubtourdestwin':
			connecttohub();
		break;
		
		case 'tourwindow':
			displaycptourlist(destresults_html);
		break;
		
		case 'ctheresample1':
			//NO ACTION
		break;
		
		default:
			cpresultslocation.innerHTML = destresults_html;
			cpresultsheader.innerHTML = resultsheader;
			//cpresultscontrols.innerHTML = tourcontrols;
			highslidecontrol('cpresultshtml');
		break;
		
	 }
	
}

function connecttohub(){
	
	var custombubblehtml;
	var lat = parseFloat(destarray[0].lat);
	var lng = parseFloat(destarray[0].lng);
	
	if(destarray[0].description != ''){
		if(destarray[0].email != ''){
			var conxstr = destarray[0].email;
			//alert(conxstr);
			var hasflag = conxstr.indexOf('createhubtour');
			if(hasflag != -1){
				var strsegs = conxstr.split('~');
		//ONCE STEW RAISES THE LIMIT TO THE EMAIL FIELD CHECK FOR THE MORE INFO AND LINK.
				//alert('Hub Tour String: '+conxstr+'\n# of string segments'+strsegs.length);
				if(strsegs.length > 0){
					custombubblehtml += '<div style="width:525px; height:25px; overflow:auto;">'
										+'<a href="javascript:createhubtour(\''+destarray[0].destinationid+'\', \''+strse+strsegs[1]+'\')">'+strsegs[2]+'</a></div>';
				}
			}
		}
		
		custombubblehtml = '<div style="width:525px; height:250px; overflow:auto;">'+destarray[0].description+'</div>';
	}else{
		custombubblehtml = hubbubble;
	}
	
	 var point = new GLatLng(lat,lng);
     var hubmarker = new GMarker(point, {icon:location_marker, zIndexProcess:importanceOrder});
	 hubmarker.importance = 2;
	 
	 GEvent.addListener(hubmarker, "click", function() {
		hubmarker.openInfoWindowHtml(custombubblehtml);
	 });
	 
	 GEvent.addListener(hubmarker, "infowindowopen", function() {
			var markerpoint = hubmarker.getLatLng();
			var xshift = (windowwidth * -0.15);
			var yshift = (windowheight * 0.2);
			
			map.setCenter(markerpoint);
			map.panBy(new GSize(xshift,yshift));
     });
	 
	 map.addOverlay(hubmarker);
	 hubmarker.openInfoWindowHtml(custombubblehtml);
	
	
	setTimeout("connecttodest("+lat+","+lng+")",500);
}
