function validatePilotForm(){
	return validatePilotFormFields('pilot');
}

function validatePilotFormFields(pilotFormName){
	var alertMsg = '';
	alertMsg += buildValidationMsg(pilotFormName, 'pilotName');
	alertMsg += buildValidationMsg(pilotFormName, 'phone');
	if(alertMsg.length > 0){
		alert(alertMsg);
		return false;
	}
	return true;
}


/*
 *	Handles checking/unchecking checkboxes and hiding/showing related hours fields.
 */
function handleCertificates(myForm, suffix){
	//get reference to fields
	var certField 	= document.getElementsByName(myForm + ':certificates' + suffix);
	var certField2 	= document.getElementsByName(myForm + ':certificates2' + suffix);
	//define labels
	var STUDENT='Student';
	var SPORT_PILOT='Sport Pilot';
	var PRIVATE='Private';
	var COMMERCIAL='Commercial';
	var ATP='ATP';
	var COMPLEX_AC='Complex Aircraft Endorsement';
	var TAILWHEEL_AC='Tailwheel Aircraft Endorsement';
	var INSTRUMENT='Instrument Rating';
	var MULTI_ENGINE='Multi-Engine';
	var SEAPLANE='Seaplane';
	var ROTORCRAFT='Rotorcraft';
	var TYPE_RATING='Type Rating (For This Model)';	
	var CFI='CFI';
	var CFII='CFII';
	var MEI='MEI';
	//initialize state
	var studentChecked = false;
	var sportPilotChecked = false;
	var privateChecked = false;
	var commercialChecked=false;
	var atpChecked=false;
	var complexACChecked = false;
	var tailwheelACChecked=false;
	var instrumentChecked=false;
	var multiEngineChecked=false;
	var seaplaneChecked=false;
	var rotorcraftChecked=false;
	var typeRatingChecked=false;	
	var cfiChecked=false;
	var cfiiChecked=false;
	var meiChecked=false;
	//assign state	
	for(var i=0; i < certField.length; i++){
		var cbox = certField[i];
		if(cbox.value==STUDENT && cbox.checked==true){
			studentChecked = true;
		}else if(cbox.value==SPORT_PILOT && cbox.checked==true){
			sportPilotChecked = true;
		}else if(cbox.value==PRIVATE && cbox.checked==true){
			privateChecked = true;
		}else if(cbox.value==COMMERCIAL && cbox.checked==true){
			commercialChecked = true;
		}else if(cbox.value==ATP && cbox.checked==true){
			atpChecked = true;
		}else if(cbox.value==COMPLEX_AC && cbox.checked==true){
			complexACChecked = true;
		}else if(cbox.value==TAILWHEEL_AC && cbox.checked==true){
			tailwheelACChecked = true;
		}else if(cbox.value==INSTRUMENT && cbox.checked==true){
			instrumentChecked = true;
		}else if(cbox.value==MULTI_ENGINE && cbox.checked==true){
			multiEngineChecked = true;
		}else if(cbox.value==SEAPLANE && cbox.checked==true){
			seaplaneChecked = true;
		}else if(cbox.value==ROTORCRAFT && cbox.checked==true){
			rotorcraftChecked = true;
		}else if(cbox.value==TYPE_RATING && cbox.checked==true){
			typeRatingChecked = true;
		}
	}
	for(var i=0; i < certField2.length; i++){
		var cbox = certField2[i];
		if(cbox.value==CFI && cbox.checked==true){
			cfiChecked = true;
		}else if(cbox.value==CFII && cbox.checked==true){
			cfiiChecked = true;
		}else if(cbox.value==MEI && cbox.checked==true){
			meiChecked = true;
		}
	}
	
	var debugString = 'studentChecked = ' + studentChecked + '\n' +
		'sportPilotChecked = ' + sportPilotChecked + '\n' +
		'privateChecked = ' + privateChecked + '\n' +
		'commercialChecked=' + commercialChecked + '\n' +
		'atpChecked=' + atpChecked + '\n' +
		'complexACChecked = ' + complexACChecked + '\n' +
		'tailwheelACChecked=' + tailwheelACChecked + '\n' +
		'instrumentChecked=' + instrumentChecked + '\n' +
		'multiEngineChecked=' + multiEngineChecked + '\n' +
		'seaplaneChecked=' + seaplaneChecked + '\n' +
		'rotorcraftChecked=' + rotorcraftChecked + '\n' +
		'typeRatingChecked=' + 	typeRatingChecked + '\n' +
		'cfiChecked=' + cfiChecked + '\n' +
		'cfiiChecked=' + cfiiChecked + '\n' +
		'meiChecked=' + meiChecked;
	
	//actual business logic
	//simple hours fields
	toggleRow(sportPilotChecked, myForm, 'sportPilotHours', suffix);
	toggleRow(complexACChecked, myForm, 'retractableGearHours', suffix);
	toggleRow(multiEngineChecked, myForm, 'multiEngineHours', suffix);
	toggleRow(tailwheelACChecked, myForm, 'tailwheelHours', suffix);
	toggleRow(seaplaneChecked, myForm, 'seaPlaneHours', suffix);
	toggleRow(rotorcraftChecked, myForm, 'rotorcraftHours', suffix);
	//aggregate hours and checkbox fields.
	if(privateChecked){
		showCheckbox(certField, COMPLEX_AC);
		showCheckbox(certField, INSTRUMENT);
		showCheckbox(certField, MULTI_ENGINE);
		showCheckbox(certField, ROTORCRAFT);
	}else{
		hideCheckbox(certField, COMPLEX_AC);
		hideCheckbox(certField, INSTRUMENT);
		hideCheckbox(certField, MULTI_ENGINE);
		hideCheckbox(certField, ROTORCRAFT);
	}
	if(commercialChecked){
		checkMe(certField, STUDENT);	
		checkMe(certField, PRIVATE);
		showCheckbox(certField, COMPLEX_AC);
		showCheckbox(certField, INSTRUMENT);
		showCheckbox(certField, MULTI_ENGINE);
		showCheckbox(certField, ROTORCRAFT);

		checkMe(certField, COMPLEX_AC);
	}
	if(atpChecked){
		checkMe(certField, STUDENT);	
		checkMe(certField, PRIVATE);
		checkMe(certField, COMMERCIAL);
		showCheckbox(certField, COMPLEX_AC);
		showCheckbox(certField, INSTRUMENT);
		showCheckbox(certField, MULTI_ENGINE);
		showCheckbox(certField, ROTORCRAFT);

		checkMe(certField, INSTRUMENT);	
		checkMe(certField, MULTI_ENGINE);
		checkMe(certField, COMPLEX_AC);
	}
	//handle checkboxes
	if(cfiChecked && instrumentChecked){
		showInlineCheckbox(certField2, CFII);
	}else{
		hideInlineCheckbox(certField2, CFII);
		//alert('hiding checkbox ' + debugString);
	}
    //show/hide recurring training
    var rtgRef = document.getElementById(myForm + ':recurringTraingingGroup' + suffix);
    if(rtgRef != null){	//is this pilot or quote form?
        rtgRef.style.display = typeRatingChecked ? YES_VAL : NO_VAL;
    }

    if(cfiChecked && multiEngineChecked){
		showInlineCheckbox(certField2, MEI);
	}else{
		hideInlineCheckbox(certField2, MEI);
	}
	//handle complex hours fields
	toggleRow((complexACChecked && instrumentChecked && multiEngineChecked), myForm, 'jetHours', suffix);
	toggleRow((complexACChecked && instrumentChecked), myForm, 'turboPropHours', suffix);

	if(document.getElementById(myForm + ':loggedHoursGroup' + suffix) != null){	//is this pilot or quote form?
		if((instrumentChecked && multiEngineChecked) || cfiChecked){
			document.getElementById(myForm + ':loggedHoursGroup' + suffix).style.display=YES_VAL;
		}else{
			document.getElementById(myForm + ':loggedHoursGroup' + suffix).style.display=NO_VAL;
		}
	
	}
}


function toggleRow(condition, myForm, myField, suffix){
	if(condition){
		showRow(myForm, myField + suffix);
	}else{
		hideRow(myForm, myField + suffix);
	}
}



//yes/no + explanation
function buildTrainingMessage(formId, fieldId, suffix){
	window.status = 'fieldId=' + fieldId;
	var myField =document.getElementById(formId + ':' + fieldId);
	var msg = '';	
	if(!validateRadio(formId, fieldId)){
		return 'Please answer \"' + document.getElementById(formId + ':_' + fieldId).innerHTML.replace(reqText, '') + '\"\n';
	}else{
		var myVal = getRadioValue(formId, fieldId + suffix);
		if(myVal == 'Yes'){
			msg += buildValidationMsg(formId , 'trainingSchool' + suffix);
			msg += buildValidationMsg(formId , 'trainingDate' + suffix);
		}
	}
	return msg;
}
/**
 *	Hides checkbox only.
 *	hide up to <TR>.  e.g.: <tr style=""><td><label><input value="Instrument Rating"  type="checkbox">Instrument Rating</label></td></tr>
 *	<tr>
 *		<td class="InputLabel"><span id="pilot:_certificates2">ddd</span></td>
 *		<td class="InputField">
 *		<table id="pilot:certificates2">
 *		<tr>
 *			<td><label><input type="checkbox" name="pilot:certificates2" value="CFI" onclick="handleCertificates('pilot', this)" />&#160;CFI</label></td>		
 *			<td><label><input type="checkbox" name="pilot:certificates2" value="CFII" onclick="handleCertificates('pilot', this)" />&#160;CFII</label></td>		
 *			<td><label><input type="checkbox" name="pilot:certificates2" value="MEI" onclick="handleCertificates('pilot', this)" />&#160;MEI</label></td>
 *		</tr>
 *		</table>
 *		</td>
 * 	</tr>
 */
function handleInlineCheckboxVisibility(myField, myValue, myStyle){
	for(var i=0; i < myField.length; i++){	//loop through all checkboxes.  Cannot find by ID, must find by name.
		var cbox = myField[i];
		if(cbox.value==myValue){	//is found checkbox the one we're looking for?
			cbox.parentNode.style.display=myStyle;
			return;
		}
	}
}
var YES_VAL = '';
var NO_VAL = 'none';
/**
 *	for CFI, CFII, MEI
 */
function hideInlineCheckbox(myField1, myValue){
	//handleInlineCheckboxVisibility(myField0, myValue, NO_VAL);	//handle group 1
	handleInlineCheckboxVisibility(myField1, myValue, NO_VAL);	//handle group 2
}

/**
 *	for CFI, CFII, MEI
 */
function showInlineCheckbox(myField1, myValue){
	//handleInlineCheckboxVisibility(myField0, myValue, YES_VAL);
	handleInlineCheckboxVisibility(myField1, myValue, YES_VAL);
}

function init(){
	var formName = 'pilot'; 
	initVisibility(formName,'questionLoggedHours');
	initVisibility(formName,'questionViolations');
	handleCertificates(formName, '');
	handleRecurringTraining(formName, getRadioValue(formName, 'questionRecurringTraining'), '');
}



/**
 *
 */
function handleRecurringTraining(myFormName, myFieldValue, suffix){
	if(myFieldValue == 'Yes'){
		showCluster2(document.getElementsByName(myFormName + ':trainingDate' + suffix)[0]);	
		showCluster2(document.getElementsByName(myFormName + ':trainingSchool' + suffix)[0]);	
	}else{
		hideCluster2(document.getElementsByName(myFormName + ':trainingDate' + suffix)[0]);	
		hideCluster2(document.getElementsByName(myFormName + ':trainingSchool' + suffix)[0]);	
	}
}