function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

// raise and centre divs
function raise() {
  var el=document.getElementById('gallery');
  el.style.margin='0px';
  el.style.display='none';
  
  document.getElementById('galImage').onload=centrePopup;

  el.style.display='block';
  
  //centrePopup();
  return false;
}

// hide the popup div
function lower() {
  document.getElementById('gallery').style.display='none';
  return false;
}

var gAdjust=2;

/*
* center the popup
*/
function centrePopup() {
  var ptop=0;

  var el=document.getElementById('gallery');

  if (document.body.parentElement && document.body.parentElement.scrollTop) ptop=document.body.parentElement.scrollTop;
  else if (window.pageYOffset) ptop=window.pageYOffset;
    
  el.style.left=((document.body.offsetWidth-el.scrollWidth)/2)+'px';
  el.style.top=((ptop+document.body.offsetHeight-el.scrollHeight)/gAdjust)+'px';
}



/*
* ajax call to retrieve and possible execute code on response from server
*
*/
function doTask(params) {
  if (document.implementation && window.document.implementation.createDocument)
  {
    xmlDoc = document.implementation.createDocument("", "", null);
    xmlDoc.onload = doResponse;
  }
  else if (window.ActiveXObject)
  {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.onreadystatechange = function () {
      if (xmlDoc.readyState == 4) doResponse()
    };
  }
  else
  {
    return false;
  }

  d = new Date; // Generic - use to create unique url requests
  var rand = d.getTime();
  var url=document.location.protocol+'//'+document.location.host+'/alt/doTask.xml?rand='+rand+'&'+params;

  try{
    xmlDoc.load(url); // this is what we like to do
  } catch(e) { // if failed try using httprequest as a fallback - (e.g. Safari)
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
    if(req) {
      req.onreadystatechange = processHttpRequest;
      req.open("GET", url, true);
      req.send("");
    }

  }

  return false;
}

/*
* some browsers require this extra step to catch the http reponse
*
*/
function processHttpRequest() {
  if (req.readyState == 4) {
        if (req.status == 200) {
      xmlDoc=req.responseXML;
      doResponse();
        }
    }
}

/*
* process the response from server to the doTask function,
*
*/
function doResponse() {

  var tasks = xmlDoc.getElementsByTagName('task');
  var cmd=null;
  
  for(var i=0;i<tasks.length;i++) {
    cmd=tasks[i].getElementsByTagName('cmd')[0];

    if (cmd.childNodes[0]) {
      switch(cmd.childNodes[0].nodeValue) {
        case 'replace': // replace the innerHTML of an element
          var id=tasks[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
          var content=tasks[i].getElementsByTagName('content')[0].childNodes[0].nodeValue;
          if (id.length>0 && document.getElementById(id)) document.getElementById(id).innerHTML=content;
          break;
        case 'execute': // run some js
          var script=tasks[i].getElementsByTagName('script')[0].childNodes[0].nodeValue;
          eval(script);
          break;

      }
    }
  }
}