//
// System : The Manna Reserve™
// Copyright : Copyright (c) 2000-2008 The Manna Reserve
// Author : WebMinister
// Webmaster : WebMinister : webministe@themannareserve.org
//
// File : TCustomPageGrid.js : page javascript include file
//
// Written Friday June 27, 2008
//

var RecordCount;

// SupplyRecords can be one, none, or many
// Confirmation if user needs to confirm operation
function _CallPage(URL, Target, SupplyRecords, Confirmation)
{
	var Filter = '';
	var FilterName = '';
	var URL2;

	if (arguments.length < 4)
		Confirmation = '';

	if (arguments.length < 3)
		SupplyRecords = 'one';

	if (arguments.length < 2)
		Target = '_blank';

	if (SupplyRecords != 'none')
	{
		var itemCount = _SelectRecords();

		if (itemCount == 0)
		{
			if (SupplyRecords == 'one')
				alert('Error: you must select one record first by clicking a checkbox.');
			else
				alert('Error: you must select at least one record first by clicking a checkbox.');
			return;
		}

		if ((SupplyRecords == 'one') && (itemCount > 1))
		{
			alert('Error: select only one record.');
			return;
		}

		Filter = RecordArray[0][1];
	}

	if (Confirmation != '')
	{
		if (!confirm(Confirmation))
			return;
	}

	if (Filter != '')
		URL2 = URL + '.' + Filter;
	else
		URL2 = URL;
	
	if (Target == '_blank')
		window.open(URL2);
	else
		location.href = URL2;
}
//--------------- end of the function _CallPage() --------------------

function _SelectRecords()
{
	var RecordCount = 0;
	var regexpression = /\+/;
	var ID = '';
	var Name = '';
	var Row = ''

	ThisForm = document.getElementById('myform');
	
	if (!ThisForm)
		return;

	if (!ThisForm)	// form missing
		return RecordCount;

	if (!ThisForm.elements['RowChecked'])	// no 'Rowchecked' elements in form
		return RecordCount;

	if (!ThisForm.elements['RowChecked'].value)	// more than 1 row of elements is present
	{
		for (counter = 0; counter < ThisForm.elements['RowChecked'].length; counter++)
		{
			if (ThisForm.elements['RowChecked'][counter].checked == true
				&& ThisForm.elements['RowChecked'][counter].value != '')
				RecordCount++;
		}

		if (RecordCount == 0)
			return RecordCount;

		for (counter = 0; counter < ThisForm.elements['RowChecked'].length; counter++)
		{
			if (ThisForm.elements['RowChecked'][counter].checked == true
				&& ThisForm.elements['RowChecked'][counter].value != '')
			{
				temp = ThisForm.elements['RowChecked'][counter].value;
				loc = temp.search(regexpression);
				if (loc != -1)
				{
					ID1 = temp.substr(0, loc);
					Name = temp.substr(loc + 1);
					Row = counter + 1;
				}
				else
					ID1 = temp;

				if (ID == '')
					ID = ID1;
				else
					ID += ',' + ID1;

			}
		}
	}
	else if (ThisForm.elements['RowChecked'].checked==true
		&& ThisForm.elements['RowChecked'].value != '')	// only 1 row of elements
	{
		temp = ThisForm.elements['RowChecked'].value;
		loc = temp.search(regexpression);
		Row = 1;
		if (loc != -1)
		{
			ID = temp.substr(0, loc);
			Name = temp.substr(loc + 1);
		}
		else
		{
			ID = temp;
			Name = '';
		}
		RecordCount = 1;
	}
	RecordArray = new Array(1);
	RecordArray[0] = new Array(3);
	RecordArray[0][0] = Name;
	RecordArray[0][1] = ID;
	RecordArray[0][2] = Row;
	return RecordCount;
}
//--------------- End of the Function _SelectRecords() --------------------

function _SelectAllRecords(SelectAll)
// toggles selection of all or none of the records in the table
{
	if (arguments.length < 1)
		SelectAll = false;

	ThisForm = document.getElementById('myform');
	
	if (!ThisForm)
		return;

	if (!ThisForm.elements['RowChecked'])	// no checkboxes
		return;

	if (ThisForm.elements['RowChecked'].value)	// only 1 row of elements
	{
		if (ThisForm.elements['RowChecked'].type == 'checkbox')
			ThisForm.elements['RowChecked'].checked = SelectAll;
		return;
	}

	for (counter = 0; counter < ThisForm.elements['RowChecked'].length; counter++)
	{
		if (ThisForm.elements['RowChecked'][counter].type == 'checkbox')
			ThisForm.elements['RowChecked'][counter].checked = SelectAll;
	}
}
//--------------- End of the Function _SelectAllRecords() --------------------
