// The details to be checked for each form //
var loginformchecks = new Array ("string2","Please enter a username to continue",
								 "string1","Please enter a password to continue");
var updatedetailsformchecks = new Array ("Name","Please enter a name to continue",
										"Address","Please enter an address to continue",
										"Telephone","Please enter an telephone number to continue",
										"Fax","Please enter a fax number to continue",
										"Mobile","Please enter a mobile number to continue",
										"Email","Please enter an email address to continue",
										"Contact","Please enter a contact to continue");
var restorechecks = new Array ("categoryfiletoupload","Please select a category backup file to continue",
 							   "documentfiletoupload","Please select a document backup file to continue",
							   "userfiletoupload","Please select a user backup file to continue",
							   "userapplicationsfiletoupload","Please select an User Applications backup file to continue",
							   "epprates1filetoupload","Please select an EPP Rates 1 backup file to continue",
							   "epprates2filetoupload","Please select an EPP Rates 2 backup file to continue",
							   "epprates3filetoupload","Please select an EPP Rates 3 backup file to continue",
							   "epprates4filetoupload","Please select an EPP Rates 4 backup file to continue",
							   "epprates5filetoupload","Please select an EPP Rates 5 backup file to continue",
							   "epprates6filetoupload","Please select an EPP Rates 6 backup file to continue",
							   "epsrates1filetoupload","Please select an EPS Rates 1 backup file to continue",
							   "epsrates2filetoupload","Please select an EPS Rates 2 backup file to continue",
							   "epsrates3filetoupload","Please select an EPS Rates 3 backup file to continue",
							   "epsrates4filetoupload","Please select an EPS Rates 4 backup file to continue");
var categorychecks = new Array ("Type","Please enter a category group name to continue",
 							    "Description","Please enter a description to continue");
var documentaddchecks = new Array ("Description","Please enter a description to continue",
				   				   "DocToUpload","Please select a document to upload");
var documenttypeaddchecks = new Array ("Description","Please enter a description to continue");
var documenteditchecks = new Array ("Description","Please enter a description to continue");
var userchecks = new Array ("Name","Please enter a name to continue",
 							"Address","Please enter an address to continue",
						    "Telephone","Please enter a telephone number to continue",
						    "Email","Please enter an email address to continue",
						    "Contact","Please enter a contact to continue",
						    "ActivationDate","Please enter an activation date to continue",
						    "UserName","Please enter a username to continue",
						    "Password","Please enter a password to continue");
var usernamechecks = new Array ("UserName","Please enter a username to continue");
var emailformchecks = new Array ("subject","You must enter a subject in order to send the email",
								 "body","You must enter the main content of the email before sending");
var empprotpackratescheck = new Array ();
var epsratescheck = new Array ();

var formswithchecks = new Array('loginformid', loginformchecks, 
								'updatedetailsform', updatedetailsformchecks, 
								'restoreform', restorechecks,
								'addcategoryform', categorychecks,
								'categorydetailsform', categorychecks,
								'adddocumentform', documentaddchecks,
								'adddocumenttypeform', documenttypeaddchecks,
								'documentdetailsform', documenteditchecks,
								'adduserform', userchecks,
								'userdetailsform', userchecks,
								'usernameupdateform', usernamechecks,
								'emailform', emailformchecks,
								'empprotpackratesform', empprotpackratescheck,
								'epsratesform', epsratescheck);

var arrayToCheck;

window.onload = function()
{
	standard_onload();

	if (document.getElementById) 
	{
		// Apply any form checks that need doing
		for (aIndex = 0; aIndex < formswithchecks.length; aIndex = aIndex + 2)
		{
			currentForm = document.getElementById(formswithchecks[aIndex]);
			if (currentForm != null)
			{
				arrayToCheck = formswithchecks[aIndex + 1];
				if (formswithchecks[aIndex] == 'emailform') currentForm.onsubmit = EmailSpecificChecks;
				else if (formswithchecks[aIndex] == 'empprotpackratesform') currentForm.onsubmit = EmpProtPackRatesSpecificChecks;
				else if (formswithchecks[aIndex] == 'epsratesform') currentForm.onsubmit = EmpProtPackRatesSpecificChecks;
				else currentForm.onsubmit = CheckForm;				
			}
		}
	}
}

function CheckForm()
{
	for (aIndex = 0; aIndex < arrayToCheck.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(arrayToCheck[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(arrayToCheck[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(arrayToCheck[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(arrayToCheck[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}

function CheckDiaryEntries(Result)
{
	if (Result != false)
	{
		if (   (document.getElementById('DiaryDate').value != '') 
			&& (document.getElementById('DiaryText').value == ''))
		{
			alert('You must enter both a date and text if you wish to add a diary item.');
			return false;
		}
	}

	if (Result != false)
	{
		if ((document.getElementById('DiaryDate').value != '') && (!isDate(document.getElementById('DiaryDate').value)))
		{
			alert('The Diary Date field must be entered in the format dd/mm/yyyy');
			return false;
		}
	}
	
	return Result;
}

function CheckStatusEntries(Result)
{
	if (Result != false)
	{
		if (   (document.getElementById('PolicyNumber').value != '') 
			&& (    (document.getElementById('FromDate').value == '') 
				 || (document.getElementById('ToDate').value == '')))
		{
			alert('If you have entered a Policy Number you must also enter both a from and to date');
			return false;
		}
	}

	if (Result != false)
	{
		if (   ((document.getElementById('FromDate').value != '') && (!isDate(document.getElementById('FromDate').value)))
			|| ((document.getElementById('ToDate').value != '') && (!isDate(document.getElementById('ToDate').value)))) 
		{
			alert('The From/To Date fields must be entered in the format dd/mm/yyyy');
			return false;
		}
	}
	
	return Result;
}

function EmpProtPackRatesSpecificChecks()
{
	Result = true;
	changeReason = document.getElementById('reasonforchange');
	if (   (changeReason != null)
		&& (changeReason.value == ''))
	{
		alert('You must provide a reason for the change of rates');
		Result = false;
	}
	return Result;
}

function EmailSpecificChecks()
{
	Result = CheckForm();
	if (Result) 
	{
		if (!confirm("Continuing will send the email to the users you have selected.  Do you wish to continue?"))
		{
			Result = false;
		}
	}
	return Result;
}

function isDate(sDate) 
{
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(sDate)) 
	{
		var dArr = sDate.split("/");
		var d = new Date(dArr[2], dArr[1] - 1, dArr[0]);
		return d.getMonth() + 1 == dArr[1] && d.getDate() == dArr[0] && d.getFullYear() == dArr[2];
	}
	else 
	{
		return false;
	}
}

function getDate(sDate) 
{
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(sDate)) 
	{
		var dArr = sDate.split("/");
		var d = new Date(dArr[2], dArr[1] - 1, dArr[0]);
		return d;
	}
	else 
	{
		return false;
	}
}