/**
 * Compare the specified date to the current date
 *
 * Returns false if it is less
 *
 */
function compareToCurrDate(year,month,day) {

    var eDate = new Date();
    eDate.setFullYear(year,month,day);

    var cDate = new Date();

    if (eDate.getTime() < cDate.getTime()) {
        return confirm("Are you sure you want to add date in the past?");
    } else {
        return true;
    }
}

/**
 *
 */
function donothing(event) {
  event.preventDefault();
}

/**
 * Don't allow users to post content more than once
 * Disable form fields after submit has been clicked
 * Note: we don't want to disable checkboxes, just make them look disabled
 *       but we do want to disable select drop downs
 */
$(document).ready(function() {
  $('form').not(':has(input.autocomplete)').each( function () {
    var form = $(this);

    var method = form.attr('method') || 'get';

    // don't bother for get requests like google searches
    // unless it is a unitis form
    if (method != 'post' && !form.hasClass('unitis-form')) {
      return true;
    }

    var no_redirect_button = false;

    form.find('input:submit').click(function() {

      // If the submit button has no redirect, don not hide other buttons
      if ($(this).hasClass('no-redirect-button')) {
        no_redirect_button = true;
        return true;
      }

      // hide other submit buttons
      form.find('input:submit').not(this).hide();
    });

    form.submit(function() {

      // Reset the connection - fixes safari upload issues
      // @todo: make this its own behaviour
      $.ajax({
        url: Drupal.url('uofc_close_connection'),
        async: false,
      });

      // If the submit button has no redirect, ignore everything and continue on
      if (no_redirect_button == true) {
        return true;
      }

      // If any elements don't want us to submit, then don't
      var toSubmit = true;
      form.find('input').each(function() {
        var ret = $(this).triggerHandler('toSubmit');
        if (ret == false) {
          toSubmit = false;
        }
      });
      if (!toSubmit) {
        return false;
      }

      // hide cancel links
      form.find('a.uofc-cancel-link').hide();

      // replace submit buttons with regular buttons that say 'please wait'
      form.find('input:submit').each(function () {
        var mytype  = 'type="button"';
        var myid    = 'id="'+$(this).attr('id')+'"';
        var myclass = 'class="'+$(this).attr('class')+' loading"';
        var mystyle = 'style="'+$(this).attr('style')+'"';
        var myvalue = 'value="Please wait ..."';
        var button  = '<input '+mytype+' '+myid+' '+myclass+' '+mystyle+' '+myvalue+' />';
        $(button).insertAfter(this);
        // hide the submit button
        $(this).removeAttr('id');
        $(this).hide();
      });

      // disable form fields while we wait
      form.find(':input:not(:submit)').disable();
      form.find('.form-item a').disable();
    });
  });
})

jQuery.fn.extend({
  disable: function () {
    $(this).fadeTo(1, 0.50).bind('click', donothing).bind('keydown', donothing);
  },
  enable: function () {
    $(this).fadeTo(1, 1.00).unbind('click', donothing).unbind('keydown', donothing);
  }
});

/**
 * todo: use jQuery
 */
function togglePublication(a) {
  var e=document.getElementById(a);
  if (!e)return true;
  if (e.style.display=="none") {
    e.style.display="block"
  } else {
    e.style.display="none"
  }
  return true;
}

