var g_bMapVisible = false;
var g_aPushPinResults = new Array();

function ValidateListAPropertyOfficeSelections(pForm)
{
   if ( !AtLeastOneChecked("officekey") ) {
      alert("You must select at least one office to contact.");
      return false;
   }

   return true;
}

function ShowListAPropertyResultMap()
{
   if ( !g_bMapVisible ) {
      g_objMapPreview.popupname = 'officemapviewpreview';
      RemoveClass( 'resultmap', 'hide' );
      AddClass( 'listapropertyresultsviewmap', 'hide' );      
      RfgCreateMap('resultmap', 'RoadVE', true, 38.6437, -98.8235, 4, false, false );
      g_bMapVisible = true;
   }
   RfgClearMap('resultmap');

   for ( var i in g_aPushPinResults ) {
      var strCmd = "RfgAddPushPin('resultmap', "+ (g_aPushPinResults[i].Latitude) +", "+ (g_aPushPinResults[i].Longitude) +", '"+(parseInt(i)+1)+"', 'empty string', '/images/map/"+(parseInt(i)+1)+".gif')";
      RfgQueueCommand(escape( strCmd ));
   }
   RfgResizeMapToPoints( 'resultmap', g_aPushPinResults );
}

function CloseListAPropertyOfficeDetail()
{
   var pPopupIframe = document.getElementById('PopupIframe');
   if ( pPopupIframe ) {
      pPopupIframe.src = '/blank.html';
   }
   var pPopupDiv = document.getElementById('PopupWrapper');
   if ( pPopupDiv ) {
      pPopupDiv.innerHTML = '';
      AddClass(pPopupDiv, 'hide');
   }
}


function ShowListAPropertyOfficeDetail(strOfficeKey)
{
   var pPopupDiv = document.getElementById('PopupWrapper');
   if ( pPopupDiv ) {
      strHtml = '<div id="PopupMask"></div>' + 
                '<div id="PopupFrameContainer" class="calloutgrey" style="position:relative;">' +
                '   <div class="simplepopupgrey" style="width:750px;height:580px;">' +
                '      <div class="right">' +
                '         <a href="javascript:CloseListAPropertyOfficeDetail();">' +
                '            Close <img src="/images/close.gif" border="0" style="position:relative;top:4px;">' +
                '         </a>' +
                '      </div>' +
                '      <iframe id="PopupIframe" src="/blank.html" width="750" height="540" scrolling="auto" frameborder="0"></iframe>' +
                '   </div>' + 
                '</div>';
      pPopupDiv.innerHTML = strHtml;
      setTimeout( function(){ writeOfficeDetail(strOfficeKey);}, 100 );
   }
}

function writeOfficeDetail(strOfficeKey)
{
   var pPopupDiv = document.getElementById('PopupWrapper');
   if ( pPopupDiv ) {

      var pPopupIframe = document.getElementById('PopupIframe');
      StopHeroes( 'homepageheroes' );
      if ( pPopupIframe ) {
         pPopupIframe.src = '/careerfranchise/career/officedetail.jsp?id='+strOfficeKey;
      }

      var pPopupFrameContainer = document.getElementById('PopupFrameContainer');
      if ( pPopupFrameContainer ) {
         pPopupFrameContainer.style.zIndex = '99999999';
         pPopupFrameContainer.style.background = '#000000';

         pPopupFrameContainer.style.position = 'absolute';
         pPopupFrameContainer.style.top = '82px';
         pPopupFrameContainer.style.left = '50%';
         pPopupFrameContainer.style.marginLeft = '-395px';         
      }

      var pPopupMask = document.getElementById('PopupMask');
      if ( pPopupMask ) {
         pPopupMask.style.position = 'absolute';
         pPopupMask.style.top = '0';
         pPopupMask.style.left = '0';
         pPopupMask.style.width = '100%';
         pPopupMask.style.height = '100%';
         pPopupMask.style.zIndex = '99999999';
      }
      RemoveClass(pPopupDiv, 'hide');
   }
}

function DoListAPropertyOfficeSearch()
{
   var strUrl = '/xml-bin/officesearch?rpp=50&where=' + escape(trim(document.getElementById('listapropertywhere').value));
   var strHtml = '<div class="singlecolumn detailsolidbg" style="border-top:2px solid white;padding:4px;">';
   strHtml += 'Loading...';
   strHtml += '</div>';
   SetInnerHtml('listapropertyresults' , strHtml);
   g_AjaxQueue.schedule("ListAPropertyOfficeSearch", strUrl, OnCompleteListAPropertyOfficeSearch);
}


function OnCompleteListAPropertyOfficeSearch(pThisAjaxReq)
{
   if (pThisAjaxReq.readyState != 4) {
      return;
   }
   var strPrefix = '';   

   var pErrorNode = pThisAjaxReq.responseXML.getElementsByTagName('error');
   if ( pErrorNode.length && pErrorNode.length >= 0 ) {
      //Uh oh. An error has been reported
      var strMsg = '';
      if ( pErrorNode[0].firstChild ) {
         strMsg = pErrorNode[0].firstChild.nodeValue;
      }
      alert( "Could not complete search.  Please make sure you have entered a valid 5-digit zip code.\n   * " + strMsg );
      SetInnerHtml('listapropertyresults' , '');
      return;
   }

   var pAreasNode = pThisAjaxReq.responseXML.getElementsByTagName('OfficeSearchResults');
   if ( !pAreasNode.length || pAreasNode.length == 0 ) {
      alert( "Could not complete search.  Please make sure you have entered a valid 5-digit zip code." );
      SetInnerHtml('listapropertyresults' , '');
      return;
   }

   var pOfficeNodes = pThisAjaxReq.responseXML.getElementsByTagName('Office');

   var strHtml = '<form action="listaproperty-form.jsp" onSubmit="return ValidateListAPropertyOfficeSelections(this);">';
   strHtml += '<div class="singlecolumn detailsolidbg" style="border-top:2px solid white;padding:4px;">';
   strHtml += '<div style="height:1.3em;">';
   strHtml += '  <div style="float:left">Results (' + pOfficeNodes.length + ')</div>';   
   strHtml += '  <div id="listapropertyresultsviewmap" style="float:right"><a href="javascript:ShowListAPropertyResultMap();" style="text-decoration:none"><img src="/images/search/map.gif" border="0"> View Map</a></div>';
   strHtml += '</div>';
   strHtml += '<table width="100%" border="0" cellpadding="3" cellspacing="0" class="searchresultstable"><tbody>';
   g_aPushPinResults = new Array();
   var iMapCount = 0;
   var strMapPreviews = '';
   for (var i=0; i<pOfficeNodes.length;i++) {
      var fLat = GetNodeValue(pOfficeNodes[i], 'Latitude');
      var fLon = GetNodeValue(pOfficeNodes[i], 'Longitude');
      if (Math.abs(fLat) > 0.01 || Math.abs(fLon) > 0.01 ) {
         g_aPushPinResults[iMapCount++] = new VELatLong( fLat, fLon );
      }
      var strOfficeKey = GetNodeValue(pOfficeNodes[i], 'OfficeKey');
      strHtml += '<tr>';
      strHtml += '<td><input type="checkbox" name="officekey" value="' + strOfficeKey + '"><img src="/images/map/'+(i+1)+'.gif"></td>';
      strHtml += '<td>' + GetNodeValue(pOfficeNodes[i], 'OfficeName') + '</td>';
      strHtml += '<td>';
      strHtml += trim(GetNodeValue(pOfficeNodes[i], 'Address') + ' ' + GetNodeValue(pOfficeNodes[i], 'Address2'));
      strHtml += ', ';
      strHtml += GetNodeValue(pOfficeNodes[i], 'City') + ', ';
      strHtml += GetNodeValue(pOfficeNodes[i], 'State') + ' ';
      strHtml += GetNodeValue(pOfficeNodes[i], 'ZipCode');
      strHtml += '</td>';     
      strHtml += '<td><a href="javascript:ShowListAPropertyOfficeDetail(\''+strOfficeKey+'\');"><img src="/images/listaproperty/details_but.gif" border="0"></a></td>';
      strHtml += '</tr>';

      strMapPreviews += '<div id="officemapviewpreview_'+(i+1)+'" class="hide">' +
                        '  <span id="officemapviewpreviewname_'+(i+1)+'">' +
                        '     <img src="/images/map/'+(i+1)+'.gif" border="0" align="top">' +
                        '     <a href="javascript:ShowListAPropertyOfficeDetail(\''+strOfficeKey+'\');" class="searchresultdetail"><b>'+GetNodeValue(pOfficeNodes[i], 'OfficeName')+'</b></a>' +
                        '  </span>' +
                        '  <span id="officemapviewpreviewdetails_'+(i+1)+'">' +
                        trim(GetNodeValue(pOfficeNodes[i], 'Address') + ' ' + GetNodeValue(pOfficeNodes[i], 'Address2')) + 
                        '<br>' + 
                        GetNodeValue(pOfficeNodes[i], 'City') + ', ' + 
                        GetNodeValue(pOfficeNodes[i], 'State') + ' ' + 
                        GetNodeValue(pOfficeNodes[i], 'ZipCode') + 
                        '  </span>' +
                        '</div>';
   }
   strHtml += '</table>';
   strHtml += '<br><input type="image" src="/images/listaproperty/contacttheseoffices.gif">';
   strHtml += '<div class="detailcolumndecoration">' + 
              '   <div class="bl"></div>' + 
              '   <div class="bm"></div>' + 
              '   <div class="br"></div>' + 
              '</div>';
   strHtml += '</div>';
   strHtml += '</form>';
   SetInnerHtml('listapropertyresults' , (strHtml + strMapPreviews));
   SetInnerHtml('listapropertyinstructions' , "Review the results below and check the offices you'd like to contact. <br>If you'd like to see these results on a map, click the map icon in the upper right corner." );

   if ( g_bMapVisible ) {
      ShowListAPropertyResultMap();
   }
}
