var separator = "|";
var separator2 = ":";
var map = null;
var mapMarker = null;
var positionMarker = null;

function load(m)  {
    	// Pass one param and split because of bug in browsers using javascript
    	var i=0, p=0, x=0, latd=44, longtd=-99, zoomlvl=4, nbrmarkers=1;
    	p=m.indexOf(":");var latd=m.substring(0, p);m=m.substring(p+1);
    	p=m.indexOf(":");var longtd=m.substring(0, p);m=m.substring(p+1);
    	p=m.indexOf(":");var zoomlvl=m.substring(0, p);m=m.substring(p+1);
    	var nbrmarkers=m;
	if (GBrowserIsCompatible()) {
     	  map = new GMap2(document.getElementById("map"));
	  var point = new GLatLng(latd, longtd);
  	  map.setCenter(point, zoomlvl);
	  // following lines are bug in google maps setting zoom level in setCenter
	  if (map.getZoom() < zoomlvl) {map.setZoom(2); for (var i=2; i<zoomlvl; i++) {map.zoomIn();}  }
     	  map.addControl(new GLargeMapControl());
   	  map.addControl(new GMapTypeControl());
	  mapMarker= new Array(nbrmarkers);
   	  for (var i=0; i<=nbrmarkers; ++i) {
		mapMarker[i] = null;
          }
  	}
}

function unload() {
     // destroy any property markers
     var nbrMarkers = mapMarker();
     for (var i=0; i<=nbrmarkers; ++i) {
	destroy(mapMarker[i]);
     }
     // destroy any attraction markers
     for (var i=0; i<=5; i++) {
	   if (attractionsShown != 'N') {
		var nbrAttractions = attractionMarkers[i]();
		for (var j=0; j<nbrAttractions; j++) {
			destroy(attractionMarkers[i, j]);
		}
	   }
     }
}

function loadandlisten(m) {
    	// Pass one param and split because of bug in browsers using javascript
    	var argm=m, i=0, p=0, x=0, latd=44, longtd=-99, zoomlvl=4, nbrmarkers=1;
    	p=m.indexOf(":");var latd=m.substring(0, p);m=m.substring(p+1);
    	p=m.indexOf(":");var longtd=m.substring(0, p);m=m.substring(p+1);
    	p=m.indexOf(":");var zoomlvl=m.substring(0, p);m=m.substring(p+1);
    	var nbrmarkers=m;
	load(argm);
	positionMarker = new GMarker(new GLatLng(latd, longtd), {draggable: true});
	addpositionmarker();
}

function loadanddisplay(m) {
    	// Pass one param and split because of bug in browsers using javascript
    	var argm=m, i=0, p=0, x=0, latd=44, longtd=-99, zoomlvl=4, nbrmarkers=1;
    	p=m.indexOf(":");var latd=m.substring(0, p);m=m.substring(p+1);
    	p=m.indexOf(":");var longtd=m.substring(0, p);m=m.substring(p+1);
    	p=m.indexOf(":");var zoomlvl=m.substring(0, p);m=m.substring(p+1);
    	var nbrmarkers=m;
	load(argm);
	positionMarker = new GMarker(new GLatLng(latd, longtd), {draggable: false});
	map.addOverlay(positionMarker);
}

function loadlistenandreset(m) {
	loadandlisten(m);
	resetsearchfields();
}

function addpositionmarker() {
       map.addOverlay(positionMarker);
       GEvent.addListener(positionMarker, "dragend", function() {
         var location = positionMarker.getPoint();
         document.searchform.mapLatLong.value = location.toString();
         document.searchform.mapLat.value = location.lat();
         document.searchform.mapLong.value = location.lng();
	 var zoomLevel = map.getZoom();
	 if (document.searchform.SearchRadius.value == '' && zoomLevel < 7) {zoomLevel = 7;}
	 if (document.searchform.SearchRadius.value == '50' && zoomLevel < 9) {zoomLevel = 9;}
         map.setCenter(location, zoomLevel);
	 if (document.searchform.SearchRadius.value == '') {document.searchform.SearchRadius.value = 50;}
       });
       GEvent.addListener(map, "zoomend", function() {
         document.searchform.mapZoom.value = map.getZoom();
       });
}

function resetform() {
	removeAttractions();
	resetsearchfields();
	addpositionmarker();
}

function resetsearchfields() {
       document.searchform.SearchRadius.value = '';
       document.searchform.skiareas.checked = '';
       document.searchform.themeparcs.checked = '';
       document.searchform.MinSleepsNbr.value = '';
       document.searchform.MinBrsNbr.value = '';
       document.searchform.PetsAllowed.value = '';
       document.searchform.SmokingAllowed.value = '';
       document.searchform.NearBeach.value = '';
       document.searchform.NearSki.value = '';
       document.searchform.HasPool.value = '';
       document.searchform.HasHottub.value = '';
       document.searchform.startDateCtrl.value = '';
       document.searchform.endDateCtrl.value = '';
}

function mapit(m) {
    // Pass one param and split because of bug in i.e.
    var i, p, x;
    p=m.indexOf(":");var latd=m.substring(0, p);m=m.substring(p+1);
    p=m.indexOf(":");var longtd=m.substring(0, p);m=m.substring(p+1);
    p=m.indexOf(":");var index=m.substring(0, p);m=m.substring(p+1);
    var specs=m;
    p=specs.indexOf("|");var sqft=specs.substring(0, p);specs=specs.substring(p+1);
    p=specs.indexOf("|");var ptype=specs.substring(0, p);specs=specs.substring(p+1);
    p=specs.indexOf("|");var brs=specs.substring(0, p);specs=specs.substring(p+1);
    p=specs.indexOf("|");var bas=specs.substring(0, p);specs=specs.substring(p+1);
    var sleeps=specs;
    var label='<font size=2>Line '+index+' -> '+sqft+' Sq Ft '+ptype+'<br> '+brs+'Brs, '+bas+'Bths, Sleeps '+sleeps+'</font>';
    var point = new GLatLng(latd, longtd);
    if (map == null) {
	load(latd, longtd, 11, index);
    }
    if (mapMarker[index] == null) {
       var marker = createMarker(point, index, label, 'yellow');
       map.addOverlay(marker);
       mapMarker[index] = marker;
       map.setCenter(point);
    }
    else {
       map.removeOverlay(mapMarker[index]);
       mapMarker[index] = null;
    }
}

function toggle(m) {
  //  Toggle Map On and Off
  var i, p, x;
  p=m.indexOf(":");var latd=m.substring(0, p);m=m.substring(p+1);
  p=m.indexOf(":");var longtd=m.substring(0, p);m=m.substring(p+1);
  p=m.indexOf(":");var index=m.substring(0, p);m=m.substring(p+1);
  p=m.indexOf(":");var pspecs =m.substring(0, p);m=m.substring(p+1);
  var specs=m;
  if (map == null) {
      var n=latd+':'+longtd+':'+index+':'+pspecs;
      mapit(n)
  }
  else {
      GUnload();
      map = null;
      p=specs.indexOf("|");var type=specs.substring(0, p);specs=specs.substring(p+1);
      if (type == 'image') {
         p=specs.indexOf("|");var source=specs.substring(0, p);specs=specs.substring(p+1);
         p=specs.indexOf("|");var align=specs.substring(0, p);specs=specs.substring(p+1);
         p=specs.indexOf("|");var width=specs.substring(0, p);specs=specs.substring(p+1);
         p=specs.indexOf("|");var height=specs.substring(0, p);specs=specs.substring(p+1);
         var border=specs;
         var myhtml='<img src='+source+' align='+align+' width='+width+' height'+height+' border='+border+'>';
         document.getElementById("map").innerHTML = myhtml;    
      }
      else {
         document.getElementById("map").innerHTML = specs;    
      }
  }
}

function createMarker(point, number, label, color) {
  var icon = new GIcon();
  if (color == 'yellow') {
	icon.image = "/BlueChipRentals/images/mm_"+color+number+".gif";
	icon.iconSize = new GSize(25, 25);
  }
  else {
	icon.image = "/BlueChipRentals/images/mm_20_"+color+".png";	
	icon.iconSize = new GSize(12, 20);
  }
  icon.shadow = "/BlueChipRentals/images/mm_20_shadow.png";
  icon.shadowSize = new GSize(22, 20);
  icon.iconAnchor = new GPoint(6, 20);
  icon.infoWindowAnchor = new GPoint(5, 1);
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(label);});
  return marker;
}

var http_request = false;
var attractionsShown = new Array('N', 'N', 'N');
var attractionColors = new Array('yellow', 'red', 'blue');
var nbrAttractionTypes = 2;
var attractionMarkers = new Array(nbrAttractionTypes);
var attractionsHTML = new Array(nbrAttractionTypes);
function processAttractions(attractionCategoryId) {
  // Remove all attractions
  var attractionShown = attractionsShown[attractionCategoryId];
  hideAttractions();
  if (attractionShown == 'N') {
	getAttractions(attractionCategoryId);
   }
   else {
	if (attractionShown == 'L') {
		showAttractions(attractionCategoryId);
	}
   }
}

function callback() {
// alert('Let Callback' + ' ' + http_request.readyState + ' ' + http_request.status);
// alert(http_request.getAllResponseHeaders());
// alert(http_request.responseText);
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            // store markers
	    var attractions = http_request.responseText.split(':');
	    var numAttractions = attractions.length;
	    var attractionCategoryId = attractions[1].split('|')[0];
	    var htmlStart = "<td align=left><a href=#pan onclick='panToAttraction(";
	    var comma1 = ',';
	    var comma2 = ',"';
	    var end1 = '")';
	    var end2 = "'>";
	    var htmlend = '</a></td>';
	    attractionsShown[attractionCategoryId] = 'L';
	    attractionsHTML[attractionCategoryId] = '<table width=95% align=center><tr>';
	    attractionMarkers[attractionCategoryId] = new Array(numAttractions-1); 
	    var row = 0;
	    for (var x = 1; x<numAttractions; x++) {
		var y= x-1;
	        var attractionAttributes = attractions[x].split('|');
	    	var point = new GLatLng(attractionAttributes[2],attractionAttributes[3]);
	        var marker = createMarker(point, x-1, attractionAttributes[1], attractionColors[attractionCategoryId]);
		row++;
	        map.addOverlay(marker);
	        attractionMarkers[attractionCategoryId][y] = marker;
	        attractionsHTML[attractionCategoryId] = attractionsHTML[attractionCategoryId] + htmlStart + attractionCategoryId + comma1 + y + comma2 + attractionAttributes[1] + end1 + end2 + attractionAttributes[1] + htmlend;
		if (row == 3 && x<numAttractions-1) {attractionsHTML[attractionCategoryId] = attractionsHTML[attractionCategoryId] + '</tr>\n<tr>';row=row-3;}
	    }
	    attractionsShown[attractionCategoryId] = 'Y';
	    attractionsHTML[attractionCategoryId] = attractionsHTML[attractionCategoryId] + '</tr></table>';
	    document.getElementById("attractions").innerHTML = attractionsHTML[attractionCategoryId];
        }
    }
}

function getAttractions(attractionCategoryId) {
	// Get attractions
	document.getElementById("attractions").innerHTML = 'Please wait ...';
	var url = "/cgi-bin/BlueChipRentals/common/perllet_getAttractions.pl?letId=" + encodeURIComponent(attractionCategoryId);
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
          http_request = new XMLHttpRequest();
          if (http_request.overrideMimeType) {
             // set type accordingly to anticipated content type
             // http_request.overrideMimeType('text/xml');
             http_request.overrideMimeType('text/html');
          }
        } else if (window.ActiveXObject) { // IE
          try {
              http_request = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
             try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (e) {}
          }
        }
        if (!http_request) {
           alert('Cannot create XMLHTTP instance');
           return false;
        }
	http_request.open("GET", url, true);
	http_request.onreadystatechange = callback;
	http_request.send(null);
}

function showAttractions(attractionCategoryId) {
	attractionsShown[attractionCategoryId] = 'Y';
	var numAttractions = attractionMarkers[attractionCategoryId].length;
	for (var x = 0; x<numAttractions; x++) {
		var attractionMarker = attractionMarkers[attractionCategoryId][x];
	        attractionMarker.show();
	}
	document.getElementById("attractions").innerHTML = attractionsHTML[attractionCategoryId];
}

function hideAttractions() {
	// hide all attractions and uncheck other categories checkbox
	for (var y = 1; y<3; y++) {
		if (attractionsShown[y] == 'Y') {
			if (y == 1) {document.searchform.skiareas.checked = '';} else {document.searchform.themeparcs.checked = '';}
			var numAttractions = attractionMarkers[y].length;
			for (var x = 0; x<numAttractions; x++) {
				var attractionMarker = attractionMarkers[y][x];
        			attractionMarker.hide();
			}
			attractionsShown[y] = 'L';
		}
	}
	map.closeInfoWindow();
	document.getElementById("attractions").innerHTML = '';
}

function removeAttractions() {
	// Remove all attractions and uncheck other categories checkbox
	map.clearOverlays();
	map.closeInfoWindow();
	for (var y = 1; y<3; y++) {
		if (attractionsShown[y] == 'Y') {
			attractionsShown[y] = 'L';
		}
	}
	document.getElementById("attractions").innerHTML = '';
}

function panToAttraction(attractionCategoryId, x, label) {
	var marker = attractionMarkers[attractionCategoryId][x];
        var location = marker.getPoint();
	map.panTo(location);
	marker.openInfoWindowHtml(label);
	positionMarker.setPoint(location);
        document.searchform.mapLatLong.value = location.toString();
        document.searchform.mapLat.value = location.lat();
        document.searchform.mapLong.value = location.lng();
	if (document.searchform.SearchRadius.value == '' || document.searchform.SearchRadius.value == 0) {document.searchform.SearchRadius.value = 50;}
}

function setVacationerSearchText()
{
	document.getElementById("searchtext").innerHTML = "<a href='javascript:void(0)' onclick='resetVacationerSearchText();return false;'>(hide explanation text)</a><br>Search using map and the fields below.<br>&nbsp;<br>Please be aware that all listings are by third parties who are unconnected with BlueChipRentals.  These property owners list their vacation rentals with us to reach prospective vacationers like you.  Please read the <a href=/BlueChipRentals/website/company/Disclaimer.htm target=disclaimer>DISCLAIMER</a> for further details.<br>&nbsp;<br>Use the map and/or search fields below to begin your search for a rental property.  To search by location first set a location to search arround by dragging the marker to that location.  When you first drag the marker the Search Radius field is reset from The Whole Country to a 50 mile radius.  Zoom the map to get a greater degree of accuracy.  You may also use the Ski Areas or Theme Parks markers.  Checking the relevant box will display markers on the map representing ski areas or theme parks and also display a list of these below the checkboxes.  When the list is shown, click on an attraction in the list to recenter and zoom to that marker on the map.<br>&nbsp;<br>You may also use the other search fields (e.g. Number of Bedrooms) in addition to the map, or instead of the map.  Please scroll down to see these search fields. To ignore location in your search (i.e. search the whole country only using the additional search fields) make sure the Search Radius field is set to The Whole Country selection.<br>&nbsp;<br>Once you find a property that is of interest, you may add it to your favorites by displaying the full listing and clicking the 'add to favorites' text at the top of the listing.<br>&nbsp;";

}

function resetVacationerSearchText()
{
	document.getElementById("searchtext").innerHTML = "<a href='javascript:void(0)' onclick='setVacationerSearchText();return false;'>(show how-to-search text)</a>";

}

function setVacationerResultsText()
{
	document.getElementById("resultstext").innerHTML = "<a href='javascript:void(0)' onclick='resetVacationerResultsText();return false;'>(hide explanation text)</a><br>The results of your search are shown below<br>&nbsp;<br>Please be aware that all listings are by third parties who are unconnected with BlueChipRentals.  These property owners list their vacation rentals with us to reach prospective vacationers like you.  Please read the <a href=/BlueChipRentals/website/company/Disclaimer.htm target=disclaimer>DISCLAIMER</a> for further details.<br>&nbsp;<br>To display a full listing click on the title text for that listing.  To show the listing on the map click on the atlas globe or the yellow numbered icon in the listing row.<br>&nbsp;<br>You may also show attractions on the map by clicking either the ski Areas or Theme Parks checkboxes.  Checking the relevant box will display markers on the map representing ski areas or theme parks and also display a list of these below the checkboxes.  When the list is shown, click on an attraction in the list to recenter and zoom to that marker on the map.<br>&nbsp;<br>You may also click on the Calendar or Review icons in the listing to show the property calendar or reviews for this property respectively.  You may also view these items in the full listing page.<br>&nbsp;";

}

function resetVacationerResultsText()
{
	document.getElementById("resultstext").innerHTML = "<a href='javascript:void(0)' onclick='setVacationerResultsText();return false;'>(show search results explanation text)</a>";

}

function setRHSText(arg)
{
	if (document.getElementById("rhstext")) {
		document.getElementById("rhstext").innerHTML = arg;
	}
}

