var errors, hddnClientId, edtClientName, edtClientSurname, hddnIdTypeId, edtClientIdNo, hddnClientTypeId, hddnAddressId, edtClientAddressNote, edtClientDistance, cbClientAddrBill, edtClientPhone, edtClientPhoneAlt, edtClientEmail, txtClientNote, hddnGenderId, edtClientBusinessName, edtClientVatNo, edtClientRegNo, cbClientContract, hddnClientParentId, hddnRewardLevelId, hddnClientDeleted;
var module = baseRootURL + "client/";

function registerClient(action)
{
	errors = false;
	validate_hddnClientId();
	validate_edtClientName();
	validate_edtClientSurname();
	validate_hddnIdTypeId();
	validate_edtClientIdNo();
	validate_hddnClientTypeId();
	validate_hddnAddressId();
	validate_edtClientAddressNote();
	validate_edtClientDistance();
	validate_cbClientAddrBill();
	validate_edtClientPhone();
	validate_edtClientPhoneAlt();
	validate_edtClientEmail();
	validate_txtClientNote();
	validate_hddnGenderId();
	validate_edtClientBusinessName();
	validate_edtClientVatNo();
	validate_edtClientRegNo();
	validate_cbClientContract();
	validate_hddnClientParentId();
	validate_hddnRewardLevelId();
	validate_hddnClientDeleted();
	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.hddnIdTypeId = hddnIdTypeId;
		data.edtClientIdNo = edtClientIdNo;
		data.hddnClientTypeId = hddnClientTypeId;
		data.hddnAddressId = hddnAddressId;
		data.edtClientAddressNote = edtClientAddressNote;
		data.edtClientDistance = edtClientDistance;
		data.cbClientAddrBill = cbClientAddrBill;
		data.edtClientPhone = edtClientPhone;
		data.edtClientPhoneAlt = edtClientPhoneAlt;
		data.edtClientEmail = edtClientEmail;
		data.txtClientNote = txtClientNote;
		data.hddnGenderId = hddnGenderId;
		data.edtClientBusinessName = edtClientBusinessName;
		data.edtClientVatNo = edtClientVatNo;
		data.edtClientRegNo = edtClientRegNo;
		data.cbClientContract = cbClientContract;
		data.hddnClientParentId = hddnClientParentId;
		data.hddnRewardLevelId = hddnRewardLevelId;
		data.hddnClientDeleted = hddnClientDeleted;
		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", "Client Name is required.");}
function validate_edtClientSurname(){edtClientSurname=validateRequiredText("edtClientSurname", "Client Surname is required.");}
function validate_hddnIdTypeId(){
	if (document.getElementById("hddnIdTypeId") != null) hddnIdTypeId=document.getElementById("hddnIdTypeId").value;}
function validate_edtClientIdNo(){edtClientIdNo=validateRequiredNumber("edtClientIdNo", "Client Id No should be a numeric.");}
function validate_hddnClientTypeId(){
	if (document.getElementById("hddnClientTypeId") != null) hddnClientTypeId=document.getElementById("hddnClientTypeId").value;}
function validate_hddnAddressId(){
	if (document.getElementById("hddnAddressId") != null) hddnAddressId=document.getElementById("hddnAddressId").value;}
function validate_edtClientAddressNote(){edtClientAddressNote=validateRequiredText("edtClientAddressNote", "Client Address Note is required.");}
function validate_edtClientDistance(){edtClientDistance=validateRequiredNumber("edtClientDistance", "Client Distance should be a numeric.");}
function validate_cbClientAddrBill(){cbClientAddrBill=validateRequiredNumber("cbClientAddrBill", "Same as billing address should be a numeric.");}
function validate_edtClientPhone(){edtClientPhone=validateRequiredText("edtClientPhone", "Client Phone is required.");}
function validate_edtClientPhoneAlt(){edtClientPhoneAlt=validateRequiredText("edtClientPhoneAlt", "Client Phone Alt is required.");}
function validate_edtClientEmail(){edtClientEmail=validateRequiredEmail("edtClientEmail", "Valid email address required.");}
function validate_txtClientNote(){txtClientNote=validateTextArea("txtClientNote", "Client Note is required.", true);}
function validate_hddnGenderId(){
	if (document.getElementById("hddnGenderId") != null) hddnGenderId=document.getElementById("hddnGenderId").value;}
function validate_edtClientBusinessName(){edtClientBusinessName=validateRequiredText("edtClientBusinessName", "Client Business Name is required.");}
function validate_edtClientVatNo(){edtClientVatNo=validateRequiredNumber("edtClientVatNo", "Client Vat No should be a numeric.");}
function validate_edtClientRegNo(){edtClientRegNo=validateRequiredNumber("edtClientRegNo", "Client Reg No should be a numeric.");}
function validate_cbClientContract(){cbClientContract = document.getElementById("cbClientContract").checked;}
function validate_hddnClientParentId(){
	if (document.getElementById("hddnClientParentId") != null) hddnClientParentId=document.getElementById("hddnClientParentId").value;}
function validate_hddnRewardLevelId(){
	if (document.getElementById("hddnRewardLevelId") != null) hddnRewardLevelId=document.getElementById("hddnRewardLevelId").value;}
function validate_hddnClientDeleted(){
	if (document.getElementById("hddnClientDeleted") != null) hddnClientDeleted=document.getElementById("hddnClientDeleted").value;}

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 associated records?"))
	{
		var data = {}
		data.dummy = "";
		data.hddnClientId = id;
		data.action = 4;
		callModule("client/client_val.php", data);
	}
}
