//=====================================================================||
//            CBC RealEstate Property Search Include                   ||
//                                                                     ||
// This JS should be included on all search pages, and contains        ||
// functions that are required by the property search.                 ||
//                                                                     ||
//=====================================================================||


//---------------------------------------------------------------------||
//                       Global Variables                              ||
//---------------------------------------------------------------------||
var g_iSearchTipPosLeft = 0;
var g_iSearchTipPosRight = 0;
var g_iSearchTipPosTop = 0;
var g_iSearchTipPosBottom = 0;
var g_bSearchTipCancelCloseRequest = false;
var g_bSearchTipTimeoutActive = false;
var g_bSearchTipVisible = false;
var g_bSearchTipCurrentId = "";
var g_strSaveListingAddMsg = "(Add to Saved Listings)";
var g_strSaveListingRemMsg = "(Remove from Saved Listings)";
var g_strCompareListingAddMsg = "";//Disabled "(Add to Compare Listings)";
var g_strCompareListingRemMsg = "";//Disabled "(Remove from Compare Listings)";
var g_iTotalCompareProperties = 3;


//---------------------------------------------------------------------||
// FUNCTION:    CbcSearchMapPushPinMouseOver                           ||
// PARAMETERS:  Pushpin X, Y, title, details -- we stuff the title with||
//              the listing div id for use with this function.         ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Handle mouseover event on search map                   ||
//---------------------------------------------------------------------||
function CbcSearchMapPushPinMouseOver( x, y, title, details ) 
{
    ShowSearchTooltip(unescape(title));
}

//---------------------------------------------------------------------||
// FUNCTION:    ShowSearchTooltip                                      ||
// PARAMETERS:  ID of search tool tip data                             ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Displays a property search hover tool-tip over the map ||
//---------------------------------------------------------------------||
function ShowSearchTooltip( strObjectIdWithData )
{
   if ( g_bSearchTipVisible ) {
      if ( g_bSearchTipCurrentId == strObjectIdWithData ) {
         return;
      }
      else {
         HideSearchToolTip();
      }
   }
   g_bSearchTipVisible = true;
   g_bSearchTipCancelCloseRequest = true;
   g_bSearchTipTimeoutActive = true;

   var intX = CyberGetMouseXPosition(event);
   var intY = CyberGetMouseYPosition(event);
   var pObject = document.getElementById( strObjectIdWithData );

   var pSearchToolTip = document.getElementById( 'searchtooltip' );
   if ( pSearchToolTip == null ) {
      pSearchToolTip = document.createElement("div");
      pSearchToolTip.id = 'searchtooltip';
      pSearchToolTip.style.position= 'absolute';
      pSearchToolTip.style.display = 'none';
      pSearchToolTip.style.border = "1px solid windowframe";
      pSearchToolTip.style.background = '#FFFFEA';
      pSearchToolTip.style.zIndex = 99;
      document.body.appendChild(pSearchToolTip);
   } else {
      if( pSearchToolTip.strLastObjectBinding ) { 
         cbcResUnHighlight(pSearchToolTip.strLastObjectBinding);
      }
   }
   pSearchToolTip.strLastObjectBinding = strObjectIdWithData;
   pSearchToolTip.innerHTML = '<table border=0><tr>' + pObject.innerHTML + '</tr></table>';

   CyberCoreSetObjectPosition(pSearchToolTip, intX - 20, intY - 20);
   pSearchToolTip.style.display = 'block';

   cbcResHighlight( strObjectIdWithData );
   setTimeout( BeginShowSearchTip, 400 );  
}


//---------------------------------------------------------------------||
// FUNCTION:   TrackSearchToolTip                                      ||
// PARAMETERS:                                                         ||
// RETURNS:                                                            ||
// PURPOSE:    Tracks mouse movement for determining mouse position    ||
//---------------------------------------------------------------------||
function TrackSearchToolTip( event )
{
   var pSearchToolTip = document.getElementById( 'searchtooltip' );
   if ( pSearchToolTip != null ) {

      var tempX = CyberGetMouseXPosition(event);
      var tempY = CyberGetMouseYPosition(event);

      if( tempX < g_iSearchTipPosLeft || tempX > g_iSearchTipPosRight ){            
         if( ! g_bSearchTipTimeoutActive ) {
            g_bSearchTipCancelCloseRequest = false;
            setTimeout( RequestHideSearchToolTip, 400 );
            g_bSearchTipTimeoutActive = true;
         }            
         return;
      }

      if( tempY < g_iSearchTipPosTop || tempY > g_iSearchTipPosBottom ){            
         if( ! g_bSearchTipTimeoutActive ) {
            g_bSearchTipCancelCloseRequest = false;
            setTimeout( RequestHideSearchToolTip, 400 );
            g_bSearchTipTimeoutActive = true;
         }
         return;
      }

      g_bSearchTipCancelCloseRequest = true;
   }
}


//---------------------------------------------------------------------||
// FUNCTION:   RequestHideSearchToolTip                                ||
// PARAMETERS:                                                         ||
// RETURNS:                                                            ||
// PURPOSE:    Requests the interruptable menu hide timer be started   ||
//---------------------------------------------------------------------||
function RequestHideSearchToolTip()
{  
   var pSearchToolTip = document.getElementById( 'searchtooltip' );
   if ( pSearchToolTip != null ) {     
      g_bSearchTipTimeoutActive = false;
      if( ! g_bSearchTipCancelCloseRequest ) {
         HideSearchToolTip();
      }
   }
}


//---------------------------------------------------------------------||
// FUNCTION:   HideSearchToolTip                                       ||
// PARAMETERS:                                                         ||
// RETURNS:                                                            ||
// PURPOSE:    Hides the search tool tip                               ||
//---------------------------------------------------------------------||
function HideSearchToolTip()
{  
   var pSearchToolTip = document.getElementById( 'searchtooltip' );
   if ( pSearchToolTip != null ) {
      CyberCoreRemoveEvent("onmousemove", document, TrackSearchToolTip );
      pSearchToolTip.style.display = 'none';
      g_bSearchTipVisible = false;
      g_bSearchTipCurrentId = "";
      if( pSearchToolTip.strLastObjectBinding ) { 
         cbcResUnHighlight(pSearchToolTip.strLastObjectBinding);
      }
   }
}


//---------------------------------------------------------------------||
// FUNCTION:   BeginShowSearchTip                                      ||
// PARAMETERS:                                                         ||
// RETURNS:                                                            ||
// PURPOSE:    Begins display of search tip, after block has been set  ||
//---------------------------------------------------------------------||
function BeginShowSearchTip()
{
   // this function ensures enough time has passed for the block to render before reading the size
   var pSearchToolTip = document.getElementById( 'searchtooltip' );
   if ( pSearchToolTip != null ) {
      g_iSearchTipPosLeft = CyberCoreFindPosX(pSearchToolTip);
      g_iSearchTipPosRight = g_iSearchTipPosLeft + CyberCoreFindWidth(pSearchToolTip);
      g_iSearchTipPosTop = CyberCoreFindPosY(pSearchToolTip) - 30;
      g_iSearchTipPosBottom = g_iSearchTipPosTop + CyberCoreFindHeight(pSearchToolTip) + 30;
   
      CyberCoreAttachEvent( 'onmousemove', document, TrackSearchToolTip );
      setTimeout( RequestHideSearchToolTip, 400 );
   }
}

function cbcResHighlight( objectId )
{
   var pObject = document.getElementById( objectId );
   if( pObject != null ) {
      if( pObject.style ) {
         //pObject.style.backgroundColor = 'yellow';
         pObject.style.backgroundColor = '#FFFFEA';
      }
   }
}

function cbcResUnHighlight( objectId )
{
   var pObject = document.getElementById( objectId );
   if( pObject != null ) {
      if( pObject.style ) {
         pObject.style.backgroundColor = '';
      }
   }
}


//---------------------------------------------------------------------||
// FUNCTION:    AddSearchAllCheckbox                                   ||
// PARAMETERS:  name\id of section checkbox belongs to                 ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Adds a checkbox that will check all checkboxes in the  ||
//              parent container                                       ||
//---------------------------------------------------------------------||
function AddSearchAllCheckbox( strId )
{
   document.write(' <span class="bodyText"><input type=checkbox name="check_' + strId + '" id="check_' + strId + '" value="1" onClick="CyberCoreCheckmarkAll(\'' + strId + '\', this.checked);"> <i> Search All</i></a></span>');
}


//---------------------------------------------------------------------||
// FUNCTION:    ClearSearchAllCheckbox                                 ||
// PARAMETERS:  name\id of section checkbox belongs to                 ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Unchecks the search all box, if it exists              ||
//---------------------------------------------------------------------||
function ClearSearchAllCheckbox( strId )
{  
   var pCb = CyberCoreGetObject('check_' + strId);

   if ( pCb ) {
      pCb.checked = 0;
   }
}


//---------------------------------------------------------------------||
// FUNCTION:    ClearLastSearch                                        ||
// PARAMETERS:  N/A                                                    ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Clears last search parameters cookie                   ||
//---------------------------------------------------------------------||
function ClearLastSearch()
{
   SetCookie ('lastsearch','',null,'/',null,false);
}


//---------------------------------------------------------------------||
// FUNCTION:    ToggleSearchFlyout                                     ||
// PARAMETERS:  name\id of section to flyout                           ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Toggles a search refinement menu nav section on\off    ||
//---------------------------------------------------------------------||
function ToggleSearchFlyout( strId )
{
   var pDiv = CyberCoreGetObject(strId);
   var pNode = CyberCoreGetObject(strId + '_node');

   if ( pDiv ) {

      if ( pDiv.style.display == 'block' ) {
         pDiv.style.display = 'none';
         if ( pNode ) {
            pNode.src = '/images/plus.gif';
         }
      }
      else {
         pDiv.style.display = 'block';
         if ( pNode ) {
            pNode.src = '/images/minus.gif';
         }
      }
   }
}


//---------------------------------------------------------------------||
// FUNCTION:    ToggleSaveListing                                      ||
// PARAMETERS:  Property ID to toggle, DIV to modify display in        ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Adds or removes property from compare function         ||
//---------------------------------------------------------------------||
function ToggleSaveListing( lProperty )
{
   var bAlreadyInCart = false;
   var iCartLocation = 0;
   var bNotify = true;
   var strPropString = GetCookie("ListingSave");
   var iCurr;
   var aPropertyArray;
   var strNewString;
   var bFirst = true;
   var dtExpiration = (new Date((new Date()).getTime() + 30*150000));
   if ( strPropString ) {
      aPropertyArray = strPropString.split('|');
   } else {
      aPropertyArray = new Array;
   }
   strNewString = "";
   for ( iCurr=0; iCurr < aPropertyArray.length; iCurr++ ) {
      if ( lProperty == aPropertyArray[iCurr] ) {
         bAlreadyInCart = true;
         iCartLocation = iCurr;
      } else {
         if ( !bFirst ) {
            strNewString += "|" + aPropertyArray[iCurr];
         } else {
            strNewString += aPropertyArray[iCurr];
            bFirst = false;
         }
      }

   }
   if ( !bAlreadyInCart ) {
      if( aPropertyArray.length > 17 ) {
         alert("You may only save up to 18 properties at a time.  Please remove one or more proeprties from your list before trying to add more.");
         return;
      } else {
         if ( !bFirst )
            strNewString += "|";
         strNewString += lProperty;
      }
   }

   SetCookie("ListingSave", strNewString, dtExpiration, "/", null, false);

   if ( document.getElementById ) {
      var pDiv = document.getElementById( 'pcd' + lProperty );
      if ( pDiv && pDiv.length && pDiv.length > 1 ) {
         for ( iCurr = 0; iCurr < pDiv.length; iCurr++ ) {
            pSubDiv = pDiv[iCurr];
            if ( pSubDiv ) {
               if ( bAlreadyInCart ) {
                  pSubDiv.innerHTML = g_strSaveListingAddMsg;
               } else {
                  pSubDiv.innerHTML = g_strSaveListingRemMsg;                  
               }
               bNotify = false;
            }
         }
      } else {
         if ( pDiv ) {
            if ( bAlreadyInCart ) {
               pDiv.innerHTML = g_strSaveListingAddMsg;
            } else {
               pDiv.innerHTML = g_strSaveListingRemMsg;
            }
            bNotify = false;
         }
      }
   }

   if ( bNotify ) {
      if ( bAlreadyInCart ) {
         alert("You have removed this property from your saved listings.");
      } else {
         alert("You have added this property to your saved listings.");
      }
   }

   if( !bAlreadyInCart ){
      strPropString = GetCookie("ListingSave");
      if( strPropString == null || strPropString == '' )
         alert("You must have cookies enabled in your browser to use this feature.");
   }
}


//---------------------------------------------------------------------||
// FUNCTION:    ShowListingSaveButton                                  ||
// PARAMETERS:  Property ID to show                                    ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Writes to document current display re function         ||
//---------------------------------------------------------------------||
function ShowListingSaveButton( lProperty )
{
   var bAlreadyInCart = false;
   var strPropString = GetCookie("ListingSave");
   var aPropertyArray;
   if ( strPropString ) {
      aPropertyArray = strPropString.split('|');
   } else {
      aPropertyArray = new Array;
   }
   for ( iCurr=0; iCurr < aPropertyArray.length; iCurr++ ) {
      if ( lProperty == aPropertyArray[iCurr] ) {
         bAlreadyInCart = true;
      }
   }
   if ( !bAlreadyInCart ) {
      document.write(g_strSaveListingAddMsg);
   } else {
      document.write(g_strSaveListingRemMsg);
   }
}

//Convert string to array
function GetCompareArrayFromString( strPropString )
{
   var iNumProperties = 0;
   var aPropArray = new Array;
   var aTmpItemArray = new Array;
   if ( strPropString ) {
      aTmpItemArray = strPropString.split('|');
      var iNumItems = aTmpItemArray.length;
      for( var i=0; i < iNumItems; i++ ) {
         if( aTmpItemArray[i] ) {         
            var aThisProp = aTmpItemArray[i].split('*');
            if( aThisProp.length == 4 ) {
               aPropArray[iNumProperties] = new Array;
               aPropArray[iNumProperties]['lProperty']    = aThisProp[0];
               aPropArray[iNumProperties]['strProperty']  = aThisProp[1];
               aPropArray[iNumProperties]['strCityState'] = aThisProp[2];
               aPropArray[iNumProperties]['strPrice']     = aThisProp[3];
               iNumProperties++;
            }
         }
      }
   } 
   return aPropArray;
}

//Convert array to string
function GetCompareStringFromArray( aPropArray )
{
   var strPropString = '';
   var iNumItems = aPropArray.length;
   for( var i=0; i < iNumItems; i++ ) {
      strPropString += aPropArray[i]['lProperty'];
      strPropString += "*";
      strPropString += aPropArray[i]['strProperty'].replace(/\*/g, " ");
      strPropString += "*";
      strPropString += aPropArray[i]['strCityState'].replace(/\*/g, " ");
      strPropString += "*";
      strPropString += aPropArray[i]['strPrice'];
      if( iNumItems > (i+1) ) {
         strPropString += "|";
      }
   }
   return strPropString;
}


//---------------------------------------------------------------------||
// FUNCTION:    ShowListingCompareButton                               ||
// PARAMETERS:  Property ID to show                                    ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Writes to document current display re function         ||
//---------------------------------------------------------------------||
function ShowListingCompareButton( lProperty )
{
   var bAlreadyInCart = false;
   var strPropString = GetCookie("PropCompare");
   var aPropertyArray;
   aPropertyArray = GetCompareArrayFromString( strPropString );
   
   for ( iCurr=0; iCurr < aPropertyArray.length; iCurr++ ) {
      if ( lProperty == aPropertyArray[iCurr]['lProperty'] ) {
         bAlreadyInCart = true;
      }
   }
   if ( !bAlreadyInCart ) {
      document.write(g_strCompareListingAddMsg);
   } else {
      document.write(g_strCompareListingRemMsg);
   }
}

//---------------------------------------------------------------------||
// FUNCTION:    ShowSavedListingTab                                    ||
// PARAMETERS:  N/A                                                    ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Displays saved listing tab if there are saved listings ||
//---------------------------------------------------------------------||
function ShowSavedListingTab()
{
   var strPropString = GetCookie("ListingSave");
   if( strPropString ) {
      if( strPropString.length > 1 ) {
         document.write('<td style="border-bottom: 1px solid #305782;width:4px;">&nbsp;</td>');
         document.write('<td style="border: 1px solid #305782;width:100px;height:30px;" align="center">');
         document.write('<a href="/cbcbin/searchproperty?t=s"><u><b>Saved Listings</b></u></a>');
         document.write('</td>');
      }
   }
}

//---------------------------------------------------------------------||
// FUNCTION:    ShowSavedListingLink                                   ||
// PARAMETERS:  bool - if true show "|" seperator                      ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Displays saved listing hyperlink if are saved listings ||
//---------------------------------------------------------------------||
function ShowSavedListingLink( bShowSeperator )
{
   var strPropString = GetCookie("ListingSave");
   if( strPropString ) {
      if( strPropString.length > 1 ) {
         if( bShowSeperator ) {
            document.write(' <font class="bodySmallText">&nbsp;|&nbsp;</font> ');
         }
         document.write('<a href="/cbcbin/searchproperty?t=s" class="bodySmallHref"><u><b>Saved Listings</b></u></a>');
      }
   }
}


//---------------------------------------------------------------------||
// FUNCTION:    ShowLargePhoto                                         ||
// PARAMETERS:  Name of photo in document                              ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Create image popup                                     ||
//---------------------------------------------------------------------||
function ShowLargePhoto( strId, strCaptionId )
{
   var strCaption = '';
   var pCaption = document.getElementById(strCaptionId);
   if( pCaption ) {
      strCaption = pCaption.innerHTML;
   }
   pImg = document.getElementById(strId);
   if( pImg ) {
      var strUrl = pImg.largephotourl;
      if( !strUrl ) strUrl = pImg.src;
      if( strUrl) {
         var pWinPop = window.open('', 'largeImage', 'width=660,height=540,scrollbars=yes');
         if( pWinPop ) {
            pWinPop.document.write('<html><head>');
            pWinPop.document.write('</head><body>');
            pWinPop.document.write('<p align=center><img src="'+strUrl+'"></p><p align=center style="font-family:arial;">');
            pWinPop.document.write(strCaption + '</p><p align=center style="font-family:arial;">');
            pWinPop.document.write('<a href="Javascript:window.close()">Close Window</a></p>');
            pWinPop.document.write(' </body></html>');
            setTimeout( function(){ fitWinToPic(pWinPop); }, 250 );
         }
      }
   }
}

function fitWinToPic( pWinHandle ) 
{ 
   if( pWinHandle ) {
       if( document.images[0].complete ) {
          var NS = (navigator.appName=="Netscape")?true:false;
          var iWidth = (NS)?pWinHandle.innerWidth:pWinHandle.document.body.clientWidth;
          var iHeight = (NS)?pWinHandle.innerHeight:pWinHandle.document.body.clientHeight;

          var iThisWidth = (NS)?window.innerWidth:document.body.clientWidth;
          var iThisHeight = (NS)?window.innerHeight:document.body.clientHeight;
   
          if( iWidth > iThisWidth ) iWidth = iThisWidth + 20;
          if( iHeight > iThisHeight ) iHeight = iThisHeight + 100;

          if( pWinHandle.document.images[0].width > iThisWidth ) pWinHandle.document.images[0].width = iThisWidth - 30;
          else if( pWinHandle.document.images[0].height > iThisHeight) pWinHandle.document.images[0].height = iThisHeight - 30;

          iWidth = (pWinHandle.document.images[0].width - iWidth) + 20;
          iHeight = (pWinHandle.document.images[0].height - iHeight) + 100;
          if( (iWidth > 0) || (iHeight > 0) ) {
             pWinHandle.resizeBy(iWidth, iHeight);
             pWinHandle.focus();
          }
       } else if ( pWinHandle.document.images[0] ) { 
          setTimeout( function(){ fitWinToPic(pWinPop); }, 250 );
       }
   }
}

//---------------------------------------------------------------------||
// FUNCTION:    ShowLargePhoto                                         ||
// PARAMETERS:  Name of photo in document                              ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Create image popup                                     ||
//---------------------------------------------------------------------||
function ShowLargePhotoUrl( strUrl, strCaptionId )
{
   var strCaption = '';
   var pCaption = document.getElementById(strCaptionId);
   if( pCaption ) {
      strCaption = pCaption.innerHTML;
   }
   if( strUrl != '' ) {
      var pWinPop = window.open('', 'largeImage', 'width=660,height=540,resizeable=yes');
      if( pWinPop ) {
         pWinPop.document.write('<html><head>');
         pWinPop.document.write('</head><body>');
         pWinPop.document.write('<p align=center><img src="'+strUrl+'"></p><p align=center>');
         pWinPop.document.write(strCaption + '</p><p align=center>');
         pWinPop.document.write('<a href="Javascript:window.close()">Close Window</a></p>');
         pWinPop.document.write(' </body></html>');
         setTimeout( function(){ fitWinToPic(pWinPop); }, 250 );
      }
   }
}

function WriteSpBcHomeLink()
{
  if( window.location ) {
     var strLocation = '' + window.location;
     if( strLocation.indexOf("poweredby") < 0 ) {
        document.write('<a href="/" class="cbcbreadcrumbnav">Home</a>');
     }
  } else {
     document.write('<a href="/" class="cbcbreadcrumbnav">Home</a>');
  }
}


//---------------------------------------------------------------------||
// FUNCTION:    ToggleCompare                                          ||
// PARAMETERS:  Property ID to toggle, DIV to modify display in        ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Adds or removes property from compare function         ||
//---------------------------------------------------------------------||
function ToggleCompare( lProperty, strProperty, strCityState, strPrice, bSuppressBeaconUpdate )
{
   var bAlreadyInCart = false;
   var iCartLocation = 0;
   var bNotify = true;
   var strPropString = GetCookie("PropCompare");
   var iCurr;
   var aPropertyArray;
   var strNewString;
   var bFirst = true;
   var dtExpiration = (new Date((new Date()).getTime() + 30*150000));
   aPropertyArray = GetCompareArrayFromString( strPropString ); 
   if (bSuppressBeaconUpdate == 'false') {
       bSuppressBeaconUpdate = false;
   }
   strNewString = "";
   for ( iCurr=0; iCurr < aPropertyArray.length; iCurr++ ) {
      if ( lProperty == aPropertyArray[iCurr]['lProperty'] ) {
         bAlreadyInCart = true;
         iCartLocation = iCurr;
         //Remove this from the cart. 
         aPropertyArray.splice(iCartLocation, 1);
         break;
      }
   }
   if ( !bAlreadyInCart ) {
      if( aPropertyArray.length > (g_iTotalCompareProperties - 1) ) {
         alert("You may only compare up to 3 properties at a time.  Please remove one or more proeprties from the comparison before trying to add more.");
         return;
      } else {
         iCartLocation = aPropertyArray.length;
         aPropertyArray[iCartLocation] = new Array;
         aPropertyArray[iCartLocation]['lProperty']    = lProperty;
         aPropertyArray[iCartLocation]['strProperty']  = strProperty;
         aPropertyArray[iCartLocation]['strCityState'] = strCityState;
         aPropertyArray[iCartLocation]['strPrice']     = strPrice;
      }
   }
   strNewString = GetCompareStringFromArray( aPropertyArray );
   SetCookie("PropCompare", strNewString, dtExpiration, "/", null, false);

   if ( document.getElementById ) {
      pDiv = document.getElementById('pcdcmp' + lProperty);
      if ( pDiv && pDiv.length && pDiv.length > 1 ) {
         for ( iCurr = 0; iCurr < pDiv.length; iCurr++ ) {
            pSubDiv = pDiv[iCurr];
            if ( pSubDiv ) {
               if ( bAlreadyInCart ) {
                  if (!bSuppressBeaconUpdate) {
                     DisplayCompareBox();
                     pSubDiv.innerHTML = "<img src=\"/images/cpadd.gif\" border=0 align=middle width=19 height=19>";
                  } else {
                     pSubDiv.innerHTML = "(Add to Compare Listings)";
                  }
               } else {
                  if (!bSuppressBeaconUpdate) {
                     DisplayCompareBox();
                     pSubDiv.innerHTML = "<img src=\"/images/cprem.gif\" border=0 align=middle width=19 height=19>";
                  } else {
                     pSubDiv.innerHTML = "(Remove from Compare Listings)";
                  }
               }
               bNotify = false;
            } else {
                //This means the property is not being displayed on the screen, lets remove from box
                if (!bSuppressBeaconUpdate) {
                   DisplayCompareBox();
                }
                bNotify = false;
            }
         }
      } else {
         if ( pDiv ) {
            if ( bAlreadyInCart ) {
               if (!bSuppressBeaconUpdate) {
                  DisplayCompareBox();
                  pDiv.innerHTML = "<img src=\"/images/cpadd.gif\" border=0 align=middle width=19 height=19>";
               } else {
                  pDiv.innerHTML = "(Add to Compare Listings)";
               }
            } else {
               if (!bSuppressBeaconUpdate) {
                  DisplayCompareBox();
                  pDiv.innerHTML = "<img src=\"/images/cprem.gif\" border=0 align=middle width=19 height=19>";
               } else {
                  pDiv.innerHTML = "(Remove from Compare Listings)";
               }
               
            }
            bNotify = false;
         } else {
           //This means the property is not being displayed on the screen, lets remove from box
           if (!bSuppressBeaconUpdate) {
              DisplayCompareBox();
           }
           bNotify = false;

         }
      }
   }

   if ( bNotify ) {
      if ( bAlreadyInCart ) {
         alert("You have removed this property from your comparison list.");
      } else {
         alert("You have added this property to your comparison list.");
      }
   }

   
   if( !bAlreadyInCart ){
      strPropString = GetCookie("PropCompare");
      if( strPropString == null || strPropString == '' )
         alert("You must have cookies enabled in your browser to use this feature.");
   }
}

//---------------------------------------------------------------------||
// FUNCTION:    DisplayCompareBox                                      ||
// PARAMETERS:  N/A                                                    ||
// RETURNS:     N/A                                                    ||
// PURPOSE:     Writes compare box to the screen.                      ||
//---------------------------------------------------------------------||
function DisplayCompareBox()
{
   var i = 0;
   var iNumberFound = 0;
   var strPropString = GetCookie("PropCompare");
   var aPropertyArray;
   var strProperty;
   var strCityState;
   var lProperty;
   var strPrice;
   aPropertyArray = GetCompareArrayFromString( strPropString );

   pCompareBox = document.getElementById('comparebox'); 
   if( pCompareBox ) {
      iNumberFound = aPropertyArray.length;
      if ( iNumberFound > 0 ) {      
         var strCpHtml;
         strCpHtml = "<table cellspacing=\"0\" cellpadding=\"0\">" +
                    "  <tr>" +
                    "    <td background=\"/images/navtop.gif\" class=\"bodySmallText\" align=\"center\" colspan=\"2\">" +
                    "      <br><font color=\"#FFFFFF\"><b>Properties To Compare</b></font>" +
                    "    </td>" +
                    "  </tr>" +
                    "  <tr>" +
                    "    <td style=\"border-collapse: collapse; border: 1px solid black;\">" +
                    "      <table cellspacing=\"0\" cellpadding=\"0\">" +
                    "        <tr bgcolor=\"#E9E9E7\">" +
                    "          <td>&nbsp;</td>" +
                    "          <td>" +
                    "<div id=\"comparepropbutton\">";

         for( i=0; i < iNumberFound; i++ ) {
            lProperty = aPropertyArray[i]['lProperty'];
            strProperty = aPropertyArray[i]['strProperty'];
            strCityState = aPropertyArray[i]['strCityState'];
            strPrice = aPropertyArray[i]['strPrice'];

            strCpHtml += "<table  cellspacing=\"0\" cellpadding=\"0\" width=\"160\">";
            strCpHtml += "<tr><td background=\"/images/bckgrn_dot.gif\" colspan=2>&nbsp;</td></tr>";
            strCpHtml += "  <tr> ";
            strCpHtml += "    <td class=\"bodySmallText\" valign=top>";
            strCpHtml += "      <b>" + strProperty + "</b>, ";
            strCpHtml += "      <font color=#666666>" + strPrice + "</font><br>";
            strCpHtml += "      <font color=#666666>" + strCityState + "</font>";
            strCpHtml += "    </td>";
            strCpHtml += "    <td class=\"bodySmallText\" valign=top width=10>";
            strCpHtml += "      <a class=eratool href=\"Javascript:ToggleCompare( '" + lProperty + "', '" + strProperty + "', '" + strCityState + "', '" + strPrice + "', false );\" title=\"Remove from Compare Listings\"><img src=\"/images/cpsprem.gif\" width=10 height=10 border=0 align=middle width=20 height=19 Alt=\"Remove From Compare Listings\"></a>";
            strCpHtml += "    </td>";
            strCpHtml += "  </tr>";
            strCpHtml += "</table>";
         }

         if ( iNumberFound > 1 ) {
            strCpHtml += "<table width=100%><tr><td background=/images/bckgrn_dot.gif >&nbsp;</td></tr><tr><td><input type=submit value=\"Compare Properties\" style=\"background-color:#61809F; color: #ffffff; font-weight: bold;width:150px;height:21px;\" onClick=\"location.href='/cbcbin/compare';\"></td></tr></table>";
         } 
         strCpHtml += "</div>" + 
                      "</td><td>&nbsp;</td></tr></table></td></tr></table>";
         pCompareBox.innerHTML = strCpHtml;
      } else {
         pCompareBox.innerHTML = '';
      }
   }
}

