// Inspired from code AT
// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this  header
// NS4-6,IE4-6
// Fade effect only in IE; degrades gracefully

var j = 0;
var preLoad = new Array();

// takes array of image urls
function setupImages()
{
  
  for (i = 0; i < arguments.length; i++)
  {
     preLoad[i] = new Image();     
     preLoad[i].src = arguments[i];
  }
  runSlideShow();
}

function runSlideShow()
{
   var slideShowSpeed = 5000
   var crossFadeDuration = 2   
   if(document.all)
   {
    document.images.display_image.style.filter="blendTrans(duration=2)";
    document.images.display_image.style.filter="blendTrans(duration=crossFadeDuration)";
    document.images.display_image.filters.blendTrans.Apply();
   }
   document.images.display_image.src = preLoad[j].src;
   if (document.all)
   {
    document.images.display_image.filters.blendTrans.Play();
   }
   j = j + 1;
   if (j > (preLoad.length-1))
   {
   j=0;
   }
   t = setTimeout('runSlideShow()', slideShowSpeed);
}

function showPrice(el)
{    
  var selected = el.options[el.selectedIndex].value;  
  if(selected.length != 0)
  {
    var price_el = document.getElementById(el.name + '_price');  
    requestPriceFromServer(selected, price_el);
  }
}

function showPenthousePrice()
{
  var price_el = document.getElementById('apmt_price');
  requestPriceFromServer('PENTHOUSE', price_el);
}

function highlightTab(el)
{
  var td_els = document.getElementsByTagName('td');
  for(var i = 0; i < td_els.length; i++)
  {
    if(el.parentNode == td_els[i])
    {
      td_els[i].className='menu-selected';
    }
    else if(td_els[i].className='menu-selected')
    {
      td_els[i].className='menu-not-selected';
    }
  }
}

function requestPriceFromServer(apmtSize, displayEl)
{
  var xmlHttp;
  try
  {  
    // Firefox, Opera 8.0+, Safari  
    xmlHttp=new XMLHttpRequest();  
  }
  catch (e)
  {  
    // Internet Explorer  
    try
    {    
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
    }
    catch (e)
    {    
      try
      {      
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
      }
      catch (e)
      {      
        alert("Your browser does not support AJAX!");      
        return false;      
      }    
    }  
  }
   var url = "/priceFetch?APMT_SIZE="+apmtSize;
   // this function handles the price sent from the server
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {      
      if(xmlHttp.responseText.indexOf('GB') != -1)
      {
        displayEl.value=xmlHttp.responseText+'£ per week';
      }
      else
      {
        displayEl.value=xmlHttp.responseText + ' per week';
      }
    }
  }
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function requestCountryFromServer(override)
{
  var xmlHttp;
  try
  {  
    // Firefox, Opera 8.0+, Safari  
    xmlHttp=new XMLHttpRequest();  
  }
  catch (e)
  {  
    // Internet Explorer  
    try
    {    
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
    }
    catch (e)
    {    
      try
      {      
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
      }
      catch (e)
      {      
        alert("Your browser does not support AJAX!");      
        return false;      
      }    
    }  
  }
   var url = "/country";
   if(override == true)
   {
     var country = getSelectedCountry();
     url += '?OVERRIDE_FLAG=TRUE&country='+country;
   }
   // this function handles the price sent from the server
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      // debug("reponse text=" + xmlHttp.responseText);
      updateCountry(xmlHttp.responseText);      
    }
  }
  //debug("url is:" + url);
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function updateCountry(country)
{
  var countryEl = document.getElementById('country');
  countryEl.value = country;
  updateFlag(countryEl.value);
}

function getSelectedCountry()
{
  var countryEl = document.getElementById('country');
  return countryEl.value;
}

function updateFlag(country)
{
 var flagEl = document.getElementById('flag');
 flagEl.src = '/img/flags/'+country+'.gif'
}

function onChange(el)
{
  updateFlag(el.value);
  requestCountryFromServer(true);
}

function debug(msg)
{
  // if window not open
  // open window with name debug
  // append msg on new line into window
  var msgElement = document.createElement("div");
  var msgNode = document.createTextNode(msg);
  msgElement.appendChild(msgNode);
  document.body.appendChild(msgElement);
}
