// aimsGeocode.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*               dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*               aimsLayers.js, aimsDHTML.js
*               aimsClick.js, aimsNavigation.js
*/

aimsGeocodePresent=true;

var GCLayers = new Array();
var GCLayerId = new Array();
var GCLayerStyle = new Array();
var GCActiveLayer=0;
var GCidCount=0;
var GCid = new Array();
var GClabel = new Array();
var GCdesc = new Array();
var GCvalue = new Array();
var GCpointX = new Array();
var GCpointY = new Array();
var GCpointCount = 0;
var GCscore = new Array();
var GCaddress = new Array();

var GCLayerCount=0;

var recherche = false;
var street= "";
var civic = "";
var mun="";
var zonage = "";
var intervale = 10;
var nbTrouver = 0;
var inter = 0;
var rechFait = false;
var resultatAve = "";
var resultatRue = "";
var resultatMontee = "";

function zoominSearch(xIn, yIn, drawIt,theLabel) {
        getMapXY(xIn,yIn);
        var tempLeft=lastLeft;
        var tempRight=lastRight;
        var tempTop=lastTop;
        var tempBottom=lastBottom;
        saveLastExtent();
        sendMapXML();
}

if (imsGeocodeURL=="") {
        imsGeocodeURL= imsURL + "&CustomService=Geocode";
}

/*
***************************************************************************************

Geocoding functions 

***************************************************************************************
*/


function afficherResultat(temp,j) {
        //alert("afficherResultat");
        var Win1;
        var theFrame = "parent.MapFrame";
        /*if ((useExternalWindow) || (!useTextFrame)) {
                //if (GCpointCount > 1){
                  //      var newHauteur = 460;
                        Win1 = window.open("","Result_gecode", "scrollbars=yes,resizable=yes");
                //}
                //alert(parent.MapFrame.name);
                theFrame = "parent.MapFrame";
                //theFrame = "opener";
                if (parent.MapFrame!=null) theFrame = "parent.parent.parent.parent.MapFrame";
        } else {
                Win1 = parent.TextFrame;
                Win1.document.open();
        }*/
        	
        	theFrame = "parent.MapFrame";
                //theFrame = "opener";
                if (parent.MapFrame!=null) theFrame = "parent.parent.parent.parent.MapFrame";
                Win1 = window.open("","Result_gecode", "scrollbars=yes,resizable=yes");
                
                if (GCpointCount > 1){
                        Win1.document.open();
                        
                        Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[31] + '</title></head>');
                        Win1.document.writeln('<body bgcolor="' + textFrameBackColor + '" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0 onload="window.focus()">');
                        Win1.document.writeln('<form>');
                        //Win1.document.writeln('<table border="1" cellspacing="0" cellpadding="2" nowrap bgcolor="' + tableBackColor + '">');
                        //Win1.document.writeln('</table>');
                        var nbRes = GCpointCount;
                        Win1.document.writeln('<center><table border="1" cellspacing="0" cellpadding="2" nowrap bgcolor="white">');
                        //Win1.document.writeln('<tr><td align="center" colspan=3><FONT FACE="Tahoma" SIZE="1" color="black"><b>' + nbRes + msgList[133] + '</b></FONT></td></tr>');
                        Win1.document.writeln('<tr><td align="center" colspan=3><FONT FACE="Tahoma" SIZE="1" color="black"><b>'  + msgList[141] + ' (' + nbRes + ')' + '</b></FONT></td></tr>');
                        //Win1.document.writeln('<tr><td>&nbsp;</td><td><FONT FACE="Tahoma" SIZE="1"><b>Adresses</b></td><td><FONT FACE="Tahoma" SIZE="1"><b>Taux de réussite</b></td></tr>');
                        //Win1.document.writeln('<tr><td colspan=2><FONT FACE="BankGothic Lt BT" SIZE="4"><b>' + nbRes + msgList[133] + '</b></td></tr>');
                        Win1.document.writeln('<br><br>');
                        var tt = temp - 10;
                        if (GCpointCount <= 10){
                                for (var i=0;i<GCpointCount;i++) {
                                        Win1.document.writeln('<tr>');                                  
                                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3">' + (i+1) + '</font></td>');
                                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3"><a href="javascript:' + theFrame + '.zoomToPoint(' + GCpointX[i] + ',' + GCpointY[i] + ',true,\'' + GCaddress[i] + '\')">' + GCaddress[i] + '</a></td>');
                                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3"><center>' + GCscore[i] + '</center></td>');
                                        Win1.document.writeln('</tr>');
                                }       
                        }else{
                                for (var i=temp;i>tt;i--) {
                                        j = j + 1;
                                        Win1.document.writeln('<tr>');  
                                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3">' + j + '</font></td>');                           
                                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3"><a href="javascript:' + theFrame + '.zoomToPoint(' + GCpointX[i] + ',' + GCpointY[i] + ',true,\'' + GCaddress[i] + '\')">' + GCaddress[i] + '</a></td>');
                                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3"><center>' + GCscore[i] + '</center></td>');
                                        Win1.document.writeln('</tr>');
                                        temp = temp - 1;
                                }
                        }
                        Win1.document.writeln('</table></center>');
                        if ((GCpointCount <= 10)||(j==GCpointCount)) {
                                if (j==GCpointCount){
                                        inter = 0;
                                        nbTrouver = GCpointCount-1;
                                        Win1.document.writeln('<P ALIGN="left"><a href="javascript:' + theFrame + '.afficherResultat(' + nbTrouver + "," + inter + ')"><img src="' + appDir + 'images/bouton_previous.gif" border=0></a>'); 
                                }               
                        } else {
                                inter = j;
                                nbTrouver = temp;
                                Win1.document.writeln('<P ALIGN="right"><a href="javascript:' + theFrame + '.afficherResultat(' + nbTrouver + "," + inter + ')"><img src="' + appDir + 'images/bouton_suivant.gif" border=0></a>');
                        }
                        rechFait=true;
                        Win1.document.writeln('<br><br>');
                        //Win1.document.writeln('<center><input type="button" onclick="document.location=\'' + appDir + 'addmatch.htm\';" value="' + buttonList[1] + '"></form></font></center>');
                        
                        Win1.document.close();
                        hideRetrieveData();
                                
                }else{          
                        var pX = GCpointX[0]-intervale;
                        var pY = GCpointY[0]-intervale;
                        parent.MapFrame.zoomToPoint(pX, pY, true, '\'' + GCaddress[0] + '\'');
                        
                        Win1.document.open();
                        
                        Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[31] + '</title></head>');
                        Win1.document.writeln('<body bgcolor="' + textFrameBackColor + '" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0 onload="window.focus()">');
                        Win1.document.writeln('<form>');
                        Win1.document.writeln('<center><table border="1" cellspacing="0" cellpadding="2" nowrap bgcolor="white">');
                        //Win1.document.writeln('<tr><td align="center" colspan=3><FONT FACE="Tahoma" SIZE="1" color="black"><b>Une' + msgList[139] + '</b></FONT></td></tr>');
                        Win1.document.writeln('<tr><td align="center" colspan=3><FONT FACE="Tahoma" SIZE="1" color="black"><b>' + msgList[140] + '</b></FONT></td></tr>');
                        //Win1.document.writeln('<tr><td>&nbsp;</td><td><FONT FACE="Tahoma" SIZE="1"><b>Adresse</b></td><td><FONT FACE="Tahoma" SIZE="1"><b>Taux de réussite</b></td></tr>');
                      
                        Win1.document.writeln('<tr>');                                  
                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3">1</font></td>');
                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3"><a href="javascript:' + theFrame + '.zoomToPoint(' + GCpointX[0] + ',' + GCpointY[0] + ',true,\'' + GCaddress[0] + '\')">' + GCaddress[0] + '</a></td>');
                        Win1.document.writeln('<td><FONT FACE="Tahoma" SIZE="-3"><center>' + GCscore[0] + '</center></td>');
                        Win1.document.writeln('</tr>');
                        
                        Win1.document.writeln('</table></center>');
                        
                        rechFait=true;
                        Win1.document.writeln('<br><br>');
                        //Win1.document.writeln('<center><input type="button" onclick="document.location=\'' + appDir + 'addmatch.htm\';" value="' + buttonList[1] + '"></form></font></center>');
                        
                        Win1.document.close();
                        hideRetrieveData();                       
                        
                }
                
                Win1=null;
                
}


// set up geocode
function setupGeocode() {
                getGeocodeParams();  
}

// get a list of geocoding layers
function getGeocodeLayers() {
        var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO />\n';
        theString += '</REQUEST>\n</ARCXML>';
        sendToServer(imsGeocodeURL,theString,20);
        
}

// get the parameters for geocoding the layer
function getGeocodeParams() {
        var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO />\n';
        theString += '</REQUEST>\n</ARCXML>';
        var theReply="";
        sendToServer(imsGeocodeURL,theString,26);


}

// write out the geocode XML request
function writeGeocodeXML() {
        var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_GEOCODE maxcandidates="' + maxGeocodeCandidates + '" minscore="' + minGeocodeScore + '">\n';
        theString += '<LAYER id="' + GCLayerId[GCActiveLayer] + '" />';
        theString += '<ADDRESS>\n';
        for (var i=0;i<GCidCount;i++) {
                theString += '<GCTAG id="' + GCid[i] + '" value="' + GCvalue[i] + '"/>\n';
        }
        theString += '</ADDRESS>\n</GET_GEOCODE>\n</REQUEST>\n</ARCXML>\n';
        //alert(theString);
        return theString;
        
}

// parse out geocoding layers
function parseGeocodeLayers(theReply) {
        var pos = 0;
        var startpos = 0;
        var endpos = 0;
        var lpos = 0;
        GCLayers.length=1;
        GCLayerCount=0;
        pos = theReply.indexOf("<LAYERINFO",endpos);
        
                
        while (pos!=-1) {
                lpos = theReply.indexOf('name=',pos);
                if (lpos!=-1) {
                        
                        startpos = lpos + 6;
                        endpos = theReply.indexOf(dQuote,startpos);
                        GCLayers[GCLayerCount] = theReply.substring(startpos,endpos);
                        startpos = theReply.indexOf('id=',endpos);
                        if (startpos!=-1) {
                                startpos += 4;
                                endpos = theReply.indexOf(dQuote,startpos);
                                GCLayerId[GCLayerCount] = theReply.substring(startpos,endpos);
                        }
                        startpos = theReply.indexOf("<GCSTYLE name=",endpos);
                        if (startpos!=-1) {
                                startpos += 15;
                                endpos = theReply.indexOf(dQuote,startpos);
                                GCLayerStyle[GCLayerCount] = theReply.substring(startpos,endpos);
                                GCLayerCount=GCLayerCount+1;
                        }
                        pos = theReply.indexOf("<LAYERINFO",endpos);

                } else {
                        pos =-1;
                }
        }
        
}

// parse out the bacic geocode parameters for the layer
function parseGeocodeParams(theReply,theLayer) {
        var pos = 0;
        var startpos = 0;
        var endpos = 0;
        var lpos = 0;
        GCid.length=0;
        GClabel.length=0;
        GCdesc.length=0;
        GCidCount=0;
        var theName = 'name="' + theLayer + '"'
        pos = theReply.indexOf(theName,endpos);
        
        if (pos!=-1) {
                startpos = pos + 17;
                lpos = theReply.indexOf("</LAYERINFO>",startpos);
                startpos = theReply.indexOf("<GCINPUT id=",startpos);
                while ((startpos<lpos) && (startpos!=-1)) {
                                startpos += 13;
                                endpos = theReply.indexOf(dQuote,startpos); 
                                GCid[GCidCount] = theReply.substring(startpos,endpos);
                                startpos = theReply.indexOf("label=",endpos);
                                startpos = startpos + 7;
                                endpos = theReply.indexOf(dQuote,startpos); 
                                GClabel[GCidCount] = theReply.substring(startpos,endpos);
                                startpos = theReply.indexOf("description=",endpos);
                                startpos += 13;
                                endpos = theReply.indexOf(dQuote,startpos); 
                                GCdesc[GCidCount] = theReply.substring(startpos,endpos);
                                GCidCount=GCidCount+1;
                        startpos = theReply.indexOf("<GCINPUT id=",endpos);
                }
                return true;
        } else {
                return false;
        }

}

// parse out geocode response and display results in table
function parseGeocodeResults(theReply) {
        GCscore.length=1;
        var pos = theReply.indexOf("<GCCOUNT count=");
        var lpos = 0;
        var startpos = pos + 16;
        var startpos2=0;
        var endpos = theReply.indexOf(dQuote,startpos);
        var fString = theReply.substring(startpos,endpos);
        GCpointCount=parseInt(fString);
        var gcCount=0;
        if ((pos>0) && (GCpointCount>0)) {
                pos = theReply.indexOf("<FEATURE");
                if (pos!=-1) {
                        while (pos!=-1) {
                                lpos = theReply.indexOf("<FIELD",pos);
                                if (lpos!=-1) {
                                                startpos2 = theReply.indexOf('name="SCORE"',lpos);
                                                startpos = theReply.indexOf("FIELDVALUE valuestring=",startpos2);
                                                startpos = startpos + 24;
                                                endpos = theReply.indexOf(dQuote,startpos);
                                                GCscore[gcCount] = theReply.substring(startpos,endpos);
                                                startpos2 = theReply.indexOf('name="ADDRESSFOUND"',lpos);
                                                startpos = theReply.indexOf("FIELDVALUE valuestring=",startpos2);
                                                startpos = startpos + 24;
                                                endpos = theReply.indexOf(dQuote,startpos);
                                                GCaddress[gcCount] = theReply.substring(startpos,endpos);
                                                startpos2 = theReply.indexOf('name="SHAPEFIELD"',lpos);
                                                startpos = theReply.indexOf("<POINT x=",startpos2);
                                                startpos += 10;
                                                endpos = theReply.indexOf(dQuote,startpos);
                                                GCpointX[gcCount] = theReply.substring(startpos,endpos);
                                                startpos = theReply.indexOf("y=",endpos);
                                                startpos = startpos + 3;
                                                endpos = theReply.indexOf(dQuote,startpos);
                                                GCpointY[gcCount] = theReply.substring(startpos,endpos);
                                                gcCount++;
                                }
                                pos = theReply.indexOf("<FEATURE",endpos);
                        }
                }
                showGeocode=true;
                geocodeX=GCpointX[0];
                geocodeY=GCpointY[0];
                geocodeLabel=GCaddress[0];
                AdresseCherchee=geocodeLabel;
                var temp = GCpointCount-1;
                var j=0;        

                afficherResultat(temp,j);
        
        } else {
                alert(msgList[60]);
                hideRetrieveData();
        }
        
}

