4/* function xPnlCss( sUrl )
 * ------------------------
 * Changes the base stylesheet for the 3 columns. Should contain
 * the new layout for the 3 columns. I.e. leftpanel = 300px,
 * rightpanel = 0px, etc.
 * Parameter:
 *  - sUrl    = /path/to/stylesheet.css
 */
function xPnlCss( sUrl ) {
   document.getElementById('rightpanel').innerHTML = '';
   document.getElementById('leftpanel').innerHTML = '';
   document.getElementById('centerpanel').innerHTML = '';
   var oPanelNode = document.getElementById("panelstyle");
   oPanelNode.href = sUrl;
}

/* function removeObjects ( sObjType, sPanel )
 * ------------------------------------------------
 * Removes one stylesheet or javascript libraries from the head element
 * with a certain 3-character mark.
 * Parameters:
 *  - sObjType = script | link
 *  - sPanel   = mid | lft | rgt
 */
function removeObjects ( sObjType, sPanel ) {
   var aObj = document.getElementsByTagName(sObjType);
   for ( var iObjNr = (aObj.length-1) ; iObjNr >= 0  ; iObjNr-- ) {
      if (aObj[iObjNr].id.substring(0,3).toLowerCase() == sPanel ) {
         aObj[iObjNr].parentNode.removeChild(aObj[iObjNr]);
      }
   }
}

/* function insJs (sPanel, iNr, sUrl)
 * ------------------------------------
 * Adds one javascript library to the head element for a panel.
 * Parameters:
 *  - sPanel = mid | lft | rgt
 *  - iNr    = sequential number
 *  - sUrl   = /path/to/script.js
 */
function insJs (sPanel, iNr, sUrl) {
   var oHead= document.getElementsByTagName('head')[0];
   var oJsNode= document.createElement('script');
   oJsNode.type= 'text/javascript';
   oJsNode.id = sPanel + '-js-' + iNr;
   oJsNode.src= sUrl;
   oHead.appendChild(oJsNode);
}

/* function insCss (sPanel, iNr, sUrl)
 * ------------------------------------
 * Adds one stylesheet to the head element for a panel.
 * Parameters:
 *  - sPanel = mid | lft | rgt
 *  - iNr    = sequential number
 *  - sUrl   = /path/to/sheet.css
 */
function insCss (sPanel, iNr, sUrl) {
   var oHead = document.getElementsByTagName("head")[0];
   var oCssNode = document.createElement('link');
   oCssNode.type = 'text/css';
   oCssNode.rel = 'stylesheet';
   oCssNode.id = sPanel + '-css-' + iNr;
   oCssNode.href = sUrl;
   oCssNode.media = 'screen';
   oHead.appendChild(oCssNode);
}

/* function replaceObjs (sType,enumObjs,sPanel)
 * ------------------------------------------------------
 * Inserts stylesheets or javascript libraries in enumeration enumObjs
 * after first removing existing objects for the sPanel
 * Parameters:
 *  - sType       = objecttype: 'script' | 'link'
 *  - enumObjs = enumration of object urls separated by ':'
 *  - sPanel      = mid | lft | rgt
 */
function replaceObjs (sType,enumObjs,sPanel) {
   removeObjects (sType, sPanel);
   var aObj = enumObjs.split(':');
   switch (sType) {
      case 'link':
         for ( var iNr = 0 ; iNr < aObj.length ; iNr++ ) { insCss (sPanel, iNr, aObj[iNr]) }
         break;
      case 'script':
         for ( var iNr = 0 ; iNr < aObj.length ; iNr++ ) {  insJs (sPanel, iNr, aObj[iNr]) }
         break;
      default :
         alert ('Onbekend enumeratie type.');
         break;
   }
}

function ahah(sUrl, sTarget, sPanel, sCssText, sJsText) {
   document.getElementById(sTarget).innerHTML = 'Moment ...';

   if (sCssText != undefined) {
      replaceObjs ('link',sCssText,sPanel);
   }

   var oRequest;

   if (window.XMLHttpRequest) {
      oRequest = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      oRequest = new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (oRequest != undefined) {
      oRequest.onreadystatechange = function() { ahahDone(oRequest, sUrl, sTarget, sPanel, sJsText); };
      oRequest.open("GET", sUrl, true);
      oRequest.send("");
   }
}

function ahahDone(oRequest, sUrl, sTarget, sPanel, sJsText) {
   if (oRequest.readyState == 4) {
      if (oRequest.status == 200) {
         document.getElementById(sTarget).innerHTML = oRequest.responseText;
         if (sJsText != undefined) {
            replaceObjs ('script',sJsText,sPanel);
         }
      }
      else {
         document.getElementById(sTarget).innerHTML="ahah error:\n"+oRequest.statusText;
      }
   }
}

function hahah(sUrl, sTarget) {
   document.getElementById(sTarget).innerHTML = 'Moment ...';
   var oRequest;
   if (window.XMLHttpRequest) {
      oRequest = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      oRequest = new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (oRequest != undefined) {
      oRequest.onreadystatechange = function() {
         hahahDone(oRequest, sTarget);
      };
      oRequest.open("GET", sUrl, true);
      oRequest.send("");
   }
}

function hahahDone(oRequest, sTarget) {
   if (oRequest.readyState == 4) {
      if (oRequest.status == 200) {
         document.getElementById(sTarget).innerHTML = oRequest.responseText;
      }
      else {
         oTarget.innerHTML="hahah error:\n"+oRequest.statusText;
      }
   }
}

function ahText(sText, sTarget) {
   document.getElementById(sTarget).innerHTML = sText;
}
