﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// shape link, this script has to work together with mapcontrol.js (get draw point), 
// selectinList.js  (get selected item)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var RESULT_ERROR = -1;
var RESULT_NULL = 0;
var RESULT_SINGLE = 1;
var RESULT_MULTI = 2;
var FAIL_MSG = "failed to reach server";        

var currentShapeLinkId = -1;
var currentSelectionIds = "";    // need a interface with selectionlist.js to get the active selection list, may not the current user clicked one
// call method using Ajax
function getUrlByPoint()
{
//alert("x= " + mouseX + ", y =" + mouseY);
  if(!sessionExpired)
  {
    PageMethods.ExecuteShapeLink(mouseX, mouseY, currentShapeLinkId, handleResponse);
  }
  else
    {
        HandleSessionExpired();
    }
  return false;
}

// call method using Ajax
function getUrlFromSelection (shapeLinkId)
{
    if(!sessionExpired)
  {
      //var hidElem = document.getElementById();
      var featureIdList = "";  // this value maybe read from client side function from selection page
      PageMethods.GetShapeLinkUrl(featureIdList, shapeLinkId, handleResponse);
  }
  else
{
    HandleSessionExpired();
}
}




// callback function
function handleResponse( response)
{
 var data = response;
 if (data != null)
 {
    var state = data.State;
    var content = data.Content;
    var wnd = data.Window;
    var title = "<html><head><title>"+data.PageTitle+"</title>";
    if (state == RESULT_ERROR || state == RESULT_NULL)
      alert(content);
    else if (state == RESULT_SINGLE)
    {
        window.open(content, wnd);
    }
    else if (state == RESULT_MULTI)
    {
        var handle = window.open(null, wnd);
        if (handle != null)
        {
          
          handle.document.write(title);
          handle.document.write("</head><body onload='self.focus()'>");
          handle.document.write(content);
          handle.document.write("</body></html>");
          handle.document.close();
                
         
//          if (window.focus)
//             {handle.focus();}
        }
    }
 }
 else
      alert(FAIL_MSG);
}

