

/************************************************
DESCRIPTION: Validates required fields for order forms

PARAMETERS:
   form to validate

RETURNS:
   True if valid, alert window with errors if false.
*************************************************/
function validate_journal(frm) {

	// Set variables
	var isErr = false; // for any type of error
	var errMessage = ""; // 
	var errField = ""; // if there is an error, focus on the first required form field error
	
	// Get form values
	var firstname = frm.firstname.value;
	var lastname = frm.lastname.value;
	var address1 = frm.address1.value;
	var city = frm.city.value;
	var state = frm.state.value;
	var postalcode = frm.postalcode.value;
	
	
	if (validateIsEmpty(firstname)) {
		isErr = true;
		errMessage = errMessage + "Please enter First Name\r\n";
		if (errField.length <= 0) errField = "firstname";
	}
	if (validateIsEmpty(lastname)) {
		isErr = true;
		errMessage = errMessage + "Please enter Last Name\r\n";
		if (errField.length <= 0) errField = "lastname";
	}	
	if (validateIsEmpty(address1)) {
		isErr = true;
		errMessage = errMessage + "Please enter an Address\r\n";
		if (errField.length <= 0) errField = "address1";
	}
	if (validateIsEmpty(city)) {
		isErr = true;
		errMessage = errMessage + "Please enter a City\r\n";
		if (errField.length <= 0) errField = "city";
	}
	if (validateIsEmpty(state)) {
		isErr = true;
		errMessage = errMessage + "Please enter a State\r\n";
		if (errField.length <= 0) errField = "state";
	}
	if(validateIsEmpty(postalcode)) {  // postalcode field is empty
		isErr = true;
		errMessage = errMessage + "Please enter ZIP Code\r\n";
		if (errField.length <= 0) errField = "postalcode";
	} else {
		postalcode = postalcode.trim(); // remove whitespace around value
		if (!validateUSZip(postalcode)) { // Check zip code is in valid format
			isErr = true;
			errMessage = errMessage + "Please enter a valid ZIP Code in 5 digit or zip+4 format.\r\n";
			if (errField.length <= 0) errField = "postalcode";
		}
	}	
	
	if (isErr) {
		alert(errMessage);
		eval("frm." + errField + ".focus()");	
		return false;
	}
	return true;
}

/************************************************
DESCRIPTION: Validates that a string is a United
  States zip code in 5 digit format or zip+4
  format. The following are acceptable:
  	12345
	12345-1234
	12345+1234
	12345 1234
	123451234

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
function validateUSZip(strValue) {
	if (strValue.search(/(^\d{5}$)|(^\d{5}-\d{4}$)|(^\d{5}\+\d{4}$)|(^\d{5}\W+\d{4}$)|(^\d{9}$)/) == -1) return false;
	return true;
}

/************************************************
DESCRIPTION: Checks if a string is empty

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid (empty), otherwise false (not empty).
*************************************************/
function validateIsEmpty(strValue) {
	strValue = strValue.trim();
	if(strValue.length > 0) return false;
	return true;
}

/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
function trimString (strValue) {
	strValue = this != window? this : strValue;
	return strValue.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
