// *********************************************************************************************************************************
//
// (c) Driftwood Interactive. All Rights Reserved.
// This code is licensed to Atlantic Bay Sotheby's International Realty for use on the rentals.atlanticbaysir.com site only.
//
// *********************************************************************************************************************************


// *********************************************************************************************************************************
// Global variables
//
var qConfirmLogouts = true;		// If you don't want confirmation alerts set this to false

var gMapBounds = null;

// *********************************************************************************************************************************
function utRedirect(destination)
{
	window.location.href = destination;
}


// *********************************************************************************************************************************
function utCreatePropertyMap(mapID, point, propertyAddress)
{
	var map = new GMap2(document.getElementById(mapID));
	map.addControl(new GSmallMapControl());

	map.setCenter(new GLatLng(0,0),0);
	if (gMapBounds == null) {
		gMapBounds = new GLatLngBounds();
	}

	if (point != null) {
		var marker = new GMarker(point, {title: propertyAddress, clickable: false});
		map.addOverlay(marker);
		zoomDelta = 2;
	}
	else {
		point = new GLatLng(41.876718930343934, -70.2520751953125);	// Center on Cape Cod
		zoomDelta = 9;
	}

	gMapBounds.extend(point);
	map.setZoom(map.getBoundsZoomLevel(gMapBounds) - zoomDelta);
	map.setCenter(gMapBounds.getCenter());
}

// *********************************************************************************************************************************
function utFocusOnField(elementID)
{
	textField = document.getElementById(elementID);
	if (textField != null)
		textField.focus();

	return;
}

// *********************************************************************************************************************************
function utEditFieldHasText(fieldID)
{
	hasText = false;
	editField = document.getElementById(fieldID);
	if (editField != null) {
		hasText = (editField.value.length > 0) ? true : false;
	}
	return hasText;
}

// *********************************************************************************************************************************
function utEditFieldHasIntegerValue(fieldID)
{
	hasText = false;
	editField = document.getElementById(fieldID);
	if ((editField != null) && (editField.value.length > 0)) {
		hasText = (parseInt(editField.value) > 0) ? true : false;
	}
	return hasText;
}

// *********************************************************************************************************************************
function utEditFieldHasFloatValue(fieldID)
{
	hasText = false;
	editField = document.getElementById(fieldID);
	if ((editField != null) && (editField.value.length > 0)) {
		hasText = (parseFloat(editField.value) > 0) ? true : false;
	}
	return hasText;
}

// *********************************************************************************************************************************
function utPopUpHasValue(popUpID)
{
	hasValue = false;
	popUp = document.getElementById(popUpID);
	if (popUp != null) {
		hasValue = (popUp.value != '') ? true : false;
	}
	return hasValue;
}

// *********************************************************************************************************************************
function utRadioBtnOn(radioBtnID)
{
	isOn = false;
	radioBtn = document.getElementById(radioBtnID);
	if (radioBtn != null) {
		isOn = radioBtn.checked;
	}
	return isOn;
}

// *********************************************************************************************************************************
function utCheckBoxOn(checkBoxID)
{
	isChecked = false;
	checkBox = document.getElementById(checkBoxID);
	if (checkBox != null) {
		isChecked = checkBox.checked;
	}
	return isChecked;
}

// *********************************************************************************************************************************
function utShowHideElement(elementID, showElement, displayType)
{
	if (typeof displayType == "undefined") {
		displayType = 'inline';
	}

	element = document.getElementById(elementID);
	if (element != null) {
		element.style.display = (showElement) ? displayType : "none";
	}
	return;
}


// *********************************************************************************************************************************
function utEnableElement(elementID, enableElement)
{
	element = document.getElementById(elementID);
	if (element != null) {
		element.disabled = ! enableElement;
	}
	return;
}

// *********************************************************************************************************************************
function utSetInnerHTML(elementID, html)
{
	theElement = document.getElementById(elementID);
	if (theElement != null) {
		theElement.innerHTML = html;
	}
	return;
}

// *********************************************************************************************************************************
function utGetFloat(fieldID)
{
	editField = document.getElementById(fieldID);
	if (editField != null) {
		return parseFloat(editField.value);
	}
	return "";
}

// *********************************************************************************************************************************
function utGetValue(fieldID)
{
	editField = document.getElementById(fieldID);
	if (editField != null) {
		return editField.value;
	}
	return "";
}

// *********************************************************************************************************************************
function utSetValue(elementID, value)
{
	theElement = document.getElementById(elementID);
	if (theElement != null) {
		theElement.value = value;
	}
	return;
}

// *********************************************************************************************************************************
// utConfirmLogOut
// 
function utConfirmLogOut(destination)
{
	if ((!qConfirmLogouts) || confirm("Are you sure you wish to log out?")) {
		utRedirect(destination);
	}
}

// *********************************************************************************************************************************
function utPopup(theURL, theWidth, theHeight)
{
	var winSize = "width=" + theWidth + ",height=" + theHeight + ",resizable=yes";

	window.open(theURL, "", winSize);
}


// *********************************************************************************************************************************
function utLeft(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

// *********************************************************************************************************************************
function utRight(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

// *********************************************************************************************************************************
function utAddCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = func;
	} 
	else 
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

//
// addUnloadEvent()
// Adds event to window.onunload without overwriting currently assigned onunload functions.
//
function addUnloadEvent(func)
{	
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function')
	{
    	window.onunload = func;
	} 
	else 
	{
		window.onunload = function()
		{
			oldonunload();
			func();
		}
	}
}

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/, "");
}

// Define methods for the Array data structure.
Array.prototype.indexOf = function(item, start) 
{ 
	for (var i = (start || 0); i < this.length; i++) 
	{ 
		if (this[i] == item) 
		{ 
			return i; 
		} 
	} 
	return -1; 
}

function getElementsByClass(searchClass, node, tagName)
{
	var	classElements =	new	Array();
	if (node == null)
		node = document;
	if (tagName ==	null)
		tagName	= '*';
	
	var	els	= node.getElementsByTagName(tagName);
	var	elsLen = els.length;
	
	for	(i = 0,	j =	0; i < elsLen; i++)
	{
		if (hasClass(els[i], searchClass))
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getClasses(element)
{
	return element.className.trim().split(/\s+/);
}

function hasClass(element, className)
{
	return getClasses(element).indexOf(className) != -1;
}

function addClass(element, className)
{
    var classes = getClasses(element);
    
    if (classes.indexOf(className) == -1)
    {
        classes.push(className);
        element.className = classes.join(' ');
    }
}

function removeClass(element, className)
{
    var classes = getClasses(element);
    var index = classes.indexOf(className);
    
    if (index != -1)
    {
        classes.splice(index, 1);
        element.className = classes.join(' ');
    }
}

function initRowHighlighting(tableClass, highlightClass)
{
	if (!document.getElementsByTagName)
		{ return; }

	var tables = getElementsByClass('highlightTable', document, 'table');

	for(var i = 0; i < tables.length; i++)
	{
		var table = tables[i];
		//Make sure to use th tags for header row.
		attachRowMouseEvents(table.getElementsByTagName('tr'), 'highlight');
	}
}

function attachRowMouseEvents(rows, highlightClass)
{
	for(var i = 0; i < rows.length; i++)
	{
		var row = rows[i];

		if (! hasClass(row, 'noHighlight')) {
			row.onmouseover = function() { addClass(this, highlightClass); }
			row.onmouseout = function() { removeClass(this, highlightClass); }
		}
	}
}


function initCellHighlighting(tableClass, highlightClass)
{
	if (!document.getElementsByTagName)
		{ return; }

	var tables = getElementsByClass('highlightCellTable', document, 'table');

	for(var i = 0; i < tables.length; i++)
	{
		var table = tables[i];
		//Make sure to use th tags for header row.
		attachRowMouseEvents(table.getElementsByTagName('td'), 'highlight');
	}
}

function attachRowMouseEvents(rows, highlightClass)
{
	for(var i = 0; i < rows.length; i++)
	{
		var row = rows[i];

		if (! hasClass(row, 'noHighlight')) {
			row.onmouseover = function() { addClass(this, highlightClass); }
			row.onmouseout = function() { removeClass(this, highlightClass); }
		}
	}
}


// caHover function enables drop down menus in IE 6 - all modern browsers do not need this (IE 7, FF, Safari)
caHover = function() {
	if (document.getElementById("nav") != null) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" caHover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" caHover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", caHover);

function utIsIE6()
{
	if (document.all && (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1))
		return true;
	else
		return false;
}


// *********************************************************************************************************************************
// global variables //
var TIMER = 5;
var SPEED = 100;
var WRAPPER = 'wrapper';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }

  if (utIsIE6()) {
	SelectFix.repairFloatingElement(dialogmask);
  }
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  if(!dialog.timer) {
    dialog.timer = setInterval("fadeDialog(0)", TIMER);
  }
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}

function utHideDialog()
{
	hideDialog();
}

function utPleaseWaitRedirect(destination, message)
{
	addUnloadEvent(utHideDialog);

	if ((typeof message == "undefined") || (message == '')) {
		message = 'Please wait.<br /><br />This may take a few moments...<br /><br /><img src="../img/loading.gif" height="32" width="32" />';
	}
	showDialog('', message, 'pleaseWaitMessage');

	if (destination != '') {
		utRedirect(destination);
	}
	return true;
}

// *********************************************************************************************************************************




addLoadEvent(initRowHighlighting);



function SearchParamChange()
{
	var propertyID = utGetValue('id');
	var location = utGetValue('l');
	var bedrooms = utGetValue('b');
	var sleeps = utGetValue('s');
	
	if (propertyID != '') {
		html = 'Click Search to show the detail for the proprerty with ID '+ propertyID +'.';
	}
	else {
		if (location == 'Any')
			locationStr = 'Any Location';
		else
			locationStr = location;
		if ((location == 'Any') && (bedrooms == 'Any') && (sleeps == 'Any')) {
			html = 'Click Search to show <b>all</b> available rental proprerties.';
		}
		else {
			if (bedrooms != 'Any') {
				if (bedrooms == 'Studio')
					html = 'Click Search to show <b>Studios</b> in <b>'+ locationStr +'</b>';
				else
					html = 'Click Search to show <b>'+ bedrooms +' bedrooms</b> in <b>'+ locationStr +'</b>';
			}
			else if (sleeps != 'Any') {
				html = 'Click Search to show properties <b>sleeping '+ sleeps +'</b> in <b>'+ locationStr +'</b>';
			}
			else {
				html = 'Click Search to show <b>all</b> properties in <b>'+ locationStr +'</b>';
			}
		}
	}
	
	html = '<i>'+ html +'</i>';
	utSetInnerHTML('searchtext', html);
	return;
}
	
addLoadEvent(SearchParamChange);
