/*
	Library: formval.js
	Programmer: Brice Mason
	Date Created: 6/26/03
	Last Updated: 7/1/03
	
	Description:
		Series of JavaScript functions used for form validation.
*/
// ***********************************************************
function test(strAlert){
	alert(strAlert);
}
// ***********************************************************
function trim(str){
	str = str.replace(/^\s+/, "");
	str = str.replace(/\s+$/, "");
	return(str);
}
// ***********************************************************
function isEmail(ea){
	var bad_email = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(^\s+)/;
  	var good_email = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
  	if (!bad_email.test(ea) && good_email.test(ea)) {
		return true;
	}
	else{
		return false;
	}
}
// ***********************************************************
 function check_all(objChkbx, chkOption){
 	for(var i = 0; i < objChkbx.length; i++){
  		objChkbx[i].checked = chkOption;
 	}
}
// ***********************************************************
function isLeapYear(strYear){
 	if(strYear % 4 == 0){
  		if((strYear % 100 == 0) && (strYear % 400 != 0)){
  			return false;
  		}
  		else{
   			return true;
  		}
 	}
 	else{
 		return false;
 	}
}
// ***********************************************************
function isDate(strDate){
// A regular expression is created to match the following pattern.
/*
	/ - Begin regular expression
	^ - At the beginning of the line
	(\d{1,2}) - Match one or two digits and store the value in the first backreference
	[\/] - Character class to represent the '/' character.
	(\d{1,2}) - Match one or two digits and store the value in the second backreference
	[\/] - Character class to represent the '/' character.
	(\d{4}) - Match exactly four digits and store the value in the third backreference
	$ - The last backreference (year) should be the last characters in this pattern
	/ - End regular expression
*/
		var reDate = /^(\d{1,2})[\/](\d{1,2})[\/](\d{4})$/;
		var testDate = reDate.test(strDate);
		var month;
		var day;
		var year;
		
// Test whether the value passed matches the regular expression. If it doesn't, then
// the date is in the wrong format and the function will return false.		
		if(testDate == true){
			var i;
// Set human-readable variables for the special RE backreferences created in the pattern.
// These new variables are then converted to numbers.		
			month = parseInt(RegExp.$1);
			day = parseInt(RegExp.$2);
			year = parseInt(RegExp.$3);
			
// Determine the number of days allowed in the given month			
			switch (month){
				case 4:
				case 6:
				case 9:
				case 11:
// 30-day month
					if((day <= 30) && (day >= 1)){
						return true;
					}
					else{
						return false;
					}
					break;
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
// 31-day month				
					if((day <= 31) && (day >= 1)){
						return true;
					}
					else{
						return false;
					}
					break;
				case 2:
// February. A leap year will have 29 days, otherwise 28.
					if(isLeapYear(year)){
						if((day <= 29) && (day >= 1)){
							return true;
						}
						else{
							return false;
						}
					}
					else{
						if((day <= 28) && (day >= 1)){
							return true;
						}
						else{
							return false;
						}
					}					
					break;
				default:
					return false;	// The month does not match a value from 1 to 12
					break;
			}
		}
		else{
			return false;
		}
}
// ***********************************************************
function isBlank(objText){
	var strText = trim(objText.value);
		if(strText.length == 0){
			objText.value = strText;
			objText.focus();
			return true;
		}
		else{
			objText.value = strText;
			return false;
		}
}
// ***********************************************************
function isUSState(objSt8){
	var isState = 0;
	var state = objSt8.value;
	var i;
	var msg;
// Delcare and fill an array with all fifty states	
	var states = new Array(50);
	
	states[0] = "AL";
	states[1] = "AK";
	states[2] = "AZ";
	states[3] = "AR";
	states[4] = "CA";
	states[5] = "CO";
	states[6] = "CT";
	states[7] = "DE";
	states[8] = "FL";
	states[9] = "GA";
	states[10] = "HI";
	states[11] = "ID";
	states[12] = "IL";
	states[13] = "IN";
	states[14] = "IA";
	states[15] = "KS";
	states[16] = "KY";
	states[17] = "LA";
	states[18] = "ME";
	states[19] = "MD";
	states[20] = "MA";
	states[21] = "MI";
	states[22] = "MN";
	states[23] = "MS";
	states[24] = "MO";
	states[25] = "MT";
	states[26] = "NE";
	states[27] = "NV";
	states[28] = "NH";
	states[29] = "NJ";
	states[30] = "NM";
	states[31] = "NY";
	states[32] = "NC";
	states[33] = "ND";
	states[34] = "OH";
	states[35] = "OK";
	states[36] = "OR";
	states[37] = "PA";
	states[38] = "RI";
	states[39] = "SC";
	states[40] = "SD";
	states[41] = "TN";
	states[42] = "TX";
	states[43] = "UT";
	states[44] = "VT";
	states[45] = "VA";
	states[46] = "WA";
	states[47] = "WV";
	states[48] = "WI";
	states[49] = "WY";

// Trim whitespace from the value passed and convert it to upper case.
	state = trim(state);
	state = state.toUpperCase();
	
// Loop through the states array and test for a match.
// If there's a match, set the 'isState' variable to '1' indicating a valid state
// and break the loop.	
	for(i = 0; i < states.length; i++){
		if(state == states[i]){
			isState = 1;
			break;
		}
	}
	
// If we have a state, return the actual state string.
// Otherwise, return a false value.	
	if(isState == 1){
		objSt8.value = state;
		return true;
	}
	else{
		msg = "Please enter a valid two-letter state abbreviation.";
		alert(msg);
		objSt8.focus();
		return false;
	}
}
// ***********************************************************
function isPhone(objPN1, objPN2, objPN3){
	var phone1 = trim(objPN1.value);
	var phone2 = trim(objPN2.value);
	var phone3 = trim(objPN3.value);
	var msg;
	
	if ((phone1.length != 3) || (isNaN(phone1))){
		msg = "Please enter three digits in the area code field.";
		alert(msg);
		objPN1.focus();
		return false;
	}
	else if ((phone2.length != 3) || (isNaN(phone2))){
		msg = "Please enter three digits in the first phone number field.";
		alert(msg);
		objPN2.focus();
		return false;
	}
	else if ((phone3.length != 4) || (isNaN(phone3))){
		msg = "Please enter four digits in the second phone number field.";
		alert(msg);
		objPN3.focus();
		return false;
	}
	else{
		return true;
	}
	
}
// ***********************************************************
function isZipCode(objZip){
	var zip = trim(objZip.value);
	var msg;
	
	if(zip.length != 5){
		msg = "Please enter five digits in the zip code field.";
		alert(msg);
		objZip.focus();
		return false;
	}
	else{
		if(isNaN(zip)){
			msg = "Please enter five digits in the zip code field.";
			alert(msg);
			objZip.focus();
			return false;
		}
		else{
			return true;
		}
	}
}
// ***********************************************************
