var decimalPlaces = 6;
var maxLength = 1950;

function addWKT() {
    xVisibility('polygonControl',false);
    xVisibility('addPolygonDiv',false);

    sPoints = "";
  //  var polygons = polyCtrl.G.prmPolygons;
   // for (var i=0; i<polygons.length; i++) {
   		//if(polygons[i]) {
    var vertices = polyPoints; //polyCtrl.returnVertices(polygons[i][1]);
       	    for (var k=0; k<vertices.length; k++) {
       	        // Limit list to 2000 characters in length
	            if (sPoints.length < maxLength) {
	                var lat = vertices[k].lat();
	                var longi = vertices[k].lng();
                    if (sPoints.length > 0) {
                        sPoints += ",";
                    }
                    sPoints += longi.toFixed(decimalPlaces) + " " + lat.toFixed(decimalPlaces);
                }
		    }
		//}
	//	polyCtrl.G.prmPolygons = new Array();
	//}
		    polyPoints = [];
	if (sPoints.length >= maxLength) {
	    alert('Your polygon contained too many points and the extra points have been removed');
	}

    if(sPoints.length > 0) {
        var theDate = new Date();

        GDownloadUrl('AddFeature.aspx?cmd=normal&Points=' + sPoints + '&FeatureType=' + gsFeatureTypeID + '&TimeStamp=' + theDate.valueOf(),

            function(data, responseCode) {
                if (responseCode == 200) {

                    if (data != "ERROR") {
                        var retmessage = data.split(',')[0];
                        var geotype = data.split(',')[1];
                        var sFeatureID = data.split(',')[2];
                        var sXpos = data.split(',')[3];
                        var sYpos = data.split(',')[4];
                        if (sFeatureID != '') {
                            var pt = new GLatLng(sYpos, sXpos);
                            addInfoTipMarker(pt, sFeatureID);
                            JDrawSingleFeature(sFeatureID);

                            //parent.frames['FeaturesFrame'].UpdateFeaturesList();
                            //UpdateFeaturesList();
                        }
                        else {
                            alert("Unable to add feature at this time");
                        }

                        if (retmessage == "PROBLEM_MSG") { alert("This polygon can not be clipped online. Finding Sanctuary will be using a processed version of this data") };

                    } else {
                        alert("Your polygon is outside the map limits, please try again.");
                    }
                   
                }
                else if (responseCode == -1) {
                    alert("Data request timed out. Please try later.");
                }
                else {
                    alert("Request resulted in error.\n Request returned :- \n" + data);
                }
            }
        );
    }
    else {
        alert("Feature NOT added " + vertices.length);
    }
    
}


function addWKTDiversIDs(){

 xVisibility('polygonControl', false);
 xVisibility('addPolygonDiv', false);
 var ids = "";
for(var i=0;i<SelectedIDs.length;i++){
    ids += SelectedIDs[i] + "|";



}
var theDate = new Date();
GDownloadUrl('AddFeature.aspx?cmd=DiversIDs&Points=' + ids.slice(0,ids.length-1) + '&FeatureType=' + gsFeatureTypeID + '&TimeStamp=' + theDate.valueOf(),

            function(data, responseCode) {
                if (responseCode == 200) {

                    if (data != "ERROR") {

                          var geotype = data.split(',')[0];
                         var sFeatureID = data.split(',')[1];
                         var sXpos = data.split(',')[2];
                         var sYpos = data.split(',')[3];
                         if (sFeatureID != '') {
                         var pt = new GLatLng(sYpos, sXpos);
                         addInfoTipMarker(pt, sFeatureID);
                         JDrawSingleFeature(sFeatureID);

                        //parent.frames['FeaturesFrame'].UpdateFeaturesList();
                        UpdateFeaturesList();
                         }
                         else {
                           alert("Unable to add feature at this time");
                         }

                        
                    } else {
                        alert("Poly outside Limits");
                    }
                }
                else if (responseCode == -1) {
                    alert("Data request timed out. Please try later.");
                }
                else {
                    alert("Request resulted in error.\n Request returned :- \n" + data);
                }
            }
        );
            
}

function addWKTDiversPoly() {
    xVisibility('polygonControl', false);
    xVisibility('addPolygonDiv', false);

    sPoints = "";
    
    var vertices = polyPoints; 
    for (var k = 0; k < vertices.length; k++) {
        
        if (sPoints.length < maxLength) {
            var lat = vertices[k].lat();
            var longi = vertices[k].lng();
            if (sPoints.length > 0) {
                sPoints += ",";
            }
            sPoints += longi.toFixed(decimalPlaces) + " " + lat.toFixed(decimalPlaces);
        }
    }
    //}
    //	polyCtrl.G.prmPolygons = new Array();
    //}
   
    polyPoints = [];
    if (sPoints.length >= maxLength) {
        alert('Your polygon contained too many points and the extra points have been removed');
    }

    if (sPoints.length > 0) {
        var theDate = new Date();
      
        GDownloadUrl('AddFeature.aspx?cmd=DiversPoly&Points=' + sPoints + '&FeatureType=' + gsFeatureTypeID + '&TimeStamp=' + theDate.valueOf(),

            function(data, responseCode) {
                if (responseCode == 200) {
                   
                    if (data != "ERROR") {

                        var geotype = data.split(',')[0];
                        var sFeatureID = data.split(',')[1];
                        var sXpos = data.split(',')[2];
                        var sYpos = data.split(',')[3];
                        if (sFeatureID != '') {
                            var pt = new GLatLng(sYpos, sXpos);
                             addInfoTipMarker(pt, sFeatureID);
                             JDrawSingleFeature(sFeatureID);

                            
                            UpdateFeaturesList();
                        }
                        else {
                            alert("Unable to add feature at this time");
                        }

                    } else {
                        alert("Poly outside Limits");
                    }
                  
                }
                else if (responseCode == -1) {
                    alert("Data request timed out. Please try later.");
                }
                else {
                    alert("Request resulted in error.\n Request returned :- \n" + data);
                }
            }
        );
    }
    else {
        alert("Feature NOT added " + vertices.length);
    }
    
}
