
/*
 * checks for a field to have a value
 */

function checkRequired(e) {
  valid = true;
  type = $(e).attr('type');
  /* check required radio */
  if (type == 'radio') {
    name = $(e).attr('name');
    v = $("input[name='" + name + "']:checked").val();
    if (!v) {
      $("." + name + "_error").addClass("error");
      valid = false;
    }
  }
  /* check required text */
  if (type == 'text') {
    name = $(e).attr('name');
    v = $(e).val();
    if (!v) {
      $("." + name + "_error").addClass("error");
      $("#" + name).addClass("input_error");
      valid = false;
    }
  }
  /* check required drop downs */
  if (type == 'select-one') {
    name = $(e).attr('name');
    v = $(e).val();
    if (!v) {
      $("." + name + "_error").addClass("error");
      $("#" + name).addClass("input_error");
      valid = false;
    }
  }
  return valid;
}

/*
 * checks for a field to be a valid email
 */

function checkEmail(e) {
	valid = true;
	type = $(e).attr('type');
	if (type == 'text') {
    	name = $(e).attr('name');
    	v = $(e).val();
    	if (v == '') {
    	} else {
	    	if (!isEmail(v)) {
				$("." + name + "_error").addClass("error");
				$("#" + name).addClass("input_error");
				valid = false;
			}
		}
	}
  	return valid;
}

/*
 * checks if v is an email
 */

function isEmail(v) {
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(v)) {
    return true;
  }
  else {
    return false;
  }
}

/*
 * checks if v is a number
 */
function isNumeric(v) {
  if (v.match(/^\d+$/) == null) return false;
  return true;
}

/*
 * clear form plugin
 */
$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form') return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea') this.value = '';
    else if (type == 'checkbox' || type == 'radio') this.checked = false;
    else if (tag == 'select') this.selectedIndex = -1;
  });
};