
/*
* 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;
          if (tasks[i].getElementsByTagName('content')[0].textContent) var content=tasks[i].getElementsByTagName('content')[0].textContent; //avoid FF 4096 char limit
          else 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;

      }
    }
  }
}

/*
* use JS to hide controls which are only required when JS is not enabled
* - do this by adding hideJS as a class to the css
*/
function hideElements() {
  if (document.styleSheets) {
    if (document.styleSheets[0].addRule) {    // IE
      document.styleSheets[0].addRule('.hideJS','display: none',0);
    } else {    // !IE
      document.styleSheets[0].insertRule('.hideJS {display: none}', 0);
    }
  }

}

/*
* Add the onload function to any other onload function
*/
function addLoadEvent(func) {
  var oldonload = window.onload;

  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(hideElements);

// show the form wait box
function showWait(id) {
  if (document.getElementById(id)) {
    var el=document.getElementById(id);

    el.style.position='absolute';
    el.style.left='50%';
    el.style.top='50%';
    el.style.display='block';
    el.style.marginLeft="-" + parseInt(el.offsetWidth  / 2)+'px';
    el.style.marginTop ="-" + parseInt(el.offsetHeight / 2)+'px';
    // IE has a quirk which means we need to reload the animated gif to make it animate
    if (document.getElementById(id+'wImg')) setTimeout("document.getElementById('"+id+"wImg').src=document.getElementById('"+id+"wImg').src",20);
  }
}

/*******************************/
/* Right Column Menu Movements */
/*******************************/
var slideRate=0;
var slideRatio=0;
var count=0;
var inProcess=false; 
var menuOpen=false;



function initDropIn() {
  var el= document.getElementById("rightmove");
  if (el){
    slideRate=10;
    slideRatio=5;
    inProcess=true;
    dropIn();
  }
  
}
var endHeight=0;

function dropIn() {
  var el= document.getElementById("rightmove");
  var cypos=parseInt(el.style.top.replace("px",""));
  var el_menu= document.getElementById("rightmenu");
  el_menu.style.visibility='hidden';
 
  var nypos=1;
  
  if (cypos<(1-slideRate)) nypos=cypos+slideRate;
  else nypos=1;
  
  if(cypos<1) {
    el.style.top=nypos+"px";
    setTimeout('dropIn()',5);
  }else{
    el_menu.style.visibility='visible';
    openMenu();
  }
}


function initDropOut(url) {
  var el= document.getElementById("rightmove");
  
  if (el){
    //alert ('initDropOut');
    slideRate=12;
    slideRatio=1;
    dropOut(url);
  } else {
    document.location=url;
  }
 return false;
}

var endHeight=0;

function dropOut(url) {
  var el= document.getElementById("rightmove");
  var cypos=el.offsetTop;
  var nypos=-300;
  if (cypos>(-300-slideRate)){
     nypos=cypos-slideRate;
  }else {
    nypos=-300;
  }
  if(cypos>-300) {
    el.style.top=nypos+"px";
    setTimeout('dropOut(\''+url+'\')',5);
  }else{
    
  document.location=url;
  }
}

function initMenu() {
  var el=document.getElementById("rightmenu");
  if (el) {
    endHeight=el.offsetHeight;
    el.style.visibility='hidden';
    el.style.height="0px";
  }
}

/**
* open the more info box
*/
function openMenu() {
  menuOpen=true;
  var el=document.getElementById("rightmenu");
  var cHeight=parseInt(el.style.height.replace("px",""));
  var nHeight=0;
 
  if(cHeight<(endHeight-10)) nHeight=cHeight+10;
  else nHeight=endHeight;
  
  if(cHeight<endHeight) {
    el.style.height=nHeight+"px";  
    setTimeout('openMenu()',15);
  } else {
    //inProcess=false;
  }
}

/**
* open the more info box
*/
function closeMenu(url) {
  //alert (menuOpen);
  //if (menuOpen) {
    
    var el=document.getElementById("rightmenu");
    var cHeight=parseInt(el.offsetHeight);
    endHeight=0;
    var nHeight=0;
    if(cHeight>(0+10)) nHeight=cHeight-10;
    else nHeight=0;
    if(cHeight>0) {
      el.style.height=nHeight+"px";  
      setTimeout('closeMenu(\''+url+'\')',15);
    } else {
      el.style.visibility='hidden';
      //menuOpen=false;
      initDropOut(url);
    }
  /*} else {
    alert ('menu not open');
    //initDropOut(url);
    window.location=url;
  }*/
}



