window.geoData = {};

function showLocation(lat, lng, addr, callback)
{
	//showAddress("<?php echo($row['address_lng']); ?>", "<?php echo($row['address_lat']); ?>", "<?php echo($row['address']); ?>", 15);
	showAddress(lat, lng, addr, 15);
	if (callback !== undefined) callback(); //updateMarker(coords); 
}

function navigate(lat, lng)
{
	var link = "https://www.google.com/maps/dir/?api=1&origin="+window.geoData.lat+","+window.geoData.lng+"&travelmode=driving&destination="+lng+","+lat+"&dir_action=navigate";
	window.location = link;
	console.log(link);
}

function getCurrentLocation() 
{
	"use strict";
	if (navigator.geolocation) 
	{
		handlePermission(false);
	} 
	else 
	{
		alert("Geolocation is not supported by this browser.");
	}
}

function handlePermission(info) 
{
	navigator.permissions.query({name:'geolocation'}).then
  	(
		function(result)
		{
			if (result.state == 'granted')
			{
				if (info)
					revertToHome();
				else
				{
					navigator.geolocation.getCurrentPosition(getPosition); 
				}
			}
			else
			{
				if (result.state == 'prompt')
				{
					//report('A message will be shown asking if you want to grant permissions. Please choose Yes.');
					if (info)
						revertToHome();
					else
					{
						navigator.geolocation.getCurrentPosition(getPosition); 
					} 
				}
				else
				{
					if (result.state == 'denied')
					{
						if (info)
							report('Permissions have been turned off in your settings. Please allow GPS for this app.');
						else
							window.location = 'enableGPS';
					}
				}
			}
			result.onchange = function()
			{
				if (info) 
					report('Current GPS State: '+result.state); 
				else 
				{
					if (result.state == 'denied')
						window.location = 'enableGPS';
					else
						revertToHome();
				}
			}
		}
	);
}

function revertToHome()
{
	window.location = baseRootURL+'index.php?/mobi/+'+getCookie('PHPSESSID'); //we try again...
}

function getPosition(position) 
{
	window.geoData.lat = position.coords.latitude;
	window.geoData.lng = position.coords.longitude;
}

function report(state) 
{
	document.getElementById('hint_gps_result').innerHTML = state;
}

function cancelScheduledItem(bsid, ServiceItemId, buildingId)
{
	if (confirm("Are you sure you want to cancel this service item?"))
	{
		var data			= {}
		data.dummy			= "";
		data.hddnBsId		= bsid;
		data.action			= 7;
		data.hddnServiceId	= ServiceItemId;
		data.selBuildingId 		= buildingId;
		callModule("booking/booking_val.php", data);
	}
}

function addServiceItem(bsid)
{
	window.location = baseRootURL+'index.php?/themes/sable/mobi/add_service.php+'+bsid+'+'+getCookie('PHPSESSID');
}

function updateServices(bsid, clientid)
{
	errors = false;
	validate_selBuildingId();
	CheckServices(); //at least 1 service must be defined for a booking.
	if (!errors)
	{
		var data = {}
		data.dummy = "";
		data.hddnBsId = bsid;
		data.action = 8;
		data.selClientId = clientid;
		data.serviceList = serviceItem.getItemsAsString();
		callModule("booking/booking_val.php", data);
	}
}

function addServiceTransaction(bsid)
{
	errors = false;
	var selPMId = validateRequiredSelection('selPMId', 'Payment method must be selected.');

	var edtCashAmount = -989898; //flag amount for the backend.
	if ($('#trCashAmount').is(":visible")) 
	{
		edtCashAmount = validateRequiredNumber('edtCashAmount', 'Cash amount received required.');
	}
	
	var cbConfirm = document.getElementById('cbConfirm').checked;
	if (cbConfirm)
	{
		if (!errors)
		{
			var data			= {}
			data.dummy			= "";
			data.hddnBsId		= bsid;
			data.selPMId		= selPMId;
			data.action			= 9;
			data.edtCashAmount	= edtCashAmount;
			callModule("booking/booking_val.php", data);
		}
	} 
	else 
	{
		errors = true;
		handleError('cbConfirm', "Confirmation required.");
	}
}

function addServiceComment(bsid)
{
	errors = false;
	var txtComment = validateRequiredText('txtComment', 'Please type in a comment.');
	if (!errors)
	{
		var data		= {}
		data.dummy		= "";
		data.hddnBsId	= bsid;
		data.txtComment	= txtComment;
		data.action		= 10;
		callModule("booking/booking_val.php", data);
	}
}

function showHideAmount()
{
	var cash = document.getElementById('selPMId').value == 1;
	if (cash)
		$('#trCashAmount').show();
	else
		$('#trCashAmount').hide();
}