// validates that the field value string has one or more characters in it
function isSet(elem) {
  var str = elem.value;
  var re = /.+/;
  if(!str.match(re)) {
    return false;
  }
  return true;
}

//validates that the entry is a positive or negative number
function isNumber(elem) {
  var str = elem.value;
  var re = /^[-]?\d*\.?\d*$/;
  str = str.toString();
  if (!str.match(re)) {
    return false;
  }
  return true;
}

// validates that the entry is formatted as an e-mail address
function isEmailValid(elem) {
  var str = elem.value;
  var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
  if (!str.match(re)) {
    return false;
  }
  return true;
}

// validate that the user made a selection, skips first Item
function isSelectedDropDown(elem) {
  if (elem.selectedIndex == 0) {
    return false;
  }
  return true;
}

function isSelected(elem) {
  for (i=0; i < elem.options.length; i++) {
    if (elem.options[i].selected) {
      return true;
    }
  }
}

// validate that the user made a selection
function isChecked(elem) {
  return elem.checked;
}

// validate that the user has checked one of the radio buttons
function isRadioChecked(setName, radioName) {
  var radios = document.getElementById(setName);
  if (radios) {
    var inputs = radios.getElementsByTagName('input');
    if (inputs) {
      for (var i = 0; i < inputs.length; ++i) {
        if (inputs[i].type == 'radio' && inputs[i].name == radioName) {
          if (inputs[i].checked) {
            return true;
          }
        }
      }
    }
  }
  return false;
}

function getCheckedRadio(setName, radioName) {
  var radios = document.getElementById(setName);
  if (radios) {
    var inputs = radios.getElementsByTagName('input');
    if (inputs) {
      for (var i = 0; i < inputs.length; ++i) {
        if (inputs[i].type == 'radio' && inputs[i].name == radioName) {
          if (inputs[i].checked) {
            return inputs[i].value;
          }
        }
      }
    }
  }
  return 'Not Set';
}

function setFocus(elem) {
  elem.focus();
}

// Vailidate the submitted form
function validateForm(theForm) {

  if (theForm.BillEmail.value == "") {
    alert("Please enter a value in the \"Bill To Email Address\" field.");
    theForm.BillEmail.focus();
    return (false);
  }
    
  if (!isEmailValid(theForm.BillEmail)) {
      alert("You have entered an invalid \"Bill To Email Address\". Your address should have this general\nformat: username\@domainname.com\n\nPlease verify your entry and try again.");
      theForm.BillEmail.focus();
      return (false);
  }

  if (theForm.ShipEmail.value != "") {
	  if (!isEmailValid(theForm.ShipEmail)) {
		  alert("You have entered an invalid \"SHIP TO\" email address. Your address should have this general\nformat: username\@domainname.com\n\nPlease verify your entry and try again.");
		  theForm.ShipEmail.focus();
		  return (false);
	  }
	  
  }
  
  return true;
  
}
