$(document).ready(function() {
// js_sink_label
// grab the text from the label with the same name
  $(".js_sink_label").each(function() {
    var sink_label_name = $(this).attr("name");
    sink_label_name = "label[for='"+sink_label_name+"']"
    $(this).attr("value", $(sink_label_name).html());
    $(this).attr("title", $(sink_label_name).html());

    $(sink_label_name).remove();
  });
        
// js_placeholder
// set the value from the title
  $(".js_placeholder").each(function () {
    $(this).attr("value", $(this).attr("title"));
  });

// empty the field on click if it has default value
  $(".js_placeholder, .js_sink_label").click(function () {
    if ($(this).attr("value") == $(this).attr("title")) {
      $(this).attr("value", '');
    }
  });

// reset the field to default value on blur if it is empty
  $(".js_placeholder, .js_sink_label").blur(function () {
    if (!$(this).attr("value")) {
      $(this).attr("value", $(this).attr("title"));
    }
  });


  // validate the fields
  $(".js_form").submit(function() {
      var missed_items = new Array();
      var invalid_items = new Array();
      var error_items = new Array();

      function is_empty(input) {
        if (input.hasClass("js_placeholder") || input.hasClass("js_sink_label")) {
          if (input.attr("value") == input.attr("title")) {
            return true;
          }
        } else {
          if (!input.attr("value")) { // empty value
            return true;
          }
        }
        return false;
      }

      // check all required
      $(this).find(".js_require").each(function() {
        if (is_empty($(this))) {
          missed_items.push($(this));
        }
      });



      // check js_valid items
      $(this).find(".js_valid_email").each(function() {
        var valid_email =/\s*^.+@.+\..{2,3}\s*$/;
        if (!is_empty($(this)) && !valid_email.test($(this).attr("value"))) {
          // add this to the invalid_items
          var example = 'example@host.com'
          var a = new Array($(this), example);
          invalid_items.push(a);
        }
      });

      $(this).find(".js_number").each(function() {
        var valid_number =/^\s*\d+\s*$/;
        if (!is_empty($(this)) && !valid_number.test($(this).attr("value"))) {
          // add this to the invalid_items
          var example = 'any number'
          var a = new Array($(this), example);
          invalid_items.push(a);
        }
      });

      $(this).find(".js_valid_zip").each(function() {
        var valid_zip =/^\s*\d{5}(-\d{4})?\s*$/;
        if (!is_empty($(this)) && !valid_zip.test($(this).attr("value"))) {
          // add this to the invalid_items
          var example = '12345-1234 or 12345'
          var a = new Array($(this), example);
          invalid_items.push(a);
        }
      });

      $(this).find(".js_valid_phone").each(function() {
        var valid_phone =/^\s*([\(]?([0-9]{3})[\)]?)?[ \.\-]*([0-9]{3})[ \.\-]*([0-9]{4})\s*$/;
        if (!is_empty($(this)) && !valid_phone.test($(this).attr("value"))) {
          // add this to the invalid_items
          var example = 'must at least have 7 digits and no alpha characters.'
          var a = new Array($(this), example);
          invalid_items.push(a);
        }
      });

      // check items for errors
      $(this).find(".js_password").each(function() {
        if (!is_empty($(this)) && $(this).attr("value") != $(this).siblings(".js_match_password").attr("value")) {
          // add this to the invalid_items
          var example = "passwords don't match"
          var a = new Array($(this), example);
          error_items.push(a);
        }
      });

      // display errors and prevent form from submitting

      // first remove any previous error messages
      $(this).find(".js_error").remove();

      if (missed_items.length || invalid_items.length || error_items.length) {
        jQuery.each(missed_items, function() {
          $(this).after("<strong class='js_error'> Required </strong> ");
        });
        jQuery.each(invalid_items, function() {
          var example = $(this)[1];
          $(this)[0].after("<span class='js_error'><strong> Invalid </strong><br><em>valid format:"+example+"</em> <br></span>");
        });
        jQuery.each(error_items, function() {
          var example = $(this)[1];
          $(this)[0].after("<span class='js_error'><strong> Error </strong><br><em>cause of error:"+example+"</em> <br></span>");
        });
        
        return false;
      }

      // empty the fields for placeholders and sink_label if the same value as title before submitting
      $(this).find(".js_placeholder, .js_sink_label").each(function() {
        if ($(this).attr("value") == $(this).attr("title")) {
          $(this).attr("value", '');
        }
      });
      return true;
  });

  //return true;

});
