var glb_vfld;

function setFocusDelayed()
{
  glb_vfld.focus()
}

function setfocus(vfld)
{
  // save vfld in global variable so value retained when routine exits
  glb_vfld = vfld;
  setTimeout( 'setFocusDelayed()', 100 );
}

function validateTelephone(inputToCheck,msg_id) {
	var stripped = inputToCheck.value.replace(/[\(\)\.\-\ ]/g, '');
	inputToCheck.value = stripped;
	//strip out acceptable non-numeric characters
	if (isNaN(stripped)) {
	  error = "The phone number contains illegal characters.";
	  document.getElementById(msg_id).innerHTML = error;
	  setfocus(inputToCheck);
	  return false;
	}
	if (!(stripped.length == 10) && !(stripped.length == 7) && !(stripped.length == 0)) {
	  error = "The phone number is the wrong length.";
	  document.getElementById(msg_id).innerHTML = error;
	  setfocus(inputToCheck);
	  return false;
	}
	
	error = "";
	document.getElementById(msg_id).innerHTML = error;
	return true;
}

function validateSSN(inputToCheck,msg_id) {
	var stripped = inputToCheck.value.replace(/[\(\)\.\-\ ]/g, '');
	inputToCheck.value = stripped;
	//strip out acceptable non-numeric characters
	if (!(stripped.length == 9) && !(stripped.length == 0)) {
	  error = "The SSN is the wrong length.";
	  document.getElementById(msg_id).innerHTML = error;
	  setfocus(inputToCheck);
	  return false;
	}
	
	error = "";
	document.getElementById(msg_id).innerHTML = error;
	return true;
}

function validateZip(inputToCheck,msg_id) {
	var stripped = inputToCheck.value.replace(/[\(\)\.\-\ ]/g, '');
	inputToCheck.value = stripped;
	//strip out acceptable non-numeric characters
	if (isNaN(stripped)) {
	  error = "The zip contains illegal characters.";
	  document.getElementById(msg_id).innerHTML = error;
	  setfocus(inputToCheck);
	  return false;
	}
	if (!(stripped.length == 5) && !(stripped.length == 0)) {
	  error = "The zip code is not a valid 5 number zip code";
	  document.getElementById(msg_id).innerHTML = error;
	  setfocus(inputToCheck);
	  return false;
	}
	error = "";
	document.getElementById(msg_id).innerHTML = error;
	return true;
}

function validateAge(inputToCheck,msg_id) {
    var stripped = inputToCheck.value.replace(/[\(\)\.\-\ ]/g, '');
	inputToCheck.value = stripped;
	//strip out acceptable non-numeric characters
	if (isNaN(stripped)) {
	  error = "Age must be in whole number years";
	  document.getElementById(msg_id).innerHTML = error;
	  setfocus(inputToCheck);
	  return false;
	}
	error = "";
	document.getElementById(msg_id).innerHTML = error;
	return true;
}

function checkWholeForm(theForm) {
    var good=1;
	if(!validateZip(document.getElementById(theForm).Zip,'ZipError'))
	  { alert("Error with your:\nPatient Zip Code"); return false; }
	if(!validateSSN(document.getElementById(theForm).ResponsiblePatientsSSN,'ResponsiblePatientsSSNError'))
	  { alert("Error with your:\nResponsible Party's SSN"); return false; }
	  
	if(document.getElementById(theForm).FirstName.value=="") { alert("Patient's First Name is Empty"); return false; }
	if(document.getElementById(theForm).LastName.value=="") { alert("Patient's Last Name is Empty"); return false; }
	if(!document.getElementById(theForm).Sex[0].checked && 
	   !document.getElementById(theForm).Sex[1].checked)
	  { alert("Patient's Sex is Empty"); return false; }
	if(document.getElementById(theForm).StreetAddress.value=="") { alert("Patient's Street Address is Empty"); return false; }
	if(document.getElementById(theForm).City.value=="") { alert("Patient's City is Empty"); return false; }
	if(document.getElementById(theForm).State.value=="") { alert("Patient's State is Empty"); return false; }
	if(document.getElementById(theForm).Zip.value=="") { alert("Patient's Zip Code is Empty"); return false; }
	if(!document.getElementById(theForm).EmploymentStatus[0].checked &&
	   !document.getElementById(theForm).EmploymentStatus[1].checked &&
	   !document.getElementById(theForm).EmploymentStatus[2].checked)
	   { alert("Patient's Employment is Empty"); return false; }
	if(document.getElementById(theForm).EmployerName.value=="") { alert("Employer name is Empty"); return false; }
	if(!validateTelephone(document.getElementById(theForm).Phone1,'Phone1Error'))
	  { alert("Error with your:\nPatient Phone #1"); return false; }
	if(!validateTelephone(document.getElementById(theForm).Phone2,'Phone2Error'))
	  { alert("Error with your:\nPatient Phone #2"); return false; }
	if(!validateTelephone(document.getElementById(theForm).AdditionalPhone,'AdditionalPhoneError'))
      { alert("Error with your:\nPatient Additional Phone"); return false; }
	if(document.getElementById(theForm).DateOfBirth.value=="" || document.getElementById(theForm).DateOfBirth.value=="YYYY-MM-DD")
	  { alert("Patient's Date of Birth has not been filled out"); return false; }
	if(!document.getElementById(theForm).MaritalStatus[0].checked &&
	   !document.getElementById(theForm).MaritalStatus[1].checked &&
	   !document.getElementById(theForm).MaritalStatus[2].checked &&
	   !document.getElementById(theForm).MaritalStatus[3].checked)
	   { alert("Patient's Marital Status is Empty"); return false; }
	
	if(document.getElementById(theForm).NameofPrimaryInsured.value=="") { alert("Primary's Name is Empty"); return false; }
	if(document.getElementById(theForm).PrimaryInsuranceCompany.value=="") { alert("Primary's Insurance Company is Empty"); return false; }
	if(document.getElementById(theForm).PrimaryGroupNumber.value=="") { alert("Primary's Group Number is Empty"); return false; }
	if(document.getElementById(theForm).PrimaryIDorPolicyNumber.value=="") { alert("Primary's Policy ID Number is Empty"); return false; }
	if(document.getElementById(theForm).PrimaryInsuredsDateofBirth.value=="" || document.getElementById(theForm).PrimaryInsuredsDateofBirth.value=="YYYY-MM-DD")
	  { alert("Primary's Date of Birth has not been filled out"); return false; }
	if(document.getElementById(theForm).PrimaryEmployer.value=="") { alert("Primary Employer is Empty"); return false; }
	if(document.getElementById(theForm).PrimaryInsuredsRelationshiptoPatient.value=="") { alert("Primary's Relationship to Patient is Empty"); return false; }
	
	if(document.getElementById(theForm).SecondarySameAs.value != 1)
	{
		if(document.getElementById(theForm).NameofSecondaryInsured.value=="") { alert("Secondary's Name is Empty"); return false; }
		if(document.getElementById(theForm).SecondaryInsuranceCompany.value=="") { alert("Secondary's Insurance Company is Empty"); return false; }
		if(document.getElementById(theForm).SecondaryGroupNumber.value=="") { alert("Secondary's Group Number is Empty"); return false; }
		if(document.getElementById(theForm).SecondaryIDorPolicyNumber.value=="") { alert("Secondary's Policy ID Number is Empty"); return false; }
		if(document.getElementById(theForm).SecondaryInsuredsDateofBirth.value=="" || document.getElementById(theForm).SecondaryInsuredsDateofBirth.value=="YYYY-MM-DD")
			{ alert("Secondary's Date of Birth has not been filled out"); return false; }
		if(!validateTelephone(document.getElementById(theForm).SecondaryInsuredsPhone,'SecondaryInsuredsPhoneError'))
			{ alert("Error with your:\nSecondary Insured's Phone"); return false; }
		if(document.getElementById(theForm).SecondaryEmployer.value=="") { alert("Secondary's Employer is Empty"); return false; }
		if(document.getElementById(theForm).SecondaryInsuredsRelationshiptoPatient.value=="") { alert("Secondary's Relationship to Patient is Empty"); return false; }  
	}
	
	if(document.getElementById(theForm).ResponsiblePartySameAs.value != 1)
	{
		if(document.getElementById(theForm).ResponsibleFirstName.value=="") { alert("Responsible Party's First Name is Empty"); return false; }
		if(document.getElementById(theForm).ResponsibleLastName.value=="") { alert("Responsible Party's Last Name is Empty"); return false; }
		if(document.getElementById(theForm).ResponsibleStreetAddress.value=="") { alert("Responsible Party's Street Address is Empty"); return false; }
		if(document.getElementById(theForm).ResponsibleCity.value=="") { alert("Responsible Party's City is Empty"); return false; }
		if(document.getElementById(theForm).ResponsibleState.value=="") { alert("Responsible Party's State is Empty"); return false; }
		if(document.getElementById(theForm).ResponsibleZip.value=="") { alert("Responsible Party's Zip Code is Empty"); return false; }
		if(!validateZip(document.getElementById(theForm).ResponsibleZip,'ResponsibleZipError'))
			{ alert("Error with your:\nResponsible Party's Zip Code"); return false; }
		if(document.getElementById(theForm).ResponsiblePhone1.value=="") { alert("Responsible Party's Work Phone is Empty"); return false; }
		if(document.getElementById(theForm).ResponsiblePhone2.value=="") { alert("Responsible Party's Home Phone is Empty"); return false; }
		if(!validateTelephone(document.getElementById(theForm).ResponsiblePhone1,'ResponsiblePhone1Error'))
			{ alert("Error with your:\nResponsible Party's Phone #1"); return false; }
		if(!validateTelephone(document.getElementById(theForm).ResponsiblePhone2,'ResponsiblePhone2Error'))
			{ alert("Error with your:\nResponsible Party's Phone #2"); return false; }
		if(document.getElementById(theForm).ResponsibleDateOfBirth.value=="" || document.getElementById(theForm).ResponsibleDateOfBirth.value=="YYYY-MM-DD")
			{ alert("Responsible Party's Date of Birth is Empty"); return false; }
		if(document.getElementById(theForm).ResponsiblePatientsSSN.value=="") { alert("Responsible Party's SSN is Empty"); return false; } 
	}
	return true;
}