//=====================================================


function fixedPosition()
{
  var r = true;
  var agent   = navigator.userAgent.toLowerCase();
  if((agent.indexOf("msie 5")!=-1)||(agent.indexOf("msie 6")!=-1))
    r = false;
  return r;
}


//=====================================================


function popupDialog(o,d)
{
  // o - Object to display.
  // d - Display = true, Hide = false

  var obj = document.getElementById(o);
  if(d)
  {    
    var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
    var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
    var sl = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
    var st = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
    if(fixedPosition())
    {
      sl = 0;
      st = 0;
    }

    var ypos = (parseInt(st)+150)+'px';
    
    if(parseInt(ch) < 500)
      ypos = st+'px';
    obj.style.top = ypos;    
    obj.style.left = (((parseInt(cw)-500)/2)+parseInt(sl))+'px';
    greyout(true);
    obj.style.display = 'block';
  }
  else
  {
    greyout(false);
    obj.style.display = 'none';
  }
  
}


//=====================================================


function popupFooter(u,t)
{
  if(t == 'dialog')
    popupDialog(u,true);
  else
  {
    document.getElementById('popupfootertitle').innerHTML = t+'<input type="image" title="Close" src="img/popupClose.gif" class="popupclose" onClick="closeFooter();"/>';
    document.getElementById('popupfooterbody').innerHTML = '';
    makeHttpRequest('popup.php?a='+u,'popupfooterbody');
    popupDialog('popupfooter',true);
  }
  return false;
}

function closeFooter()
{
  popupDialog('popupfooter',false);
}


//=====================================================


var popupUrl;

function popupPage(d,t,u,w)
{
  // d - display = true, hide = false
  // t - title
  // u - url
  // w - ideal width

  var obj = document.getElementById('popupPage');
  var ifr = document.getElementById('popupPageIframe');

  if(d)
  {
  
    var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
    var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
    var sl = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
    var st = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;

    if(fixedPosition())
    {
      sl = 0;
      st = 0;
    }

    if(parseInt(ch) < 500)
    {
      obj.style.top = (parseInt(st)+10)+'px';
      ifr.style.height = Math.max((parseInt(ch)-20),200)+'px';
    }
    else
    {
      obj.style.top = (parseInt(st)+25)+'px';
      ifr.style.height = (parseInt(ch)-100)+'px';
    }

    if((!w)|(parseInt(cw) < w))
    {
      obj.style.left  = (parseInt(sl)+10)+'px';
      ifr.style.width = Math.max((parseInt(cw)-20),200)+'px';
    }
    else
    {
      obj.style.left  = (((parseInt(cw)-w)/2)+parseInt(sl))+'px';
      ifr.style.width = w+'px';
    }
    document.getElementById('popupPageTitle').innerHTML = t;
    greyout(true);
    obj.style.display = 'block';
    window.open(u,'popupPageIframe');
    popupUrl = u;
  }
  else
  {
    greyout(false);
    obj.style.display = 'none';
    window.open('img/loading.gif','popupPageIframe');
  }

}


function popupGo()
{
  window.open(popupUrl,'_blank');
  popupPage(false);
}


//=====================================================


function popupAccuweather(pc)
{
  pc = pc.replace(' ','%20');
  popupPage(true,'Weather (by AccuWeather.com)','http://www.accuweather.com/ukie/index-forecast.asp?postalcode='+pc, 1004);
}

function popupBbcweather(pc)
{
  pc = pc.replace(' ','');
  popupPage(true,'Weather','http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl?search_query='+pc+'&go.x=0&go.y=0'+pc, 810);
}

function popupGooglemap(pc)
{
  pc = pc.replace(' ','+');
  popupPage(true,'Map (by Google Maps)','http://maps.google.co.uk/maps?f=q&hl=en&geocode=&q='+pc+'&ie=UTF8&z=15');
}

function popupGoogledirections(dpc)
{
  var spc = getCookie('pc');
  if(spc)
    popupGoogledirections2(spc,dpc);
  else
  {
    document.getElementById('destination').value = dpc;
    popupPostCode();
  }
}

function popupGoogledirections2(spc,dpc)
{
  spc = spc.replace(' ','+');
  dpc = dpc.replace(' ','+');
  popupPage(true,'Directions (by Google Maps)','http://maps.google.co.uk/maps?f=d&hl=en&geocode=&saddr='+spc+'&daddr='+dpc+'&ie=UTF8&z=15');
}

function popupTraffic(url)
{
  popupPage(true,'Traffic Report',url, 1004);
}


//=====================================================


function addEvent(obj ,evt, fnc)
{
  if (obj.addEventListener)
    obj.addEventListener(evt,fnc,false);
  else if (obj.attachEvent)
    obj.attachEvent('on'+evt,fnc);
  else
    return false;
  return true;
}

function removeEvent(obj ,evt, fnc)
{
  if (obj.removeEventListener)
    obj.removeEventListener(evt,fnc,false);
  else if (obj.detachEvent)
    obj.detachEvent('on'+evt,fnc);
  else
    return false;
  return true;
}


//=====================================================


function appendElement(node,tag,id,htm)
{
  var ne = document.createElement(tag);
  if(id) ne.id = id;
  if(htm) ne.innerHTML = htm;
  node.appendChild(ne);
}


//=====================================================


function greyout(d,z)
{

  var wh = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
  var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
  var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
  var sh = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
  var sw = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;

  var obj = document.getElementById('greyout');

  if(!obj)
  {
    appendElement(document.body,'div','greyout');
    obj = document.getElementById('greyout');
    obj.style.position = 'absolute';
    obj.style.top = '0px';
    obj.style.left = '0px';
    obj.style.background = '#111';
    obj.style.opacity = '.5';
    obj.style.filter = 'alpha(opacity=50)';
  }
  if(d)
  {
    if(!z){ z = 50 }
    obj.style.zIndex = z;
    obj.style.height = Math.max(wh,Math.max(sh,ch))+'px';
    obj.style.width  = Math.max(sw,cw)+'px';
    obj.style.display = 'block';
    addEvent(window,'resize',greyoutResize);
  }
  else
  {
    obj.style.display = 'none';   
    removeEvent(window,'resize',greyoutResize);
  }
}

function greyoutResize()
{
  var wh = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
  var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
  var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
  var sh = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
  var sw = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;
  var obj = document.getElementById('greyout');
  obj.style.height = ch+'px';
  obj.style.width  = cw+'px';
  obj.style.height = Math.max(wh,Math.max(sh,ch))+'px';
  obj.style.width  = Math.max(sw,cw)+'px';
}


//=====================================================


var ukMapTimer;
var ukMapAniStep = 0;
var ukMapDirection = 0;

function ukMapExpand()
{
  i=4;
  if(ukMapDirection!=i)
  {
    if(ukMapTimer)
      clearInterval(ukMapTimer);
    ukMapDirection = i;
    var obj = document.getElementById('imgAni');
    obj.style.opacity = '1';
    obj.style.filter  = 'alpha(opacity=100)';
    document.getElementById('imgmapdiv').style.zIndex  = '30';
    document.getElementById('divchoose').style.display = 'none';
    ukMapTimer = setInterval('ukMapAnimation();',10);
  }
}
function ukMapCollapse()
{
  i=-4;
  if(ukMapDirection!=i)
  {
    if(ukMapTimer)
      clearInterval(ukMapTimer);
    ukMapDirection = i;
    ukMapTimer = setInterval('ukMapAnimation();',10);
  }
}

function ukMapClose(e)
{
  var obj = document.all ? event.srcElement : e.target;
  if((obj.id!='imgOvr')&(obj.id!='imgAni')&(obj.tagName!='AREA'))
  {
    removeEvent(document.body,'mouseout',ukMapClose);
    document.getElementById('divAni').style.display = 'block';
    document.getElementById('divMap').style.display = 'none';
    addEvent(document.getElementById('imgAni'),'mouseout',ukMapCollapse);
    addEvent(document.getElementById('imgAni'),'mouseover',ukMapExpand);
    ukMapCollapse();
  }
}

function ukMapAnimation(i)
{
  var obj = document.getElementById('imgAni');
  ukMapAniStep += ukMapDirection;
  if((ukMapAniStep<0)|(ukMapAniStep>66))
  {
    clearInterval(ukMapTimer);
    ukMapDirection=0;
    if(ukMapAniStep<0)
    {
      ukMapAniStep=0;
      obj.style.opacity = '.4';
      obj.style.filter  = 'alpha(opacity=40)';
      document.getElementById('imgmapdiv').style.zIndex  = '10';
      document.getElementById('divchoose').style.display = 'block';
    }
    else
    {
      ukMapAniStep=66;
      removeEvent(document.getElementById('imgAni'),'mouseout',ukMapCollapse);
      removeEvent(document.getElementById('imgAni'),'mouseover',ukMapExpand);
      document.getElementById('divAni').style.display = 'none';          
      document.getElementById('divMap').style.display = 'block';
      addEvent(document.body,'mouseout',ukMapClose);
    }
  }
  else
  {
    obj.style.height = Math.floor(116+(3.49*ukMapAniStep))+"px";
    obj.style.width  = Math.floor(98+(2.95*ukMapAniStep))+"px";
  }
}

//=====================================================

var imgSelected;

function ukMapSelect(a)
{
  document.getElementById('imgOvr').src = 'img/ukMap'+a+'.gif';
  imgSelected=a;
}

function ukMapDeselect(a)
{
  if(imgSelected==a)
    document.getElementById('imgOvr').src = 'img/ukMapT.gif';
}

//=====================================================
// Standard Cookie Functions 


function setCookie( name, value, expires, path, domain, secure )
{
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires )
    expires = expires * 1000 * 60 * 60 * 24;
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name+"="+escape( value ) +
    ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}
 
function getCookie( name )
{
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) 
    return null;
  if ( start == -1 )
    return null;
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 )
    end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}
 
function deleteCookie( name, path, domain )
{
  var today = new Date();
  today.setTime( today.getTime() );
  var expires_date = new Date( today.getTime() - 100 );
  setCookie(name,"",expires_date,path,domain,'');
}

//=====================================================

function setPostCode(pc,des,rpc)
{
  var expires;
  if(rpc.checked)
    expires = 9999;
  setCookie('pc',pc.value,expires,'/','','');
  popupDialog('postcodepop',false);
  if(des.value)
    popupGoogledirections2(pc.value,des.value);
}

function popupPostCode()
{
  var pc = getCookie('pc');
  if(pc==null)
    pc="";
  document.getElementById('postcode').value = pc;
  return popupDialog('postcodepop',true);
}


//=====================================================
// AJAX Request


function makeHttpRequest(url,obj)
{
  var httpRequest;
  try 
  {
    httpRequest = new XMLHttpRequest();  // Mozilla, Safari, etc
  }
  catch(trymicrosoft)
  {
    try
    {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(oldermicrosoft)
    {
      try
      {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(failed)
      {
        httpRequest = false;
      }
    }
  }
  if(!httpRequest)
  {
    alert('Your browser does not support Ajax.');
    return false;
  }

  httpRequest.onreadystatechange = function()
  {
    if(httpRequest.readyState == 4)
      if(httpRequest.status == 200)
        document.getElementById(obj).innerHTML = httpRequest.responseText;
      else
        document.getElementById(obj).innerHTML = 'Request Error: '+httpRequest.status;
  }
  httpRequest.open('GET',url,true);
  httpRequest.send(null);
 
}


//=====================================================

