// aimsNewTemp.js

//swapoutdoublequotesforsingle ... looks expensive

var dQuote = '"';
var theLocn = new Array();
var theLocnNum;
var kk = "";
var selectPoints = new Array();
var selectLeft = new Array();
var selectRight = new Array();
var selectTop = new Array();
var selectBottom = new Array();
//var useThis = false;                    // TONY FEB 05 //t103
queryStartRecord=0;

function writeTable(tabName,numAttribs,attribNo,makeHyperLink,theReply) {
  // numAttribs is the Number of Attributes, including #Shape#, #Id#; 
  // attribNo is where the required number comes in the list (starting with 0)
  getUniqueIdentifyStrs(numAttribs,attribNo,theReply);
  var theFrame = "parent.MapFrame";
  kk += '<table border="1" cellspacing="0" width="190px" cellpadding="2" nowrap class="hel2">';
  kk += '<tr><th>'+tabName+'</th></tr>';
  
  if (makeHyperLink) {
    for (var s1=0;s1<theLocnNum+1;s1++) {
      kk += '<tr><td><a href="javascript:' + theFrame + '.showHighlight(' + s1 + ')">' + theLocn[s1] + '</a></td></tr>';
    }  
  } else {
    var unduped = new Object;for (var i = 0; i < theLocnNum+1; i++) {   
      unduped[theLocn[i]] = theLocn[i];}
    var uniques = new Array;for (var k in unduped) {
      uniques.push(unduped[k]);}
    for (var s1=0;s1<uniques.length;s1++) {
          kk += '<tr><TD>'+uniques[s1]+'</td></tr>';
    }
  }

  kk += '</table><br>';

  selectCount = 1;
  if ((queryStartRecord==0)  && (toolMode!=4) && (toolMode!=9) && (toolMode!=15) && (toolMode!=25)) {
      if ((selectCount==1) && (zoomToSingleSelect)) {
          var fWidth = selectRight[0] - selectLeft[0];
          var fHeight = selectTop[0] - selectBottom[0];
          var mWMargin = 0;
          var mHMargin = 0;
          if (selectType=="point") {
              mWMargin = fullWidth * selectPointMargin;
              mHMargin = fullHeight * selectPointMargin;
              if (mWMargin > xDistance/2) {
                  mWMargin = xDistance/2;
                  mHMargin = yDistance/2;
              }
          } else {
              mWMargin = fWidth * selectMargin;
              mHMargin = fHeight * selectMargin;
          }
          saveLastExtent();
          left = selectLeft[0] - mWMargin;
          right = selectRight[0] + mWMargin;
          top = selectTop[0] + mHMargin;
          bottom = selectBottom[0] - mHMargin;
      }   
      //sendMapXML();     // show selected areas prior zoom
  }
}

function calcIdentTables(theLayer,theReply) {
    selectPoints.length=0;
    selectLeft.length=0;
    selectRight.length=0;
    selectTop.length=0;
    selectBottom.length=0;
    drawSelectBoundary=false;
  if (theLayer=='Roads_Search') {
    kk = '<font face="Arial" fontsize="2pt" style="bold">';
  }
  switch(theLayer) {
    case "Composite_Admin": { //for: identify
        writeTable("Suburb / Locality",4,0,false,theReply);
        writeTable("Municipality",4,1,false,theReply);
        writeTable("Melways Dir Reference",4,2,false,theReply);
        writeTable("VicRoads Dir Reference",4,3,false,theReply);
        break;
      }
    case "Local_Govt_Areas_Search": {    //for: find
        writeTable("Municipality",3,0,true,theReply);
        break;
      }
    case "Roads_Search": {
        if (fromTOCnotIdent) {
          writeTable("Road Name",3,0,true,theReply);    //find: Roads_Search = all_rds. 
        } else {     
          writeTable("Road Name",1,0,false,theReply);   //for: identify
        }
        break;
      }
    case "VR_Ed5_Directory_Grids_Search": {    //for: find
        writeTable("VicRoads Dir Reference",3,0,true,theReply);
        break;
      }
    case "Melway29_Grids_Search": {             //for: find
        writeTable("Melways Dir Reference",3,0,true,theReply);
        break;
      }
    case "Suburbs_/_Localities_Search": {        //for: find
        writeTable("Suburb / Locality",3,0,true,theReply);
        break;
      }  
  }
  hideRetrieveData();
  parent.TOCFrame.document.getElementById('div1').innerHTML = kk;
  parent.TOCFrame.document.getElementById('div1').style.visibility = 'visible';
  parent.TOCFrame.document.getElementById('theLgaE').style.visibility = 'hidden';
}

function getUniqueIdentifyStrs(numAttribs,attribNo,theReply) {
  var featureCount = justGetFeatureCount(theReply);
  var theLocnJoined = "";
var useThis = false;                    // TONY FEB 05 //t103
  theLocnNum =-1;
  if (featureCount > 0) {
    var startpos;
    var startpos0 = 0;
    var endpos = 0;
    var theNewStrSaved = new Array(4);
    var pos;
    if (fromTOCnotIdent) {
      pos = theReply.indexOf("<ENVELOPE ",endpos);
    } else {
      pos = theReply.indexOf("<FIELDS ",endpos);
    }
    startpos = pos + 8;
    endpos =theReply.indexOf("><FEATURECOUNT",startpos);
    inData = theReply.substring(startpos,endpos);

    for (var i=0;i<featureCount;i++) {
      if (fromTOCnotIdent) {
        for (var m=0;m<4;m++) {
          startpos0 = inData.indexOf('"',startpos0);
          endpos = inData.indexOf('"',startpos0+1);
          theNewStrSaved[m] = inData.substring(startpos0+1,endpos);
          startpos0 = endpos+2;
        }
      }
      for (var m=0;m<numAttribs;m++) {
        startpos0 = inData.indexOf('"',startpos0);
        endpos = inData.indexOf('"',startpos0+1);
        theNewStr = inData.substring(startpos0+1,endpos);
        if (m==attribNo) {
            theLocnNum +=1;
            theLocn[theLocnNum] = theNewStr
            theLocnJoined +=theNewStr+",";
            useThis = true;
        }
        startpos0 = endpos+2;
      }
      
      if (useThis && fromTOCnotIdent) {
        selectLeft[theLocnNum]=theNewStrSaved[0];
        selectBottom[theLocnNum]=theNewStrSaved[1];
        selectRight[theLocnNum]=theNewStrSaved[2];
        selectTop[theLocnNum]=theNewStrSaved[3];
      }
    }
  }
}