function validEmail(Email) {
	invalidChars = " /:,;";

	if (Email == "") {           // cannot be empty
		return false;
	}
	for (i=0; i < invalidChars.length; i++) {   // does it contain any invalid characters?
		badChar = invalidChars.charAt(i);
		if (Email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	atPos = Email.indexOf("@",1);       // there must be one "@" symbol
	if (atPos == -1) {
		return false;
	}
	if (Email.indexOf("@",atPos+1) != -1) {  // and only one "@" symbol
		return false;
	}
	periodPos = Email.indexOf(".",atPos+2);
	if (periodPos == -1) {         // and at least one "." after the "@"
		return false;
	}
	if (periodPos+3 > Email.length) {   // must be at least 2 characters after the "."
		return false;
	}
	return true;
}
// end of validEmail

function validPhone(PhoneNo) { // CANADIAN PHONE NUMBERS ONLY
	strlen=PhoneNo.length; if (strlen!=12) {return false}
	// Check for legal characters in string - note index starts at zero
	if ('0123456789'.indexOf(PhoneNo.charAt(0))<0) {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(1))<0) {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(2))<0) {return false}
	if (PhoneNo.charAt(3) != " ") {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(4))<0) {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(5))<0) {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(6))<0) {return false}
	if (PhoneNo.charAt(7) != "-") {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(8))<0) {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(9))<0) {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(10))<0) {return false}
	if ('0123456789'.indexOf(PhoneNo.charAt(11))<0) {return false}
	return true;
}
// end of validPhone

function validPostal(PCode){ // CANADIAN CODES ONLY
	strlen=PCode.length; if (strlen!=7) {return false}
	entry=PCode.toUpperCase();    // in case of lowercase characters
	// Check for legal characters in string - note index starts at zero
	if ('ABCEGHJKLMNPRSTVXY'.indexOf(PCode.charAt(0))<0) {return false}
	if ('0123456789'.indexOf(PCode.charAt(1))<0) {return false}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(PCode.charAt(2))<0) {return false}
	if (PCode.charAt(3) != " ") {return false}
	if ('0123456789'.indexOf(PCode.charAt(4))<0) {return false}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(PCode.charAt(5))<0) {return false}
	if ('0123456789'.indexOf(PCode.charAt(6))<0) {return false}
	return true;
}
// end of validPostal

function allLettersSpace(dumstring) {
	validchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";

	for (i=0; i < dumstring.length; i++) {
		theChar = dumstring.charAt(i);
		if (validchars.indexOf(theChar,0) > -1) { // is it valid alpha characters?
			// do nothing
		} else {
			return false;
		}
	}
	return true;
}

function allLetters(dumstring) {
	validchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

	for (i=0; i < dumstring.length; i++) {
		theChar = dumstring.charAt(i);
		if (validchars.indexOf(theChar,0) > -1) { // is it valid alpha characters?
			// do nothing
		} else {
			return false;
		}
	}
	return true;
}

function allNumbers(dumstring) {
	validchars = "0123456789";

	for (i=0; i < dumstring.length; i++) {
		theChar = dumstring.charAt(i);
		if (validchars.indexOf(theChar,0) > -1) { // is it valid numeric characters?
			// do nothing
		} else {
			return false;
		}
	}
	return true;
}

function nameCheck(dumstring) {
	validchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz -.,";

	for (i=0; i < dumstring.length; i++) {
		theChar = dumstring.charAt(i);
		if (validchars.indexOf(theChar,0) > -1) { // is it valid alpha characters?
			// do nothing
		} else {
			return false;
		}
	}
	return true;
}
  
function allASCII(dumstring) {
	validchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789.,;:&-'";

	for (i=0; i < dumstring.length; i++) {
		theChar = dumstring.charAt(i);
		if (dumstring.charCodeAt(i) == 13) {
			theChar = " ";
		}
		if (dumstring.charCodeAt(i) == 10) {
			theChar = " ";
		}
		if (validchars.indexOf(theChar,0) > -1) { // is it valid characters?
			//do nothing
		} else {
			return false;
		}
	}
	return true;
}

function submitIt(form){

	theStatus = true;
	errorMessage = "";

	if (form.name.value == ""){
		errorMessage += "The name field must be entered\n";
		form.name.focus();
		form.name.select();
		theStatus = false;
	} else {
		if (!allASCII(form.name.value)){
			errorMessage += "You must enter a valid name\n";
			form.name.focus();
			form.name.select();
			theStatus = false;
		}
	}

	if (form.address.value == ""){
		errorMessage += "You must enter at least one line of the address\n";
		form.address.focus();
		form.address.select();
		theStatus = false;
	} else {
		if (!allASCII(form.address.value)){
			errorMessage += "You must enter a valid address line\n";
			form.address.focus();
			form.address.select();
			theStatus = false;
		}
	}

	if (form.phone.value == ""){
		errorMessage += "You must enter a phone number\n";
		form.phone.focus();
		form.phone.select();
		theStatus = false;
	} else {
		if (!validPhone(form.phone.value)){
			errorMessage += "You must enter a valid phone number: 999 999-9999\n";
			form.phone.focus();
			form.phone.select();
			theStatus = false;
		}
	}

	// check to see if the email's valid
	if (form.email.value == ""){
		errorMessage += "You must enter an email address\n";
		form.email.focus();
		form.email.select();
		theStatus = false;
	} else {
		if (!validEmail(form.email.value)){
			errorMessage += "Invalid email address\n";
			form.email.focus();
			form.email.select();
			theStatus = false;
		}
	}

	if ((form.serviceType[0].checked == false) && 
		(form.serviceType[1].checked == false) && 
		(form.serviceType[2].checked == false) && 
		(form.serviceType[3].checked == false) && 
		(form.serviceType[4].checked == false) && 
		(form.serviceType[5].checked == false)) {
		errorMessage += "The type of service must be selected\n";
		theStatus = false;
	}
	
	// these checks below are for Group Benefits only
	if (form.serviceType[2].checked == true){
		if (form.employer.value == ""){
			errorMessage += "The employer name must be entered\n";
			form.employer.focus();
			form.employer.select();
			theStatus = false;
		} else {
			if (!nameCheck(form.employer.value)){
				errorMessage += "You must enter a valid employer name with only letters and spaces\n";
				form.employer.focus();
				form.employer.select();
				theStatus = false;
			}
		}
		if (form.employerPhone.value == ""){
			errorMessage += "You must enter an employer phone number\n";
			form.employerPhone.focus();
			form.employerPhone.select();
			theStatus = false;
		} else {
			if (!validPhone(form.employerPhone.value)){
				errorMessage += "You must enter a valid employer phone number: (999) 999-9999\n";
				form.employerPhone.focus();
				form.employerPhone.select();
				theStatus = false;
			}
		}
		if (form.employerAddress.value == ""){
			errorMessage += "You must enter at least one line of the employer address\n";
			form.employerAddress.focus();
			form.employerAddress.select();
			theStatus = false;
		} else {
			if (!allASCII(form.employerAddress.value)){
				errorMessage += "You must enter a valid employer address line\n";
				form.employerAddress.focus();
				form.employerAddress.select();
				theStatus = false;
			}
		}
		// check to see if the email's valid
		if (form.employerEmail.value == ""){
			errorMessage += "You must enter an employer email address\n";
			form.employerEmail.focus();
			form.employerEmail.select();
			theStatus = false;
		} else {
			if (!validEmail(form.employerEmail.value)){
				errorMessage += "Invalid employer email address\n";
				form.employerEmail.focus();
				form.employerEmail.select();
				theStatus = false;
			}
		}
		if (form.product.value == ""){
			errorMessage += "The type of product must be entered\n";
			form.product.focus();
			theStatus = false;
		}
		// these checks above are for Group Benefits only

	}

	if ((form.serviceType[0].checked == true) || 
		(form.serviceType[1].checked == true) || 
		(form.serviceType[3].checked == true) || 
		(form.serviceType[4].checked == true) || 
		(form.serviceType[5].checked == true)) {

		// these checks are for all other service types
		if (form.age.value == ""){
			errorMessage += "The age must be entered\n";
			form.age.focus();
			form.age.select();
			theStatus = false;
		}
		if (form.amount.value == ""){
			errorMessage += "The amount ($ value) must be entered\n";
			form.amount.focus();
			form.amount.select();
			theStatus = false;
		}
		if (form.numPeople.value == ""){
			errorMessage += "The number of people must be entered\n";
			form.numPeople.focus();
			form.numPeople.select();
			theStatus = false;
		}
	}

	// If we made it to here, everything's valid, so return true
	if (theStatus == false){
		alert(errorMessage);
		return false;
	} else {
		return true;
	}
}
