Ajax.Responders.register({
    onCreate: function() {
      if (Ajax.activeRequestCount > 0) {
        if ($('busy')) {
          Element.show('busy');
        }
      }
    },

      onComplete: function() {
      if (Ajax.activeRequestCount == 0) {
        if ($('busy')) {
          Element.hide('busy');
        }
      }
    }
  });

/*** GENERAL UTILITY FUNCTIONS ***/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } 
  else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function trim(str) {  
  return str.replace(/^\s+|\s+$/g, '');  
}  

function submitForm(formName, url)
{
  document.forms[formName].action = url;
  document.forms[formName].submit();
}

function toggleVisibility(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'block')
    e.style.display = 'none';
  else
    e.style.display = 'block';
}

/* Used to highlight the current user after the page loads due to HTML caching via Rails */
function highlightCurrentUserRows(userId){  
  jQuery(document).ready(function(){
    jQuery('.userId' + userId).addClass("highlightRow");
  });
}

function ordinalize(integer) {
  var remainder = integer % 10;
  var mod_100 = integer % 100;

  if (remainder == 1 && mod_100 != 11) {
    return integer + "st";
  }
  else if (remainder == 2 && mod_100 != 12) {
    return integer + "nd";
  }
  else if (integer == 3 && mod_100 != 13) {
    return integer + "rd";
  }
  else {
    return integer + "th";
  }
}

function textCounter(field, cntfield, maxlimit) {
  if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
  // otherwise, update 'characters left' counter
  else
    cntfield.value = maxlimit - field.value.length;
}

/*** PRIZES ADMIN PAGE STUFF ***/

function setSubmitStatus(button_id, description_id) {
  if ($(description_id).value.match(/^\s*$/)) {
    $(button_id).disabled = true;
  }
  else {
    $(button_id).disabled = false;
  }
}

function ordinalizePrizesTable(table_id) {
  // The following doesn't work for some reason (the problem is :first-child):
  // var cells = $$('table#' + table_id + ' tbody tr td:first-child');

  var cells = $$('table#' + table_id + ' tbody tr td');
  for (var i = 0; i < cells.length; ++i) {
    if (i % 3 == 0) {
      cells[i].childNodes[0].data = ordinalize((i / 3) + 1);
    }
  }
}

/*** GOLFERS USED STUFF ***/
/* used to generate a unique id for each usage column */
var nextColumnNumber = 0;

function usageString(usage) {
  return usage[0] + " (" + usage[1] + ")";
}

function usageClass(usage) {
  return "golferUsage" + usage[1];
}

/* Adds a column to the golfer usages table. */
function addUsagesColumn(label, usages) {
  if (!usages || usages.length == 0) {
    return;
  }

  var table = $('golferUsagesTable');

  // add extra th to thead
  var colNum = nextColumnNumber++;
  var th = document.createElement('th');
  th.setAttribute("name", "usageColumn" + colNum);
  th.innerHTML = "<span>" + label + "</span><img align='right' src='/images/redCancel_16x16.png' onclick='javascript:removeUsagesColumn(" + colNum + ");'/>";
  table.tHead.rows[0].appendChild(th);

  // add extra rows if necessary
  var body = table.tBodies[0];
  var numRows = body.rows.length;
  var numCols = numRows > 0 ? body.rows[0].cells.length : 0;
  for (var i = numRows; i < usages.length; i++) {
    var row = document.createElement('tr');
    for (var j = 0; j < numCols; j++) {
      var td = document.createElement('td');
      td.appendChild(document.createTextNode(''));
      row.appendChild(td);
    }
    body.appendChild(row);
  }
  numRows = body.rows.length;

  // add extra td to rows
  for (i = 0; i < numRows; i++) {
    td = document.createElement('td');

    if (i < usages.length) {
      var text = i < usages.length ? usageString(usages[i]) : '';
      td.appendChild(document.createTextNode(text));
      td.className = usageClass(usages[i]);
    }
    else {
      td.appendChild(document.createTextNode(''));
    }

    table.tBodies[0].rows[i].appendChild(td);
  }
}

/* Removes a golfer usages column */
function removeUsagesColumn(colNum) {
  var table = $('golferUsagesTable');
  var headerRow = table.tHead.rows[0];

  var index = getIndexOfColumnToRemove(headerRow, colNum);
  if (index == -1) {
    return;
  }
  headerRow.removeChild(headerRow.cells[index]);

  var rows = table.tBodies[0].rows;
  for (var i = 0; i < rows.length; i++) {
    rows[i].removeChild(rows[i].cells[index]);
  }

  removeBlankRows(table);
}

function getIndexOfColumnToRemove(row, colNum) {
  var colName = "usageColumn" + colNum;
  for (var i = 0; i < row.cells.length; i++) {
    if (row.cells[i].getAttribute("name") == colName) {
      return i;
    }
  }
  return -1;
}

function removeBlankRows(table) {
  var rows = table.tBodies[0].rows;
  for (var i = rows.length - 1; i >= 0; i--) {
    if (isBlankRow(rows[i])) {
      table.tBodies[0].removeChild(rows[i]);
    }
  }
}

function isBlank(string) {
  return !string || string.match(/^\s*$/);
}

function isBlankRow(row) {
  var cells = row.cells;
  for (var i = 0; i < cells.length; i++) {
    if (!isBlank(cells[i].innerHTML)) {
      return false;
    }
  }
  return true;
}

/* Signup functions */
function validateHaveReadCheckbox(theForm) {
  if (theForm.haveRead.checked) {
    return true;
  }
  alert("Please confirm you have read the rules along with the terms & conditions by selecting the checkbox.")  
  return false;
}    

/* Admin functions */
function enableEntryFeeForEdit(team_id) {
  $("team_" + team_id + "_entry_fee_amount").disabled = false;
  $("team_" + team_id + "_entry_fee_amount").removeAttribute("readOnly");
  $("team_" + team_id + "_entry_fee_paid").disabled = false;
  $("team_" + team_id + "_entry_fee_paid").removeAttribute("readOnly");
  $("team_" + team_id + "_entry_fee_notes").disabled = false;
  $("team_" + team_id + "_entry_fee_notes").removeAttribute("readOnly");
  $("team_" + team_id + "_row").className = "entryFeesSelectedRow";
}