var errors, hddnServiceId, selAreaId, edtServiceName, edtServiceDuration, edtServicePrice;
var module = baseRootURL + "service/";
var areaItem = new addSelectionList('areaItem', 'area', 'AreaElement');
areaItem.setColWidth(4);
function registerService(action)
{
	errors = false;
	validate_hddnServiceId();
	//validate_selAreaId();
	CheckArea();
	validate_edtServiceName();
	validate_edtServiceDuration();
	validate_edtServicePrice();
	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("hddnServiceId") != null) data.hddnServiceId = hddnServiceId;
		data.areaList = areaItem.getItemsAsString();
		data.edtServiceName = edtServiceName;
		data.edtServiceDuration = edtServiceDuration;
		data.edtServicePrice = edtServicePrice;
		callModule("service/service_val.php", data);
	}
}
function insertService(){registerService(0)}
function modifyService(){registerService(1)}

function validate_hddnServiceId(){
	if (document.getElementById("hddnServiceId") != null) hddnServiceId=document.getElementById("hddnServiceId").value;}
function validate_selAreaId(){selAreaId=validateRequiredSelection("selAreaId", "Please make a valid selection.");}
function CheckArea(){areaItem.validateItems('1', 'selAreaId', 'area');}
function validate_edtServiceName(){edtServiceName=validateRequiredText("edtServiceName", "This field is required.");}
function validate_edtServiceDuration(){edtServiceDuration=validateRequiredNumber("edtServiceDuration", "Please enter a valid numeric value.");}
function validate_edtServicePrice(){edtServicePrice=validateRequiredNumber("edtServicePrice", "Please enter a valid numeric value.");}

function viewService(id)
{
	var data = {}
	data.dummy = "";
	data.hddnServiceId = id;
	data.action = 2;
	callModule("service/service_val.php", data);
}

function editService(id)
{
	var data = {}
	data.dummy = "";
	data.hddnServiceId = id;
	data.action = 3;
	callModule("service/service_val.php", data);
}

function copyService(id, desc)
{
	if (confirm("Are you sure you want to duplicate "+desc+"?"))
	{
		var data = {}
		data.dummy = "";
		data.hddnServiceId = id;
		data.action = 5;
		callModule("service/service_val.php", data);
	}
}

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

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');
	document.getElementById('edtAreaName').value = sel.options[sel.selectedIndex].innerHTML;
}
