// JavaScript Document

var booking ={
	init  : function(fromTimeH,fromTimeM,toTimeH,toTimeM)
	// booking form onload when edited
	{
		document.getElementById('fromHours').value = fromTimeH;
		document.getElementById('fromMinutes').value = fromTimeM;
		document.getElementById('toHours').value = toTimeH;
		document.getElementById('toMinutes').value = toTimeM;
		
	},
	
	validate : function()
	{
		var fromHours = document.getElementById('fromHours').value;
		var fromMinutes = document.getElementById('fromMinutes').value;
		var toHours = document.getElementById('toHours').value;
		var toMinutes = document.getElementById('toMinutes').value;
				
		
		var fromDate$ = document.getElementById('fromDate').value;
		var toDate$ = document.getElementById('toDate').value;
		
		var fromDate = getFieldDate(fromDate$);
		var toDate = getFieldDate(toDate$);
	 
	 	fromDate.setHours(fromHours,fromMinutes);
		toDate.setHours(toHours,toMinutes);
		
		var nowDate = new Date();
		
		if (fromDate >= toDate)
		{
			alert('Start date and time must be BEFORE end date and time. Please check your dates and times carefully.');	
		}
		else if (fromDate <= nowDate || toDate <=nowDate)
		{
			
			alert('You cannot make a booking in the past. Please check your dates and times carefully.');	
			
		}
		else
		{
			document.forms[0].submit();	
		}
	
	},
	
	selectAircraft : function()
	// called from bookings - pick aircraft:onclick
	{
		if (document.forms[0].aircraftList[0].selected != true)
		{
			document.forms[0].showOnlyAircraft.checked = true;
		}
		else
		{
			document.forms[0].showOnlyAircraft.checked = false;
		}
		
		
	},
	
	selectFrom : function()
	// called from bookings -- (any) from field:onchange
	{
		document.forms[0].anyDate[1].checked = true;
		
	},
	
	parseForView : function()
	// called from bookings - [view booking]:onclick
	// 
	{
		var query = "/bookings.php?view=filtered";
		var dateFrom = null;
		var dateTo = null;
		// 1. Date Any | Range (from-to)
		if (document.forms[0].anyDate[1].checked) // 'range' selected
		{
			dateFrom = document.getElementById('fromDate').value.split('/').join('-');
			dateTo = document.getElementById('toDate').value.split('/').join('-');
			
			query += '&dateFrom=' +  dateFrom + '&dateTo=' + dateTo;

		}
		
		// onlymy:
		if (document.forms[0].showMyBookings.checked)
		{
			query += '&onlymy=true';
		}
		
		// ac id
		if (document.forms[0].showOnlyAircraft.checked && document.forms[0].aircraftList[0].selected != true)
		{
			query += '&aircraft=' +  document.forms[0].aircraftList.value;
		}
		
		//console.log(query);
		
		
		window.location.replace(query);
	},
	
	setUpdate : function()
	{
		document.getElementById('viewUpdateBookingsButton').value = "Refresh";
		
		
	},
	confirmCancel : function()
	// called from showBookingDetails [cancel]:onclick
	{
		if (confirm("Your booking will be deleted. Click Okay to delete or cancel to return."))
		{
			// set the (empty) deleteId field to this one's id and post.
			document.forms[0].deleteId.value = document.forms[0].editId.value;
			document.forms[0].submit();
				
		}
			
	}
	
}

var notice = 
{
	
	confirmCancel : function()
	// called from showBookingDetails [cancel]:onclick
	{
		if (confirm("Your notice will be deleted. Click Okay to delete or cancel to return."))
		{
			// set the (empty) deleteId field to this one's id and post.
			document.forms[0].deleteId.value = document.forms[0].editId.value;
			document.forms[0].submit();
				
		}
			
	}	
}

var message = 
{
	selectAll : function()
	{
		var recipients = document.forms[0].recipients;
		for (var i=0;i<recipients.length;i++)
		{
			recipients[i].selected=true;
		}
		
	},
	
	unselectAll : function()
	{
		document.forms[0].all[1].checked = true;
	},
	
	validate : function()
	{
		var msg = "";
		
		var recipients = document.forms[0].recipients;
		var recipientsSel = 0;
		for (var i=0;i<recipients.length;i++)
		{
			if (recipients[i].selected)
			{
				recipientsSel++;	
			}
		}
		if (recipientsSel<1)
		{
			msg += "1 or more recipients must be specified.\n";
		}
		
		if (document.forms[0].messageSubject.value.length<1)
		{
			msg += "Each message needs a subject.\n"	
		}
		
		//if (document.forms[0].messageBody.value.length<3)
		//{
		//	msg += "No message body text entered.\n"	
		//}
		
		
		if (msg == "")
		{
			// good to go
			document.forms[0].submit();
			
		}
		else
		{
			alert ("The following items are missing or invalid:\n\n" + msg);	
		}
		
	}
}


var dutypilot ={
	init  : function(fromTimeH,fromTimeM,toTimeH,toTimeM)
	// dutypilot form onload when edited
	{
		document.getElementById('fromHours').value = fromTimeH;
		document.getElementById('fromMinutes').value = fromTimeM;
		document.getElementById('toHours').value = toTimeH;
		document.getElementById('toMinutes').value = toTimeM;
		
	},
	
	validate : function()
	{
		var fromHours = document.getElementById('fromHours').value;
		var fromMinutes = document.getElementById('fromMinutes').value;
		var toHours = document.getElementById('toHours').value;
		var toMinutes = document.getElementById('toMinutes').value;
				
		
		var fromDate$ = document.getElementById('fromDate').value;
		var toDate$ = document.getElementById('toDate').value;
		
		var fromDate = getFieldDate(fromDate$);
		var toDate = getFieldDate(toDate$);
	 
	 	fromDate.setHours(fromHours,fromMinutes);
		toDate.setHours(toHours,toMinutes);
		
		var nowDate = new Date();
		
		if (fromDate >= toDate)
		{
			alert('Start date and time must be BEFORE end date and time. Please check your dates and times carefully.');	
		}
		else if (fromDate <= nowDate || toDate <=nowDate)
		{
			
			alert('You cannot make an entry in the past. Please check your dates and times carefully.');	
			
		}
		else
		{
			document.forms[0].submit();	
		}
	
	},
	
	
	selectFrom : function()
	// called from dutypilots -- (any) from field:onchange
	{
		document.forms[0].anyDate[1].checked = true;
		
	},
	
	parseForView : function()
	// called from dutybookings - [view dutypilot]:onclick
	// 
	{
		var query = "/dutybookings.php?view=filtered";
		var dateFrom = null;
		var dateTo = null;
		// 1. Date Any | Range (from-to)
		if (document.forms[0].anyDate[1].checked) // 'range' selected
		{
			dateFrom = document.getElementById('fromDate').value.split('/').join('-');
			dateTo = document.getElementById('toDate').value.split('/').join('-');
			
			query += '&dateFrom=' +  dateFrom + '&dateTo=' + dateTo;

		}
		
		// onlymy:
		if (document.forms[0].showMyDutyBookings.checked)
		{
			query += '&onlymy=true';
		}
		
		
		//console.log(query);
		
		
		window.location.replace(query);
	},
	
	setUpdate : function()
	{
		document.getElementById('viewUpdateDutyBookingsButton').value = "Refresh";
		
		
	},
	confirmCancel : function()
	// called from showdutypilotDetails [cancel]:onclick
	{
		if (confirm("Your entry will be deleted. Click Okay to delete or cancel to return."))
		{
			// set the (empty) deleteId field to this one's id and post.
			document.forms[0].deleteId.value = document.forms[0].editId.value;
			document.forms[0].submit();		
		}

	}
	
}


var adminmember = 
{
	
	validate : function()
	{
		var msg = "";
		
		
		if (document.forms[0].Email.value.length<7)
		{
			msg += "You must enter a valid email address as it is your user id for this website.\n"	
		}

		if (document.forms[0].FName.length<1 || document.forms[0].SName.value.length<1 ||
			document.forms[0].Email.value.length<7)
		{
			msg += "You must enter firstname, surname and an email address.\n"	
		}

		if (document.forms[0].NewPassword.value != document.forms[0].ConfirmPassword.value)
		{
			document.forms[0].NewPassword.value = "";
			document.forms[0].ConfirmPassword.value = "";
			msg += "New Password and Confirm Password fields must be the same.\n"	;
		}
		
		if (msg == "")
		{
			// good to go
			document.forms[0].submit();
			
		}
		else
		{
			alert ("The following items are missing or invalid:\n\n" + msg);	
		}
		
	},
	
	confirmCancel : function()
	// called from adminmember [cancel]:onclick
	{
		if (confirm("Your entry will be deleted. Click Okay to delete or cancel to return."))
		{
			// set the (empty) deleteId field to this one's id and post.
			document.forms[0].deleteId.value = document.forms[0].editId.value;
			document.forms[0].submit();		
		}

	}	
	
}



var member = 
{
	
	validate : function()
	{
		var msg = "";
		
		
		if (document.forms[0].Email.value.length<7)
		{
			msg += "You must enter a valid email address as it is your user id for this website.\n"	
		}

		if (document.forms[0].HomePhone.value.length<1 && document.forms[0].WorkPhone.value.length<1 &&
			document.forms[0].Mobile.value.length<1 && document.forms[0].Address1.value.length<1 )
		{
			msg += "You must enter phone number or address for contact purposes.\n"	
		}

		if (document.forms[0].NewPassword.value != document.forms[0].ConfirmPassword.value)
		{
			document.forms[0].NewPassword.value = "";
			document.forms[0].ConfirmPassword.value = "";
			msg += "New Password and Confirm Password fields must be the same.\n"	;
		}
		
		if (msg == "")
		{
			// good to go
			document.forms[0].submit();
			
		}
		else
		{
			alert ("The following items are missing or invalid:\n\n" + msg);	
		}
		
	}
}


