var errors, hddnAttendanceId, selUserCode, edtAttendanceIn, edtAttendanceOut, edtAttendanceInLat, edtAttendanceInLng, edtAttendanceOutLat, edtAttendanceOutLng, edtAttendanceInIp, edtAttendanceOutIp;
var module = baseRootURL + "attendance/";

function registerAttendance(action)
{
	errors = false;
	validate_hddnAttendanceId();
	validate_selUserCode();
	validate_edtAttendanceIn();
	validate_edtAttendanceOut();
	validate_edtAttendanceInLat();
	validate_edtAttendanceInLng();
	validate_edtAttendanceOutLat();
	validate_edtAttendanceOutLng();
	validate_edtAttendanceInIp();
	validate_edtAttendanceOutIp();
	if (!errors)
	{
		var data = {};
		data.dummy = ""; //we seem to get errors from the first value, so always add a dummy value first.
		data.action = action;
		if (document.getElementById("hddnAttendanceId") != null) data.hddnAttendanceId = hddnAttendanceId;
		data.selUserCode = selUserCode;
		data.edtAttendanceIn = edtAttendanceIn;
		data.edtAttendanceOut = edtAttendanceOut;
		data.edtAttendanceInLat = edtAttendanceInLat;
		data.edtAttendanceInLng = edtAttendanceInLng;
		data.edtAttendanceOutLat = edtAttendanceOutLat;
		data.edtAttendanceOutLng = edtAttendanceOutLng;
		data.edtAttendanceInIp = edtAttendanceInIp;
		data.edtAttendanceOutIp = edtAttendanceOutIp;
		callModule("attendance/attendance_val.php", data);
	}
}
function insertAttendance(){registerAttendance(0)}
function modifyAttendance(){registerAttendance(1)}

function validate_hddnAttendanceId(){
	if (document.getElementById("hddnAttendanceId") != null) hddnAttendanceId=document.getElementById("hddnAttendanceId").value;}
function validate_selUserCode(){selUserCode=validateRequiredSelection("selUserCode", "Please make a valid selection.");}
function validate_edtAttendanceIn(){edtAttendanceIn=validateRequiredText("edtAttendanceIn", "This field is required.");}
function validate_edtAttendanceOut(){edtAttendanceOut=validateRequiredText("edtAttendanceOut", "This field is required.");}
function validate_edtAttendanceInLat(){edtAttendanceInLat=validateRequiredText("edtAttendanceInLat", "This field is required.");}
function validate_edtAttendanceInLng(){edtAttendanceInLng=validateRequiredText("edtAttendanceInLng", "This field is required.");}
function validate_edtAttendanceOutLat(){edtAttendanceOutLat=validateRequiredText("edtAttendanceOutLat", "This field is required.");}
function validate_edtAttendanceOutLng(){edtAttendanceOutLng=validateRequiredText("edtAttendanceOutLng", "This field is required.");}
function validate_edtAttendanceInIp(){edtAttendanceInIp=validateRequiredText("edtAttendanceInIp", "This field is required.");}
function validate_edtAttendanceOutIp(){edtAttendanceOutIp=validateRequiredText("edtAttendanceOutIp", "This field is required.");}

function viewAttendance(id)
{
	var data = {}
	data.dummy = "";
	data.hddnAttendanceId = id;
	data.action = 2;
	callModule("attendance/attendance_val.php", data);
}

function editAttendance(id)
{
	var data = {}
	data.dummy = "";
	data.hddnAttendanceId = id;
	data.action = 3;
	callModule("attendance/attendance_val.php", data);
}

function deleteAttendance(id, desc)
{
	if (confirm("Are you sure you want to delete "+desc+" and all associated records?"))
	{
		var data = {}
		data.dummy = "";
		data.hddnAttendanceId = id;
		data.action = 4;
		callModule("attendance/attendance_val.php", data);
	}
}

function checkIn()
{
	var data = {}
	data.dummy = "";
	data.edtAttendanceInLat = document.getElementById('hddnLat').value;
	data.edtAttendanceInLng = document.getElementById('hddnLng').value;
	data.action = 5;
	callModule("attendance/attendance_val.php", data);
}

function checkOut()
{
	var data = {}
	data.dummy = "";
	data.edtAttendanceOutLat = document.getElementById('hddnLat').value;
	data.edtAttendanceOutLng = document.getElementById('hddnLng').value;
	data.action = 6;
	callModule("attendance/attendance_val.php", data);
}

function presentAddress(lat, lng, addressField)
{
	getAddressFromCoords(lat, lng, addressField);
	showAddress(lat, lng, '', 20);
	scrollIntoViewIfNeeded(document.getElementById("mapCanvas"));
}

function getCurrentPosition(fieldLat, fieldLng) 
{
	if ("geolocation" in navigator)
	{
		navigator.permissions.query({ name: 'geolocation' }).then((result) => 
		{
			var go = false;
			if (result.state === 'granted')
			{
				go = true;
			}
			else 
				if (result.state === 'prompt')
				{
					go = true;
				}
				else
					if (result.state === 'denied')
					{
						alert('Please enable permissions for the use of GPS on your device.');
					}

			if (go)
			{
				navigator.geolocation.getCurrentPosition
				(
					function(position)
					{						
						document.getElementById(fieldLat).value = position.coords.latitude;
						document.getElementById(fieldLng).value = position.coords.longitude;
					}
				);
			}
		});
	}
	else
	{
		alert('Your browser, or device, does not support GPS. This function cannot be used.');
	}
}
