/*
this.buildFieldValueString = function(sLayerName,sFieldName,sFieldValue){
  //check to see is value should be part of hyperlink
  var nLoopCounter = 0;
  var sReturnVal = sFieldValue;
  var sHref = "";

  for (nLoopCounter = 0;nLoopCounter < g_aHyperLinkFields.length;nLoopCounter++){
    if (g_aHyperLinkFields[nLoopCounter]["layerName"] == sLayerName){

      if (g_aHyperLinkFields[nLoopCounter]["fieldName"] == sFieldName){
        sHref = g_aHyperLinkFields[nLoopCounter]["linkPrefix"] + sReturnVal + g_aHyperLinkFields[nLoopCounter]["linkSuffix"];
        if (sFieldName.toUpperCase() == "PIN"){

          sReturnVal = '<span class="darkUlineSmall"><a href="javascript:void(0);" onclick="document.getElementById(\'advSearchFrame\').src = \'' + sHref + '\';$(\'advSearchDiv\').style.display = \'block\';">' + sFieldValue + '</a></span>';
        } else {
          sReturnVal = '<span class="darkUlineSmall"><a href="javascript:void(0);" onclick="var mapParent = window.open(\'' + sHref + '\', \'' + g_sLinkWindowName + '\');mapParent.focus()">' + sFieldValue + '</a></span>';
        }
      }

    }
  }
  return sReturnVal;

}
*/

this.popUpPrintPage = function(){
  var sPopUpUrl = 'printPage.php?printMapHeight=' + g_nPrintMapHeight + '&printMapWidth=' + g_nPrintMapWidth + "&showresults=" + g_bIncludeResultsTable + "&showlegend=" + g_bIncludeLegend;
  g_oPrintPageWindow = window.open(sPopUpUrl, "PrintPage", "height=" + (g_nPrintMapHeight + 100) + ",width=" + (g_nPrintMapWidth + 100) + ",scrollbars=1,resizable=1,menubar=1,toolbar=1,status=1,location=1");
}


this.createPrintPage = function(){
  var pageSizeElem = document.getElementById("printPageSize");
  var pageOrientElem = document.getElementById("printPageOrientation");
  g_bPreserveMapExtent = false;
  g_bPreserveMapScale = false;
  g_bIncludeResultsTable = false;
  g_bIncludeLegend = false;


  g_bPreserveMapExtent = document.getElementById("preserveMapExtent").checked;
  g_bPreserveMapScale = document.getElementById("preserveMapScale").checked;
  g_bIncludeResultsTable = document.getElementById("includeResults").checked;
  g_bIncludeComments = document.getElementById("includeComments").checked;
  g_bIncludeLegend = document.getElementById("includeLegend").checked;
  g_sPrintPageComments = document.getElementById("printPageComments").value;
  g_sPrintPageTitle = document.getElementById("printPageTitleValue").value;

  var sPageSize = pageSizeElem.options[pageSizeElem.selectedIndex].value;
  var sPageOrient = pageOrientElem.options[pageOrientElem.selectedIndex].value;

  switch(sPageSize){
    case "85x11":
      if (sPageOrient == "portrait"){
        g_nPrintMapWidth = 600;
        g_nPrintMapHeight = 800;
      } else {
        g_nPrintMapWidth = 800;
        g_nPrintMapHeight = 600;
      }
      break;
    case "11x17":
      if (sPageOrient == "portrait"){
        g_nPrintMapWidth = 900;
        g_nPrintMapHeight = 1200;
      } else {
        g_nPrintMapWidth = 1200;
        g_nPrintMapHeight = 900;
      }
      break;
  }

  //load new legend if needed
  if (g_bIncludeLegend == true){
    createLegend();
  }

  if (g_bOkToGetMap){
    popUpPrintPage();
    //loadPrintMap();
  }


}


this.loadPrintMap = function(){
  //this is now called on the onload of the print page div
  var sLayers = "";
  showSpinner();
  sLayers = buildLayerString();
  g_bIsPrintMap = true;
  calcPrintExtents();
  getMap(g_fPrintMinX,g_fPrintMinY,g_fPrintMaxX,g_fPrintMaxY,g_nPrintMapHeight,g_nPrintMapWidth,sLayers,g_sLayersDataFrame);
}


this.parseGetFeaturesResponse = function(origRequest){
  var bLoadMap = false;
  var gmReply = origRequest.responseText;
  var nStartPos = 0;
  var nEndPos = 0;

  if (gmReply.indexOf('<ERROR') != -1){
    hideQuerySpinner();
    nStartPos = gmReply.indexOf('<ERROR');
    nStartPos = gmReply.indexOf('>',nStartPos) + 1;
    nEndPos = gmReply.indexOf('<',nStartPos);
    //reinit booleans
    g_bOkToGetFeatures = true;
    var sErrorMsg = gmReply.substring(nStartPos,nEndPos);
    alert("Error requesting map:\n" + sErrorMsg);
    mapImgElem.src = mapImgElem.src;
    return;
  }

  //get feature count and if it has more
  nStartPos = gmReply.indexOf('<FEATURECOUNT count="');
  nStartPos = nStartPos + 21;
  nEndPos = gmReply.indexOf(g_sDQuote,nStartPos);
  var nNewSelectCount = parseInt(gmReply.substring(nStartPos,nEndPos));

  nStartPos = gmReply.indexOf('hasmore="',nStartPos);
  nStartPos = nStartPos + 9;
  nEndPos = gmReply.indexOf(g_sDQuote,nStartPos);

  var sHasMore = gmReply.substring(nStartPos,nEndPos);

  if (sHasMore == "true"){
    g_bHasMore = true;
  } else {
    g_bHasMore = false;
  }

  //if there are no features found and the target layer is the parcel layer
  //then check additional PIN layers for matches - this will repeat for as
  //many PIN layers in g_aPINLayers
  if (nNewSelectCount == 0 && g_bSearchingPinLayers == true){
    if (g_nPINLayerIdx < g_aPINLayers.length){
      g_bOkToGetMap = true;
      g_bOkToGetFeatures = true;
      //2007_05_09 - JS - added next line to turn query spinner off
      hideQuerySpinner();
      searchOtherPinLayers();
      return;
    }
  } else {
    g_nPINLayerIdx = 0;
    g_bSearchingPinLayers = false;
  }

  //if there was a selected feature, but now there isn't then redraw map
  //to clear selected feature layer
  if (g_nSelectCount > 0 && nNewSelectCount == 0){
    bLoadMap = true;
  }

  g_nSelectCount = nNewSelectCount;

  if (g_nSelectCount > 1){
    bLoadMap = true;
  }


  //if only one reset global extents and zoom to it
  if (g_nSelectCount == 1 && g_bZoomToSingleSelect == true && g_bIsNextPrevQuery == false){
    nStartPos = gmReply.indexOf('<ENVELOPE minx="');
    nStartPos = nStartPos + 16;
    nEndPos = gmReply.indexOf(g_sDQuote,nStartPos);
    g_fMinX = parseFloat(gmReply.substring(nStartPos,nEndPos));

    nStartPos = gmReply.indexOf('miny="',nStartPos);
    nStartPos = nStartPos + 6;
    nEndPos = gmReply.indexOf(g_sDQuote,nStartPos);
    g_fMinY = parseFloat(gmReply.substring(nStartPos,nEndPos));

    nStartPos = gmReply.indexOf('maxx="',nStartPos);
    nStartPos = nStartPos + 6;
    nEndPos = gmReply.indexOf(g_sDQuote,nStartPos);
    g_fMaxX = parseFloat(gmReply.substring(nStartPos,nEndPos));

    nStartPos = gmReply.indexOf('maxy="',nStartPos);
    nStartPos = nStartPos + 6;
    nEndPos = gmReply.indexOf(g_sDQuote,nStartPos);
    g_fMaxY = parseFloat(gmReply.substring(nStartPos,nEndPos));

    //if it is a point, apply zoom out factor
    if (g_fMinX == g_fMaxX && g_fMinY == g_fMaxY){
      g_fMinX = g_fMinX - (g_nZoomToPointDist / 2);
      g_fMaxX = g_fMinX + g_nZoomToPointDist;
      g_fMinY = g_fMinY - (g_nZoomToPointDist / 2);
      g_fMaxY = g_fMinY + g_nZoomToPointDist;
    } else {
      //is not a point, so apply singleselect zoom factor
      var fXDist = g_fMaxX - g_fMinX;
      var fYDist = g_fMaxY - g_fMinY;
      fXDist = (fXDist * g_fSingleSelectZoomFactor);
      fYDist = (fYDist * g_fSingleSelectZoomFactor);

      g_fMinX = g_fMinX - fXDist;
      g_fMaxX = g_fMaxX + fXDist;
      g_fMinY = g_fMinY - fYDist;
      g_fMaxY = g_fMaxY + fYDist;
    }

    bLoadMap = true;
  }

  //parse features and build results div, unless request is coming from
  //adv. search list
  if(g_bUpdateResultsDiv){
    //extract everything from <FEATURES> TO </FEATURES> and send to buildResultsDiv()
    nStartPos = gmReply.indexOf('<FEATURES>');
    nStartPos = nStartPos + 10;
    nEndPos = gmReply.indexOf('</FEATURES>');
    var sFeatureList = gmReply.substring(nStartPos,nEndPos);

    buildFeatureEnvelopeArray(sFeatureList);
    buildResultsDiv(sFeatureList);
  }

  //reset global boolean to update results
  g_bUpdateResultsDiv = true;

  //don't draw map if results are from a buffer query.  This draw happens before query when it's a buffer
  if (g_sDrawBufferQuery != ""){
    bLoadMap = false;
  }

  //check to see if the query is a result of a get next, or get previous records request if so then don't loadmap
  if (g_bIsNextPrevQuery){
    bLoadMap = false;

    //reinit boolean
    g_bIsNextPrevQuery = false;
  }

  if (bLoadMap == true){
    loadMap();
  }

  //if it's a buffer select, then reset selected features to 0
  if(g_bIsBufferSelect){
    g_nSelectCount = 0;
  }

  g_bOkToGetFeatures = true;
  hideQuerySpinner();
}
