jQuery.ketchup

.validation('optional', 'This field is optional', function(form, el, value) {
  return true;
})

.validation('required', 'This field is required.', function(form, el, value) {
  var type = el.attr('type').toLowerCase();
  if(el.attr("data-placeholder"))
  	if(el.attr('data-placeholder') == value)
		return false;
  
  if(type == 'checkbox' || type == 'radio') {
    return (el.attr('checked') == true);
  } else {
    return (value.length != 0);
  }
})

.validation('minlength', 'This field must have a minimal length of {arg1}.', function(form, el, value, min) {
if(el.attr("data-placeholder"))
  	if(el.attr('data-placeholder') == value)
		return false;
  return (value.length >= +min);
})

.validation('maxlength', 'This field must have a maximal length of {arg1}.', function(form, el, value, max) {
  return (value.length <= +max);
})

.validation('rangelength', 'This field must have a length between {arg1} and {arg2}.', function(form, el, value, min, max) {
  return (value.length >= min && value.length <= max);
})

.validation('min', 'Must be at least {arg1}.', function(form, el, value, min) {
  return (this.isNumber(value) && +value >= +min);
})

.validation('max', 'Can not be greater than {arg1}.', function(form, el, value, max) {
  return (this.isNumber(value) && +value <= +max);
})

.validation('range', 'Must be between {arg1} and {arg2}.', function(form, el, value, min, max) {
  return (this.isNumber(value) && +value >= +min && +value <= +max);
})

.validation('number', 'Must be a number.', function(form, el, value) {
  return this.isNumber(value);
})

.validation('birthday', 'Must be a valid birthday.', function(form, el, value) {
  return /^\d{2}\/\d{2}\/\d{4}$/.test(value);
})

.validation('phone', 'Invalid phone number.', function(form,el,value) {
	return this.isPhone(value);
})

.validation('email', 'Must be a valid E-Mail.', function(form, el, value) {
  return this.isEmail(value);
})

.validation('url', 'Must be a valid URL.', function(form, el, value) {
  return this.isUrl(value);
})

.validation('username', 'Must be a valid username.', function(form, el, value) {
  return this.isUsername(value);
})

.validation('static', 'Must be {arg1}.', function(form, el, value, word) {
  return (el.val() == word);
})

.validation('contain', 'Must contain {arg1}', function(form, el, value, word) {
  return this.contains(value, word);
})

.validation('date', 'Must be a valid date.', function(form, el, value) {
  return this.isDate(value);
})

.validation('minselect', 'Select at least {arg1} checkboxes.', function(form, el, value, min) {
  return (min <= this.inputsWithName(form, el).filter(':checked').length);
}, function(form, el) {
  this.bindBrothers(form, el);
})

.validation('maxselect', 'Select not more than {arg1} checkboxes.', function(form, el, value, max) {
  return (max >= this.inputsWithName(form, el).filter(':checked').length);
}, function(form, el) {
  this.bindBrothers(form, el);
})

.validation('match', 'Must match {arg2}.', function(form, el, value, field, label) {
	if(value != $("#"+field).val())
		return false;
	else return true;
})

.validation('regex', 'Invalid entry.', function(form, el, value, regex) {
	if(regex.test(value))
		return true;
	else return false;
})

.validation('rangeselect', 'Select between {arg1} and {arg2} checkboxes.', function(form, el, value, min, max) {
  var checked = this.inputsWithName(form, el).filter(':checked').length;
  return (min <= checked && max >= checked);
}, function(form, el) {
  this.bindBrothers(form, el);
});
