//---------------------------------------------------
// Copyright (c) 2005 Basic-Software-Beratungs GmbH
// --------------------------------------------------

var show = true;
var plan = false;

var X_POS =0;
var Y_POS = 0;
var down= new Array();         // Koordinaten Klartext
var X_koord = new Array();
var up= new Array();
var Y_koord= new Array();

function planung() {

   if(document.cb.cbk_plan.checked == true) {
          plan= true;
          Event_init_toern();
       //   getElem("id","coord",0).style.visibility="visible";
   }
   else { Event_off_toern();
          plan = false;
    //      getElem("id","coord",0).style.visibility="hidden";
   }
}


// -----------------------------------------------------------------------
function fehler(msg,url,line) {
 alert("Fehler in : "+url+"\n"+"Zeile   "+line+"\n"+"Text : "+msg);
}
onerror=fehler;

// -----------------------------------------------------------------------
function position() {

   if(document.cb.cbk_pos.checked == true) {
          show= true;
          Event_init();
          getElem("id","coord",0).style.visibility="visible";
   }
   else { Event_off();
          show = false;
          getElem("id","coord",0).style.visibility="hidden";
   }
}


xdiff = dezigrad(lng1g,lng1m,lng1s) - dezigrad(lng2g,lng2m,lng2s);
xfakt = xdiff / (px1 - px2);

ydiff = dezigrad(br2g,br2m,br2s) - dezigrad(br1g,br1m,br1s);
var yfakt = ydiff /(py2 - py1);
var xorg  = dezigrad(lng1g,lng1m,lng1s)  - xfakt * px1;
var yorg  = dezigrad(br1g,br1m,br1s) - yfakt * py1;



function dezigrad(g, m , s) {
 grad = g + m / 60 + s / 10000;
 return grad;
}

function dGradToStr(grad,ne) {
 g  = parseInt(grad);
 if (g > 100) gstr=g+"° ";
 else  if(ne == "N"){
     if(g < 10  ) gstr = " 0"+g+"° ";
     else         gstr = "  "+g+ "° ";
     }
 else { if(g > 9  ) gstr = "0"+g+"° ";
        else gstr="00" + g + "° ";   }
 r  = (grad - g) * 60 ;
 m = parseInt(r);
 if (m < 10) mstr="0"+m+'.';
 else       mstr = m+'.';
 s = parseInt((r - m) * 100) ;
 if (s< 10) sstr="0" + s+"'";
 else       sstr= s+"'";
 str = gstr +  mstr + sstr +" "+ne+" ";
 return str;
}

// berechnet die Laenge  im Moment nur oestliche Laenge
// -------------------------------------------------------
function calc_x(X) {
  if(show == true) {
    xp = xorg + xfakt * X;           //(X + winXoffs);
    alert("test="+test+"  "+X)
    txtLn = dGradToStr(xp,"E");        //   "  "++(X + winXoffs)+
    if(test == true) { ; document.xy.x.value=X+" "+xp;   }
    document.p.ln.value = txtLn;
  }
}

// berechnet die Breite  im Moment nur noerdliche Breite
// -------------------------------------------------------
function calc_y(Y) {
  if(show == true) {
    yp=  yorg + yfakt * Y;         //(Y + winYoffs)
    txtBr= dGradToStr(yp,"N");                // +(Y + winYoffs)+
    if(test == true)  document.xy.y.value=Y+" "+yp;
    document.p.br.value = txtBr;
  }
}

function handleMove(e) {
   /* MSIE, Konqueror, Opera : */

 if (document.all){           // IE   if (window.event)
    X_POS = window.event.clientX ;
    if (document.body.scrollLeft)   X+=document.body.scrollLeft;

    Y_POS = window.event.clientY ;
    if (document.body.scrollTop)    Y+=document.body.scrollTop
 }
 else {      // scrollwerte sind bei Mozilla, Netscape bereits dabei
    X_POS = e.pageX;
    Y_POS = e.pageY;
 }
 if( (X_POS < 1000) && (Y_POS < 800) ){
   calc_x(X_POS);
   calc_y(Y_POS);
 }

}

function handleDown(e) {

  down[down.length]= document.p.br.value;
  up[down.length] = document.p.ln.value;
  X_koord[X_koord.length]=parseInt(X_POS);
  Y_koord[Y_koord.length]=parseInt(Y_POS);
 // if(Y_koord.length > 4) drawKurs();
}

function handleUp(e) {

  down[down.length]= document.p.br.value;
  up[down.length] = document.p.ln.value;
  X_koord[X_koord.length]=parseInt(X_POS);
  Y_koord[Y_koord.length]=parseInt(Y_POS);
//  if(Y_koord.length > 2) drawKurs();

}


function Event_init_toern() {
 /* Netscape6, Mozilla :
 if (typeof(document.addEventListener)=="function")
      document.addEventListener("mousemove",MouseMove,true);  */
 if(show == false) {
   Event_init();
 }

 if(DOM && !MS && !OP) {  /* Netscape6, Mozilla : */
  getElem("tagname","body",0).addEventListener("mousedown", handleDown, true);
   getElem("tagname","body",0).addEventListener("mouseup", handleUp, true);

  plan=true;
 }
 if(NS) {     /* Netscape4 : */
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=handleDown;
  document.captureEvents(Event.MOUSEUP);
  document.onmousedown=handleUp;
  plan=true;
 }
 /* MSIE, Konqueror, Opera :
 if (document.all)    window.onmousemove=MouseMove;     */
 if (DOM && OP) {
    document.onmousedown=handleDown;
    document.onmousedown=handleUp;
    plan=true;

 }
 if (MS) getElem("tagname","body",0).onmousemove=handleDown;
}
function Event_off_toern() {

 if(DOM && !MS && !OP) {
  getElem("tagname","body",0).removeEventListener("mousemove", handleDown, false);
 }
 if(NS) {
  document.releaseEvents(Event.MOUSEDOWN);
  document.onmousedown=null;
  document.releaseEvents(Event.MOUSEUP);
  document.onmouseup=null;
 }
 if (DOM && OP) {
    document.onmousedown=null;
    document.onmouseup=null;
 }
 if (MS) getElem("tagname","body",0).onmousedown=null;

 document.p.br.value = "";
 document.p.ln.value = "";
 plan=false;
}
// ---------------------------------------------------------------------------------
// Mousehandling für Positionsbestimmung
// ---------------------------------------------------------------------------------
function Event_init() {
 /* Netscape6, Mozilla :
 if (typeof(document.addEventListener)=="function")
      document.addEventListener("mousemove",MouseMove,true);  */

 if(DOM && !MS && !OP) {  /* Netscape6, Mozilla : */
  getElem("tagname","body",0).addEventListener("mousemove", handleMove, true);
 }
 if(NS) {     /* Netscape4 : */
  document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove=handleMove;
 }
 /* MSIE, Konqueror, Opera :
 if (document.all)    window.onmousemove=MouseMove;     */
 if (DOM && OP) {
    document.onmousemove=handleMove;
 show=true;

 }
 if (MS) getElem("tagname","body",0).onmousemove=handleMove;
}
function Event_off() {

 if(DOM && !MS && !OP) {
  getElem("tagname","body",0).removeEventListener("mousemove", handleMove, false);
 }
 if(NS) {
  document.releaseEvents(Event.MOUSEMOVE);
  document.onmousemove=null;
 }
 if (DOM && OP) {
    document.onmousemove=null;


 }
 if (MS) getElem("tagname","body",0).onmousemove=null;

  document.p.br.value = "";
  document.p.ln.value = "";
  show=false;
}
// -----------------------------------------------------------

/*

function myDrawFunction()
{
*/
/*
  jg_doc.setColor("#00ff00"); // green
  jg_doc.fillEllipse(200, 200, 250, 280); // co-ordinates related to the document
  jg_doc.setColor("maroon");
  jg_doc.drawPolyline(new Array(250, 110, 370, 350, 40), new Array(190, 150, 170, 320, 500));
  jg_doc.paint(); // draws, in this case, directly into the document
  */
 /*
  jg.setColor("#ff0000");        // red
  jg.drawLine(10, 113, 220, 55); // co-ordinates related to "myCanvas"
  jg.setColor("#0000ff");        // blue
  jg.fillRect(110, 120, 30, 60);
  jg.setColor("maroon");
  jg.drawPolyline(new Array(50, 10, 120, 370, 350, 40), new Array(10, 50, 70,170, 320, 500));
  jg.paint();

  */
/*
  jg.setColor("#0000ff"); // blue
  jg.drawEllipse(10, 50, 30, 100);
  jg.drawRect(400, 10, 100, 50);
  jg.paint();
  */
/*
}

// setStroke(Number);               Specifies the thickness of the drawing "pen" for lines
//                                  and bounding lines of shapes. Once set, this thickness
//                                  will be used by the subsequently called drawing methods
//                                  until it will be overridden through another call of
//                                 setStroke(). Default line thickness is 1 px, as long as
//                                 b.setStroke() has not yet been invoked.

// To create dotted lines, setStroke() requires the constant Stroke.DOTTED as argument.
// Width of dotted lines is always 1 pixel.
// drawLine(X1, Y1, X2, Y2);
// drawPolyline(Xpoints, Ypoints);    A polyline is a series of connected line segments.
//                                    Xpoints and Ypoints are arrays which specify the x
//                                    and y coordinates of each point as follows:

function drawKurs() {
   alert("X_koord="+X_koord); alert("Y_koord="+Y_koord);
   alert(down);
   alert(up);
   jg.setColor("#0000ff"); // blue
   alert("jg"+jg);
   jg.drawPolyline(X_koord, Y_koord);
   jg.paint(); // draws, in this case, directly into the document
}

 var jg_doc = new jsGraphics(); // draw directly into document
var jg = new jsGraphics("myCanvas");
// var jg2 = new jsGraphics("anotherCanvas");

myDrawFunction();

*/
