/* All validation routines are here.  Alpha order.
 *
 */

// used for obv reasons 
function confirm_same_password(Obj1, Obj2, require_password)
{
	if (require_password && Obj1.value=='') {
		alert("You must specify a password");
		Obj1.focus();
		return false;
	}
	if (Obj1.value == Obj2.value)
		return true;
	else
	{
		alert("The passwords do not match, please reenter.")
		Obj1.value = '';
		Obj2.value = '';
		Obj1.focus();
		return false;	
	}
} 

function validateEmail(theObject, objectDescription)
{
	var email = theObject.value;
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if(!email)
		return true;
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length >= 6) && ((pindex+3) <= theStr.length)) {
			result = true;
		}
	}
	if (!result) {
		alert("Please enter a valid email address.");
		theObject.focus();
	}
	return result;
}

function validateFloat(theObject, objectDescription, scale)
{
	if (!theObject.value) {
		return true;
	}
	if (theObject.value!=parseFloat(theObject.value)) {
		alert("The value you entered is not a proper numeric value.\r\nThe value should only contain numbers and an optional decimal.");
		theObject.focus();
		return false;
	}
	if(scale == -1)
		return true;
	var pattern = /\d*\.?\d*/;
	var scale_test;
	if (!pattern.test(theObject.value))
	{
		alert("Please enter a numeric value " + objectDescription + ".");
		return false;
	}
	else
	{
		if (theObject.value.search(/\.{1}/) != -1)
		{
			scale_test = theObject.value.split('.');

			if (scale_test[1].length <= scale)
			{
				if (theObject.value.search(/[a-zA-Z]/) == -1)
					return true;
				else
				{
					alert("Please enter only digits for " + objectDescription + ".");
					return false;
				}
			}
			else
			{
				alert("Please enter only " + scale + " digits after the decimal for " + objectDescription + ".");
				return false;
			}
		}
		else 
		{
			if (theObject.value.search(/[a-zA-Z]/) == -1)
				return true;
			else
			{
				alert("Please enter only digits for " + objectDescription + ".");
				return false;
			}
		}
	}
}

// validate a string that's xxx-xxx-xxxx or (xxx)xxx-xxxx
function validateFon(theObject, objectDescription, dont_show_alert_message)
{
	if(theObject.value.match(/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/) || theObject.value.match(/^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/) || theObject.value.match(/^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/))
		return true;
	else
	{
		if(dont_show_alert_message != true){
			alert("Invalid format for "+objectDescription+". \nAccepted formats are: \n123-456-7890 \n(123)456-7890 \n(123) 456-7890");
		}
		theObject.focus();
		return false;
	}
		
}

// validate a string that's xxx-xx-xxxx or xxxxxxxxx
function validateSSN(theObject, objectDescription, dont_show_alert_message)
{
	if(theObject.value.match(/[0-9]{3}-[0-9]{2}-[0-9]{4}$/) || theObject.value.match(/[0-9]{9}$/))
		return true;
	else
	{
		if(dont_show_alert_message != true){
			alert("Invalid format for "+objectDescription+". \nAccepted formats are: \n123-45-6789 \n123456789");
		}
		theObject.focus();
		return false;
	}
		
}

function validateTime(theObject, objectDescription, dont_show_alert_message)
{
	if(dont_show_alert_message==null)	{
		dont_show_alert_message = false;
	}
	
	// If the first digit is a 0, go ahead and just pull it off. Previously, such a digit was invalid, but removing it should make
	// it a little more robust for the user while maintaining validity
	if(theObject.value.substr(0, 1)=="0")	{
		theObject.value = theObject.value.substr(1);
	}
	
	if(theObject.value.match(/^([1-9]|1[0-2]):[0-5][0-9] [A,a,P,p][M,m]$/))
		return true;
	else
	{
		if(!dont_show_alert_message)	{
			alert("Invalid format for "+objectDescription+". \nAccepted formats are: \n12:34 am \n2:34 PM");
		}
		theObject.focus();
		return false;
	}
}

// most generic router type validation routine
function validateInput(theObject, objectDescription)
{	
	var theValue;
	theValue = getInputValue(theObject);
	return validateValue(theObject, theValue, objectDescription);
}

function getInputValue(theObject) {
	var theValue;
	if (theObject.type=="select-one" || theObject.type=="select-multiple")
	{
		if (theObject.options.selectedIndex < 0) 
			theValue="";
		else 
			theValue=theObject.options[theObject.selectedIndex].value;
	}
	else
	{
		if (theObject.type=="text" || theObject.type=="file" || theObject.type=="password") 
			theValue=theObject.value;
		else 
		{
			if (theObject.type=="textarea") 
				theValue=theObject.value;
			else 
				return 1;
		}
	}
	return theValue;
}

function validateDateText(objName, return_format, return_formatted_string, dont_show_alert_message) {
	var datefield = objName;
	var dataReturned = chkdate(objName, return_format, return_formatted_string, dont_show_alert_message);
	if (dataReturned == false) {
		datefield.select();
		//alert("That date is invalid.  Please try again.");
		datefield.focus();
		return false;
	}
	else {
		if (return_formatted_string == true){
			return dataReturned;
		}
		else{
			return true;
		}
	}
}
function chkdate(objName, return_format, return_formatted_string, dont_show_alert_message) {
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay = "";
	var strMonth = "";
	var strYear = "";
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	var invalid_format_message = "Invalid Format. \nAccepted formats are: \nyyyy-mm-dd \nyyyy/mm/dd \nyyyy-m-d \nyyyy/m/d \nmm-dd-yyyy \nmm/dd/yyyy \nmm.dd.yyyy \nmm dd yyyy \nmmm dd yyyy \nmmddyyyy \nm-d-yyyy \nm/d/yyyy \nm.d.yyyy \nm d yyyy \nmmm d yyyy \nm-d-yy \nm/d/yy \nm.d.yy \nm d yy \nmmm d yy \n(Note: yy is 20yy)";
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				if (dont_show_alert_message != true){
					alert(invalid_format_message);
				}
				return false;
			}
			else {
				if (strDateArray[0].length==4) {
					strYear = strDateArray[0];
					strMonth = strDateArray[2];
					strDay = strDateArray[1];
				}
				else {
					strDay = strDateArray[0];
					strMonth = strDateArray[1];
					strYear = strDateArray[2];
				}
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
	if (strYear.length < 2) {
		if (dont_show_alert_message != true){
			alert(invalid_format_message);
		}
		return false;
	}
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	
	//check for chars
	if(isNaN(strYear))
	{
		err = 2;
		if (dont_show_alert_message != true){
			alert(invalid_format_message);
		}
		return false;
	}
	if(isNaN(strDay))
	{
		err = 2;
		if (dont_show_alert_message != true){
			alert(invalid_format_message);
		}
		return false;
	}
	if(isNaN(strMonth))
	{
		err = 2;
		if (dont_show_alert_message != true){
			alert(invalid_format_message);
		}
		return false;
	}
	
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		if (dont_show_alert_message != true){
			alert(invalid_format_message);
		}
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
		   }
		}
		if (isNaN(intMonth)) {
			err = 3;
			if (dont_show_alert_message != true){
				alert(invalid_format_message);
			}
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		if (dont_show_alert_message != true){
			alert(invalid_format_message);
		}
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		alert("Month must be between 1 and 12");
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		alert("Day must be between 1 and 31");
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		alert("Day must be between 1 and 30");
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			alert("Day must be greater than or equal to 1");
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				alert("Day must be between 1 and 29");
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				alert("Day must be between 1 and 28");
				return false;
			}
		}
	}
	/*if (strDatestyle == "US") {
		datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}*/
	var strMonth = "" + intMonth;
	strMonth = ((strMonth.length < 2) ? "0"+strMonth : strMonth);
	var strDay = "" + intday;
	strDay = ((strDay.length < 2) ? "0"+strDay : strDay);
	switch (return_format) {
		case "yyyy-mm-dd" :
			datefield.value = strYear+"-"+strMonth+"-"+strDay;
			break;
		case "mm-dd-yyyy" :
			datefield.value = strMonth+"-"+strDay+"-"+strYear;
			break;
		case "yyyy/mm/dd" :
			datefield.value = strYear+"/"+strMonth+"/"+strDay;
			break;
		case "mm/dd/yyyy" :
			datefield.value = strMonth+"/"+strDay+"/"+strYear;
			break;
		case "mm/dd/yy" :
			datefield.value = strMonth+"/"+strDay+"/"+strYear.substring(strYear.length-2, strYear.length);
			break;
	}
	if (return_formatted_string == true){
		return datefield.value;
	}
	else{
		return true;
	}
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}
// check if something's to long
function validateLength(theObject, theValue, theLength, objectDescription) {
	if (theValue.length > theLength) 
	{
		alert("The length of "+objectDescription+" must be less than "+(theLength+1)+" characters");
		theObject.focus();
		return false;
	}
	else
		return true;
}

function validateFileType(theObject, filetypes) {
	var filename = theObject.value;
	if (filename) {
		var parts = filename.split(".");
		var type = parts[parts.length-1];
		type = type.toLowerCase();
		var is_valid_type = false;
		for (var i=0; i < filetypes.length; i++) {
			if (type == filetypes[i]) {
				is_valid_type = true;
				break;
			}
		}
		if (!is_valid_type) {
			var supported_file_types = "";
			for (var i=0; i < filetypes.length; i++) {
				supported_file_types += "     "+filetypes[i]+"\r\n";
			}
			alert("The file you are trying to upload is not one of the supported file types:\r\n"+supported_file_types);
		}
		return is_valid_type;
	}
	else {
		return true;
	}
}

// validation for select boxes that accept multiple selections
function validateMultiple(theObject, objectDescription, blank_line) 
{
	// blank_line was added after this function was first created
	if(typeof(blank_line) == "undefined") {
		blank_line = 1;
	}
	
	return validateValue(theObject,(theObject.options.length > blank_line), objectDescription);
}

// radios are handled differently, so a special routine is needed
function validateRadio(theObject, objectDescription)
{
	if (theObject.type)			// if it's a radio w/only 1 button
	{
		if (theObject.checked)
			return true;
		else
			return false;
	}
	else
	{
		for (var i = 0; i < theObject.length; i++)
		{
			if (theObject[i].checked)
				return true;
		}
	}
	
	alert("Please select " + objectDescription + ".");
	return false;
}

// makes sure enough items are in a box
function validateSelectSize(theObject, objectDescription, totalSize)
{
	if (theObject.length < totalSize)
	{
		sizeDif = totalSize - theObject.length;
		alert("Please enter at least " + sizeDif + " more elements in the " + objectDescription + " box to continue.")
		theObject.focus();
		return false;
	}
	else 
		return true;
}

// just determines which specialized text field to validate w/
function validateText(theObject, theRoutine, objectDescription, validate_existence)
{
	if (typeof(validate_existence)=="undefined") {
		var validate_existence = true;
	}
	if (validate_existence) {
		if (!validateInput(theObject, objectDescription)) {
			return false;
		}
	}
	else if(!getInputValue(theObject) || getInputValue(theObject)=="") {
		return true;
	}
	
	switch (theRoutine)
	{
		case 2:
			return validateFloat(theObject, objectDescription, 0);
		break;
		
		case 3:
			return validateFloat(theObject, objectDescription, 2);
		break;
		
		case 4:
			return validateFon(theObject, objectDescription);
		break;
		
		case 5:
			return validateEmail(theObject, objectDescription);
		break;
		
		case 6:
			return validateZipcode(theObject);
		break;
		
		case 7:
			return validateFloat(theObject, objectDescription, -1);
			/*if (!theObject.value) {
				return true;
			}
			if (theObject.value==parseFloat(theObject.value)) {
				return true;
			}
			else {
				alert("The value entered for "+objectDescription+" must be a float.");
				theObject.focus();
				return false;
			}*/
		break;
		
		// Positive Integer
		case 8:
			if(! validate_existence && ! theObject.value) {
				return true;
			}
			if(!(theObject.value.match('^[0-9]*[1-9][0-9]*$') )) {
				alert('Value for ' +objectDescription + ' must be a positive number.');
				theObject.focus();
				return false;
			}
			return true;
		break;
		
		// Positive Float
		case 9:
			if(! validate_existence && ! theObject.value) {
				return true;
			}
			if(!(theObject.value.match('^[0-9]*[1-9][0-9]*\.[0-9]*$') )) {
				alert('Value for ' +objectDescription + ' must be a positive number.');
				theObject.focus();
				return false;
			}
			return true;
		break;
		
		// Not Negative Integer
		case 10:
			if(! validate_existence && ! theObject.value) {
				return true;
			}
			if(!(theObject.value.match('^[0-9]*$') )) {
				alert('Value for ' +objectDescription + ' must be a non-negative integer.');
				theObject.focus();
				return false;
			}
			return true;
		break;
		
		// SSN
		case 11:
			return validateSSN(theObject, objectDescription);
		break;
		
		//Time
		case 12:
			return validateTime(theObject, objectDescription);
		break;
		
		case 13:
			return validateJS_Variable(theObject, objectDescription);
		break;
		
		case 14:
			return validateCreditCard(theObject, objectDescription);
		break;
		
		case 15:
			return validateFilename(theObject, objectDescription);
		break;
	}
	return true;
}
		
		

// this one is called by the router validateInput
function validateValue(theObject, theValue, objectDescription) 
{
	if (!theValue || theValue=="") 
	{
		if ((objectDescription == '')||(objectDescription == null)) {
			objectDescription = "a value";
		}
		alert("Please supply " + objectDescription + " in order to continue.");
		theObject.focus();
		return false;
	}
	else 
		return true;
}

function validateZipcode(theObject)
{
	var valid        = "0123456789-";
	var hyphen_count = 0;

	if (theObject.value.length != 5 && theObject.value.length != 10) 
	{
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		theObject.focus();
		return false;
	}
	
	for (var i = 0; i < theObject.value.length; i++) 
	{
		temp = theObject.value.substring(i, i + 1);
		if (temp == "-") 
			hyphen_count++;
		if (valid.indexOf(temp) == "-1") 
		{
			alert("Invalid characters in your zip code.  Please try again.");
			theObject.focus();			
			return false;
		}
	}
	
	if(hyphen_count > 0 && theObject.value.length == 5)
	{
		alert("Invalid characters in your zip code.  Please try again.");
		theObject.focus();
		return false;
	}
	
	if((hyphen_count > 1) || (theObject.value.length == 10 && theObject.value.charAt(5) != "-"))
	{
		alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
		theObject.focus();			
		return false;
	}
	
	return true;
}

function validate_time_selector(store_hidden, time_selector_0, time_selector_1, description) {
	period = times_to_period(time_selector_0.value, time_selector_1.value);
	if (!validate_time_period(period)) {
		alert("Invalid period for "+description+" ("+period+"), times must be in order.");
		return false;
	}
	store_hidden.value = period;
	return true;
}

function validate_time_period(period, to_std) {
	var times = period_to_times(period, to_std);
	return compare_times(times[0], times[1]) <= 0;
}

function period_to_times(period) {
	var times = new Array();
	if (period.length > 14) {
		times[0] = period.substr(0,8);
		times[1] = period.substr(12,8);
		times[0] = convert_to_standard(times[0]);
		times[1] = convert_to_standard(times[1]);
	}
	else {
		times[0] = period.substr(0,5);
		times[1] = period.substr(9,5);
	}
	return times;
}

function times_to_period(time_0, time_1) {
	return time_0 + " to " + time_1;
}

function compare_times(time_0, time_1) {
	if (time_0 == time_1) return 0;
	else return (time_0 < time_1) ? -1 : 1;
}

function standard_time_difference(time_0, time_1) {
	return standard_to_hours(time_0) - standard_to_hours(time_1);
}

function standard_to_hours(time) {
	var time_info = time.split(":");
	return parseFloat(time_info[0]) + parseFloat(time_info[1]) / 60;
}

function standard_time_difference(time_0, time_1) {
	return standard_to_hours(time_1) - standard_to_hours(time_0);
}

function convert_to_standard(time) {
	var time_array = time.split(":");
	if (time_array[2] == "pm" && time_array[0]<12) time_array[0] =  parseInt(parseFloat(time_array[0])) + 12;
	return time_array[0] + ":" + time_array[1];
}

function time_periods_cross(period_0, period_1) {
	var times_0 = period_to_times(period_0,1);
	var times_1 = period_to_times(period_1,1);
	
	//time_0[0] is within period_1 ||
	//time_0[1] is within period_1 ||
	//period_1 is withing period_0 
	return (!(standard_time_difference(times_0[0], times_1[0]) >= 0 || standard_time_difference(times_0[0], times_1[1]) <= 0) || 
	   	!(standard_time_difference(times_0[1], times_1[0]) >= 0 || standard_time_difference(times_0[1], times_1[1]) <= 0) ||
		(standard_time_difference(times_0[0], times_1[0]) >= 0 && standard_time_difference(times_0[1], times_1[1]) <= 0));
	
}

function validateTimeText(obName) {
	if(chTime(obName)) {
		return true;
	}
	else
		alert('Please use "hh:mm:ss[ am/pm]" format with the time text box');
		return false;
}

function chTime(obName) {
	var timefield = obName;
	strTime = timefield.value;
	if (strTime.length < 1) {
		return true;
	}
	var timeStr = new String(strTime);
	array = timeStr.split(' ');
	if(array.length > 2) {
		return false;
	}
	myTime = new String(array[0]);
	t_array = myTime.split(':');
	if(t_array.length != 3)
		return false;
	if(!t_array[0] || !t_array[1] || !t_array[2] || isNaN(t_array[0]) || isNaN(t_array[1]) || isNaN(t_array[2]))
		return false;
	hours = parseInt(t_array[0], 10);
	minutes = parseInt(t_array[1], 10);
	seconds = parseInt(t_array[2], 10);
	if(array.length == 2) {
		if(hours > 12 || hours < 1)
			return false;
		if(array[1] == 'pm' || array[1] == 'PM' || array[1] == 'p.m.' || array[1] == 'P.M.') {
			hours = hours + 12;
			if(hours == 24)
				hours = 12;
		}
		else if(array[1] == 'am' || array[1] == 'AM' || array[1] == 'a.m.' || array[1] == 'A.M.') {
			if(hours == 12)
				hours = 0;
		}
	}
	if(hours < 0 || hours > 23 || minutes > 59 || minutes < 0 || seconds > 59 || seconds < 0)
		return false;
		
	timefield.value = ((hours < 10) ? '0' : '')+hours+':'+((minutes < 10) ? '0' : '')+minutes+':'+((seconds < 10) ? '0' : '')+seconds; 
	
	return true;
}

// this one is called by the router validateInput
function validateJS_Variable(theObject, objectDescription) 
{
	var theValue = theObject.value;
	
	// First check to make sure the first char of the value is not a numeral
	first_char = theValue.substr(0, 1);
	numeral_search = first_char.search(/[0-9]/);
	if(numeral_search > -1)	{	// First char is a numeral
		alert("The value for " + objectDescription + " is not a valid Javascript variable name. The first character cannot be a numeral.");
		theObject.focus();
		return false;
	}
	
	// If we get here, first char is not a numeral
	// Therefore, next check to make sure the value as a whole only contains letters, numerals, and the underscore character
	search_results = theValue.search(/[^\w]/);
	if(search_results == -1)	{	// No non-word characters found
		return true;
	}
	else	{
		alert("The value for " + objectDescription + " is not a valid Javascript variable name. The value can only contain upper- and lowercase letters, numerals, and the underscore character.");
		theObject.focus();
		return false;
	}
}

function validateCreditCard(theObject, objectDescription)
{
	cc_num = theObject.value;
	if(cc_num.match("^[0-9 ]+$"))
	{
		while(cc_num.indexOf(" ") > -1)
		{
			cc_num = cc_num.replace(" ", "");
		}
		sum = 0;
		digits = "";
		rev_cc_num = "";
		for(i = 0; i < cc_num.length; i++)
		{
			rev_cc_num = cc_num.substr(i, 1)+rev_cc_num;
		}
		for(i = 0; i < rev_cc_num.length; i++)
		{
			if((i%2))
			{
				digits += (parseInt(rev_cc_num.substr(i, 1))*2).toString();
			}
			else
			{
				digits += rev_cc_num.substr(i, 1);
			}
		}
		for(i = 0; i < digits.length; i++)
		{
			sum += parseInt(digits.substr(i, 1));
		}
		if(sum % 10)
		{
			alert("The value for "+objectDescription+" is not a valid Credit Card Number.");
			theObject.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		alert("The value for "+objectDescription+" must contain only numerals.");
		theObject.focus();
		return false;
	}
}
		
		
function validateFilename(theObject, objectDescription)	{
	var theValue = theObject.value;
	
	// First make sure that there is only 1 or 0 periods in the value
	value_parts = theValue.split(".");
	if(value_parts.length > 2)	{
		alert("The value for " + objectDescription + " is not a valid filename. The value can contain a maximum of one period.");
		return false;
	}
	
	// If we get here, value has the right number of periods
	// Therefore, next check to make sure the value as a whole only contains letters, numerals, and the underscore character
	// Do this check on each part of the array created above
	for(i=0;i<value_parts.length;i++)	{
		search_results = value_parts[i].search(/[^\w]/);
		if(search_results != -1)	{	// Non-word characters found
			alert("The value for " + objectDescription + " is not a valid filename. The value can only contain upper- and lowercase letters, numerals, the underscore character, and a period.");
			theObject.focus();
			return false;
		}
	}
	
	// If we get here, everything checked out, so return true
	return true;
}



function validatePhoneMultibox(theObject, objectDescription, required, store_ext_sep, ext_syntax, return_msg)	{
	if(store_ext_sep==null)	{
		store_ext_sep = false;
	}
	if(ext_syntax==null)	{
		ext_syntax = "Ext. #";
	}
	if(return_msg==null)	{
		return_msg = false;
	}
	
	var desc_syntax = ((objectDescription!="")?" for "+objectDescription:"");
	
	// First validate each of the boxes (phone_1 may not exist)
	var ph_arr = [];
	var ext = "";
	var first_ph_box = false;
	for(var i=1; i<=4; i++)	{
		var ph = document.getElementById(theObject.id+"_"+i);
		if(ph!=null)	{
			var val = trim(ph.value);
			
			if(isNaN(val))	{
				return validatePhoneMultibox_msg_helper("Please enter only numerical digits"+desc_syntax+".", ph, return_msg);
			}
			else if(i<3 && val!="" && val.substring(0, 1)=="0")	{
				return validatePhoneMultibox_msg_helper("Please enter a starting digit greater than 0"+desc_syntax+".", ph, return_msg);
			}
			else if(i<4 && (required || (!required && val.length>0)) && val.length!=((i<3)?3:4))	{
				return validatePhoneMultibox_msg_helper("Please enter the proper number of digits"+desc_syntax+".", ph, return_msg);
			}
			else	{		// Don't add extension to array
				if(i<4)	{
					ph_arr[ph_arr.length] = val;
				}
				else if(trim(val))	{
					ext = val;
				}
			}
			
			if(!first_ph_box)	{
				first_ph_box = ph;
			}
		}
	}
	
	// Need to do one final check on the overall length of the phone number
	var ph_val = ph_arr.join("-");
	if(ph_val=="--" || ph_val=="---")	{
		ph_val = "";
	}
	if((required || (!required && ph_val.length>0)) && ph_val.length!=((ph_arr.length==2)?8:12))	{
		return validatePhoneMultibox_msg_helper("Please enter the proper number of digits"+desc_syntax+".", first_ph_box, return_msg);
	}
	
	// If we get here, all is well, set the hiddens' value, and return
	if(!store_ext_sep)	{
		theObject.value = ph_val+((trim(ext)!="")?" "+ext_syntax+ext:"");
	}
	else	{
		theObject.value = ph_val;
		document.getElementById(theObject.id+"_ext").value = ext;
	}
	
	return ((return_msg)?[true]:true);
}


function validatePhoneMultibox_msg_helper(msg, ph, return_msg)	{
	if(return_msg)	{
		return [false, msg, ph];
	}
	else	{
		alert(msg);
		ph.focus();
		return false;
	}
}
