
function disable() {
	element = document.getElementById('rbtMayMailAddress');
	if(element.checked)	{
		element = document.getElementById('txtLocationAddress');
		element.readOnly=true;
		element.value='';
		element = document.getElementById('txtLocationCity');
		element.readOnly=true;
		element.value='';
		element = document.getElementById('txtLocationState');
		element.readOnly=true;
		element.value='';
		element = document.getElementById('txtLocationZipCode');
		element.readOnly=true;
		element.value='';
	}
	else {
		element = document.getElementById('txtLocationAddress');
		element.readOnly=false;
		element = document.getElementById('txtLocationCity');
		element.readOnly=false;
		element = document.getElementById('txtLocationState');
		element.readOnly=false;
		element = document.getElementById('txtLocationZipCode');
		element.readOnly=false;
	}
}

function SetDOB()
{
	var Year = document.getElementById('selectYear');
	var Day = document.getElementById('selectDay');
	var Month = document.getElementById('selectMonth');
	if (Year.value != 'Year' && Day.value != 'Day' && Month.value != 'Month')
	{
		document.getElementById('txtAge').value = CalculateAge(Day.value + "/" + Month.value + "/" + Year.value);
	}
	else
		document.getElementById('txtAge').value = '';
}

function ValidateDataPatient(theForm)
{	
	if (theForm.contactDate.value != "")
	{
		return (false);
	}
	
	if(theForm.selectDay.selectedIndex > 0 && theForm.selectMonth.selectedIndex > 0 && theForm.selectYear.selectedIndex > 0)
	   theForm.txtAge.value = CalculateAge(theForm.selectDay.value + "/" + theForm.selectMonth.value + "/" + theForm.selectYear.value);
	
	if (theForm.txtFirstName.value == "")
	{
		alert('Please, enter your first name.');
		theForm.txtFirstName.focus();
		return (false);
	}
	
	if (theForm.txtLastName.value == "")
	{
		alert('Please, enter your last name.');
		theForm.txtLastName.focus();
		return (false);
	}

	if (theForm.txtAddress.value == "")
	{
		alert('Please, enter your address.');
		theForm.txtAddress.focus();
		return (false);
	}
	
	if (theForm.txtCity.value == "")
	{
		alert('Please, enter your city.');
		theForm.txtCity.focus();
		return (false);
	}
	
	if (theForm.txtState.value == "")
	{
		alert('Please, enter your state.');
		theForm.txtState.focus();
		return (false);
	}
	
	if (theForm.txtZipCode.value == "")
	{
		alert("Please enter your Zip Code.");
		theForm.txtZipCode.focus();
		return (false);
	}
	else
	{
		if (theForm.txtZipCode.value.length != 5)
		{
			alert("Please enter 5 characters in the Zip Code field.");
			theForm.txtZipCode.focus();
			return (false);
		}
		else 
		{
			if (theForm.txtZipCode.value.length == 5)
			{
				if (isNaN(theForm.txtZipCode.value)) 
				{
					alert("Please enter only numerical digits in the Zip Code field.");
					theForm.txtZipCode.focus();
					return false;	
				}					    
			}
		}
	}
    
	if (theForm.rbtMayMailAddress[1].checked)
	{
		if (theForm.txtLocationAddress.value == "")
		{
			alert('Please, enter your address location to mail.');
			theForm.txtLocationAddress.focus();
			return (false);
		}
		
		if (theForm.txtLocationCity.value == "")
		{
			alert('Please, enter your city location to mail.');
			theForm.txtLocationCity.focus();
			return (false);
		}
		
		if (theForm.txtLocationState.value == "")
		{
			alert('Please, enter your state location to mail.');
			theForm.txtLocationState.focus();
			return (false);
		}
		
		if (theForm.txtLocationZipCode.value == "")
		{
			alert("Please enter your Zip Code location to mail.");
			theForm.txtLocationZipCode.focus();
			return (false);
		}
		else
		{
			if (theForm.txtLocationZipCode.value.length != 5)
			{
				alert("Please enter 5 characters in the Zip Code field.");
				theForm.txtLocationZipCode.focus();
				return (false);
			}
			else 
			{
				if (theForm.txtLocationZipCode.value.length == 5)
				{
					if (isNaN(theForm.txtLocationZipCode.value)) 
					{
						alert("Please enter only numerical digits in the Zip Code field.");
						theForm.txtLocationZipCode.focus();
						return false;	
					}					    
				}
			}
		}  
	}
	
	if (theForm.selectMonth.selectedIndex < 0)
	{
		alert("Please select one Month.");
		theForm.selectMonth.focus();
		return (false);
	}

	if (theForm.selectMonth.selectedIndex == 0)
	{
		alert("Please select one Month.");
		theForm.selectMonth.focus();
		return (false);
	}
	
	if (theForm.selectDay.selectedIndex < 0)
	{
		alert("Please select one Day.");
		theForm.selectDay.focus();
		return (false);
	}

	if (theForm.selectDay.selectedIndex == 0)
	{
		alert("Please select one Day.");
		theForm.selectDay.focus();
		return (false);
	}
	
	if (theForm.selectYear.selectedIndex < 0)
	{
		alert("Please select one Year.");
		theForm.selectYear.focus();
		return (false);
	}

	if (theForm.selectYear.selectedIndex == 0)
	{
		alert("Please select one Year.");
		theForm.selectYear.focus();
		return (false);
	}

	if (theForm.txtEmail.value == "")
	{
		alert('Please, enter your email address.');
		theForm.txtEmail.focus();
		return (false);
	}
	else
	{
		if(!ValidateEmail(theForm.txtEmail.value))
		{
			alert("Please check your email address.");
			theForm.txtEmail.focus();
			return false;
		}
	}
	
	if (theForm.rbtMayCallPhoneNumberHome[0].checked)
	{
		if (theForm.txtPhoneNumberHome.value == "")
		{
			alert('Please, enter your phone number (home).');
			theForm.txtPhoneNumberHome.focus();
			return (false);
		}	    
	}
	
	if (theForm.rbtMayCallPhoneNumber[0].checked)
	{
		if (theForm.txtPhoneNumber.value == "")
		{
			alert('Please, enter your phone number.');
			theForm.txtPhoneNumber.focus();
			return (false);
		}	    
	}
	
	if (theForm.rbtMayCallPhoneNumberEmployer[0].checked)
	{
		if (theForm.txtPhoneNumberEmployer.value == "")
		{
			alert('Please, enter your phone number (employer).');
			theForm.txtPhoneNumberEmployer.focus();
			return (false);
		}	    
	}
	
	if (theForm.txtEmergencyContactPerson.value == "")
	{
		alert('Please, enter your emergency contact person.');
		theForm.txtEmergencyContactPerson.focus();
		return (false);
	}
	
	if (theForm.txtPhoneNumberEmergency.value == "")
	{
		alert('Please, enter your phone number (emergency).');
		theForm.txtPhoneNumberEmergency.focus();
		return (false);
	}
	
	if (theForm.txtRelationshipToPatient.value == "")
	{
		alert("Please, enter the relationship to patient.");
		theForm.txtRelationshipToPatient.focus();
		return (false);
	}
		
	theForm.txtAge.value = CalculateAge(theForm.selectDay.value + "/" + theForm.selectMonth.value + "/" + theForm.selectYear.value);
	
	if ((theForm.txtDateOfInjury.value != ""))
	{
		if(!ValidateDate(theForm.txtDateOfInjury.value))
		{
			alert("Please check the date of injury");
			theForm.txtDateOfInjury.focus();
			return false;
		}
	}
	
	if ((theForm.txtAccidentDate.value != ""))
	{
		if(!ValidateDate(theForm.txtAccidentDate.value))
		{
			alert("Please check the accident date");
			theForm.txtAccidentDate.focus();
			return false;
		}
	}
	
	if ((theForm.txtDateUnder18.value == ""))
	{
	     alert("Please check the date");
		 theForm.txtDateUnder18.focus();
		 return false;
	}
	else	
	{
		if(!ValidateDate(theForm.txtDateUnder18.value))
		{
			alert("Please check the date");
			theForm.txtDateUnder18.focus();
			return false;
		}
	}
	
	if ((theForm.txtDateResponsible.value == ""))
	{
	     alert("Please check the date");
		 theForm.txtDateResponsible.focus();
		 return false;
	}	
	else
	{
		if(!ValidateDate(theForm.txtDateResponsible.value))
		{
			alert("Please check the date");
			theForm.txtDateResponsible.focus();
			return false;
		}
	}
	
	if ((theForm.txtDatePatientSignature.value != ""))
	{
		if(!ValidateDate(theForm.txtDatePatientSignature.value))
		{
			alert("Please check the date");
			theForm.txtDatePatientSignature.focus();
			return false;
		}
	}
		
	if(parseFloat(theForm.txtAge.value) < 18)
	{
		if (theForm.txtNameOfPersonSSNDOB.value == "")
		{
			alert("Please, enter the name of person who carries insurance SSN DOB.");
			theForm.txtNameOfPersonSSNDOB.focus();
			return (false);
		}
		
		if (theForm.txtParentSignatureUnder18.value == "")
		{
			alert("Please, enter the signature of parent/guardian.");
			theForm.txtParentSignatureUnder18.focus();
			return (false);
		}
		
		if ((theForm.txtDateParentSignatureUnder18.value == ""))
		{
		     alert("Please check the date");
			 theForm.txtDateParentSignatureUnder18.focus();
			 return false;
		}		
		else
		{
			if(!ValidateDate(theForm.txtDateParentSignatureUnder18.value))
			{
				alert("Please check the date");
				theForm.txtDateParentSignatureUnder18.focus();
				return false;
			}
		}   
	}
	else
	{
		if (theForm.txtPatientSignatureOlder.value == "")
		{
			alert("Please, enter the patient's signature.");
			theForm.txtPatientSignatureOlder.focus();
			return (false);
		}
		
		if ((theForm.txtDatePatientSignature.value == ""))
		{
		     alert("Please check the date");
			 theForm.txtDatePatientSignature.focus();
			 return false;
		}		
		else
		{
			if(!ValidateDate(theForm.txtDateParentSignatureUnder18.value))
			{
				alert("Please check the date");
				theForm.txtDateParentSignatureUnder18.focus();
				return false;
			}
		}	
	}
	
	if ((theForm.txtNameUnder18.value == ""))
	{
	     alert("Please enter the name");
		 theForm.txtNameUnder18.focus();
		 return false;
	}
	
	if ((theForm.txtNameResponsible.value == ""))
	{
	     alert("Please enter the name");
		 theForm.txtNameResponsible.focus();
		 return false;
	}
}

function ValidateDate(fld) 
{
 var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
 if ((fld.match(RegExPattern))) {
     var ar = fld.split("/"); 
     var a = ar[2];
     if(a.length==4) 
      return true;
     else
      return false; 
 } 
 else{
     return false;
 } 
}

function ValidateEmail(valor) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
		return true;
	else
		return false;
}

function CalculateAge(lpDate)
{ 
    lvNow = new Date()
    var array_date = lpDate.split("/") 
    if (array_date.length!=3) 
       return '' 

    var lvYear 
    lvYear = parseInt(array_date[2]); 
    if (isNaN(lvYear)) 
       return '' 

    var lvMonth
    lvMonth = parseInt(array_date[1]); 
    if (isNaN(lvMonth)) 
       return '' 

    var lvDay 
    lvDay = parseInt(array_date[0]); 
    if (isNaN(lvDay)) 
       return '' 

    if (lvYear<=99) 
       lvYear +=1900 

    lvEdad=lvNow.getFullYear()- lvYear - 1;
    if (lvNow.getMonth() + 1 - lvMonth < 0)
       return lvEdad 
    if (lvNow.getMonth() + 1 - lvMonth > 0) 
       return lvEdad+1 

    if (lvNow.getUTCDate() - lvDay >= 0) 
       return lvEdad + 1 

    return lvEdad 
}
