var errors, hddnFinanceId, edtTxnDate, selClientId, selTxnTypeId, selPmId, edtTxnAmount, cbTxnPaid, txtTxnNotes, selBsId;
var module = baseRootURL + "finance/";

function registerFinance(action)
{
	errors = false;
	validate_hddnFinanceId();
	validate_edtTxnDate();
	validate_selClientId();
	validate_selTxnTypeId();
	validate_selPmId();
	validate_edtTxnAmount();
	validate_cbTxnPaid();
	validate_txtTxnNotes();
	validate_selBsId();
	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("hddnFinanceId") != null) data.hddnFinanceId = hddnFinanceId;
		data.edtTxnDate = edtTxnDate;
		data.selTxnTypeId = selTxnTypeId;
		data.selPmId = selPmId;
		data.edtTxnAmount = edtTxnAmount;
		data.cbTxnPaid = cbTxnPaid;
		data.txtTxnNotes = txtTxnNotes;
		data.selBsId = selBsId;
		data.selClientId = selClientId;
		callModule("finance/finance_val.php", data);
	}
}
function insertFinance(){registerFinance(0)}
function modifyFinance(){registerFinance(1)}

function validate_hddnFinanceId(){
	if (document.getElementById("hddnFinanceId") != null) hddnFinanceId=document.getElementById("hddnFinanceId").value;}
function validate_edtTxnDate(){edtTxnDate=validateRequiredText("edtTxnDate", "This field is required.");}
function validate_selClientId(){selClientId=validateRequiredSelection("selClientId", "Please make a valid selection.");}
function validate_selTxnTypeId(){selTxnTypeId=validateRequiredSelection("selTxnTypeId", "Please make a valid selection.");}
function validate_selPmId(){selPmId=validateRequiredSelection("selPmId", "Please make a valid selection.");}
function validate_edtTxnAmount(){edtTxnAmount=validateRequiredNumber("edtTxnAmount", "Please enter a valid numeric value.");}
function validate_cbTxnPaid(){cbTxnPaid = document.getElementById("cbTxnPaid").checked;}
function validate_txtTxnNotes(){txtTxnNotes=validateTextArea("txtTxnNotes", "Field is required.", false);}
function validate_selBsId(){selBsId=validateRequiredSelection("selBsId", "Please make a valid selection.");}

function viewFinance(id)
{
	var data = {}
	data.dummy = "";
	data.hddnFinanceId = id;
	data.action = 2;
	callModule("finance/finance_val.php", data);
}

function editFinance(id)
{
	var data = {}
	data.dummy = "";
	data.hddnFinanceId = id;
	data.action = 3;
	callModule("finance/finance_val.php", data);
}

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

function copyFinance(id, desc)
{
	if (confirm("Are you sure you want to duplicate "+desc+"?"))
	{
	var data = {}
	data.dummy = "";
	data.hddnFinanceId = id;
	data.action = 5;
	callModule("finance/finance_val.php", data);
	}
}

function setClientName()
{
	"use strict"; 
	var sel = document.getElementById('selClientId');
	var edt = document.getElementById('edtClientName');
	edt.value = sel.options[sel.selectedIndex].innerHTML;
}

function doClientRefinement(obj)
{
	var data = {}
	data.dummy = "";
	data.field_id = 'selClientId';
	data.input_id = obj.value;
	callModule("lib/getClientsByNameNumber.php", data);
}

function getClientBookings()
{
	var data = {};
	data.input_id = document.getElementById('selClientId').value;
	data.field_id = 'selBsId';
	callModule("lib/getClientBookings.php", data);
}
