var errors, hddnTeamId, edtTeamName, selVehicleId, selUserCode, cbTeamActive, lstDOW;
var module = baseRootURL + "team/";

var userItem = new addSelectionList('userItem', 'user', 'UserElement');
userItem.setColWidth(4);

function registerTeam(action)
{
	errors = false;
	validate_hddnTeamId();
	validate_edtTeamName();
	validate_selVehicleId();
	validate_cbTeamActive();
	CheckUser();
	getCheckedWeekDays();
	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("hddnTeamId") != null) data.hddnTeamId = hddnTeamId;
		data.edtTeamName = edtTeamName;
		data.selVehicleId = selVehicleId;
		data.cbTeamActive = cbTeamActive;
		data.userList = userItem.getItemsAsString();
		data.lstDOW = lstDOW == undefined ? 0 : JSON.stringify(lstDOW);
		callModule("team/team_val.php", data);
	}
}
function insertTeam(){registerTeam(0)}
function modifyTeam(){registerTeam(1)}

function validate_hddnTeamId(){
	if (document.getElementById("hddnTeamId") != null) hddnTeamId=document.getElementById("hddnTeamId").value;}
function validate_edtTeamName(){edtTeamName=validateRequiredText("edtTeamName", "This field is required.");}
function validate_selVehicleId(){selVehicleId=validateSelection("selVehicleId", "Please make a valid selection.");}
function validate_cbTeamActive(){cbTeamActive = document.getElementById("cbTeamActive").checked;}
function CheckUser(){userItem.validateItems('1', 'selUserCode', 'user');}

function viewTeam(id)
{
	var data = {}
	data.dummy = "";
	data.hddnTeamId = id;
	data.action = 2;
	callModule("team/team_val.php", data);
}

function editTeam(id)
{
	var data = {}
	data.dummy = "";
	data.hddnTeamId = id;
	data.action = 3;
	callModule("team/team_val.php", data);
}

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

function getCheckedWeekDays()
{
	var wd = document.getElementsByName('dayOfWeek');
	lstDOW = {};
	var c = 0;
	var v = 0;
	for(var i = 0; i < wd.length; i++) 
	{
		v = i+1;
		if (wd[i].checked) 
		{
			lstDOW[c]		= {};
			lstDOW[c].value	= v; //Monday = 1 thru Sunday = 7
			c++;
		}
	}

	if (c === 0) 
	{
		document.getElementsByName('cbTeamActive').checked = false;
		cbTeamActive = false;
	}
}