var compute_centered_window_options = function(width, height) {
  var left = parseInt((screen.availWidth/2) - (width/2));
  var top = parseInt((screen.availHeight/2) - (height/2));
  return "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top;
};

var open_inline_popup = function(url) {
  window.open(url, 'inlinepopup',
              compute_centered_window_options(780, 500) +
              ',toolbar=no,location=yes,directories=no,status=yes,' +
              'menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
};


// --------------------------------------------------------------------------------
var sync_address_details = function(event, ui){
  var input = event.target;
  var id    = $(input).attr("id");

  var elems = $.map(["state", "suburb", "postcode", "country"],
                    function(value, index){
                      var new_id = "#" + id.replace("suburb", value);
                      $(new_id).val(ui.item.id[value]);
                      return new_id;
                    });
  $(elems.join(",")).effect("highlight", {}, 2000);
  return false;
};


// --------------------------------------------------------------------------------
//
//
//
// All callback functions receive the following Object...
// {
// "id": [the ID of the first form element stipulated within the initialisation object],
// "date": [a Javascript Date Object representing the selected date or NULL if no date is selected],
// "dd": [the date part of the selected date or NULL if no date is selected],
// "mm": [the month part of the selected date or NULL if no date is selected],
// "yyyy": [the year part of the selected date or NULL if no date is selected],
// // NOTE: only the "redraw" callback gets the following additional parameters
// "firstDateDisplayed": [a String representing the YYYYMMDD value of the first date shown],
// "lastDateDisplayed": [a String representing the YYYYMMDD value of the last date shown]
// }
var get_day = function(d){
  var result = d.getDate();
  if (result < 10) {
    return "0" + result;
  }
  return result;
};
var get_month = function(d){
  var result = d.getMonth() + 1;
  if (result < 10) {
    return "0" + result;
  }
  return result;
};

var update_date_display = function(target_id, o){
  var d = new Date( o.date.getTime() - 86400000 );
  $('#'+target_id).text(get_day(d) + '/' + get_month(d) + '/' + d.getFullYear()).effect("highlight", {}, 2000);
  return false;
};

