// checks for a date in the format (m)m/(d)d/(yy)yy
function checkDate(field) {
	var value = field.value;
	// if no value, just return
	if (value.length == 0)
		return true;
	
	// expression for proper date format
	var dateFormat = /^(\d{1,2}\/){2}(\d{2}|\d{4})$/;
	
	// check if the value is in the proper format
	if (value.search(dateFormat) == -1)
		return false;
	
	// extract month, day, and year
	var slash1 = value.indexOf('/', 0);
	var slash2 = value.indexOf('/', slash1 + 1);
	
	var month = parseInt(value.substring(0, slash1), 10);
	var day = parseInt(value.substring(slash1 + 1, slash2), 10);
	var year = value.substring(slash2 + 1, value.length);
	// make year 4 digits if it is not
	if (year.length == 2)
		year = "20" + year;
	year = parseInt(year, 10);

	// check if the value is an actual date
	// set maximum days in month
	var maxDay = 31;

	if (month == 4 || month == 6 ||
			month == 9 || month == 11)
		maxDay = 30;
	else
	if (month == 2)
	{
		if (year % 4 > 0)
			maxDay =28;
		else
		if (year % 100 == 0 && year % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}
	
	// check month and day ranges - at this point year is irrelevant
	if (!checkRange(month, 1, 12) || !checkRange(day, 1, maxDay))
		return false;

	return true;
}

// check a value for proper time format (h)h:(m)m
function checkTime(field) {
	var value = field.value;
	// if no value, just return
	if (value.length == 0)
		return true;

	// regular expression for proper time format
	var timeFormat = /^\d{1,2}:\d{2}$/;
	
	// check for proper format
	if (value.search(timeFormat) == -1)
		return false;
	
	// extract hours and minutes
	var colon = value.indexOf(":", 0);
	
	var hour = value.substring(0, colon);
	var minute = value.substring(colon + 1, value.length);
	
	// make sure hour is between 0 and 23, minutes between 0 and 59
	if (!checkRange(hour, 0, 23) || !checkRange(minute, 0, 59))
		return false;
	
	return true;
}

// check a phone number for the format 123-456-7890 or 123-4567
function checkPhone(field) {
	var value = field.value;
	// if no value, just return
	if (value.length == 0)
		return true;
		
	// regular expression for phone number format
	var phoneFormat = /^(\d{3}-){1,2}\d{4}$/
	
	if (value.search(phoneFormat) == -1)
		return false;
	else
		return true;
}

// check for proper email format
function checkEmail(field) {
	var value = trim(field.value);
	// if no value, just return
	if (value.length == 0)
		return true;
	var emailFormat = /^[\w]+([\w]+|[\.-]+[^ ]+)*@[\w]+([\w]+|[\.-]+[^ ]+)*\.[a-zA-Z]+$/;

	if (value.search(emailFormat) == -1)
		return false;
	else
		return true; 
}

// check for zip code in the format 12345 or 12345-6789
function checkZip(field) {
	var value = field.value;
	// if no value, just return
	if (value.length == 0)
		return true;

	var zipFormat = /^\d{5}(-\d{4})?$/;
	
	if (value.search(zipFormat) == -1)
		return false;
	else
		return true;
}

// checks for social security number in the format 123-45-6789
function checkSSN(field) {
	var value = field.value;
	// if no value, just return
	if (value.length == 0)
		return true;
	
	var ssnFormat = /^\d{3}-\d{2}-\d{4}$/;
	
	if (value.search(ssnFormat) == -1)
		return false;
	else
		return true;
}

// checks a value and checks to see if it falls between the other two values (inclusive)
function checkRange(value, lower, upper) {
	if (value >= lower && value <= upper)
		return true;
	else
		return false;
}

// checks two fields to see if they are the same - useful for passwords
function compareFields(field1, field2) {
	if (field1.value == field2.value)
		return true;
	else
		return false;
}

// check to make sure a value is present in a form field
function checkRequired(field) {
	// text, password, textarea, file
	if (field.type == "text" || field.type == "password" || field.type == "textarea" || field.type == "file")
		if (field.value.length == 0)
			return false;
		else
			return true;
	
	// select box
	if (field.type == "select-one" || field.type == "select-multiple") {
		for (var i=0; i < field.length; i++)
			if (field.options[i].selected)
				return true;
	return false;
	}
	
	// radio or check box - need to handle both single and arrays
	if (field.type == "radio" || field.type == "checkbox") {
		if (field.checked) 
			return true;
		else
			return false;
	}
	if (field[0].type == "radio" || field[0].type == "checkbox") {
		for (var i=0;  i < field.length; i++)
			if (field[i].checked)
				return true;
	return false;
	}
}
// make sure a valid number is entered
function checkNumeric(field) {
	var value = field.value;
	// if no value, just return
	if (value.length == 0)
		return true;
	
	return !isNaN(Number(field.value.replace(/,/, '')));
	
}
// remove whitespace from the beginning of a string
function ltrim(s) {
	return s.replace(/^\s*/, "")
}
// remove whitespace from the end of a string
function rtrim(s) {
	return s.replace(/\s*$/, "");
}
// remove whitespace from the beginning and end of a string
function trim(s) {
	return rtrim(ltrim(s));
}



