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 where = escape(trim(document.getElementById('listapropertywhere').value));
   var lap_where = document.getElementById("lap_where");
   if( lap_where ) {
      lap_where.value = where;
   }
   var lap_zipcode = document.getElementById("lap_zipcode");
   if( lap_zipcode ) {
      lap_zipcode.value = where;
   }
   //check for zip
   var numericRegExp = new RegExp("[0-9][0-9][0-9][0-9][0-9]");
   if(numericRegExp.test(where) && where.length == 5){
      var strUrl = '/xml-bin/officesearch?rpp=50&shortformat=Y&where=' + where;
      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, OnCompleteListAPropertyOfficeSearch2);
   }else{
      alert( "Please make sure you have entered a valid 5-digit zip code." );
   }
}


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();
   }
}

function OnCompleteListAPropertyOfficeSearch2(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 contact_info = document.getElementById("contact_info");
   var thankyou = document.getElementById("thankyou");
   var office_list = document.getElementById("office_list");
   var lap_form = document.getElementById("lap_form");
   var lap_state = document.getElementById("lap_state");
   if( contact_info && office_list && lap_form ) {
      if( pOfficeNodes.length > 0 ) {
         var strHtml = "<table>";      
         for (var i=0; i<pOfficeNodes.length;i++) {
   
            var strOfficeKey  = GetNodeValue(pOfficeNodes[i], 'officekey');
            var strOfficeName = GetNodeValue(pOfficeNodes[i], 'officename');
            var strAddress    = GetNodeValue(pOfficeNodes[i], 'address');
            var strCity       = GetNodeValue(pOfficeNodes[i], 'city');
            var strState      = GetNodeValue(pOfficeNodes[i], 'state');
            var strZip        = GetNodeValue(pOfficeNodes[i], 'zipcode');
            if( i == 0 && lap_state ) {
               lap_state.value = strState;
            }
            if( ((i%2)==0) && i!=0) {
               strHtml += "</tr>";
            }
            if( ((i%2)==0) ) {
               strHtml += "<tr>";
            }
            strHtml += "<td><input type=\"checkbox\" name=\"officekey\" value=\""+strOfficeKey+"\"/></td>";
            strHtml += "<td>";
            strHtml += "<div class=\"office_name\">"+strOfficeName+"</div>";
            strHtml += "<div class=\"office_address\">";
            strHtml += strAddress+"<br/>";
            strHtml += strCity+", "+strState+" "+strZip;
            strHtml += "</div>";
            strHtml += "</td>";
         }
         contact_info.style.visibility = "visible";
      }else{
         contact_info.style.visibility = "hidden";
         strHtml = "<div style='margin:10px;'><span style='color:red;font-weight:bold;'>We are currently unable to find an office to serve this zip code.</span><br/>Please call us at 866.732.6139 to speak with a CENTURY 21 representative now.</div>";
      }
      office_list.innerHTML=strHtml;
      lap_form.style.visibility="visible";
   }
   if( thankyou ) {
      thankyou.style.display = "none";
   }
}
function removeDefault(obj, strDefault){
   if( obj ) {
      if( obj.value == strDefault ) {
         obj.value = "";
      }
   }
}

function onListAPropertySubmit(){
   var firstname = document.getElementById("firstname");
   var lastname = document.getElementById("lastname");
   var email = document.getElementById("email");
   var phone = document.getElementById("phone");
   var comments = document.getElementById("comments");

   if( phone && email) {
      if( (phone.value == "Enter your phone number" || phone.value == "") && (email.value == "Enter your email address" || email.value == "")) {
         alert("You must supply either an email address or a phone number.");
         return false;
      }
   }

   //clear defaults
   if( firstname ) {
      if( firstname.value == "Enter your first name" ) {
         firstname.value = "";
      }
   }
   if( lastname ) {
      if( lastname.value == "Enter your last name" ) {
         lastname.value = "";
      }
   }
   if( email ) {
      if( email.value == "Enter your email address" ) {
         email.value = "";
      }
   }
   if( phone ) {
      if( phone.value == "Enter your phone number" ) {
         phone.value = "";
      }
   }
   if( comments ) {
      if( comments.value == "Enter additional comments" ) {
         comments.value = "";
      }
   }
   return true;
} 
function initializeDefaults(){
   //clear defaults
   var firstname = document.getElementById("firstname");
   if( firstname ) {
      if( firstname.value == "" ) {
         firstname.value = "Enter your first name";
      }
   }
   var lastname = document.getElementById("lastname");
   if( lastname ) {
      if( lastname.value == "" ) {
         lastname.value = "Enter your last name";
      }
   }
   var email = document.getElementById("email");
   if( email ) {
      if( email.value == "" ) {
         email.value = "Enter your email address";
      }
   }
   var phone = document.getElementById("phone");
   if( phone ) {
      if( phone.value == "" ) {
         phone.value = "Enter your phone number";
      }
   }
   var comments = document.getElementById("comments");
   if( comments ) {
      if( comments.value == "" ) {
         comments.value = "Enter additional comments";
      }
   }
}


