// JavaScript Form Validation
// Corrective Speech And Language Therapy, Inc.
// www.centralfltherapy.com


var reqfields = new Array('patient_lname',
						 'patient_fname',
						 'pdob_month',
						 'pdob_day',
						 'pdob_year',
						 'guardian',
						 'guardian_names',
						 'patient_address',
						 'patient_city',
						 'patient_zip',
						 'patient_email',
						 'planguage',
						 'covtype',
						 'cov_pcp',
						 'cov_phone1',
						 'cov_phone2',
						 'cov_phone3',
						 'bg_history');

var reqInsurance = new Array('ins_policyholder',
						 'ins_policyholder_month',
						 'ins_policyholder_day',
						 'ins_policyholder_year',
						 'ins_policy_id',
						 'ins_group_number',
						 'ins_employer',
						 'ins_co_phone1',
						 'ins_co_phone2',
						 'ins_co_phone3',
						 'ins_company',
						 'ins_address',
						 'ins_city',
						 'ins_state',
						 'ins_zip',
						 'ins_type');


function validate_form(thisform) {
			
	// Form Fields and Drop down menus
	if (valfields(thisform)==false) return false;
	
	// Radio buttons	
	if (valbuttons(thisform.patient_sex)==false) return false;
	
	// Check if at least one phone number is provided
	if (valPhoneNumbers(thisform)==false) return false;
	
	// If Coverage type is Insurance, check all insurance fields
	if (checkCovType(thisform)==false) return false;	
		
	// Check if at least one Therapy needed is provided
	if (checkTherapy(thisform)==false) return false;	
		
	thisform.submit(); 
}


function valfields(thisform) {
	
	var i = 0;
	
	for (i=0; i < reqfields.length; i++) {
			
		if (!thisform[reqfields[i]].value) {
			
			errormsg();
			
			document.getElementsByName( thisform[reqfields[i]].name)[0].focus();
			
			return false;		
		}
	}
}



function valbuttons(buttons) {
	
	
	 var truefound = false;
	 var i=0;
	 
	 for (i=0; i< buttons.length; i++) {		 
		 var x = buttons[i].checked;		 
	 	if (x) truefound = true;		
	 }
	 
	 if (truefound) {
		return true;
	 }else{
		 
		 errormsg();
		 
		 document.getElementsByName( buttons[0].name)[0].focus();
		 
		 return false;	
	 }	
}

function errormsg() {	
	
	alert("Plese fill out all required information.");
			
}

function valPhoneNumbers(thisform) {
	
	var numfound = false;
	
	if (thisform['patient_phone1'].value) numfound = true;	
	
	if (thisform['patient_wphone1'].value) numfound = true;	
	
	if (thisform['patient_cphone1'].value) numfound = true;	
	
	if (numfound)
	{
		return true;
	}else{
		
		alert('Please provide at least one phone number contact.');
		
		return false;
		
	}
	
	
}

function checkCovType(thisform) {
	
	
	if (thisform['covtype'].value == "Insurance"){
			
		var i = 0;
	
		for (i=0; i < reqInsurance.length; i++) {
			
			if (!thisform[reqInsurance[i]].value) {
			
				errormsg();
			
				document.getElementsByName( thisform[reqInsurance[i]].name)[0].focus();
			
				return false;		
			}
		}			
	}
		
}

function checkTherapy(thisform)
{
	
	
	var therapyfound = false;
	
	if (thisform['tot_speech'].checked) therapyfound = true;	
	
	if (thisform['tot_occupational'].checked) therapyfound = true;	
	
	if (thisform['tot_physical'].checked) therapyfound = true;	
	
	if (thisform['tot_eiitds'].checked) therapyfound = true;	
	
	if (therapyfound)
	{
		return true;
	}else{
		
		alert('Please provide at least one type of Therapy Needed.');
		
		document.getElementsByName('tot_speech')[0].focus();
			
		return false;
		
	}
	
	
}
