/*
 * Commercial Micro-Site Scripts
 * ================================
 * Scripts that support the C21 Commercial Micro-site.
 *
 */
// These values will be overriden by the commercialproperty.jsp
g_iPriceLowBound = 0;
g_iPriceHighBound = 2000000;
g_iPriceIncrement = 50000;
var g_strPriceTypeSliderLabel = 'Price Range';

RfgQueueCommand('InitializeCommercialSliders%28%29%3B');

// On Click Handler for commercial property type (cpt) checkboxes
function cptOnClick( pCheckbox )
{
   if( pCheckbox ) {
      var pTd = document.getElementById( 'td_cpt_' + pCheckbox.getAttribute('cpttypename') );
      if( pTd ) { 
         if( pCheckbox.checked ) {
            AddClass( pTd, 'active' );
         } else {
            RemoveClass( pTd, 'active' );
         }
      }
   }
   UpdatePropertySearchLimits();
}

// On Click Handler for commercial sale type (cst) radio
function cstOnClick(pRadio)
{
   if( pRadio ) {
      var pLblBuy = document.getElementById( 'lbl_saletype_buy' );
      var pLblLease = document.getElementById( 'lbl_saletype_lease' );
      if(  pRadio.checked && pRadio.value == 'lease'  ) {
         AddClass( pLblLease, 'active' );
         RemoveClass( pLblBuy, 'active' );
         ResetPriceSlider('lease');
      } else {
         AddClass( pLblBuy, 'active' );
         RemoveClass( pLblLease, 'active' );
         ResetPriceSlider('buy');
      }
   }
   UpdatePropertySearchLimits();
}


// Updated commercial property type sidebar active state(s)
function UpdateCommPropTypeActiveState()
{
   var aElements = document.getElementsByName('t');
   if( aElements && aElements.length > 1 ) {
      for( var i=0; i < aElements.length; i++ ) {
         if( aElements[i].checked  ) {
            var pTd = document.getElementById( 'td_cpt_' + aElements[i].getAttribute('cpttypename') );
            if( pTd ) { 
               AddClass( pTd, 'active' );
            }
         }
      }
   }
}


function InitializeCommercialSliders()
{
   if( document.getElementById ) {

     /**
      * 
      * Initialize Property Slider Values
      * 
      **/
      var iMinPrice  = GetHiddenFieldIntValue('propertyminprice');
      var iMaxPrice  = GetHiddenFieldIntValue('propertymaxprice');
      var iMinSqFt   = GetHiddenFieldIntValue('propertyminsqft');
      var iMaxSqFt   = GetHiddenFieldIntValue('propertymaxsqft');

      var pDivPrice = document.getElementById('propertypriceslide');
      var pDivSqFt  = document.getElementById('propertysqftslide');

      if( g_iPriceLowBound > iMinPrice ) {
         iMinPrice = Math.max(iMinPrice, g_iPriceLowBound);
         document.getElementById('propertyminprice').value = iMinPrice;
      }

      if( pDivPrice && pDivSqFt ) {
         if( iMaxPrice > 0 ) {
            document.getElementById('propertypricelabel').innerHTML = '$' + AddCommas(iMinPrice) + ' to $' + AddCommas(iMaxPrice);
         } else {
            document.getElementById('propertypricelabel').innerHTML = '$' + AddCommas(iMinPrice) + ' to No limit';
         }
         if( iMaxSqFt > 0 ) {
            document.getElementById('propertysqftlabel').innerHTML = AddCommas(iMinSqFt) + ' to ' + AddCommas(iMaxSqFt);
         } else {
            document.getElementById('propertysqftlabel').innerHTML = AddCommas(iMinSqFt) + ' to No limit';
         }

         try {
            g_pPriceSlider = new CyberC21Slider( pDivPrice.id, true, g_iPriceLowBound, g_iPriceHighBound, g_iPriceIncrement,
                                                 iMinPrice, (iMaxPrice>0)?iMaxPrice:g_iPriceHighBound,
                                                 '/images/sidebar/sliderbg_122.gif', '/images/sidebar/slideractivearea.gif', 122, 16, '',
                                                 '/images/sidebar/grabwidget.gif', '/images/sidebar/grabwidget.gif', 15, 16,
                                                 1, 1,
                                                 onChangePriceSlider, onMovingPriceSlider );
         
            g_pSqFtSlider = new CyberC21Slider( pDivSqFt.id, true, 0, 5000, 500,
                                                iMinSqFt, (iMaxSqFt>0)?iMaxSqFt:5000,
                                                '/images/sidebar/sliderbg_122.gif', '/images/sidebar/slideractivearea.gif', 122, 16, '',
                                                '/images/sidebar/grabwidget.gif', '/images/sidebar/grabwidget.gif', 15, 16,
                                                1, 1,
                                                onChangeSqFtSlider, onMovingSqFtSlider );
            //g_AjaxQueue.RegisterErrorHandler( SearchPropertyOnAjaxError );
            g_bInitializeComplete = true;
         } catch ( e ) {
            g_bAjaxEnabled = false;
         }
      }
   }
}

function DoCommercialPropertySearch(pForm)
{
   if( window.DoAjaxCommercialSearch  ) {
      DoAjaxCommercialSearch(0);
   } else {
      //The user is not on the search page, take them there.
      if( document.forms.sidebar_form_property ) {
         setTimeout( function(){document.forms.sidebar_form_property.submit();}, 100 );
      } else {
         setTimeout( function(){document.location.href='/commercial/search.jsp';}, 100 );
      }      
   }   
   return false;
}

UpdatePropertySearchLimits = function ()
{
   if( !g_bInitializeComplete ) return;
   if( g_bIgnoreSearchInput ) return;

   var pSearchResultsDiv = document.getElementById('searchresultscontainer')
   if( pSearchResultsDiv && document.forms.sidebar_form_property ) {
      if( document.forms.sidebar_form_property.onsubmit ) {
         if( !document.forms.sidebar_form_property.onsubmit() ) {
            return;
         }
      }
      document.forms.sidebar_form_property.submit();
   }
}

//Set, or reset, the price slider values
function ResetPriceSlider( strSaleType )
{
   if( strSaleType == 'lease'  ) {
      g_iPriceLowBound = 0;
      g_iPriceHighBound = 150;
      g_iPriceIncrement = 1;
      g_strPriceTypeSliderLabel = 'Price Range/SF';
   } else {
      g_iPriceLowBound = 0;
      g_iPriceHighBound = 2000000;
      g_iPriceIncrement = 50000;
      g_strPriceTypeSliderLabel = 'Price Range';
   }
   SetInnerHtml( 'propertypriceslidertypelabel', g_strPriceTypeSliderLabel );
   //Sliders are initialized, let re-initialize them
   if( g_bInitializeComplete ) {
      //Let's set the values to DEFAULTS if changed.
      var pDivPrice = document.getElementById('propertypriceslide');
      g_pPriceSlider = new CyberC21Slider( pDivPrice.id, true, g_iPriceLowBound, g_iPriceHighBound, g_iPriceIncrement,
                                           g_iPriceLowBound, g_iPriceHighBound,
                                           '/images/sidebar/sliderbg_122.gif', '/images/sidebar/slideractivearea.gif', 122, 16, '',
                                           '/images/sidebar/grabwidget.gif', '/images/sidebar/grabwidget.gif', 15, 16,
                                           1, 1,
                                           onChangePriceSlider, onMovingPriceSlider );
      onChangePriceSlider( g_pPriceSlider );
   }
}

function LoadCommercialSearch( strWhere ) 
{
   var strParms = '?action=loadwhere';
   if( strWhere && strWhere != '' ) {
      strParms += '&where=' + escape( strWhere );
   }
   document.location.href = '/commercial/search.jsp' + strParms;
}


function showEmailDialog(pObj, strPropertyId, strOfficeId){
    var pPopup = document.getElementById('emailafriendpopup');
    if( pPopup ) {      
       try{
          positionPopup(pObj, pPopup);
       }catch (e){
       }
       pPopup.style.display = 'block';
       pPopup.style.zIndex = 50000;
       var rid = (new Date()).getTime();
       var pFrame = document.getElementById('emailafriendframe');
       if(pFrame) pFrame.src = '/emailafriend/property.jsp?id='+ escape(strPropertyId) +'&officeid='+ escape(strOfficeId) +'&commercial=true' +'&rid='+ escape(rid);
    }
    return false;
}
