$(document).ready(function ()
{
  jQuery.validator.addMethod("no_default_text", function(value, element)
  {
    if(value == 'Add your personal message here.')
    {
      window.scrollTo(0, 400);
      return false;
    }
    return true;
  }, "Please add a message.");
  
  $('label.pretty').labelOver('over-apply');
  
  // My account shouldn't have this - you should be able to remove your last name
  if(!window.location.pathname.match('my_account'))
  {
    $('input:text').blur(function(i) {if(this.type == 'input'){return}; if (this.value == '') this.value = this.defaultValue;});
  }
  
  $('textarea').focus(function(i) {if (this.value == 'Type your personal message here') this.value = '';});
  //$('textarea').blur(function(i) {if (this.value == '') this.value = 'Type your personal message here';});
  
  $('a.remove-receiver').each(function(i) {this.onclick = removeMyLine});
  $('a.remove-contributor').each(function(i) {this.onclick = removeMyLine});
  
  $('form').each(
    function(i)
    {
      try
      {
        $(this).validate();
      } catch (e)
      {
        //alert("Error occured\n" + 'Error: ' + e.name + '\nMessage: ' + e.message + '\nObject' + this);
      }
    }
    );

  // Ensure we have been given a valid date.
  jQuery.validator.addMethod("valid_date", function(value)
  {
    if (value != undefined)
    {
      var match = value.match(/^(\d\d)?\/(\d\d)?\/(\d\d\d\d)$/);
      if (match != null)
      {
        var md = new Array();
        md['01'] = md['03'] = md['05'] = md['07'] = md['08'] = md['10'] = md['12'] = 31;
        md['04'] = md['06'] = md['09'] = md['11'] = 30;
        md['02'] = (match[3] % 4 == 0) ? 29 : 28; // handle February for leap years

        if (match[1] <= md[match[2]])
        { // day given is less than or equal to max days in month
          return true;
        }
      }
    }
    return false;
  }, "Please add a valid date (in DD/MM/YYYY format).");
});
