var theEvent;
var theTarget;
var orderEvent;
var orderTarget;
var oldMenu = '';
var oldSubMenu = '';
var agent;
var isFirefox = false;
var isMozilla = false;
var isOpera = false;
var isIEge5lt6 = false;
var isIE7plus = false;
var detailsChanged = false;
var isLoaded = false;
var otherClass = "";
var contentsHeight = 0;
var prevShowBusiness = '0';

function loaded( )
{
  isLoaded = true;

  agent = navigator.userAgent.toLowerCase();

  if (agent.indexOf('firefox') > -1)
    isFirefox = true;
  if (agent.indexOf('opera') > -1)
    isOpera = true;
  if (agent.indexOf('gecko') > -1)
    isMozilla = true;
  if (agent.indexOf('msie 5') > -1)
    isIEge5lt6 = true;
  if (agent.indexOf('msie 7') > -1)
    isIE7plus = true;

  if (!(top.location==self.location))
  {
    top.location=self.location;
  }
  
  setCopyright();

  renderEmails();
}

function loadedSplash( )
{
  isLoaded = true;

  agent = navigator.userAgent.toLowerCase();

  if (agent.indexOf('firefox') > -1)
    isFirefox = true;
  if (agent.indexOf('opera') > -1)
    isOpera = true;
  if (agent.indexOf('gecko') > -1)
    isMozilla = true;
  if (agent.indexOf('msie 5') > -1)
    isIEge5lt6 = true;
  if (agent.indexOf('msie 7') > -1)
    isIE7plus = true;

  if (!(top.location==self.location))
  {
    top.location=self.location;
  }
  
  setCopyright();

  renderEmails();
  
  runSplash();
}

function getWindowHeight( isScrollbar )
{
  var windowHeight = 0;
  if (typeof(window.innerHeight) == 'number')
  {
    windowHeight = window.innerHeight;
  }
  else
  {
    if (document.documentElement && document.documentElement.clientHeight)
    {
      windowHeight = document.documentElement.clientHeight;
    }
    else
    {
      if (document.body && document.body.clientHeight)
      {
        windowHeight = document.body.clientHeight;
      }
    }
  }

  if (document.all)
    return windowHeight;
  else
  {
    if (isScrollbar)
      return windowHeight-22;
    else
      return windowHeight-2;
  }
}

function getWindowWidth()
{
  var windowWidth = 0;

  if (typeof(window.innerWidth) == 'number')
  {
    windowWidth = window.innerWidth;
  }
  else
  {
    if (document.documentElement && document.documentElement.clientWidth)
    {
      windowWidth = document.documentElement.clientWidth;
    }
    else
    {
      if (document.body && document.body.clientWidth)
      {
        windowWidth = document.body.clientWidth;
      }
    }
  }

  if (document.all)
  {
    if (isOpera)
      return windowWidth-17;
    else
      return windowWidth;
  }
  else
  {
    if (isFirefox)
      return windowWidth-21;
    else
      return windowWidth-17;
  }
}

function setCopyright()
{
  if (document.getElementById)
  {
    windowWidth = getWindowWidth();

    if (windowWidth < 750)
      windowHeight = getWindowHeight(true);
    else
      windowHeight = getWindowHeight(false);

    if (contentsHeight == 0)
    {
      contentsHeight = document.getElementById('contents').offsetHeight;
      if (!(isFirefox || isMozilla || isOpera || isIE7plus))
        contentsHeight -= 230;
    }

    contentsElement = document.getElementById('contents');

    if (document.getElementById('menuTextPhone'))
    {
      menuHeight = document.getElementById('menuTextPhone').style.top;
      menuHeight = parseInt(menuHeight.replace(/px/,""))+180;
      if (windowHeight < menuHeight)
        windowHeight = menuHeight;
    }

    if (windowHeight > contentsHeight)
    {
      if (isFirefox || isMozilla)
        contentsElement.style.height = windowHeight-131 + 'px';
      else if (isIEge5lt6)
        contentsElement.style.height = windowHeight-19 + 'px';
      else
        contentsElement.style.height = windowHeight-133 + 'px';
    }
  }
}

function runSplash()
{
  moveDigger(0);
}

function moveDigger(iter)
{
  if (iter > 219)
  {
    shrinkCover(0);
    return;
  }
  else
  {
    iter+=2;
    diggerElement = document.getElementById('diggerSplash');
    newPos = iter*2-450;
    diggerElement.style.right = newPos+'px';
    if (iter > 80)
      moveLogo('1', iter);
    if (iter > 150)
      moveLogo('2', iter);
    setTimeout("moveDigger("+iter+")",10);
  }  
}

function moveLogo(logoID, iter)
{
  logoElement = document.getElementById('logo'+logoID);
  if (logoID == 1)
    newPos = iter*2-450;
  if (logoID == 2)
    newPos = iter*2-450;
  logoElement.style.right = newPos+'px';
}

function shrinkCover(iter)
{
  if (iter > 149)
  {
    imageMapElement = document.getElementById('imageMap');
    imageMapElement.style.display = 'block';
    mapBarsElement = document.getElementById('mapBars');
    mapBarsElement.style.display = 'block';
    contentsHeight = 800;
    contentsElement = document.getElementById('contents');
    contentsElement.style.height = contentsHeight + 'px';
    setCopyright();
    if (!isOpera)
      showMap(0);
    return;
  }
  else
  {
    iter+=2;
    coverElement = document.getElementById('cover');
    if (iter < 130)
    {
      newWidth = 160-iter;
      coverElement.style.width = newWidth+'px';
    }
    else
    {
      newPos = 120-iter;
      coverElement.style.right = newPos+'px';
    }
    setTimeout("shrinkCover("+iter+")",20);
  }
}

function showMap(iter)
{
  if (iter == 100)
  {
    return;
  }
  else
  {
    iter+=10;
    imageMapElement = document.getElementById('imageMap');
    imageMapElement.style.opacity = iter/100;
    imageMapElement.style.filter = 'alpha(opacity='+iter+')';
    mapBarsElement = document.getElementById('mapBars');
    mapBarsElement.style.opacity = iter/100;
    mapBarsElement.style.filter = 'alpha(opacity='+iter+')';
    setTimeout("showMap("+iter+")",10);
  }
}

function econvert(str)
{
  str = str.replace(/_DOT_/gi, '.');
  str = str.replace(/_AT_/gi, '@');
  return str;
}

function renderEmails()
{
  var spans = document.getElementsByTagName("span");
  for (i=0; i< spans.length; i++) 
    if (spans[i].className=="email") 
      spans[i].innerHTML = econvert(spans[i].innerHTML);
}

function clearValue( form, text )
{
  if (form.value == text)
  {
    form.value = '';
  }
  return true;
}

function showValue( form, text, override )
{
  if ((form.value == '') || override)
  {
    form.value = text;
  }
  return true;
}

function showMenu( name )
{
  if (document.getElementById)
  {
    eval("thePointer = document.getElementById('" + name + "'); thePointer.style.visibility = 'visible';");
    eval("thePointer = document.getElementById('" + name + "Link'); thePointer.className = 'menuHover';");
  }
}

function hideMenu( name )
{
  if (document.getElementById)
  {
    eval("thePointer = document.getElementById('" + name + "'); thePointer.style.visibility = 'hidden';");
    eval("thePointer = document.getElementById('" + name + "Link'); thePointer.className = 'menu';");
  }
}

function validate()
{
  if((document.searchForm.query.value == "Search Text") || (document.searchForm.query.value == ""))
  {
    alert("Please enter something to search for, even if it's only part of what you're looking for.");
    document.searchForm.query.focus();
    return false;
  }
}

function highlight( tableEvent, tableName )
{
  if (document.getElementById)
  {
    eval("thePointer = document.getElementById('" + tableName + "Top'); thePointer.className='menuBoxHover';");
    eval("thePointer = document.getElementById('" + tableName + "Link'); thePointer.className='menuBoxHover';");
  }
  else if (document.all)
  {
    theTarget = (document.all) ? tableEvent.srcElement : tableEvent.target;
    theTarget.className = 'menuBoxHover';
  }

  return true;
}

function dehighlight( tableEvent, tableName )
{
  if (document.getElementById)
  {
    eval("thePointer = document.getElementById('" + tableName + "Top'); thePointer.className='menuBox';");
    eval("thePointer = document.getElementById('" + tableName + "Link'); thePointer.className='menuBox';");
  }
  else if (document.all)
  {
    theTarget = (document.all) ? tableEvent.srcElement : tableEvent.target;
    theTarget.className = 'menuBoxHover';
  }

  return true;
}

function toggle( name, state )
{
  if (eval("typeof(show"+name+")") == "undefined")
  {
    eval("show"+name+" = state;");
    if (eval("show" + name))
    {
      eval("thePointer = document.getElementById('" + name + "Show'); thePointer.style.display = '';");
      eval("thePointer = document.getElementById('" + name + "Hide'); thePointer.style.display = 'none';");
      eval("thePointer = document.getElementById('" + name + "'); thePointer.style.display = 'none';");
      eval("show" + name + "= false;")
    }
    else
    {
      eval("thePointer = document.getElementById('" + name + "Show'); thePointer.style.display = 'none';");
      eval("thePointer = document.getElementById('" + name + "Hide'); thePointer.style.display = '';");
      eval("thePointer = document.getElementById('" + name + "'); thePointer.style.display = '';");
      eval("show" + name + "= true;")
    }
  }
  else
  {
    if (eval("show" + name))
    {
      eval("thePointer = document.getElementById('" + name + "Show'); thePointer.style.display = '';");
      eval("thePointer = document.getElementById('" + name + "Hide'); thePointer.style.display = 'none';");
      eval("thePointer = document.getElementById('" + name + "'); thePointer.style.display = 'none';");
      eval("show" + name + "= false;")
    }
    else
    {
      eval("thePointer = document.getElementById('" + name + "Show'); thePointer.style.display = 'none';");
      eval("thePointer = document.getElementById('" + name + "Hide'); thePointer.style.display = '';");
      eval("thePointer = document.getElementById('" + name + "'); thePointer.style.display = '';");
      eval("show" + name + "= true;")
    }
  }
}

function toggleForm( name, showObject, hideObject )
{
  if (eval("typeof(show"+name+")") == "undefined")
  {
    eval("thePointer = document.getElementById('"+showObject+"'); thePointer.style.display = '';");
    eval("thePointer = document.getElementById('"+hideObject+"'); thePointer.style.display = 'none';");
    eval("show"+name+" = true;");
  }
  else
  {
    if (eval("show" + name))
    {
      eval("thePointer = document.getElementById('"+showObject+"'); thePointer.style.display = 'none';");
      eval("thePointer = document.getElementById('"+hideObject+"'); thePointer.style.display = '';");
      eval("show"+name+" = false;");
    }
    else
    {
      eval("thePointer = document.getElementById('"+showObject+"'); thePointer.style.display = '';");
      eval("thePointer = document.getElementById('"+hideObject+"'); thePointer.style.display = 'none';");
      eval("show"+name+" = true;");
    }
  }
}

function show( name )
{
  eval("thePointer = document.getElementById('" + name + "Show'); thePointer.style.display = 'none';");
  eval("thePointer = document.getElementById('" + name + "Hide'); thePointer.style.display = '';");
  eval("thePointer = document.getElementById('" + name + "'); thePointer.style.display = '';");
}

function hide( name )
{
  eval("thePointer = document.getElementById('" + name + "Show'); thePointer.style.display = '';");
  eval("thePointer = document.getElementById('" + name + "Hide'); thePointer.style.display = 'none';");
  eval("thePointer = document.getElementById('" + name + "'); thePointer.style.display = 'none';");
}

function popWindow(theURL, windowName)
{
  var myWindow;
  eval("myWindow=window.open('"+theURL+"','"+windowName+"','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,left=50,top=50')");
  if (myWindow==null || typeof(myWindow)=="undefined") 
  {
    alert("A popup blocker is stopping you opening this window, please disable it and try again.");
  }
}

function jumpTo( theURL )
{
  top.location.href = theURL;
}

function addToOrder()
{
  alert('added');
}

function showHelp( section )
{
  alert(section);
}

function getURL()
{
  theURL = top.location.href;
  theIndex = theURL.indexOf("#");

  if (theIndex > 0)
  {
    if (theIndex == (theURL.length-1))
      return theURL.replace(/#/,"");
    else
      return theURL.substring(0, theIndex);
  }
  else
    return theURL;
}

function submitDetails( detailsType )
{
  if (detailsChanged)
  {
    var e = document.createElement("script");
    var updateString = "https://192.168.0.55/Account?action=updateDetails";

    if (detailsType == "lostPassword")
    {
      setDetailsUnchanged('Password');
      updateString += "&update=lostPassword";
      updateString += "&username="+document.lostPassword.username.value;
    }
    else if (detailsType == "lostUsername")
    {
      setDetailsUnchanged('Username');
      updateString += "&update=lostUsername";
      updateString += "&email="+document.lostUsername.email.value;
    }

    updateString += "&key="+Math.random();

    e.src = unescape(updateString.replace(/\+/g, " "));

    e.type="text/javascript";
    document.getElementsByTagName("head")[0].appendChild(e);
  }
}

function setDetailsChanged( objectType )
{
  detailsChanged = true;
  eval("document.lost"+objectType+".submitButton.disabled = false;");
}

function setDetailsUnchanged( pageType )
{
  detailsChanged = false;

  if (document.getElementById)
  {
    if (pageType == "Password")
      thePointer = document.getElementById('lostPassword');
    else if (pageType == "Username")
      thePointer = document.getElementById('lostUsername');

    thePointer.disabled = 'true';
  }
}

function getkey(e)
{
  if (window.event)
    return window.event.keyCode;
  else if (e)
    return e.which;
  else
    return null;
}

function getDirections()
{
  if (document.getElementById)
  {
    thePointer = document.getElementById('isNewWindow');

    if (thePointer.checked)
    {
      saddr = document.getElementById('saddrValue');
      daddr = document.getElementById('daddrValue');
      directionsURL = "http://maps.google.co.uk/maps?saddr="+saddr.value+"&daddr="+daddr.value+"&hl=en";
      regex = /\s/g ;
      directionsURL = directionsURL.replace(regex, "+");
      popWindow(directionsURL, 'directions');
    }
    else
    {
      thePointer = document.getElementById('directionsForm');
      thePointer.submit();
    }
  }
}

function resetDirections()
{
  if (document.getElementById)
  {
    thePointer = document.getElementById('saddr');
    thePointer.value = "Postcode or Street Name, City";
  }
}

function getDirectionsUpper()
{
  if (document.getElementById)
  {
    thePointer = document.getElementById('isNewWindowUpper');

    if (thePointer.checked)
    {
      saddr = document.getElementById('saddrUpperValue');
      daddr = document.getElementById('daddrUpperValue');
      directionsURL = "http://maps.google.co.uk/maps?saddr="+saddr.value+"&daddr="+daddr.value+"&hl=en";
      regex = /\s/g ;
      directionsURL = directionsURL.replace(regex, "+");
      popWindow(directionsURL, 'directions');
    }
    else
    {
      thePointer = document.getElementById('directionsFormUpper');
      thePointer.submit();
    }
  }
}

function resetDirectionsUpper()
{
  if (document.getElementById)
  {
    thePointer = document.getElementById('saddrUpper');
    thePointer.value = "Postcode";
  }
}

function showBusiness(show, hide)
{
  if (document.getElementById)
  {
    thePointer = document.getElementById('business'+hide);
    thePointer.style.display = "none";
    thePointer = document.getElementById('business'+show);
    thePointer.style.display = "";
    thePointer = document.getElementById('applicationForm');
    thePointer.style.display = "";
    thePointer = document.getElementById('businessVal');
    thePointer.value = show;
  }
  if (isFirefox || isMozilla || isIE7plus)
  {
    if ((prevShowBusiness == '0') && (show == '1'))
      contentsHeight += 800;
    else if ((prevShowBusiness == '0') && (show == '2'))
      contentsHeight += 970;
    else if ((prevShowBusiness == '2') && (show == '1'))
      contentsHeight -= 160;
    else if ((prevShowBusiness == '1') && (show == '2'))
      contentsHeight += 160;
    prevShowBusiness = show;
    contentsElement = document.getElementById('contents');
    contentsElement.style.height = contentsHeight + 'px';
  }
  setCopyright();
}

function checkRequired()
{
  if (document.getElementById)
  {
    thePointer = document.getElementById('businessType');
    thePointerOther = document.getElementById('businessTypeOther');

    if (otherClass == "")
      otherClass = thePointerOther.className;

    if (thePointer.value == "other")
    {
      thePointerOther.className = otherClass;
      thePointerOther.className = thePointerOther.className.replace("optional","required");
    }
    else
      thePointerOther.className = "optional";
  }
}

function showText( theText )
{
  if (document.getElementById)
  {
    thePointer = document.getElementById(theText+'Text');
    thePointer.style.visibility = 'visible';
    thePointer = document.getElementById(theText+'Image');
    thePointer.style.visibility = 'hidden';
  }
}

function hideText( theText )
{
  if (document.getElementById)
  {
    thePointer = document.getElementById(theText+'Image');
    thePointer.style.visibility = 'visible';
    thePointer = document.getElementById(theText+'Text');
    thePointer.style.visibility = 'hidden';
  }
}