/* browser sniffer */
var agent = navigator.userAgent.toLowerCase();
var major = parseInt(navigator.appVersion);
var minor = parseFloat(navigator.appVersion);
var ns = ((agent.indexOf('mozilla') != -1) && ((agent.indexOf('spoofer') == -1) && (agent.indexOf('compatible') == -1)));
var ns4 = (this.ns && (this.major == 4));
var ns6 = (this.ns && (this.major >= 5));
var ie = (agent.indexOf("msie") != -1);
var ie3 = (this.ie && (this.major < 4));
var ie4 = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.0") == -1));
var ie5 = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.0") != -1));
var ie55 = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.5") != -1));
var ie6 = (this.ie && (agent.indexOf("msie 6.0")!=-1) );
var javaEnabled = navigator.javaEnabled();
// mozilla == netscape 6

/* opens a tooltip near to the position where the event was triggerd 
 * @param id the id of the layer/div
 * @param event the event trigger
 * e.g. onmouseover="showToolTip( 'tooltip1', event )"
 */
function showToolTip( id, event ) {
	if ( ns6 ){
	    document.getElementById(id).style.top=event.clientY+10;
        document.getElementById(id).style.left=event.clientX+10;
	    document.getElementById(id).style.visibility = "visible";
	}
	else if ( ns4 ){
	    document.layers[id].left=event.pageX+10;
	    document.layers[id].top=event.pageY+10;
	    document.layers[id].visibility = "show";
	}	
	else if ( ie ){
		document.all[id].style.pixelLeft=window.event.clientX+document.body.scrollLeft+10;
	    document.all[id].style.pixelTop=window.event.clientY+document.body.scrollTop;
	    document.all[id].style.visibility = "visible";
    }
}

/* hides a tooltip  
 * @param id the id of the layer/div
 * e.g. onmouseout="showToolTip( 'tooltip1' )"
 */
function hideToolTip( id ) {
	if ( ns6 ) document.getElementById(id).style.visibility = "hidden";
	else if (ns4) document.layers[id].visibility = "hide";
	else if (ie) document.all[id].style.visibility = "hidden";
}

/* the current visible tooltip */
var activeToolTip = null;

/* opens a tooltip near to the position where the event was triggerd 
 * hides the last open tool tip
 * sets the tooltip as active 
 * @param id the id of the layer/div
 * @param the event
 * e.g. onClick="showToolTipWindow( 'tooltip1', event )"
 */
function showToolTipWindow( id, event){
    hideToolTipWindow( activeToolTip );
	showToolTip( id, event );
    activeToolTip = id;
}

/* hides a tooltip
 * is called by handle(event)
 * @param id the id of the layer/div
 */
function hideToolTipWindow(id) {
	if ( id == null ) return;
	hideToolTip( id );
	activeToolTip=null;
}

/* captures the click events on this site for hiding visible layers
 */
function capture(){
	if (window.Event){
	  	document.captureEvents(Event.CLICK);
	}
}
/* handles all click events
 * attention this is qick&dirty!!
 * we just check if the event src element is text or an image ( ie -> has an name )
 * then we return otherwise we close the active tooltip
 * @param the event
 */
document.onclick=handle;
function handle(evt){
	if ( ie ){
	    if ( event.srcElement.name != null )return
	}
	else if ( ns4 || ns6){
	    var target = (evt.target).toString()
		if ( target.indexOf("Image") > -1 || target.indexOf("Text") > -1 ) return
	}
	if ( activeToolTip == null ) return;
	hideToolTipWindow( activeToolTip );
}

/** 
  * creates an 2 dim array
  * @param rows
  * @param colums
  */
function MakeArray2Dim(rows, colums){
	this.dim = new Array(rows, colums)

	for (var i =0; i< rows; i++)
	this[i] = new Array(colums);
}



/**
  * selects all options from a list
  * @param the form element
  */
function selectAllOptions( fo ) {
	c = fo.checked;
	for (i = 0; i < fo.options.length; i++)
		fo.options[i].selected = c;
}

/**
  * removes an substring of an string
  * @text original text
  * @toSlice the string which should be removed from the text
  * @delimiter
  * @restText
  * e.g. getSliceText( "t1:t2:t:t3", "t", ":", "" );
  * returns "t1:t2:t3"
  */
var newText ="";
function resectText( text, toSlice, delimiter, restText){
    var posStart = text.indexOf(toSlice);
    var stop = false;
    var delLength = delimiter.length;    
    if ( text == toSlice ){
        return "";
    }else if ( stop == false && posStart > -1 ){
        //not first element
        if ( posStart != 0 ){
            //char before != delimiter then we are looking for the next occurence of toSlice
            var charbefore = text.substring( (posStart-delLength), posStart );
            var h = (charbefore != delimiter) ;
            if ( charbefore != delimiter ){
                var rt = text.substring(0, posStart+(toSlice+delimiter).length);
                resectText( text.substring( posStart+(toSlice+delimiter).length, text.length ), toSlice, delimiter, restText+rt);
                stop = true;
            }
        }
        //char after != delimiter
        if ( stop == false && (posStart+toSlice.length) < text.length ){
            var charafter = text.substring( (posStart+toSlice.length),((posStart+toSlice.length)+delimiter.length) );
            var h1 = (charafter != delimiter);
            //if char after != delimiter
            if ( charafter != delimiter ){
                //search for next delimiter
                s =  text.substring( (posStart+toSlice.length+delLength), text.length );
                var nt = s.substring( (s.indexOf(delimiter)+delimiter.length), s.length );
                //char after != delimiter then we are looking for the next occurence of toSlice
                var rt = text.substring(0, ( text.indexOf(delimiter)+delimiter.length) );
                resectText( nt, toSlice, delimiter, restText+rt );
                stop = true;
            }
        }else if ( stop == false ){
            posStart=posStart-1;
        }	      
        if ( stop == false || this.newText == null ){  
            c1 = text.substring(0,posStart);
            c2 = text.substring( posStart+((toSlice+delimiter).length), text.length);
            if ( c2.length > toSlice.length ){
                resectText( c2, toSlice, delimiter, (restText+c1) );
            }else{
                this.newText = restText+c1+c2;
            }
        }
        return this.newText;
    }else{
        this.newText = (restText+text);
        return (restText+text);
    }
    
}


/**
  * encodes a uri component to UTF-8 
  * is simular to the encodeURIComponent 
  * which is supported from ie 5.5 upwards
  * @the uri component
  */
function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}

function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}


var hexchars = "0123456789ABCDEF";
var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}