window.onload = jsFnOnLoad;

function jsFnOnLoad()
{	
	if (document.getElementById)
	{
		var aLogo = document.getElementById('logo');
		if (aLogo != null) 
		{
			aLogo.onclick = function() {window.location = 'index.html';};
			aLogo.style.cursor = 'pointer';
		}
		
		var aDocumentAddForm = document.getElementById('documentaddform');
		if (aDocumentAddForm != null) aDocumentAddForm.onsubmit = JSFnValidateDocumentForm;
		
		var aDocumentEditForm = document.getElementById('documenteditform');
		if (aDocumentEditForm != null) aDocumentEditForm.onsubmit = JSFnValidateDocumentEditForm;
		
		var aSubmissionsForm = document.getElementById('uploadeddocadd');
		if (aSubmissionsForm != null) aSubmissionsForm.onsubmit = JSFnValidateUploadDocAddForm;
		
		var aSubmissionsEditForm = document.getElementById('uploadeddocedit');
		if (aSubmissionsEditForm != null) aSubmissionsEditForm.onsubmit = JSFnValidateUploadDocEditForm;
			
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;
		
//		var aIHTCalc = document.getElementById('disclaimerpara');
//		if (aIHTCalc != null) JSFnIHTCalc();

		var aIHTCalcLink = document.getElementById('ihtcalclink');
		if (aIHTCalcLink != null) aIHTCalcLink.onclick = JsFnCalcLink;
		
		var aIHTAbort = document.getElementById('ihtcalculatorabort');
		if (aIHTAbort != null)
		{
			aIHTAbort.onclick = JSFnHideIHTForm;
		}
		
		aLinks = document.getElementsByTagName("a");
		for (aIndex = 0; aIndex < aLinks.length; aIndex++)
		{
			if (aLinks[aIndex].className == 'parentlink') 
			{
				aLinks[aIndex].onclick = JsFnCalcParentLink;
			}
			if (aLinks[aIndex].href.indexOf('images/map.jpg') != -1) aLinks[aIndex].onclick = JsFnShowMap;;
		}
		
		aSelects = document.getElementsByTagName("select");
		for (aIndex = 0; aIndex < aSelects.length; aIndex++)
		{
			if (aSelects[aIndex].id.indexOf('pageimage') == 0) 
			{
				aAssociatedImage = 'viewimage' + aSelects[aIndex].id.substr(9);
				jsFnChangeImage(aAssociatedImage, aSelects[aIndex], 'pageimages');
			}
		}
	}
}	


var aDocumentRequiredFields = new Array ("Category","Please select a category to continue",
										 "Name","Please enter a document name to continue",
										 "documenttoupload","Please select a document to upload");
function JSFnValidateDocumentForm()
{
	return JSFnValidateForm(aDocumentRequiredFields);
}

var aDocumentEditRequiredFields = new Array ("Category","Please select a category to continue",
										     "Name","Please enter a document name to continue");
function JSFnValidateDocumentEditForm()
{
	return JSFnValidateForm(aDocumentEditRequiredFields);
}

var aUploadDocAddRequiredFields = new Array ("Name","Please enter a document name to continue",
 										     "Reference","Please enter a reference to continue",
										     "documenttoupload1","You must select at least one document to upload");
function JSFnValidateUploadDocAddForm()
{
	return JSFnValidateForm(aUploadDocAddRequiredFields);
}

var aUploadDocEditRequiredFields = new Array("Name","Please enter a document name to continue",
 										     "Reference","Please enter a reference to continue");
function JSFnValidateUploadDocEditForm()
{
	return JSFnValidateForm(aUploadDocEditRequiredFields);
}

var aContactFormRequiredFields = new Array("Name","Please enter your name to continue");
function JSFnValidateContactForm()
{
	var aDaytimeTelephone = document.getElementById('DaytimeTelephone');
	var aEveningTelephone = document.getElementById('EveningTelephone');
	
	if ((aDaytimeTelephone != null) && (aEveningTelephone != null))
	{
		if ((aDaytimeTelephone.value == '') && (aEveningTelephone.value == ''))
		{
			alert('You must complete either your daytime or evening telephone number to continue');
			return false;
		}
		else return JSFnValidateForm(aContactFormRequiredFields);
	}
	
	return true;
}

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 jsFnChangeImage(aImageToChange, aImageToShow, aDir)
{
	if  (document.getElementById)
	{
		var aImage = document.getElementById(aImageToChange);
		aImage.src = '../images/' + aDir + '/' + aImageToShow.value;
	}
}

function JSFnIHTCalc()
{
	var aIHTPara = document.getElementById('disclaimerpara');
	var aIHTForm = document.getElementById('ihtcalculatorform');
	if (document.location.href.indexOf('#yourcurrentsituation') == -1)
	{
		if ((aIHTPara != null) && (aIHTForm != null))
		{
			aIHTForm.style.display = 'none';
			
			aButton = document.createElement("input");
			aButton.type = 'button';
			aButton.className = 'button';	
			aButton.value = 'Continue';
			aButton.onclick = JSFnShowIHTForm;
			aIHTPara.appendChild(aButton);
			
			aButton2 = document.createElement("input");
			aButton2.type = 'button';
			aButton2.className = 'button';
			aButton2.value = 'Abort';
			aButton2.onclick = JSFnHideIHTForm;
			aIHTPara.appendChild(aButton2);
		}
	}
	else if (aIHTPara != null) 
	{
		aIHTPara.style.display = 'none';
	}
}

function JSFnShowIHTForm()
{
	var aIHTPara = document.getElementById('disclaimerpara');
	var aIHTForm = document.getElementById('ihtcalculatorform');
	if (aIHTForm != null)
	{
		aIHTForm.style.display = 'block';
		aIHTPara.style.display = 'none';
	}
}

function JSFnHideIHTForm()
{
	window.close();
}

function JsFnCalcLink()
{
	var aNewWindow = window.open(this.href, '', 'width=630px, height=720px, scrollbars');
	aNewWindow.focus();
	return false;
}

function JsFnCalcParentLink()
{
	window.opener.location = this.href;
	window.opener.focus();
	return false;
}

function JsFnShowMap()
{
	var aMapWindow = window.open(this.href, '', 'width=800px, height=500px');
	aMapWindow.focus();
	return false;
}