//------------------------------------------------------------------------------------------------------------------------------------------------------------------------>

// 16-05-2003 | David Polinelli | Generic Form Validation function

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------>

//

// Checks the following:

// ----------------------------

//

//	+	R 		=	REQUIRED, 	that the field contains data

//	+	NUM		= 	NUMBER,		that the entered data is a number

//	+	EMAIL	=	EMAIL,		that the entered data is a properly formated email address, thus contains '@' and at least one '.' 	eg. name@domain.com.au

//	+	TEXT	=	TEXT,		that the entered data is text

//	+	URL		=	URL,		that the entered data is a properly formated URL, thus containing 'http://' and at least one '.'	eg. http://www.domain.com.au

//	+	PHONE	=	PHONE NUM,	that the entered data is a properly formated phone number, thus containing '(' area_code ')'.		eg. (08) 9555 1234 or (+61) 0408 555 1234

//

//

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------>



function checkForm() 

	{



	var theTags=checkForm.arguments;

	var theForm=theTags[0];

	var elementHiLite=0;

	

	ErrorMSG = "Some of the required fields have been left empty, fields requiring attention have been highlighted in grey.\n\n";

	ErrorMSG2 = "";

	

	// Repeat for each form element in "theTags", excluding the first [form name]

	for (i=1; i<(theTags.length); i=i+2)

		{

			var j = i + 1

			

			// Create a local variable with the form element name

			var theElement=theTags[i];

			

			// Create a local variable with the type of check

			var theChecker=theTags[j];

			

			// split the incoming parameter into an array based on the presne ceof the '-' character

			action = theChecker.split("-");

			

			document.forms[theForm].elements[theElement].style.background='#FFFFFF';

			

			// if the field is required then check that it contains data then set the ToCheck varaible to the type to check	

			if(action[0] == "R")

				{

					if(document.forms[theForm].elements[theElement].value == "")

						{

							if(elementHiLite == 0)

								{

									elementHiLite=theTags[i];

								}



							document.forms[theForm].elements[theElement].style.background='#CCCCCC';

						}

					

					var ToCheck = action[1];

				}

			else

				{

					var ToCheck = action[0];

				}





					

			switch(ToCheck)

				{

					//---------------------------------------------------------------------------------------------->

					// 15-05-2003 | David Polinelli | Check that the field contents is a NUMBER

					//---------------------------------------------------------------------------------------------->

					case "NUM":

					

					if(document.forms[theForm].elements[theElement].value != "")

						{

							fieldName = (document.forms[theForm].elements[theElement].name);

						

							if(isNaN(document.forms[theForm].elements[theElement].value))

								{

									ErrorMSG2 += "  - ";

									ErrorMSG2 += "Not a valid number:\n";

									

								if(elementHiLite == 0)

									{

										elementHiLite=theTags[i];

									}

								

									document.forms[theForm].elements[theElement].style.background='#CCCCCC';

								}

						}

						

					break;

					//---------------------------------------------------------------------------------------------->

					// 15-05-2003 | David Polinelli | Check that the field contents is a properly formated EMAIL

					//---------------------------------------------------------------------------------------------->

					case "EMAIL":

					

					if(document.forms[theForm].elements[theElement].value != "")

						{

							// check if @ or the '.' is missing

							if(document.forms[theForm].elements[theElement].value.indexOf('@') == -1 || document.forms[theForm].elements[theElement].value.indexOf('.') == -1)

								{

									ErrorMSG2 += "  - ";

									ErrorMSG2 += "Email format incorrect.     try: me@domain.com.au\n";

									

									if(elementHiLite == 0)

										{

											elementHiLite=theTags[i];

										}

									document.forms[theForm].elements[theElement].style.background='#CCCCCC';

								}

						}

						

					break;

					//---------------------------------------------------------------------------------------------->

					// 15-05-2003 | David Polinelli | Check that the field contents is a properly formated URL

					//---------------------------------------------------------------------------------------------->

					case "URL":

					

					if(document.forms[theForm].elements[theElement].value != "")

						{

							// check if http:// or the '.' is missing

							if(document.forms[theForm].elements[theElement].value.indexOf('http://') == -1)

								{

									ErrorMSG2 += "  - ";

									ErrorMSG2 += "URL format incorrect.       try: http://www.domain.com.au\n";

									

									if(elementHiLite == 0)

										{

											elementHiLite=theTags[i];

										}

									document.forms[theForm].elements[theElement].style.background='#CCCCCC';

								}

						}

					

					break;

					//---------------------------------------------------------------------------------------------->

					// 15-05-2003 | David Polinelli | Check that the field contents is a properly formated PHONE NUM

					//---------------------------------------------------------------------------------------------->

					case "PHONE":

					

					if(document.forms[theForm].elements[theElement].value != "")

						{

							// check if '(' or the ')' is missing

							if(document.forms[theForm].elements[theElement].value.indexOf('(') == -1 || document.forms[theForm].elements[theElement].value.indexOf(')') == -1)

								{

									ErrorMSG2 += "  - ";

									ErrorMSG2 += "Phone format incorrect.    try: (08)/(+61) 9555 1234\n";

									

									if(elementHiLite == 0)

										{

											elementHiLite=theTags[i];

										}

									document.forms[theForm].elements[theElement].style.background='#CCCCCC';

								}

						}

						

					break;

					//---------------------------------------------------------------------------------------------->

					// 15-05-2003 | David Polinelli | Check that the field contents is TEXT

					//---------------------------------------------------------------------------------------------->

					case "TEXT":

					

					if(document.forms[theForm].elements[theElement].value != "")

						{

							fieldName = (document.forms[theForm].elements[theElement].name);

						

							if(!isNaN(document.forms[theForm].elements[theElement].value))

								{

									ErrorMSG2 += "  - ";

									ErrorMSG2 += fieldName.toUpperCase();

									ErrorMSG2 += " is not text\n";

									

								if(elementHiLite == 0)

									{

										elementHiLite=theTags[i];

									}

								

									document.forms[theForm].elements[theElement].style.background='#CCCCCC';

								}

						}

						

					break;

					//---------------------------------------------------------------------------------------------->

					// 15-05-2003 | David Polinelli | Check that the field contents is MONEY

					//---------------------------------------------------------------------------------------------->

					case "MONEY":

					

					if(document.forms[theForm].elements[theElement].value != "")

						{

							// check if '$' is missing

							if(document.forms[theForm].elements[theElement].value.indexOf('$') == -1)

								{

									ErrorMSG2 += "  - ";

									ErrorMSG2 += "Money format incorrect.    try: $8594.83\n";

									

									if(elementHiLite == 0)

										{

											elementHiLite=theTags[i];

										}

									document.forms[theForm].elements[theElement].style.background='#CCCCCC';

								}

						}

						

					break;

					//---------------------------------------------------------------------------------------------->

				}

		}

	

	if(elementHiLite == 0)

		{

				return true;

		}

	else

		{

			if(ErrorMSG2 != "")

				{

					ErrorMSG += "In addition the following fields have been flagged as containing errors:\n\n";

					ErrorMSG += ErrorMSG2;

				}

			

			// display the alert dialog box showing the errors that were found

			alert(ErrorMSG);

		

			document.forms[theForm].elements[elementHiLite].focus();



			return false;

		}

	} 

