var c_el = new Array();
function change(id)
{
  c_el[id] = getE(id);
  if (c_el[id] != null && c_el[id] != '')
  {
    if(c_el[id].style.display == "none" || c_el[id].style.display == "") //|| c_el[id].style.display == ""
      c_el[id].style.display = "block"; //block nicht, damit es in tabellen geht!
    else
      c_el[id].style.display = "none";
  }
}

function show(id)
{
  c_el[id] = getE(id);
  if (c_el[id] != null && c_el[id] != '')
    c_el[id].style.display = "block"; //block nicht, damit es in tabellen geht!
}
function hide(id)
{
  c_el[id] = getE(id);
  if (c_el[id] != null && c_el[id] != '')
    c_el[id].style.display = "none";
}

function randomNr(){ return String((new Date()).getTime()).replace(/\D/gi,'') }

function popup(loc, width, height, scroll)
{
  if(width != null && height != null && scroll != null)
  {  
    lft=(screen.availWidth)?(screen.availWidth-width)/2:100;
    tp=(screen.availHeight)?(screen.availHeight-height)/2:100;
    
    p = window.open(loc,'popup','toolbar=no,width='+width+',height='+height+',screenX='+lft+',screenY='+tp+',left='+lft+',top='+tp+',scrollbars='+scroll+',resizable=yes,status=no,menubar=no,directories=no,location=no,dependent=no');
    p.focus();
  }
}


function htmlPopup(imageloc, width, height, scroll)
{
  if(width != null && height != null && scroll != null)
  {  
    lft=(screen.availWidth)?(screen.availWidth-width)/2:100;
    tp=(screen.availHeight)?(screen.availHeight-height)/2:100;
    HTML = "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n<html><head><meta http-equiv='content-type' content='text/html; charset=UTF-8'><style>body, html{margin:0px;padding:0px;background-color:#FFFFFF;}</style></head><body><img src='"+ imageloc +"'/><br/></body></html>";
 

    if(typeof(popupImage) != "undefined" && popupImage != null)
      popupImage.close();
  

    randomnumber=1;//randomNr()
    popupImage = window.open("",'htmlpopup'+randomnumber,'width='+width+',height='+height+',screenX='+lft+',screenY='+tp+',left='+lft+',top='+tp+',toolbar=no,scrollbars='+scroll+',resizable=yes,status=no,menubar=no,directories=no,location=no,dependent=no');
    popupImage.document.open();
    popupImage.document.write(HTML);
    popupImage.document.close();
    popupImage.focus();
  }
}





//Text-Counter JS
function textCounter(field, cntfield, maxlimit) 
{
  if(field != null && cntfield != null)
  {
  if (field.value.length > maxlimit) 
    field.value = field.value.substring(0, maxlimit);
  else 
    cntfield.innerHTML = maxlimit - field.value.length;
  }
}


function toggler(showid, elementstr)
{
  for (i = 0; i < 200; i++)
  { 
    if(getE(elementstr+i))
    {
      if(elementstr+i == elementstr+showid)
      { 
        if(getE(elementstr+i).style.display == "block")
          getE(elementstr+i).style.display = "none";
        else
          getE(elementstr+i).style.display = "block";
      } 
      else
        getE(elementstr+i).style.display = "none";
    }
  }
}

//Usage: <a href="javascript:void(0)" onClick="boldize(this);">bold</a>
function boldize(obj)
{
	if(obj.style.fontWeight == '' || obj.style.fontWeight == 'normal' )
	  obj.style.fontWeight = "bold";
  else
	  obj.style.fontWeight = "normal";
}

//TOOL: leading zeros
function LZ(x) {return(x<0||x>9?"":"0")+x} 

//TOOL: getElementById
function getE(myel_id){if(document.getElementById(myel_id) != null && document.getElementById(myel_id) != '' && document.getElementById(myel_id) != 'undefined') return document.getElementById(myel_id);}


//Content des Popups darf kein <body> <html> etc. enthalten!
function divPopup(loc, width, height, scroll, div_html_id)
{
  if(width != null && height != null)
  {  
    lft=(screen.availWidth)?(screen.availWidth-width)/2.5:100;
    tp=(screen.availHeight)?(screen.availHeight-height)/2.5:100;

    if(scroll == "yes") sc = "overflow-y:scroll;overflow-x:hidden";
    else sc = "overflow:hidden";

$.ajax({
 type: "POST", dataType: "html", url: loc,
 success: function(htmlcode)
 {
   if($("#"+div_html_id).html() != null)
     $("#"+div_html_id).show();
   else
   {
     
     $(document).ready(function(){
     $('body').append("<div id="+div_html_id+" style='z-index:900000;width:"+width+"px;height:"+height+"px;"+sc+";background:white;position:absolute;top:"+parseInt(tp)+"px;left:"+parseInt(lft)+"px;' class='popup-div'><div class='popup-div-close'><a href='javascript:;' onClick='$(\"#"+div_html_id+"\").fadeOut();'><b>x</b></a></div><div class='popup-div-inner'>"+htmlcode+"</div></div>");
     });
     
   }                 
 },
 error: function(msg) { alert("Error: " + msg); }
});

  }

}



var arrlft = new Array();
var arrtp = new Array();
function divImagePopup(imgsrc, width, height, scroll, klasse)
{
  if(width != null && height != null)
  {  
     div_html_id = "dip"+imgsrc;
     div_html_id = div_html_id.replace(/[^a-zA-Z0-9]/g, "");

    if(arrlft[div_html_id] == null)
      lft=(screen.availWidth)?(screen.availWidth-width)/2.5:100;
    else
      lft=arrlft[div_html_id];
    
    if(arrtp[div_html_id] == null)
      tp=(screen.availHeight)?(screen.availHeight-height)/2.5:100;
    else
      tp=arrtp[div_html_id];

    if(scroll == "yes") sc = "overflow-y:scroll;overflow-x:hidden";
    else sc = "overflow:hidden";

    arrlft[div_html_id] = lft;
    arrtp[div_html_id] = tp;
     
    div_html_id2 = "dip2"+randomNr();
    closex = "javascript:$(\"#"+div_html_id+"\").fadeOut(300, function() {$(\"#"+div_html_id+"\").remove();} );";
	if($('#'+div_html_id).html() == null)
	{
		$('body').append("<div id='"+div_html_id+"'>");
		$("#"+div_html_id).attr("style", "display:none;z-index:900000;"+sc+";background:#AC003B;position:absolute;top:"+parseInt(tp)+"px;left:"+parseInt(lft)+"px;text-align:right");
		$("#"+div_html_id).attr("class", "popup-image-div");
		$("#"+div_html_id).html("<span onClick='"+closex+"' style='cursor:pointer !important'><b style='color:white;font-size:15px;padding-bottom:2px;padding-right:5px;'>&times;</b></span>");
		$("#"+div_html_id).append("<div id='"+div_html_id2+"'>");
		$("#"+div_html_id2).attr("style", "cursor:pointer !important;width:"+width+"px;height:"+height+"px;");
		$("#"+div_html_id2).html("<img onClick='"+closex+"' class='"+klasse+"' src='"+imgsrc+"'>");
		$("#"+div_html_id).easydrag();
	
	
		//MERK DIR LETZTE POS (CA.) BEI ON DROP
		$("#"+div_html_id).ondrop(function(e, element){ 
		arrlft[div_html_id] = $("#"+div_html_id).x() ;
		arrtp[div_html_id] = $("#"+div_html_id).y() ;
		});
	}

  $("#"+div_html_id).fadeIn(300);
  }

}



/**
* EasyDrag 1.4 - Drag & Drop jQuery Plug-in
*
* Thanks for the community that is helping the improvement
* of this little piece of code.
*
* For usage instructions please visit http://fromvega.com
*/

(function($){

  // to track if the mouse button is pressed
  var isMouseDown    = false;

  // to track the current element being dragged
  var currentElement = null;

  // callback holders
  var dropCallbacks = {};
  var dragCallbacks = {};

  // global position records
  var lastMouseX;
  var lastMouseY;
  var lastElemTop;
  var lastElemLeft;

  // track element dragStatus
  var dragStatus = {};

  // returns the mouse (cursor) current position
  $.getMousePosition = function(e){
    var posx = 0;
    var posy = 0;

    if (!e) var e = window.event;

    if (e.pageX || e.pageY) {
      posx = e.pageX;
      posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
      posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
    }

    return { 'x': posx, 'y': posy };
  };

  // updates the position of the current element being dragged
  $.updatePosition = function(e) {
    var pos = $.getMousePosition(e);

    var spanX = (pos.x - lastMouseX);
    var spanY = (pos.y - lastMouseY);

    $(currentElement).css("top",  (lastElemTop + spanY));
    $(currentElement).css("left", (lastElemLeft + spanX));
  };

  // when the mouse is moved while the mouse button is pressed
  $(document).mousemove(function(e){
    if(isMouseDown && dragStatus[currentElement.id] == 'on'){
      // update the position and call the registered function
      $.updatePosition(e);
      if(dragCallbacks[currentElement.id] != undefined){
        dragCallbacks[currentElement.id](e, currentElement);
      }

      return false;
    }
  });

  // when the mouse button is released
  $(document).mouseup(function(e){
    if(isMouseDown && dragStatus[currentElement.id] == 'on'){
      isMouseDown = false;
      if(dropCallbacks[currentElement.id] != undefined){
        dropCallbacks[currentElement.id](e, currentElement);
      }

      return false;
    }
  });

  // register the function to be called while an element is being dragged
  $.fn.ondrag = function(callback){
    return this.each(function(){
      dragCallbacks[this.id] = callback;
    });
  };

  // register the function to be called when an element is dropped
  $.fn.ondrop = function(callback){
    return this.each(function(){
      dropCallbacks[this.id] = callback;
    });
  };

  // stop the element dragging feature
  $.fn.dragOff = function(){
    return this.each(function(){
      dragStatus[this.id] = 'off';
    });
  };


  $.fn.dragOn = function(){
    return this.each(function(){
      dragStatus[this.id] = 'on';
    });
  };

  // set an element as draggable - allowBubbling enables/disables event bubbling
  $.fn.easydrag = function(allowBubbling){

    return this.each(function(){

      // if no id is defined assign a unique one
      if(undefined == this.id || !this.id.length) this.id = "easydrag"+(new Date().getTime());

      // set dragStatus
      dragStatus[this.id] = "on";

      // change the mouse pointer
      $(this).css("cursor", "move");

      // when an element receives a mouse press
      $(this).mousedown(function(e){

        // set it as absolute positioned
        $(this).css("position", "absolute");

        // set z-index
        $(this).css("z-index", "10000");

        // update track variables
        isMouseDown    = true;
        currentElement = this;

        // retrieve positioning properties
        var pos    = $.getMousePosition(e);
        lastMouseX = pos.x;
        lastMouseY = pos.y;

        lastElemTop  = this.offsetTop;
        lastElemLeft = this.offsetLeft;

        $.updatePosition(e);

        return allowBubbling ? true : false;
      });
    });
  };

})(jQuery);


$.fn.x = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var x = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     x += o.offsetLeft;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = x;
             } else {
                 result = Math.min(result, x);
             }
         } else {
             o.style.left = n + 'px';
         }
     });
     return result;
};

$.fn.y = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var y = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     y += o.offsetTop;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = y;
             } else {
                 result = Math.min(result, y);
             }
         } else {
             o.style.top = n + 'px';
         }
     });
     return result;
};




