var map;
var bounds;
var htmls = new Array();
var gMarkers = new Array();
var from_htmls = new Array();
var to_htmls = new Array();
var map;
var waitMarker;

function GenerateMap()
{       
       // document.getElementById("divLoading").style.display= "block";
        
        //clear the House markers
        map.clearOverlays();
       
        //show the Loading gif
        dispWaitIcon();
        
        //set the initial value for the xml elements
        document.getElementById("MapHome1_hdnVar").value = 0;
        
        //set the properties for the house markers
         baseIcon = new GIcon();
         baseIcon.shadow = "../Images/SearchMap/icon10_shadow.png";
         baseIcon.iconSize = new GSize(32, 32);
         baseIcon.shadowSize = new GSize(49,32);
         baseIcon.iconAnchor = new GPoint(16,16);
         baseIcon.transparent = "gmarker.png";
         baseIcon.infoWindowAnchor = new GPoint(16,16);
        baseIcon.infoShadowAnchor = new GPoint(9, 32);
        
        baseIcon.transparent = "../Images/SearchMap/icon10_transparent.png";
       
       //get all the values from the text box and dropdowns
    var minPrice = document.getElementById("MapHome1_ucDDLPriceMin").value; 
    var maxPrice = document.getElementById("MapHome1_ucDDLPriceMax").value;
    var bath = 0; var bed=0;
    var sel;
    var bathSelected = false;
    var bedSelected = false;
    
   var BathSelect = document.forms[0].MapHome1_ddBaths;
  
   if(BathSelect.selectedIndex > -1)
      {
       bath=  BathSelect.options[BathSelect.selectedIndex].value;
       bathSelected = true;
      }
   
   var BedSelect = document.forms[0].MapHome1_ddBeds;
  
   if(BedSelect.selectedIndex > -1)
      {
        bed=  BedSelect.options[BedSelect.selectedIndex].value;
        bedSelected = true;
      }
       
         bounds = map.getBounds();

         southWest = bounds.getSouthWest();
         northEast = bounds.getNorthEast();
         
    
     
         var d = new Date();
         var t_mil = d.getMilliseconds();
 
   //call the code behind of GetResult.aspx file
    xmlHttp = GXmlHttp.create();
    xmlHttp.open("GET","../Search/GetResult.aspx?maptype=office&nocache=" + t_mil + "&sw=" + southWest + "&ne=" + northEast + "&pl=" + minPrice + "&ph=" + maxPrice + "&bed=" + bed + "&bath=" + bath + "&cnty=All", true);
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.send(null);
  
}
 
 function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
    var Dispstr = xmlHttp.responseText;
    
    var recCnt = Dispstr.substring(0,Dispstr.indexOf(" Properties found"));
 
    if(recCnt == ''){recCnt = 0;}

    DispAllHouses(recCnt);       
}
}

function DispAllHouses(rec)
{
  
  var boolInd = false;
  var vLat;
  var vLng;
     
       if (parseInt(rec) <= 50 && parseInt(rec) >0)
        {
            loadXML("../Search/Misc/OfficeMap.xml", rec);
             boolInd = true;
        }
        else
        {
        boolInd = false;
        }

         map.removeOverlay(waitMarker);
 
      lat = document.getElementById("MapHome1_hdnIndexLat").value;
      lng = document.getElementById("MapHome1_hdnIndexLng").value;
     
       var point = new GLatLng(parseFloat(lat),parseFloat(lng));
       map.addOverlay(createMarker(point,["Street View", "Direction"]));
     
      if (boolInd == true)
      {
        if(window.ActiveXObject)
        {
          for (var i = 0; i < xmlObj.childNodes.length; i++) {
          document.getElementById("MapHome1_hdnVar").value = parseInt(document.getElementById("MapHome1_hdnVar").value) + 1;
          
          if(xmlObj.childNodes(i).childNodes(16).firstChild.text != "0")
          {
         var point = new GLatLng(parseFloat(xmlObj.childNodes(i).childNodes(16).firstChild.text),parseFloat(xmlObj.childNodes(i).childNodes(17).firstChild.text));
         map.addOverlay(createHouseMarker(point, i));
         
            // ==== Each time a point is found, extent the bounds and include it =====
            bounds.extend(point);
         } 
       }
      }       
     
             // ===== determine the zoom level from the bounds =====
          map.setZoom(map.getBoundsZoomLevel(bounds));
         
          // ===== determine the centre from the bounds ======
          map.setCenter(bounds.getCenter());
          
     }  
       
         //document.getElementById("divLoading").style.display= "none";
}

function loadXML(xmlFile, recCnt) 
{ 
if (window.ActiveXObject)
  {
 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
 xmlDoc.async="false"; 
 xmlDoc.onreadystatechange=verify; 
 xmlDoc.load(xmlFile); 
 xmlObj=xmlDoc.documentElement; 

  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xmlDoc=document.implementation.createDocument("","",null);
  var isLoaded = xmlDoc.load(xmlFile);
  if (isLoaded == true)
  { xmlDoc.onload= function(){

    for (var i = 0; i < recCnt; i++) {
   
      document.getElementById("MapHome1_hdnVar").value = parseInt(document.getElementById("MapHome1_hdnVar").value) + 1;
      
      if(xmlDoc.getElementsByTagName("Latitude")[i].childNodes[0].nodeValue != "0")
      {
         var point = new GLatLng(parseFloat(xmlDoc.getElementsByTagName("Latitude")[i].childNodes[0].nodeValue),parseFloat(xmlDoc.getElementsByTagName("Longitude")[i].childNodes[0].nodeValue));
          map.addOverlay(createMarkerFF(point, i));
         
            // ==== Each time a point is found, extent the bounds and include it =====
            bounds.extend(point);
         } 
     }
  }
  }
  }
else
  {
  alert('Your browser cannot handle this script');
  return;
  }
}
function verify() 
{ 
 if (xmlDoc.readyState != 4) 
 { 
   return false; 
 } 
}  


function createHouseMarker(point, number) {
      
     //set icon
     var letteredIcon = new GIcon(baseIcon);  
     letteredIcon.image = "../Images/SearchMap/icon10.png";
 
      markerOptions = { icon:letteredIcon }; 
     
      var marker = new GMarker(point, markerOptions);
      marker.value = number;
      
      //gmarkers[iarr] = marker;
      
      
     var iInd = document.getElementById("MapHome1_hdnVar").value;
     var pImgUrl;
     var myHtml;


       if (xmlObj.childNodes(iInd-1).childNodes(18).firstChild.text == "No")
        { pImgUrl = '../../images/design/noimage.jpg'; }
        else
        { pImgUrl = 'http://images.watsonrealtycorp.com/newphotos/' + xmlObj.childNodes(iInd-1).childNodes(18).firstChild.text; }
        
            myHtml = "<table class='shadetabs' width='230px' border='0'><tr><td width='40%'><a href='/Search/SearchDetails.aspx?ID=" + xmlObj.childNodes(iInd-1).childNodes(0).firstChild.text + "&lat=" + xmlObj.childNodes(iInd-1).childNodes(16).firstChild.text + "&lg=" + xmlObj.childNodes(iInd-1).childNodes(17).firstChild.text + "&e=i' target=_blank><img src='" + pImgUrl + "' width=70 height=65 alt='House Image' border='0' onError='ImgError(this)' /></a>";
            myHtml += "</td><td align='left' width='60%'>"
          
            if(xmlObj.childNodes(iInd-1).childNodes(1).firstChild.text != "0")
            {
              myHtml += xmlObj.childNodes(iInd-1).childNodes(1).firstChild.text + " " 
            }
           
            myHtml += xmlObj.childNodes(iInd-1).childNodes(2).firstChild.text + "&nbsp;";
            
            var streetType = xmlObj.childNodes(iInd-1).childNodes(4).firstChild.text;
            if(streetType != 'Not Available')
              myHtml += streetType.substring(0,1).toUpperCase() + streetType.substring(1, streetType.length).toLowerCase();
            
            myHtml += "<br />Beds:&nbsp;";
            myHtml += xmlObj.childNodes(iInd-1).childNodes(9).firstChild.text + "<br />Baths:&nbsp;" +  xmlObj.childNodes(iInd-1).childNodes(12).firstChild.text +"<br />Price:&nbsp;";
            myHtml += xmlObj.childNodes(iInd-1).childNodes(5).firstChild.text + "<br />";
            myHtml += "<a href='/Search/SearchDetails.aspx?ID=" + xmlObj.childNodes(iInd-1).childNodes(0).firstChild.text + "&lat=" + xmlObj.childNodes(iInd-1).childNodes(16).firstChild.text + "&lg=" + xmlObj.childNodes(iInd-1).childNodes(17).firstChild.text + "&e=i' target=_blank>More Details<a></td></tr></table>"
    
                
             GEvent.addListener(marker,"click", function() {
                  map.openInfoWindowHtml(point, myHtml, {noCloseOnClick:false});
              });


	  return marker;
 }

function createMarkerFF(point, number) {
      
     //set icon
     var letteredIcon = new GIcon(baseIcon);  
     letteredIcon.image = "../Images/SearchMap/icon10.png";

      markerOptions = { icon:letteredIcon }; 
     
      var marker = new GMarker(point, markerOptions);
      marker.value = number;
      
      
     var iInd = document.getElementById("MapHome1_hdnVar").value;
     var pImgUrl;
     var myHtml;
     
        if (xmlDoc.getElementsByTagName("Photo")[iInd-1].childNodes[0].nodeValue == "No")
        { pImgUrl = '../../images/design/noimage.jpg'; }
        else
        { pImgUrl = 'http://images.watsonrealtycorp.com/newphotos/' + xmlDoc.getElementsByTagName("Photo")[iInd-1].childNodes[0].nodeValue; }
        
        var myHtml =  "<table class='shadetabs' width='230px' border='0'><tr><td width='40%'><a href='/Search/SearchDetails.aspx?ID=" + xmlDoc.getElementsByTagName("ID")[iInd-1].childNodes[0].nodeValue + "&lat=" + xmlDoc.getElementsByTagName("Latitude")[iInd-1].childNodes[0].nodeValue + "&lg=" + xmlDoc.getElementsByTagName("Longitude")[iInd-1].childNodes[0].nodeValue + "&e=i' target=_blank><img src='" + pImgUrl + "' width=70 height=65 alt='House Image' border='0'/></a>";
                 myHtml += "</td><td align='left' width='60%'>"
          
            if(xmlDoc.getElementsByTagName("StreetNumber")[iInd-1].childNodes[0].nodeValue != "0")
            {
              myHtml += xmlDoc.getElementsByTagName("StreetNumber")[iInd-1].childNodes[0].nodeValue + " " 
            }
            myHtml += xmlDoc.getElementsByTagName("Street")[iInd-1].childNodes[0].nodeValue + "&nbsp;";
            
            var streetType = xmlDoc.getElementsByTagName("StreetType")[iInd-1].childNodes[0].nodeValue;
            if(streetType != 'Not Available')
              myHtml += streetType.substring(0,1).toUpperCase() + streetType.substring(1, streetType.length).toLowerCase();
            
            myHtml += "<br />Beds:&nbsp;";
            myHtml += xmlDoc.getElementsByTagName("Bedrooms")[iInd-1].childNodes[0].nodeValue + "<br />Baths:&nbsp;" +  xmlDoc.getElementsByTagName("Baths")[iInd-1].childNodes[0].nodeValue +"<br />Price&nbsp;";
            myHtml += xmlDoc.getElementsByTagName("Price")[iInd-1].childNodes[0].nodeValue + "<br />";
            myHtml += "<a href='/Search/SearchDetails.aspx?ID=" + xmlDoc.getElementsByTagName("ID")[iInd-1].childNodes[0].nodeValue + "&lat=" + xmlDoc.getElementsByTagName("Latitude")[iInd-1].childNodes[0].nodeValue + "&lg=" + xmlDoc.getElementsByTagName("Longitude")[iInd-1].childNodes[0].nodeValue + "&e=i' target=_blank>More Details<a></td></tr></table>"
     
      
     GEvent.addListener(marker,"click", function() {
          map.openInfoWindowHtml(point, myHtml, {noCloseOnClick:false});
      });
       
       
     
       //htmls[iarr] = myHtml;
       // _arrIDSno[iarr] = iarr + '|' + xmlDoc.getElementsByTagName("ID")[iInd-1].childNodes[0].nodeValue
  
    //iarr++;
    
	  return marker;
 }


function createMarker(point, tabNames) {
      
        var icon = new GIcon();

    icon.image = "../Images/Icons/watsonlogo.gif";
    icon.shadow = "../Images/Icons/shadow-watsonlogo.png";
    icon.iconSize = new GSize(32.0, 17.0);
    icon.shadowSize = new GSize(42.0, 19.0);
    icon.iconAnchor = new GPoint(16.0, 8.0);
    icon.infoWindowAnchor = new GPoint(16.0, 8.0);

  
     //set icon
     var letteredIcon = new GIcon(icon);  
    letteredIcon.image = "../Images/Icons/watsonlogo.gif";
   
      var markerOptions = { icon:letteredIcon }; 
     
      var marker = new GMarker(point, markerOptions);
      
      var i = 0;
      var html = "";
      
html = "<div style='text-align:left'><b>" + document.getElementById("MapHome1_hdnCompleteAddress").value + "&nbsp;" ;
html += "</b><br>"
html += "Ph&nbsp;&nbsp;: " + document.getElementById("MapHome1_hdnContact").value + "&nbsp;&nbsp;"
html += "Fax&nbsp;: " + document.getElementById("MapHome1_hdnFax").value
html += "</div><br>";
html +=  '<div style="text-align:left">' +
           '<input type="text" SIZE=30 MAXLENGTH=60 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" type="button" style="font-size:x-small" onClick="CallSForm()">' +
           '<input type="hidden" name="daddr" value="' + document.getElementById("MapHome1_hdnCompleteAddress").value + 
           '"/></div>';
    
      

              // The info window version with the "to here" form open
        to_htmls2[i] = html + '<div style="text-align:left">Directions: <b>To here</b> - <a href="javascript:from(' + i + ')">From here</a>' +
           '<br>Start address:<br>' +
           '<input type="text" SIZE=30 MAXLENGTH=60 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" type="button" style="font-size:x-small" onClick="CallSForm()">' +
           '<input type="hidden" name="daddr" value="' + document.getElementById("MapHome1_hdnCompleteAddress").value +
           '"/></div>';
    
        from_htmls2[i] = html + '<div style="text-align:left">Directions: <a href="javascript:toDir(' + i + ')">To here</a> - <b>From here</b>' +
           '<br>End address:<br>' +
           '<input type="text" SIZE=30 MAXLENGTH=60 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" type="button" style="font-size:x-small" onClick="CallSForm()">' +
           '<input type="hidden" name="saddr" value="' + document.getElementById("MapHome1_hdnCompleteAddress").value +
           '"/></div>';
    
        // The inactive version of the direction info
       // html += '<div style="text-align:left">Directions: <a href="javascript:toDir('+i+')">To here</a> - <a href="javascript:from('+i+')">From here</a></div>';
        
        
        var a= 10;
        var OfficeImg = document.getElementById("MapHome1_hdnPhoto").value;
        
         GEvent.addListener(marker,"mouseover", function() {
                var tabs = [];
                tabs.push(new GInfoWindowTab(tabNames[0],"<div style='text-align:center'><img src='http://images.watsonrealtycorp.com/Office_images/"+ OfficeImg + "' style='width:200px;height:80px;' /><br><a href='javascript:ShowStreetForm()'>Click here</a> for Street view</div>"));
                tabs.push(new GInfoWindowTab(tabNames[1],html)); 
          
          marker.openInfoWindowTabsHtml(tabs);
           });

        gmarkers2[i] = marker;
        
        htmls2[i] = html;
        
        

      	  return marker;

}

 function dispWaitIcon()
{          
// to display the waiticon
//SECTION STARTS
    var center = map.getCenter();
          
    var baseIcon2 = new GIcon(); 
  
    baseIcon2.iconAnchor = new GPoint(9, 34); 

      var markerIcon2 = new GIcon(baseIcon2); 
      
     markerIcon2.image = "../RadControls/Window/Skins/Default2006/Img/Splash.gif";
     markerOptions2 = { draggable: false, icon: markerIcon2 }; 

    waitMarker = new GMarker(center, markerOptions2); 
   
    map.addOverlay(waitMarker);
   
//SECTION ENDS
}


    
