
function getFormElement(frmName,elemName)
{
	var frm = document.forms[frmName];
	if (frm == null)
	{
		alert("Cannot find form: " + frmName);
		return null;
	}

	var elem = frm.elements[elemName];
	if (elem == null)
	{
		alert("Cannot find select: " + elemName);
		return null;
	}
	return elem;
}

function getNoOfCheckboxesChecked(frmName,arrElems)
{
	if (arrElems == null)
		return 0;
		
	var count = 0;
	var selCount = 0;
	for (count=0; count<arrElems.length; count++)
	{
		var chk = getFormElement(frmName,arrElems[count]);
		if (chk == null)
			return 0;
		if (chk.checked)
			selCount++;
	}
	return selCount;
}

function validateAddCountrySelect(frmName,selName)
{
	try
	{
		var sel = getFormElement(frmName,selName);
		if (sel == null)
			return false;

		if (sel.selectedIndex < 0)
		{
			alert("Please select a country");
			return false;
		}
	}
	catch(Error)
	{
	}
	return true;
}

function validateAddCitySelect(frmName,selName)
{
	try
	{
		var sel = getFormElement(frmName,selName);
		if (sel == null)
			return false;

		if (sel.selectedIndex < 0)
		{
			alert("Please select a city to add");
			return false;
		}
	}
	catch(Error)
	{
	}
	return true;
}

function validateMakeHomeCheckbox(frmName,arrElems)
{
	try
	{
		var selCount = getNoOfCheckboxesChecked(frmName,arrElems);
		switch (selCount)
		{
		case 0:
			alert("Please select a city to become your home city");
			return false;
		case 1:
			return true;
		}

		if (confirm("The first city selected will become your home city"))
			return true;
		return false;
	}
	catch(Error)
	{
	}
	return true;
}

function validateDeleteCitiesCheckbox(frmName,arrElems)
{
	try
	{
		var selCount = getNoOfCheckboxesChecked(frmName,arrElems);
		if (selCount == 0)
		{
			alert("Please select cities to remove from your group");
			return false;
		}
	}
	catch(Error)
	{
	}
	return true;
}

function SelectInput(frmName,selName)
{
	try
	{
		var sel = getFormElement(frmName,selName);
		if (sel == null)
			return false;

		sel.focus();
	}
	catch(Error)
	{
	}
}

function checkEnter(e)
{
	try
	{
		//e is event object passed from function invocation
		var characterCode // literal character code will be stored in this variable

		if (e && e.which)
		{			//if which property of event object is supported (NN4)
			e = e
			characterCode = e.which //character code is contained in NN4's which property
		}
		else
		{
			e = event
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}

		if (characterCode == 13)
		{			//if generated character code is equal to ascii 13 (if enter key)
			document.forms[0].submit() //submit the form
			return false
		}
		else
		{
			return true
		}
	}
	catch(Error)
	{
	}
}

