function clearSearchResult() {
    gMapObj.deleteAllShapes();
    myPoints.length = 0;
    document.getElementById('resultDiv').innerHTML = "";
    document.getElementById('divMapListings').innerHTML = "";
    document.getElementById('divListLoading').innerHTML = "";

}

function startDraw() {
    try {

        gMapObj.deleteAllShapes();
        myPoints.length = 0;
        gMapObj.map.DetachEvent("onendpan", EventEndCPan);
        gMapObj.map.DetachEvent("onendzoom", EventEndCPan);
   }
    catch(err) {}
    
    gMapObj.map.AttachEvent("onmouseup", DrawPolyMouseClick);
    gMapObj.map.AttachEvent("ondoubleclick", DrawPolyMouseClick);
    document.getElementById("myMap").childNodes[0].style.cursor = 'crosshair';

}

function MouseMove(e) {
    var x = e.mapX;
    var y = e.mapY;
    pixel = new VEPixel(x, y);
    var LL = gMapObj.map.PixelToLatLong(pixel);


}

function DrawPolyMouseClick(e) {
    var x = e.mapX;
    var y = e.mapY;
    pixel = new VEPixel(x, y);
    var LL = gMapObj.map.PixelToLatLong(pixel);
    
    gMapObj.addPolylinePoint(LL);
    gMapObj.addPolygonPoint(LL);

    if (myPoints.length == 0 && myGeomType != "point") {

        gMapObj.map.AttachEvent("onmousemove", DrawPolyMouseMove);

    }

    myPoints.push(LL);
    
    if (myPoints.length == 1) {
        firstLL = LL;
    }

    if (myPoints.length == 8) {
        alert('The maximum number of points to define your polygon is 8');
    }

    if (e.eventName == 'ondoubleclick' || e.rightMouseButton || myPoints.length == 8) {
        if (LL != firstLL) {
            myPoints.push(firstLL);

        }

        try {
            gMapObj.map.DetachEvent("onmousemove", DrawPolyMouseMove);
            gMapObj.map.DetachEvent("onmouseup", DrawPolyMouseClick);
            gMapObj.map.DetachEvent("ondoubleclick", DrawPolyMouseClick);

        }catch(err) {
        }
        
        gMapObj.polylineLayer.DeleteAllShapes();
		//gMapObj.polygonShape=1;
		gMapObj.polygonShape.SetFillColor(new VEColor(0, 0, 111, .1));
        gMapObj.polygonShape.SetLineColor(new VEColor(0, 0, 111, 1));
        gMapObj.polygonShape.SetLineWidth(1);

       getPolypoints.defer(200);
	  // getPolypoints();
        
    }
}

function getPolypoints() {

    var myPolyPoins = "";
    var myPolyLats = "";
    var myPolyLongs = "";
    var myTempPoly = "";
    url = "getPointsInPoly.cfm?all=1";
    icon = "poly";
	//alert('in getpolypoints()');
    
    gMapObj.panAndZoomToPolygonPoints();
	/*
	alert('1 in getPolypoints' + gMapObj.polygonShape);
	 if(gMapObj.polygonLayer.GetShapeCount() > 0){
				alert('initialize polygonshape in getpolypoints');
                polygonShape = gMapObj.polygonLayer.GetShapeByIndex(0)
            }
			*/
    HAR.PropertySearchApp.submitSearchRequest();
    //updatepropcount();
	//alert('2 in getPolypoints ' + gMapObj.polygonShape);
    document.getElementById("myMap").childNodes[0].style.cursor = '';
    
}

function DrawPolyMouseMove(e) {
    var LL = getLatLongFromXY(e.mapX, e.mapY);

    tempPoints = myPoints.slice(0, myPoints.length);
    tempPoints.push(LL);

    gMapObj.drawPolyline(LL);
    if(tempPoints.length > 2){
        gMapObj.drawPolygon(LL);
    }

}

function DeleteShape() {
    clearSearchResult();
    deletePoly();
    try {
        gMapObj.map.DeleteAllPushpins();

    }
    catch(err) {}

}

/*
function deletePoly() {
	alert('in deletePoly');
    gMapObj.map.DeleteAllPolygons();
    gMapObj.map.DeleteAllShapes();

    myPoints = new Array();
    //alert(myPoints.length);
    tempPoints = null;
    try {
        gMapObj.map.DeleteRoute();

    }
    catch(err) {}


}*/

function doSort(list) {
    var inputString = "";
    var inputNumbers = list.split(",");
    for (var i = 0; i < inputNumbers.length - 1; i++) {
        inputNumbers[i] = parseFloat(inputNumbers[i], 10);
        //alert(inputNumbers[i]);
  }

    inputNumbers = bubbleSort(inputNumbers, 0, inputNumbers.length - 1);

    return inputNumbers;

}

function bubbleSort(inputArray, start, rest) {

    var tempValue;
    for (var i = rest - 1; i >= start; i--) {
        for (var j = start; j <= i; j++) {

            //isNaN(inputNumbers[i])
            if (inputArray[j + 1] != ',' && inputArray[j] != ',') {
                if (inputArray[j + 1] < inputArray[j]) {
                    tempValue = inputArray[j];
                    //alert(tempValue);
                    inputArray[j] = inputArray[j + 1];
                    inputArray[j + 1] = tempValue;

                }

            }

        }

    }
    //alert('after sort:'+ inputArray);
    return inputArray;
    //alert(inputArray);

}

function getLatLongFromXY(x, y) {
    var pixel = new VEPixel(x, y);
    return gMapObj.map.PixelToLatLong(pixel);

}

