/*
* google maps loading code
*/
var map;
var gDir;
var point

function gMapLoad() {
      if (GBrowserIsCompatible()) {

        map = new GMap2(document.getElementById("map"));
        point= new GLatLng(52.782180,-1.202556);
        map.setCenter(point, 11);
        map.addControl(new GSmallZoomControl());

        var marker=new GMarker(point);
        var windowContent="<b>Colaw</b><br />19 The Office Village<br />North Road<br />Loughborough<br />Leicestershire<br/>LE11 1QJ<br /><br /><strong>08456 588 703</strong><br /><br /><form action=\"http://maps.google.co.uk/maps\" method=\"get\" target=\"_blank\"><input id=\"daddr\" class=\"text\" type=\"hidden\" size=\"10\" value=\"LE11 1QJ\" name=\"daddr\"/><label for='saddr'>Enter your postcode</label><br /><input id=\"saddr\" class=\"text\" type=\"text\" size=\"10\" value=\"\" name=\"saddr\" style=\"margin-right:10px;\" /><input type='submit' value='Get Directions!' name='submit'  /><br /><p style=\"font-size:10px;\">directions open in a new window</p></form>"
        map.addOverlay(marker);
        //GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(windowContent);
        //});
        GEvent.addListener(marker,"click",function(){
          marker.openInfoWindowHtml(windowContent);
        });

        gDir=new GDirections(map,document.getElementById("directionsCon"));
        GEvent.addListener(gDir,"load",onGDirectionsLoad);
        GEvent.addListener(gDir, "error", handleErrors);
        //setDirections("LE11 1NL");

      }

    }

    function setDirections(fromAddress){
      //alert (fromAddress);
      gDir.load("from: "+fromAddress+", UK to: 52.782180,-1.202556");
    }

    function onGDirectionsLoad(){
      //don't know what to do here....
      //document.getElementById("directionsCon").innerHTML = gDir.getStatus().code;
    }

    function handleErrors(){
        if (gDir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gDir.getStatus().code);
        else if (gDir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gDir.getStatus().code);

        else if (gDir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gDir.getStatus().code);

        // else if (gDir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong
        // alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gDir.getStatus().code);

        else if (gDir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gDir.getStatus().code);

        else if (gDir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gDir.getStatus().code);

        else alert("An unknown error occurred.");
     }

     
/*
* 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);
  }
}