// characters not allowed in a name
var noCharName = "`~1234567890!@#$%^*()_=+[{]}\\|;:\",<.>/?";

// characters not allowed in a title
var noCharTitle = "`~1234567890!@#$%^*_=+[]{}\\|:;\"<>/?";

// characters not allowed in a company name
var noCharCompany = "`~!@#$%^*_=+[{]}\\|;<>?";

// characters not allowed in a phone/fax number
var noCharPhone = "`~!@$%^&_=qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM[{]}\\|;:'\",<.>/?";

// characters not allows in a number
var noCharNum = "`~!@#$%^&*()-_=+[]\\{}|;':\",./<>?qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";

// characters not allowed in an email address
var noCharEmail = " ";

// all non-letter characters
var nonLetters = "`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",./<>?";

function CheckFields()
{
	// check individual fields

	for (var i = 0; i < formFields.length; i++)
	{

		// check mandatory field entered

		if (document.KardiniaForm.elements[i].value.length == 0 && (formFields[i].minimumLength > 0))
		{
			alert("You must complete the field \"" + formFields[i].name + "\".");
			document.KardiniaForm.elements[i].focus();
			return;
		}

		// check minimumLength

		if (document.KardiniaForm.elements[i].value.length < formFields[i].minimumLength && (formFields[i].minimumLength != 1))
		{
			alert("You must enter a minimum of " + formFields[i].minimumLength + " characters in the field \"" + formFields[i].name + "\".");
			document.KardiniaForm.elements[i].focus();
			return;
		}

		// check maximumLength

		if ((document.KardiniaForm.elements[i].value.length > formFields[i].maximumLength) && (formFields[i].maximumLength != null))
		{
			alert("You must enter a maximum of " + formFields[i].maximumLength + " characters in the field \"" + formFields[i].name + "\".");
			document.KardiniaForm.elements[i].focus();
			return;
		}

		// check for disallowed characters

		for (var j = 0; j < formFields[i].disallowedCharacters.length; j++)
		{
			if (document.KardiniaForm.elements[i].value.indexOf(formFields[i].disallowedCharacters.charAt(j)) > -1)
			{
				alert("You have entered invalid characters in the field \"" + formFields[i].name + "\". Invalid characters are:\n" + formFields[i].disallowedCharacters);
				document.KardiniaForm.elements[i].focus();
				return;
			}
		}

		// check for disallowed values

		for (var j = 0; j < formFields[i].disallowedValues.length; j++)
		{
			if (document.KardiniaForm.elements[i].value == formFields[i].disallowedValues[j])
			{
				alert("You have entered the invalid value \"" + formFields[i].disallowedValues[j] + "\" in the field \"" + formFields[i].name + "\".");
				document.KardiniaForm.elements[i].focus();
				return;
			}
		}

		// check compulsory fields

		if (document.KardiniaForm.elements[i].value != "")
		{
			for (var j = 0; j < formFields[i].compulsoryFieldsWith.length; j++)
			{
				if (document.KardiniaForm.elements[formFields[i].compulsoryFieldsWith[j]].value == "")
				{
					alert("If you fill in the \"" + formFields[i].name + "\" field you must complete the \"" + formFields[formFields[i].compulsoryFieldsWith[j]].name + "\" field.");
					document.KardiniaForm.elements[formFields[i].compulsoryFieldsWith[j]].focus();
					return;
				}
			}
		}

		else
		{
			for (var j = 0; j < formFields[i].compulsoryFieldsWithout.length; j++)
			{
				if (document.KardiniaForm.elements[formFields[i].compulsoryFieldsWithout[j]].value == "")
				{
					alert("If you do not fill in the \"" + formFields[i].name + "\" field you must complete the \"" + formFields[formFields[i].compulsoryFieldsWithout[j]].name + "\" field.");
					document.KardiniaForm.elements[formFields[i].compulsoryFieldsWithout[j]].focus();
					return;
				}
			}
		}

		// do email check if relevant

		if (formFields[i].isEmail)
		{

			// if there is no "@" in the address
			if (document.KardiniaForm.elements[i].value.indexOf("@") == -1)
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}

			// if there is more than one "@"
			else if (document.KardiniaForm.elements[i].value.indexOf("@", document.KardiniaForm.elements[i].value.indexOf("@") + 1) > -1)
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}

			// if there is no "." after the "@"
			else if (document.KardiniaForm.elements[i].value.indexOf(".", document.KardiniaForm.elements[i].value.indexOf("@") + 1) == -1)
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}

			// if the "." is the last character in the address
			else if (document.KardiniaForm.elements[i].value.charAt(document.KardiniaForm.elements[i].value.length - 1) == '.')
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}
		}

		// do number check if relevant

		if (formFields[i].isNumber)
		{
			// check minimum value
			if (document.KardiniaForm.elements[i].value < formFields[i].minimumValue)
			{
				alert("The minimum value you can enter in the \"" + formFields[i].name + "\" field is " + formFields[i].minimumValue + ".");
				document.KardiniaForm.elements[i].focus();
				return;
			}

			// check maximum value
			if ((document.KardiniaForm.elements[i].value > formFields[i].maximumValue) && (formFields[i].maximumValue != null))
			{
				alert("The maximum value you can enter in the \"" + formFields[i].name + "\" field is " + formFields[i].maximumValue + ".");
				document.KardiniaForm.elements[i].focus();
				return;
			}
		}
	}

	// check Option Sets

	for (var i = 0; i < optionSets.length; i++)
	{
		var numSelected = 0;
		for (var j = 0; j < optionSets[i].fields.length; j++)
		{
			if (optionSets[i].useCheckedAttribute)
			{
				if (document.KardiniaForm.elements[optionSets[i].fields[j]].checked != optionSets[i].unselectedValue)
				{
					numSelected++;
				}
			}
			else
			{
				if (document.KardiniaForm.elements[optionSets[i].fields[j]].value != optionSets[i].unselectedValue)
				{
					numSelected++;
				}
			}
		}
		if ((numSelected < optionSets[i].minimumSelected) || ((numSelected > optionSets[i].maximumSelected) && (optionSets[i].maximumSelected != null)))
		{
			alert(optionSets[i].errorMessage);
			document.KardiniaForm.elements[optionSets[i].fields[0]].focus();
			return;
		}
	}

	// if all checks have been passed
	document.KardiniaForm.action = "formsub.asp";
	document.KardiniaForm.submit();
}

function NetscapeCheck()
{
	for (var i = 0; i < formFields.length; i++)
	{
		if ((formFields[i].minimumLength > 0) && (document.KardiniaForm.elements[i].value == ""))
		{
			alert("You must complete the \"" + formFields[i].name + "\" field.");
			document.KardiniaForm.elements[i].focus();
			return;
		}
		if (formFields[i].isEmail)
		{
			// if there is no "@" in the address
			if (document.KardiniaForm.elements[i].value.indexOf("@") == -1)
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}

			// if there is more than one "@"
			else if (document.KardiniaForm.elements[i].value.indexOf("@", document.KardiniaForm.elements[i].value.indexOf("@") + 1) > -1)
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}

			// if there is no "." after the "@"
			else if (document.KardiniaForm.elements[i].value.indexOf(".", document.KardiniaForm.elements[i].value.indexOf("@") + 1) == -1)
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}

			// if the "." is the last character in the address
			else if (document.KardiniaForm.elements[i].value.charAt(document.KardiniaForm.elements[i].value.length - 1) == '.')
			{
				alert("You have entered an invalid E-mail address.");
				document.KardiniaForm.elements[i].focus();
				return;
			}
		}
	}

	// if all checks pass

	document.KardiniaForm.action = "formsub.asp";
	document.KardiniaForm.submit();
}

function Field(nameForUser, minLen, badChars)
{
	this.name = nameForUser;
	this.minimumLength = minLen;
	this.disallowedCharacters = badChars;
	this.disallowedValues = new Array();
	this.compulsoryFieldsWith = new Array();
	this.compulsoryFieldsWithout = new Array();
}

new Field("dummy value", 0, ""); // force creation of prototype object in Netscape 3 (unnecessary for other browsers)

Field.prototype.maximumLength = null;
// Field.prototype.disallowedValues = new Array(); // class types cannot be set for prototype as they are referred to by reference and become the equivalent of static variables here
// Field.prototype.compulsoryFieldsWith = new Array();
// Field.prototype.compulsoryFieldsWithout = new Array();
Field.prototype.isEmail = false;
Field.prototype.isNumber = false;
Field.prototype.minimumValue = null;
Field.prototype.maximumValue = null;

function OptionSet(elements, minSel, maxSel, errorMsg)
{
	this.fields = arrayCopy(elements);
	this.minimumSelected = minSel;
	this.maximumSelected = maxSel;
	this.errorMessage = errorMsg;
}

new OptionSet(new Array(), 0, 0, ""); // force creation of prototype object in Netscape 3 (unnecessary for other browsers)

OptionSet.prototype.unselectedValue = "";
OptionSet.prototype.useCheckedAttribute = false;

function arrayCopy(originalArray)
{
	var newArray = new Array();
	if (originalArray == null)
	{
		return newArray;
	}
	else
	{
		for (var i = 0; i < originalArray.length; i++)
		{
			newArray[i] = originalArray[i];
		}
		return newArray;
	}
}

function checkForNetscape()
{
	if ((navigator.appName.toLowerCase().indexOf("netscape") > -1) && (navigator.appVersion.substring(0, 1) >= 5))
	{
		// Netscape 6 or later
	}
	else if (navigator.appName.toLowerCase().indexOf("internet explorer") > -1)
	{
		// Internet Explorer
	}
	else
	{
		// Netscape 4.7 or other browser
		document.KardiniaForm.action = "javascript:NetscapeCheck();";
	}
}

var selectNum = 0;

function otherCheck()
{
	if (selectNum == 0)
	{
		alert("Please wait until the page has finished loading.");
		return;
	}
	if (document.KardiniaForm.country.selectedIndex == selectNum - 1)
	{
		var newCountry = "";
		while (newCountry == "")
		{
			newCountry = window.prompt("Please enter the name of your country", "");
		}
		if (newCountry == null)
		{
			document.KardiniaForm.country.selectedIndex = 0;
			return;
		}
		if (document.KardiniaForm.country.options.length == selectNum)
		{
			document.KardiniaForm.country.options.length++;
		}
		document.KardiniaForm.country.options[selectNum].text = newCountry;
		document.KardiniaForm.country.options[selectNum].value = newCountry;
		document.KardiniaForm.country.selectedIndex = selectNum;
	}
}
