/**
* Print an image using the ImageX.dll extension.
* The logic is to determine the approximate page size in inches, then set up a table with each cell measured in page inches,
* generate each table cell as an image from ecw_image.dll extension of the webserver as a JPG, embed in the tables and then
* print it. Since we dont know when setting up the page what the output page size is, we ask the user. We could use the DPI to
* work out the sizex and sizey parameters of the ecw_image.dll for the optimal size of each tile.
*/ 
function getFirstActiveLayerName(NCSView) {
  var cnt=NCSView.GetNumberLayers();
  var result=null;
  for (var i=(cnt-1);i>=0;i--) {
    var sLayer=NCSView.GetLayerName(i);
    if (NCSView.GetLayerType(sLayer).toUpperCase()=="ECW") {
      var isshow=NCSView.GetLayerParameter(sLayer,"visibility").toUpperCase();
      if (isshow=="TRUE") {
        result=sLayer;
        break;
      }
    }
  }
  return result;
}
function getLayersFileName(NCSView, isAbsolute) {
  var result="";
  var sLayer=getFirstActiveLayerName(NCSView);
  if (sLayer) {
    if (isAbsolute)
      result+=(result!=""?",":"")+NCSView.GetLayerFileName(sLayer);
    else result+=(result!=""?",":"")+MakeRelativePath(NCSView.GetLayerFileName(sLayer)).replace(/ /g,"%20");
  }
  return result;
}
function getWatermarke() {
  var orthoCheck=getObject("orthoCheck");
  var dgmCheck=getObject("dgmCheck");
  var result=null;
  if (!dgmCheck && !orthoCheck) return result;
  if (dgmCheck && dgmCheck.checked) {
    result=new Array();
    result["WIMG"]=dgmArray[lastDgm][1];
    result["WPOS"]=dgmArray[lastDgm][2];
  }    
  else if (orthoCheck && orthoCheck.checked) {
    result=new Array();
    result["WIMG"]=orthoArray[lastOrtho][1];
    result["WPOS"]=orthoArray[lastOrtho][2];  
  }
  return result;
}
function ECWPrintImage(NCSView) {
  if (!(clientID>0)) {
    alert("Sie sind zum Exportieren nicht berechtigt.");
    return false;
  }
  //if (NCSToolbar.StrgDown || !isIE) {
    // Get the layer name of the 1st layer, this should be a parameter.
    //var sLayer = NCSView.GetLayerName(0);

    // Get the layer file name.
    var sLayerFileName = getLayersFileName(NCSView);//NCSView.GetLayerFileName(sLayer);

    // When using the java plugin, we need to convert the returned JavaScript wrapper to a JavaScript string by appending
    // an empty string to it.
    if(bUseJavaPlugin) {
      sLayerFileName += "";
    }
    var w=NCSView.GetViewWidth();
    var h=NCSView.GetViewHeight();  
    var pgWidth=2970;
    //var pgHeight=2100;
    var pgHeight=h*pgWidth/w;
    document.frmexp.execute.value="http://" + document.location.host + "/ecwp/ImageX.dll?image?";
    document.frmexp.ecws.value=sLayerFileName;
    document.frmexp.worldtlx.value=document.ECWView1.GetTopLeftWorldCoordinateX();
    document.frmexp.worldtly.value=document.ECWView1.GetTopLeftWorldCoordinateY();
    document.frmexp.worldbrx.value=document.ECWView1.GetBottomRightWorldCoordinateX();
    document.frmexp.worldbry.value=document.ECWView1.GetBottomRightWorldCoordinateY();
    document.frmexp.sizex.value=pgWidth;
    document.frmexp.sizey.value=pgHeight;
    document.frmexp.fillcolor.value="0x00FFFFFF";
    document.frmexp.quality.value=100;
    document.frmexp.type.value="jpeg";
    document.frmexp.output.value="print";
    document.frmexp.client.value=client;
    document.frmexp.srv.value=clientID;
    document.frmexp.anr.value=aNr;
    var wmarke=getWatermarke();
    if (wmarke) {
      document.frmexp.wimg.value=wmarke["WIMG"];
      document.frmexp.wpos.value=wmarke["WPOS"];    
    }    
    document.frmexp.lpos.value=logoPos;
    document.frmexp.file.value="file.pdf";
    document.frmexp.submit();    
    NCSToolbar.StrgDown=false;
}
function ECWExportImage(NCSView) {
  if (!(clientID>0)) {
    alert("Sie sind zum Exportieren nicht berechtigt.");
    return false;
  }
  //if (NCSToolbar.StrgDown || !isIE) {
    // Get the layer name of the 1st layer, this should be a parameter.
    //var sLayer = NCSView.GetLayerName(0);

    // Get the layer file name.
    var sLayerFileName = getLayersFileName(NCSView);//NCSView.GetLayerFileName(sLayer);

    // When using the java plugin, we need to convert the returned JavaScript wrapper to a JavaScript string by appending
    // an empty string to it.
    if(bUseJavaPlugin) {
      sLayerFileName += "";
    }
    var w=NCSView.GetViewWidth();
    var h=NCSView.GetViewHeight();  
    var pgWidth=2970;
    //var pgHeight=2100;
    var pgHeight=h*pgWidth/w;
    document.frmexp.execute.value="http://" + document.location.host + "/ecwp/ImageX.dll?image?";
    document.frmexp.ecws.value=sLayerFileName;
    document.frmexp.worldtlx.value=document.ECWView1.GetTopLeftWorldCoordinateX();
    document.frmexp.worldtly.value=document.ECWView1.GetTopLeftWorldCoordinateY();
    document.frmexp.worldbrx.value=document.ECWView1.GetBottomRightWorldCoordinateX();
    document.frmexp.worldbry.value=document.ECWView1.GetBottomRightWorldCoordinateY();
    document.frmexp.sizex.value=pgWidth;
    document.frmexp.sizey.value=pgHeight;
    document.frmexp.fillcolor.value="0x00FFFFFF";
    document.frmexp.quality.value=100;
    document.frmexp.type.value="jpeg";
    document.frmexp.output.value="export";
    document.frmexp.client.value=client;
    document.frmexp.srv.value=clientID;
    document.frmexp.anr.value=aNr;
    var wmarke=getWatermarke();
    if (wmarke) {
      document.frmexp.wimg.value=wmarke["WIMG"];
      document.frmexp.wpos.value=wmarke["WPOS"];    
    }
    document.frmexp.lpos.value=logoPos;    
    document.frmexp.file.value="file.zip";    
    document.frmexp.submit();    
    NCSToolbar.StrgDown=false;
}
function ECWPrintURL() {
  //alert("ECWPrintURL");
  var NCSView=document.ECWView1;
  if (!(clientID>0)) {
    alert("Sie sind nicht berechtigt zu Drucken.");
    return false;
  }

  // Get the layer file name.
  var sLayerFileName = getLayersFileName(NCSView);//NCSView.GetLayerFileName(sLayer);

  // When using the java plugin, we need to convert the returned JavaScript wrapper to a JavaScript string by appending
  // an empty string to it.
  if(bUseJavaPlugin) {
    sLayerFileName += "";
  }
  var w=NCSView.GetViewWidth();
  var h=NCSView.GetViewHeight();  
  var pgWidth=2970;
  //var pgHeight=2100;
  var pgHeight=h*pgWidth/w;
  var wmarke=getWatermarke();
  var wmarkeparam="";
  if (wmarke) {
    wmarkeparam="&WIMG="+wmarke["WIMG"]+"&WPOS="+wmarke["WPOS"];
  }
  var aURL="printexport.php?"+
           "execute=http://" + document.location.host + "/ecwp/ImageX.dll?image?"+
           "&ecws="+sLayerFileName+
           "&worldtlx="+document.ECWView1.GetTopLeftWorldCoordinateX()+
           "&worldtly="+document.ECWView1.GetTopLeftWorldCoordinateY()+
           "&worldbrx="+document.ECWView1.GetBottomRightWorldCoordinateX()+
           "&worldbry="+document.ECWView1.GetBottomRightWorldCoordinateY()+
           "&sizex="+pgWidth+
           "&sizey="+pgHeight+
           "&fillcolor=0x00FFFFFF"+
           "&quality=100"+
           "&type=jpeg"+
           "&output=print"+
           "&client="+client+
           "&srv="+clientID+   
           "&anr="+aNr+
           wmarkeparam+ 
           "&lpos="+logoPos+        
           "&file=file.pdf";
  var aLink=getObject("priLink");
  if (aLink)
    aLink.href=aURL;
}
function ECWExportURL(isGeoRef) {
  var NCSView=document.ECWView1;
  if (!(clientID>0)) {
    alert("Sie sind zum Exportieren nicht berechtigt.");
    return false;
  }

  // Get the layer file name.
  var sLayerFileName = getLayersFileName(NCSView);//NCSView.GetLayerFileName(sLayer);

  // When using the java plugin, we need to convert the returned JavaScript wrapper to a JavaScript string by appending
  // an empty string to it.
  if(bUseJavaPlugin) {
    sLayerFileName += "";
  }
  var w=NCSView.GetViewWidth();
  var h=NCSView.GetViewHeight();  
  var pgWidth=2970;
  //var pgHeight=2100;
  var pgHeight=h*pgWidth/w;
  var wmarke=getWatermarke();
  var wmarkeparam="";
  if (wmarke) {
    wmarkeparam="&WIMG="+wmarke["WIMG"]+"&WPOS="+wmarke["WPOS"];
  }  
  var aURL="printexport.php?"+
           "execute=http://" + document.location.host + "/ecwp/ImageX.dll?image?"+
           "&ecws="+sLayerFileName+  
           "&worldtlx="+document.ECWView1.GetTopLeftWorldCoordinateX()+
           "&worldtly="+document.ECWView1.GetTopLeftWorldCoordinateY()+
           "&worldbrx="+document.ECWView1.GetBottomRightWorldCoordinateX()+
           "&worldbry="+document.ECWView1.GetBottomRightWorldCoordinateY()+
           "&sizex="+pgWidth+
           "&sizey="+pgHeight+
           "&fillcolor=0x00FFFFFF"+
           "&quality=100"+
           "&type=jpeg"+
           "&output=export"+
           "&client="+client+
           "&srv="+clientID+
           "&anr="+aNr+
           wmarkeparam+
           "&lpos="+logoPos+
           (isGeoRef?"&georef=true":"")+
           "&file=export.zip";
  var aLink=getObject(isGeoRef?"expGeoRefLink":"expLink");
  if (aLink)
    aLink.href=aURL;   
}
function MakeRelativePath(sUrl){
  var slashLoc = -1;
  // Find the third occurrence of '/' in the url.
  for(i = 0; i < 3; i++){
    slashLoc = sUrl.indexOf("/", slashLoc + 1);
  }
  // Return the relative path.
  return sUrl.substr(slashLoc, sUrl.length - slashLoc);
}
function setURLParams(alink,NCSView) {
  if (alink.baseLink) alink.href=alink.baseLink
  else alink.baseLink=alink.href;
  var aURL=alink.href;
  alert(aURL);
  var sLayerFileName = getLayersFileName(NCSView,true);
  //var comment=getOrthoComment(NCSView);
  var orthoCheck=getObject("orthoCheck");
  var dgmCheck=getObject("dgmCheck");
  //if (!dgmCheck && !orthoCheck) return false; 
  if (dgmCheck && dgmCheck.checked) {
    var comment=dgmArray[lastDgm][0];
    var title=dgmArray[lastDgm][3];
  }
  else if (orthoCheck && orthoCheck.checked) {
    var comment=orthoArray[lastOrtho][0];
    var title=orthoArray[lastOrtho][3];
  }   
     
  // When using the java plugin, we need to convert the returned JavaScript wrapper to a JavaScript string by appending
  // an empty string to it.  
  if(bUseJavaPlugin) {
    sLayerFileName += "";
  }
  var w=NCSView.GetViewWidth();
  var h=NCSView.GetViewHeight();    
  var pgWidth=150;//198;
  var pgHeight=h*pgWidth/w;
  var xmin = NCSView.GetTopLeftWorldCoordinateX();
  var ymin = NCSView.GetBottomRightWorldCoordinateY();
  var xmax = NCSView.GetBottomRightWorldCoordinateX();
  var ymax = NCSView.GetTopLeftWorldCoordinateY();  
  var thePoints="";
  var thePointsPX="";
  if (ECWVectorLayer) {
    if (ECWVectorLayer.AreaPointsX.length>2) {
      thePoints=ECWVectorLayer.PointsAsString(";");
      for (var i=0; i<ECWVectorLayer.AreaPointsX.length;i++) {
        //thePoints+=(thePoints==""?"":";")+ECWVectorLayer.AreaPointsX[i]+";"+ECWVectorLayer.AreaPointsY[i];
        var pxP=ECWVectorLayer.WordCoord2Pixel(ECWVectorLayer.AreaPointsX[i],ECWVectorLayer.AreaPointsY[i],pgWidth,pgHeight,xmin,ymin,xmax,ymax);
        thePointsPX+=(thePointsPX==""?"":";")+pxP[0]+";"+pxP[1];
      }
    }
    else {
      alert("Zeichnen Sie ein Polygon");
      return;
    }
  }
  var ort=getSelectValue("orte",1,"");
  var plz=getSelectValue("plz",1,"");
  var str=getSelectValue("str",0,"");
  var extentPX= ECWVectorLayer.getPixelExtent(pgWidth,pgHeight,xmin,ymin,xmax,ymax);
  var jetzt = new Date();
  var monat=jetzt.getMonth()+1;
  monat=(monat<10?"0":"")+monat;  
  var tag=jetzt.getDate();
  tag=(tag<10?"0":"")+tag;
  var stunden=jetzt.getHours();
  stunden=(stunden<10?"0":"")+stunden;
  var minuten=jetzt.getMinutes();
  minuten=(minuten<10?"0":"")+minuten;
  var segunden=jetzt.getSeconds();
  segunden=(segunden<10?"0":"")+segunden;
  //var orderNr=jetzt.getFullYear()+monat+tag+stunden+minuten+segunden; 
  var orderNr=stunden+minuten+segunden;
  var datum=tag+"."+monat+"."+jetzt.getFullYear();
  
  aURL+="&execute=http://" + document.location.host + "/ecwp/ImageX.dll?image?";
  aURL+="&LAYERS="+MakeRelativePath(sLayerFileName);
  aURL+="&worldtlx="+NCSView.GetTopLeftWorldCoordinateX();
  aURL+="&worldtly="+NCSView.GetTopLeftWorldCoordinateY();
  aURL+="&worldbrx="+NCSView.GetBottomRightWorldCoordinateX();
  aURL+="&worldbry="+NCSView.GetBottomRightWorldCoordinateY();
  aURL+="&client="+client;
  aURL+="&sizex="+pgWidth;
  aURL+="&sizey="+pgHeight;
  aURL+="&fillcolor=0x00FFFFFF";
  aURL+="&quality=100";
  aURL+="&type=png";
  aURL+="&points="+thePoints;
  aURL+="&bb="+ECWVectorLayer.getExtent();
  aURL+="&pointsPX="+thePointsPX;
  aURL+="&extentPX="+extentPX;
  aURL+="&polyArea="+ECWVectorLayer.getAbsArea();
  aURL+="&rectArea="+ECWVectorLayer.getPolyExtentArea();
  aURL+="&orderNr="+orderNr;
  aURL+="&datum="+datum;
  aURL+="&frmtyp=ORDER";
  aURL+="&aNr="+aNr;
  aURL+="&ORT="+escape(ort);
  aURL+="&PLZ="+plz;
  aURL+="&STR="+escape(str);
  aURL+="&kommentar="+escape(comment);  
  aURL+="&title="+title;
  aURL+="&HNR="+globalHNR;
  alink.href=aURL;
  alert(alink.href);
}
