var FIRE_ONLOAD_FUNCS = new Array();
function FireOnLoad(func)
{
  FIRE_ONLOAD_FUNCS.push(func);
}
function Start()
{
  for (var i = 0; i < FIRE_ONLOAD_FUNCS.length; i++)
    FIRE_ONLOAD_FUNCS[i]();
}
function Log(str)
{
  if (document.all) return;
  if (typeof(console) != "undefined")
    console.log(str);
}

var Global = {

  // used for genericForm submission
  submitFired: false,

  doLoad: function()
  {
  
    // in the case of the website being loaded within a frameset from another website, we can't access
    // anything in the frameset due to XSS protection
    try {
      if (window.parent && /\beditMode/.test(window.parent.document.body.className))
        this.loadLimeFloatingPropertyBar();
      this.doLimeIframe();
    }
    catch (e){
      //this.wrapInIframe();
    }
    
    this.doPromotions();
    this.doGenericForms();
      
    if (document.getElementById("housenumberbutton") != null)
      Spif.DOMEvents.attach(document.getElementById("housenumberbutton"), "click", this.getAddress);
      
    if (document.getElementById("country") != null)
			Spif.DOMEvents.attach(document.getElementById("country"), "change", this.showOrHideAddressButton, this);
  },
  
  loadLimeFloatingPropertyBar: function()
  {
    if (window.parent.Skin && window.parent.Skin.makeFloatingPropertyBarDraggable)
      window.parent.Skin.makeFloatingPropertyBarDraggable();
  },
  
  /**
   * If we're logged into Lime and we visit one of the site pages outside of the
   * Lime iframe, log out and redirect so we're not stuck with all the editmode crap
   * in the page.
  **/
  doLimeIframe: function()
  {
    if (top.window == window)
    {
      var els = document.getElementsByTagName("*");
      for (var i = 0; i < els.length; i++)
      {
        if (els[i].getAttribute('limeid'))
          document.location.href = "/lime/log-off.aspx";
      }
    }
  },
  
  doPromotions: function()
  {
    if (document.getElementById('promotions') && top.window == window)
    {
      var lis = document.getElementById('promotions').getElementsByTagName('li');
      for (var i = 0; i < lis.length; i++)
        Spif.DOMEvents.attach(lis[i], "click", this.doPromotionClick, this);
    }
  },
  
  doPromotionClick: function(evt)
  {
    for (var el = evt.subject; el.nodeName.toLowerCase() != "li"; el = el.parentNode)
      ; // do nothing    
    window.location.href = el.getElementsByTagName('a')[0].href;
  },
  
  doGenericForms: function(evt)
  {
    // Submit genericForm to an iframe for javascript-enabled browsers .. if it exists
    var genericForm = document.getElementById('genericForm');
    if (genericForm != null)
    {
      genericForm.setAttribute("target", "validator");
      Spif.DOMEvents.attach(genericForm, "submit", function(){Global.submitFired = true;});
    }
  },
  
  displayFormValidationConfirmation: function()
  {
    document.getElementById('validationConfirmation').style.display = 'block';
  },
  
  getAddress: function()
  {
    if (document.getElementById("zipcode").value != "" && document.getElementById("housenumber").value != "")
    {
      xmlhttp.request({
          url:                      "/Booking/GetAddress.aspx?date=" + new Date().getTime() + "&zipcode=" + document.getElementById("zipcode").value + "&housenumber=" + document.getElementById("housenumber").value,
          send:                     "",
          asynchronous:             true,
          processReqChangeFunction: "Global.setAddress",
          post:                     false
        });
    }
    else
    {
      alert(Resources.Messages.FillInHouseNumberAndZipCodeFirst);
    }
  },
  
  setAddress: function()
  {
    var result = xmlhttp.req["Global.setAddress"];
    if (result.readyState == 4)
    {
      if (result.status == 200)
      {
        var errorList = result.responseXML.getElementsByTagName("Errors").item(0).getElementsByTagName('Error');
        if (errorList.length > 0)
        {
          alert(Resources.Messages.ErrorGetAddress);
        }
        else
        {
          document.getElementById("street").value = result.responseXML.getElementsByTagName("Street").item(0).firstChild.nodeValue;
          document.getElementById("city").value = result.responseXML.getElementsByTagName("Town").item(0).firstChild.nodeValue;
        }
      }
      else
      {
        alert(Resources.Messages.ErrorGetAddress);
      }
    }
  },
  
  showOrHideAddressButton: function()
  {
    var button = document.getElementById("housenumberbutton");
		if (button != null)
		{
		  var country = document.getElementById("country");
			if (country.options[country.selectedIndex].innerHTML == Resources.Global.Netherlands)
				Spif.ClassNameAbstraction.replace(button, "hidden", "shown");
			else
				Spif.ClassNameAbstraction.replace(button, "shown", "hidden");
		}
	},
	
	emailDetailPage: function(evt)
	{
	  var el = evt.srcElement || evt.target;

	  var name = document.getElementById("locationName").innerHTML
	  var href = document.getElementById("locationUrl").value
	  var html = [Resources.Global.EmailPageIntro];
	  html.push(name + " " + href);
	  
	  el.href += "?subject=" + escape(Resources.Global.EmailPageSubject);
	  el.href += "&body=" + escape(html.join("\n\r"));	
	},
	
	emailPage: function(evt)
	{
	  var el = evt.srcElement || evt.target;
	  var results = document.getElementById('results');
	  var h2s = results.getElementsByTagName('h2');
	  var html = [Resources.Global.EmailPageIntro];
	  for (var i = 0; i < h2s.length; i++)
	  {
	    var h2 = h2s[i];
	    var a = h2.getElementsByTagName('a')[0];
	    var href = a.href;
	    var name = a.innerHTML.replace(/^\s+|\s+$/g, "");
	    name = name.substring(0,1).toUpperCase() + name.substring(1, name.length);
	    html.push(name + ": " + href);
	  }
	  el.href += "?subject=" + escape(Resources.Global.EmailPageSubject);
	  el.href += "&body=" + escape(html.join("\n\r"));	  
	}

};

function openHyloPopup ()
{
  top.hylo_chatbot_openlink(this,310,300);
}


FireOnLoad(Global.doLoad.closure(Global));