function checkSpecify(select) {
	var selectedValue = select.options[select.selectedIndex].value;
	var elem = document.getElementById("title_other");
	if(selectedValue == "other") {
		elem.style.visibility = "visible";
	} else {
		elem.style.visibility = "hidden";
	}
}

/**********************************
* GENERAL FORM VALIDATION METHODS *
***********************************/

function hover(hoverElement, className) {
	hoverElement.className = className;
}

function highlightError(element) {
	element.style.backgroundColor = '#f99';
}

function clearHighlight(element, original_color) {
	element.style.backgroundColor = original_color;
}

function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


/**
 * Validates an email address, allowing multiple subdomains appending email name
 * e.g. developer.eric_malone@opensolutions.com.tw
 */
function validEmail(email) {
    if(email.value)
        email = email.value;
    var emailRegExp = /^[\w\.-]+@[\w-]+(\.[\w-]{2,}){1,}$/;
    return emailRegExp.test(email);
}

/**
 * Validates a comma seperated list of email addresses, or a single email address 
 * (e.g. 'eric@gmail.com, bob@zimuta.org,  dave@yourmom.us')
 */
function validEmailList(list) {
   var listRegExp = /^([\w\.-]+@[\w-]+(\.[\w-]{2,}){1,}\b(\ *,\ *)?)+$/;
   return listRegExp.test(list);
}

/* ERIC I messed with your magic beans and split the password function
   for flexibility. Hugs and kisses -alex */
function validPassword(password) {
	if(password.value)
        password = password.value;
    var pwordChars = /^\w{5,}$/;
	return pwordChars.test(password);
}



/**
 * Validates a 3 digit phone number prefix (or area code) without spaces, dashes, or parens
 */
function validPhonePrefix(number) {
    var phonePrefix = /^\d{3}$/;
    return phonePrefix.test(number.value);
}

/**
 * Validates a 4 digit phone number suffix without spaces, dashes, or parens
 */
function validPhoneSuffix(number) {
    var phoneSuffix = /^\d{4}$/;
    return phoneSuffix.test(number.value);
}

/**
 * Validates a 10 digit phone number without spaces, dashes, or parens
 */
function validPhone(phone) {
    //a phone may be sent as a form item, or as the concatenated values of a form item
    if(phone.value)  
        phone = phone.value;
    var phoneRegExp = /^\d{10}$/;
    return phoneRegExp.test(phone);
}

/**
 * Checks to make sure the data entered into a form field is n address.
 */
function validAddress(address) {
	// RegEX for alphanumeric chars, spaces, and periods.
	var alphaChars = /[^a-zA-Z0-9_]{1,}$/;
	if(address.value == "" || address.value == null) return false;
	
	if (alphaChars.test(address.value)) return false;

	return true;
}

/**
 * Checks to make sure the data entered into a form field is a city name
 */
function validCity(city) {
	// RegEX for alpha chars, spaces, and periods.
	var alphaChars = /[^a-zA-Z_]{1,}$/;
	if(city.value == "") return false;
	
	if (alphaChars.test(city.value)) return false;

	return true;	
}

function validStateOrCountry(state) {
	var val = state.value;
	if(val == '0' || val == '1' || val == '-')
		return false;
	return true;
}

/**
 * Validates a 5 digit zip code without spaces, dashes, or parens
 */
function validZip(zip) {
    var zipRegExp = /^\d{5}$/;
    return zipRegExp.test(zip.value);
}

/**
 * Validates a numerical form item or field
 */
function validNumber(num) {
    
    if(num.value) 
        num = num.value;
    var numRegExp = /^[0-9]{1,}$/;
    return numRegExp.test(num);
}

function validFirstName(fname) {
	// RegEX for alpha chars, spaces, and periods.
	var alphaChars = /[^a-zA-Z_]{1,}$/;
	if(fname.value == "") return false;
	
	if (alphaChars.test(fname.value)) return false;

	return true;	
}

function validLastName(lname) {
	// RegEX for alpha chars, spaces, and periods.
	var alphaChars = /[^a-zA-Z_]{1,}$/;
	if(lname.value == "") return false;
	
	if (alphaChars.test(lname.value)) return false;

	return true;	
}

/**
 * Validates a full name such as "John Doe" and "Jill St. John" but not "Jill" or "Doe".
 * Even validates 'St. John S. Paul II'  !
 */
function validName(str) {
	var nameChars = /^([a-zA-Z]+\b\.?\ *){2,}$/;  /* tests for spaces and '.' in name */
	return nameChars.test(str);
}

//A valid price has at most 5 digits in the dollar value and/or at most 2 
//digits in the fractional value
function validPrice(price) {
	var priceChars = /^\d{0,5}(\.\d{2})?$/;
	return priceChars.test(price);
}

function validPosition(position) {
	// RegEX for alpha chars, spaces, periods and numbers.
	var alphaChars = /[^\w\d\ \.\']{1,}$/;
	if(position.value == "") return false;
	
	if (alphaChars.test(position.value)) return false;

	return true;	
}

function validDepartment(department) {
	// RegEX for alpha chars, spaces, periods and numbers.
	var alphaChars = /[^\w\d\ \.\']{1,}$/;
	if(department.value == "") return false;
	
	if (alphaChars.test(department.value)) return false;

	return true;	
}

function validTitle(title,other) {
	if((title.value == "other") && (!(validPosition(other))))
		return false;
	else
		return true;
}

function validHomePhone(form) {
	var passesTest = true;
	if(!validPhonePrefix(form.phone_area)) passesTest = false;
	if(!validPhonePrefix(form.phone_prefix)) passesTest = false;
	if(!validPhoneSuffix(form.phone_suffix)) passesTest = false;
	return passesTest;
}

function validCellPhone(form) {
	var passesTest = true;
	if(!validPhonePrefix(form.cell_phone_area)) passesTest = false;
	if(!validPhonePrefix(form.cell_phone_prefix)) passesTest = false;
	if(!validPhoneSuffix(form.cell_phone_suffix)) passesTest = false;
	return passesTest;
}

function validateRequestForm(form) {
	var original_color = '#ffc';
	var passesTest = true;
	var error = "";
	clearHighlight(form.title_other, original_color);
	if(!validTitle(form.title,form.title_other)) {
		highlightError(form.title_other);
		error += "Please enter a title.<br/>";
		passesTest = false;
		form.title.focus();
	}
	clearHighlight(form.first_name, original_color);
	if(!validFirstName(form.first_name)) {
		highlightError(form.first_name);
		error += "Please enter a valid first name.<br/>";
		passesTest = false;
		form.first_name.focus();
	}
	clearHighlight(form.last_name, original_color);
	if(!validLastName(form.last_name)) {
		highlightError(form.last_name);
		error += "Please enter a valid last name.<br/>";
		passesTest = false;
		form.last_name.focus();
	}
	clearHighlight(form.department, original_color);
	if(!validDepartment(form.department)) {
		highlightError(form.department);
		error += "Please eneter a valid department name.<br/>";
		passesTest = false;
		form.department.focus();
	}
	clearHighlight(form.city, original_color);
	if(!validCity(form.city)) {
		highlightError(form.city);
		error += "Please enter a valid city name.<br/>";
		passesTest = false;
		form.city.focus();
	}
	clearHighlight(form.state, original_color);
	if(!validStateOrCountry(form.state)) {
		highlightError(form.state);
		error += "Please select a state.<br/>";
		passesTest = false;
		form.state.focus();
	}clearHighlight(form.zip, original_color);
	if(!validZip(form.zip)) {
		highlightError(form.zip);
		error += "Please enter a valid ZIP code.<br/>";
		passesTest = false;
		form.zip.focus();
	}
	clearHighlight(form.email, original_color);
	if(!validEmail(form.email)) {
		highlightError(form.email);
		error += "Please enter a valid email address.<br/>";
		passesTest = false;
		form.email.focus();
	}
	clearHighlight(form.phone_area, original_color);
	clearHighlight(form.phone_prefix, original_color);
	clearHighlight(form.phone_suffix, original_color);
	if(form.phone_area.value != "" || form.phone_prefix.value != "" || form.phone_suffix.value != "") {
		if(!validHomePhone(form)) {
			highlightError(form.phone_area);
			highlightError(form.phone_prefix);
			highlightError(form.phone_suffix);
			error += "The phone number entered was not valid. ((xxx) xxx-xxxx)<br/>";
			passesTest = false;
			form.phone_area.focus();
		}
	}
	clearHighlight(form.cell_phone_area, original_color);
	clearHighlight(form.cell_phone_prefix, original_color);
	clearHighlight(form.cell_phone_suffix, original_color);
	if(form.cell_phone_area.value != "" || form.cell_phone_prefix.value != "" || form.cell_phone_suffix.value != "") {
		if(!validCellPhone(form)) {
			highlightError(form.cell_phone_area);
			highlightError(form.cell_phone_prefix);
			highlightError(form.cell_phone_suffix);
			error += "The cell phone number entered was not valid. ((xxx) xxx-xxxx)<br/>";
			passesTest = false;
			form.cell_phone_area.focus();
		}
	}
	if(!passesTest) document.getElementById('error').innerHTML = error;
	return passesTest;
}
