//determine if the Point lies in Polygon area
// receives array of x-coordinates, y-coordinates, and x,y of the point
//return true or false
function isPointOnPolygon(xPoints, yPoints, x, y)
 {
      var i, j, c = 0;
      for (i = 0, j = xPoints.length-1; i < xPoints.length; j = i++) {
        if ((((yPoints[i] <= y) && (y < yPoints[j])) ||
             ((yPoints[j] <= y) && (y < yPoints[i]))) &&
            (x < (xPoints[j] - xPoints[i]) * (y - yPoints[i]) / (yPoints[j] - yPoints[i]) + xPoints[i]))
          c = !c;
      }
      return c;
 }


//determine if the Point lies in Polygons area (2)
// receives array of polygons, and x,y of the point
// returns polygon index  or -1 if the point not lies on polygons
function getPolygonIndexFromPoint(polygons, x, y)
{
 var i;
 if(polygons!=null)
   for(i=0;i<polygons.length;i++)
   {
    var lies = polygons[i]!=null?isPointOnPolygon(polygons[i].xPoints,polygons[i].yPoints,x,y):false;
	if(lies)
		return i;
   }
 return -1;
}


//get polygon center
// receives array of x-coordinates, y-coordinates, and x,y of the point
//return Array(x,y) of the center
function getPolygonCenter(xPoints, yPoints)
 {
	  if(xPoints.length ==0 || yPoints.length ==0)
		  return new Array(0,0);
      var i, j;
	  var minX = xPoints[0];
	  var minY = yPoints[0];
	  var maxX = xPoints[0];
	  var maxY = yPoints[0];

      for (i = 0; i < xPoints.length && i < yPoints.length; i++) 
	  {
           if(minX>xPoints[i])
			 minX=xPoints[i];
           if(maxX<xPoints[i])
			 maxX=xPoints[i];

           if(minY>yPoints[i])
			 minY=yPoints[i];
           if(maxY<yPoints[i])
			 maxY=yPoints[i];

      }
	  var cx = (maxX-minX)/2+minX;
	  var cy = (maxY-minY)/2+minY;
      return new Array(cx,cy);
 }
