var Loaded = false;
var SessionId = Math.floor(Math.random()*100000000);
var MapScript, MapPath;
var MapWidth, MapHeight;
var State="Zoom";
var Hint;
var timeId = 0;
var InfoWindow, LocateWindow, GotoWindow, OrientationWindow, EnlargeWindow, PrintWindow;
var ShowLL=1;
var VisibleRect;

// C2A changes begin
function afterImageLoaded() {//markers wil be drawn after map is loaded
  if (document.all) {

    if(eval("document.all.trackingmapframe")) 

      eval("document.all.trackingmapframe").src = "index.php?SID="+SessionId+"&view=trackingmapframe&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("trackingmapframe")) 

      document.getElementById("trackingmapframe").src = "index.php?SID="+SessionId+"&view=trackingmapframe&killcache="+Math.floor(Math.random()*100000000);

  }
  if (document.all) {

    if(eval("document.all.tripmonframe")) 

      eval("document.all.tripmonframe").src = "index.php?SID="+SessionId+"&view=tripmonframe&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("tripmonframe")) 

      document.getElementById("tripmonframe").src = "index.php?SID="+SessionId+"&view=tripmonframe&killcache="+Math.floor(Math.random()*100000000);

  }

  if (document.all) {

    if(eval("document.all.loc8frame")) 

      eval("document.all.loc8frame").src = "index.php?SID="+SessionId+"&view=loc8frame&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("loc8frame")) 

      document.getElementById("loc8frame").src = "index.php?SID="+SessionId+"&view=loc8frame&killcache="+Math.floor(Math.random()*100000000);

  }

  if (document.all) {

    if(eval("document.all.paniclocateframe")) 

      eval("document.all.paniclocateframe").src = "index.php?SID="+SessionId+"&view=paniclocframe&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("paniclocateframe")) 

      document.getElementById("paniclocateframe").src = "index.php?SID="+SessionId+"&view=paniclocframe&killcache="+Math.floor(Math.random()*100000000);

  }

  if (document.all) {

    if(eval("document.all.locateframe")) 

      eval("document.all.locateframe").src = "index.php?view=locateframe&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("locateframe")) 

      document.getElementById("locateframe").src = "index.php?view=locateframe&killcache="+Math.floor(Math.random()*100000000);

  }

  if (document.all) {

    if(eval("document.all.incidentframe")) 

      eval("document.all.incidentframe").src = "index.php?SID="+SessionId+"&view=incidentsearchingmap&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("incidentframe")) 

      document.getElementById("incidentframe").src = "index.php?SID="+SessionId+"&view=incidentsearchingmap&killcache="+Math.floor(Math.random()*100000000);

  }
  
  if (document.all) {

    if(eval("document.all.cleanupframe")) 

      eval("document.all.cleanupframe").src = "index.php?SID="+SessionId+"&view=incidentsearchingmap2&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("cleanupframe")) 

      document.getElementById("cleanupframe").src = "index.php?SID="+SessionId+"&view=incidentsearchingmap2&killcache="+Math.floor(Math.random()*100000000);

  }
  
  if (document.all) {

    if(eval("document.all.tbframe")) 

      eval("document.all.tbframe").src = "index.php?SID="+SessionId+"&view=tbmap&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("tbframe")) 

      document.getElementById("tbframe").src = "index.php?SID="+SessionId+"&view=tbmap&killcache="+Math.floor(Math.random()*100000000);

  }
  
  if (document.all) {

    if(eval("document.all.vframe")) 

      eval("document.all.vframe").src = "index.php?SID="+SessionId+"&view=vmaplist&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("vframe")) 

      document.getElementById("vframe").src = "index.php?SID="+SessionId+"&view=vmaplist&killcache="+Math.floor(Math.random()*100000000);

  }

  if (document.all) {  

    if(eval("document.all.rectangleframe")) 

      eval("document.all.rectangleframe").src = "index.php?SID="+SessionId+"&view=strsearch&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("rectangleframe")) 

      document.getElementById("rectangleframe").src = "index.php?SID="+SessionId+"&view=strsearch&killcache="+Math.floor(Math.random()*100000000);

  }

  if (document.all) {  

    if(eval("document.all.rectframe")) 
	  
      eval("document.all.rectframe").src = "lib/map/getrect.php";

  } else {

    if(document.getElementById("rectframe")) 
	  
      document.getElementById("rectframe").src = "lib/map/getrect.php";

  }
  if (document.all) {  

    if(eval("document.all.homemap")) 

      eval("document.all.homemap").src = "index.php?SID="+SessionId+"&view=homemap&killcache="+Math.floor(Math.random()*100000000);

  } else {

    if(document.getElementById("homemap")) 

      document.getElementById("homemap").src = "index.php?SID="+SessionId+"&view=homemap&killcache="+Math.floor(Math.random()*100000000);

  }
}
// C2A changes end

function LocateFeature(View, Display, Key, Scale, Tag)
{
  Query="";
  if (View) Query+="&view="+View;
  Query+="&display="+Display;
  Query+="&Key="+Key;
  if (Scale>0) Query+="&scale="+Scale;
  if (Tag) Query="Action=tag"+Query
  else Query="Action=locate"+Query;
  //if (KeepTags) Query+="&keeptags=yes";
  SetQuery(Query);
}

function GetFeatureAt(x, y)
{
  var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
  request.open("GET", MapScript+MapPath+"?xml&clickX="+x+"&clickY="+y+"&width="+MapWidth+"&height="+MapHeight+"&sessionid="+SessionId+"&time="+d.getTime(), false);
  request.send(null);
  str = new String(request.responseText);
  lines=request.responseText.split('\r\n');
  vals=lines[0].split('\t');
  this.Key=vals[0];
  this.FeatureClass=vals[1];
}

function GetRect()
{
  var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
  request.open("GET", MapScript+MapPath+"?getrect&sessionid="+SessionId+"&time="+d.getTime(), false);
  request.send(null);
  vals=request.responseText.split(',');
  this.x0=new Number(vals[0]);
  this.y0=new Number(vals[1]);
  this.x1=new Number(vals[2]);
  this.y1=new Number(vals[3]);
}

function GetLLStr(x, y)
{
    /*if (!VisibleRect) VisibleRect=new GetRect();
  var lon=new Number(x*(VisibleRect.x1-VisibleRect.x0)/MapWidth+VisibleRect.x0);
  var lat=new Number(VisibleRect.y1+y*(VisibleRect.y0-VisibleRect.y1)/MapHeight);
  return 'Lat: '+lat.toFixed(6)+' Long: '+lon.toFixed(6);
  */
  
  mylongitude = x*(rect[2]-rect[0])/MapWidth+rect[0];
  mylatitude = -(y*(rect[3]-rect[1])/MapHeight-rect[3]);
  return 'Lat:'+mylatitude.toFixed(6)+' Long:'+mylongitude.toFixed(6);//alert(mylongitude);

}

function SetSrc(id, url)
{
  if (Loaded)
  {
    elem=document.getElementById(id+"img");
    if (elem) elem.src = url;
  }
}

function MapLoading()
{
  document.getElementById('LoadingDiv').style.visibility='visible';
}

function MapLoaded()
{
  mapdiv=document.getElementById('MapArea');
  if (mapdiv)
  {
    mapdiv.style.left=0;
    mapdiv.style.top=0;
  }
  document.getElementById('LoadingDiv').style.visibility='hidden';
  elem=document.getElementById("markers");
  if (elem) elem.innerHTML="";
  //if (ShowLL)
  //  VisibleRect=new GetRect();  
// C2A changes begin
  afterImageLoaded();
// C2A changes end
}

function SetQuery(Query)
{
  MapLoading();
  d=new Date();
  SetSrc("MapArea", MapScript+MapPath+"?"+Query+"&width="+MapWidth+"&height="+MapHeight+"&sessionid="+SessionId+"&time="+d.getTime());
  //c2a changes
  if(Query=='Action=ZoomAll'&&document.getElementById('search')) {
    document.getElementById('search').src='index.php?SID='+SessionId+'&type=search&provID=&cityID=&burbID=';  
  }
}

function SetAlt(id, alt)
{
  if (Loaded)
    document.getElementById(id + "img").title = alt;
}

function SetState(S)
{
  if (Loaded)
  {
    State=S;
    SetSrc("Info", (State=="Info")?"/mapicons/info_2.gif":"/mapicons/info_0.gif");
    SetSrc("Zoom", (State=="Zoom")?"/mapicons/zoom_2.gif":"/mapicons/zoom_0.gif");
    SetSrc("Pan", (State=="Pan")?"/mapicons/pan_2.gif":"/mapicons/pan_0.gif");
    if (State=="Info") SetHint('Click on the map to get information about an object', true)
    else if (State=="Zoom") SetHint('Click on the map to zoom in; drag to zoom to area; right-click to zoom out', true)
    else if (State=="Capture") SetHint('Click on the map to capture a point.', true)
    else if (State=="Pan") SetHint('Click on the map to center that position', true);
  }
}

function SetHint(Text, Permanent)
{
  if (Loaded)
  {
    if (timeId>0) clearTimeout(timeId);
    timeId=0;
    document.getElementById('hint').innerHTML=Text;
    if (Permanent)
    {
      Hint=Text;
      //SetAlt("MapArea", Text);
    }
    window.status=Text;
  }
}

function RestoreHint()
{
  if (Loaded)
  {
    if (timeId==0) timeId=setTimeout("SetHint('"+Hint+"')", 50);
  }
}

function PreloadImg(URL)
{
  img = new Image();
  img.src = URL;
}

function CreateButton(ID, NormalURL, HighlightURL, ClickedURL, Width, Height, Alt, X, Y, onClick)
{
  PreloadImg(NormalURL);
  PreloadImg(HighlightURL);
  PreloadImg(ClickedURL);
  return '<STYLE TYPE="text/css">'+
    '#'+ID+' {'+
      'position:absolute;'+
      'left:'+X+';'+
      'top:'+Y+';'+
      'width:'+Width+';'+
      'height:'+Height+';'+
      'z-index:3'+
    '}'+
    '</STYLE>'+
    '<DIV ID="'+ID+'">'+
    '<IMG NAME="'+ID+'img" ID="'+ID+'img" SRC="'+((State==ID)?ClickedURL:NormalURL)+'" TITLE="'+Alt+
    '" BORDER="0" HEIGHT="'+Height+'" WIDTH="'+Width+'"'+
    ' onClick="'+
    ((ClickedURL)?"SetSrc('"+ID+"','"+ClickedURL+"');":"")+
    onClick+'"'+
    ((HighlightURL)?
      ' onMouseOver="SetHint('+"'"+Alt+"', false); "+'if (State!='+"'"+ID+"') "+"SetSrc('"+ID+"','"+HighlightURL+"')"+'"'+
      ' onMouseOut="RestoreHint(); if (State!='+"'"+ID+"') "+"SetSrc('"+ID+"','"+NormalURL+"')"+'"':'')+'>'+    
    '</DIV>';
}

function CreateMapImage(ID, X, Y, Width, Height, URL)
{
  return '<STYLE TYPE="text/css">'+
    '#'+ID+'ctr {'+
      'position:absolute;'+
      'left:'+(X-1)+'px;'+
      'top:'+(Y-1)+'px;'+
      'width:'+(Width)+'px;'+
      'height:'+(Height)+'px;'+
      'border: 1px solid black;'+
      'cursor:pointer;'+
      'overflow: hidden;'+
      'z-index:1'+
    '}'+
    '#'+ID+' {'+
      'position: absolute; left:0px; top:0px; width:500px; height:500px;'+
    '}'+
    '</STYLE>'+
    '<DIV ID="'+ID+'ctr">'+
//      '<DIV ID="'+ID+'old">'+
//        '<IMG NAME="'+ID+'oldimg" ID="'+ID+'oldimg" SRC="'+URL+'" BORDER="0" HEIGHT="'+Height+'" WIDTH="'+Width+' STYLE="{z-index:0;cursor:pointer;}" oncontextmenu="return false;">'+
//      '</DIV>'+
      '<DIV ID="'+ID+'">'+
        '<IMG NAME="'+ID+'img" ID="'+ID+'img" SRC="'+URL+'" BORDER="0" HEIGHT="'+Height+'" WIDTH="'+Width+' STYLE="{z-index:1;cursor:pointer;}" oncontextmenu="return false;" onload="MapLoaded();">'+
      '</DIV>'+
      '<DIV style="position:absolute;'+
        'left:'+((Width-32)/2)+';'+
        'top:'+((Height-32)/2)+';'+
        'width:32;'+
        'height:32;'+
        'cursor: pointer;'+
        'z-index:2;" ID="LoadingDiv" oncontextmenu="return false;">'+
        '<IMG NAME="Loadingimg" ID="Loadingimg" SRC="/mapicons/ajax-loader.gif" BORDER="0" STYLE="{z-index:2;cursor:pointer;}" oncontextmenu="return false;">'+
      '</DIV>'+
      '<DIV style="position: absolute;'+
        'left:0;'+
	'top:0;'+
        'width:'+Width+';'+
        'height:'+Height+';'+
        'cursor: pointer;'+
        'z-index:3;" ID="MarqueeDiv" oncontextmenu="return false;">'+
        /*'<DIV style="position: absolute;'+
          'left:0;'+
	  'top:0;'+
          'width:'+Width+';'+
          'height:'+Height+';'+
          'cursor: pointer;'+
          'z-index:2;" ID="markers" oncontextmenu="return false;">'+
        '</DIV>'+*/    
      '</DIV>'+    
    '</DIV>';
 /*   '<DIV style="position:absolute;'+
      'left:'+(X+(Width-32)/2)+';'+
      'top:'+(Y+(Height-32)/2)+';'+
      'width:32;'+
      'height:32;'+
      'cursor: pointer;'+
      'z-index:2;" ID="LoadingDiv" oncontextmenu="return false;">'+
      '<IMG NAME="Loadingimg" ID="Loadingimg" SRC="/mapicons/ajax-loader.gif" BORDER="0" STYLE="{z-index:2;cursor:pointer;}" oncontextmenu="return false;">'+
    '</DIV>'+*/
   /* '<DIV style="position:absolute;'+
      'left:'+(X)+';'+
      'top:'+(Y)+';'+
      'width:'+Width+';'+
      'height:'+Height+';'+
      'cursor: pointer;'+
      'z-index:3;" ID="MarqueeDiv" oncontextmenu="return false;">'+
    '</DIV>'+
    '<DIV style="position:absolute;'+
      'left:'+(X)+';'+
      'top:'+(Y)+';'+
      'width:'+Width+';'+
      'height:'+Height+';'+
      'cursor: pointer; cursor: hand;'+
      'z-index:4;" ID="markers" oncontextmenu="return false;">'+
    '</DIV>'*/
}

function CreateMapSWF(ID, X, Y, Width, Height, URL)
{
  return '<STYLE TYPE="text/css">'+
    '#'+ID+' {'+
      'position:absolute;'+
      'left:'+X+';'+
      'top:'+Y+';'+
      'width:'+Width+';'+
      'height:'+Height+';'+
      'cursor:pointer;'+
      'z-index:1'+
    '}'+
    '</STYLE>'+
    '<DIV ID="'+ID+'">'+
    '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+Width+'" height="'+Height+'">'+
    '  <param name="movie" value="'+URL+'" />'+
    '  <param name="quality" value="high" />'+
    '  <embed src="'+URL+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+Width+'" height="'+Height+'"></embed>'+
    '</object>'+
    '</DIV>';
}

function CreateHint(ID, X, Y, Width, Height, Text)
{
  Hint=Text;
  return '<STYLE TYPE="text/css">'+
    '#'+ID+' {'+
      'position: absolute;'+
      'left: '+X+';'+
      'top: '+Y+';'+
      'width: '+Width+';'+
      'height: '+Height+';'+
      'z-index: 1;'+
      'font: 10pt Arial'+
    '}'+
    '</STYLE>'+
    '<DIV ID="'+ID+'">'+
    Text+
    '</DIV>';
}

function DoInfo(x, y, key) // key added for c2a
{
  d=new Date();
// c2a change
  //url=MapScript+MapPath+"?info&clickX="+x+"&clickY="+y+"&sessionid="+SessionId+"&time="+d.getTime();
  url="index.php?type=entitydetails&key="+key;
// end c2a change
  if (!(InfoWindow) || (InfoWindow.closed)) InfoWindow=window.open(url, "InfoWindow", "width=600,height=400,resizable=yes,scrollbars=yes")
  else
  {
    InfoWindow.location=url;
    InfoWindow.focus();
  }
}

/*function MapClick(e)
{
  if (document.all)
  {
    x=window.event.offsetX;
    y=window.event.offsetY;
  }
  else
  {
    x=e.layerX;
    y=e.layerY;
  }
  if (State=="Info") DoInfo(x, y)
  else if (State=="Capture") DoCapture(x, y)
  else SetQuery("Action="+State+"&clickX="+x+"&clickY="+y);
}*/

function ZoomScale(Scale)
{
  SetQuery("Action=Zoom&scale="+Scale);
}

function Redraw()
{
  SetQuery("Action=Redraw");
}

function DoLocate()
{
  if (!(LocateWindow) || (LocateWindow.closed)) LocateWindow=window.open(MapScript+MapPath+"?findstreet", "LocateWindow", "width=400,height=300,resizable=yes,scrollbars=yes")
  else
  {
    LocateWindow.location=MapScript+MapPath+"?findstreet";
    LocateWindow.focus();
  }
}

function DoGoto()
{
  if (!(GotoWindow) || (GotoWindow.closed)) GotoWindow=window.open(MapScript+MapPath+"?goto", "GotoWindow", "width=400,height=300,resizable=yes,scrollbars=yes")
  else
  {
    GotoWindow.location=MapScript+MapPath+"?goto";
    GotoWindow.focus();
  }
}

function DoOrientate()
{
  d=new Date();
  if (!(OrientationWindow) || (OrientationWindow.closed)) OrientationWindow=window.open(MapScript+MapPath+"?noupdate&Action=orientate&factor=5&width=400&height=300&sessionid="+SessionId+"&time="+d.getTime(), "OrientationWindow", "width=440,height=340,resizable=yes,scrollbars=yes")
  else
  {
    OrientationWindow.location=MapScript+MapPath+"?noupdate&Action=orientate&factor=5&width=400&height=300&sessionid="+SessionId+"&time="+d.getTime();
    OrientationWindow.focus();
  }
}

function DoEnlarge()
{
  d=new Date();
  if (!(EnlargeWindow) || (EnlargeWindow.closed)) EnlargeWindow=window.open(MapScript+MapPath+"?noupdate&Action=Redraw&width=800&height=600&sessionid="+SessionId+"&time="+d.getTime(), "EnlargeWindow", "width=830,height=620,resizable=yes,scrollbars=yes")
  else
  {
    EnlargeWindow.location=MapScript+MapPath+"?noupdate&Action=Redraw&width=800&height=600&sessionid="+SessionId+"&time="+d.getTime();
    EnlargeWindow.focus();
  }
}

function DoPrint()
{
  d=new Date();
  if (!(PrintWindow) || (PrintWindow.closed)) PrintWindow=window.open(MapScript+MapPath+"?noupdate&Action=Redraw&width="+(MapWidth*2)+"&height="+(MapHeight*2)+"&sessionid="+SessionId+"&time="+d.getTime(), "PrintWindow", "")
  else
  {
    PrintWindow.location=MapScript+MapPath+"?noupdate&Action=Redraw&width="+(MapWidth*2)+"&height="+(MapHeight*2)+"&sessionid="+SessionId+"&time="+d.getTime();
    PrintWindow.focus();
  }
  //PrintWindow.onload="javascript:window.print();";
  //PrintWindow.print();
}

/*function DoCapture(x,y) {
   if (!VisibleRect) VisibleRect=new GetRect();
   var mylongitude=new Number(x*(VisibleRect.x1-VisibleRect.x0)/MapWidth+VisibleRect.x0);
   var mylatitude=new Number(VisibleRect.y1+y*(VisibleRect.y0-VisibleRect.y1)/MapHeight);
   mylongitude=mylongitude.toFixed(6);
   mylatitude=mylatitude.toFixed(6);
   //mylongitude = x*(rect[2]-rect[0])/350+rect[0];
   if (document.all) {
     eval("document.all.longitude").value = mylongitude;
   } else {
     document.getElementById("longitude").value = mylongitude;
   }
   //mylatitude = -(y*(rect[3]-rect[1])/350-rect[3]);
   if (document.all) {
     eval("document.all.latitude").value = mylatitude;
   } else {
     document.getElementById("latitude").value = mylatitude;
   }
   if (document.all) {
     var elm=eval("document.all.MarqueeDiv");
   } else {
     var elm=document.getElementById("MarqueeDiv");
   }
   
   if(elm) elm.innerHTML="";
   var elmImage=new Image();
 		if(typeof(w)=="undefined" || w==null) w=7;
 		if(typeof(h)=="undefined" || h==null) h=7;
 		elmImage.style.position="absolute";
 		if(typeof(name)=="undefined" || name==null || name.length==0) name="dotgreen7x7.gif";
 		elmImage.src="images/markerfile/dotgreen7x7.gif"
 		if(typeof(title)=="undefined") title="";
 		//elmImage.title=title;
 		//elmImage.id=id;
 		elmImage.width=w;
 		elmImage.height=h;
 		elmImage.style.left=String(x-(w>>1)-2)+"px";
 		elmImage.style.top=String(y-(h>>1)-2)+"px";
 		//elmImage.onmouseover=marker_onmouseover;
 		//elmImage.onmouseout=marker_onmouseout;
 		//elmImage.onclick=noeffect;
 		elmImage.style.borderStyle="none";
 		elmImage.style.borderWidth="2px";
 		elmImage.style.borderColor="red";
 		elmImage.style.margin="2px";
 		if (document.all) {
     var elmMarkers=eval("document.all.MarqueeDiv");
   } else {
     var elmMarkers=document.getElementById("MarqueeDiv");
   }
 		elmMarkers.appendChild(elmImage);
}*/
function DoCapture(x,y) {

   mylongitude = x*(rect[2]-rect[0])/350+rect[0];

   if (document.all) {

     eval("document.all.longitude").value = mylongitude;

   } else {

     document.getElementById("longitude").value = mylongitude;

   }

   mylatitude = -(y*(rect[3]-rect[1])/350-rect[3]);

   if (document.all) {

     eval("document.all.latitude").value = mylatitude;

   } else {

     document.getElementById("latitude").value = mylatitude;

   }

   if (document.all) {

     var elm=eval("document.all.MarqueeDiv");

   } else {

     var elm=document.getElementById("MarqueeDiv");

   }

   

   if(elm) elm.innerHTML="";
        var elmImage=new Image();
        elmImage.src="images/dotgreen7x7.gif"
 		if(typeof(w)=="undefined" || w==null) w=7;

 		if(typeof(h)=="undefined" || h==null) h=7;

 		elmImage.style.position="absolute";

 		if(typeof(name)=="undefined" || name==null || name.length==0) name="dotgreen7x7.gif";

 		

 		if(typeof(title)=="undefined") title="";

 		//elmImage.title=title;

 		//elmImage.id=id;

 		elmImage.width=w;

 		elmImage.height=h;

 		elmImage.style.left=String(x-(w>>1)-2)+"px";

 		elmImage.style.top=String(y-(h>>1)-2)+"px";

 		//elmImage.onmouseover=marker_onmouseover;

 		//elmImage.onmouseout=marker_onmouseout;

 		//elmImage.onclick=noeffect;

 		elmImage.style.borderStyle="none";

 		elmImage.style.borderWidth="2px";

 		elmImage.style.borderColor="red";

 		elmImage.style.margin="2px";

 		if (document.all) {

     var elmMarkers=eval("document.all.MarqueeDiv");

   } else {

     var elmMarkers=document.getElementById("MarqueeDiv");

   }

 		elmMarkers.appendChild(elmImage);

}

function Pan(X,Y)
{
  SetQuery("Action=Pan&X="+X+"&Y="+Y);
}

function CreateMap(Script, Path, Query, Style, Width, Height, Buttons, Session, Capture, access)
{
  MapScript=Script;
  MapPath=Path;
  MapWidth=Width;
  MapHeight=Height;
  if (Session) SessionId=Session;
  d=new Date();
  MapQuery=Script+Path+"?width="+Width+"&height="+Height+((Query!="")?"&"+Query:"")+"&sessionid="+SessionId+"&time="+d.getTime();
  TopButtons="";
  T=0;
  L=32;
  Buttons=Buttons.toLowerCase();
//  if (Buttons.indexOf("locate")>=0) { TopButtons+=CreateButton("Locate", "/mapicons/locate0.gif", "/mapicons/locate1.gif", "/mapicons/locate2.gif", 64, 32, "Locate", L, 0, "DoLocate()"); L+=64; }
//  if (Buttons.indexOf("goto")>=0) { TopButtons+=CreateButton("Goto", "/mapicons/goto0.gif", "/mapicons/goto1.gif", "/mapicons/goto2.gif", 58, 32, "Goto", L, 0, "DoGoto()"); L+=58; }
  if (Buttons.indexOf("orientation")>=0) { TopButtons+=CreateButton("Orientation", "/mapicons/orient0.gif", "/mapicons/orient1.gif", "/mapicons/orient2.gif", 84, 32, "Orientation", L, 0, "DoOrientate()"); L+=84; }
  if (Buttons.indexOf("enlarge")>=0) { TopButtons+=CreateButton("Enlarge", "/mapicons/enlarge0.gif", "/mapicons/enlarge1.gif", "/mapicons/enlarge2.gif", 75, 32, "Enlarge", L, 0, "DoEnlarge()"); L+=75; }
  if (Buttons.indexOf("help")>=0) { TopButtons+=CreateButton("Help", "/mapicons/help0.gif", "/mapicons/help1.gif", "/mapicons/help2.gif", 48, 32, "Help", L, 0, "DoHelp()"); }
  LeftButtons="";
  if (L>32)
    T=32;
  MapTop=T;
  //LeftButtons+=CreateButton("Info", "/mapicons/info_0.gif", "/mapicons/info_1.gif", "/mapicons/info_2.gif", 32, 32, "Info", 0, T, "SetState('Info')"); T+=32;
  LeftButtons+=CreateButton("Zoom", "/mapicons/zoom_0.gif", "/mapicons/zoom_1.gif", "/mapicons/zoom_2.gif", 32, 32, "Zoom in", 0, T, "SetState('Zoom')"); T+=32;
  LeftButtons+=CreateButton("ZoomOut", "/mapicons/zoomo_0.gif", "/mapicons/zoomo_1.gif", "", 32, 32, "Zoom out", 0, T, "SetQuery('Action=ZoomOutx2&ClickX="+(Width/2)+"&ClickY="+(Height/2)+"')"); T+=32;
  LeftButtons+=CreateButton("Pan", "/mapicons/pan_0.gif", "/mapicons/pan_1.gif", "/mapicons/pan_2.gif", 32, 32, "Centre a point", 0, T, "SetState('Pan')"); T+=32;
  LeftButtons+=CreateButton("ZoomAll", "/mapicons/zoomall_0.gif", "/mapicons/zoomall_1.gif", "", 32, 32, "Restart", 0, T, "SetQuery('Action=ZoomAll')"); T+=32;
  LeftButtons+=CreateButton("ZoomPrev", "/mapicons/zoomp_0.gif", "/mapicons/zoomp_1.gif", "", 32, 32, "Previous", 0, T, "SetQuery('Action=ZoomPrev')"); T+=32;
  LeftButtons+=CreateButton("Print", "/mapicons/print_0.gif", "/mapicons/print_1.gif", "", 32, 32, "Print", 0, T, "DoPrint()"); T+=32;
  (Capture?LeftButtons+=CreateButton("Capture", "/mapicons/pushpin.gif", "/mapicons/pushpinover.gif", "/mapicons/pushpinin.gif", 32, 32, "Capture", 0, T, "SetState('Capture')"):'');T+=32;
  document.write(
    '<STYLE TYPE="text/css">',
    '#map {', Style,
      'width:', Width+34, ';',
      'height:', Height+(TopButtons!=""?32:0)+22, ';',
    '}',
    '</STYLE>',
    '<DIV ID="map">',
  TopButtons,
  LeftButtons,
  CreateMapImage("MapArea", 33, MapTop+1, Width, Height, MapQuery),
  CreateHint("hint", 32, Height+MapTop+2, Width, 32, ""),
  CreateButton("PanLeft", "/mapicons/left.gif", "/mapicons/left_h.gif", "", 17, 15, "Pan left", 33, MapTop+Height/2-8, "Pan(-90,0)"),
  CreateButton("PanLeftUp", "/mapicons/nw.gif", "/mapicons/nw_h.gif", "", 16, 16, "Pan left and up", 32, MapTop, "Pan(-90,90)"),
  CreateButton("PanUp", "/mapicons/up.gif", "/mapicons/up_h.gif", "", 15, 17, "Pan up", 32+Width/2-8, MapTop+1, "Pan(0,90)"),
  CreateButton("PanRightUp", "/mapicons/ne.gif", "/mapicons/ne_h.gif", "", 16, 16, "Pan right and up", Width+18, MapTop, "Pan(90,90)"),
  CreateButton("PanRight", "/mapicons/right.gif", "/mapicons/right_h.gif", "", 17, 15, "Pan right", 32+Width-16, MapTop+Height/2-8, "Pan(90,0)"),
  CreateButton("PanRightDown", "/mapicons/se.gif", "/mapicons/se_h.gif", "", 16, 16, "Pan right and down", Width+18, MapTop+Height-14, "Pan(90,-90)"),
  CreateButton("PanDown", "/mapicons/down.gif", "/mapicons/down_h.gif", "", 15, 17, "Pan down", 32+Width/2-8, MapTop+Height-16, "Pan(0,-90)"),
  CreateButton("PanLeftDown", "/mapicons/sw.gif", "/mapicons/sw_h.gif", "", 16, 16, "Pan left and down", 32, MapTop+Height-14, "Pan(-90,-90)"),
  '</DIV>');
}

function CreateSWFMap(Script, Path, Query, Style, Width, Height, Buttons)
{
  State = "Info";
  MapScript=Script;
  MapPath=Path;
  MapWidth=Width;
  MapHeight=Height;
  d=new Date();
  MapQuery=Script+Path+"?width="+Width+"&height="+Height+((Query!="")?"&"+Query:"")+"&time="+d.getTime();
  TopButtons="";
  L=32;
  Buttons=Buttons.toLowerCase();
//  if (Buttons.indexOf("locate")>=0) { TopButtons+=CreateButton("Locate", "/mapicons/locate0.gif", "/mapicons/locate1.gif", "/mapicons/locate2.gif", 64, 32, "Locate", L, 0, "DoLocate()"); L+=64; }
//  if (Buttons.indexOf("goto")>=0) { TopButtons+=CreateButton("Goto", "/mapicons/goto0.gif", "/mapicons/goto1.gif", "/mapicons/goto2.gif", 58, 32, "Goto", L, 0, "DoGoto()"); L+=58; }
  if (Buttons.indexOf("orientation")>=0) { TopButtons+=CreateButton("Orientation", "/mapicons/orient0.gif", "/mapicons/orient1.gif", "/mapicons/orient2.gif", 84, 32, "Orientation", L, 0, "DoOrientate()"); L+=84; }
  TopButtons+=CreateButton("Enlarge", "/mapicons/enlarge0.gif", "/mapicons/enlarge1.gif", "/mapicons/enlarge2.gif", 75, 32, "Enlarge", L, 0, "DoEnlarge()");
  L+=75;
  document.write(
    '<STYLE TYPE="text/css">',
    '#map {', Style,
      'width:', Width+64, ';',
      'height:', Height+64, ';',
    '}',
    '</STYLE>',
    '<DIV ID="map">',
  TopButtons,
  CreateButton("Info", "/mapicons/info_0.gif", "/mapicons/info_1.gif", "/mapicons/info_2.gif", 32, 32, "Info", 0, 32, "SetState('Info')"),
  CreateButton("Zoom", "/mapicons/zoom_0.gif", "/mapicons/zoom_1.gif", "/mapicons/zoom_2.gif", 32, 32, "Zoom In", 0, 64, "SetState('Zoom')"),
  CreateButton("Pan", "/mapicons/pan_0.gif", "/mapicons/pan_1.gif", "/mapicons/pan_2.gif", 32, 32, "Pan", 0, 192-64, "SetState('Pan')"),
  CreateButton("ZoomAll", "/mapicons/zoomall_0.gif", "/mapicons/zoomall_1.gif", "", 32, 32, "Zoom all", 0, 224-64, "SetQuery('Action=ZoomAll')"),
  CreateButton("ZoomPrev", "/mapicons/zoomp_0.gif", "/mapicons/zoomp_1.gif", "", 32, 32, "Zoom previous", 0, 256-64, "SetQuery('Action=ZoomPrev')"),
  CreateMapSWF("MapArea", 33, 33, Width, Height, MapQuery),
  CreateHint("hint", 32, Height+32, Width, 32, "Click on the map to get information about an object"),
  CreateButton("PanLeft", "/mapicons/left.gif", "/mapicons/left_h.gif", "", 17, 15, "Pan left", 32, 32+Height/2-8, "Pan(-90,0)"),
  CreateButton("PanLeftUp", "/mapicons/nw.gif", "/mapicons/nw_h.gif", "", 16, 16, "Pan left and up", 32, 32, "Pan(-90,90)"),
  CreateButton("PanUp", "/mapicons/up.gif", "/mapicons/up_h.gif", "", 15, 17, "Pan up", 32+Width/2-8, 32, "Pan(0,90)"),
  CreateButton("PanRightUp", "/mapicons/ne.gif", "/mapicons/ne_h.gif", "", 16, 16, "Pan right and up", Width+16, 32, "Pan(90,90)"),
  CreateButton("PanRight", "/mapicons/right.gif", "/mapicons/right_h.gif", "", 17, 15, "Pan right", 32+Width-17, 32+Height/2-8, "Pan(90,0)"),
  CreateButton("PanRightDown", "/mapicons/se.gif", "/mapicons/se_h.gif", "", 16, 16, "Pan right and down", Width+16, Height+16, "Pan(90,-90)"),
  CreateButton("PanDown", "/mapicons/down.gif", "/mapicons/down_h.gif", "", 15, 17, "Pan down", 32+Width/2-8, 32+Height-17, "Pan(0,-90)"),
  CreateButton("PanLeftDown", "/mapicons/sw.gif", "/mapicons/sw_h.gif", "", 16, 16, "Pan left and down", 32, Height+16, "Pan(-90,-90)"),
  '</DIV>');
}

var x, y, x0, y0, x1, y1;
var MouseDown = false;
var Marquee = null;

function onMouseDown(e)
{
  if(document.all)
  {
    x=event.offsetX;
    y=event.offsetY;
    elm=window.event.srcElement.id;
  }
  else
  {
    x=e.layerX;
    y=e.layerY;
    elm=e.target.id;
    e.stopPropagation();
  }
  //SetHint(Hint+' down'+elm+e.bubbles+','+e.currentTarget.id+','+e.layerX, true);
  if (elm!="MapAreaimg" && elm!="MarqueeDiv" && elm!='Marquee' && elm!="markers")
      return;
  x0=x1=x;
  y0=y1=y;
  if (State=="Zoom")
  {
    MouseDown = true;
    Marquee=document.getElementById('Marquee');
    if (!Marquee)
    {
      Marquee=document.createElement('DIV');
      Marquee.style.border="1px dashed black";
      Marquee.style.position="absolute";
      Marquee.style.visibility="hidden";
      Marquee.id="Marquee";
      document.getElementById("MarqueeDiv").appendChild(Marquee);
    }
    Marquee.style.left=x;
    Marquee.style.top=y;
    Marquee.style.width=0;
    Marquee.style.height=0;
  }
  else if (State=="Pan")
  {
    MouseDown=true;
  }
}

function onMouseMove(e)
{
  if(document.all)
  {
    x=event.offsetX;
    y=event.offsetY;
    event.cancelBubble = true;
    event.returnValue = false;
    elm=window.event.srcElement.id;
  }
  else
  {
    x=e.layerX;
    y=e.layerY;
    e.stopPropagation();
    elm=e.target.id;
  }
  //SetHint(Hint+' move'+elm+','+e.currentTarget.id, true);
  if (MouseDown)
  {
    if (elm=="Marquee")
    {
      if (document.all)
        return;
      x=Math.min(x0,x1)+x;
      y=Math.min(y0,y1)+y;
    }
    else if (elm!="MapAreaimg" && elm!="MarqueeDiv" && elm!="markers")
      return;
    if (x>MapWidth-1) x=MapWidth-1;
    if (y>MapHeight-1) y=MapHeight-1;
    x1=x;
    y1=y;
    if (State=="Zoom")
    {
      Marquee.style.left=Math.min(x0,x1);
      Marquee.style.top=Math.min(y0,y1);
      Marquee.style.width=Math.abs(x1-x0);
      Marquee.style.height=Math.abs(y1-y0);
      Marquee.style.visibility="visible";
    }
    /*else if (State=="Pan")
    {
      elem=document.getElementById("MapArea");
      //alert(elem.style);
      if (elem) 
      {
        elem.style.left=x1-x0;
	elem.style.top=y1-y0;
      }
    }*/
  }
  if (elm!="MapAreaimg" && elm!="MarqueeDiv" && elm!="markers")
      return; 
  if (ShowLL)
    SetHint(GetLLStr(x,y));
}

function onMouseUp(e)
{
  MouseDown = false;
  if (Marquee)
    Marquee.style.visibility="hidden";
  if(document.all)
    elm=window.event.srcElement.id
  else
  {
    elm=e.target.id;
    e.stopPropagation();
  }
  //mapdiv=document.getElementById("MapArea");
  //map=document.getElementById("MapAreaimg");
  //SetHint(Hint+' up'+elm, true);
  /*if (State=="Pan")
  { //alert(x0+' '+y0+' '+x1+' '+y1);
    if ((Math.abs(x1-x0)>2) && (Math.abs(y1-y0)>2))
    {
      x0=(MapWidth/2)-(x1-x0);
      y0=(MapHeight/2)-(y1-y0);
      //alert(x0+' '+y0);
    }
  }*/
  if (elm!="MapAreaimg" && elm!="MarqueeDiv" && elm!='Marquee' && elm!="markers")
  { //alert(elm);
      return;}
  right=false;
  if (document.all)
  {
    if (event.button==2) right=true;
  }
  else if (e.which==3)
    right=true;
  if (right)
  {
    //if (State=="ZoomInX2" || State=="ZoomInX5")
    /*if (map) 
    {
      //alert(map.width);
      //map.width=map.width/2;
      //map.height=map.height/2;
    }*/
      SetQuery("Action=zoomoutx2&ClickX="+x0+"&ClickY="+y0);
  }
  else
  {
    if (State=="Info")
    {
      DoInfo(x0, y0);
      // or write your own handler eg.:
      // F = new GetFeatureAt(x0, y0);
      // alert(F.Key);
    }
    else if (State=="Capture") DoCapture(x0, y0)
    else if ((x1>x0+2) && (y1>y0+2) && (State=="Zoom"))
      SetQuery("Action=zoomrect&x0="+x0+"&y0="+y0+"&x1="+x1+"&y1="+y1)
    else if (State=="Zoom")
    {
      //map.width=map.width*2;
      //map.height=map.height*2;
	  
      SetQuery("Action=ZoomInX2&ClickX="+x0+"&ClickY="+y0)
    }
    else
      SetQuery("Action="+State+"&ClickX="+x0+"&ClickY="+y0);
  }
}

function onDragEnter(e)
{
  e.stopPropagation();
  e.preventDefault();
}

function onDragExit(e)
{
  if (e.layerX>0 && e.layerY>0 && e.layerX<MapWidth && e.layerY<MapHeight)
    onMouseUp(e);
}

function onLoad()
{
  if (document.all) {
    //document.all.MapAreaimg.onclick=MapClick;
    document.getElementById('MapAreaimg').attachEvent('onmousedown', onMouseDown);
    document.getElementById('MapAreaimg').attachEvent('onmousemove', onMouseMove);
    document.getElementById('MapAreaimg').attachEvent('onmouseup', onMouseUp);
  } else {
    //document.getElementById('markers').addEventListener('click', MapClick, false);
    /*document.getElementById('MarqueeDiv').addEventListener('mousemove', onMouseMove, false);
    document.getElementById('MarqueeDiv').addEventListener('mousedown', onMouseDown, false);
    document.getElementById('MarqueeDiv').addEventListener('mouseup', onMouseUp, false);
    document.getElementById('markers').addEventListener('mousemove', onMouseMove, false);
    document.getElementById('markers').addEventListener('mousedown', onMouseDown, true);
    document.getElementById('markers').addEventListener('mouseup', onMouseUp, false);
    document.getElementById('MapAreactr').addEventListener('mousemove', onMouseMove, false);
    document.getElementById('MapAreactr').addEventListener('mousedown', onMouseDown, false);
    document.getElementById('MapAreactr').addEventListener('mouseup', onMouseUp, false);*/
    document.getElementById('map').addEventListener('mousemove', onMouseMove, true); document.getElementById('map').addEventListener('dragover', onMouseMove, true);
    document.getElementById('map').addEventListener('mousedown', onMouseDown, true); document.getElementById('map').addEventListener('dragenter', onDragEnter, true);
    document.getElementById('map').addEventListener('mouseup', onMouseUp, true);  document.getElementById('map').addEventListener('dragexit', onDragExit, true);  
  }
  Loaded=true;
  SetState(State);
}

window.onload=onLoad;



