
// Cet objet a pour rôle de chercher des enregistrements
// dans une Layer en prenant en compte la contrainte spatiale et attributaire

function SpatialEngine(){
	this.Layer=null;
	this.Where=null;
	this.Fields=new Array();
	this.Response=null;
	this.Geometry=new Geometry;
	this.Execute=SpatialEngine_FindIt;
	//this.Features=null;
}

function SpatialEngine_SelectFromWhere(IDLayer,Fields,ClauseWhere,Geometry) {
	var selectFields="";
	MSActiveLayer=IDLayer;
	for (var i=0;i<Fields.length;i++){
		selectFields+= ' ' + Fields[i]; 
	}
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' featurelimit="1000" beginrecord="1">';
	theString += '<LAYER id="' + IDLayer + '" />';
	if (!Geometry==null) {
		// keep this within the limitExtent
		theString += '<SPATIALQUERY subfields="#ALL#"' + selectFields + '" where= "' + ClauseWhere + '">';
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		if (Geometry.Type=='Envelope'){
			strMaxX=Geometry.Point[0];
			strMaxY=Geometry.Point[1];
			strMinX=Geometry.Point[2];
			strMinY=Geometry.Point[3];
			theString += '<ENVELOPE maxx="' + forceComma(strMaxX) + '" maxy="' + forceComma(strMaxY) + '" minx="' + forceComma(strMinX) + '" miny="' + forceComma(strMinY) + '" />\n';
		}
		if (Geometry.Type=='Polygone') {
			theString += '<POLYGON> \n <RING> \n'
            for(var i=0; i<Geometry.PointCount;i++){
              theString += '<POINT x="' + forceComma(Geometry.point[i]) + '" y="' + forceComma(Geometry.point[i+1]) + '" />\n'
            }
            theString += '</RING> \n';
            theString += '</POLYGON> \n'
		}
		theString += '</SPATIALFILTER> \n';
		theString += '</SPATIALQUERY> \n';
		
	} else {
	theString += '<SPATIALQUERY subfields="' + selectFields +'" where="' + ClauseWhere + '"/>';
	}
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;
	
}
function SpatialEngine_FindIt(InFeatures){
	var XMLRequest= SpatialEngine_SelectFromWhere(this.Layer,this.Fields,this.Where,this.Geometry);
	var Features= new Array(0);
	var Form=parent.PostFrame.document.forms[0];
	Features=InFeatures;
	NbFields=this.Fields.length;
	Fields=this.Fields;
	MSsendToServer(imsURL + "&CustomService=Query",XMLRequest,777,Form);
}

function SpatialEngine_ParseResponse(XMLResponse){
var FeatureCount=SpatialEngine_GetFeatureCount(XMLResponse);
for(var i=0;i<FeatureCount;i++){
}
}


function SpatialEngine_GetFeatureCount(XMLResponse){
var FeatureCount=0;
var theString = XMLResponse;
theString.toUpperCase( );
var	tempString="";
pos = theString.indexOf("FEATURECOUNT",0);
if (pos!=-1) {
	pos += 10;
	startpos = theString.indexOf("count",pos);
	startpos += 7;
	var endpos = theString.indexOf(dQuote,startpos);
	tempString = theString.substring(startpos,endpos);
	FeatureCount=tempString;
}

return(FeatureCount)
}

function SpatialEngine_GetFeatures(XMLResponse){					 
var InFeatureCount=SpatialEngine_GetFeatureCount(XMLResponse);	
if(InFeatureCount!=0)
{
var InFeatures =new Array(InFeatureCount);
var theString=XMLResponse;
theString.toUpperCase( );
var	tempString="";
var pos;
var CurrentFeature;
var StartXML=0;
	for(var i=0;i<InFeatureCount;i++){
		CurrentFeature=new Feature;
		pos = theString.indexOf("FEATURE",StartXML);
		startpos = pos;
		var endpos=theString.indexOf("/FEATURE",startpos)
		tempString=theString.substring(startpos,endpos);
		CurrentFeature.Envelope=SpatialEngine_GetEnvelopeXYs(tempString,0); 
		CurrentFeature.FieldsValues=SpatialEngine_GetFields(tempString);
		InFeatures[i]=CurrentFeature;
		StartXML=endpos+8;
	}
return InFeatures;
}
var InFeatures=new Array();
return InFeatures;
}

function SpatialEngine_GetFields(InString) {
	var theFieldsValues = new Array();
	var CurentField;
	var theString = InString;
	theString.toUpperCase();
	var tempString = "";
	var pos = theString.indexOf("FIELDS",0);
	if (pos!=-1) {
		pos = pos + 6;
		for (var i=0;i<NbFields;i++){
			CurentField=Fields[i]
			if (pos!=-1) {
				startpos = theString.indexOf(CurentField,pos);
				startpos += CurentField.length+2;
				var endpos = theString.indexOf(dQuote,startpos);
				tempString = theString.substring(startpos,endpos);
				theFieldsValues[i]=tempString;
			}
		}
	}	
	return theFieldsValues;
}

function MSsendToServer(URLString,XMLRequest,theType,Form) {
	if (Form!=null) {
		if (MSOkToSend) {
			XMLMode = theType;
			MSOkToSend =false;
				var theForm =Form;
				URLString = URLString + cMSVersion;
				var requestURL = URLString;
				if (theForm.RedirectURL!=null) {
					if (MSisNotSameHostInURL(URLString, hostName)) {
						requestURL = MSconnectorURL;
						theForm.RedirectURL.value = URLString;
						
					} else {
						theForm.RedirectURL.value = "";
					}

					
				}
				if (doURLencode) {
					theForm.action = requestURL + "&Form=True&Encode=True";
				} else {
					theForm.action = requestURL + "&Form=True&Encode=False";
				}
				var xmlHeader = '<?xml version="1.0" ' + MSlocaleEncoding + '?>';
				theForm.ArcXMLRequest.value = xmlHeader + XMLRequest;
				if (theForm.FormCharset!=null) theForm.FormCharset.value = MScharEncoding;
				if (theForm.FormCharset!=null) theForm.FormCharset.value = MScharSet;
				theForm.JavaScriptFunction.value="parent.MapFrame.MSprocessXML";
				theForm.submit();
				
		} else {
			hideRetrieveMap();
			hideRetrieveData();
		}
	} else {
			hideRetrieveMap();
			hideRetrieveData();
	}
		
}
function MSprocessXML(theReplyIn) {
	var NBanswers=0;
	var strResultat='';
	if (doURLencode) {
		theReplyIn = replacePlus(theReplyIn);
		var theReply = unescape(theReplyIn);
	} else {
		var theReply = theReplyIn;
	}
	MSOkToSend =true;
	Features=SpatialEngine_GetFeatures(theReply);
	var MinX=Features[0].Envelope[0];
	var MinY=Features[0].Envelope[1];
	var MaxX=Features[0].Envelope[2];
	var MaxY=Features[0].Envelope[3];
	var ID=Features[0].FieldsValues[0];
	switch(XMLMode)
	{
	  case 777:
	    parent.MapFrame.ZoomToFeature(MinX,MinY,MaxX,MaxY,ID,LastsearchIdLayer);
	    break;
		
          case 888:
	      break;
	      
	  default:
	      break;
	}
	   	
}


function MSisNotSameHostInURL(theURL, theHost) {
	var startpos = theURL.indexOf("//");
	if (startpos==-1) {
		startpos = 0;
	} else {
		startpos = startpos + 2;
	}
	var endpos = theURL.indexOf("/",startpos);
	if (endpos==-1) endpos = theURL.length;
	var thisHost = theURL.substring(startpos,endpos);
	if (thisHost==theHost) {
		return false;
	} else {
		return true;
	}
}

function SpatialEngine_GetEnvelopeXYs(theString, startpos) {
	var theEnvelope = new Array();
	theString = theString.toUpperCase();
	var tempString = "";
	var pos = theString.indexOf("ENVELOPE",startpos);
	if (pos!=-1) {
		pos = pos + 8;
		startpos = theString.indexOf("MINX=",pos);
		startpos += 6;
		var endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[0] = parseFloat(setDecimalString(tempString));
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[1] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[2] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[3] = parseFloat(setDecimalString(tempString)); 
		xmlEndPos = endpos;
	}
	return theEnvelope;
}