/* This depends on:
    jquery.validate.js
    jquery.metadata.js
    jquery.maskedinput-1.2.2.js
*/
  
(function($) {

  function title_to_value() {
    input_field = $(this);
    if (input_field.attr("value") == input_field.attr("title")) {
      input_field.attr("value", '');
    }
  }

  function reset_to_value() {
    input_field = $(this);
    if (!input_field.attr("value")) {
      input_field.attr("value", input_field.attr("title"));
    }
  }


  // grab the text from the label with the same name
  $.fn.sink_label = function() {
    return this.each(function() {
      var input_field = $(this);

      // empty the field on click if it has default value
      input_field.bind("focus", title_to_value);
      // reset the field to default value on blur if it is empty
      input_field.bind("blur", reset_to_value);

      // empty the field if the same value as title before submitting
      $("form:has(input[name='"+input_field.attr("name")+"'])").bind('submit', title_to_value);

      // reset the field to the title
      $("form:has(input[name='"+input_field.attr("name")+"'])").bind('reset', function() {
        input_field.attr("value", input_field.attr("title"));
        return false;
      });

      var sink_label_name = input_field.attr("name");
      sink_label_name = "label[for='"+sink_label_name+"']";
      input_field.attr("value", $(sink_label_name).html());
      input_field.attr("title", $(sink_label_name).html());

      $(sink_label_name).remove();
    });
  };

  // set the value from the title
  $.fn.placeholder = function() {
    return this.each(function() {
      var input_field = $(this);

      // empty the field on click if it has default value
      input_field.bind("focus", title_to_value);

      // reset the field to default value on blur if it is empty
      input_field.bind("blur", reset_to_value);

      // empty the field if the same value as title before submitting
      $("form:has(input[name='"+input_field.attr("name")+"'])").bind('submit', title_to_value);

      // reset the field to the title
      $("form:has(input[name='"+input_field.attr("name")+"'])").bind('reset', function() {
        input_field.attr("value", input_field.attr("title"));
        return false;
      });

      input_field.attr("value", input_field.attr("title"));
    });
  };

    



})(jQuery);

$(document).ready(function() {
  /* not using these yet...
  $.validator.addMethod("phone", function(value, element) {
    return this.optional(element) || /^\s*([\(]?([0-9]{3})[\)]?)?[ \.\-]*([0-9]{3})[ \.\-]*([0-9]{4})\s*$/.test(value);
  }, "must at least have 7 digits and no alpha characters.");

  $.validator.addMethod("zip", function(value, element) {
    return this.optional(element) || /^\s*\d{5}(-\d{4})?\s*$/.test(value);
  }, "12345-1234 or 12345");

  $.validator.addMethod("max10", function(value, element, params) {
    return this.optional(element) || value == params[0] + params[1];
  }, "incorrect...");

  */
  $(".valid-form").validate();
  /*
  $(".valid-form .date.mask").mask("99/99/9999");

  $(".valid-form .phoneUS.mask").mask("(999) 999-9999");
  */
  $(".js_sink_label").sink_label();
  /*
  $(".js_placeholder").placeholder();
  */
});

