// JScript File
var pins = new Array(null,null);
var pushpinOnMap = false;
var CurrCount=null;
var map  = null;
dataArray=new Array();

var isPolygon = 0;
var drawSquar = 0;
var cntPoint = 0;
var isAddCircle = 0;  
var points = new Array(null);
var cpoints = new Array(null);
var num =0;
var isAddCircleYes = 0;
var isPolygonAlready = 0;
var PolygonType = null;

var leastLatitude = 0;
var highestLatitude = 0;
var leastLongitude = 0;
var highestLongitude = 0;

var lastLatitude = 0;
var lastLongitude = 0;
var firstLatitude = 0;
var firstLongitude = 0;

var pushpinOnMap = false;       
var interestRate = 0;       
        
var tempPoints = null;
dataArray=new Array(); 


var zeroPropertyMessage;
var zeroPropertyFound;
var tooManyPropertyMessage;
var tooManyPropertyFound;

function OnPageLoad2()
{   
    zeroPropertyMessage = '<b class="rtop"><b class="r1"></b> <b class="r2"></b> <b class="r3"></b> <b class="r4"></b></b>';
    zeroPropertyMessage += '<table width="100%" cellpadding="0" cellspacing="0" class="container"><tr style="height:20px">';
    zeroPropertyMessage += '<td align="center" valign="middle" class="Verdana11black">No property found</td></tr></table>';
    zeroPropertyMessage += '<b class="rbottom"><b class="r4"></b> <b class="r3"></b> <b class="r2"></b> <b class="r1"></b></b>';
    
    zeroPropertyFound = '<table width="100%" cellpadding="0" cellspacing="0"><tr><td style="left:10px" align="center" valign="middle" class="VerdanaBold12"><span class="VerdanaBold14">0/  TOTALPROPERTIES </span></td>';
    zeroPropertyFound += '</tr></table>';
    
    tooManyPropertyMessage = '<b class="rtop"><b class="r1"></b> <b class="r2"></b> <b class="r3"></b> <b class="r4"></b></b>';
    tooManyPropertyMessage += '<table width="100%" cellpadding="0" cellspacing="0" class="container"><tr style="height:20px">';
    tooManyPropertyMessage += '<td align="center" valign="middle" class="Verdana11black">Too many properties, narrow your search results to less than 300</td></tr></table>';
    tooManyPropertyMessage += '<b class="rbottom"><b class="r4"></b> <b class="r3"></b> <b class="r2"></b> <b class="r1"></b></b>';
    
    tooManyPropertyFound = '<table width="100%" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle" class="VerdanaBold12" style="left:10px"><span class="VerdanaBold14">TOTALPROPERTY /  TOTALPROPERTIES</span></td>';
    tooManyPropertyFound += '</tr></table>';
    document.getElementById("SearchingDiv").style.visibility = "hidden";
     
}
var layer = null;
function onSearchPropertyComplete(Properties) {
try{

 document.getElementById("SearchingDiv").style.visibility = "hidden";
 document.getElementById("divPropertyFoundMaster").style.visibility = "visible";
        if(pushpinOnMap == true) {
            map.DeleteAllShapes();
        }
	if(Properties.length > 0){
	  
	    CurrCount=Properties.length;
		var charwidth=6;
		var i = 0;
		var j;
		var pinid = 1;
		
		dataArray=new Array(Properties.length);
		indexArray=new Array();
		dataArrayImg=new Array();
		
		for(i=0;i<Properties.length;i++){
			
		        indexArray.push(i);
		        
		        
			    dataArray[i]=new Array(8);
			    			    
			    dataArray[i][0]=Properties[i]["Lng"];
			    dataArray[i][1]=Properties[i]["Lat"];
			    dataArray[i][2]=Properties[i]["ZipCode"];
			    dataArray[i][3]=Properties[i]["MSLNUM"];
			    dataArray[i][4]=Properties[i]["Beds"];
			    dataArray[i][5]=Properties[i]["Baths"];
			    dataArray[i][6]=Properties[i]["AreaNum"];
			    dataArray[i][7]=Properties[i]["Address"];
			    dataArray[i][8]=Properties[i]["DisProAddress"];
			    dataArray[i][9]=Properties[i]["Remarks"];
			    dataArray[i][10]=Properties[i]["YearBuilt"];
			    dataArray[i][11]=Properties[i]["Garages"];
			    dataArray[i][12]=Properties[i]["Acres"];
			    dataArray[i][13]=Properties[i]["Levels"];
			    dataArray[i][14]=Properties[i]["ListPrice"];
			    dataArray[i][15]=Properties[i]["Rooms"];
			    dataArray[i][16]=Properties[i]["PropertyType"];
			    dataArray[i][17]=Properties[i]["ProAddress"];
				dataArray[i][18]=Properties[i]["Status"];
				dataArray[i][19]=Properties[i]["Town"];
				dataArray[i][20]=Properties[i]["Taxes"];
				dataArray[i][21]=Properties[i]["SqFt"];
				dataArray[i][22]=Properties[i]["Bookmark"];
				dataArray[i][23]=Properties[i]["BookmarkUID"];
				dataArray[i][24]=Properties[i]["Records"];
				
				  if(i == 0){
				    
			            var icon = "<div id='housePin2'><div id='pinNumber2'>" + pinid +"</div></div>";
			            
			            var Lati = Properties[i]['Lat'];
                        var Longi = Properties[i]['Lng'];
                          //Add a pushpin
                        var shapeLayer = new VEShapeLayer();
                        var shape = new VEShape(VEShapeType.Pushpin,new VELatLong(Number(Lati), Number(Longi)));
                        shape.SetCustomIcon(icon);
                         //Set the info box
                        //map.ClearInfoBoxStyles();
                    
                        //shape.SetTitle("");
                        //shape.SetDescription("Hello");
                        var myObject=new Object();
                        myObject.ID=pinid;
                        shape.data = myObject; 
                       
                       // map.AttachEvent('onmouseover', OnMouseOverV5);
                        
                      
                        shapeLayer.AddShape(shape);
                       
                                   
                        map.AddShapeLayer(shapeLayer);
                        
                        pushpinOnMap = true;
                            
                       
                        pinid++;
                        j = i;
                       
                        
			        
			    }
			    else{
			        if(dataArray[i-1][3] != dataArray[i][3]){
			            var icon = "<div id='housePin2' ><div id='pinNumber2'>" + pinid +"</div></div>";
                        var Lati = Properties[i]['Lat'];
                        var Longi = Properties[i]['Lng'];
                          //Add a pushpin
                        var shapeLayer = new VEShapeLayer();
                        var shape = new VEShape(VEShapeType.Pushpin,new VELatLong(Number(Lati), Number(Longi)));
                        shape.SetCustomIcon(icon);
                         //Set the info box
                        //map.ClearInfoBoxStyles();
                    
                        //shape.SetTitle("");
                        //shape.SetDescription("Hello");
                        var myObject=new Object();
                        myObject.ID=pinid;
                        shape.data = myObject; 
                       // map.AttachEvent('onmouseover', OnMouseOverV5); 
                      
                        shapeLayer.AddShape(shape);
                       
                                   
                        map.AddShapeLayer(shapeLayer);
                        
                        pushpinOnMap = true;
                            
                       
                        pinid++;
                        j = 1;

         
			         }
			         else{
			           if(dataArray[i][23] == document.getElementById("hidsessUserID").value){
			                dataArray[j][22] = "Yes";
		           
			           }
			         }
			         
			    }
			    
		

            
		};
			
		 var propFound = tooManyPropertyFound.replace("TOTALPROPERTY",Properties.length);
		
		 propFound = propFound.replace("TOTALPROPERTIES",document.getElementById("hidTotalRecord").value);
		
		 document.getElementById("divPropertyFoundMaster").innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"tcontainer\"><tr style=\"height:20px\"><td align=\"center\" valign=\"middle\" class=\"Verdana11white\">"+propFound+"</td></tr></table><b class=\"rtbottom\"><b class=\"rt4\"></b> <b class=\"rt3\"></b> <b class=\"rt2\"></b><b class=\"rt1\"></b></b>";
		
		
	}		
}
catch(ex) {
        alert(ex.message);
            
     }	

		
};
function OnMouseOverV5(e) {
    try
    { 
        if (e.elementID) 
        { 
            var pin= map.GetShapeByID(e.elementID);
            var ID = pin.data.ID;
            
            
            map.ShowInfoBox(pin); 


        }
    } 

    catch(ex)
    {
        alert (ex.message);
    }
} 


////POLYGON

function StartDrawPolygon() {
     try {
        PolygonType = "";
        map.DeleteAllShapes();
        map.DeleteAllPolylines();
        map.DeleteAllPolygons();
        document.getElementById("SearchingDiv").style.visibility = "hidden";
        document.getElementById("divMessage").style.visibility = "hidden";
        document.getElementById("myMap").style.cursor = "crosshair";
        isPolygon = 1;
        drawSquar = 0;
        var i = 0;
        cntPoint = 0;
        drawPoly = 1;
        points.splice(0,points.length);
        cpoints.splice(0,cpoints.length);
        
        map.AttachEvent('onclick', mapClick);
        map.AttachEvent('onmousemove', mapMouseMove);//    
        map.DetachEvent("onclick",MouseHandler);             
        map.vemapcontrol.EnableGeoCommunity(true);
        PolygonType = "Polygon";
    }
    catch(ex) {
        alert(ex.message);
    }
 };
 
 
 //SQUARE
 
 function DrawSquare() {
     try {
       
        
        isPolygon = 0;
        isAddCircle = 0;
        drawSquar = 1;
        map.DeleteAllPolylines();
        map.DeleteAllPolygons();
        document.getElementById("SearchingDiv").style.visibility = "hidden";
        document.getElementById("divMessage").style.visibility = "hidden";
        document.getElementById("myMap").style.cursor = "crosshair";
        map.DeleteAllShapes();
        highestLatitude = 0;
        highestLongitude = 0;
        leastLongitude = 0;
        leastLatitude = 0;
        map.AttachEvent('onclick', mapClick);
        map.AttachEvent('onmousemove', mapMouseMove);
        map.DetachEvent("onclick",MouseHandler); 
        map.vemapcontrol.EnableGeoCommunity(true);
    }
    catch(err) {
        alert(err.message);
    }
 };
 
 //CIRCLE
 
 function startAddCircle() {
    try {
        PolygonType = "";
        
        map.DeleteAllPolylines();
        map.DeleteAllPolygons();
        document.getElementById("SearchingDiv").style.visibility = "hidden";
        document.getElementById("divMessage").style.visibility = "hidden";
        document.getElementById("myMap").style.cursor = "crosshair";
        map.DeleteAllShapes();
        
        map.AttachEvent('onclick', mapClick);
        map.AttachEvent('onmousemove', mapMouseMove);
        map.DetachEvent("onclick",MouseHandler); 
        map.vemapcontrol.EnableGeoCommunity(true);
        isAddCircle = 1;
        drawSquar = 0;
        PolygonType = "Circle";
    }
    catch(err) {
        alert(err.message);
    }
 }
 
  function AddCircle(radius) {
    try {
       
        map.DeleteAllPolylines();
        map.DeleteAllPolygons();

        latin = circleLatitude;
        lonin = circleLongitude;
        var locs = new Array(); 
        var lat1 = latin * Math.PI/180.0; 
        var lon1 = lonin * Math.PI/180.0; 
        var d = radius/3956; 
        var x; 
        for (x = 0; x <= 360; x++) 
        { 
            var tc = (x / 90)* Math.PI / 2; 
            var lat = Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc)); 
            lat = 180.0 * lat / Math.PI; 
            var lon; 
             if (Math.cos(lat1)==0) 
            { 
                lon=lonin; // endpoint a pole 
            } 
            else 
            { 
                lon = ((lon1 - Math.asin(Math.sin(tc) * Math.sin(d)/Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI; 
            } 
            lon = 180.0 * lon / Math.PI; 
            var loc = new VELatLong(lat,lon); 
            locs.push(loc); 
        } 
        var poly = new VEPolyline(999, locs, new VEColor(255,0,0,1) , 4); 
        map.AddPolyline(poly);
        isAddCircleYes = 1;
        isAddCircle = 0;
        searchCircle();

        map.DetachEvent('onclick', mapClick);
        map.DetachEvent('onmousemove', mapMouseMove);

        
        map.vemapcontrol.EnableGeoCommunity(false);
    }
    catch(err) {
        alert(err.message);
    }
 }
 
 var circleLatitude = 0;
 var circleLongitude = 0;
 var circleMiles = 5;
 
 function searchCircle() {
    try {
        
        if(isAddCircleYes == 1) {
            
              map.DeleteAllShapes();
            
            document.getElementById("SearchingDiv").style.visibility = "visible";
            document.getElementById("divMessage").style.visibility = "hidden";
            document.getElementById("divPropertyFoundMaster").style.visibility = "hidden";
           
            
           polysearch.GetPropertiesinCirclePolygon(circleLatitude,circleLongitude,circleMiles,document.getElementById("hidsessUserID").value,onGetSearchComplete);
            isAddCircle = 0;
        }
    }
    catch(err) {
        alert(err.message);
    }
 }
 function onGetSearchComplete(Properties){

  
 try {
 
    if(Properties.length > 0) {
       
              if(Properties[0]['Id'] == 0) {
                   
                       if(pushpinOnMap == true) {
                            map.DeleteAllShapes();
                        }
                   document.getElementById("SearchingDiv").style.visibility = "hidden";
                   document.getElementById("divPropertyFoundMaster").style.visibility = "visible";
                    var zeroProp = zeroPropertyFound.replace("TOTALPROPERTIES",document.getElementById("hidTotalRecord").value);
                    document.getElementById("divPropertyFoundMaster").innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"tcontainer\"><tr style=\"height:20px\"><td align=\"center\" valign=\"middle\" class=\"Verdana11white\">"+zeroProp+"</td></tr></table><b class=\"rtbottom\"><b class=\"rt4\"></b> <b class=\"rt3\"></b> <b class=\"rt2\"></b><b class=\"rt1\"></b></b>";
                    document.getElementById("divMessage").style.visibility = "visible";
                    document.getElementById("divMessage").innerHTML = zeroPropertyMessage;
                    
                    
              }
              else if(Properties[0]['Id'] == -100) {
              
                    if(pushpinOnMap == true) {
                        map.DeleteAllShapes();
                    }
                document.getElementById("divPropertyFoundMaster").style.visibility = "visible";
                document.getElementById("SearchingDiv").style.visibility = "hidden";
                document.getElementById("divMessage").style.visibility = "visible";
                document.getElementById("divMessage").innerHTML = tooManyPropertyMessage;
                
                var tooManyProp = tooManyPropertyFound.replace("TOTALPROPERTY",Properties[0]['Records']);
                tooManyProp = tooManyProp.replace("TOTALPROPERTIES",document.getElementById("hidTotalRecord").value);
                document.getElementById("divPropertyFoundMaster").innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"tcontainer\"><tr style=\"height:20px\"><td align=\"center\" valign=\"middle\" class=\"Verdana11white\">"+tooManyProp+"</td></tr></table><b class=\"rtbottom\"><b class=\"rt4\"></b> <b class=\"rt3\"></b> <b class=\"rt2\"></b><b class=\"rt1\"></b></b>";
                                 
              }
              else {
                        onSearchPropertyComplete(Properties);
                    
              }
         }
      }  
     catch(ex) {
        
     }
    
}
function mapClick(e) {

            try {
            document.getElementById("divMessage").style.visibility = "hidden";
             document.getElementById("SearchingDiv").style.visibility = "hidden";
                var x = e.mapX;
                var y = e.mapY;
                var pixel = new VEPixel(x, y);
                var latlong = map.PixelToLatLong(pixel);
                
                if (e.rightMouseButton) {
                    if (Number(isPolygon) == 1) {
                            lastLatitude = latlong.Latitude;
                            lastLongitude = latlong.Longitude;
                            
                            points.push(latlong);
                            cpoints.push(latlong);
                            points.push(points[0]);
                            var polygon = new VEPolygon('polygon_' + num++, points);
                            polygon.SetFillColor(new VEColor(170, 150, 150, 0.65));
                            polygon.SetOutlineColor(new VEColor(170, 150, 150, 1.0));
                            polygon.SetOutlineWidth(2);
                            map.AddPolygon(polygon);

                            try {
                                 map.DeletePolygon('tempPolygon');
                            } catch (err) {
                            }
                            
                            points = new Array();
                            map.DetachEvent('onclick', mapClick);
                            map.DetachEvent('onmousemove', mapMouseMove);
                            map.DetachEvent('onclick',MouseHandler); 
                            map.vemapcontrol.EnableGeoCommunity(false);
                            isPolygon = 2;
                     }
                 }
                
                 else if (Number(isPolygon) == 1) {
                        if (Number(drawPoly) == 1) {
                            firstLatitude = latlong.Latitude;
                            firstLongitude = latlong.Longitude;
                            
                            points.push(latlong);
                            cpoints.push(latlong);
                        }
                        drawPoly = Number(drawPoly)+1;
                        
                    }
                   
                if (Number(isPolygon) == 2) {
                            searchPolygon();
                     }
                else if(Number(isAddCircle) == 1) {
               
                    
                    map.DeleteAllShapes(); // if there is any poloygon on map it remove.
                    var pin = new VEPushpin(500, 
                        new VELatLong(latlong.Latitude,latlong.Longitude),
                        "images/redCircle.png",
                        " Lat ", 
                        " Lon"
                    );                                                  //for make point on map & draw circle.
                    VEPushpin.ShowDetailOnMouseOver = false;
                    
                    //map.AddPushpin(pin);        // push point on map.
                    //pushpinOnMap = true;
                    
                    circleLatitude = latlong.Latitude; // get circle latitude.
                    circleLongitude = latlong.Longitude; // get circle longitude.
                   
                    if(document.getElementById("txtRadius").value == "") {
                        circleMiles = 5;                              // if radius is not define then it take 5 radius
                    }
                    else {
                        circleMiles = document.getElementById("txtRadius").value; // it take radius of taxradius value.
                    }
                    AddCircle(circleMiles);  // add circle on map
                }
                else if(Number(drawSquar) == 2) {
                    map.DeleteAllPushpins();
                    lastLatitude = latlong.Latitude;; // get lastLatitude
                    lastLongitude = latlong.Longitude; // get lastLongitude.
                    CompleteSquare(); // go to CompleteSquare function.
                }
                else {
                    if(Number(drawSquar == 1)) {
                        map.DeleteAllPushpins();
                        leastLatitude = latlong.Latitude; // get leastlatitude on map
                        highestLatitude = latlong.Latitude; // get highestlatitude
                        leastLongitude = latlong.Longitude; // get leastlatitude
                        highestLongitude = latlong.Longitude; // get leastlongitude
                        firstLatitude = latlong.Latitude;     //get firstlatitude
                        firstLongitude = latlong.Longitude; // get firstlongitude

                        drawSquar = 2;  //set drawsquar to 2
                    }
                    points.push(latlong);
                    cpoints.push(latlong);
                }
            }
         catch(err) {
             alert(err.message);
         }
         }
         
 function mapMouseMove(e) {
            try {
             
                var x = e.mapX;
                var y = e.mapY;
                var pixel = new VEPixel(x, y);
                var latlong = map.PixelToLatLong(pixel);
                
                if(Number(isPolygon) == 1) {
                    tempPoints = points.slice(0, points.length);
                    try {
                        map.DeletePolygon('tempPolygon');
                    } catch (err) {
                    }
                    tempPoints.push(latlong);
                    var polygon = new VEPolygon('tempPolygon', tempPoints);
                    polygon.SetFillColor(new VEColor(170, 150, 150, 0.65));
                    polygon.SetOutlineColor(new VEColor(170, 150, 150, 1.0));
                    polygon.SetOutlineWidth(2);
                    map.AddPolygon(polygon);
                }
                else if(Number(drawSquar) == 2) {
             
                    try {
                        map.DeletePolygon('tempPolygon');
                    } catch (err) {
                    }

                    if(firstLatitude < latlong.Latitude) {
                        highestLatitude = latlong.Latitude;
                    }
                    else {
                        leastLatitude = latlong.Latitude;
                    }
                    if(firstLongitude < latlong.Longitude) {
                        highestLongitude = latlong.Longitude;
                    }
                    else {
                        leastLongitude = latlong.Longitude;
                    }
                    
                    var squarePoly = new Array();
                    squarePoly.push(new VELatLong(highestLatitude,highestLongitude));
                    squarePoly.push(new VELatLong(highestLatitude,leastLongitude));
                    squarePoly.push(new VELatLong(leastLatitude,leastLongitude));
                    squarePoly.push(new VELatLong(leastLatitude,highestLongitude));
                    squarePoly.push(new VELatLong(highestLatitude,highestLongitude));
                                    
                     var polygon = new VEPolygon('tempPolygon', squarePoly);
                    polygon.SetFillColor(new VEColor(170, 150, 150, 0.65));
                    polygon.SetOutlineColor(new VEColor(170, 150, 150, 1.0));
                    polygon.SetOutlineWidth(2);
                    map.AddPolygon(polygon);
                    
                }
            }
         catch(err) {
             alert(err.message);
         }
        }
            
   function searchPolygon() {
    try {
            if(Number(isPolygon) > 0) {
             document.getElementById("SearchingDiv").style.visibility = "visible";
             document.getElementById("divMessage").style.visibility = "hidden";
             document.getElementById("divPropertyFoundMaster").style.visibility = "hidden";
                    if(Number(cntPoint) > 0) {
                        CompleteDrawPolygon();
                    }
                    var leastLatitude = cpoints[0].Latitude;
                    var highestLatitude = cpoints[0].Latitude;
                    var leastLongitude = cpoints[0].Longitude;
                    var highestLongitude = cpoints[0].Longitude;
                    var strPolygonPoint = "";                
                    
                    for(i=0;i<cpoints.length;i++) {
                        if (leastLatitude > cpoints[i].Latitude) {
                            leastLatitude = cpoints[i].Latitude;
                        }
                        if (Math.abs(leastLongitude) > Math.abs(cpoints[i].Longitude)) {
                            leastLongitude = cpoints[i].Longitude;
                        }
                        if (highestLatitude < cpoints[i].Latitude) {
                            highestLatitude = cpoints[i].Latitude;
                        }
                        if (Math.abs(highestLongitude) < Math.abs(cpoints[i].Longitude)) {
                            highestLongitude = cpoints[i].Longitude;
                        }
                        if(i == 0) {
                            strPolygonPoint = cpoints[i].Latitude+","+cpoints[i].Longitude;
                        }
                        else {
                            strPolygonPoint += ","+cpoints[i].Latitude+","+cpoints[i].Longitude;
                        }
                    }
                     
                      var LLrect;
   
	        LLrect = map.GetMapView();
	        var currentZoom = map.GetZoomLevel();
	        if(currentZoom < 4) {
	            map.SetZoomLevel(4);
	        }
	    
            var Y1= LLrect.TopLeftLatLong.Latitude;
            var X1= LLrect.TopLeftLatLong.Longitude;
            var pixel = new VEPixel(X1, Y1+35);
            var ll = map.PixelToLatLong(pixel).toString();
            var llString = ll.split(",");
            Y1 = llString[0];
            X1 = llString[1];
            var Y2= LLrect.BottomRightLatLong.Latitude;
            var X2= LLrect.BottomRightLatLong.Longitude;
                    polysearch.GetPropertiesinPolygon(highestLatitude,highestLongitude,leastLatitude,leastLongitude,strPolygonPoint,cpoints.length,document.getElementById("hidsessUserID").value,Y1,X1,Y2,X2,onGetSearchComplete);//UserID also Include Saved Search Name if Saved facility available. go to the webservice function. 
                    
                    map.DetachEvent('onclick', mapClick);
                    map.DetachEvent('onmousemove', mapMouseMove);
                    map.DetachEvent('onclick',MouseHandler); 
                    map.vemapcontrol.EnableGeoCommunity(false);
                    
                }
            }
            catch(err) {
                alert(err.message);
            }
         }
         
  function DrawPoly(id,points,width,color) {
    try {
        map.DeleteAllPolylines();
        poly = new VEPolyline(id,points);
        poly.SetWidth(width);
        poly.SetColor(color);
        map.AddPolyline(poly);
       
    }
    catch(err) {
        alert(err.message);
    }
}
function CompleteDrawPolygon() {
    try {
   
        if(Number(isPolygon) > 0) {
            map.DeleteAllPolylines();
            points[cntPoint] = points[0];
            cpoints[cntPoint] = points[0];
            poly = new VEPolyline(cntPoint,points);
            poly.SetWidth(3);
            poly.SetColor(new VEColor(255,0,0,1));

            map.AddPolyline(poly);
            var i = 0;
            /*while (i < points.length) {
                points.splice(i, 1);
                i++;
            }*/
            points.splice(0,points.length);
            cntPoint = 0;
            document.getElementById("SearchingDiv").style.visibility = "visible";
        }
    }
    catch(err) {
        alert(err.message);
    }
 };
  function removeSearchPolygon() {
   try {
        
        map.DeleteAllPolylines();
        map.DeleteAllShapes();
        map.DeleteAllPolygons();
        
        isAddCircle = 0;
        isAddCircleYes = 0;
        drawSquar = 0;
        isPolygon = 0;
        isPolygonAlready = 0;
        
        var i = 0;
        /*while (i < cntPoint.length) {
            cntPoint.splice(i, 1);
            i++;
        }*/
        cntPoint = 0;
        cpoints.splice(0, cpoints.length);
      
        map.DetachEvent('onclick', mapClick);
        map.DetachEvent('onmousemove', mapMouseMove);
        map.AttachEvent('onclick',MouseHandler); 
        map.vemapcontrol.EnableGeoCommunity(false);
        document.getElementById("SearchingDiv").style.visibility = "hidden";
        document.getElementById("divMessage").style.visibility = "hidden";
        document.getElementById("divPropertyFoundMaster").style.visibility = "hidden";
        document.getElementById("myMap").style.cursor = "";
             
    }
    catch(err) {
        alert(err.message);
    }
 }
  function CompleteSquare() {
    try {
    
        map.DeletePolygon('tempPolygon');
    } catch (err) {
    }
    try {  
        
                   
        if(leastLatitude < lastLatitude) {
            highestLatitude = lastLatitude;
        }
        else {
            leastLatitude = lastLatitude;
        }
        if(leastLongitude < lastLongitude) {
            highestLongitude = lastLongitude;
        }
        else {
            leastLongitude = lastLongitude;
        }
       
        var squarePoly = new Array();
        squarePoly.push(new VELatLong(highestLatitude,highestLongitude));
        squarePoly.push(new VELatLong(highestLatitude,leastLongitude));
        squarePoly.push(new VELatLong(leastLatitude,leastLongitude));
        squarePoly.push(new VELatLong(leastLatitude,highestLongitude));
        squarePoly.push(new VELatLong(highestLatitude,highestLongitude));
                        
        var polygon = new VEPolygon('polygon_' + num++, squarePoly);
        polygon.SetFillColor(new VEColor(170, 150, 150, 0.65));
        polygon.SetOutlineColor(new VEColor(170, 150, 150, 1.0));
        polygon.SetOutlineWidth(2);
        map.AddPolygon(polygon);
        drawSquar = 0;
        
        searchSquare(highestLatitude,highestLongitude,leastLatitude,leastLongitude);
        
        points = new Array();
        
    }
    catch(err) {
        alert(err.message);
    }
 }
 function searchSquare(highLat,highLon,lowLat,lowLon) {
    try {
    
        map.DeleteAllPushpins();
        document.getElementById("SearchingDiv").style.visibility = "visible";
        document.getElementById("divMessage").style.visibility = "hidden";
        document.getElementById("divPropertyFoundMaster").style.visibility = "hidden";
       
       
        polysearch.GetPropertiesinSquarePolygon(highLat,lowLon,lowLat,highLon,document.getElementById("hidsessUserID").value,onGetSearchComplete);
            
        map.DetachEvent('onclick', mapClick);
        map.DetachEvent('onclick',MouseHandler); 
        map.DetachEvent('onmousemove', mapMouseMove);
        map.vemapcontrol.EnableGeoCommunity(false);
    }
    catch(ex) {
        alert(ex.message);
    }
}
