function clearForm() {
  document.getElementById('selecteditemname').innerHTML = '';
  document.getElementById('selecteditemid').value = '';
}

function closeFloatItem(id) {
  if (document.getElementById('fitem' + id)) {
    document.getElementById('fitem' + id).innerHTML = '';
  }
  if (document.getElementById('fpm' + id)) {
    document.getElementById('fpm' + id).src = '/layout/gfx/plus.png';
  }
}

function getDocvalue(name) {
  return document.getElementById(name).value;
}

function getUrlvalue( url, name, standard ) {
	// returns value of a parameter in url string
  name = '?' + name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( url );
  if (results == null){
    if (!standard) {
    	standard = '';
    }
    return standard;
  } else
    return results[1];
}

function hideFloat() {
	// hide floating window:
  var floatbackelement = document.getElementById('floatback');
  floatbackelement.style.display = 'none';
}

function openFloatItem(p_id, p_script, p_params) {
	// open selected item, get sub-items:
	// if parameters given, then store parameters, else read parameters:
  if (p_params) {
    document.getElementById('fscriptparams').value = p_params;
  } else {
    if (document.getElementById('fscriptparams')) {
      var p_params = document.getElementById('fscriptparams').value;
    } else {
    	var p_params = '';
    }
  }
  
  if (p_script) {
    document.getElementById('fscript').value = p_script;
  } else {
    if (document.getElementById('fscript')) {
      var p_script = document.getElementById('fscript').value;
    } else {
    	var p_script = '';
    }
  }
  
  if (document.getElementById('fpm' + p_id)) {
    if (document.getElementById('fpm' + p_id).src.indexOf('minus.png') > -1) {
      closeFloatItem(p_id);
      return;
    }
  }
	
  if (document.getElementById('fitem' + p_id)) {
    // item is visible
    var xmlhttp2 = startxmlhttp();
    var url = '/portal/lib/ajax/' + p_script + '.php';
    var params = 'id=' + p_id + ((p_params !='') ? '&' + p_params : '');
    xmlhttp2.open('POST', url, true);
    
    // send the proper header information along with the request
    xmlhttp2.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xmlhttp2.setRequestHeader('Content-length', params.length);
    xmlhttp2.setRequestHeader('Connection', 'close');
    
    xmlhttp2.onreadystatechange = function() {
      if ((xmlhttp2.readyState == 4) && (xmlhttp2.status == 200)) {
        var result = trim(xmlhttp2.responseText);
        if (result == 'not found') {
           showMessage('not found', 'error', 5000);
        } else {
          if (result.charAt(0) == '{') {    // json encoded: may return different contents  
            eval('arrfloat = ' + result);
            if (arrfloat['div']) {
            	document.getElementById('fitem' + p_id).innerHTML = arrfloat['div'];
            }
          } else {	// not json encoded: returns item content
            document.getElementById('fitem' + p_id).innerHTML = result;
          }
        }
        
        if (document.getElementById('fpm' + p_id)) {
          document.getElementById('fpm' + p_id).src = '/layout/gfx/minus.png';
        }
      }
    }
    xmlhttp2.send(params);
  }
}

function returnFloatresult() {
	// ********************************** return result *****************************
	// return result of selection,etc.:
  var fdemo = (getDocvalue('fdemo') != '');   // true if demo...

  if (document.getElementById('floatdiv')) {
    var extname = document.getElementById('namefield').value;
    var exttype = document.getElementById('typefield').value;

    if (extname != '') {
      var elName = document.getElementById(extname);  // get field to return text result to 
      elName.innerHTML =  document.getElementById('selecteditemname').innerHTML; // set value to text result
    }
    
    // field to return interger-result to:
    var extid = document.getElementById('idfield').value;    // name of id-field
    if (extid != '') {
      elId = document.getElementById(extid);  // get field to return integer result to 
      switch (exttype) {        // output depends on type of field to return result to 
        case 'id':              // id (input) field
          elId.value = document.getElementById('selecteditemid').value;  // set value to integer result
          break;
        case 'sel':             // selection list field: add option to list
          var oOption = document.createElement('OPTION');
          oOption.text = document.getElementById('selecteditemname').innerHTML;
          oOption.selected = 'true';
          oOption.value = document.getElementById('selecteditemid').value;
          var elOptOld = elId.options[0];
          try {
            elId.add(oOption, elOptOld); // standards compliant; doesn't work in IE
          }
          catch(ex) {
            elId.add(oOption, 0);        // IE only
          }
          if (fdemo) {
          	document.getElementById('testid').value = document.getElementById('selecteditemid').value;
          }
          break;
      }
    }
		
    // if flagfield is given, return a 1 to this field (flag to indicate a selection from this div)
    var extflag = document.getElementById('flagfield').value;  // name of flag-field
    if ((extflag) && (extflag != '')) {
      elId = document.getElementById(extflag);             // get flag-element in form
      if (elId) elId.value = 1;                            // set flag
    }
		
    // if funcfield is given, will execute string as function
    var extfunc = document.getElementById('funcfield').value;  // function to call on return
    if ((extfunc) && (extfunc != '')) {
      if (fdemo) {
      	document.getElementById('testfunc').innerHTML = extfunc;
      } else {
      	eval(extfunc);                     // evaluates the function string and executes it
      }
    }
    hideFloat();                         // hide floating div
  }
}

function setExternfields(params){ // idfield, namefield, typefield, funcfield, flagfield) {
	// set name of fields to return result to (name and id):
  setUrlvalue(params, 'name', 'namefield');
  setUrlvalue(params, 'id', 'idfield');
  setUrlvalue(params, 'type', 'typefield');
  setUrlvalue(params, 'func', 'funcfield');
  setUrlvalue(params, 'flag', 'flagfield');
}

function selectFloatItem(p_id, p_xid) {
	// when an item in a tree is selected:
  if (!document.getElementById('span' + p_id)) return;
  var itemname = document.getElementById('span' + p_id).innerHTML;    // name of tree-node
	
	// unselect currently selected item:
  var selecteditemvalue = document.getElementById('selecteditemid').value;
  if ((selecteditemvalue != 0) && (document.getElementById('span' + selecteditemvalue))) {
    document.getElementById('span' + selecteditemvalue).className = 'unselectedfolder';
  }
	
	// store id and name of newly selected item:
  document.getElementById('selecteditemid').value = p_xid; // p_id;
  document.getElementById('selecteditemname').innerHTML = itemname;
  document.getElementById('selecteditemname').title = p_id;  // title?
	// show newly selected item as selected:
  document.getElementById('span' + p_id).className = 'selectedfolder';
	
  // if complete path in tree to item required: get path:
  //TODO: general function to get path in tree
  if (document.getElementById('itempath')) {
    document.getElementById('itempath').innerHTML = '&lt;img src="/layout/gfx/wait16trans.gif" border="0" title="wait" alt="wait" /&gt;';
    var xmlhttp5 = startxmlhttp();
    var url = '/index.php?c=folder&m=getFolderpath&nl=empty';
    var params = 'id=' + p_id;
    xmlhttp5.open('POST', url, true);
    
    // send the proper header information along with the request
    xmlhttp5.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xmlhttp5.setRequestHeader('Content-length', params.length);
    xmlhttp5.setRequestHeader('Connection', 'close');
    
    xmlhttp5.onreadystatechange = function() {
      if ((xmlhttp5.readyState == 4) && (xmlhttp5.status == 200)) {
        document.getElementById('itempath').innerHTML = xmlhttp5.responseText;
      }
    }
    xmlhttp5.send(params);
  }
	
	// show subitems if not yet visible:
  if (document.getElementById('fpm' + p_id)) {
    if (document.getElementById('fpm' + p_id).src.indexOf('minus.png') == -1) {
      openFloatItem(p_id);
    }
  }
  
  if ((!document.getElementById('fbody' + p_id)) && (document.getElementById('fitem' + p_id))) {
    // create table for newly selected item:
    var newtable = '<div id="fbody' +  p_id + '"></div>';
    document.getElementById('fitem' + p_id).innerHTML = newtable;
  }
}

function setFloat(p_content, p_title, p_showfooter, p_newwidth, p_newheight) {
	// set content and title of floating window:
	if (p_showfooter == 'undefined') {
		p_showfooter = true;
	}
  document.getElementById('floatdiv').innerHTML = p_content;
  document.getElementById('floattitle').innerHTML = p_title;
  if (p_showfooter) {
  	document.getElementById('floatfooter').style.display = 'inline';
  } else {
  	document.getElementById('floatfooter').style.display = 'none';
  }
  if ((p_newwidth != 'undefined') && (p_newheight != 'undefined')) {
  	// resize float
  	setFloatWindow(p_newwidth, p_newheight);
  }
  if (document.getElementById('floatback').style.display != 'inline') {
  	showFloat();
  }
}

function setFloatBackground(p_main, p_header) {
	// set background color of main div and header:
  document.getElementById('floatback').style.background = p_main;
  document.getElementById('floatdiv').style.background = p_main;
  document.getElementById('floatheader').style.background = p_header;
  document.getElementById('floatfooter').style.background = p_main;
}

function setFloatFrame(p_url, p_title, p_width, p_height, p_left, p_top) {
	//***************************** call page *******************************     
	// get window content from url, set title, position and size:
	// use for info window, not possible to return result to calling form
  document.getElementById('floatdiv').innerHTML = '<iframe id="floatframe" src="' + p_url + '"></iframe>';
  document.getElementById('floattitle').innerHTML = p_title;
  setFloatWindow(p_width,p_height,p_left,p_top,true);
  var floatdivelement = document.getElementById('floatdiv');
  var floatframeelement = document.getElementById('floatframe');
  floatframeelement.style.width = floatdivelement.style.width;
  floatframeelement.style.height = floatdivelement.style.height;
  floatframeelement.style.border = 0;
  floatframeelement.style.top = 0;
  showFloat();
}

function setFloatScript(p_script, p_id, p_scriptparams, p_params) { //, title,p_width,p_height, p_extid, p_extname, p_exttype, p_extfunc, p_extflag) {
	//***************************** call script *******************************
	// get window content from php-script, set title, position and size:
	// set info on calling form(field),
  document.getElementById('fscript').value = p_script;
  document.getElementById('fscriptparams').value = p_scriptparams;
  document.getElementById('fparams').value = p_params;
  setExternfields(p_params); // p_extid, p_extname, p_exttype, p_extfunc, p_extflag);  // store info on calling form(field)
  var xmlhttp2 = startxmlhttp();    // Creating the AJAX object
  var url = '/portal/lib/ajax/' + p_script + '.php';
	
  xmlhttp2.open('POST', url, true);
  p_scriptparams = 'id=' + p_id + ((p_scriptparams != '') ? '&' + p_scriptparams : '') ;
  
  // send the proper header information along with the request
  xmlhttp2.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xmlhttp2.setRequestHeader('Content-length', p_scriptparams.length);
  xmlhttp2.setRequestHeader('Connection', 'close');
  
  xmlhttp2.onreadystatechange = function() {
    if ((xmlhttp2.readyState == 4) && (xmlhttp2.status == 200)) {
      //alert(xmlhttp2.responseText);
      var result = trim(xmlhttp2.responseText);
      if (result == 'not found') {
         showMessage('not found', 'error', 5000);
      } else {
        if (result.charAt(0) == '{') {    // json encoded: may return different contents  
          eval('arrfloat = ' + result);
          if (arrfloat['div']) {
          	document.getElementById('floatdiv').innerHTML = arrfloat['div'];
          }
        } else {                       // not json encoded: returns div content
          document.getElementById('floatdiv').innerHTML = result;
        }
      }
      
      var p_width = getUrlvalue(p_params, 'w', 500) * 1;
      var p_height = getUrlvalue(p_params, 'h', 400) * 1;
      var p_top = getUrlvalue(p_params, 't');
      var p_left = getUrlvalue(p_params, 'l');
      setFloatWindow(p_width, p_height, p_left, p_top, true);
      document.getElementById('fdemo').value = getUrlvalue(p_params, 'demo');
      document.getElementById('floattitle').innerHTML = getUrlvalue(p_params, 'title');
      var promptvalue = getUrlvalue(p_params, 'prom');
      if (promptvalue !='') promptvalue += ': ';
      document.getElementById('floatprompt').innerHTML = promptvalue;
      clearForm();   // clear previous selection
      
      var floatdivelement = document.getElementById('floatdiv');
      floatdivelement.onClick = '';
      showFloat();
    }
  }
  xmlhttp2.send(p_scriptparams);
}

function setFloatUrl(p_url, p_title, p_width, p_height, p_left, p_top) {
	//***************************** call page *******************************     
	// get window content from url, set title, position and size:
	// use for info window, not possible to return result to calling form
  var xmlhttp = startxmlhttp();    // Creating the AJAX object
  xmlhttp.open('GET', p_url, true);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      document.getElementById('floatdiv').innerHTML = xmlhttp.responseText;
      document.getElementById('floattitle').innerHTML = p_title;
      setFloatWindow(p_width, p_height, p_left, p_top, false);
    }
  }
  xmlhttp.send(null);
  showFloat();
}

function setFloatWindow(p_width, p_height, p_left, p_top, showfooter) {
	// set position and size of floating window:
	// if left and top are not supplied, will center div in window
  // calculate window size:
  if (showfooter) {
  	footheight = 24 * 1;
  } else {
  	footheight = -1;
  }
  
  if (typeof(window.innerWidth) == 'number') {
    //Non-IE
    var myWidth = window.innerWidth;
    var myHeight = window.innerHeight;
  } else if ((document.documentElement) && ((document.documentElement.clientWidth) || (document.documentElement.clientHeight)) ) {
    //IE 6+ in 'standards compliant mode'
    var myWidth = document.documentElement.clientWidth * 1;
    var myHeight = document.documentElement.clientHeight * 1;
  } else if((document.body) && ((document.body.clientWidth) || (document.body.clientHeight))) {
    //IE 4 compatible
    var myWidth = document.body.clientWidth;
    var myHeight = document.body.clientHeight;
  }
	
  if (p_height > myHeight) {
    p_height = myHeight;  // div my not be larger than visible window
    p_top = 0;
  }
  if (p_width > myWidth - 30) {
    p_width = myWidth - 30;
    p_left = 0;
  }
  //alert('height=' + p_height + ',width=' + p_width + ',top=' + p_top + ',left=' + p_left);
  if ((!p_left) || (p_left == '')) {                     // left-position not given : center div horizontally
    p_left = (myWidth - p_width - 30) / 2;
  }
  if ((!p_top) || (p_top == '')) {                       // top-position not given : center div vertically
    p_top = (myHeight - p_height) / 2;
  }
  //alert('height=' + p_height + ',width=' + p_width + ',top=' + p_top + ',left=' + p_left);
  var floatbackelement = document.getElementById('floatback');
  floatbackelement.style.left = p_left + 'px';
  floatbackelement.style.top = (p_top - 18) + 'px';
  floatbackelement.style.width = p_width + 'px';
  floatbackelement.style.height = (p_height + 4 + 16 + footheight) + 'px';
  
  var floatdivelement = document.getElementById('floatdiv');
  floatdivelement.style.left = p_left + 'px';
  if (p_top < 18) floatdivtop = 18;
  else floatdivtop = p_top;
  floatdivelement.style.top = floatdivtop + 'px';
  floatdivelement.style.width = p_width + 'px';
  floatdivelement.style.height = p_height + 'px';
  
  var floatheaderelement = document.getElementById('floatheader');
  floatheaderelement.style.left = p_left + 'px';
  floatheaderelement.style.width = (p_width + 10) + 'px';
  if (p_top - 18 < 0) var headertop = 0;
  else var headertop = p_top - 18;
  floatheaderelement.style.top = headertop + 'px';
  
  var floatfooterelement = document.getElementById('floatfooter');
  if (showfooter) {
    floatfooterelement.style.display = 'inline';
    floatfooterelement.style.left = p_left + 'px';
	  floatfooterelement.style.width = (p_width + 10) + 'px';
	  floatfooterelement.style.top = (p_top + p_height + 16 - 4) + 'px';
  } else {
    floatfooterelement.style.display = 'none';
  }
}

function setUrlvalue(url, name, fieldname, standard) {
	// store url-parameter value in field
  if (document.getElementById(fieldname)) {
    document.getElementById(fieldname).value = getUrlvalue(url, name, standard);
  }
}

function showFloat() {
	// show floating window:
  var floatbackelement = document.getElementById('floatback');
  floatbackelement.style.display = 'inline';
}

function showObservationpanel(p_obsid, p_role) {
  setFloatWindow(1152, 520, 50, 50, false);
  showFloat();
  
	// 1. test if observation panel is available:
  if (document.getElementById('obsdetaildiv')) {
    showObservation(p_obsid, p_role);  // refresh panel with given observation
  } else {  // first create observation panel
	  var obspanxmlhttp = startxmlhttp();    // Creating the object
	  var url = '/portal/lib/ajax/observationpanel.php';
	  var params = '';
		
	  // POST METHOD
	  obspanxmlhttp.open('POST', url, true);
		
	  //Send the proper header information along with the request
	  obspanxmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	  obspanxmlhttp.setRequestHeader('Content-length', params.length);
	  obspanxmlhttp.setRequestHeader('Connection', 'close');
	  
	  obspanxmlhttp.onreadystatechange = function() {
	    if ((obspanxmlhttp.readyState == 4) && (obspanxmlhttp.status == 200)) {
        document.getElementById('floatdiv').innerHTML = obspanxmlhttp.responseText;
        showObservation(p_obsid, p_role);  // refresh panel with given observation
			}
	  }
	  obspanxmlhttp.send(params);
  }
}

function showPicturepanel(p_picid, p_role) {
// 1. test if picture panel is available:
  if (document.getElementById('picdetaildiv')) {
    showPicture(p_picid, p_role);  // refresh panel with given picture
  } else {  // first create picture panel
	  var xmlhttp = startxmlhttp();    // Creating the object
	  var url = '/portal/lib/ajax/picturepanel.php';
	  var params = '';
		
	  // POST METHOD
	  xmlhttp.open('POST', url, true);
		
	  //Send the proper header information along with the request
	  xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	  xmlhttp.setRequestHeader('Content-length', params.length);
	  xmlhttp.setRequestHeader('Connection', 'close');
	  
	  xmlhttp.onreadystatechange = function() {
	    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
        document.getElementById('floatdiv').innerHTML = xmlhttp.responseText;
        showPicture(p_picid, p_role);  // refresh panel with given picture
			}
	  }
	  xmlhttp.send(params);  
  }
  setFloatWindow(1152, 520, 50, 50, true);
  showFloat();
}