window.onload = function() 
{
	if (document.getElementById)
	{
		var aApplicationForm = document.getElementById('applicationform');
		if (aApplicationForm != null) aApplicationForm.onsubmit = JSFnValidateApplicationForm;

		SetupArchive();

		SetupApplicationForm();

		// location maintenance
		var aLocations = document.getElementById('locations');
		if (aLocations != null) SetupLocationFunctionality();
	}	
}

var aApplicationFormRequiredFields = new Array ("Name","Please enter your organisation name to continue",
												"Address","Please enter your address to continue",
												"Telephone","Please enter your telephone number to continue",
												"FirstName","Please enter your first name to continue",
												"Surname","Please enter your surname to continue",
												"Email","Please enter your email address to continue",
												"Password","Please enter a password to continue",
												"PasswordConfirmation","Please enter a password confirmation to continue");

function JSFnValidateApplicationForm()
{
	var aPwd = document.getElementById('Password');
	var aPwd2 = document.getElementById('PasswordConfirmation');
	if (aPwd.value != aPwd2.value)
	{
		alert('Your password and password confirmation do not match. Please re-enter the password you require.');
		return false;
	}

	return JSFnValidateForm(aApplicationFormRequiredFields);
}

function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}

function SetupArchive()
{
	aArchiveSpan = document.getElementById('archivespan');
	aArchiveDiv = document.getElementById('archive');
	if ((aArchiveSpan != null) && (aArchiveDiv != null))
	{
		HideArchive();
	}
}

function ShowArchive()
{
	aArchiveSpan = document.getElementById('archivespan');
	aArchiveDiv = document.getElementById('archive');
	if ((aArchiveSpan != null) && (aArchiveDiv != null))
	{
		aArchiveSpan.innerHTML = '<p><a href="" onclick="HideArchive(); return false;" title="Hide Archives">Click here to hide our archives.</a></p>';
		aArchiveDiv.style.display = 'block';
	}
}

function HideArchive()
{
	aArchiveSpan = document.getElementById('archivespan');
	aArchiveDiv = document.getElementById('archive');
	if ((aArchiveSpan != null) && (aArchiveDiv != null))
	{
		aArchiveSpan.innerHTML = '<p><a href="" onclick="ShowArchive(); return false;" title="Show Archives">Click here to show our archives.</a></p>';
		aArchiveDiv.style.display = 'none';
	}
}

function SetupApplicationForm()
{
	aAppForm = document.getElementById('applicationform');
	aCodeOfPracticeCB = document.getElementById('codeofpracticecb');
	if ((aAppForm != null) && (aCodeOfPracticeCB != null))
	{
		aCodeOfPracticeCB.onchange = CodeOfPracticeChange;
		CodeOfPracticeChange();
	}
}

function CodeOfPracticeChange()
{
	aAppForm = document.getElementById('applicationform');
	aCodeOfPracticeCB = document.getElementById('codeofpracticecb');
	if ((aAppForm != null) && (aCodeOfPracticeCB != null))
	{
		if (aCodeOfPracticeCB.checked) aInputEnabled = true;
		else  aInputEnabled = false;
		
		aInputs = aAppForm.getElementsByTagName("input");
		for (aIndex = 0; aIndex < aInputs.length; aIndex++)
		{
			if (aInputs[aIndex].id != 'codeofpracticecb') aInputs[aIndex].disabled = !aInputEnabled;
		}

		aTextareas = aAppForm.getElementsByTagName("textarea");
		for (aIndex = 0; aIndex < aTextareas.length; aIndex++)
		{
			aTextareas[aIndex].disabled = !aInputEnabled;
		}
	}
}

function SetupMemberList()
{
	aRightPage = document.getElementById('memberspage');
	if (aRightPage != null)
	{
		var aFirstMember = null; 

		var aNewSelect = document.createElement('select');
		aNewSelect.onchange = JumpToMember;
		aNewSelect.style.width = '100%';

		aMembers = document.getElementsByTagName("h3");
		for (aIndex = 0; aIndex < aMembers.length; aIndex++)
		{
			if (aMembers[aIndex].className == "membernameheader")
			{
				if (aIndex == 0) aFirstMember = aMembers[aIndex];
				var aNewOption = document.createElement('option');
				aNewOption.innerHTML = aMembers[aIndex].innerHTML;
				aNewOption.value = aIndex;
				aNewSelect.appendChild(aNewOption);
				aMembers[aIndex].id = 'member' + aIndex;
			}
		}

		var aNewP = document.createElement('p');
		aNewP.innerHTML = "To jump straight to a particular member's details simply select their name from the list below.";
		var aNewHR = document.createElement('hr');

		aContent = document.getElementById('content');
		aContent.insertBefore(aNewP, aFirstMember);
		aContent.insertBefore(aNewSelect, aFirstMember);
		aContent.insertBefore(aNewHR, aFirstMember);
	}
}

function JumpToMember()
{
	window.location = '#member' + this.value;
}

function SetupLocationFunctionality()
{
	aLocationsDD = document.getElementById('locations');
	aAddLocationBtn	= document.getElementById('addlocation'); 
	aRemoveLocationBtn	= document.getElementById('removelocation'); 
	if ((aLocationsDD != null) && (aAddLocationBtn != null))
	{
		aAddLocationBtn.onclick = JSFnAddLocation;
		aRemoveLocationBtn.onclick = JSFnRemoveLocation;
	}
}

function JSFnAddLocation()
{
	aLocationsDD = document.getElementById('locations');
	aRegionDD = document.getElementById('regionid');
	aNewLocationBox = document.getElementById('newlocation');
	aLocationsText = document.getElementById('locationstext');
	if ((aLocationsDD != null) && (aRegionDD != null) && (aNewLocationBox != null) && (aLocationsText != null))
	{
		if ((aNewLocationBox.value != '') && (aRegionDD.options[aRegionDD.selectedIndex].text != 'Select Region...'))
		{	
			var aNewOption = document.createElement('option');
			aNewOption.text = aNewLocationBox.value + ' (' + aRegionDD.options[aRegionDD.selectedIndex].text + ')';
			aNewOption.value = '' ;
			aNewOption.selected = true ;
			aLocationsDD.options[aLocationsDD.options.length] = aNewOption;
			aLocationsText.value = aLocationsText.value + aNewLocationBox.value + ',' + aRegionDD.options[aRegionDD.selectedIndex].text + '**';
		}
	}
}

function JSFnRemoveLocation()
{
	aLocationsDD = document.getElementById('locations');
	aLocationsText = document.getElementById('locationstext');
	if ((aLocationsDD != null) && (aLocationsText != null))
	{
		if (aLocationsDD.selectedIndex > -1)
		{
			aLocationToRemove = aLocationsDD.options[aLocationsDD.selectedIndex].text.replace(' (', ',').replace(')', '**');
			aLocationsDD.remove(aLocationsDD.selectedIndex);
			aLocationsText.value = aLocationsText.value.replace(aLocationToRemove, '');
			aLocationsDD.selectedIndex = 0;
		}
	}
}