function GeoExtent() {
  this.xmin=0;
  this.ymin=0;
  this.xmax=0;
  this.ymax=0;
  this.first=true;
  this.AddCoord=function(x,y) {
    if (this.first) {
      this.xmin=x;
      this.ymin=y;
      this.xmax=x;
      this.ymax=y;
      this.first=false;
    }
    else {
      if (this.xmin>x) this.xmin=x;
      if (this.ymin>y) this.ymin=y;
      if (this.xmax<x) this.xmax=x;
      if (this.ymax<y) this.ymax=y;
    }
  }
  this.asString=function(trenner,delta) {
    if (!trenner) trenner=",";
    if (!delta) delta=0;
    return (this.xmin-delta)+trenner+(this.ymin-delta)+trenner+(this.xmax+delta)+trenner+(this.ymax+delta);
  }
  this.setExtent=function(xmin,ymin,xmax,ymax) {
    this.xmin=xmin;
    this.ymin=ymin;
    this.xmax=xmax;
    this.ymax=ymax;
    this.first=false;
  }
  this.addExtent=function(xmin,ymin,xmax,ymax) {
    if (this.first) {
      this.xmin=xmin;
      this.ymin=ymin;
      this.xmax=xmax;
      this.ymax=ymax;
      this.first=false;
    }
    else {
      if (this.xmin>xmin) this.xmin=xmin;
      if (this.ymin>ymin) this.ymin=ymin;
      if (this.xmax<xmax) this.xmax=xmax;
      if (this.ymax<ymax) this.ymax=ymax;    
    }
  }
  this.containInExtent=function(aExtent) {    
    if ((this.xmin>aExtent.xmin) && (this.ymin>aExtent.ymin) && (this.xmax<aExtent.xmax) && (this.ymax<aExtent.ymax))
      return true;
    else return false;  
  }
  this.contain=function(ECWView) {
    var geoExtentView=new GeoExtent();
    geoExtentView.setExtent(ECWView.GetTopLeftWorldCoordinateX(),ECWView.GetBottomRightWorldCoordinateY(),
                            ECWView.GetBottomRightWorldCoordinateX(),ECWView.GetTopLeftWorldCoordinateY());
    if (this.containInExtent(geoExtentView)) return GeoExtent.ContainedInView;
    else {
      var geoExtentGlobal=new GeoExtent();
      var n=ECWView.GetNumberLayers();
      for (var i=0; i<n;i++) {
        var lyrName=ECWView.GetLayerName(i);
        if (lyrName=="LogoLayer") continue;
        geoExtentGlobal.addExtent(
          ECWView.GetLayerImageTopLeftWorldCoordinateX(lyrName),ECWView.GetLayerImageBottomRightWorldCoordinateY(lyrName),
          ECWView.GetLayerImageBottomRightWorldCoordinateX(lyrName),ECWView.GetLayerImageTopLeftWorldCoordinateY(lyrName)
        );
      }
      if (this.containInExtent(geoExtentGlobal)) return GeoExtent.ContainedInMap;
      else return GeoExtent.NoContained;
    }
  }
}
GeoExtent.NoContained    =0;
GeoExtent.ContainedInView=1;
GeoExtent.ContainedInMap =2;
function NCSCaptureCoord (objID, multi,className, ECWVectorLayer, foreignID, foreignTitle) {
  this.objID=objID;
  this.foreignID=foreignID;
  this.foreignTitle=foreignTitle;
  this.multi=multi;
  //this.className=className; 
  this.ECWVectorLayer=ECWVectorLayer;
  this.txtAreaID="capCoord";
  this.btnName="btnDraw";
  this.build=function() {
    var text="<div id=\""+this.objID+"\">"+// class=\""+this.className+"\">"+
             "<table width=100% height=100%>";
    /*if (this.image && this.imageDown) text+="<tr><th><table width=100%><tr><th>&nbsp;&nbsp;Koordinaten</th><td align=right><image src=\""+this.image.src+"\" onmousedown=\"this.src='"+this.imageDown.src+"'; return true;\" onmouseout=\"this.src='"+this.image.src+"'; return true;\" onclick=\"closeCapCoord('"+this.objID+"');\"></td></tr></table></th></tr>";
    else*/
    text+="<tr><th>&nbsp;&nbsp;Koordinaten</th></tr>";
    text+="<tr><td height=100%>";
    if (!this.multi) text+="<input type=\"text\" id=\""+this.txtAreaID+"\" name=\""+this.txtAreaID+"\" class=\"controls\">";
    else text+="<textArea id=\""+this.txtAreaID+"\" name=\""+this.txtAreaID+"\" wrap=\"off\" class=\"controlArea\"></textArea>";
    text+="</td></tr>"+
          "<tr height=25>"+
          "<td align=\"center\"><input type=\"button\" id=\""+this.btnName+"\" name=\""+this.btnName+"\" class=\"btnNew\" value=\"Zeigen\" onclick=\"drawGeo('"+this.txtAreaID+"');\"><input type=\"button\"  class=\"btnNew\" value=\"Kopieren\" onclick=\"TxtCopy('"+this.txtAreaID+"')\"></td>"+
          "</tr>"+          
          "<tr>"+
          "<th class=\"deactive\" onmouseover=\"this.className='active';\" onmouseout=\"this.className='deactive';\" onclick=\"getLayer('"+this.objID+"').display='none';getLayer('"+this.foreignID+"').display='block';\">"+this.foreignTitle+"</th>"+
          "</tr>"+
          "</table>"+
          "</div>";
    return text;
  }
  this.txtAreaChange=function() {
    var txtArea=getObject(this.txtAreaID);
    var theBtn=getObject(this.btnName);
    if (txtArea && theBtn && txtArea.value.length>0) 
      theBtn.disabled=false;
    else theBtn.disabled=true;
  }  
  this.AddCoord=function(x,y) {
    var txtArea=getObject(this.txtAreaID);
    if (txtArea) {
      txtArea.value+=formatFloat(x,nk,false)+" "+formatFloat(y,nk,false)+"\n";
      //txtArea.value+=x+" "+y+"\n";
    }
    //this.txtAreaChange();
  }
  this.AddText=function(txt) {
    var txtArea=getObject(this.txtAreaID);
    if (txtArea) {
      txtArea.value+=txt+"\n";
    }    
  }
  this.clear=function() {
    var txtArea=getObject(this.txtAreaID);
    if (txtArea) {
      txtArea.value="";
    }
  }
  
}
function checkText(aObj, btnName) {
  var theBtn=getObject(btnName);
  if (theBtn) {
    if (aObj.value.length>0) {
      theBtn.disabled=false;
      return true;
    }
    else {
      theBtn.disabled=true;
      return false;
    }
  }
}
function TxtCopy(txtElement) {
  var txtArea=getObject(txtElement);
  if (txtArea) {
    txtArea.focus();
    txtArea.select();
    ablage = txtArea.createTextRange();
    ablage.execCommand("RemoveFormat");
    ablage.execCommand("Copy");
  }
}
function drawGeo(txtElement) {  
  var txtArea=getObject(txtElement);
  //if (checkText(txtArea,"btnDraw")) {
    if (txtArea) {
      var txt=txtArea.value;
      var txtList=txt.split("\n");
      ECWVectorLayer.clear();
      //tagName="polyline1";
      tagName="polygon1";
      var vecObject = "linewidth=" + ECWVectorLayer.lineThickness + ";color=" + ECWVectorLayer.penColor + ";fillcolor=" + ECWVectorLayer.fillColor + ";"+tagName+"=";
      //var geoExtent=new GeoExtent;
      ECWVectorLayer.polyExtent=new GeoExtent();
      ECWVectorLayer.AreaPointsX= new Array();
      ECWVectorLayer.AreaPointsY= new Array();
      for (var i=0;i<txtList.length;i++) {
        txtList[i]=txtList[i].replace(/,/g,".");
        var XYCoord=txtList[i].split(" ");
        if (XYCoord.length==2) {
          var x=parseFloat(XYCoord[0]);
          var y=parseFloat(XYCoord[1]);
          vecObject += x + "," + y + (i==txtList.length? "" : "|");
          ECWVectorLayer.polyExtent.AddCoord(x,y);
          ECWVectorLayer.AreaPointsX.push(x);
          ECWVectorLayer.AreaPointsY.push(y);
        }
      }
      ECWVectorLayer.objectNumPoints=ECWVectorLayer.AreaPointsX.length;
      var containt=ECWVectorLayer.polyExtent.contain(ECWVectorLayer.ncsView);
      if (containt==GeoExtent.ContainedInView || containt==GeoExtent.ContainedInMap) {
        vecObject += ";";
        ECWVectorLayer.ncsView.SetLayerParameter(ECWVectorLayer.layerName,vecObject);
        if (containt==GeoExtent.ContainedInMap) {
          var x=ECWVectorLayer.polyExtent.xmin+(ECWVectorLayer.polyExtent.xmax-ECWVectorLayer.polyExtent.xmin)/2;
          var y=ECWVectorLayer.polyExtent.ymin+(ECWVectorLayer.polyExtent.ymax-ECWVectorLayer.polyExtent.ymin)/2
        }
        delta=(ECWVectorLayer.polyExtent.xmax-ECWVectorLayer.polyExtent.xmin)/10;
        setExtent(ECWVectorLayer.polyExtent.asString(",",delta));
      }
      else alert("Geometrie befindet sich nicht innerhalb dieser Karte");
    }
  //}
}
function closeCapCoord(objID) {
  hideLayer(objID);
  showLayer("Ort");
  theStatus.StatusScale.focus();
}


// please keep these lines on when you copy the source
// made by: Nicolas - http://www.javascript-page.com

var XX = -70;
var YY = -70;
var cur_one = "emptycell"
var moving = false;
var xpos1 = 20;
var ypos1 = 20;
var myX = 0;
var myY = 0;

function InitializeMove() {
  windowSetEvents();
  cur_one = "XY";
  XX = eval("xpos1");
  YY = eval("ypos1");
}

function CaptureMove() {
  if (document.layers) document.captureEvents(Event.MOUSEMOVE);
}
function EndMove() {
  if (document.layers) document.releaseEvents(Event.MOUSEMOVE);
  cur_one = "emptycell"
  moving = false;
  document.close();
}

function WhileMove() {
  if (document.all) {
    eval(cur_one+".style.left="+myX);
    eval(cur_one+".style.top="+myY);
  }
  if (document.layers) {
    eval("document."+cur_one+".left="+myX);
    eval("document."+cur_one+".top="+myY);
  }
}

function MoveHandler(e) {
  myX = (document.all) ? event.clientX : e.pageX;
  myY = (document.all) ? event.clientY : e.pageY;
  if (!moving) {
    diffX =  XX - myX;
    diffY = YY - myY;
    moving = true;
    if (cur_one == "emptycell") moving = false;
  }
  myX += diffX;
  myY += diffY;
  if (moving) {
    xpos1 = myX;
    ypos1 = myY;
  }
  WhileMove();
}

function ClearError() {
  return true;
}
function windowSetEvents() {
  if (document.layers) {
    document.captureEvents(Event.CLICK);
    document.captureEvents(Event.DBLCLICK);
  }
  document.onmousemove = MoveHandler;
  document.onmousedown = CaptureMove;
  document.onmouseup = EndMove;
  window.onerror = ClearError;
  WhileMove();
}
