function checkLen(o, next) {
	if ( o.value.length == o.size ) next.focus();
	return true;
}

var validateMsg = "";
var validateCount = 0;
var petRadioClicked = false;
var gateRadioClicked = false;

function allReqVars(fs) {
	var msg = "";
	var len = fs.length;

	for ( var i = 0; i < len; i++ ) {
		msg += fs.elements[i].name + ": " + fs.elements[i].value + "\n";
//		msg += eval(fs + ".elements[" + i + "].name") + ": " + eval(fs + ".elements[" + i + "].value") + "\n";
	}
	return msg;
}

function formatPhoneNumber(n) {

	var s = "";

// Get digits from phone number

	for ( var i = 0; i < n.value.length; i++ ) {
		if ( n.value.charAt(i) >= '0' && n.value.charAt(i) <= '9' ) {
			s = s.concat(n.value.charAt(i));
		}
	}

// If 10 digits long, format (npa) nxx-line

	if ( s.length == 10 ) {
/*		n.value = "(" + s.substr(0,3) + ") " + s.substr(3,3) + "-" + s.substr(6); */
		n.value = s.substr(0,3) + "-" + s.substr(3,3) + "-" + s.substr(6);
	}
}

//
// Validate Appointment Request Form
//

function validateAppointment(f) {
	var pattern = "";
	validateMsg = "";
	validateCount = 0;
	focusSet = 0;

// Check that all fields are populated correctly
	
	if ( trim(f.patient.value) == "" ) {
		validateMsg = "    Name of Patient\n";
		validateCount += 1;
		f.patient.focus();
		focusSet = 1;
	}
	if ( trim(f.birth_date.value) == "" ) {
		validateMsg += "    Date of Birth\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.birth_date.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.requester.value) == "" ) {
		validateMsg += "    Name of Requester\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.requester.focus();
			focusSet = 1;
		}
	}
	if ( f.relationship.selectedIndex == 0 ) {
		validateMsg += "    Relationship of Requester to Patient\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.relationship.focus();
			focusSet = 1;
		}
	}
	if ( f.appt_type.selectedIndex == 0 ) {
		validateMsg += "    Type of Appointment\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.appt_type.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.date1.value) == "" ) {
		validateMsg += "    Date - 1st Preference\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.date1.focus();
			focusSet = 1;
		}
	}
	if ( f.time1.selectedIndex == 0 ) {
		validateMsg += "    Time - 1st Preference\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.time1.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.date2.value) != "" && f.time2.selectedIndex == 0 ) {
		validateMsg += "    Time - 2nd Preference\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.time2.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.date3.value) != "" && f.time3.selectedIndex == 0 ) {
		validateMsg += "    Time - 3rd Preference\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.time3.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.phone.value) == "" ) {
		validateMsg += "    Phone Number\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.phone.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.email.value) == "") {
		validateMsg += "    E-mail Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.email.focus();
			focusSet = 1;
		}
	}


// Announce any missing fields

	if ( validateCount > 0 ) {
		var instructions = "Please complete the following field";
		if ( validateCount > 1 ) {
			instructions += "s:\n";
		}
		else {
			instructions += ":\n";
		}
		alert( instructions + validateMsg );
		return false;
	}

// Validate e-mail if provided

	if (trim(f.email.value) != "" ) {
		if (!validateEmail(f.email)) return false;
	}

// Validate phone number if provided

	if (trim(f.phone.value) != "") {
		var phoneDigits = getDigits(f.phone.value);
		var phoneLen = phoneDigits.length;
		if (phoneLen != 10) {
			msg = "The phone number you provided is\n" +
				phoneLen + " digits long. Is this correct?";
			if (phoneLen < 10)
				msg += "\n\nPlease include your area code.";
			return confirm(msg);
		}
	}
	return true;
}

//
// Parent Address Same as Patient
//

function fatherSameAsPatient(c) {
	if (c.checked == true ) {
		f = c.form;
		f.father_address.value = f.patient_address.value;
		f.father_city.value = f.patient_city.value;
		f.father_state.value = f.patient_state.value;
		f.father_zip.value = f.patient_zip.value;
	}
}

function motherSameAsPatient(c) {
	if (c.checked == true ) {
		f = c.form;
		f.mother_address.value = f.patient_address.value;
		f.mother_city.value = f.patient_city.value;
		f.mother_state.value = f.patient_state.value;
		f.mother_zip.value = f.patient_zip.value;
	}
}

//
// Validate Patient General Information Form
//

function validateGenInfoForm(f) {
	var pattern = "";
	validateMsg = "";
	validateCount = 0;
	focusSet = 0;

// Check that all fields are populated correctly
	
	if ( trim(f.first_name.value) == "" ) {
		validateMsg = "    First Name\n";
		validateCount += 1;
		f.first_name.focus();
		focusSet = 1;
	}
	if ( trim(f.last_name.value) == "" ) {
		validateMsg += "    Last Name\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.last_name.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.birth_date.value) == "" ) {
		validateMsg += "    Patient Date of Birth\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.birth_date.focus();
			focusSet = 1;
		}
	}
	if ( !f.gender[0].checked && !f.gender[1].checked ) {
		validateMsg += "    Gender\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.gender[0].focus();
			focusSet = 1;
		}
	}
/*
	if ( trim(f.ssn.value) == "" ) {
		validateMsg += "    Patient Social Security Number\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.ssn.focus();
			focusSet = 1;
		}
	}
*/
	if ( trim(f.patient_address.value) == "" ) {
		validateMsg += "    Patient Street Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.patient_address.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.patient_city.value) == "" ) {
		validateMsg += "    Patient City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.patient_city.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.patient_state.value) == "" ) {
		validateMsg += "    Patient State\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.patient_state.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.patient_zip.value) == "" ) {
		validateMsg += "    Patient Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.patient_zip.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.patient_phone.value) == "" && trim(f.patient_phone.value) == "" && trim(f.patient_phone.value) == "" ) {
		validateMsg += "    Parent or Patient Home Phone Number\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.father_phone.focus();
			focusSet = 1;
		}
	}
	if ( f.race.selectedIndex == 0 ) {
		validateMsg += "    Race\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.race.focus();
			focusSet = 1;
		}
	}
	if ( f.ethnicity.selectedIndex == 0 ) {
		validateMsg += "    Ethnic Group\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.ethnicity.focus();
			focusSet = 1;
		}
	}
	if ( f.language.selectedIndex == 0 ) {
		validateMsg += "    Language\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.language.focus();
			focusSet = 1;
		}
	}
/*
	if ( trim(f.patient_email.value) == "" && trim(f.patient_email.value) == "" && trim(f.patient_email.value) == "" ) {
		validateMsg += "    Parent or Patient E-mail Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.father_email.focus();
			focusSet = 1;
		}
	}
*/
	if ( trim(f.emergency_contact.value) == "" ) {
		validateMsg += "    Emergency Contact Name\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergency_contact.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.emergency_relationship.value) == "" ) {
		validateMsg += "    Emergency Contact Relationship to Patient\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergency_relationship.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.emergency_phone.value) == "" && trim(f.emergency_cell.value) == "" &&  trim(f.emergency_work.value) == "" ) {
		validateMsg += "    Emergency Home/Cell/Work Phone\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergency_phone.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.insurance_company.value) != "" && trim(f.claims_address.value) != "" ) {
	    if ( trim(f.claims_city.value) == "" ) {
		validateMsg += "    Claims Address City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.claims_city.focus();
			focusSet = 1;
		}
	    }
	    if ( trim(f.claims_state.value) == "" ) {
		validateMsg += "    Claims Address State\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.claims_state.focus();
			focusSet = 1;
		}
	    }
	    if ( trim(f.claims_zip.value) == "" ) {
		validateMsg += "    Claims Address Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.claims_zip.focus();
			focusSet = 1;
		}
	    }
	    if ( trim(f.primary_insured.value) == "" ) {
		validateMsg += "    Name of Primary Insured or Policy Owner\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.primary_insured.focus();
			focusSet = 1;
		}
	    }
	    if ( trim(f.insurance_id.value) == "" && trim(f.policy_number.value) == "" ) {
		validateMsg += "    Patient ID/Policy Number\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insurance_id.focus();
			focusSet = 1;
		}
	    }
	}
	if ( !f.we_call_cell.checked && !f.we_call_home.checked && !f.we_call_work.checked ) {
		validateMsg += "    May we call?\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.we_call_cell.focus();
			focusSet = 1;
		}
	}
	if ( !f.contact_email_yn[0].checked && !f.contact_email_yn[1].checked ) {
		validateMsg += "    May we contact you by e-mail?\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.contact_email_yn[0].focus();
			focusSet = 1;
		}
	}
	if ( !f.pref_contact_cell.checked && !f.pref_contact_home.checked && !f.pref_contact_work.checked && !f.pref_contact_email.checked ) {
		validateMsg += "    What is your preferred correspondence with our office?\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.pref_contact_cell.focus();
			focusSet = 1;
		}
	}
	if ( !f.leave_msg_cell.checked && !f.leave_msg_home.checked && !f.leave_msg_work.checked ) {
		validateMsg += "    May we leave messages?\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.leave_msg_cell.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.how_heard.value) == "" ) {
		validateMsg += "    How did you hear about us or whom may we thank for your referral?\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.how_heard.focus();
			focusSet = 1;
		}
	}

// Announce any missing fields

	if ( validateCount > 0 ) {
		var instructions = "Please complete the following field";
		if ( validateCount > 1 ) {
			instructions += "s:\n";
		}
		else {
			instructions += ":\n";
		}
		alert( instructions + validateMsg );
		return false;
	}

// Validate dates

	if (trim(f.birth_date.value) != "" ) {
		if (!validateDate(f.birth_date, "Patient Birth Date")) return false;
	}
	if (trim(f.father_birthdate.value) != "" ) {
		if (!validateDate(f.father_birthdate, "Father's Birth Date")) return false;
	}
	if (trim(f.mother_birthdate.value) != "" ) {
		if (!validateDate(f.mother_birthdate, "Mother's Birth Date")) return false;
	}

// Validate e-mail addresses if provided

	if (trim(f.patient_email.value) != "" ) {
		if (!validateEmail(f.patient_email)) return false;
	}
	if (trim(f.father_email.value) != "" ) {
		if (!validateEmail(f.father_email)) return false;
	}
	if (trim(f.mother_email.value) != "" ) {
		if (!validateEmail(f.mother_email)) return false;
	}

// Validate phone number if provided

	if (trim(f.patient_phone.value) != "" ) {
		if (!validatePhone(f.patient_phone)) return false;
	}
	if (trim(f.patient_cell.value) != "" ) {
		if (!validatePhone(f.patient_cell)) return false;
	}
	if (trim(f.father_phone.value) != "" ) {
		if (!validatePhone(f.father_phone)) return false;
	}
	if (trim(f.father_cell.value) != "" ) {
		if (!validatePhone(f.father_cell)) return false;
	}
	if (trim(f.father_work.value) != "" ) {
		if (!validatePhone(f.father_work)) return false;
	}
	if (trim(f.mother_phone.value) != "" ) {
		if (!validatePhone(f.mother_phone)) return false;
	}
	if (trim(f.mother_cell.value) != "" ) {
		if (!validatePhone(f.mother_cell)) return false;
	}
	if (trim(f.mother_work.value) != "" ) {
		if (!validatePhone(f.mother_work)) return false;
	}
	if (trim(f.emergency_phone.value) != "" ) {
		if (!validatePhone(f.emergency_phone)) return false;
	}
	if (trim(f.emergency_cell.value) != "" ) {
		if (!validatePhone(f.emergency_cell)) return false;
	}
	if (trim(f.emergency_work.value) != "" ) {
		if (!validatePhone(f.emergency_work)) return false;
	}

// Request Final Review

	return (confirm("Click OK to submit your Patient General Information Form.\n\n" + "Click Cancel to review or to make changes."));
}


//
// Validate Patient Medical History Form
//

function validateMedHistoryForm(f) {
	var pattern = "";
	validateMsg = "";
	validateCount = 0;
	focusSet = 0;

// Check that all fields are populated correctly
	
	if ( trim(f.first_name.value) == "" ) {
		validateMsg = "    First Name\n";
		validateCount += 1;
		f.first_name.focus();
		focusSet = 1;
	}
	if ( trim(f.last_name.value) == "" ) {
		validateMsg += "    Last Name\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.last_name.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.birth_date.value) == "" ) {
		validateMsg += "    Date of Birth\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.birth_date.focus();
			focusSet = 1;
		}
	}
	if ( !f.delivery_type[0].checked && !f.delivery_type[1].checked ) {
		validateMsg += "    Delivery Type\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.delivery_type[0].focus();
			focusSet = 1;
		}
	}
	if ( f.delivery_type[1].checked && trim(f.why_caesarian.value) == "" ) {
		validateMsg += "    If Caesarian, Why?\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.why_caesarian.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.gestation_age.value) == "" ) {
		validateMsg += "    Gestational Age\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.gestation_age.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.birth_weight.value) == "" ) {
		validateMsg += "    Birth Weight\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.birth_weight.focus();
			focusSet = 1;
		}
	}
	if ( trim(f.birth_length.value) == "" ) {
		validateMsg += "    Birth Length\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.birth_length.focus();
			focusSet = 1;
		}
	}
	if ( !f.hearing_screen[0].checked && !f.hearing_screen[1].checked && !f.hearing_screen[2].checked ) {
		validateMsg += "    Hearing Screen\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.hearing_screen[0].focus();
			focusSet = 1;
		}
	}
	if ( f.allergy.checked && trim(f.allergy_desc.value) == "" ) {
		validateMsg += "    Specify Allergies\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.allergy_desc.focus();
			focusSet = 1;
		}
	}
	if ( f.dev_delay.checked && trim(f.dev_delay_desc.value) == "" ) {
		validateMsg += "    Specify Developmental Delay\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.dev_delay.focus();
			focusSet = 1;
		}
	}
	if ( f.other_problems.checked && trim(f.other_prob_desc.value) == "" ) {
		validateMsg += "    Specify Other Chronic Problems\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.other_prob_desc.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_diabetes.checked && trim(f.fh_diabetes_who.value) == "" ) {
		validateMsg += "    Diabetes: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_diabetes_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_seizures.checked && trim(f.fh_seizures_who.value) == "" ) {
		validateMsg += "    Seizures: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_seizures_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_asthma.checked && trim(f.fh_asthma_who.value) == "" ) {
		validateMsg += "    Asthma: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_asthma_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_cholesterol.checked && trim(f.fh_chol_who.value) == "" ) {
		validateMsg += "    High Cholesterol: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_chol_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_heart.checked && trim(f.fh_heart_who.value) == "" ) {
		validateMsg += "    Heart Disease: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_heart_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_anemia.checked && trim(f.fh_anemia_who.value) == "" ) {
		validateMsg += "    Anemia: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_anemia_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_hypertension.checked && trim(f.fh_hypert_who.value) == "" ) {
		validateMsg += "    High Blood Pressure: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_hypert_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_cancer.checked && trim(f.fh_cancer_who.value) == "" ) {
		validateMsg += "    Cancer: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_cancer_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_eczema.checked && trim(f.fh_eczema.value) == "" ) {
		validateMsg += "    Eczema: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_eczema.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_allergy.checked && trim(f.fh_allergy_who.value) == "" ) {
		validateMsg += "    Allergies: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_allergy_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_bleeding.checked && trim(f.fh_bleeding_who.value) == "" ) {
		validateMsg += "    Bleeding Disorder: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_bleeding_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_liver.checked && trim(f.fh_liver_who.value) == "" ) {
		validateMsg += "    Liver Disease: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_liver_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_kidney.checked && trim(f.fh_kidney_who.value) == "" ) {
		validateMsg += "    Kidney Disease: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_kidney_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_mental.checked && trim(f.fh_mental_who.value) == "" ) {
		validateMsg += "    Mental Health: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_mental_who.focus();
			focusSet = 1;
		}
	}
	if ( f.fh_other.checked && trim(f.fh_other_who.value) == "" ) {
		validateMsg += "    Other Medical Problems: List Family Members\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fh_other_who.focus();
			focusSet = 1;
		}
	}

// Announce any missing fields

	if ( validateCount > 0 ) {
		var instructions = "Please complete the following field";
		if ( validateCount > 1 ) {
			instructions += "s:\n";
		}
		else {
			instructions += ":\n";
		}
		alert( instructions + validateMsg );
		return false;
	}

// Validate dates

	if (trim(f.birth_date.value) != "" ) {
		if (!validateDate(f.birth_date, "Date of Birth")) return false;
	}

// Request Final Review

	return (confirm("Click OK to submit your Patient Medical History Form.\n\n" + "Click Cancel to review or to make changes."));
}

function validatePhone(p, plabel) {
		var phoneDigits = getDigits(p.value);
		var phoneLen = phoneDigits.length;
		if (phoneLen != 10) {
			msg = "The " + plabel + " you provided is\n" +
				phoneLen + " digits long. Is this correct?";
			if (phoneLen < 10)
				msg += "\n\nPlease include your area code.";
			if (!confirm(msg)) {
				p.focus();
				return false;
			}
		}
		return true;
}

function getDigits(str) {

	var digits = new String("");

	for ( var i = 0; i < str.length; i++ ) {
		if ( str.charAt(i) >= '0' && str.charAt(i) <= '9' ) {
			digits = digits.concat(str.charAt(i));
		}
	}
	return digits;
}

// ============================================================
// Function to check if character is whitespace
// ============================================================

function isWhitespace(c) {
    whitespace = " \t\n\r";
    if (whitespace.indexOf(c) < 0) {
	return false;
    }
    return true;
}

// ============================================================
// Function to check if whitespace is embedded in string
// ============================================================

function hasWhitespace(s) {
   var sBuff = trim(s);
   var bLength = sBuff.length;

// Check for embedded whitespace

    for (var sPos = 0; sPos < bLength; sPos++) {
	if (isWhitespace(sBuff.charAt(sPos))) return true;
    }
    return false;
}

// ============================================================
// Function to remove leading and trailing whitespace
// ============================================================

function trim(s) {

   var sBuff = s;
   var sLength = s.length;

// Remove leading whitespace

    for (var sPos = 0; sPos < sLength; sPos++) {
	if (!isWhitespace(s.charAt(sPos))) {
	    sBuff = s.substr(sPos);
	    break;
	}
    }

// Remove trailing whitespace

    var bLength = sBuff.length;
    for (sPos = bLength - 1; sPos >= 0; sPos--) {
	if (isWhitespace(sBuff.charAt(sPos))) {
	    return sBuff.substr(0, (sPos + 1));
	    break;
	}
    }
    return sBuff;
}

function formatSSN(ssnInput) {

	var digits = getDigits(ssnInput.value);

	if (digits.length == 0 && ssnInput.length != 0) {
		alert("Please enter a valid Social Security Number.");
		ssnInput.focus();
		return false;
	}

// If 9 digits long, format ###-##-####

	if ( digits.length == 9 ) {
		ssnInput.value = digits.substr(0,3) + "-" + digits.substr(3,2) + "-" + digits.substr(5);
	}
	return true;
}

function formatZipCode(zipInput) {

	var digits = getDigits(zipInput.value);

	if (digits.length == 0 && zipInput.value.length > 0) {
		alert("Please enter a valid US Zip Code.");
		zipInput.focus();
		return false;
	}
	if (digits.length != 0 && digits.length != 5 && digits.length != 9) {
		alert("US Zip Code should be 5 or 9 digits long.");
		zipInput.focus();
		return false;
	}

// If 9 digits long, format #####-####

	if ( digits.length == 9 ) {
		zipInput.value = digits.substr(0,5) + "-" + digits.substr(5);
	}
	return true;
}

function formatPhoneNumber(phoneInput) {

	var s = "";
	var digits = getDigits(phoneInput.value);

	if (digits.length == 0 && phoneInput.value.length > 0) {
		alert("Please enter a valid Phone Number.");
		phoneInput.focus();
		return false;
	}

// Require area code (North American standard)

	if ( digits.length < 10 && digits.length != 0) {
		alert("Please provide area code and phone number");
		phoneInput.focus();
		return false;
	}

// If 10 digits long, format (npa) nxx-line

	if ( digits.length == 10 ) {
		phoneInput.value = digits.substr(0,3) + "-" + digits.substr(3,3) + "-" + digits.substr(6);
	}
	return true;
}

function validateDate(t, tlabel) {
    var dateChars  = "01234567890/-";
    var s = trim(t.value);

    if (s.length == 0) return false;
    if (s.length != 10 && s.length != 0) {
	alert("Invalid " + tlabel + "date format:  Use the form mm-dd-yyyy or mm/dd/yyyy, with no spaces.");
	t.focus();
	return false;
    }
    for (var i = 0; i < s.length; i++) {
	if (dateChars.indexOf(s.charAt(i)) < 0) {
	    alert("Invalid characters in" + tlabel + " date:  Use only digits and slashes in the form mm-dd-yyyy or mm/dd/yyyy.");
	    t.focus();
	    return false;
	}
    }
    var yr = s.substr(6, 4) - 0;
    var month = s.substr(0, 2) - 0;
    var day  = s.substr(3, 2) - 0;
    if (yr < 1900) {
	alert("Check" + tlabel + " year:  Minimum allowed is 1900.");
	t.focus();
	return false;
    }
    if (month < 1 || month > 12) {
	alert("Invalid" + tlabel + " month: Should be between 01 and 12.");
	t.focus();
	return false;
    }
    switch(month) {
	case  1 : 
	case  3 : 
	case  5 : 
	case  7 : 
	case  8 : 
	case 10 : 
	case 12 : if (day > 0 && day <= 31) return true; break;
	case  4 : 
	case  6 : 
	case  9 : 
	case 11 : if (day > 0 && day <= 30) return true; break;
	default : if ((yr % 4 == 0 && day <= 29) || (yr % 4 != 0 && day <= 28)) return true; break;
    }
    alert("Check" + tlabel + " day " + day + ": Should be between 1 and appropriate End-of-Month (30, 31, 28 or 29).");
    t.focus();
    return false;
}

function validateEmail(emailInput) {
	var email_pattern = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]+$/;
	if((result = email_pattern.exec(emailInput.value)) == null) {
		alert("Please check for a valid e-mail address format\nExamples: john@yahoo.com, jane.doe@my-isp.net");
		emailInput.focus();
		return false;
	}
	return true;	
}

/*

<nobr>
handle<span style="font-size:1px;">
</span>&#064;<span style="font-size:1px;">
</span>domain<span style="font-size:1px;">
</span>&#046;<span style="font-size:1px;">
</span>com
</nobr>

OR

<script language="javascript">
<!--
function buildEmail(a, domain, handle) {
	a.href = "mailto:" + handle + "@" + domain;
}
//-->
</script>
<a
href="javascript:void(0)"
onMouseOver="buildEmail(this, 'domain.ext', 'handle');"
class="copyright">
 	<nobr>
	handle<span style="font-size:1px;">
	</span>&#064;<span style="font-size:1px;">
	</span>domain<span style="font-size:1px;">
	</span>&#046;<span style="font-size:1px;">
	</span>ext</nobr>
</a>

<p><img src="imgcounter.php?filename=home&color=000000">
CAVEAT - Verify:
Perms 446 or 447
Path /imgcounter.php, relative or absolute
*/

