Event.observe(document, "dom:loaded", function() {
  var info = {
    have_country: false,
    country: "",
    busy: false,
    form: $("highbury_form"),
    country_ele: $("highbury_country")
  };

  $$(".need_country").each(function(ele) {
    ele.observe("click", function(ev, info) {
      ev.stop();

      info.link = this;
      $("highbury_wrapper").style.display = "block";
    }.bindAsEventListener(ele, info));
  }.bind(info));

  $("highbury_country").observe("change", function() {
    var country = this.country_ele.value;
    if (country == this.country)
      return;

    this.country = country;
    $("highbury_country_bad").style.display = "none";
    $("highbury_country_good").style.display = "none";
    if (info.timer) {
      window.clearTimeout(info.timer);
      delete info.timer;
    }
    if (country == "US" || country == "JP" || country == "CA") {
      $("highbury_country_bad").style.display = "block";
      info.timer = window.setTimeout(function() {
	$("highbury_wrapper").style.display = "none";
      }.bind(info), 5000);
    }
    else {
      $("highbury_country_good").style.display = "block";
    }
  }.bind(info));
  $("highbury_agree").observe("click", function(ev) {
    ev.stop();
    this.busy = true;
    popup(this.link, "BTIMEGM2011Webcast");
    $("highbury_form").request({
      onSuccess: function() {
	// uncover the content
	$("highbury_wrapper").style.display = "none";
      }.bind(this),
      onFailure: function() {
	$("highbury_error").update("Unexpected error saving information");
	$("highbury_error").style.display = "block";
	this.busy = false;
	$("highbury_busy").style.display = "none";
	this.update_valid();
      }.bind(this)
    });
  }.bind(info));
  $("highbury_not_agree").observe("click", function(ev) {
    ev.stop();
    $("highbury_wrapper").style.display = "none";
  });
});

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + "; path=/";
document.cookie=c_name + "=" + c_value;
}
