var errors, hddnClientId, edtClientName, edtClientSurname, txtAddressId, edtClientCell, edtClientPhone, edtClientEmail, building, 
	txtAddressNote, txtClientNotes, txtClientBookingNote;;
var module = baseRootURL + "client/";
var buildingItem = new addSelectionList('buildingItem', 'building', 'buildingElement');

function registerClient(action)
{
	//ignore this function if the enter key is pressed in the textarea field for typing:
	var ae = document.activeElement;
	var ta = document.getElementById('txtAddressNote');
	if (ae == ta) return;

	var ta = document.getElementById('txtAddressId');
	if (ae == ta) return;
	
	var ta = document.getElementById('txtClientNotes');
	if (ae == ta) return;	
	
	errors = false;
	validate_hddnClientId();
	validate_edtClientName();
	validate_edtClientSurname();
	validate_txtAddressId();
	validate_edtClientCell();
	validate_edtClientPhone();
	validate_edtClientEmail();
	validate_txtAddressNote();
	validate_txtClientBookingNote();
	
	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("hddnClientId") != null) data.hddnClientId = hddnClientId;
		data.edtClientName		= edtClientName;
		data.edtClientSurname	= edtClientSurname;
		data.txtAddressId		= txtAddressId;
		data.edtClientCell		= edtClientCell;
		data.edtClientPhone		= edtClientPhone;
		data.edtClientEmail		= edtClientEmail;
		data.longitude			= window.lng === undefined ? '' : window.lng;
		data.latitude			= window.lat === undefined ? '' : window.lat;	
		data.buildingList			= buildingItem.getItemsAsString();
		data.txtAddressNote		= txtAddressNote;
		if (document.getElementById("txtClientBookingNote")) 
			data.txtClientBookingNote = document.getElementById('txtClientBookingNote').value;
		if (document.getElementById("txtClientNotes")) 
			data.txtClientNotes = document.getElementById('txtClientNotes').value;
	
		callModule("client/client_val.php", data);
	}
}
function insertClient(){registerClient(0)}
function modifyClient(){registerClient(1)}

function validate_hddnClientId(){
	if (document.getElementById("hddnClientId") != null) hddnClientId=document.getElementById("hddnClientId").value;}
function validate_edtClientName(){edtClientName=validateRequiredText("edtClientName", "First name is required.");}
function validate_edtClientSurname(){edtClientSurname=validateText("edtClientSurname", "Surname is required.");}
function validate_txtAddressId(){txtAddressId=validateTextArea("txtAddressId", "Address is required.", true);}
function validate_edtClientCell(){edtClientCell=validatePhoneNumber("edtClientCell", "Cell no. is required.", true);}
function validate_edtClientPhone(){edtClientPhone=validatePhoneNumber("edtClientPhone", "Phone number.", false);}
function validate_edtClientEmail(){edtClientEmail=validateEmail("edtClientEmail", "Valid email address required.");}
function validate_txtAddressNote(){txtAddressNote=document.getElementById('txtAddressNote').value;}
function validate_txtClientBookingNote(){txtClientBookingNote=validateTextArea("txtClientBookingNote", "Field is required.", false);}

function checkBuildings(){buildingItem.validateItems('1', 'edtBuildingName', 'building');}

function viewClient(id)
{
	var data = {}
	data.dummy = "";
	data.hddnClientId = id;
	data.action = 2;
	callModule("client/client_val.php", data);
}

function editClient(id)
{
	var data = {}
	data.dummy = "";
	data.hddnClientId = id;
	data.action = 3;
	callModule("client/client_val.php", data);
}

function deleteClient(id, desc)
{
	if (confirm("Are you sure you want to delete "+desc+" and all their buildings and bookings?"))
	{
		var data = {}
		data.dummy = "";
		data.hddnClientId = id;
		data.action = 4;
		callModule("client/client_val.php", data);
	}
}

function viewBuildings(id)
{
	var data = {}
	data.dummy = "";
	data.hddnClientId = id;
	data.action = 5;
	callModule("client/client_val.php", data);
}

function viewTxns(id)
{
	var data = {}
	data.dummy = "";
	data.hddnClientId = id;
	data.action = 6;
	callModule("client/client_val.php", data);
}

function viewHistory(id)
{
	var data = {}
	data.dummy = "";
	data.hddnClientId = id;
	data.action = 8;
	callModule("client/client_val.php", data);
}

function validate_selAreaId(){selAreaId=validateRequiredSelection("selAreaId", "Please make a valid selection.");}
function validate_edtBuildingName(){edtBuildingName=validateRequiredText("edtBuildingName", "This field is required.");}
function validate_edtBuildingNotes(){edtBuildingNotes=document.getElementById("edtBuildingNotes").value;}
function validate_txtBuildingNeeds(){txtBuildingNeeds=document.getElementById("txtBuildingNeeds").value;}

function addBuilding()
{
	errors = false;
	validate_selAreaId();
	validate_edtBuildingName();
	validate_edtBuildingNotes();
	validate_txtBuildingNeeds();
	if (!errors)
	{
		building					= {};
		building.selAreaId 		= selAreaId;
		building.edtBuildingName 		= edtBuildingName;
		building.edtBuildingNotes 	= edtBuildingNotes;
		building.txtBuildingNeeds 	= txtBuildingNeeds;

		//building.setItmIdx(uniqueId);
		var brd = document.getElementById('edtAreaName').value;
		buildingItem.colWidth = 4;
		buildingItem.setTag(building); //keeps the building information.
		buildingItem.setDisplayText(edtBuildingName+" <i style='color:green'>&rarr;</i> "+brd);
		buildingItem.addBasicItem(edtBuildingName, edtBuildingName); //sets the service (value).
		
		//reset the values:
		document.getElementById('selAreaId').value = 0;
		document.getElementById('edtAreaName').value = '';
		document.getElementById('edtBuildingName').value = '';
		document.getElementById('edtBuildingNotes').value = '';
		document.getElementById('txtBuildingNeeds').value = '';
		setAreaName();
	}
}

function doAreaRefinement(obj)
{
	var data = {}
	data.dummy = "";
	data.field_id = 'selAreaId';
	data.input_id = obj.value;
	callModule("lib/getAreasByName.php", data);
}

function setAreaName()
{
	"use strict"; 
	var sel = document.getElementById('selAreaId');
	if (sel.options[sel.selectedIndex])
		document.getElementById('edtAreaName').value = sel.options[sel.selectedIndex].innerHTML;
}

function addBooking(id)
{
	var data = {}
	data.dummy = "";
	data.hddnClientId = id;
	data.action = 7;
	callModule("client/client_val.php", data);}
