/******************************************************************************
* Adding a bookmark.
******************************************************************************/

function add_bookmark(txt,url,description) {
  var ver = navigator.appName;
  var num = parseInt(navigator.appVersion);

  if ((ver == 'Microsoft Internet Explorer') && (num >= 4)) {
   document.write('<a href="javascript:window.external.AddFavorite(\'' + url + '\',\'' + description + '\');">' + txt + '</a>');
  }
  else {
    // Do nothing
  } 
}

/******************************************************************************
* Disable right click script II (on images)- By Dynamicdrive.com
* For full source, Terms of service, and 100s DTHML scripts
* Visit http://www.dynamicdrive.com
******************************************************************************/

var disable_click_message = 'All images on the Anadune website are protected by copyright, and must not be reproduced without permission.';

function disable_click_function(e) {
  if (document.all) {
    if (event.button == 2 || event.button == 3) {
      if (event.srcElement.tagName == 'IMG') {
        alert(disable_click_message);
        return false;
      }
    }
  }
  else if (document.layers) {
    if (e.which == 3) {
      alert(disable_click_message);
      return false;
    }
  }
  else if (document.getElementById) {
    if (e.which == 3 && e.target.tagName == 'IMG') {
      alert(disable_click_message);
      return false;
    }
  }
}

function associate_images() {
  for(i = 0; i < document.images.length; i++) {
    document.images[i].onmousedown = disable_click_function;
  }
}

function disable_right_click() {
  if (document.all) {
    document.onmousedown = disable_click_function;
  }
  else if (document.getElementById) {
    document.onmouseup = disable_click_function;
  }
  else if (document.layers) {
    associate_images();
  }
}

/******************************************************************************
* Equalize the column heights of the navigation bar and the content.
******************************************************************************/

function equalize_column_heights() {
  var navbar = document.getElementById('navigation');
  var content = document.getElementById('content');

  if (navbar.offsetHeight < content.offsetHeight) {
    var spacer = document.getElementById('navigation-spacer');
    var spacer_height = spacer.offsetHeight;
    spacer_height += content.offsetHeight - navbar.offsetHeight;
    spacer.style.height = spacer_height + 'px';
  }
}

/******************************************************************************
* Get the scroll position.
******************************************************************************/

function getScrollingPosition() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getVerticalScrollingPosition() {
  var p = getScrollingPosition();
  return p[1];
}

function setVerticalScrollingPosition(y) {
  window.scrollTo(0,y);
}

/******************************************************************************
* Confirm the operation (delete / accept) on some things selected by checkboxes.
******************************************************************************/

function confirm_operate_checkboxes(operation, sing, plur, name) {
  var checkboxes = document.getElementsByName(name);

  var count = 0;

  for (var i = 0; i < checkboxes.length; ++i) {
    if (checkboxes.item(i).checked) { ++count; }
  }

  if (count == 0) {
    var text = 'Please select some ' + plur + ' before clicking ' + operation;
    alert(text);
    return false;
  }
  else if (count == 1) {
    var text = 'Are you sure you want to ' + operation + ' 1 ' + sing + '?';
    return confirm(text);
  }
  else {
    var text = 'Are you sure you want to ' + operation + ' ' + count + ' ' + plur + '?';
    return confirm(text);
  }
}

/******************************************************************************
* Script to be executed when page is resized.
******************************************************************************/

function on_resize() {
  equalize_column_heights();
}

/******************************************************************************
* Script to be executed when a form is submitted.
******************************************************************************/

function on_submit() {
  var ret = true;

  /* Write the vertical scroll into the form input, if there is one */
  if (document.getElementById('verticalscroll')) {
    document.getElementById('verticalscroll').value =
      getVerticalScrollingPosition();
  }

  return ret;
}

/******************************************************************************
* Script to be executed when an advert is deleted.
******************************************************************************/

function confirm_delete_advert() {
  return confirm('Are you sure you want to delete this advert?');
}

/******************************************************************************
* Script to be executed when an advert slot is deleted.
******************************************************************************/

function confirm_delete_advert_slot() {
  return confirm('Are you sure you want to delete this advert slot?');
}

/******************************************************************************
* Script to be executed when a user account is deleted.
******************************************************************************/

function confirm_delete_user(login) {
  return confirm('Are you sure you want to delete the ' + login +
                 ' member account?');
}

/******************************************************************************
* Script to be executed when photos are deleted.
******************************************************************************/

function confirm_delete_photos() {
  return confirm_operate_checkboxes('delete', 'photo','photos','photos[]');
}

/******************************************************************************
* Script to be executed when additions are deleted.
******************************************************************************/

function confirm_delete_additions() {
  return confirm_operate_checkboxes('delete', 'addition','additions','dataids[]');
}

/******************************************************************************
* Script to be executed when corrections are deleted.
******************************************************************************/

function confirm_delete_corrections() {
  return confirm_operate_checkboxes('delete', 'correction','corrections','dataids[]');
}

/******************************************************************************
* Script to be executed when shows are deleted.
******************************************************************************/

function confirm_delete_shows() {
  return confirm_operate_checkboxes('delete', 'show','shows','dataids[]');
}

/******************************************************************************
* Script to be executed when additions are accepted.
******************************************************************************/

function confirm_accept_additions() {
  return confirm_operate_checkboxes('accept', 'addition','additions','acceptids[]');
}

/******************************************************************************
* Script to be executed when corrections are accepted.
******************************************************************************/

function confirm_accept_corrections() {
  return confirm_operate_checkboxes('accept', 'correction','corrections','acceptids[]');
}

/******************************************************************************
* Script to be executed when shows are accepted.
******************************************************************************/

function confirm_accept_shows() {
  return confirm_operate_checkboxes('accept', 'show','shows','acceptids[]');
}

/******************************************************************************
* Script to be executed when page is loaded.
******************************************************************************/

function on_load() {
  disable_right_click();
  equalize_column_heights();
  window.onresize = on_resize;

  /* Apply the vertical scroll to the browser */
  if (document.getElementById('verticalscroll')) {
    var y = document.getElementById('verticalscroll').value;
    if (y > 0) { setVerticalScrollingPosition(y); }
  }
}

