var validateMsg = "";
var validateCount = 0;
var petRadioClicked = false;
var gateRadioClicked = false;
var fsForm = document.forms[0];

function setSameAsPatient(c, f) {
    if ( c.checked == true ) {
	eval("f." + c.value + "address.value = f.address.value;");
	eval("f." + c.value + "city.value = f.city.value;");
	eval("f." + c.value + "state.value = f.state.value;");
	eval("f." + c.value + "zip.value = f.zip.value;");
	eval("f." + c.value + "phone.value = f.phone.value;");
    }
}

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

// Check that all fields are populated correctly
	
	if ( f.lastname.value == "" ) {
		validateMsg = "    Last Name\n";
		validateCount += 1;
		f.lastname.focus();
		focusSet = 1;
	}
	if ( f.firstname.value == "" ) {
		validateMsg += "    First Name\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.firstname.focus();
			focusSet = 1;
		}
	}
	if ( f.gender[0].checked == false && f.gender[1].checked == false  ) {
		validateMsg += "    Gender\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.gender[0].focus();
			focusSet = 1;
		}
	}
	if ( f.birthdate.value == "" ) {
		validateMsg += "    Date Of Birth\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.birthdate.focus();
			focusSet = 1;
		}
	}
	if ( f.address.value == "" ) {
		validateMsg += "    Street Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.address.focus();
			focusSet = 1;
		}
	}
	if ( f.city.value == "" ) {
		validateMsg += "    City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.city.focus();
			focusSet = 1;
		}
	}
	if ( f.zip.value == "" ) {
		validateMsg += "    Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.zip.focus();
			focusSet = 1;
		}
	}
	if ( f.phone.value == "" ) {
		validateMsg += "    Home Phone\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.phone.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[0].checked == false &&
	     f.responsible[1].checked == false &&
	     f.responsible[2].checked == false  ) {
		validateMsg += "    Billing Responsibility\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.responsible[0].focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[0].checked == true && f.fathername.value == "" ) { // Father
		validateMsg += "    Father's Name\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fathername.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[0].checked == true && f.fatherssn.value == "" ) { // Father
		validateMsg += "    Father's SSN\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fatherssn.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[0].checked == true && f.fatheraddress.value == "" ) { // Father
		validateMsg += "    Father's Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fatheraddress.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[0].checked == true && f.fathercity.value == "" ) { // Father
		validateMsg += "    Father's City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fathercity.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[0].checked == true && f.fatherzip.value == "" ) { // Father
		validateMsg += "    Father's Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.fatherzip.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[1].checked == true && f.motherphone.value == "" ) { // Mother
		validateMsg += "    Mother's Home Phone\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.motherphone.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[1].checked == true && f.mothername.value == "" ) { // Mother
		validateMsg += "    Mother's Name\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.mothername.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[1].checked == true && f.motherssn.value == "" ) { // Mother
		validateMsg += "    Mother's SSN\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.motherssn.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[1].checked == true && f.motheraddress.value == "" ) { // Mother
		validateMsg += "    Mother's Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.motheraddress.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[1].checked == true && f.mothercity.value == "" ) { // Mother
		validateMsg += "    Mother's City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.mothercity.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[1].checked == true && f.motherzip.value == "" ) { // Mother
		validateMsg += "    Mother's Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.motherzip.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[1].checked == true && f.motherphone.value == "" ) { // Mother
		validateMsg += "    Mother's Home Phone\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.motherphone.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[2].checked == true && f.othername.value == "" ) { // Other
		validateMsg += "    Name Of Other Responsible Party\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.otherresp.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[2].checked == true && f.relation.value == "" ) { // Other
		validateMsg += "    Relationship To Patient\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.relation.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[2].checked == true && f.otheraddress.value == "" ) { // Other
		validateMsg += "    Other's Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.otheraddress.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[2].checked == true && f.othercity.value == "" ) { // Other
		validateMsg += "    Other's City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.othercity.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[2].checked == true && f.otherzip.value == "" ) { // Other
		validateMsg += "    Other's Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.otherzip.focus();
			focusSet = 1;
		}
	}
	if ( f.responsible[2].checked == true && f.otherphone.value == "" ) { // Other
		validateMsg += "    Other's Home Phone\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.otherphone.focus();
			focusSet = 1;
		}
	}
	if ( f.emergencyname.value == "" ) {
		validateMsg += "    Emergency Contact Name\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergencyname.focus();
			focusSet = 1;
		}
	}
	if ( f.emergencyphone.value == "" ) {
		validateMsg += "    Emergency Contact Phone\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergencyphone.focus();
			focusSet = 1;
		}
	}
	if ( f.emergencyaddress.value == "" ) {
		validateMsg += "    Emergency Contact Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergencyaddress.focus();
			focusSet = 1;
		}
	}
	if ( f.emergencycity.value == "" ) {
		validateMsg += "    Emergency Contact City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergencycity.focus();
			focusSet = 1;
		}
	}
	if ( f.emergencyzip.value == "" ) {
		validateMsg += "    Emergency Contact Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.emergencyzip.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insurancename.value == "" ) {
		validateMsg += "    Insurance Company\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insurancename.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insurancephone.value == "" ) {
		validateMsg += "    Insurance Company Phone\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insurancephone.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insuranceaddress.value == "" ) {
		validateMsg += "    Claims Mailing Address\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insuranceaddress.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insurancecity.value == "" ) {
		validateMsg += "    Insurance Company City\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insurancecity.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insurancezip.value == "" ) {
		validateMsg += "    Insurance Company Zip Code\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insurancezip.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insuredname.value == "" ) {
		validateMsg += "    Name of Primary Insured\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insuredname.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insuranceid.value == "" ) {
		validateMsg += "    Policy # or Insurance ID\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insuranceid.focus();
			focusSet = 1;
		}
	}
	if ( f.noinsurance.checked == false && f.insurancegroup.value == "" ) {
		validateMsg += "    Insurance Group\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.insurancegroup.focus();
			focusSet = 1;
		}
	}
	if ( f.certify.checked == false ) {
		validateMsg += "    Certify Insurance Information Is Correct\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.certify.focus();
			focusSet = 1;
		}
	}
	if ( f.authorizerelease.checked == false ) {
		validateMsg += "    Authorize Release Of Medical Information\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.authorizerelease.focus();
			focusSet = 1;
		}
	}
	if ( f.assignbenefits.checked == false ) {
		validateMsg += "    Assign Benefits For Covered Services\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.assignbenefits.focus();
			focusSet = 1;
		}
	}
	if ( f.permitcopy.checked == false ) {
		validateMsg += "    Permit Copy In Place Of Original\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.permitcopy.focus();
			focusSet = 1;
		}
	}
	if ( f.by.value == "" ) {
		validateMsg += "    Authorized By\n";
		validateCount += 1;
		if ( ! focusSet ) {
			f.by.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 zip codes



// Validate phone numbers



// Validate e-mail addresses



	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 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) {
    var dateChars  = "01234567890/-";
    var s = trim(t.value);

    if (s.length == 0) return false;
    if (s.length != 10 && s.length != 0) {
	alert("Invalid 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 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 year:  Minimum allowed is 1900.");
	t.focus();
	return false;
    }
    if (month < 1 || month > 12) {
	alert("Invalid 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 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;	
}


