var savedestination=[];
var savedestinationname;
var savedestinationid;
var savecount=1;

function markctherelocsearch(type){
	
	locsearchresult = buildcreatebubble('locsearch');
	
	map.setCenter(currentlocation[0].point, defaultzoom);
	var newmarker=new GMarker(currentlocation[0].point, {icon:dynamicmarker, draggable: true});
	map.addOverlay(newmarker);
	
	activemarker=newmarker;
	
	GEvent.addListener(newmarker, "drag", function(){
		map.closeInfoWindow();
	});
				
	GEvent.addListener(newmarker, "click", function(marker, point) {
		//alert('create by location search');
		markertype='create';
		createtype = type;
		activemarker = newmarker;
		newmarker.openInfoWindowHtml(locsearchresult);
	});
				
	newmarker.openInfoWindowHtml(locsearchresult);
	
	//highslidecontrol('dynhtmloff');
	  
}

function buildcreatebubble(type){
	
	mastercreatetype = type;
	
	var locsearchresult = '<div style="width:350px; height:auto; white-space:normal;" class="cpfont">';
	
	locsearchresult += '<table align="center" border="0" cellspacing="0" cellpadding="4" style="border-bottom:solid 1px #999999;">'
	locsearchresult += '<tr><td align="center"><img src="globalgraphics/general/cp_create_icon.png" align="right" /></td>'
	locsearchresult += '<td align="left" valign="middle">'
	locsearchresult += '<span class="cpfontheading18"><i>Step 2</i></span><br />'
	locsearchresult += '<span class="cpfontheading"><i>Verify location and map settings</i></span></td></tr></table>'
				
				
	locsearchresult += '<div align="left" class="cpfont"><span class="minifont">Location Result:</span><br />'
	locsearchresult += '<span class="cpfontheading" style="font-style:italic;">';
	
	
	switch (type){
		case 'locsearch':
		
			try{
				if (currentlocation[0].address!=""){
					locsearchresult +=currentlocation[0].address+' ';
				}
				if (currentlocation[0].city!=""){
					locsearchresult += currentlocation[0].city;
				}
				if (currentlocation[0].state!=""){
					locsearchresult += ', '+currentlocation[0].state;
				}
				if (currentlocation[0].zip!=""){
					locsearchresult += ', '+currentlocation[0].zip;
				}
				if (currentlocation[0].country!=""){
					locsearchresult += ' '+currentlocation[0].country;
				}
			}catch(err){
				//Error catch goes here.
			}
			
		break;
		
		case 'latlng':
			
			try{
				locsearchresult += 'Latitude: ' + activemarker.getPoint().y;
				locsearchresult += '<br />Longitude: ' + activemarker.getPoint().x;
			}catch(err){
				//Error catch goes here.
			}
			
		break;
		
		case 'mapclick':
			
			try{
				locsearchresult += 'Latitude: ' + activemarker.getPoint().y;
				locsearchresult += '<br />Longitude: ' + activemarker.getPoint().x;
			}catch(err){
				//Error catch goes here.
			}
			
		break;
		
		case 'bizsearch':
			
			locsearchresult += bizbubble;
			
		break;
	}
	
	locsearchresult += '</span></div><br />';
	
	locsearchresult += '<div align="left" style="font-size:11px; width:90%;">'
						+'<li>Use the map settings in the top left corner of the map to set the zoom and map type.</li>'
						+'<li>To adjust the marker\'s location, close this bubble and drag the marker.</li>'
						+'<li>Click the marker to reopen the bubble.</li></span></div><br />';
						
	locsearchresult += '<table border="0" cellspacing="0" cellpadding="0"><tr>'
						+'<td ><div align="left"><input id="btnRestartCthere" type="button" value="Start Over" onclick="restartcreate()" />'
						+'<td width="20%"></td>'
						+'<td ><div align="right">'
						+'<input id="btnPopDestFields" type="button" value="Next" onclick="saveactivemarker()" /></div></td></tr></table></div>';				
	
	return locsearchresult;
	
}

// THIS FUNCTION MARKS THE MAP WITH A USER ENTERED DESTINATION FROM LATITUDE AND LONGITUDE
function showlatlng(type){
	
	var plotthepoint = false;
	
	if (type!='dec'){
		if(document.getElementById("lathour").value != 'hours'){
			var lathour = parseFloat(document.getElementById("lathour").value);
			var latmin = parseFloat(document.getElementById("latmin").value);
			var latsec = parseFloat(document.getElementById("latsec").value);
			var latdir = document.getElementById("latdir").value
			
			var lnghour = parseFloat(document.getElementById("lnghour").value);
			var lngmin = parseFloat(document.getElementById("lngmin").value);
			var lngsec = parseFloat(document.getElementById("lngsec").value);
			var lngdir = document.getElementById("lngdir").value
			
			//alert(lnghour+' , '+lngmin+' , '+lngsec+' , '+lngdir)
			
			var decmlat = lathour+((latmin/60)+(latsec/3600));
			var decmlng = lnghour+((lngmin/60)+(lngsec/3600));
			
			if(latdir=='s'){
				decmlat = decmlat*-1;
			}
			
			if(lngdir=='w'){
				decmlng = decmlng*-1;
			}
			
			//alert('Lat: '+decmlat+', Lng: '+decmlng);
			
			var point = new GLatLng(decmlat,decmlng);
			plotthepoint = true;
		}else{
			alert('Please enter a valid Latitude and Longitude');	
		}
	}else{
		var formlat = document.getElementById("latdec").value;
		var formlng = document.getElementById("lngdec").value;
		
		if(formlat != 'decimal'){
			var lat = parseFloat(formlat);
			var lng = parseFloat(formlng);
			var point = new GLatLng(lat,lng);
			plotthepoint = true;
		}else{
			alert('Please enter a valid Latitude and Longitude');	
		}
		
	}
	
	if(plotthepoint == true){
		try{
			changetabstrip('map');
		}catch(err){
			//No Action
		}
	
		map.setCenter(point, 10);
		var newmarker = new GMarker(point, {icon:dynamicmarker, draggable: true});
		map.addOverlay(newmarker);
		newmarker.enableDragging();
		activemarker = newmarker;
	
		var latlngtext = buildcreatebubble('latlng');
					
		GEvent.addListener(newmarker, "drag", function(){
			map.closeInfoWindow();
		});
					
		GEvent.addListener(newmarker, "click", function(marker, point) {
			//alert('create by latlng');
			markertype='create';
			createtype='latlng';
			activemarker = newmarker;
			var latlngtext = buildcreatebubble('latlng');
			newmarker.openInfoWindowHtml(latlngtext);
		});
		
		newmarker.openInfoWindowHtml(latlngtext);
		
		try{
			closeactiveexpander();
		}catch(err){
			//No Action
		}
	}
		
}

		
// THIS FUNCTION MARKS THE MAP WITH A USER ENTERED DESTINATION FROM A MAP CLICK
function clickmapexact(){
	
	try{
		changetabstrip('map');
	}catch(err){
		//No Action
	}
		
	GEvent.addListener(map, "click", function(overlay, point) {
		if(tbmapclickactive==true){
			if (point != null) {
				geopurpose = 'create';
				clickloc = true;
				//alert(latlng);
				
				try{
					//document.getElementById('mapsetflagicon').src = 'globalgraphics/general/tourmapclickbut.png';
					document.getElementById('mapsetflagglobal').src = 'globalgraphics/general/cpexloremapclick.png';
					document.getElementById('mapclickbut').src = 'globalgraphics/dmo_menu/dmo_mapclick.jpg';
					document.getElementById('tourmapclickbut').src = 'globalgraphics/general/tour_activatemapclick.png';
				}catch(err){
					//Error catch goes here.
				}
				
				var newmarker=new GMarker(point, {icon:dynamicmarker, draggable: true});
				lat=point.y;
				lng=point.x;
				
				map.addOverlay(newmarker);
				newmarker.enableDragging();
				activemarker=newmarker;
				
				var clicktext = buildcreatebubble('mapclick');
		
				GEvent.addListener(newmarker, "drag", function(){
					map.closeInfoWindow();										
					activemarker = newmarker;
				});
							
						
				GEvent.addListener(newmarker, "click", function(marker) {
					//alert('create by mapclick');
					markertype='create';
					createtype='mapclick';
					activemarker=newmarker;
					var clicktext = buildcreatebubble('mapclick');
					newmarker.openInfoWindowHtml(clicktext);
				});
			
				newmarker.openInfoWindowHtml(clicktext);
				tbmapclickactive = false;
			}
		}
	});

}


function markcreatebiz(biz){
	
	try{
		closeactiveexpander();
	}catch(err){
		//No Action
	}
	
	try{
		changetabstrip('map');
	}catch(err){
		//No Action
	}
	
	activeresult=biz;
	//changemarkerimage('/globalgraphics/markers/marker_test.png');
	
	var markonmap = false;
	var htmltab2="";
	var label1='Info';
	var label2='Directions';
	var point = new GLatLng(currentbusinesses[biz].lat,currentbusinesses[biz].lng);
    var selectedbiz = new GMarker(point, {icon:dynamicmarker, draggable: true});
	map.setCenter(point, destinationzoom);
	activemarker = selectedbiz;
	buildhtml(biz, false);
	
	var businesstext = buildcreatebubble('bizsearch');
		
	map.addOverlay(selectedbiz);
	
	selectedbiz.enableDragging();
	
	GEvent.addListener(selectedbiz, "drag", function(){
		map.closeInfoWindow();
	});
				
	GEvent.addListener(selectedbiz, "click", function(marker, point) {
		activemarker = selectedbiz;
		selectedbiz.openInfoWindowHtml(businesstext);
	});
				
	selectedbiz.openInfoWindowHtml(businesstext);
		
}

function verifiedaddpassport(type){
		switch(type){
			case 'business':
				mastercreatetype = 'bizsearch';
				saveactivemarker();
			break;	
		}
}

function setmastercreatetype(createtype){
	mastercreatetype = createtype;
}



function saveactivemarker(){
	
	var tempholder;
	var blank='';
	var lat = activemarker.getPoint().y;
	var lng = activemarker.getPoint().x;
	var zoom = map.getZoom();
	var maptype = map.getCurrentMapType().getName();
	var destmarker = markericon;
	
//	if(type!='edit'){
//		formtype='create';
//		activedescription = '';
//	}else{
//		formtype='edit';	
//	}
//	
//	buildcreateform();
	
	switch (mastercreatetype){
			case 'mapclick':
				var destname = 'Enter Destination Name Here';
				savedestination[0] = new popsavedestination(blank, destname, blank, blank, 1, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, lat, lng, zoom, maptype, destmarker);
				savecount=savecount+1;
			break;
			
			case 'latlng':
				var destname='Destination #'+savecount+' from Lat/Lng';	
				savedestination[0] = new popsavedestination(blank, destname, blank, blank, 1, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, blank, lat, lng, zoom, maptype, destmarker);
				savecount=savecount+1;
			break;
			
			case 'tourlocation':
				//var destname = currentlocation[0].fulladdress;
				var destname = currenttourlocation[0].fulladdress;
				savedestination[0] = new popsavedestination(blank, destname, blank, blank, 1, currenttourlocation[0].address, blank, currenttourlocation[0].city, currenttourlocation[0].state, currenttourlocation[0].zip, currenttourlocation[0].country, blank, blank, blank, blank, blank, blank, lat, lng, zoom, maptype, destmarker);
				savecount=savecount+1;
			break;
			
			case 'locsearch':
				//var destname = currentlocation[0].fulladdress;
				var destname = 'Enter Destination Name Here';
				savedestination[0] = new popsavedestination(blank, destname, blank, blank, 1, currentlocation[0].address, blank, currentlocation[0].city, currentlocation[0].state, currentlocation[0].zip, currentlocation[0].country, blank, blank, blank, blank, blank, blank, lat, lng, zoom, maptype, destmarker);
				savecount=savecount+1;
			break;
			
			case 'event':
				//var destname = currentlocation[0].fulladdress;
				var destname = activeevent[0].name;
				savedestination[0] = new popsavedestination(blank, destname, activeevent[0].description, blank, 1, activeevent[0].address, blank, activeevent[0].city, activeevent[0].state, activeevent[0].zip, activeevent[0].country, blank, blank, blank, blank, activeevent[0].startdate, blank, lat, lng, zoom, maptype, destmarker);
				savecount=savecount+1;
			break;
			
			case 'bizsearch':
				var destname = currentbusinesses[activeresult].name;
					
				savedestination[0]= new popsavedestination(blank, destname, blank, blank, 1, currentbusinesses[activeresult].address1, currentbusinesses[activeresult].address2, currentbusinesses[activeresult].city, currentbusinesses[activeresult].state, currentbusinesses[activeresult].zip, currentbusinesses[activeresult].country, currentbusinesses[activeresult].phone, blank, currentbusinesses[activeresult].weblink, blank, blank, blank, lat, lng, zoom, maptype, destmarker);
				savecount=savecount+1;
			break;
			
			case 'edit':
				var destname = currentdestinations[activeresult].name;
				
				document.getElementById("HiddenDestinationID").value = currentdestinations[activeresult].destinationid;
				activedescription = currentdestinations[activeresult].description;
				
				savedestination[0]= new popsavedestination(currentdestinations[activeresult].destinationid, destname, currentdestinations[activeresult].description, currentdestinations[activeresult].desttags, currentdestinations[activeresult].public, currentdestinations[activeresult].address1, currentdestinations[activeresult].address2, currentdestinations[activeresult].city, currentdestinations[activeresult].state, currentdestinations[activeresult].zip, currentdestinations[activeresult].country, currentdestinations[activeresult].phone, currentdestinations[activeresult].fax, currentdestinations[activeresult].weblink, currentdestinations[activeresult].email, currentdestinations[activeresult].datemanual, currentdestinations[activeresult].datecreated, currentdestinations[activeresult].lat, currentdestinations[activeresult].lng, currentdestinations[activeresult].zoom, currentdestinations[activeresult].maptype, currentdestinations[activeresult].marker);
				
			break;
			
	}
	
	//alert(savedestination[0].name);
//	GEvent.addListener(activemarker, "click", function(marker, point) {
//		activemarker.openInfoWindowHtml(destbubbleform_t1);
//		popdestbubble();
//	});
//	
//	activemarker.openInfoWindowHtml(destbubbleform_t1);
//	if(!cyosf_active){
//		manualcalsetup();
//	}
	
	
	popdestfields();
	
	//This function is specific to the page saving the destination.
	detailsform();
}

function selectdestmarker(){
	var htitle = '<img src="globalgraphics/general/cp_globe_icon_30h.png" /><i>Please select a destination marker.</i>';
	highslideajax('cpajax/cpsnippets.html', 'markermenu', 'markermenuanchor', htitle, 200, 550);	
}

function assignmarker(image){
	
	closeactiveexpander();
	document.getElementById("HiddenMarker").value = image;
	
	markericon = image;
	markerimage = markerpath+markericon;
	document.getElementById("destdetailsmarker").src = markerimage;
}


function popsavedestination(destinationid, name, description, desttags, public, address1, address2, city, state, zip, country, phone, fax, weblink, email, datemanual, datecreated, lat, lng, zoom, maptype, marker){
	
	this.destinationid=destinationid;
	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.datemanual=datemanual;
	this.datecreated=datecreated;
	this.lat = lat;
	this.lng = lng;
	this.zoom= zoom;
	this.maptype=maptype;
	this.marker=marker;
	
	savedestinationname=name;
	
}


function popdestfields(){
	
	
	//alert(savedestination[0].name);
	
	document.getElementById("HiddenLat").value = savedestination[0].lat;
	document.getElementById("HiddenLng").value = savedestination[0].lng;
	document.getElementById("HiddenZoom").value = savedestination[0].zoom;
	document.getElementById("HiddenMapType").value = savedestination[0].maptype;
	document.getElementById("HiddenMarker").value = savedestination[0].marker;
	
	//THE FOLLOWING VARIABLES ARE SENT TO THEIR CORRESPONDING FORM FIELDS IN THE BUBBLE
	if(savedestination[0].name!=''){
		document.getElementById("txtName").value = savedestination[0].name;
	}else{
		document.getElementById("txtName").value = '';
	}
	
//	if(savedestination[0].desttags!=''){
//		document.getElementById("txtDTags").value = savedestination[0].desttags;
//	}else{
//		document.getElementById("txtDTags").value = '';
//	}
	
//	if(savedestination[0].description!=''){
//		document.destinfoform2.destdescript.value = savedestination[0].description;
//	}else{
//		document.destinfoform2.destdescript.value = '';
//	}
	
	if(savedestination[0].address1!=''){
		document.getElementById("txtAddress").value = savedestination[0].address1;
	}else{
		document.getElementById("txtAddress").value = '';
	}
	
	if(savedestination[0].address2!=''){
		document.getElementById("txtAdd2").value = savedestination[0].address2;
	}else{
		document.getElementById("txtAdd2").value = '';
	}
	
	if(savedestination[0].city!=''){
		document.getElementById("txtCity").value = savedestination[0].city;
	}else{
		document.getElementById("txtCity").value = '';
	}
	
	if(savedestination[0].state!=''){
		document.getElementById("txtState").value = savedestination[0].state;
	}else{
		document.getElementById("txtState").value = '';
	}
	
	if(savedestination[0].zip!=''){
		document.getElementById("txtZip").value = savedestination[0].zip;
	}else{
		document.getElementById("txtZip").value = '';
	}
	
	if(savedestination[0].country!=''){
		document.getElementById("txtCountry").value = savedestination[0].country;
	}else{
		document.getElementById("txtCountry").value = '';
	}
	
	if(savedestination[0].phone!=''){
		document.getElementById("txtPhone").value = savedestination[0].phone;
	}else{
		document.getElementById("txtPhone").value = '';
	}
	
	if(savedestination[0].weblink!=''){
		document.getElementById("txtWebsite").value = savedestination[0].weblink;
	}else{
		document.getElementById("txtWebsite").value = '';
	}
	
	if(savedestination[0].email!=''){
		document.getElementById("txtEmail").value = savedestination[0].email;
	}else{
		document.getElementById("txtEmail").value = '';
	}
	
}

//called for association
function associatedestination(destid){
	document.getElementById("HiddenDestinationID").value = destid;
	verifythenopen('associatedest');
	//opencontrol('associatedest');
}

function unassociatecpdestination(){
	//alert(currentdestinations[activeresult].associatedid);
	unassociatedestination(currentdestinations[activeresult].associatedid, currentdestinations[activeresult].tourid);
}
