// ---------------------------------------------------------------------
//                             VALIDATION SCHEMA
// several functions that we can use in trying to validate our forms
// ---------------------------------------------------------------------

function isImageFile(val){ // checks to see if the file type is gif or jpg
	if (val.match(/^\S+\.(gif|jpg|jpeg)$/)) {
    return true;
  } else {
    return false;
  }
}

function isEmpty(val) { // check to see if input is whitespace only or empty
  if (val.match(/^\s+$/) || val == "") {
    return true;
  } else {
    return false;
  }
}
function isWholeNumber(val) { // check to see if input is number period not allowed
	if (val.match(/^[\d]+$/)) {
    return true;
  } else {
    return false;
  }
}

function isRealNumber(val) { // check to see if input is number period allowed
  if (isNaN(val)) {
    return false;
  } else {
    return true;
  }
}

function isAlphabetic(val) { // check to see if input is alphabetic
  if (val.match(/^[a-zA-Z\s]+$/)) {
    return true;
  } else {
    return false;
  }
}
function isAlphaNumeric(val) { // check to see if input is alphanumeric
  if (val.match(/^[\w\s]+$/)) {
    return true;
  } else {
    return false;
  }
}

function isName(val) { // check to see if input is words and numbers
  if (val.match(/^[a-zA-Z0-9\s.'-,&\/]+$/)) {
    return true;
  } else {
    return false;
  }
}
function isTitle(val) { // check to see if input is a valid title
  if (val.match(/^[a-zA-Z0-9\s.'-,&:%$?#\/]+$/)) {
    return true;
  } else {
    return false;
  }
}
function isAddress(val) { // check to see if input is a valid address
  if (val.match(/^[a-zA-Z0-9\s.'-,&#\/]+$/)) {
    return true;
  } else {
    return false;
  }
}
function isPostCode(val) { // check to see if input is a valid post code
  if (val.match(/^[A-Z0-9\s-]+$/)) {
    return true;
  } else {
    return false;
  }
}
function isPhoneNumber(val) {
  if (val.match(/^\(?\d{3}\)?\s|-\d{3}-\d{4}$/)) {
    return true;
  } else {
    return false;
  }
}
function isIntlPhoneNumber(val) {
  if (val.match(/^\d(\d|-){7,20}/)) {
    return true;
  } else {
    return false;
  }
}
function isWithinRange(val, min, max) { // check to see if value is within range
  if (val >= min && val <= max) {
    return true;
  } else {
    return false;
  }
}
function isEmailAddress(val) { // check to see if input is a valid email address
  if (val.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
    return true;
  } else { 
    return false;
  }
}
function isChecked(obj) { // check to see if form value is checked
  if (obj.checked) {
    return true;
  } else {
    return false;
  }
}
function isRadioSelected(obj) { // check to see if radio is checked
  var arr = document.getElementsByName(obj.name);
  for (var i=0; i<arr.length; i++) {
    if (arr[i].checked) {
      return true;
    }
  }
  // it has not returned true, so...
  return false;
}

function radioValue(obj) { // get radio value
  if (obj[0]) {
    for (var i=0; i<obj.length; i++) {
      if (obj[i].checked) {
        return obj[i].value;
      }
    }
  } else {
    if (obj.checked) {
      return obj.value;
    } else {
      return "";
    }
  }
  return "";
}

function checkedValue(obj) { //returns the value of a checkbox if it is checked
	if (obj.checked){
  	return obj.value
  } else {
  	return ""
  }
}

function listValue(obj) { //returns the value of the selected item in a list
	if(obj.selectedIndex!=-1){
		return obj.options [obj.selectedIndex].value
	} else {
		return ""
	}
}


function totalChecked(obj) { // limit checkboxes
  var total = 0;
  for (var i = 0; i < obj.length; i++)
  {
    if (obj[i].checked)
    {
      total++;
    }
  }
  return total;
}
function limitReached(obj,limit)
{
  var total = totalChecked(obj);
  if (total > limit)
  {
    return true;
  } else {
    return false;
  }
}
