﻿// Flag Variables
var bForceUnLoadPostback = false;
var isCCS, isIE6, isNN7;

// Watch Dog
var hWatchDog        = null;
var bWatchDogStarted = false;

var iRotateDelay     = 30000;
var pVSListings      = null;

//****************************************************************************
// Method to determine the type of browser
function initDHTMLAPI()
{
  if (document.images)
  {
    isCCS = (document.body && document.body.style) ? true : false;
    isIE6 = (isCCS && document.all) ? true : false;
    isNN7 = (isIE6) ? false : true;
  }
}

//*****************************************************************************
// Called when the page is loaded
function OnLoad()
{
  initDHTMLAPI();
  
  if (document.getElementById('SubmitContact') != null)
  {
    if (document.getElementById('SubmitContact').attributes['EmailSent'] != null)
      alert(document.getElementById('SubmitContact').getAttribute('EmailSent')); 
  }
  
  // Load the search forms
  if (document.getElementById('PriceMin') != null)
  {
    LoadPriceBoxes();
    LoadPropertyTypes();
  }
  
  // If we are on the Homepage rotate the listings
  if ((typeof(Homekeys) != 'undefined') && (typeof(Homekeys.Default) != 'undefined'))
    setTimeout('RotateListings()', 0);
}

//*****************************************************************************
// Called when the form is unloading
function OnBeforeUnLoad()
{
  try
  {
    SetCursor('wait');
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to change the cursor on the form
function SetCursor(sCursor)
{
  try
  {
    if (document.getElementById('SubmitContact') != null)
      document.getElementById('SubmitContact').style.cursor = sCursor;
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to redirect the browser and track the click
function TrackClick(sUrl, sTrackingString)
{
  try
  {
    if (typeof(pageTracker) != 'undefined')
      pageTracker._trackPageview(sTrackingString);
    
    document.location.href = sUrl;
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called when a key stroke is press in the specified text box
function ValidateKeyPress(evt, oTextBox, sType, iLength)
{
  try
  {
    var sCode = (isIE6 == true) ? evt.keyCode : evt.charCode;
    
    // Get the key that was pressed
    var sChar = String.fromCharCode(sCode);
    
    if ((typeof(sType) == 'undefined') || (sType == 'Numeric'))
    {
      // If this is not a number do not allow it
      if ((sCode > 0) && (isNaN(parseInt(sChar)) == true))
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
    }
    else if (sType == 'Currency')
    {
      // If this is not a number and not a '.'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '.')))
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
    }
    else if (sType == 'Date')
    {
      // If this is not a number and not a '/'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '/')))
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
    }
    else if (sType == 'Phone')
    {
      // If this is not a number and not a '( ) or space'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '(') && (sChar != ')') && (sChar != ' ') && (sChar != '-')))
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
    }
    else if (sType == 'CurrencyNeg')
    {
      // If this is not a number and not a '.'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '.') && (sChar != '-')))
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
    }
    else if (sType == 'SingleDecimal')
    {
      var RxPattern = /^\d+\.{0,1}(\d{1})?$/;
      if (oTextBox.value.length > 0)
      {
        // Create Regular expression object
        var oRegEx = new RegExp(RxPattern);
        oRegEx.Global = true

        // Test input field with regular expression
        var result = oRegEx.exec(oTextBox.value + sChar);
        if (result == null)
          if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
      }
    }

    if (typeof(iLength) != 'undefined')
    {
      if (document.selection.type == 'None')
        if (oTextBox.value.length >= iLength)
          if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
    }
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to validate email address  
function ValidateEmail(oTextBox)
{
  var bRetVal = false;
  
  // Email Regex object - email@some.com
  var RxPattern = /([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})/;
  
  if (oTextBox.value.length > 0 )
  {
    // Create Regular expression object
    var oRegEx = new RegExp(RxPattern)
    
    // Test input field with regular expression
    var oResult = oRegEx.exec(oTextBox.value)
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Romoves leading and trailing spaces on a string.
// 's' represents any input string.
function TrimString(s) 
{
  // Remove leading spaces and carriage returns
  while ((s.substring(0, 1) == ' ')  || 
        (s.substring(0, 1) == '\n') || 
        (s.substring(0, 1) == '\r'))
  {
    s = s.substring(1, s.length);
  }

  // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1, s.length) == ' ')  || 
        (s.substring(s.length-1, s.length) == '\n') || 
        (s.substring(s.length-1, s.length) == '\r'))
  {
    s = s.substring(0, s.length-1);
  }
  
  return s;
}

//*****************************************************************************
// Add favorites
function AddToFavorites()
{
  if (isIE6 == true)
    window.external.Addfavorite(window.location.href, document.title);
  else
  {
    var title = document.title;
    var url   = window.location.href;

    if (window.sidebar) window.sidebar.addPanel(title, url, "");

    else if (window.opera && window.print)
    {
      var mbm = document.createElement('a');
      mbm.setAttribute('rel', 'sidebar');
      mbm.setAttribute('href', url);
      mbm.setAttribute('title', title);
      mbm.click();
    }
  }
}



//=============================================================================
// SECTION: Ajax Support

//*****************************************************************************
// When the watchdog thread times out we need to reset everything
function WatchDog()
{
  hWatchDog = null;
  FinishAjaxCall();
}

//*****************************************************************************
// Provides the current state of the application
function IsWaitState()
{
  return bWatchDogStarted;
}

//*****************************************************************************
// Called when a ajax call is made
function StartAjaxCall()
{
  var bStart = false;
  
  try
  {
    if (bWatchDogStarted == false)
    {
      bWatchDogStarted = true;
      
      // Change everything to the wait cursor
      SetCursor('wait');
      
      // Start the watchdog thread for 15 seconds
      hWatchDog = setTimeout("WatchDog()", iRotateDelay);
      
      // Flag that we started
      bStart = true;
    }
  }
  
  catch (e)
  {
  }
  
  return bStart;
}

//*****************************************************************************
// Called when a ajax call is finished
function FinishAjaxCall()
{
  try
  {
    // Reset the watchdog flag
    bWatchDogStarted = false;
    
    // Stop the watchdog
    if (hWatchDog != null)
    {
      clearTimeout(hWatchDog);
      hWatchDog = null;
    }
    
    // Reset the cursor
    SetCursor('pointer');
  }
  
  catch (e)
  {
  }
}

//=============================================================================
// SECTION: Search Support

//*****************************************************************************
// Called to perform a marketing search
function OnSearchListings()
{
  try
  {
    var oSearchValue    = document.getElementById('SearchValue');
    var oSearchTypeList = document.getElementById('SearchTypeList');
    var oType           = document.getElementById('Type');
    var oPriceMin       = document.getElementById('PriceMin');
    var oPriceMax       = document.getElementById('PriceMax');
    var oBedsMin        = document.getElementById('BedsMin');
    var oBathsMin       = document.getElementById('BathsMin');
    
    var sSearchValue = TrimString(oSearchValue.value);
    if (sSearchValue.length > 0)
    { 
      var sQueryString = '?Find=' + sSearchValue;

      if (oPriceMin.value != '0')
        sQueryString += '&PriceMin=' + oPriceMin.value;
      
      if (oPriceMax.value != '0')
        sQueryString += '&PriceMax=' + oPriceMax.value;
        
      sQueryString += '&Type='     + oType.value;
      sQueryString += '&BedsMin='  + oBedsMin.value;
      sQueryString += '&BathsMin=' + oBathsMin.value;
      
      if (oSearchTypeList.value == 'Rentals')
        sQueryString += '&ListingType=' + oSearchTypeList.value;
        
      top.location.href = oSearchValue.getAttribute('search') + sQueryString;
    }
    else
    {
      top.location.href = oSearchValue.getAttribute('search');
    }
  }
  
  catch (e)
  {
  }
}

//=============================================================================
// SECTION: Input Form Support

//*****************************************************************************
// Called to submit the contact us form
function SubmitContactUsForm()
{
  var bRetVal = true;
  
  try
  {
    var oObject = document.getElementById('FirstName');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
    
    var oObject = document.getElementById('LastName');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
    
    var oObject = document.getElementById('Email');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
    {
      if (ValidateEmail(oObject) == false)
      {
        bRetVal = false;
        oObject.attributes['class'].value = 'textboxerrorstyle1';
      }
      else
        oObject.attributes['class'].value = 'textboxstyle1';
    }
    
    var oObject = document.getElementById('Phone');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
    
    var oObject = document.getElementById('BestTime');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
  }
  
  catch (e)
  {
    bRetVal = false;
  }
  
  if (bRetVal == false)
    alert('Please provide the missing fields in red.');
  
  return bRetVal;
}

//*****************************************************************************
// Called to submit the career form
function SubmitCareerForm()
{
  var bRetVal = true;
  
  try
  {
    var oObject = document.getElementById('Name');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
    
    var oObject = document.getElementById('Email');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
    {
      if (ValidateEmail(oObject) == false)
      {
        bRetVal = false;
        oObject.attributes['class'].value = 'textboxerrorstyle1';
      }
      else
        oObject.attributes['class'].value = 'textboxstyle1';
    }
  }
  
  catch (e)
  {
    bRetVal = false;
  }
  
  if (bRetVal == false)
    alert('Please provide the missing fields in red.');
  
  return bRetVal;
}

//*****************************************************************************
// Called to submit the mortgage form
function SubmitMortgageForm()
{
  var bRetVal = true;
  
  try
  {
    var oObject = document.getElementById('Purpose');
    if (oObject.selectedIndex == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
      
    var oObject = document.getElementById('FirstName');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
    
    var oObject = document.getElementById('LastName');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
    
    var oObject = document.getElementById('Email');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
    {
      if (ValidateEmail(oObject) == false)
      {
        bRetVal = false;
        oObject.attributes['class'].value = 'textboxerrorstyle1';
      }
      else
        oObject.attributes['class'].value = 'textboxstyle1';
    }
    
    var oObject = document.getElementById('Phone');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
    
    var oObject = document.getElementById('BestTime');
    var sTest = TrimString(oObject.value);
    if (sTest.length == 0)
    {
      bRetVal = false;
      oObject.attributes['class'].value = 'textboxerrorstyle1';
    }
    else
      oObject.attributes['class'].value = 'textboxstyle1';
  }
  
  catch (e)
  {
    bRetVal = false;
  }
  
  if (bRetVal == false)
    alert('Please provide the missing fields in red.');
  
  return bRetVal;
}

//*****************************************************************************
// Called to load the video
function ShowVideo(sFile)
{
  try
  {
    var sVideo  = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,16,0" width="430" height="400" id="' + sFile + '" align="middle">\n';
        sVideo += '<param name="base" value="http://www.homekeys.net/videos/" />\n';
        sVideo += '<param name="allowScriptAccess" value="Always" />\n';
        sVideo += '<param name="movie" value="http://www.homekeys.net/videos/' + sFile + '" />\n';
        sVideo += '<param name="quality" value="high" />\n';
        sVideo += '<embed src="http://www.homekeys.net/videos/' + sFile + '" quality="high" bgcolor="#ffffff" width="430" height="400" name="' + sFile + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n';
        sVideo += '</object>\n';

    Video.innerHTML = sVideo;
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to open the panel
function OnShowDetailMessaging(sPanel)
{
  try
  {
    __doPostBack('OpenPanel', sPanel);
  }
    
  catch (e)
  {
  }
}

//*****************************************************************************
// Function to remove any $, % and , from the amount string
function GetUnformattedNumber(sAmount)
{
  try
  {
    if (sAmount.length > 0)
    {
      sAmount = sAmount.replace('%', '');
      sAmount = sAmount.replace('$', '');
      sAmount = sAmount.replace('/', '');
      sAmount = sAmount.replace('/', '');
      sAmount = sAmount.replace(',', '');
      sAmount = sAmount.replace(',', '');
      sAmount = sAmount.replace(',', '');
      sAmount = sAmount.replace(',', '');
      sAmount = sAmount.replace(',', '');
      sAmount = sAmount.replace('-', '');
      sAmount = sAmount.replace('-', '');
    }
  }
  
  catch (e)
  {
  }
    
  return sAmount;
}

//*****************************************************************************
// Method to add currency comma formaing
function CommaFormatAmount(Amount, bAddEvenDecimals)
{
  var sNewAmount = "";
  
  try
  {
	  // Private Variables
	  var sDelimiter                  = ','; 
	  var sNegativeNumber             = '';
    var aAmount                     = Amount.split('.',2);
	  var iWholeNumberPortionOfAmount = parseInt(aAmount[0]);  
    var iDecimalPortionOfAmount     = aAmount[1];
    var iABSWholePortionOfAmount    = Math.abs(iWholeNumberPortionOfAmount);
    var sWholePortionOfAmount       = new String(iABSWholePortionOfAmount);
    var aFormattedArray             = new Array();
    
    // Set negative sign if required 
    if (iWholeNumberPortionOfAmount < 0) 
		  sNegativeNumber = '-'; 
  	
	  // Parse string and populate array
	  while (sWholePortionOfAmount.length > 3)
	  {
		  var sNewSubstring = sWholePortionOfAmount.substr((sWholePortionOfAmount.length) - 3);
		  aFormattedArray.unshift(sNewSubstring);
		  sWholePortionOfAmount = sWholePortionOfAmount.substr(0,(sWholePortionOfAmount.length) - 3);
	  }
  	
	  // Get the last part of the number in the array
	  if (sWholePortionOfAmount.length > 0) 
		  aFormattedArray.unshift(sWholePortionOfAmount); 
  	
	  // Add the commas
	  sWholePortionOfAmount = aFormattedArray.join(sDelimiter);
  	
	  // Add decimal portion if required
	  if (aAmount.length == 1)
	    if (bAddEvenDecimals == true)
		    sNewAmount = sWholePortionOfAmount + '.00';
		  else
		    sNewAmount = sWholePortionOfAmount;
	  else 
	  { 
	    if (iDecimalPortionOfAmount.length == 1)
	      sNewAmount = sWholePortionOfAmount + '.' + iDecimalPortionOfAmount + '0';
		  else if (iDecimalPortionOfAmount.length == 2)
		    sNewAmount = sWholePortionOfAmount + '.' + iDecimalPortionOfAmount; 
		  else
		  {
		    var sFraction = iDecimalPortionOfAmount.substring(0, 3);
		    var iFraction = new Number(sFraction);
		    iFraction = (Math.round(iFraction / 10)) * 10;
  		  
		    sNewAmount = sWholePortionOfAmount + '.' + iFraction.toString().substring(0, 2);
		  }
	  }
  	
	  // Add negative sign if required
	  sNewAmount = sNegativeNumber + sNewAmount;
	}
	
	catch (e)
	{
	}
	
	return sNewAmount;
}

//*****************************************************************************
// Method to format the specified textbox to currency
function FormatCurrency(sNumber, bAddEvenDecimals)
{
  return '$' + CommaFormatAmount(GetUnformattedNumber(sNumber), bAddEvenDecimals);
}

//*****************************************************************************
// Called to load the price boxes
function LoadPriceBoxes()
{
  try
  {
    if (document.getElementById('SearchTypeList').value == 'Listings')
      LoadPriceListings();
    else
      LoadPriceRentals();
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to load the pricing for listings
function LoadPriceListings()
{
  try
  {
    var oPriceMin = document.getElementById('PriceMin');
    var oPriceMax = document.getElementById('PriceMax');
    
    // Clear the current list
    var iLength = oPriceMin.options.length;
    for (var iOption = 0; iOption < iLength; ++iOption)
      oPriceMin.removeChild(oPriceMin.options[0])
    
    // Clear the current list
    var iLength = oPriceMax.options.length;
    for (var iOption = 0; iOption < iLength; ++iOption)
      oPriceMax.removeChild(oPriceMax.options[0])
      
    // Add the min
    var oNewOption = document.createElement('option');
    oNewOption.value = '0';
    oNewOption.innerHTML = 'No Minimum';
    oPriceMin.appendChild(oNewOption);
    
    // Add the min
    var oNewOption = document.createElement('option');
    oNewOption.value = '0';
    oNewOption.innerHTML = 'No Maximum';
    oPriceMax.appendChild(oNewOption);
    
    var iAmount = 100000;
    for (var iOption = 0; iOption < 19; ++iOption)
    {
      var oNewOption = document.createElement('option');
      oNewOption.value = iAmount.toString();
      oNewOption.innerHTML = FormatCurrency(oNewOption.value);
      oPriceMin.appendChild(oNewOption);
      
      iAmount += 50000;
    }
    
    var iAmount = 100000;
    for (var iOption = 0; iOption < 19; ++iOption)
    {
      var oNewOption = document.createElement('option');
      oNewOption.value = iAmount.toString();
      oNewOption.innerHTML = FormatCurrency(oNewOption.value);
      oPriceMax.appendChild(oNewOption);
      
      iAmount += 50000;
    }
    
    // Add the last
    var oNewOption = document.createElement('option');
    oNewOption.value = '1500000';
    oNewOption.innerHTML = '$1,500,000';
    oPriceMin.appendChild(oNewOption);
    
    // Add the last
    var oNewOption = document.createElement('option');
    oNewOption.value = '1500000';
    oNewOption.innerHTML = '$1,500,000';
    oPriceMax.appendChild(oNewOption);
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to load the pricing for rentals
function LoadPriceRentals()
{
  try
  {
    var oPriceMin = document.getElementById('PriceMin');
    var oPriceMax = document.getElementById('PriceMax');
    
    // Clear the current list
    var iLength = oPriceMin.options.length;
    for (var iOption = 0; iOption < iLength; ++iOption)
      oPriceMin.removeChild(oPriceMin.options[0])
    
    // Clear the current list
    var iLength = oPriceMax.options.length;
    for (var iOption = 0; iOption < iLength; ++iOption)
      oPriceMax.removeChild(oPriceMax.options[0])
      
    // Add the min
    var oNewOption = document.createElement('option');
    oNewOption.value = '0';
    oNewOption.innerHTML = 'No Minimum';
    oPriceMin.appendChild(oNewOption);
    
    // Add the min
    var oNewOption = document.createElement('option');
    oNewOption.value = '0';
    oNewOption.innerHTML = 'No Maximum';
    oPriceMax.appendChild(oNewOption);
    
    var iAmount = 200;
    for (var iOption = 0; iOption < 19; ++iOption)
    {
      var oNewOption = document.createElement('option');
      oNewOption.value = iAmount.toString();
      oNewOption.innerHTML = FormatCurrency(oNewOption.value);
      oPriceMin.appendChild(oNewOption);
      
      if (iAmount < 2000)
        iAmount += 200;
      else
        iAmount += 500;
    }
    
    var iAmount = 200;
    for (var iOption = 0; iOption < 19; ++iOption)
    {
      var oNewOption = document.createElement('option');
      oNewOption.value = iAmount.toString();
      oNewOption.innerHTML = FormatCurrency(oNewOption.value);
      oPriceMax.appendChild(oNewOption);
      
      if (iAmount < 2000)
        iAmount += 200;
      else
        iAmount += 500;
    }
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to load the property type
function LoadPropertyTypes()
{
  try
  {
    if (document.getElementById('SearchTypeList').value == 'Listings')
      LoadPropertyTypeListings();
    else
      LoadPropertyTypeRentals();
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to load the property type for listings
function LoadPropertyTypeListings()
{
  try
  {
    var oType = document.getElementById('Type');
    
    // Clear the current list
    var iLength = oType.options.length;
    for (var iOption = 0; iOption < iLength; ++iOption)
      oType.removeChild(oType.options[0])
      
    // Add the All
    var oNewOption = document.createElement('option');
    oNewOption.value = 'A';
    oNewOption.innerHTML = 'All';
    oType.appendChild(oNewOption);
    
    // Add the Single Family
    var oNewOption = document.createElement('option');
    oNewOption.value = 'S';
    oNewOption.innerHTML = 'Single Family';
    oType.appendChild(oNewOption);
    
    // Add the Condo/Town Home
    var oNewOption = document.createElement('option');
    oNewOption.value = 'M';
    oNewOption.innerHTML = 'Condo/Town Home';
    oType.appendChild(oNewOption);
    
    // Add the Multi-Family
    var oNewOption = document.createElement('option');
    oNewOption.value = 'D';
    oNewOption.innerHTML = 'Multi-Family';
    oType.appendChild(oNewOption);
    
    // Add the Mobile Home
    var oNewOption = document.createElement('option');
    oNewOption.value = 'B';
    oNewOption.innerHTML = 'Mobile Home';
    oType.appendChild(oNewOption);
    
    // Add the Vacant Land
    var oNewOption = document.createElement('option');
    oNewOption.value = 'V';
    oNewOption.innerHTML = 'Vacant Land';
    oType.appendChild(oNewOption);
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to load the property type for rentals
function LoadPropertyTypeRentals()
{
  try
  {
    var oType = document.getElementById('Type');
    
    // Clear the current list
    var iLength = oType.options.length;
    for (var iOption = 0; iOption < iLength; ++iOption)
      oType.removeChild(oType.options[0])
      
    // Add the Single Family
    var oNewOption = document.createElement('option');
    oNewOption.value = 'S';
    oNewOption.innerHTML = 'Single Family';
    oType.appendChild(oNewOption);
    
    // Add the Condo/Town Home
    var oNewOption = document.createElement('option');
    oNewOption.value = 'M';
    oNewOption.innerHTML = 'Condo/Town Home';
    oType.appendChild(oNewOption);
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to rotate the listings
function RotateListings()
{
  try
  {
    if (StartAjaxCall())
    {
      Homekeys.Default.RetrieveTopVSDeals(RetrieveTopVSDeals_CallBack)
    }
  }
  
  catch (e)
  {
    FinishAjaxCall();
  }
}

//****************************************************************************
// Called after the new listings are returned
function RetrieveTopVSDeals_CallBack(sResponse)
{
  try
  {
    FinishAjaxCall();
    
    if (sResponse.value != null)
    {
      pVSListings = sResponse.value;
      
      setTimeout("LoadVSListings(0)", 0);
    }
    else
      setTimeout('RotateListings()', iRotateDelay);
  }
  
  catch (e)
  {
  }
}

//****************************************************************************
// Called to load the VS listings
function LoadVSListings(iListing)
{
  try
  {
    // Capture the correct listing
    var oImage = document.getElementById('ListImage'  + iListing.toString());
    var oAnchr = document.getElementById('ListAnchor' + iListing.toString());
    var oValue = document.getElementById('ListValue'  + iListing.toString());
    var oCity  = document.getElementById('ListCity'   + iListing.toString());
    var oZip   = document.getElementById('ListZip'    + iListing.toString());

    // Build the link
    var sCounty    = (pVSListings.Rows[iListing].County != null)       ? pVSListings.Rows[iListing].County.toString().replace(/\s/gi, '_')  : '';
    var sCity      = (pVSListings.Rows[iListing].City != null)         ? pVSListings.Rows[iListing].City.toString().replace(/\s/gi, '_')    : '';
    var sZipCode   = (pVSListings.Rows[iListing].ZipCode != null)      ? pVSListings.Rows[iListing].ZipCode.toString()                      : '';
    var sAddress   = (pVSListings.Rows[iListing].Address != null)      ? pVSListings.Rows[iListing].Address.toString().replace(/'&'/gi, '').replace(/'-'/gi, '_').replace(/\s/gi, '_') : '';
    var sListingId = (pVSListings.Rows[iListing].MLSListingId != null) ? pVSListings.Rows[iListing].MLSListingId.toString()                 : '';
    var sLink      = 'HomesForSale/Listings-Florida-' + sCounty + '-' + sCity + '-' + sZipCode + '-' + sAddress + '-' + sListingId + '.aspx';
    
    // Change the image
    oImage.src  = pVSListings.Rows[iListing].PhotoURL.toString();
    oAnchr.href = sLink;

    // Change the value    
    var dVSPercent = parseFloat(pVSListings.Rows[iListing].VSPercent.toFixed(0));
    oValue.innerHTML = '<b style="color: green">Below Value: ' + dVSPercent.toString() + '%</b>';
    
    // Show the city
    oCity.innerHTML = pVSListings.Rows[iListing].City + ',';
    if (oCity.innerHTML.length > 16)
      oCity.innerHTML = oCity.innerHTML.substr(0, 15) + ',';
    
    // Show the zip
    oZip.innerHTML  = pVSListings.Rows[iListing].ZipCode;
    
    if (iListing < 5)
    {
      // Load the next listing      
      ++iListing;
      setTimeout("LoadVSListings('" + iListing.toString() + "')", 250);
    }
    else
      setTimeout('RotateListings()', iRotateDelay);
  }
  
  catch (e)
  {
    setTimeout('RotateListings()', 15000);
  }
}

//*****************************************************************************
// Called to redirect the user to the purchase page
function PurchaseOnLoad(bNow)
{
  try
  {
    if (document.getElementById('Redirected').value == '0')
    {
      if ((typeof(bNow) != 'undefined') && (bNow == true))
        setTimeout('DoLoadPurchase()', 0);
      else
        setTimeout('DoLoadPurchase()', 2000);
    }
    else
      history.back();
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to redirect the user to the purchase page
function DoLoadPurchase()
{
  try
  {
    document.getElementById('Redirected').value = '1';
    
    var sCode = document.getElementById('HeaderTable').getAttribute('Code');
    if (sCode != null)
      document.location.href = document.getElementById('HeaderTable').getAttribute('Purchase') + '/Purchase.aspx?Code=' + sCode;
    else
      document.location.href = document.getElementById('HeaderTable').getAttribute('Purchase') + '/Purchase.aspx';
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to show the login panel
function OnLogin()
{
  try
  {
    var oLogin = document.getElementById('LoginPanel');
    if (oLogin.style.display == '')
      oLogin.style.display = 'none';
    else
      oLogin.style.display = '';
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to show the news item
function ShowNewsItem(sLink, iHeight, iWidth)
{
  try
  {
    if (iHeight == 0)
    {
      iHeight = 700;
      iWidth  = 900;
    }

    var sAtt    = 'resizable=yes, scrollbars=yes, toolbar=yes, width=' + iWidth + ', height=' + iHeight + ', top=0 left=0';
    var iNumber = parseInt(Math.random() * 1000000);
    
    var oWindow = window.open(sLink, iNumber.toString(), sAtt);
    if (oWindow == null)
      alert('Popup Blocker is preventing you from bringing up the listing details. Please turn off all popup blockers for this site.');
    else
      oWindow.focus();
  }
  
  catch (e)
  {
  }
}