
String.prototype.beginsWith = function(t, i) {
  if (i==false) {
    return (t == this.substring(0, t.length));
  } else {
      return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
  }
};


var STATE_BEGIN = 'begin';
var STATE_GRANT_MADE = STATE_BEGIN  + '_grantmade';
var STATE_GRANT_MADE_END = STATE_GRANT_MADE + '_end';
var STATE_INCPUB_CHECK = STATE_GRANT_MADE + "checkpub";
var STATE_INCPUB_FALSE = STATE_INCPUB_CHECK + "_false";
var STATE_PUBLICATION_DATE = STATE_INCPUB_CHECK + '_publicationdate';
var STATE_PUBLICATION_END = STATE_PUBLICATION_DATE + '_publicationend';
var STATE_CHECK1978 = STATE_BEGIN + '_1978';
var STATE_BEFORE1978 = STATE_CHECK1978 + '_before';
var STATE_AFTER1978 = STATE_CHECK1978 + '_after';
var STATE_BEFORE1939 = STATE_BEFORE1978 + '_before1939';
var STATE_AFTER1939 = STATE_BEFORE1978 + '_after1939';
var STATE_WAS_TERMINATED = STATE_BEFORE1939 + '_wasterminated';
var STATE_WASNT_TERMINATED = STATE_BEFORE1939 + '_wasntterminated';

var grantDate = null;
var isAuthor = false;
var publicationDate = null;
var createdBefore1978 = false;
var registrationDate = null;

var init = function() {
    $("input[type='text']").val('');

    // Handles changes to first grant date.
    $("#grantDate input[type='submit']").click(
      function() {
        var textField = $("#grantDate input[type='text']");
        grantDate = Date.fromString($(textField).val(), 'mm/dd/yyyy');
        if (grantDate) {
          $('span.grantDate').html(grantDate.asString('mmmm dd, yyyy'));
          if (grantDate < new Date(1978, 0, 1)) {
            $("#check1978 input[type='radio']").attr('checked', false);
            setState(STATE_CHECK1978);
          } else {
            $("#grantMade input[type='radio']").attr('checked', false);
            setState(STATE_GRANT_MADE);
          }
        } else {
          alert('invalid grant date - must be mm/dd/yyyy'); 
          setState(STATE_BEGIN);
          $(textField).focus();
          $(textField).select();
          return;
        }
      }
    );

    // Handles changes to the was work created after january 1st 1978 button
    $("#grantMade input[type='radio'][value='author']").click( function() {
          //setState(STATE_PUBLICATION_DATE);
          setState(STATE_INCPUB_CHECK);
          var textField = $("#publicationDate input[type='text']");
          isAuthor = true;
    });
    $("#grantMade input[type='radio'][value='other']").click( function() {
        setState(STATE_GRANT_MADE_END);
        isAuthor = false;
    });

    // Handles changes to the "included right of publication?" radio buttons.
    $("#includedPublication input[type='radio'][value='true']").click( function() {
          setState(STATE_PUBLICATION_DATE);
          var textField = $("#publicationDate input[type='text']");
          $(textField).val('');
          $(textField).focus();
    });
    $("#includedPublication input[type='radio'][value='false']").click( function() {
          var d1 = new Date(grantDate).addYears(35);
          var d2 = new Date(grantDate).addYears(40);
          $('#datePlusThirtyFive').html(d1.asString('mmmm dd, yyyy'));
          $('#datePlusForty').html(d2.asString('mmmm dd, yyyy'));
          setState(STATE_INCPUB_FALSE);
    });


    // Handles changes to publication date.
    $("#publicationDate input[type='submit']").click(
      function() {
        var textField = $("#publicationDate input[type='text']");
        publicationDate = Date.fromString($(textField).val(), 'mm/dd/yyyy');
        if (publicationDate) {
          $('span.publicationDate').html(publicationDate.asString('mmmm dd, yyyy'));
          var d1 = new Date(publicationDate).addYears(35);
          var d2 = new Date(grantDate).addYears(40);
          var terminationDateBeg = (d1 <= d2) ? d1 : d2;
          var terminationDateEnd = new Date(terminationDateBeg).addYears(5);
          $('span.terminationDateBeg').html(terminationDateBeg.asString('mmmm dd, yyyy'));
          $('span.terminationDateEnd').html(terminationDateEnd.asString('mmmm dd, yyyy'));
          setState(STATE_PUBLICATION_END);
        } else {
          alert('invalid publication date - must be mm/dd/yyyy'); 
          setState(STATE_BEGIN);
          $(textField).focus();
          $(textField).select();
          return;
        }
      }
    );

    // Handles changes to the was work created after january 1st 1978 button
    $("#check1978 input[type='radio'][value='yes']").click( function() {
          setState(STATE_AFTER1978);
          before1978 = false;
    });
    $("#check1978 input[type='radio'][value='no']").click( function() {
          var textField = $("#before1978 input[type='text']");
          $(textField).val('');
          $(textField).focus();
          setState(STATE_BEFORE1978);
          before1978 = true;
    });

    // Handles changes to registration date.
    $("#before1978 input[type='submit']").click(
      function() {
        var textField = $("#before1978 input[type='text']");
        registrationDate = Date.fromString($(textField).val(), 'mm/dd/yyyy');
        if (registrationDate) {
          $('span.registrationDate').html(registrationDate.asString('mmmm dd, yyyy'));
          if (registrationDate < new Date(1939, 9, 26)) {
            $("#checkTerminated input[type='radio']").attr('checked', false);
            setState(STATE_BEFORE1939);
          } else {
            var begin = new Date(registrationDate).addYears(56);
            var end = new Date(registrationDate).addYears(61);
            $('span.recaptureBegin').html(begin.asString('mmmm dd, yyyy'));
            $('span.recaptureEnd').html(end.asString('mmmm dd, yyyy'));
            setState(STATE_AFTER1939);
          }
        } else {
          alert('invalid registration date - must be mm/dd/yyyy'); 
          setState(STATE_BEGIN);
          $(textField).focus();
          $(textField).select();
          return;
        }
      }
    );

    // Handles changes to the "check terminated" radio buttons
    $("#checkTerminated input[type='radio'][value='yes']").click( function() {
      setState(STATE_WAS_TERMINATED);
    });
    $("#checkTerminated input[type='radio'][value='no']").click( function() {
      var begin = new Date(registrationDate).addYears(75);
      var end = new Date(registrationDate).addYears(80);
      $('span.recaptureBegin').html(begin.asString('mmmm dd, yyyy'));
      $('span.recaptureEnd').html(end.asString('mmmm dd, yyyy'));
      setState(STATE_WASNT_TERMINATED);
    });
}

var setState = function(state) {
  if (state.beginsWith(STATE_BEGIN)) {
    $('#grantDate').show();
  } else {
    $('#grantDate').hide();
    grantDate = null;
  }

  if (state.beginsWith(STATE_GRANT_MADE)) {
    $('#grantMade').show();
  } else {
    $('#grantMade').hide();
  }

  if (state.beginsWith(STATE_GRANT_MADE_END)) {
    $('#grantMadeEnd').show();
  } else {
    $('#grantMadeEnd').hide();
  }

  if (state.beginsWith(STATE_PUBLICATION_DATE)) {
    $('#publicationDate').show();
  } else {
    $('#publicationDate').hide();
  }

  if (state.beginsWith(STATE_INCPUB_CHECK)) {
    $('#includedPublication').show();
  } else {
    $('#includedPublication').hide();
  }

  if (state.beginsWith(STATE_INCPUB_FALSE)) {
    $('#didNotIncludePublication').show();
  } else {
    $('#didNotIncludePublication').hide();
  }

  if (state.beginsWith(STATE_PUBLICATION_END)) {
    $('#publicationEnd').show();
  } else {
    $('#publicationEnd').hide();
  }

  if (state.beginsWith(STATE_CHECK1978)) {
    $('#check1978').show();
  } else {
    $('#check1978').hide();
  }

  if (state.beginsWith(STATE_BEFORE1978)) {
    $('#before1978').show();
  } else {
    $('#before1978').hide();
  }

  if (state.beginsWith(STATE_AFTER1978)) {
    $('#after1978').show();
  } else {
    $('#after1978').hide();
  }

  if (state.beginsWith(STATE_BEFORE1939)) {
    $('#checkTerminated').show();
  } else {
    $('#checkTerminated').hide();
  }

  if (state.beginsWith(STATE_AFTER1939)) {
    $('#after1939').show();
  } else {
    $('#after1939').hide();
  }

  if (state.beginsWith(STATE_WAS_TERMINATED)) {
    $('#terminatedTrue').show();
  } else {
    $('#terminatedTrue').hide();
  }

  if (state.beginsWith(STATE_WASNT_TERMINATED)) {
    $('#terminatedFalse').show();
  } else {
    $('#terminatedFalse').hide();
  }

};

$().ready(function () {
    setState(STATE_BEGIN);
    init();
  });

