function ValidateTelephone(ctlTelephone, blnMustExist)
{
	if (blnMustExist == true)
	{
		// Check that the Telephone Number has a value
		if (ctlTelephone.value.length == 0)
		{
			if (arguments.length == 2)
				alert("Please specifiy your telephone number");
			else
				alert(arguments[2]);

			ctlTelephone.focus();
			return false;
		}
	}

	if (ctlTelephone.value.length > 0)
	{
		if (ctlTelephone.value.search(/[^\d| |\-]/) != -1)
		{
			if (arguments.length == 2)
				alert("Please enter a valid telephone number");
			else
				alert(arguments[3]);

			ctlTelephone.focus();
			return false;
		}
	}
	
	return true;
}

function ValidateApplicationType()
{
	var blnSelectedType = false;
	for(intCount=0; intCount < document.application.App_Type.length; intCount++)
	{
		if (document.application.App_Type[intCount].checked == true)
		{
			blnSelectedType = true;
			break;
		}
	}

	if (blnSelectedType == false)
	{
		alert("Please select an application type");
		document.application.Media[0].focus();
		return false;
	}


	return true;
	

}

function ValidatePostcode(ctlPostcode, blnMustExist)
{
	if (blnMustExist == true)
	{
		// Check that the postcode has a value
		if (ctlPostcode.value.length == 0)
		{
			alert("Please specifiy your postcode");
			ctlPostcode.focus();
			return false;
		}
	}

	if (ctlPostcode.value.length > 10)
	{
		alert("Please enter a valid postcode");
		ctlPostcode.focus();
		return false;
	}

	if (ctlPostcode.value.length > 0)
	{
		if (ctlPostcode.value.search(/[^A-Z|a-z|\d| ]/) != -1)
		{
			alert("Please enter a valid postcode");
			ctlPostcode.focus();
			return false;
		}
	}

	return true;
}

function ValidateInitial()
{
	// Check that the initial has a value
	if (document.register.Initial.value.length == 0)
	{
		alert("Please specifiy your initial");
		document.register.Initial.focus();
		return false;
	}
	
	return true;
}

function ValidateEmail(ctlEmail, blnMustExist)
{
	if (blnMustExist == true)
	{
		// Check that the email address has a value
		if (ctlEmail.value.length == 0)
		{
			alert("Please specifiy your Email address");
			ctlEmail.focus();
			return false;
		}
	}

	if (ctlEmail.value.length > 0)
	{
		if (ctlEmail.value.search(/.+@.+\..+/) == -1)
		{
			alert("Please enter a valid Email address");
			ctlEmail.focus();
			return false;
		}
	}

	return true;
}

function ValidateTextDate(ctlTextDate)
{
	if (ctlTextDate.value.length > 0)
	{
		if (ctlTextDate.value.search(/\d{4}-\d\d?-\d\d?/) == -1)
		{
			alert("Please enter a valid Date in the format YYYY-MM-DD");
			ctlTextDate.focus();
			return false;
		}

		// replace -0 with - to enable parseint to work
		var strDate = ctlTextDate.value.replace(/-0/, "-");
		// we may have to do it twice. It doesn't hurt
		strDate = strDate.replace(/-0/, "-");

		var arrDateParts = strDate.split("-");
		var dateCheck = new Date(parseInt(arrDateParts[0]), parseInt(arrDateParts[1]) - 1, arrDateParts[2]);

		if (dateCheck.getDate() != parseInt(arrDateParts[2]))
		{
			alert("Please enter a valid date");
			ctlTextDate.focus();
			return false;
		}
	}

	return true;
}

function ValidateDate()
{
	var intDay = document.application.Day.options[document.application.Day.selectedIndex].text;
	var strMonth = document.application.Month.options[document.application.Month.selectedIndex].text;
	var intYear = document.application.Year.options[document.application.Year.selectedIndex].text;

	if (intDay == "Select")
	{
		alert("Please select a day");
		document.application.Day.focus();
		return false;
	}
	
	if (strMonth == "Select")
	{
		alert("Please select a month");
		document.application.Month.focus();
		return false;
	}

	if (intDay == "Select")
	{
		alert("Please select a year");
		document.application.Year.focus();
		return false;
	}

	var dateCheck = new Date(strMonth + " " + intDay + ", " + intYear);
	if (dateCheck.getDate() != parseInt(intDay))
	{
		alert("Please enter a valid date");
		document.application.Day.focus();
		return false;
	}

	return true;
}

function ValidateMediaType()
{
	//No media for Associate Licence
	if (document.application.ApplicationType[2].checked == true)
	{
		if ((document.application.Media[0].checked == true) || (document.application.Media[1].checked == true))
		{
			alert("No media type available for Associate License - Media type has been deselected. Please re-submit form");
			document.application.Media[0].checked = false;
			document.application.Media[1].checked = false;
			document.application.DataFile[0].checked = false;
			document.application.DataFile[1].checked = false;
			document.application.ListofNNDCodes.value = "";
			document.application.Media[0].focus();
			return false;
		}
		else
		{
			return true;
		}
	}

	// Check that a meda type has been selected
	var blnSelectedMedia = false;
	for(intCount=0; intCount < document.application.Media.length; intCount++)
	{
		if (document.application.Media[intCount].checked == true)
		{
			blnSelectedMedia = true;
			break;
		}
	}

	if (blnSelectedMedia == false)
	{
		alert("Please select a media type");
		document.application.Media[0].focus();
		return false;
	}

	if ((document.application.ApplicationType[0].checked == false) && (document.application.Media[1].checked == true))
	{
		alert("Can only have CDROM with Full License");
		document.application.Media[1].focus();
		return false;
	}


	if ((document.application.DataFile[0].checked == false) && (document.application.Media[1].checked == true))
	{
		alert("Can only have CDROM with Full Data File");
		document.application.Media[1].focus();
		return false;
	}

	// Check for valid email address if email media selected
	//if (intCount == 1)
	//{
		// Check that the email address is valid
		//if (!ValidateEmail(document.application.EmailAddress, true))
			//return false;
	//}

	return true;
}

function ValidateDataSelection()
{
	// Check that a meda type has been selected
	var blnDataSelection = false;

	for(intCount=0; intCount < document.application.DataSelection.length; intCount++)
	{
		if (document.application.DataSelection[intCount].checked == true)
		{
			blnDataSelection = true;
			break;
		}
	}

	if (blnDataSelection == false)
	{
		alert("Please select a data type(D)");
		document.application.DataSelection[0].focus();
		return false;
	}

	
	return true;
}

function ValidateDataFileType()
{
	//No File type for Associate Licence
	if (document.application.ApplicationType[2].checked == true)
	{
		if ((document.application.DataFile[0].checked == true) || (document.application.DataFile[1].checked == true))
		{
			alert("No Data File available for Associate License - Data File has been deselected. Please re-submit form");
			document.application.DataFile[0].checked = false;
			document.application.DataFile[1].checked = false;
			document.application.Media[0].checked = false;
			document.application.Media[1].checked = false;
			document.application.ListofNNDCodes.value = "";
			document.application.Datafile[0].focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	// Check that a data file type has been selected
	var blnSelectedDFT = false;
	for(intCount=0; intCount < document.application.DataFile.length; intCount++)
	{
		if (document.application.DataFile[intCount].checked == true)
		{
			blnSelectedDFT = true;
			break;
		}
	}

	if (blnSelectedDFT == false)
	{
		alert("Please select a data file type");
		document.application.DataFile[0].focus();
		return false;
	}

	// If full data file NND codes list must be empty
	if (intCount == 0)
	{
		if (document.application.ListofNNDCodes.value.length > 0)
		{
			alert("Please select partial data file if specifying NND codes");
			document.application.DataFile[1].focus();
			return false;
		}
	}

	// If partial data file validate NND codes list
	if (intCount == 1)
	{
		if (!ValidateNNDCodes(document.application.ListofNNDCodes))
			return false;
	}

	return true;
}

function ValidateNNDCodes(ctlNNDCodes)
{
	if (ctlNNDCodes.value.length == 0)
	{
		alert("Please specify NND codes for partial data file");
		ctlNNDCodes.focus();
		return false;
	}
	else if (ctlNNDCodes.value.search(/[^\d\r\n ]/) != -1)
	{
		alert("Please specify a valid list of NND codes");
		ctlNNDCodes.focus();
		return false;
	}

	var arrNNDCodes = ctlNNDCodes.value.split("\r\n");
	//alert(ctlNNDCodes.value);	
	//alert(arrNNDCodes.length);
	for(intCount=0; intCount < arrNNDCodes.length; intCount++)
	{
		
		if ((intCount != arrNNDCodes.length-1) && (arrNNDCodes[intCount].length < 2))
		{
			alert("NND codes cannot contain less than 2 digits");
			ctlNNDCodes.focus();
			return false;
		} 
		else if (arrNNDCodes[intCount].length > 10)
		{
			alert("NND codes cannot contain more than 10 digits");
			ctlNNDCodes.focus();
			return false;
		}
	}

	return true;
}

function ValidatePostcodes(ctlNNDCodes)
{
	if (ctlNNDCodes.value.length == 0)
	{
		alert("Please specify some postcodes for partial data file");
		ctlNNDCodes.focus();
		return false;
	}

	return true;
}

function ValidateRegisterForm()
{
	// Check that the name has a value
	if (document.register.NameOfPerson.value.length == 0)
	{
		alert("Please specify your name");
		document.register.NameOfPerson.focus();
		return false;
	}

	// Check that address 1 has a value
	if (document.register.Address1.value.length == 0)
	{
		alert("Please specify your address");
		document.register.Address1.focus();
		return false;
	}

	// Check that the town has a value
	if (document.register.Address3.value.length == 0)
	{
		alert("Please specify your town");
		document.register.Address3.focus();
		return false;
	}

	if (!ValidatePostcode(document.register.PostCode, true))
		return false;

	if (!ValidateTelephone(document.register.TelephoneNo, true))
		return false;

	return true;
}

function ValidateMPSRegisterForm()
{
	// Check that the name has a value
	if (document.register.NameOfPerson.value.length == 0)
	{
		alert("Please specify your name");
		document.register.NameOfPerson.focus();
		return false;
	}

	// Check that address 1 has a value
	if (document.register.Address1.value.length == 0)
	{
		alert("Please specify your address");
		document.register.Address1.focus();
		return false;
	}

	// Check that the town has a value
	if (document.register.Address4.value.length == 0)
	{
		alert("Please specify your town");
		document.register.Address4.focus();
		return false;
	}

	// Check that the county has a value
	//if (document.register.Address5.value.length == 0)
	//{
	//	alert("Please specify your county");
	//	document.register.Address5.focus();
	//	return false;
	//}

	if (!ValidatePostcode(document.register.PostCode, true))
		return false;

	var blnSelectedType = false;
	for(intCount=0; intCount < document.register.Method.length; intCount++)
	{
		if (document.register.Method[intCount].checked == true)
		{
			blnSelectedType = true;
			if(intCount == 0 && document.register.Pub.value == 1)
			{
				alert("Please select a publication");
				return false;
			}

			if (intCount == 5 && document.register.OtherDetails.value == "")
			{
				alert("Please complete the details text box");
				return false;
			}
			break;
		}
	}

	if (blnSelectedType == false)
	{
		alert("Please let us know where you heard about the MPS");
		document.register.Method[0].focus();
		return false;
	}

	return true;
}

function ValidateCheckboxes()
{
	// Check that at least one of the category checkboxes have been selected
	var blnSelectedBox = false;

	if (document.register.Home.checked == true)
		blnSelectedBox = true;
	
	if (document.register.Leisure.checked == true)
		blnSelectedBox = true;
	
	if (document.register.Clothing.checked == true)
		blnSelectedBox = true;
	
	if (document.register.Financial.checked == true)
		blnSelectedBox = true;
	
	if (document.register.Sport.checked == true)
		blnSelectedBox = true;
	
	if (document.register.Travel.checked == true)
		blnSelectedBox = true;
	
	if (document.register.Children.checked == true)
		blnSelectedBox = true;

	if (document.register.ComServices.checked == true)
		blnSelectedBox = true;
	
	if (blnSelectedBox == false)
	{
		alert("You must select at least one category");
		return false;
	}


	return true;
}

function ValidateContactDetails()
{
	// Check that the company name has a value
	if (document.application.CompanyName.value.length == 0)
	{
		alert("Please specifiy your company name");
		document.application.CompanyName.focus();
		return false;
	}

	// Check that the address1 has a value
	if (document.application.Address1.value.length == 0)
	{
		alert("Please specifiy at least two address lines");
		document.application.Address1.focus();
		return false;
	}

	// Check that the address2 has a value
	if (document.application.Address2.value.length == 0)
	{
		alert("Please specifiy at least two address lines");
		document.application.Address2.focus();
		return false;
	}

	// Check that the postcode is valid
	if (!ValidatePostcode(document.application.Postcode, true))
		return false;

	// Check that the telephone muber is valid
	if (!ValidateTelephone(document.application.Telephoneno, true))
		return false;
	
	// Check that the fax number is valid
	if (!ValidateTelephone(document.application.Faxno, false, "", "Please specifiy a valid fax number"))
		return false;
	
	// Check that the email address is valid
	if (!ValidateEmail(document.application.EmailAddress, true))
		return false;
	
	// Check that the contact name has a value
	if (document.application.ContactName.value.length == 0)
	{
		alert("Please specifiy your contact name");
		document.application.ContactName.focus();
		return false;
	}

	return true;
}


function ValidateBMPSRegisterForm()
{
	var dDate;
	dDate = new Date();
	dDate.setMonth((document.register.select_month.value-1));
	dDate.setYear(document.register.select_year.value);
	dDate.setDate(document.register.select_day.value);
		
	// Check that Due Date is a valid date
	//if (dDate.getMonth()+1 != document.register.select_month.value)
	//{
	//	alert("Please specify a valid due date");
	//	document.register.select_day.focus();
	//	return false;	
	//}

	// Check that the forename/inits has a value
	if (document.register.ForeInits.value.length == 0)
	{
		alert("Please specify your Forename / Initials");
		document.register.ForeInits.focus();
		return false;
	}

	// Check that the name has a value
	if (document.register.NameOfPerson.value.length == 0)
	{
		alert("Please specify your Surname");
		document.register.NameOfPerson.focus();
		return false;
	}

	// Check that address 1 has a value
	if (document.register.Address1.value.length == 0)
	{
		alert("Please specify your address");
		document.register.Address1.focus();
		return false;
	}

	// Check that the town has a value
	if (document.register.Address4.value.length == 0)
	{
		alert("Please specify your town");
		document.register.Address4.focus();
		return false;
	}

	// Check that the county has a value
	//if (document.register.Address5.value.length == 0)
	//{
	//	alert("Please specify your county");
	//	document.register.Address5.focus();
	//	return false;
	//}

	if (!ValidatePostcode(document.register.PostCode, true))
		return false;

	var blnSelectedType = false;
	
	alert(document.register.Method.length);
	for(intCount=0; intCount < document.register.Method.length; intCount++)
	{
		
		if (document.register.Method[intCount].checked == true)
		{	
			alert("Selected one");
			blnSelectedType = true;
	
		// Removed as there is no publication combo on the form
		// 
		//	if(intCount == 0 && document.register.Pub.value == 1)
		//	{
		//		alert("Please select a publication");
		//		return false;
		//	}
	
			if (intCount == 6 && document.register.OtherDetails.value == "")
			{
				alert("Please complete the details text box");
				return false;
			}
			break;
		}
	}

	if (blnSelectedType == false)
	{
		alert("Please let us know where you heard about the MPS");
		document.register.Method[0].focus();
		return false;
	}

	return true;
}