function checkSearchTerm() {
	var skey = document.Searchasota.keywords.value;
	if (skey == "") {
		window.location.href="/section/searchasota"
		return false;
	} else {
		return true;
	}
}

function getBodyBAK(body,intro,keyword,len) {
	var indexkey = body.indexOf(keyword);
	var keylen = keyword.length;
	var bodyHTML = '';
	
	if (indexkey < 1) {
		newBody = body.substring(0,200);
		var indexfirstspace = newBody.indexOf(' ');
		var indexlastspace = newBody.lastIndexOf(' ');
		newBody = newBody.substring(0,indexlastspace);
		bodyHTML = newBody + '...';
	} else if((parseInt(indexkey) + parseInt(keylen) + 4) >= body.length) {							
		newBody = body.substring(indexkey - 200,(indexkey + keylen+4));
		var indexfirstspace = newBody.indexOf(' ');
		newBody = newBody.substring(indexfirstspace);
		bodyHTML = intro + '...' + newBody;							
	} else {
		newBody = body.substring(indexkey - 100,indexkey + 100);
		var indexfirstspace = newBody.indexOf(' ');
		var indexlastspace = newBody.lastIndexOf(' ');
		newBody = newBody.substring(indexfirstspace,indexlastspace);
		bodyHTML = intro + '...' + newBody + '...';
	}
	return bodyHTML;
}

function getBody(body,introold,keyword,len) {
	var introvalid = body.indexOf('--');
	var intro = '';
	if (introvalid > 0) {
		intro = body.substring(0,introvalid);
	}
	if ((keyword.length > 2) || (keyword != "")) {
		body = highlightSearchTerms(body, keyword, false, true);
	
		//First Highlight Phrase	
		var indexkey = body.indexOf('<b>');
		var keylen = intro.length;
		var bodyHTML = '';

		if (indexkey <= len) {
			type='start';
			len = len + 100;
			newBody = body.substring(0,len);
			var indexfirstspace = newBody.indexOf(' ');
			var indexlastspace = newBody.lastIndexOf(' ');
			newBody = newBody.substring(0,indexlastspace);
			bodyHTML = newBody + '...';
		} else if((parseInt(indexkey) + 50) >= body.length) {							
			type = 'end';
			newBody = body.substring(indexkey - len,body.length);
			var indexfirstspace = newBody.indexOf(' ');
			newBody = newBody.substring(indexfirstspace);
			if (body.length > (len + 100)) {
				intro = body.substring(0,50);
			} 

			bodyHTML = intro + '...' + newBody;							
		} else {
			type = 'middle';
			newBody = body.substring(indexkey - len/2,indexkey + len/2);
			var indexfirstspace = newBody.indexOf(' ');
			var indexlastspace = newBody.lastIndexOf(' ');
			newBody = newBody.substring(indexfirstspace,indexlastspace);
			if (body.length > (len + 100)) {
				intro = body.substring(0,50);
			}		
			bodyHTML = intro + '...' + newBody + '...';
		}

	} else {
		len = len + 100;
		newBody = body.substring(0,len);
		var indexfirstspace = newBody.indexOf(' ');
		var indexlastspace = newBody.lastIndexOf(' ');
		newBody = newBody.substring(0,indexlastspace);
		bodyHTML = newBody + '...';
	}
	
	//return body;
	//return bodyHTML + '<br><br>' + type + 'First Keyword: ' + indexkey + ' Intro Length: '+ keylen + '<br>' + body;
	return bodyHTML;
}

function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<b>";
    highlightEndTag = "</b>";
  }
  
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


function highlightSearchTerms(body, searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
/*
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }

  var bodyText = document.body.innerHTML;
*/  
  var bodyText = body;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
//  document.body.innerHTML = bodyText;
  return bodyText;
//  return true;
}

function NewWindow(width,height,url) {
	window.open(url,"PopUp","menubars=0,scrollbars=1,resizable=1,height="+height+",width="+width);
}



var pdp = {};


// define pdp.overviewImages namespace
pdp.overviewImages = {};
pdp.overviewImages.images = [];
pdp.overviewImages.preloaded_images = [];
pdp.overviewImages.current_image = 0;
pdp.overviewImages.last_adjust_preview = null;

pdp.overviewImages.preload_image = function(image) {
  //var preloadedImage = new Image();
	// commented out to save initial load time - PDP version C
  //pdp.overviewImages.preloaded_images.push(preloadedImage);
  //preloadedImage.src = image.uri;
}


pdp.overviewImages.adjust_preview_image = function(amount) {
  var now = new Date();
  if (null == pdp.overviewImages.last_adjust_preview
    || pdp.overviewImages.last_adjust_preview.getTime() + 200 < now.getTime()) {
    pdp.overviewImages.last_adjust_preview = now;
  }
  else {
    return;
  }
  if (amount != 0) {
	  var index = pdp.overviewImages.current_image + amount;
	  if (index < 0)
	    index = pdp.overviewImages.images.length - 1;
	  if (index > pdp.overviewImages.images.length - 1)
	    index = 0;
  } else {
  	index = 0;
  }
  pdp.overviewImages.current_image = index;
      

  // display the "Image X of Y" X value.
  top.document.getElementById('current_image').innerHTML = index+1;

  // update the actual displayed image
  var element = top.document.getElementById('preview_image');

  var image = pdp.overviewImages.images[index];
  if (null == element) {
    if (null != debug)
      top.alert("No element 'preview_image' found, cannot set image");
    return;
  }
  element.src = '/g/t.gif';
  element.src = image.uri;
  //element.title = image.name;
  //element.height = image.height;
  //element.width = image.width;
}

pdp.overviewImages.first_preview_image = function() {
  var index = 0;
  // update the actual displayed image
  var element = top.document.getElementById('preview_image');
  var image = pdp.overviewImages.images[index];  
}

function mapDirections(){
var daddr = document.getElementById("rLatLng").value;
  var saddr = document.getElementById("myLatLng").value;
  saddr = saddr.replace(/ /g, "+");
  daddr = daddr.replace(/ /g, "+");
  //alert(saddr);
  window.open("http://maps.google.com/maps?saddr="+unescape(saddr)+"&daddr="+unescape(daddr), "directions",
	"width=900, height=600, status=no, toolbar=no, menubar=no, resizable=no");

}

function LTrim(str) { 
 for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
 return str.substring(k,str.length);
}
function RTrim(str) {
 for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
 return str.substring(0,j+1);
}
function Trim(str) {
 str=str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
 return LTrim(RTrim(str));
}