// checks for a valid email address using regular expressions
function InvalidEmail(email)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	return !(filter.test(email));
}

// checks for a valid name using regular expressions
function InvalidName (name)
{
	var filter = /^([a-zA-Z0-9_\.\-\s])+$/;
	return !(filter.test(name));
}
function submitSubscribeForm()
{
	var errorMsg = "Unable to save your details because :\n"; // default error message
	var errorReason = ""; // to hold actual error reasons
	var focusItem = "";	 // to hold name of input to focus on
	var theForm;
	
	theForm = document.getElementById("frmSubscribe");
	
	// check that the email is valid
	if (theForm.txtEmail.value=="" || InvalidEmail(theForm.txtEmail.value))
	{
		errorReason += "	* Your Email address is not valid\n";
		if (focusItem == "") focusItem = "txtEmail";
	}
	
	// if there are any errors display them, otherwise submit the form
	if (!(errorReason==""))
	{
		alert(errorMsg + errorReason);
		eval("theForm." + focusItem + ".focus()");
		return false;
	} else {
		return true;
	}
	
}

var aFieldsToValidate = new Array();
var sNumbers = "0123456789";

function submitBrochureRequestForm(lAtBranch)
{
	var errorMsg = "Unable to save your details because :\n"; // default error message
	var errorReason = ""; // to hold actual error reasons
	var focusItem = "";	 // to hold name of input to focus on
	var theForm;

	theForm = document.getElementById("frmSubscribe");

	// check user has entered a salutation
	if (theForm.txtSalutation.value == "")
	{
		errorReason += "	* You have not entered a title\n";
		focusItem = "txtSalutation";
	}

	// check user has entered a forename
	if (theForm.txtForename.value == "" && !lAtBranch)
	{
		errorReason += "	* You have not entered a forename\n";
		if (focusItem == "") focusItem = "txtForename";
	}

	// check user has entered a surname
	if (theForm.txtSurname.value == "")
	{
		errorReason += "	* You have not entered a surname\n";
		if (focusItem == "") focusItem = "txtSurname";
	}

	// check user has entered address 1
	if (theForm.txtAddress1.value == "" && !lAtBranch)
	{
		errorReason += "	* You have not entered the first line of your address\n";
		if (focusItem == "") focusItem = "txtAddress1";
	}

	// check user has entered a town
	if (theForm.txtTown.value == "" && !lAtBranch)
	{
		errorReason += "	* You have not entered a town\n";
		if (focusItem == "") focusItem = "txtTown";
	}

	// check user has entered a post code
	if (theForm.txtPostCode.value == "" && !lAtBranch)
	{
		errorReason += "	* You have not entered a post/zip code\n";
		if (focusItem == "") focusItem = "txtPostCode";
	}

	// check that the email is valid
	if ((!lAtBranch && theForm.txtEmail.value == "") || (theForm.txtEmail.value != "" && InvalidEmail(theForm.txtEmail.value)))
	{
		errorReason += "	* Your Email address is not valid\n";
		if (focusItem == "") focusItem = "txtEmail";
	}

	// Extra validation fields when at Branch
	if (lAtBranch)
	{
		function validateDateFields(sDescription, oDay, oMonth, oYear, lAllowNull)
		{
			var dDate = new Date();
			
			if (lAllowNull && oDay.value == "" && oMonth.value == "" && oYear.value == "")
				return true;
			
			if (oDay.value != "" || oMonth.value != "" || oYear.value != "")
			{	// They have typed something in this date
				if (oDay.value == "" || oMonth.value == "")
				{	// But not in the day and month
					errorReason += "	* You must enter both the Day and the Month for " + sDescription + "\n";
					if (focusItem == "") focusItem = oDay.name;
				}
			}
			if (oYear.value != "" && oYear.value.length < 4)
			{
				errorReason += "	* " + sDescription + " year must be 4 digits long\n";
				if (focusItem == "") focusItem = oYear.name;
			}

			if (oYear.value == "")
				oYear.value = 1212;
				
			dDate.setFullYear(oYear.value, oMonth.value - 1, oDay.value)
			if (dDate.getFullYear() != oYear.value || dDate.getMonth() + 1 != oMonth.value || dDate.getDate() != oDay.value)
			{
				errorReason += "	* " + sDescription + " date is not correct\n";
				if (focusItem == "") focusItem = oDay.name;
			}
		}
		
		validateDateFields("Birthday", theForm.cboBDay, theForm.cboBMonth, theForm.cboBYear, true);
		validateDateFields("Anniversary", theForm.cboADay, theForm.cboAMonth, theForm.cboAYear, true);
		validateDateFields("Partner's Birthday", theForm.cboPDay, theForm.cboPMonth, theForm.cboPYear, true);
		validateDateFields(theForm.txt1.value + "'s Birthday", theForm.cbo1Day, theForm.cbo1Month, theForm.cbo1Year, true);
		validateDateFields(theForm.txt2.value + "'s Birthday", theForm.cbo2Day, theForm.cbo2Month, theForm.cbo2Year, true);

		for (a in aFieldsToValidate)
		{
			var oField;
			oField = aFieldsToValidate[a];

			if (oField.myAllowNull && oField.value == "")
			{
				// That's ok then
			}
			else if (!oField.myAllowNull && oField.value == "")
			{
				errorReason += "	* You have not entered: " + oField.myDescription + "\n";
				if (focusItem == "") focusItem = oField.name;
			}
			else if (oField.value < oField.myMinValue)
			{
				errorReason += "	* " + oField.myDescription + " can not be less than " + oField.myMinValue + "\n";
				if (focusItem == "") focusItem = oField.name;
			}
			else if (oField.value > oField.myMaxValue)
			{
				errorReason += "	* " + oField.myDescription + " can not be greater than " + oField.myMaxValue + "\n";
				if (focusItem == "") focusItem = oField.name;
			}
			else if (oField.myNumericOnly)
			{
				var allValid = true;
				
				for (i = 0; i < oField.value.length; i++)
				{
					ch = oField.value.charAt(i);
					for (k = 0;	k < sNumbers.length;	k++)
					if (ch == sNumbers.charAt(k))
						break;
					if (k == sNumbers.length)
					{
						allValid = false;
						break;
					}
				}
				if (!allValid)
				{
					errorReason += "	* " + oField.myDescription + " can only contain numbers\n";
					if (focusItem == "") focusItem = oField.name;
				}
			}
		}
		// check user has entered a branch
		if (theForm.txtBranch.value == "")
		{
			errorReason += "	* You have not entered a branch\n";
			if (focusItem == "") focusItem = "txtBranch";
		}
	}

	// if there are any errors display them, otherwise submit the form
	if (!(errorReason == ""))
	{
		alert(errorMsg + errorReason);
		eval("theForm." + focusItem + ".focus()");
		return false;
	}
	else
	{
		return true;
	}
}

function submitItemsForm()
{
	var errorMsg = "Unable to send item details because :\n"; // default error message
	var errorReason = ""; // to hold actual error reasons
	var focusItem = "";   // to hold name of input to focus on
	var theForm;
	
	theForm = document.getElementById("frmNewsletter");
	
	// check user has entered a name
	if (theForm.txtName.value == "")
	{
		errorReason += "  * You have not entered a name\n";
		focusItem = "txtName";
	}
  
	// check that the email is valid
	if (theForm.txtEmail.value == "" || InvalidEmail(theForm.txtEmail.value))
	{
		errorReason += "  * The Email address is not valid\n";
		if (focusItem == "") focusItem = "txtEmail";
	}

	// check user has entered a subject
	if (theForm.txtSubject.value == "")
	{
		errorReason += "  * You have not entered a subject\n";
		if (focusItem == "") focusItem = "txtSubject";
	}

	if (theForm.fileImage)
	{
		// check user has chosen a picture
		if (theForm.cboImage.value == "" && theForm.fileImage.value == "")
		{
			errorReason += "  * You have not chosen an image\n";
			if (focusItem == "") focusItem = "cboImage";
		}
	}
	else
	{
		// check user has chosen a picture
		if (theForm.cboImage.value == "")
		{
			errorReason += "  * You have not chosen an image\n";
			if (focusItem == "") focusItem = "cboImage";
		}
	}

	// check user has entered an Item ID
	if (theForm.Item.value == "")
	{
		errorReason += "  * You have not entered an item id\n";
		if (focusItem == "") focusItem = "Item";
	}

	// check user has entered an Item Description
	if (theForm.ItemDesc.value == "")
	{
		errorReason += "  * You have not entered an item description\n";
		if (focusItem == "") focusItem = "ItemDesc";
	}

	// check user has selected a from person
	if (theForm.cboSignature.value == "")
	{
		errorReason += "  * You have not selected who the email is from\n";
		if (focusItem == "") focusItem = "cboSignature";
	}

	// if there are any errors display them, otherwise submit the form
	if (!(errorReason == ""))
	{
		alert(errorMsg + errorReason);
		eval("theForm." + focusItem + ".focus()");
		return false;
	}
	else
	{
		return true;
	}
}

function submitMItemsForm()
{
	var errorMsg = "Unable to send item details because :\n"; // default error message
	var errorReason = ""; // to hold actual error reasons
	var focusItem = "";   // to hold name of input to focus on
	var theForm = document.getElementById('frmNewsletter');

	// check user has entered a name
	if (theForm.txtName.value=="" )
	{
		errorReason += "  * You have not entered a name\n";
		focusItem = "txtName";
	}

	// check that the email is valid
	if (document.forms[0].txtEmail.value=="" || InvalidEmail(document.forms[0].txtEmail.value))
	{
		errorReason += "  * The Email address is not valid\n";
		if ( focusItem == "" ) focusItem = "txtEmail";
	}

	// check user has entered a subject
	if (theForm.txtSubject.value=="" )
	{
		errorReason += "  * You have not entered a subject\n";
		if ( focusItem == "" ) focusItem = "txtSubject";
	}

	// check user has chosen a picture
	if (theForm.fileImage1)
	{
		if (theForm.cboImage1.value == "" && theForm.fileImage1 == "")
		{
			errorReason += "  * You have not chosen an image\n";
			if ( focusItem == "" ) focusItem = "cboImage1";
		}
	}
	else
	{
		if (theForm.cboImage1.value=="")
		{
			errorReason += "  * You have not chosen an image\n";
			if ( focusItem == "" ) focusItem = "cboImage1";
		}
	}

	// check user has selected a from person
	if (theForm.cboSignature.value=="" )
	{
		errorReason += "  * You have not selected who the email is from\n";
		if ( focusItem == "" ) focusItem = "cboSignature";
	}

	// if there are any errors display them, otherwise submit the form
	if (!(errorReason=="" ))
	{
		alert(errorMsg + errorReason);
		eval("document.forms[0]." + focusItem + ".focus()");
		return false;
	}
	else
	{
		return true;
	}
}
