function initCaps(string) {											// force initial caps of all words; drop excess spaces
	wk=string+' ';													// append a blank
	result="";
	while (wk.length > 0) {
		p=wk.indexOf(" ");											// find next blank
		z=wk.substring(0,p);										// pull out a word
		if (z.length > 0) {
			c=z.substring(0,1);									// first letter
			c=c.toUpperCase();									// make it a capital
			z=c+z.substring(1,z.length);							// reassemble the word
			if (result !== '') result += ' ';							// space between words
			result += z; 											// add word to result
		}
		wk=wk.substring((p+1),wk.length);						// remove processed piece
	}
	return result;			
}
function goodName(string) { 
	if (!string) return false; 										// null string -- error
	var iChars = "*|,\":<>[]{}`~\';()@&$#%^/?_"; 					// unacceptable characters
	for (var i = 0; i < string.length; i++) { 							// loop through each character of the string
		if (iChars.indexOf(string.charAt(i)) != -1) return false;		// if any wrong'un, reject the name
	} 
	return true; 													// well, it's okay, then
} 
function goodEmail(string) { 
	if (!string) return false; 										// null string -- error
	var a = string.split("@");										// split it at the @
	if (a.length != 2) return false;									// if not two pieces, too bad
	var iChars = " *|,\":<>[]{}`~\';()@&$#%^/?";						// unacceptable characters in each piece
	for (var i = 0; i < a[0].length; i++) { 							// check out user name
		if (iChars.indexOf(a[0].charAt(i)) != -1) return false; 		// if any wrong character, tilt
	} 
	var b = a[1].split("\.");											// break domain name at dots
	if (b.length < 2) return false;									// if only one piece, too bad
	var c = b.length - 1;											// last piece index
	if (b[c].length < 2 || b[c].length > 3) return false;				// must end in .xxx or .xx
	for (var i = 0; i < a[1].length; i++) { 							// check out domain name
		if (iChars.indexOf(a[1].charAt(i)) != -1) return false; 		// if any wrong character, tilt
	} 
	return true;														// must be okay, I guess 
} 
function goodPhone(string) { 
	if (!string) return false; 										// null string -- error
	var iChars = "0123456789";									// unacceptable characters in each piece
	var a = "";
	for (var i = 0; i < string.length; i++) { 							// loop through each character of the string
		var b = string.charAt(i);
		if (iChars.indexOf(b) != -1) a = a + b;						// if a number, concatenate it to other result
	}
	if (a.length == 10) {											// must have exactly ten digits
		var b = a.substring(0,3) + "-" + a.substring(3,6) + "-" + a.substring(6,10);
		return b; 													// if it does, it's okay
	}
	return false;
} 
function goodHours(num) { 
	var z = parseInt(num);
	if (z < 1 || z > 40) return false; 								// check within range
	return true; 													// well, it's okay, then
} 
function goodAddress(string) { 
	if (!string) return false; 										// null string -- error
	var iChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\,\.\/-\' "; // acceptable chars
	for (var i = 0; i < string.length; i++) { 							// loop through each character of the string
		if (iChars.indexOf(string.charAt(i)) == -1) return false;		// if any wrong'un, reject the name
	} 
	return true; 													// well, it's okay, then
} 
function goodState(string) { 
	if (!string) return false; 										// null string -- error
	if (string.length != 2) return false;
	var iChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 		// acceptable characters
	for (var i = 0; i < string.length; i++) { 							// loop through each character of the string
		if (iChars.indexOf(string.charAt(i)) == -1) return false;		// if any wrong'un, reject the name
	} 
	return true; 													// well, it's okay, then
} 
function goodZip(string) { 
	if (!string) return false; 										// null string -- error
	var iChars = "0123456789-"; 									// acceptable characters
	for (var i = 0; i < string.length; i++) { 							// loop through each character of the string
		if (iChars.indexOf(string.charAt(i)) == -1) return false;		// if any wrong'un, reject the zip
	}
	var a = string.split("-");										// split it at the hyphens
	if (a.length > 2) return false;
	if (a.length == 2) {
		if (a[0].length != 5) return false;							// xxxxx-xxxx
		if (a[1].length != 4) return false;
		return string;												// return as is
	}
	if (a[0].length == 5) return a[0];								// xxxxx
	if (a[0].length != 9) return false;
	var b = a[0].substring(0,5) + "-" + a[0].substring(5,9);
	return b;
} 

function checkvform(form) { 
	if (goodName(form.first_name.value) == false) {				// check for a valid first name
		alert("Please enter a valid first name-avoid special characters."); 
		form.first_name.focus(); 
		return false; 
	} 
	if (goodName(form.last_name.value) == false) {			     // check for a valid last name
		alert("Please enter a valid last name-avoid special characters."); 
		form.last_name.focus(); 
		return false; 
	} 
	if (goodEmail(form.email.value) == false) {		      		// check for a valid e-mail address
		alert("A valid e-mail address is required."); 
		form.email.focus(); 
		return false; 
	} 
// See if anything is filled in in the 2nd part of the form
	var z = '';														// capture days-of-the-week from the checkboxes
	for (var i=0;i<6;i++) {
		if (form.wkday[i].checked) z = z + form.wkday[i].value;
	}
	form.days.value = z;
	if (form.days.value || form.hours.value || form.address1.value || form.city.value
		|| form.state.value || form.zip.value) {						// key item(s) filled in ...
		if (form.hours.value !== '') {
			if (goodHours(form.hours.value) == false) {			// check for valid hours per week
				alert("Hours should be between 1 and 40."); 
				form.hours.focus(); 
				return false; 
			} 
		}
		var z = goodPhone(form.home_phone.value); 
		if (z == false) {												// check for a valid home phone number
			alert("Please enter a valid home phone number."); 
			form.home_phone.focus(); 
			return false; 
		} 
		else {
			form.home_phone.value = z;
		}
		if (form.work_phone.value) {
			var z = goodPhone(form.work_phone.value); 
			if (z == false) {											// check for a valid work phone number
				alert("Work phone format is not valid."); 
				form.work_phone.focus(); 
				return false; 
			} 
			else {
				form.work_phone.value = z;
			}
		}
		if (form.cell_phone.value) {
			var z = goodPhone(form.cell_phone.value); 
			if (z == false) {											// check for a valid cell phone number
				alert("Cell phone format is not valid."); 
				form.cell_phone.focus(); 
				return false; 
			}
			else {
				form.cell_phone.value = z;
			}
		} 
		if (goodAddress(form.address1.value) == false) {		   	// check for a valid mailing address
			alert("Please enter a valid mailing address."); 
			form.address1.focus(); 
			return false; 
		} 
		if (form.address2.value) {
			if (goodAddress(form.address2.value) == false) {		// check for a valid mailing address
				alert("Avoid special characters in the address."); 
				form.address2.focus(); 
				return false; 
			}
		} 
		if (goodName(form.city.value) == false) {		   				// check for a valid city name
			alert("Please enter a valid city name."); 
			form.city.focus(); 
			return false; 
		} 
		if (goodState(form.state.value) == false) {		   				// check for a valid state ID
			alert("Please enter the two-letter code for the state."); 
			form.state.focus(); 
			return false; 
		} 
		var z = goodZip(form.zip.value); 
		if (z == false) {													// check for a valid zip code
			alert("Please enter the 5- or 9-digit zip code."); 
			form.zip.focus(); 
			return false; 
		} 
		else {
			form.zip.value = z;
		}
	}

	return true;
}
//  End of script
