// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
	var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        alert("Please fill in the required field.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        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)) {
        alert("Enter only numbers into the field.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    }
    return true;
}
// validates that the entry is 16 characters long
function isLen16(elem) {
	var str = elem.value;
    var re = /\b.{16}\b/;
    if (!str.match(re)) {
        alert("Entry does not contain the required 16 characters.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validates that the entry is formatted as an e-mail address
function isEMailAddr(elem) {
	var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        alert("Verify the e-mail address format.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validate that the user made a selection other than default
function isChosen(select) {
    if (select.selectedIndex == 0) {
        alert("Please select a State from the list.");
        return false;
    } else {
        return true;
    }
}

// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    alert("Please select Commercial or Residential for your shipping address.");
    return false;
}

// validate that the user has checked one of the radio buttons
function isValidCheckbox(checkbox) {
    var valid = false;
    if (checkbox.checked) {
      return true;
    }
    alert("Please click on the Agreement checkbox.");
    return false;
}

function validatePwd(elem) {
  var invalid = " "; // Invalid character is a space
  var minLength = 4; // Minimum length
  var pw1 = document.registration_form.password.value;
  var pw2 = elem.value;
  // check for minimum length
  if (document.registration_form.password.value.length < minLength) {
    alert('Your password must be at least ' + minLength + ' characters long. Try again.');
    return false;
  }
  // check for spaces
  if (document.registration_form.password.value.indexOf(invalid) > -1) {
    alert("Sorry, spaces are not allowed.");
    return false;
  }
  else {
    if (pw1 != pw2) {
      alert ("You did not enter the same new password twice. Please re-enter your password.");
      return false;
    }
  return true;
  }
}

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}

// batch validation router
function validateForm(form) {
  if (isNotEmpty(form.email)) {
  if (isEMailAddr(form.email)) {
  if (isNotEmpty(form.password)) {
  if (isNotEmpty(form.password2)) {
  if (validatePwd(form.password2)) {
    if (isNotEmpty(form.fullname)) {
      if (isNotEmpty(form.company_name)) {
        if (isNotEmpty(form.address)) {
          if (isNotEmpty(form.city)) {
          if (isChosen(form.state)) {
          if (isNumber(form.zipcode)) {
        if (isNotEmpty(form.s_address1)) {
          if (isNotEmpty(form.s_city1)) {
          if (isChosen(form.s_state1)) {
          if (isNumber(form.s_zipcode1)) {
            if (isValidRadio(form.s_addtype1)) {
              if (isNotEmpty(form.phone)) {
                  return true;
              }
            }
          }
          }
          }
        }
          }
          }
          }
        }
      }
    }
  }
  }
  }
  }
  }
  return false;
}

// Rollover changes for input buttons (for <a> links see common/forms.css)
function liteOnFW(regform,regbutton) {
  window.document.forms[regform].elements[regbutton].style.color = "#dd0000";
  window.document.forms[regform].elements[regbutton].style.backgroundColor = "#ffeeee";
  window.document.forms[regform].elements[regbutton].style.borderColor = "#dd0000";
}

function liteOnBW(regform,regbutton) {
  window.document.forms[regform].elements[regbutton].style.color = "#dddddd";
  window.document.forms[regform].elements[regbutton].style.backgroundColor = "#363636";
  window.document.forms[regform].elements[regbutton].style.borderColor = "#363636";
}

function liteOff(regform,regbutton) {
  window.document.forms[regform].elements[regbutton].style.color = "";
  window.document.forms[regform].elements[regbutton].style.backgroundColor = "";
  window.document.forms[regform].elements[regbutton].style.borderColor = "";
}

