var notOver = true;

var addBookmarkObj = {
	exec:function(title, url) 
	{
		// user agent sniffing is bad in general, but this is one of the times 
		// when it's really necessary
		var ua=navigator.userAgent.toLowerCase();
		var isKonq=(ua.indexOf('konqueror')!=-1);
		var isSafari=(ua.indexOf('webkit')!=-1);
		var isMac=(ua.indexOf('mac')!=-1);
		var buttonStr=isMac?'Command/Cmd':'CTRL';

		if(window.external && (!document.createTextNode || (typeof(window.external.AddFavorite)=='unknown'))) 
		{
			// IE4/Win generates an error when you
			// execute "typeof(window.external.AddFavorite)"
			// In IE7 the page must be from a web server, not directly from a local 
			// file system, otherwise, you will get a permission denied error.
			window.external.AddFavorite(url, title); // IE/Win
		} 
		else if(isKonq) 
		{
			alert('You need to press CTRL + B to bookmark our site.');
		}
		else if(window.opera) 
		{
			void(0); // do nothing here (Opera 7+)
		}
		else if(window.home || isSafari)
		{ // Firefox, Netscape, Safari, iCab
			alert('Unfortunately your browser does not support bookmarking within a web page. After clicking \"OK\" below please use '+buttonStr+' + D to bookmark this listing');
		}
		else if(!window.print || isMac) 
		{ // IE5/Mac and Safari 1.0
			alert('You need to press Command/Cmd + D to bookmark our site.');    
		}
		else 
		{
			alert('In order to bookmark this site you need to do so manually '+ 'through your browser.');
		}
	}
};

//Mapping Global Variables
var MapingID = null;
var map = null;
var WhereAmI = 0; 		
var VToken = "<%=GetVECustomerToken() %>";
var pinInfo = new Array();
var points = null;
var tempPoints = null;
var num = 0;  
var tempShape = null;
var startPin = null;
var shape = null;
var polyLayer = null;
var polyLayer2 = null;
var startPointSet = null;
var selecting = null;
var startX = null;
var startY = null;
var StartLatLon = null;
var currentX = null;
var currentY = null;
var EndLatLon = null;
var selectBox = null;
var polyline = null;
var polylineMask = null;
var moving = false;
var currentDragHandle = null;
var pinLayer = null;
var pointIndex = null;

var Mapping = {
	AddListPushPin: function (lat, lon, count, label) {
		var pointHolder = new VELatLong(parseFloat(lat), parseFloat(lon));
		var imageSTR = "/images/pushpin.png";
		pinInfo[count] = new VEShape(VEShapeType.Pushpin, pointHolder);
		pinInfo[count].SetCustomIcon(imageSTR);
		pinInfo[count].SetTitle("");
		pinInfo[count].SetDescription(label);
		pinInfo[count].PinID = count;
		map.AddShape(pinInfo[count]);
	},
	ClearMap: function () {
		if (tempShape) {
			map.DeleteShape(tempShape);
		}
		if (startPin) {
			map.DeleteShape(startPin);
		}
		if (selectBox) {
			map.DeleteShape(selectBox);
		}
		if (polylineMask) {
			map.DeleteShape(polylineMask);
		}
		if (pinLayer) {
			map.DeleteShapeLayer(pinLayer);
		}
		map.DetachEvent("onmousemove", EditRecMoveHandler);
		map.DetachEvent("onmousedown", EditRecDownHandler);
		map.DetachEvent("onmouseup", EditRecUpHandler);
		map.DetachEvent("onmousemove", EditPolyMoveHandler);
		map.DetachEvent("onmousedown", EditPolyDownHandler);
		map.DetachEvent("onmouseup", EditPolyUpHandler);
		$("#propCount").hide();
		$(MapingID).val("");
	},
	CloseInfoBoxes: function (pinID) {
		if (map) {
			if (pinInfo[pinID]) {
				var currentShape = map.GetShapeByID(pinInfo[pinID].GetID());
				var currentIcon = currentShape.GetCustomIcon();
				var imageSTR = "/images/pushpin.png";
				currentShape.SetCustomIcon(imageSTR);
				currentShape.SetZIndex(1000, null);
				map.HideInfoBox();
				document.getElementById("listinElement_" + pinID).className = "color5";
			}
		}
	},
	EndDrawing: function () {
		//Add drag handles
		map.AttachEvent("onmousemove", EditPolyMoveHandler);
		map.AttachEvent("onmousedown", EditPolyDownHandler);
		map.AttachEvent("onmouseup", EditPolyUpHandler);
		polyline = tempShape;
		//Create Polygon mask(for editing);
		polylineMask = new VEShape(VEShapeType.Polygon, points);
		polylineMask.HideIcon();
		polylineMask.Hide();
		polylineMask.SetLineColor(new VEColor(0, 0, 0, 0.5));
		polylineMask.SetFillColor(new VEColor(0, 0, 0, 0.0));
		polylineMask.Primitives[0].symbol.stroke_dashstyle = "Dash";
		map.AddShape(polylineMask);

		pinLayer = new VEShapeLayer();
		map.AddShapeLayer(pinLayer);
		for (i = 0; i <= (points.length - 1); i++) {
			var dragHandle = new VEShape(VEShapeType.Pushpin, points[i]);
			dragHandle.SetCustomIcon('/images/DragHandle.gif');
			pinLayer.AddShape(dragHandle);
		}
		$(MapingID).val(points);
		idxAjax.runSearch(points);
		DetachDrawingEvents();
		map.DeleteShape(startPin);
	},
	GetDefaultMap: function () {
		map = new VEMap('myMap');
		map.LoadMap();

	},
	GetMap: function (lat, lon) {
		map = null;
		map = new VEMap('myMap');
		map.SetClientToken(VToken);
		map.LoadMap(null, null, VEMapStyle.Aerial);
		var latlon = new VELatLong(lat, lon);
		DoCenterZoom(latlon);
	},
	GetMapElement: function () {
		//Get position and width of map
		var pos = $(".mapControl").offset();
		var width = $(".mapControl").width();
		//position controls in upper right hand corner
		$("#drawControlsContainer").css({ "left": (pos.left + width) - 135 + "px", "top": (pos.top) + "px" });
		$("#poly").css({ "left": (pos.left + width) - 130 + "px", "top": (pos.top + 5) + "px" });
		$("#poly").hover(function () { $("#drawControls").fadeTo("fast", 0.80); }, function () { $("#drawControls").fadeTo("fast", 0.60); });
		$("#propCount").css({ "left": (pos.left + 65) + "px", "top": (pos.top + 30) + "px" });
		MapingID = "#" + controlID;
		//create map
		map = new VEMap('mapControlNew');
		//get values to display map 
		var mapView = $("#mapView").val();
		var mapMinX = $("#mapMinX").val();
		var mapMinY = $("#mapMinY").val();
		var mapMaxX = $("#mapMaxX").val();
		var mapMaxY = $("#mapMaxY").val();
		var mapZoom = $("#mapZoom").val();
		var mapTraffic = $("#mapTraffic").val();

		var mapStyle = null;
		var mapLocations = new Array();

		switch (mapView) {
			case "1":
				mapStyle = VEMapStyle.Road;
				break;
			case "2":
				mapStyle = VEMapStyle.Aerial;
				break;
			case "3":
				mapStyle = VEMapStyle.Hybrid;
				break;
		}
		if (mapMaxX != 0) {
			mapLocations.push(new VELatLong(mapMinX, mapMinY));
			mapLocations.push(new VELatLong(mapMaxX, mapMaxY));
			map.LoadMap();
			this.SetMap(mapLocations);
		}
		else {
			map.LoadMap(new VELatLong(mapMinX, mapMinY), mapZoom, mapStyle);
		}
		if (mapTraffic == "True") {
			map.LoadTraffic(true);
		}
		var ElementTxtBox = $(MapingID);
		if (ElementTxtBox.val() != "") {
			var corrdsArray = new Array();
			corrdsArray = ElementTxtBox.val().split(",");

			points = new Array();
			for (i = 0; i <= (corrdsArray.length / 2) - 1; i++) {
				var x = corrdsArray[i * 2];
				var y = corrdsArray[(i * 2) + 1];
				points.push(new VELatLong(x, y));
			}
			//if there are 4 points and the first the latitude of the first point is = to the latitude of the second then we are displaying a rectangle
			if (points.length == 4 && points[0].Latitude == points[1].Latitude) {
				selectBox = new VEShape(VEShapeType.Polygon, points);
				selectBox.HideIcon();
				selectBox.SetLineWidth(2);
				selectBox.SetFillColor(new VEColor(153, 153, 153, 0.4));
				selectBox.SetLineColor(new VEColor(153, 153, 153, 1.0));
				selectBox.ShowDetailOnMouseOver = false;
				selectBox.SetLineWidth(2);
				map.AddShape(selectBox);

				//add edit shap and handles
				map.AttachEvent("onmousemove", EditRecMoveHandler);
				map.AttachEvent("onmousedown", EditRecDownHandler);
				map.AttachEvent("onmouseup", EditRecUpHandler);

				polyline = selectBox;

				//Create Polygon mask(for editing);

				polylineMask = new VEShape(VEShapeType.Polygon, points);
				polylineMask.HideIcon();
				polylineMask.Hide();
				polylineMask.SetLineColor(new VEColor(0, 0, 0, 0.5));
				polylineMask.SetFillColor(new VEColor(0, 0, 0, 0.0));
				polylineMask.Primitives[0].symbol.stroke_dashstyle = "Dash";
				map.AddShape(polylineMask);
				pinLayer = new VEShapeLayer();
				map.AddShapeLayer(pinLayer);
				for (i = 0; i <= (points.length - 1); i++) {
					var dragHandle = new VEShape(VEShapeType.Pushpin, points[i]);
					dragHandle.SetCustomIcon('/images/DragHandle.gif');
					pinLayer.AddShape(dragHandle);
				}
			}
			else //add polygon shape and edit handles
			{
				polyLayer = new VEShapeLayer();

				tempShape = new VEShape(VEShapeType.Polygon, points);
				tempShape.SetFillColor(new VEColor(153, 153, 153, 0.4));
				tempShape.SetLineColor(new VEColor(153, 153, 153, 1.0));
				tempShape.SetLineWidth(2);
				tempShape.HideIcon();
				polyLayer.AddShape(tempShape);
				map.AddShapeLayer(polyLayer);

				//attach edit events to map
				map.AttachEvent("onmousemove", EditPolyMoveHandler);
				map.AttachEvent("onmousedown", EditPolyDownHandler);
				map.AttachEvent("onmouseup", EditPolyUpHandler);
				polyline = tempShape;
				//Create Polygon mask(for editing)
				polylineMask = new VEShape(VEShapeType.Polygon, points);
				polylineMask.HideIcon();
				polylineMask.Hide();
				polylineMask.SetLineColor(new VEColor(0, 0, 0, 0.5));
				polylineMask.SetFillColor(new VEColor(0, 0, 0, 0.0));
				polylineMask.Primitives[0].symbol.stroke_dashstyle = "Dash";
				map.AddShape(polylineMask);

				pinLayer = new VEShapeLayer();
				map.AddShapeLayer(pinLayer);
				for (i = 0; i <= (points.length - 1); i++) {
					var dragHandle = new VEShape(VEShapeType.Pushpin, points[i]);
					dragHandle.SetCustomIcon('/images/DragHandle.gif');
					pinLayer.AddShape(dragHandle);
				}
			}
		}
		DetachDrawingEvents();
	},
	SetMap: function (mapView) {
		if (map) {
			map.SetMapView(mapView);
		}
	},
	ShowPropInfo: function (pinID) {
		if (map) {
			if (pinInfo[pinID]) {
				var points = pinInfo[pinID].GetPoints();
				var currentShape = map.GetShapeByID(pinInfo[pinID].GetID());
				var currentIcon = currentShape.GetCustomIcon();
				var imageSTR = "/images/pushpinHover.png";
				currentShape.SetCustomIcon(imageSTR);
				currentShape.SetZIndex(1050, null);
				//Clear default bing map styles to allow content to be pushed down.
				map.ClearInfoBoxStyles();
				map.SetCenter(points[0]);
				map.ShowInfoBox(pinInfo[pinID]);
				document.getElementById("listinElement_" + pinID).className = "ListingElementHover";
			}
		}
	},
	StartDrawing: function () {
		this.ClearMap();
		initalize();

		if (polyLayer == null) {
			polyLayer = new VEShapeLayer();
			map.AddShapeLayer(polyLayer);
		}
		if (polyLayer2) // delete any boundaries
		{
			polyLayer2.DeleteAllShapes();
		}
		document.getElementById('mapControlNew').style.cursor = 'crosshair';
		map.AttachEvent('onclick', mapDrawingClick);
		map.AttachEvent('onmousemove', mapDrawingMouseMove);
		map.vemapcontrol.EnableGeoCommunity(true);
	},
	StartDrawingRectangle: function () {
		this.ClearMap();
		initalize();
		if (polyLayer == null) {
			polyLayer = new VEShapeLayer();
			map.AddShapeLayer(polyLayer);
		}
		if (polyLayer2) // delete any boundaries
		{
			polyLayer2.DeleteAllShapes();
		}
		// Attach the event handlers to the mouse 
		document.getElementById('mapControlNew').style.cursor = 'crosshair';
		map.AttachEvent("onclick", recMouseDownHandler);

		map.vemapcontrol.EnableGeoCommunity(true);
	}
};


function initalize()
{
    points = new Array();
    tempPoints = new Array();
    startPointSet = 0;
}

function mapDrawingClick(e) 
{
    points.push(GetClickLatLong(e));
}
function mapDrawingMouseOver(e)
{
     map.EndContinuousPan();
}
function mapDrawingMouseOut(e)
{
    var x = 0;
    var y = 0;
    var isPanning = false;
    var br = map.LatLongToPixel(map.GetMapView().BottomRightLatLong);
    if(e.mapX < 15 || e.mapX > br.x - 15)
    {
        if(e.mapX < 15)
        {
            x = -2;
        }
        else
        {
            x = 2;
        }
    }
    if(e.mapY < 15 || e.mapY > br.y - 15)
    {
        if(e.mapY < 15)
        {
            y = -2;
        }
        else
        {
            y = 2;
        }
    }
    if((x != 0 || y != 0))
    {
        map.StartContinuousPan(x,y);
        isPanning = true;
        return;
    }
    else
    {
        if(isPanning)
        {
            map.EndContinuousPan();
            isPanning = false;
        }
    }            

}

function mapDrawingMouseMove(e) 
{
    tempPoints = points.slice(0, points.length);
    tempPoints.push(GetClickLatLong(e));  
    try
    {
        polyLayer.DeleteShape(tempShape);
    }
    catch (err)
    {
    }
    try
    {		
        if(tempPoints.length < 3)
        {
            tempShape = new VEShape(VEShapeType.Polyline, tempPoints);
        }
        else
        {
            tempShape = new VEShape(VEShapeType.Polygon, tempPoints);
            
        }
        //document.getElementById("spanTempPoints").innerHTML = tempPoints.length;
		tempShape.SetFillColor(new VEColor(153, 153, 153, 0.4));
		tempShape.SetLineColor(new VEColor(153, 153, 153, 1.0));
        tempShape.SetLineWidth(2);
        tempShape.HideIcon();
        
        polyLayer.AddShape(tempShape);
        
		//document.getElementById("spanPoints").innerHTML = points.length;
 		//document.getElementById("spanstartPointSet").innerHTML = startPointSet;
       if (points.length >=1)
        {
            map.AttachEvent('onmouseout', mapDrawingMouseOut); 
            map.AttachEvent('onmouseover', mapDrawingMouseOver);   
        }
        if (points.length == 3 && startPointSet != 1)
        {
            startPointSet = 1;
            //document.getElementById("spanShapeEvents").innerHTML = points[0];
            
            startPin = new VEShape(VEShapeType.Pushpin,points[0]);
            startPin.SetCustomIcon("<img src='/images/endDraw.png' onclick='Mapping.EndDrawing()' />");
            startPin.ShowDetailOnMouseOver = false;
            map.AddShape(startPin);
            
            //map.AddShapeLayer(polyLayer);
            //document.getElementById("spanShapeEvents").innerHTML = "Shape Added";
        }
    }
    catch (err)
     {
    }
}

function GetClickLatLong(e)
{
    var x = e.mapX;
    var y = e.mapY;
    var pixel = new VEPixel(x, y);
    var latLong = map.PixelToLatLong(pixel);
    latLong.Latitude *= 1000000;
    latLong.Latitude = Math.floor(latLong.Latitude)/1000000;
    latLong.Longitude *= 1000000;
    latLong.Longitude = Math.floor(latLong.Longitude)/1000000;
    return latLong;
}
function DetachDrawingEvents()
{
    map.DetachEvent('onclick', mapDrawingClick);
    map.DetachEvent('onmousemove', mapDrawingMouseMove);
    map.DetachEvent('onmouseout', mapDrawingMouseOut); 
    map.DetachEvent('onmouseover', mapDrawingMouseOver);
    map.DetachEvent("onclick", recMouseEndHandler);
	map.DetachEvent("onmousemove", recMouseMoveHandler); 
    map.vemapcontrol.EnableGeoCommunity(false);
    map.EndContinuousPan();
}

function DoCenterZoom(location)
{
		map.SetCenterAndZoom(location,17);
		AddPushpin(location);
}
 function AddPushpin(latlon)
  {    
		//alert(Img);             
		var icon = "<div style='position:absolute;  left: 0px; top: -20px;'><img src='/images/push.png' /></div>";
	    
		var shape = new VEShape(VEShapeType.Pushpin, latlon);         
		//Set the icon         
		shape.SetCustomIcon(icon);                  
		//Set the info box
		map.ClearInfoBoxStyles();
		//Add the shape the the map         
		map.AddShape(shape);                       
  }
function mouseOverHandler(e){
    if (e.elementID && notOver){
        mouseOverSidebarItem(e.elementID);
        notOver = false;
    }
}
		
function mouseOutHandler(e){
    if (e.elementID && !notOver){ 
        mouseOutSidebarItem(e.elementID);
        notOver = true;
    }
}
function mouseOverSidebarItem(markerId){
    //Update pushpin
    currentShape = map.GetShapeByID(markerId);
    currentIcon = currentShape.GetCustomIcon();
	var imageSTR = "/images/pushpinHover.png"; 
    currentShape.SetCustomIcon(imageSTR);
    map.ShowInfoBox(currentShape);
}

function mouseOutSidebarItem(markerId){
    //Update pushpin
	currentShape = map.GetShapeByID(markerId);
	currentIcon = currentShape.GetCustomIcon();
	var imageSTR = "/images/pushpin.png"; 
    currentShape.SetCustomIcon(imageSTR);
    map.HideInfoBox(currentShape);
}
function recMouseDownHandler(e)
{ 
	// On mouse down, check to see if the control key is pressed.  If not, do nothing.  If, so start the selection process.
	// enable selecting mode
	selecting = true;
	
	StartLatLon = GetClickLatLong(e);
	map.AttachEvent("onmousemove", recMouseMoveHandler); 
	// disable the VE mouse events 
	return true; 
}    

function recMouseMoveHandler(e)
{ 
    //When moving the mouse, if in "selecting" mode, draw, otherwise do nothing.
    if(!selecting) 
    {
        return false;
    } 
    else
     {
		 
        // clear select box
        if (selectBox) 
        {
			map.DeleteShape(selectBox);
        }

        // set the latest endpoints (opposite side of the selection box) 
        currentX = e.mapX; 
        currentY = e.mapY;
        
        EndLatLon = GetClickLatLong(e);

        // create new selection box (VEShape) from start and end (corners) coordinates
        selectBox = new VEShape(VEShapeType.Polygon, 
            [
            StartLatLon,
            new VELatLong(StartLatLon.Latitude, EndLatLon.Longitude),
            EndLatLon,
            new VELatLong(EndLatLon.Latitude, StartLatLon.Longitude) 
            ]); 
        
        selectBox.HideIcon();
        selectBox.SetLineWidth(2);
		selectBox.SetFillColor(new VEColor(153, 153, 153, 0.4));
		selectBox.SetLineColor(new VEColor(153, 153, 153, 1.0));
		selectBox.ShowDetailOnMouseOver = false;
		selectBox.SetLineWidth(2);

        map.AddShape(selectBox);
        map.DetachEvent("onclick", recMouseDownHandler); 
        map.AttachEvent("onclick", recMouseEndHandler); 
    } 
}    
    
function recMouseEndHandler(e){ 
        // On mouse up, if in selecting mode cancel selecting mode
        if(selecting){ 
            // cancel selecting mode
            selecting = false;
            points.push(StartLatLon, new VELatLong(StartLatLon.Latitude, EndLatLon.Longitude), EndLatLon, new VELatLong(EndLatLon.Latitude, StartLatLon.Longitude));
            //document.getElementById('spanCoords').innerHTML = points;
			$(MapingID).val(points);

			map.AttachEvent("onmousemove", EditRecMoveHandler);
			map.AttachEvent("onmousedown", EditRecDownHandler);
			map.AttachEvent("onmouseup", EditRecUpHandler);

			polyline = selectBox;

			//Create Polygon mask(for editing);

			polylineMask = new VEShape(VEShapeType.Polygon, points); 
			polylineMask.HideIcon();
			polylineMask.Hide();
			polylineMask.SetLineColor(new VEColor(0,0,0, 0.5));
			polylineMask.SetFillColor(new VEColor(0,0,0,0.0));
			polylineMask.Primitives[0].symbol.stroke_dashstyle = "Dash";
			map.AddShape(polylineMask);
			pinLayer = new VEShapeLayer();
			map.AddShapeLayer(pinLayer);
			for (i = 0; i <= (points.length -1); i++)
			{
				var dragHandle = new VEShape(VEShapeType.Pushpin, points[i]);
				dragHandle.SetCustomIcon('/images/DragHandle.gif');
				pinLayer.AddShape(dragHandle);
			}
			idxAjax.runSearch(points);
			DetachDrawingEvents();
        } 
    }      

function PanMap(map, points)
{
	map.PanToLatLong(points);
	return;
}
function GoToListing(link)
{
	window.location = link;
}
//Edit Shapes
//onmousedown handler
function EditPolyDownHandler(e) 
{
    if (e.elementID) {
        currentDragHandle = map.GetShapeByID(e.elementID);
        if (currentDragHandle.GetType() == VEShapeType.Pushpin){
           moving = true;
           polylineMask.Show();
           map.vemapcontrol.EnableGeoCommunity(true);
           document.getElementById("mapControlNew").style.cursor = 'crosshair';
           
           //Determine point index
           var handleLocation = currentDragHandle.GetPoints();
           for (i = 0; i <= (points.length - 1); i++)
           {
				var pointLocation = points[i].toString();
				if(handleLocation == pointLocation)
				{
					pointIndex = i;
					break;
				}   
           }       
        }
    }
}
//onmousemove handler
    function EditPolyMoveHandler(e) {
        if (moving) {
            var loc = GetClickLatLong(e);
            currentDragHandle.SetPoints(loc);
            points[pointIndex] = loc;
            polylineMask.SetPoints(points);
        }
    }
//onmouseup handler
    function EditPolyUpHandler(e) {
        if (moving) {
            moving = false;
            polylineMask.Hide();
            map.vemapcontrol.EnableGeoCommunity(false);
            document.getElementById("mapControlNew").style.cursor = '';
            polyline.SetPoints(points);
            //document.getElementById('spanCoords').innerHTML = points;
            $(MapingID).val(points);
            idxAjax.runSearch(points);
        }
    }
    
function EditRecDownHandler(e) 
{
    if (e.elementID) {
        currentDragHandle = map.GetShapeByID(e.elementID);
        if (currentDragHandle.GetType() == VEShapeType.Pushpin){
           moving = true;
           polylineMask.Show();
           map.vemapcontrol.EnableGeoCommunity(true);
           document.getElementById("mapControlNew").style.cursor = 'crosshair';
           
           //Determine point index
           var handleLocation = currentDragHandle.GetPoints();
           for (i = 0; i <= (points.length - 1); i++)
           {
				var pointLocation = points[i].toString();
				if(handleLocation == pointLocation)
				{
					pointIndex = i;
					break;
				}   
           }       
        }
    }
}
//onmousemove handler
    function EditRecMoveHandler(e) {
        if (moving) {
            var loc = GetClickLatLong(e);
            currentDragHandle.SetPoints(loc);
            if(pointIndex == 0)
            {
				points[pointIndex] = loc;
				points[1] = new VELatLong(loc.Latitude, points[1].Longitude);
				points[3] = new VELatLong(points[3].Latitude, loc.Longitude);
            }
            else if(pointIndex == 1)
            {
				points[pointIndex] = loc;
				points[0] = new VELatLong(loc.Latitude, points[0].Longitude);
				points[2] = new VELatLong(points[2].Latitude, loc.Longitude);
            }
            else if(pointIndex == 2)
            {
				points[pointIndex] = loc;
				points[1] = new VELatLong(points[1].Latitude, loc.Longitude);
				points[3] = new VELatLong(loc.Latitude, points[3].Longitude);
            }
            else if(pointIndex == 3)
            {
				points[pointIndex] = loc;
				points[0] = new VELatLong(points[0].Latitude, loc.Longitude);
				points[2] = new VELatLong(loc.Latitude, points[2].Longitude);
            }
            polylineMask.SetPoints(points);
        }
    }
//onmouseup handler
    function EditRecUpHandler(e) {
        if (moving) {
            moving = false;
            polylineMask.Hide();
            map.vemapcontrol.EnableGeoCommunity(false);
            document.getElementById("mapControlNew").style.cursor = '';
            polyline.SetPoints(points);
            //document.getElementById('spanCoords').innerHTML = points;
            $(MapingID).val(points);
            map.DeleteShapeLayer(pinLayer);
            pinLayer = new VEShapeLayer();
			map.AddShapeLayer(pinLayer);
			for (i = 0; i <= (points.length -1); i++)
			{
				var dragHandle = new VEShape(VEShapeType.Pushpin, points[i]);
				dragHandle.SetCustomIcon('/images/DragHandle.gif');
				pinLayer.AddShape(dragHandle);
			}
			idxAjax.runSearch(points);
        }
    }

//Modal Window Popup
var PopUp = {
	Open:function(divID)
	{
		var Browser = document.body;
	
		window.onscroll = function () { document.getElementById(divID).style.top = document.body.scrollTop; };
		document.getElementById(divID).style.display = "block";
		document.getElementById(divID).style.top = document.body.scrollTop;
		document.getElementById(divID).style.height = Browser.clientHeight;
		document.getElementById(divID).style.width =  Browser.clientWidth;
		// call once to center everything
		WindowResize();
		
		if (window.attachEvent)
		{
			window.attachEvent('onresize', WindowResize);
		}
		else if (window.addEventListener)
		{
			window.addEventListener('resize', WindowResize, false);
		}
		else
		{
			window.onresize = WindowResize;
		}
		
		// we won't bother with using javascript in CSS to take care
		//   keeping the window centered
		if (document.all)
		{
			document.documentElement.onscroll = WindowResize;
		}
	},
	Close:function(divID)
	{
		document.getElementById(divID).style.display = "none";
	}
};
//Popup ajustment functions
//relocates the modal window when the browser window is resized
function WindowResize()
{
	// we only need to move the dialog based on scroll position if
	//   we're using a browser that doesn't support position: fixed, like < IE 7
	var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
	var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
	var div = document.getElementById('modal');
	
	div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
	div.style.top = Math.max((top + (GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
}
//gets the window width
function GetWindowWidth()
{
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;
		
	return width;
}
//Gets the window Height
function GetWindowHeight()
{
	var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
		0;
  		
	return height;
}
