function clearDropDownDatepickers() {
	if ($('input[name*="mainMemberSelectedPicker"]').val() == "") {
		$('input[name*="mainMemberSelectedPicker"]').siblings(
				'select[name*="selectedDay"]').val("DD");
		$('input[name*="mainMemberSelectedPicker"]').siblings(
				'select[name*="selectedMonth"]').val("MMM");
		$('input[name*="mainMemberSelectedPicker"]').siblings(
				'select[name*="selectedYear"]').val("YYYY");
	}
}

function setDropDownDatepickers(updateDependantsOccupation) {
	$.datepicker.setDefaults($.datepicker.regional["en-GB"]);

	$('input[name*="mainMemberSelectedPicker"]').datepicker({
		dateFormat : "dd/M/yy",
		inline : true,
		maxDate : "0"});

	$('input[name*="dependantSelectedPicker"]').datepicker({
		dateFormat : "dd/M/yy",
		inline : true,
		maxDate : "0"
	});

	// Update datepicker from three select controls
	$('select[name*="selected"]').change(function() {
		if ($(this).parent('.errorTextbox').length == 0) {
			
			if ($(this).siblings('select[name*="selectedDay"]').find(":selected").text() == '') {
				
				$(this).siblings('input[name*="mainMemberSelectedPicker"]').val(
						$(this).find(":selected").text()+ '/'+ $(this).siblings('select[name*="selectedMonth"]').find(
								":selected").text()+ '/'+ $(this).siblings('select[name*="selectedYear"]').find(
								":selected").text());
				$(this).siblings('input[name*="dependantSelectedPicker"]').val(
						$(this).find(":selected").text()
								+ '/'+ $(this).siblings('select[name*="selectedMonth"]').find(":selected").text()
								+ '/'+ $(this).siblings('select[name*="selectedYear"]').find(":selected").text());
			} else if ($(this).siblings('select[name*="selectedMonth"]').find(":selected").text() == '') {
				$(this).siblings('input[name*="mainMemberSelectedPicker"]').val(
						$(this).siblings('select[name*="selectedDay"]').find(":selected").text()
								+ '/'+ $(this).find(":selected").text()
								+ '/'+ $(this).siblings('select[name*="selectedYear"]').find(":selected").text());
				$(this).siblings('input[name*="dependantSelectedPicker"]').val(
						$(this).siblings('select[name*="selectedDay"]').find(":selected").text()
								+ '/'+ $(this).find(":selected").text()
								+ '/'+ $(this).siblings('select[name*="selectedYear"]').find(":selected").text());
			} else if ($(this).siblings('select[name*="selectedYear"]').find(":selected").text() == '') {
				$(this).siblings('input[name*="mainMemberSelectedPicker"]').val(
						$(this).siblings('select[name*="selectedDay"]').find(":selected").text()
								+ '/'+ $(this).siblings('select[name*="selectedMonth"]').find(":selected").text()
								+ '/'+ $(this).find(":selected").text());
				$(this).siblings('input[name*="dependantSelectedPicker"]').val(
						$(this).siblings('select[name*="selectedDay"]').find(":selected").text()
								+ '/'+ $(this).siblings('select[name*="selectedMonth"]').find(":selected").text()
								+ '/'+ $(this).find(":selected").text());
			}	
			
			if (updateDependantsOccupation) {				

				var datePicker = $(this).siblings('input[name*="dependantSelectedPicker"]');
				var dependantDate = datePicker.datepicker('getDate');
				var age = getAge(dependantDate);				
				var dependantsDiv = datePicker.closest('.dependants');		
				
				
				if (age >= 18) {		
				   
				   //dependantsDiv.find('select[name*="dependantOccupation"]').val("");	
				   dependantsDiv.find('.dependantOccupation').show();				  
				
				} else if (age < 18){				
					
					
					dependantsDiv.find('.dependantOccupation').hide();
					dependantsDiv.find('select[name*="dependantOccupation"]').val("");	
					
					/*dependantsDiv.find('select[name*="dependantOccupation"] option').each(function(){						
						 if ($(this).val() ==  '11') {
			                  option = this;
			                  option.selected = true;
			                  return false;    
			              }
			        }); 	*/				
					
					
					
					//console.log(dependantsDiv.find('select[name*="dependantOccupation"]').val());
					
				}
					
			}	
			
		} else {
			if ($(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedDay"]').find(":selected").text() == '') {
				$(this).parent('.errorTextbox').siblings('input[name*="mainMemberSelectedPicker"]').val(
					$(this).find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedMonth"]').find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedYear"]').find(":selected").text());
				$(this).parent('.errorTextbox').siblings('input[name*="dependantSelectedPicker"]').val(
					$(this).find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedMonth"]').find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedYear"]').find(":selected").text());
			} else if ($(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedMonth"]').find(":selected").text() == '') {
				$(this).parent('.errorTextbox').siblings('input[name*="mainMemberSelectedPicker"]').val(
					$(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedDay"]').find(":selected").text()
						+ '/'+ $(this).find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedYear"]').find(":selected").text());
				$(this).siblings('.errorTextbox').children('input[name*="dependantSelectedPicker"]').val(
					$(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedDay"]').find(":selected").text()
						+ '/'+ $(this).find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedYear"]').find(":selected").text());
			} else if ($(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedYear"]').find(":selected").text() == '') {
				$(this).parent('.errorTextbox').siblings('input[name*="mainMemberSelectedPicker"]').val(
					$(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedDay"]').find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedMonth"]').find(":selected").text()
						+ '/'+ $(this).find(":selected").text());
				$(this).parent('.errorTextbox').siblings('input[name*="dependantSelectedPicker"]').val(
					$(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedDay"]').find(":selected").text()
						+ '/'+ $(this).parent('.errorTextbox').siblings('.errorTextbox').children('select[name*="selectedMonth"]').find(":selected").text()
						+ '/'+ $(this).find(":selected").text());
			}
			
			
			if (updateDependantsOccupation) {				

				var datePicker = $(this).parent('.errorTextbox').siblings('input[name*="dependantSelectedPicker"]');		
				
				var dependantDate = datePicker.datepicker('getDate');

				var age = getAge(dependantDate);				
				var dependantsDiv = datePicker.closest('.dependants');		
				
				
				if (age >= 18) {		
				   
				   //dependantsDiv.find('select[name*="dependantOccupation"]').val("");	
				   dependantsDiv.find('.dependantOccupation').show();				  
				
				} else if (age < 18){						
					
					dependantsDiv.find('.dependantOccupation').hide();	
					dependantsDiv.find('select[name*="dependantOccupation"]').val("");	
					
					/*dependantsDiv.find('select[name*="dependantOccupation"] option').each(function(){						
						 if ($(this).val() ==  '11') {
			                  option = this;
			                  option.selected = true;
			                  return false;    
			              }
			        }); 	*/				
					
					
					//console.log(dependantsDiv.find('select[name*="dependantOccupation"]').val());					
									
					
				}
					
			}	
			
			
			
			
		}
		
		

	});

}

//Update three select controls to match a datepicker selection
function updateSelected() {
	var datePicker = $('input[name*="mainMemberSelectedPicker"]');
	var date = datePicker.datepicker('getDate');
	var day = date ? date.getDate() : '';
	var month=datePicker.val().substr(3,3);
	var year = date ? date.getFullYear() : '';
	
	if ($('.jQuery_selection_mainmemberdob_wrapper.errorTextbox').length == 0) {
		datePicker.siblings('select[name*="selectedDay"]').find('option')
				.filter(function() {
					return $(this).text() == day;
				}).prop('selected', true);
		datePicker.siblings('select[name*="selectedMonth"]').find('option')
				.filter(function() {
					return $(this).text() == month;
				}).prop('selected', true);
		datePicker.siblings('select[name*="selectedYear"]').find('option')
				.filter(function() {
					return $(this).text() == year;
				}).prop('selected', true);
	}
		var dependantDatePicker = $('input[name*="dependantSelectedPicker"]');
		var dependantDate;
		var dependantDay;
		var dependantMonth;
		var dependantYear;

        dependantDatePicker.each(function(index) {

		dependantDate = $(this).datepicker('getDate');
		dependantDay = $(this).val().substr(0,2);
		dependantMonth= $(this).val().substr(3,3);
		dependantYear = $(this).val().substr(7,4);
		
		//console.log('dependantDate GQS1 ' + dependantDate);
		//console.log('dependantDay ' + dependantDay);
		//console.log('dependantMonth ' + dependantMonth);
		//console.log('dependantYear ' + dependantYear);
		
		if ($(this).siblings('.errorTextbox').length == 0) {
			
						
			if (dependantDate == null) {					
				
				$(this).siblings('select[name*="selectedDay"]').val('');
				$(this).siblings('select[name*="selectedMonth"]').val('');
				$(this).siblings('select[name*="selectedYear"]').val('');				
			
			} else {				
				
				$(this).siblings('select[name*="selectedDay"]').find('option')
						.filter(function() {
							return $(this).text() == dependantDay;
						}).prop('selected', true);
				$(this).siblings('select[name*="selectedMonth"]')
						.find('option').filter(function() {
							return $(this).text() == dependantMonth;
						}).prop('selected', true);
				$(this).siblings('select[name*="selectedYear"]').find('option')
						.filter(function() {
							return $(this).text() == dependantYear;
						}).prop('selected', true);
			}

		} else {
			
				$(this).siblings('.errorTextbox').children('select[name*="selectedDay"]').find('option')
						.filter(function() {
							return $(this).text() == dependantDay;
						}).prop('selected', true);
				$(this).siblings('.errorTextbox').children('select[name*="selectedMonth"]')
						.find('option').filter(function() {
							return $(this).text() == dependantMonth;
						}).prop('selected', true);
				$(this).siblings('.errorTextbox').children('select[name*="selectedYear"]').find('option')
						.filter(function() {
							return $(this).text() == dependantYear;
						}).prop('selected', true);
		

			
			
		}
	});


}


function getAge(dependantDate) {
	  var ageMS = Date.parse(Date()) - Date.parse(dependantDate);
	  var age = new Date();
	  age.setTime(ageMS);
	  var ageYear = age.getFullYear() - 1970;
	  return ageYear;	 
};



