function do_onError(form_object, input_object, object_value, error_message) {
  alert(error_message);
  return false;
}

function chk_hasValue(obj, obj_type) {
  if (obj_type == "TEXT") {
    if (obj.value.length == 0)
      return false;
    else
      return true;
  } else if (obj_type == "HIDDEN") {
    if (obj.value.length == 0)
      return false;
    else
      return true;
  } else if (obj_type == "SELECT") {
    for (i=0; i < obj.length; i++) {
      if (obj[i].selected) {
        if (obj[i].value != "")
          return true;
        }
      }
      return false;
  } else if (obj_type == "RADIO") {
    for (i=0; i < obj.length; i++) {
      if (obj[i].checked)
        return true;
      }
    return false;
  } else if (obj_type == "CHECKBOX") {
    for (i=0; i < obj.length; i++) {
      if (obj[i].checked)
        return true;
      }
    return false;
  }  
}

function checkYes(obj) {
    for (y=0; y < obj.length; y++) {
      if (obj[y].checked) {
        if (obj[y].value == 0)
          return false;
      }
    }
    return true;
}

function billingAddress(obj) {
    for (b=0; b < obj.length; b++) {
      if (obj[b].checked) {
        if (obj[b].value == 1)
          return false;
      }
    }
    return true;
}

// check to make sure numeric values are numeric, includes hyphen and period to allow for phone and zip
function IsNumeric(strString) {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

	   if (strString.length == 0) return false;
	
		   //  test strString consists of valid characters listed above
		   for (i = 0; i < strString.length && blnResult == true; i++) {
			  strChar = strString.charAt(i);
			  if (strValidChars.indexOf(strChar) == -1) {
				 blnResult = false;
			  }
		   }
	   return blnResult;
}

function checkmyForm(theForm) {

// if same as organization radio button is "no", then force a personal address

    //if (!chk_hasValue(theForm.theOrg, "TEXT")) {
    //  if (!do_onError(theForm, theForm.theOrg, theForm.theOrg.value, "You must enter an ORGANIZATION NAME to continue.")) {
    //  theForm.theOrg.focus();
    //  return false;
    //  }
    //}

    if (!chk_hasValue(theForm.mail_addr, "TEXT")) {
      if (!do_onError(theForm, theForm.mail_addr, theForm.mail_addr.value, "You must enter an ADDRESS to continue.")) {
      theForm.mail_addr.focus();
      return false;
      }
    }
  
    if (!chk_hasValue(theForm.mail_city, "TEXT")) {
      if (!do_onError(theForm, theForm.mail_city, theForm.mail_city.value, "You must enter a CITY to continue.")) {
      theForm.mail_city.focus();
      return false;
      }
    }
    
    if (!chk_hasValue(theForm.mail_state, "TEXT")) {
      if (!do_onError(theForm, theForm.mail_state, theForm.mail_state.value, "You must enter a STATE to continue.")) {
      theForm.mail_state.focus();
      return false;
      }
    }
    
    if (!chk_hasValue(theForm.mail_zip, "TEXT")) {
      if (!do_onError(theForm, theForm.mail_zip, theForm.mail_zip.value, "You must enter a ZIP to continue.")) {
      theForm.mail_zip.focus();
      return false;
      }
    }
    
		if (IsNumeric(theForm.mail_zip.value) == false) {
		  alert("Please check - ORGANIZATION MAILING ZIP is a numeric value!");
		  theForm.mail_zip.focus();
		  return false;
		}    
    
  if (!checkYes(theForm.useUserAddr)) {
    if (!chk_hasValue(theForm.alt_addr, "TEXT")) {
      if (!do_onError(theForm, theForm.alt_addr, theForm.alt_addr.value, "You must enter a MAILING ADDRESS to continue.")) {
      theForm.alt_addr.focus();
      return false;
      }
    }
  
    if (!chk_hasValue(theForm.alt_city, "TEXT")) {
      if (!do_onError(theForm, theForm.alt_city, theForm.alt_city.value, "You must enter a MAILING CITY to continue.")) {
      theForm.alt_city.focus();
      return false;
      }
    }
    
    if (!chk_hasValue(theForm.alt_state, "TEXT")) {
      if (!do_onError(theForm, theForm.alt_state, theForm.alt_state.value, "You must enter a MAILING STATE to continue.")) {
      theForm.alt_state.focus();
      return false;
      }
    }
    
    if (!chk_hasValue(theForm.alt_zip, "TEXT")) {
      if (!do_onError(theForm, theForm.alt_zip, theForm.alt_zip.value, "You must enter a MAILING ZIP to continue.")) {
      theForm.alt_zip.focus();
      return false;
      }
    }
    
		if (IsNumeric(theForm.alt_zip.value) == false) {
		  alert("Please check - MAILING ZIP is a numeric value!");
		  theForm.alt_zip.focus();
		  return false;
		}    
  }
  
    
    if (!chk_hasValue(theForm.firstName, "TEXT")) {
      if (!do_onError(theForm, theForm.firstName, theForm.firstName.value, "You must enter a FIRST NAME to continue.")) {
      theForm.firstName.focus();
      return false;
      }
    }
    
    if (!chk_hasValue(theForm.lastName, "TEXT")) {
      if (!do_onError(theForm, theForm.lastName, theForm.lastName.value, "You must enter a LAST NAME to continue.")) {
      theForm.lastName.focus();
      return false;
      }
    } 
    
    if (!chk_hasValue(theForm.phone, "TEXT")) {
      if (!do_onError(theForm, theForm.phone, theForm.phone.value, "You must enter a PHONE to continue.")) {
      theForm.phone.focus();
      return false;
      }
    }
    
    if (!chk_hasValue(theForm.email, "TEXT")) {
      if (!do_onError(theForm, theForm.email, theForm.email.value, "You must enter a primary EMAIL address to continue.")) {
      theForm.email.focus();
      return false;
      }
    }
		if (chk_hasValue(theForm.email, "TEXT")) {		
			if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.email.value)){
				alert("Invalid E-mail Address! Please re-enter.")
				return false;
			}	
		}
		
		
    
//    if (!chk_hasValue(theForm.tool_subj_key, "SELECT")) {
//      if (!do_onError(theForm, theForm.tool_subj_key, theForm.tool_subj_key.value, "You must select one or more SUBJECTS to continue.")) {
//      theForm.tool_subj_key.focus();
//      return false;
//      }
//    }
    
//    if (!chk_hasValue(theForm.gradeLevel, "CHECKBOX")) {
//      if (!do_onError(theForm, theForm.gradeLevel, theForm.gradeLevel.value, "You must select one or more GRADES to continue.")) {
//      // theForm.gradeLevel.focus();
//      return false;
//      }
//    }


    
    // this option is not on all forms (pay only, no billing) 
    if (theForm.billTo) {
		if (!chk_hasValue(theForm.billTo, "TEXT")) {
		  if (!do_onError(theForm, theForm.billTo, theForm.billTo.value, "You must enter a BILL TO name to continue.")) {
		  theForm.billTo.focus();
		  return false;
		  }
		}
	
		if (!chk_hasValue(theForm.billingAddress, "TEXT")) {
		  if (!do_onError(theForm, theForm.billingAddress, theForm.billingAddress.value, "You must enter a BILLING ADDRESS to continue.")) {
		  theForm.billingAddress.focus();
		  return false;
		  }
		}
	  
		if (!chk_hasValue(theForm.billingCity, "TEXT")) {
		  if (!do_onError(theForm, theForm.billingCity, theForm.billingCity.value, "You must enter a BILLING CITY to continue.")) {
		  theForm.billingCity.focus();
		  return false;
		  }
		}
		
		if (!chk_hasValue(theForm.billingState, "TEXT")) {
		  if (!do_onError(theForm, theForm.billingState, theForm.billingState.value, "You must enter a BILLING STATE to continue.")) {
		  theForm.billingState.focus();
		  return false;
		  }
		}
		
		if (!chk_hasValue(theForm.billingZip, "TEXT")) {
		  if (!do_onError(theForm, theForm.billingZip, theForm.billingZip.value, "You must enter a BILLING ZIP to continue.")) {
		  theForm.billingZip.focus();
		  return false;
		  }
		}
		
		if (IsNumeric(theForm.billingZip.value) == false) {
		  alert("Please check - BILLING ZIP is a numeric value!");
		  theForm.billingZip.focus();
		  return false;
		}    
		
	  
  }


if ((theForm.billing) && (!chk_hasValue(theForm.billing, "SELECT"))) {
  if (!do_onError(theForm, theForm.billing, theForm.billing.value, "You must select a BILLING TYPE to continue.")) {
  theForm.billing.focus();
  return false;
  }
}

// if they select PO or Check, must include the number
if (theForm.billing) {
	billVal = theForm.billing.options[theForm.billing.selectedIndex].value;
	if ( ((billVal == "Purchase Order") || (billVal == "Check")) && (theForm.poChkNumber.value.length <= "1")) {
		if (!do_onError(theForm, theForm.poChkNumber, theForm.poChkNumber.value, "Since you selected the billing method of " + billVal + ", you must enter a PO/CHECK NUMBER to continue.")) {
			theForm.poChkNumber.focus();
			return false;
		}
	}
}

	return true;
}
