// various dynamic behaviour/validation logic used in the civil-contact-form

// CUSTOM BUSINESS LOGIC
function privateIndividualCheck() {
	//alert(document.getElementById('Private_Individual__c').value);
	if (document.getElementById('Private_Individual__c').value == "no") {
		$("#company_field").show();
		$('#company').attr('required', 'required')
		//$("#training_region_field").hide();
		}
	else {
		$("#company_field").hide();
		$('#company').attr('required', false)

}
//NEW
	if (document.getElementById('Private_Individual__c').value == "yes") {
		$("#Product_of_Interest__c option[value='Simulator Time Rental (Dry)']").hide();
		$("#Product_of_Interest__c option[value='Simulation Products']").hide();
		$("#Product_of_Interest__c option[value='Crew Sourcing (Pilot and Technical Personnel)']").hide();
		$("#Product_of_Interest__c option[value='Ferry Flight and Aircraft Leasing Support Services']").hide();
	}
	else {
		$("#Product_of_Interest__c option[value='Simulator Time Rental (Dry)']").show();
		$("#Product_of_Interest__c option[value='Simulation Products']").show();
		$("#Product_of_Interest__c option[value='Crew Sourcing (Pilot and Technical Personnel)']").show();
		$("#Product_of_Interest__c option[value='Ferry Flight and Aircraft Leasing Support Services']").show();
	}
}
function stateProvinceCheck() {
	if (document.getElementById('Country__c').value=="United States" || document.getElementById('Country__c').value == "Canada") {
			$("#state_province_field").show();
			$('select[name="Billing_State_Province__c"]').attr('required', 'required')
	}
	else {
		$("#state_province_field").hide();
		$('select[name="Billing_State_Province__c"]').attr('required', false)

}
}
function productServiceCheck() {
	// I'm not sure what the intended behaviour is with these conditions, but
	// I had to remove an option (index 3) from the product_of_interest__c select,
	// so I've shifted all indexes > 3 by -1, and commented ones == 3.
	// it seems there are many outdated/invalid jquery element selectors (id's)

	// Business segment conditions
	if ($('#Product_of_Interest__c')[0].selectedIndex == "2" ||
		/*$('#Product_of_Interest__c')[0].selectedIndex == "3" ||*/
		$('#Product_of_Interest__c')[0].selectedIndex == "3" ||
		$('#Product_of_Interest__c')[0].selectedIndex == "6" ||
		$('#Product_of_Interest__c')[0].selectedIndex == "7") {

		$('#Business_Segment__c').val("Commercial Aviation");
		$('#business_segment_field').hide();
	}
	else {
		$('#Business_Segment__c').val("");
		$('#business_segment_field').show();
	}

	// when the user selects the following for "interested in":
	// 1) type training for pilots and copilots (Professional Pilot Training (Wet, Initial type rating and Recurrent))
	// 2) Maintenace Training
	// hide both the following from business segment:
	// Business Aviation, Rotorcrafts
	if ($('#Product_of_Interest__c')[0].selectedIndex == "1" ||
		$('#Product_of_Interest__c')[0].selectedIndex == "5") {
		$("#Business_Segment__c option[value='Business Aviation']").hide();
		$("#Business_Segment__c option[value='Rotorcrafts']").hide();
	}
	else {
		$("#Business_Segment__c option[value='Business Aviation']").show();
		$("#Business_Segment__c option[value='Rotorcrafts']").show();
	}

	// Maintenance training slection
	if ($('#Product_of_Interest__c')[0].selectedIndex == "5") {
		$("#business_segment_field option[value='Commercial Aviation']").hide();
	}
	// Preferred training region conditions [THIS VALUE WAS REMOVED]
	// if ($('#Product_of_Interest__c').val()=="Cadet/Ab-initio Pilot Training") {
	// 	$("#training_region_field").show();
	// 	$("#training_region_field option[value='Africa']").hide();
	// 	$("#training_region_field option[value='China']").hide();
	// 	$("#training_region_field option[value='Latin America and Caribbean']").hide();
	// 	$("#training_region_field option[value='Middle East']").hide();
	// }
	// else {$("#training_region_field").hide();}
}

$('#Product_of_Interest__c').on('change', function(){
	switch ($(this).val()){
		case 'Simulation Products' :  $('#Business_Segment__c').val('None').closest('.fiftyPerc').hide(); break;
		case 'Cadet/Ab-initio Pilot Training' : $('#Business_Segment__c').val('None').closest('.fiftyPerc').hide(); break;
		case 'Simulator Time Rental (Dry)' : $('#Business_Segment__c').val('None').closest('.fiftyPerc').hide(); break;
		case 'Cabin Crew Training' : $('#Business_Segment__c').val('None').closest('.fiftyPerc').hide(); break;
		case 'Ferry Flight and Aircraft Leasing Support Services' : $('#Business_Segment__c').val('None').closest('.fiftyPerc').hide(); break;
		case 'Professional Pilot Training (Wet, Initial type rating and Recurrent)' : $('#Business_Segment__c').val('Commercial Aviation').closest('.fiftyPerc').show(); break;
		case 'Crew Sourcing (Pilot and Technical Personnel)' : $('#Business_Segment__c').val('None').closest('.fiftyPerc').show(); break;
		case 'Maintenance Training' : $('#Business_Segment__c').val('None').closest('.fiftyPerc').show();
	}
})

$('.civilForm').on('submit', function(){
	var validated = true;
	$(this).find('select:visible').each(function(){
		if ($(this).val() == ""){
			validated = false;
		}
	})
	if (!validated){
		alert('Please fill in all required fields')
		return false;
	}
})

// trigger change on load so that the behaviour reflects the selected value
$("#Product_of_Interest__c").trigger("change");